source: flex_extract.git/documentation/Sphinx/build/html/_modules/get_mars_data.html @ f66f990

dev
Last change on this file since f66f990 was f66f990, checked in by Anne Philipp <anne.philipp@…>, 7 months ago

deleted jobscripts for tracking and updated Sphinx doc

  • Property mode set to 100644
File size: 39.9 KB
Line 
1
2
3<!DOCTYPE html>
4<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
5<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
6<head>
7  <meta charset="utf-8">
8 
9  <meta name="viewport" content="width=device-width, initial-scale=1.0">
10 
11  <title>get_mars_data &mdash; flex_extract 7.1 alpha documentation</title>
12 
13
14 
15 
16 
17 
18
19 
20
21 
22 
23   
24
25 
26
27  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
28  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
29    <link rel="index" title="Index" href="../genindex.html" />
30    <link rel="search" title="Search" href="../search.html" /> 
31
32 
33  <script src="../_static/js/modernizr.min.js"></script>
34
35</head>
36
37<body class="wy-body-for-nav">
38
39   
40  <div class="wy-grid-for-nav">
41
42   
43    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
44      <div class="wy-side-scroll">
45        <div class="wy-side-nav-search">
46         
47
48         
49            <a href="../index.html" class="icon icon-home"> flex_extract
50         
51
52         
53          </a>
54
55         
56           
57           
58              <div class="version">
59                7.1
60              </div>
61           
62         
63
64         
65<div role="search">
66  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
67    <input type="text" name="q" placeholder="Search docs" />
68    <input type="hidden" name="check_keywords" value="yes" />
69    <input type="hidden" name="area" value="default" />
70  </form>
71</div>
72
73         
74        </div>
75
76        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
77         
78           
79           
80             
81           
82           
83              <p class="caption"><span class="caption-text">Table of Contents:</span></p>
84<ul>
85<li class="toctree-l1"><a class="reference internal" href="../downloads.html">Downloads</a></li>
86<li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a></li>
87<li class="toctree-l1"><a class="reference internal" href="../program_structure.html">Program Structure</a></li>
88<li class="toctree-l1"><a class="reference internal" href="../user_guide.html">User Guide</a></li>
89<li class="toctree-l1"><a class="reference internal" href="../api.html">Auto Generated Documentation</a></li>
90<li class="toctree-l1"><a class="reference internal" href="../support.html">Support</a></li>
91</ul>
92
93           
94         
95        </div>
96      </div>
97    </nav>
98
99    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
100
101     
102      <nav class="wy-nav-top" aria-label="top navigation">
103       
104          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
105          <a href="../index.html">flex_extract</a>
106       
107      </nav>
108
109
110      <div class="wy-nav-content">
111       
112        <div class="rst-content">
113       
114         
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130<div role="navigation" aria-label="breadcrumbs navigation">
131
132  <ul class="wy-breadcrumbs">
133   
134      <li><a href="../index.html">Docs</a> &raquo;</li>
135       
136          <li><a href="index.html">Module code</a> &raquo;</li>
137       
138      <li>get_mars_data</li>
139   
140   
141      <li class="wy-breadcrumbs-aside">
142       
143      </li>
144   
145  </ul>
146
147 
148  <hr/>
149</div>
150          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
151           <div itemprop="articleBody">
152           
153  <h1>Source code for get_mars_data</h1><div class="highlight"><pre>
154<span></span><span class="ch">#!/usr/bin/env python</span>
155<span class="c1"># -*- coding: utf-8 -*-</span>
156<span class="c1">#*******************************************************************************</span>
157<span class="c1"># @Author: Anne Fouilloux (University of Oslo)</span>
158<span class="c1">#</span>
159<span class="c1"># @Date: October 2014</span>
160<span class="c1">#</span>
161<span class="c1"># @Change History:</span>
162<span class="c1">#</span>
163<span class="c1">#    November 2015 - Leopold Haimberger (University of Vienna):</span>
164<span class="c1">#        - moved the getEIdata program into a function &quot;get_mars_data&quot;</span>
165<span class="c1">#        - moved the AgurmentParser into a seperate function</span>
166<span class="c1">#        - adatpted the function for the use in flex_extract</span>
167<span class="c1">#        - renamed file to get_mars_data</span>
168<span class="c1">#</span>
169<span class="c1">#    February 2018 - Anne Philipp (University of Vienna):</span>
170<span class="c1">#        - applied PEP8 style guide</span>
171<span class="c1">#        - added structured documentation</span>
172<span class="c1">#        - minor changes in programming style for consistence</span>
173<span class="c1">#        - added function main and moved function calls vom __main__ there</span>
174<span class="c1">#          (necessary for better documentation with docstrings for later</span>
175<span class="c1">#          online documentation)</span>
176<span class="c1">#        - use of UIFiles class for file selection and deletion</span>
177<span class="c1">#        - seperated get_mars_data function into several smaller pieces:</span>
178<span class="c1">#          write_reqheader, mk_server, mk_dates, remove_old, do_retrievment</span>
179<span class="c1">#</span>
180<span class="c1"># @License:</span>
181<span class="c1">#    (C) Copyright 2014-2019.</span>
182<span class="c1">#    Anne Philipp, Leopold Haimberger</span>
183<span class="c1">#</span>
184<span class="c1">#    This work is licensed under the Creative Commons Attribution 4.0</span>
185<span class="c1">#    International License. To view a copy of this license, visit</span>
186<span class="c1">#    http://creativecommons.org/licenses/by/4.0/ or send a letter to</span>
187<span class="c1">#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.</span>
188<span class="c1">#*******************************************************************************</span>
189<span class="sd">&#39;&#39;&#39;This script extracts MARS data from ECMWF servers.</span>
190
191<span class="sd">At first, the necessary parameters from command line and CONTROL files are</span>
192<span class="sd">extracted. They define the data set to be extracted from MARS.</span>
193
194<span class="sd">This file can also be imported as a module and contains the following</span>
195<span class="sd">functions:</span>
196
197<span class="sd">    * main - the main function of the script</span>
198<span class="sd">    * get_mars_data - overall control of ECMWF data retrievment</span>
199<span class="sd">    * write_reqheader - writes the header into the mars_request file</span>
200<span class="sd">    * mk_server - creates the server connection to ECMWF servers</span>
201<span class="sd">    * mk_dates - defines the start and end date</span>
202<span class="sd">    * remove_old - deletes old retrieved grib files</span>
203<span class="sd">    * do_retrievement - creates individual retrievals</span>
204
205<span class="sd">Type: get_mars_data.py --help</span>
206<span class="sd">to get information about command line parameters.</span>
207<span class="sd">Read the documentation for usage instructions.</span>
208<span class="sd">&#39;&#39;&#39;</span>
209<span class="c1"># ------------------------------------------------------------------------------</span>
210<span class="c1"># MODULES</span>
211<span class="c1"># ------------------------------------------------------------------------------</span>
212<span class="kn">import</span> <span class="nn">os</span>
213<span class="kn">import</span> <span class="nn">sys</span>
214<span class="kn">import</span> <span class="nn">inspect</span>
215<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timedelta</span>
216
217<span class="c1"># software specific classes and modules from flex_extract</span>
218<span class="c1"># add path to local main python path for flex_extract to get full access</span>
219<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span>
220    <span class="n">inspect</span><span class="o">.</span><span class="n">getfile</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">currentframe</span><span class="p">())))</span> <span class="o">+</span> <span class="s1">&#39;/../&#39;</span><span class="p">)</span>
221<span class="kn">import</span> <span class="nn">_config</span>
222<span class="kn">from</span> <span class="nn">tools</span> <span class="k">import</span> <span class="p">(</span><span class="n">setup_controldata</span><span class="p">,</span> <span class="n">my_error</span><span class="p">,</span> <span class="n">normal_exit</span><span class="p">,</span> <span class="n">get_cmdline_args</span><span class="p">,</span>
223                   <span class="n">read_ecenv</span><span class="p">,</span> <span class="n">make_dir</span><span class="p">)</span>
224<span class="kn">from</span> <span class="nn">classes.EcFlexpart</span> <span class="k">import</span> <span class="n">EcFlexpart</span>
225<span class="kn">from</span> <span class="nn">classes.UioFiles</span> <span class="k">import</span> <span class="n">UioFiles</span>
226<span class="kn">from</span> <span class="nn">classes.MarsRetrieval</span> <span class="k">import</span> <span class="n">MarsRetrieval</span>
227
228<span class="k">try</span><span class="p">:</span>
229    <span class="n">ec_api</span> <span class="o">=</span> <span class="kc">True</span>
230    <span class="kn">import</span> <span class="nn">ecmwfapi</span>
231<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
232    <span class="n">ec_api</span> <span class="o">=</span> <span class="kc">False</span>
233
234<span class="k">try</span><span class="p">:</span>
235    <span class="n">cds_api</span> <span class="o">=</span> <span class="kc">True</span>
236    <span class="kn">import</span> <span class="nn">cdsapi</span>
237<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
238    <span class="n">cds_api</span> <span class="o">=</span> <span class="kc">False</span>
239<span class="c1"># ------------------------------------------------------------------------------</span>
240<span class="c1"># FUNCTION</span>
241<span class="c1"># ------------------------------------------------------------------------------</span>
242<div class="viewcode-block" id="main"><a class="viewcode-back" href="../api.html#get_mars_data.main">[docs]</a><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
243    <span class="sd">&#39;&#39;&#39;Controls the program to get data out of mars.</span>
244
245<span class="sd">    This is done if it is called directly from command line.</span>
246<span class="sd">    Then it also takes program call arguments and control file input.</span>
247
248<span class="sd">    Parameters</span>
249<span class="sd">    ----------</span>
250
251<span class="sd">    Return</span>
252<span class="sd">    ------</span>
253
254<span class="sd">    &#39;&#39;&#39;</span>
255
256    <span class="n">c</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">setup_controldata</span><span class="p">()</span>
257    <span class="n">get_mars_data</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
258    <span class="n">normal_exit</span><span class="p">(</span><span class="s1">&#39;Retrieving MARS data: Done!&#39;</span><span class="p">)</span>
259
260    <span class="k">return</span></div>
261
262<div class="viewcode-block" id="get_mars_data"><a class="viewcode-back" href="../api.html#get_mars_data.get_mars_data">[docs]</a><span class="k">def</span> <span class="nf">get_mars_data</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
263    <span class="sd">&#39;&#39;&#39;Retrieves the EC data needed for a FLEXPART simulation.</span>
264
265<span class="sd">    Start and end dates for retrieval period is set. Retrievals</span>
266<span class="sd">    are divided into smaller periods if necessary and datechunk parameter</span>
267<span class="sd">    is set.</span>
268
269<span class="sd">    Parameters</span>
270<span class="sd">    ----------</span>
271<span class="sd">    c : ControlFile</span>
272<span class="sd">        Contains all the parameters of CONTROL file and</span>
273<span class="sd">        command line.</span>
274
275<span class="sd">    Return</span>
276<span class="sd">    ------</span>
277
278<span class="sd">    &#39;&#39;&#39;</span>
279    <span class="n">c</span><span class="o">.</span><span class="n">ec_api</span> <span class="o">=</span> <span class="n">ec_api</span>
280    <span class="n">c</span><span class="o">.</span><span class="n">cds_api</span> <span class="o">=</span> <span class="n">cds_api</span>
281
282    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">inputdir</span><span class="p">):</span>
283        <span class="n">make_dir</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">inputdir</span><span class="p">)</span>
284
285    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">request</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
286        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Retrieving EC data!&quot;</span><span class="p">)</span>
287    <span class="k">else</span><span class="p">:</span>
288        <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">request</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
289            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Printing mars requests!&quot;</span><span class="p">)</span>
290        <span class="k">elif</span> <span class="n">c</span><span class="o">.</span><span class="n">request</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
291            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Retrieving EC data and printing mars request!&quot;</span><span class="p">)</span>
292        <span class="n">write_reqheader</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">inputdir</span><span class="p">,</span> <span class="n">_config</span><span class="o">.</span><span class="n">FILE_MARS_REQUESTS</span><span class="p">))</span>
293
294    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;start date </span><span class="si">%s</span><span class="s2"> &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">start_date</span><span class="p">))</span>
295    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;end date </span><span class="si">%s</span><span class="s2"> &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">end_date</span><span class="p">))</span>
296
297    <span class="n">server</span> <span class="o">=</span> <span class="n">mk_server</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
298
299    <span class="c1"># if data are to be retrieved, clean up any old grib files</span>
300    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">request</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">c</span><span class="o">.</span><span class="n">request</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
301        <span class="n">remove_old</span><span class="p">(</span><span class="s1">&#39;*grb&#39;</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">inputdir</span><span class="p">)</span>
302
303    <span class="c1"># --------------  flux data ------------------------------------------------</span>
304    <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">datechunk</span> <span class="o">=</span> <span class="n">mk_dates</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
305    <span class="n">do_retrievement</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">server</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">datechunk</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
306
307    <span class="c1"># --------------  non flux data --------------------------------------------</span>
308    <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">datechunk</span> <span class="o">=</span> <span class="n">mk_dates</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
309    <span class="n">do_retrievement</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">server</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">datechunk</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
310
311    <span class="k">return</span></div>
312
313<div class="viewcode-block" id="write_reqheader"><a class="viewcode-back" href="../api.html#get_mars_data.write_reqheader">[docs]</a><span class="k">def</span> <span class="nf">write_reqheader</span><span class="p">(</span><span class="n">marsfile</span><span class="p">):</span>
314    <span class="sd">&#39;&#39;&#39;Writes header with column names into mars request file.</span>
315
316<span class="sd">    Parameters</span>
317<span class="sd">    ----------</span>
318<span class="sd">    marsfile : str</span>
319<span class="sd">        Path to the mars request file.</span>
320
321<span class="sd">    Return</span>
322<span class="sd">    ------</span>
323
324<span class="sd">    &#39;&#39;&#39;</span>
325    <span class="n">MR</span> <span class="o">=</span> <span class="n">MarsRetrieval</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
326    <span class="n">attrs</span> <span class="o">=</span> <span class="nb">vars</span><span class="p">(</span><span class="n">MR</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
327    <span class="k">del</span> <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;server&#39;</span><span class="p">]</span>
328    <span class="k">del</span> <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;public&#39;</span><span class="p">]</span>
329    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">marsfile</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
330        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;request_number&#39;</span> <span class="o">+</span> <span class="s1">&#39;, &#39;</span><span class="p">)</span>
331        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">attrs</span><span class="o">.</span><span class="n">iterkeys</span><span class="p">())))</span>
332        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
333
334    <span class="k">return</span></div>
335
336<div class="viewcode-block" id="mk_server"><a class="viewcode-back" href="../api.html#get_mars_data.mk_server">[docs]</a><span class="k">def</span> <span class="nf">mk_server</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
337    <span class="sd">&#39;&#39;&#39;Creates a server connection with available python API.</span>
338
339<span class="sd">    Which API is used depends on availability and the dataset to be retrieved.</span>
340<span class="sd">    The CDS API is used for ERA5 dataset no matter if the user is a member or</span>
341<span class="sd">    a public user. ECMWF WebAPI is used for all other available datasets.</span>
342
343<span class="sd">    Parameters</span>
344<span class="sd">    ----------</span>
345<span class="sd">    c : ControlFile</span>
346<span class="sd">        Contains all the parameters of CONTROL file and</span>
347<span class="sd">        command line.</span>
348
349<span class="sd">    Return</span>
350<span class="sd">    ------</span>
351<span class="sd">    server : ECMWFDataServer, ECMWFService or Client</span>
352<span class="sd">        Connection to ECMWF server via python interface ECMWF WebAPI or CDS API.</span>
353
354<span class="sd">    &#39;&#39;&#39;</span>
355    <span class="k">if</span> <span class="n">cds_api</span> <span class="ow">and</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">marsclass</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;EA&#39;</span><span class="p">):</span>
356        <span class="n">server</span> <span class="o">=</span> <span class="n">cdsapi</span><span class="o">.</span><span class="n">Client</span><span class="p">()</span>
357        <span class="n">c</span><span class="o">.</span><span class="n">ec_api</span> <span class="o">=</span> <span class="kc">False</span>
358    <span class="k">elif</span> <span class="n">c</span><span class="o">.</span><span class="n">ec_api</span><span class="p">:</span>
359        <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">public</span><span class="p">:</span>
360            <span class="n">server</span> <span class="o">=</span> <span class="n">ecmwfapi</span><span class="o">.</span><span class="n">ECMWFDataServer</span><span class="p">()</span>
361        <span class="k">else</span><span class="p">:</span>
362            <span class="n">server</span> <span class="o">=</span> <span class="n">ecmwfapi</span><span class="o">.</span><span class="n">ECMWFService</span><span class="p">(</span><span class="s2">&quot;mars&quot;</span><span class="p">)</span>
363        <span class="n">c</span><span class="o">.</span><span class="n">cds_api</span> <span class="o">=</span> <span class="kc">False</span>
364    <span class="k">else</span><span class="p">:</span>
365        <span class="n">server</span> <span class="o">=</span> <span class="kc">False</span>
366
367    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Using ECMWF WebAPI: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">ec_api</span><span class="p">))</span>
368    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Using CDS API: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">cds_api</span><span class="p">))</span>
369
370    <span class="k">return</span> <span class="n">server</span></div>
371
372
373<div class="viewcode-block" id="check_dates_for_nonflux_fc_times"><a class="viewcode-back" href="../api.html#get_mars_data.check_dates_for_nonflux_fc_times">[docs]</a><span class="k">def</span> <span class="nf">check_dates_for_nonflux_fc_times</span><span class="p">(</span><span class="n">types</span><span class="p">,</span> <span class="n">times</span><span class="p">):</span>
374    <span class="sd">&#39;&#39;&#39;</span>
375<span class="sd">    &#39;&#39;&#39;</span>
376    <span class="k">for</span> <span class="n">ty</span><span class="p">,</span> <span class="n">ti</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">types</span><span class="p">,</span><span class="n">times</span><span class="p">):</span>
377        <span class="k">if</span> <span class="n">ty</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;FC&#39;</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">ti</span><span class="p">)</span> <span class="o">==</span> <span class="mi">18</span><span class="p">:</span>
378            <span class="k">return</span> <span class="kc">True</span>
379    <span class="k">return</span> <span class="kc">False</span></div>
380
381
382<div class="viewcode-block" id="mk_dates"><a class="viewcode-back" href="../api.html#get_mars_data.mk_dates">[docs]</a><span class="k">def</span> <span class="nf">mk_dates</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fluxes</span><span class="p">):</span>
383    <span class="sd">&#39;&#39;&#39;Prepares start and end date depending on flux or non flux data.</span>
384
385<span class="sd">    If forecast for maximum one day (upto 24h) are to be retrieved, then</span>
386<span class="sd">    collect accumulation data (flux data) with additional days in the</span>
387<span class="sd">    beginning and at the end (used for complete disaggregation of</span>
388<span class="sd">    original period)</span>
389
390<span class="sd">    If forecast data longer than 24h are to be retrieved, then</span>
391<span class="sd">    collect accumulation data (flux data) with the exact start and end date</span>
392<span class="sd">    (disaggregation will be done for the exact time period with</span>
393<span class="sd">    boundary conditions)</span>
394
395<span class="sd">    Since for basetime the extraction contains the 12 hours upfront,</span>
396<span class="sd">    if basetime is 0, the starting date has to be the day before and</span>
397
398<span class="sd">    Parameters</span>
399<span class="sd">    ----------</span>
400<span class="sd">    c : ControlFile</span>
401<span class="sd">        Contains all the parameters of CONTROL file and</span>
402<span class="sd">        command line.</span>
403
404<span class="sd">    fluxes : boolean, optional</span>
405<span class="sd">        Decides if the flux parameter settings are stored or</span>
406<span class="sd">        the rest of the parameter list.</span>
407<span class="sd">        Default value is False.</span>
408
409<span class="sd">    Return</span>
410<span class="sd">    ------</span>
411<span class="sd">    start : datetime</span>
412<span class="sd">        The start date of the retrieving data set.</span>
413
414<span class="sd">    end : datetime</span>
415<span class="sd">        The end date of the retrieving data set.</span>
416
417<span class="sd">    chunk : datetime</span>
418<span class="sd">        Time period in days for one single mars retrieval.</span>
419
420<span class="sd">    &#39;&#39;&#39;</span>
421    <span class="n">start</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">start_date</span><span class="p">,</span> <span class="s1">&#39;%Y%m</span><span class="si">%d</span><span class="s1">&#39;</span><span class="p">)</span>
422    <span class="n">end</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">end_date</span><span class="p">,</span> <span class="s1">&#39;%Y%m</span><span class="si">%d</span><span class="s1">&#39;</span><span class="p">)</span>
423    <span class="n">chunk</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">date_chunk</span><span class="p">))</span>
424
425    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">basetime</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
426        <span class="n">start</span> <span class="o">=</span> <span class="n">start</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
427
428    <span class="k">if</span> <span class="ow">not</span> <span class="n">c</span><span class="o">.</span><span class="n">purefc</span> <span class="ow">and</span> <span class="n">fluxes</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">c</span><span class="o">.</span><span class="n">basetime</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
429        <span class="n">start</span> <span class="o">=</span> <span class="n">start</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
430        <span class="n">end</span> <span class="o">=</span> <span class="n">end</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
431
432    <span class="c1"># if we have non-flux forecast data starting at 18 UTC</span>
433    <span class="c1"># we need to start retrieving data one day in advance</span>
434    <span class="k">if</span> <span class="ow">not</span> <span class="n">fluxes</span> <span class="ow">and</span> <span class="n">check_dates_for_nonflux_fc_times</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">time</span><span class="p">):</span>
435        <span class="n">start</span> <span class="o">=</span> <span class="n">start</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
436
437    <span class="k">return</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">chunk</span></div>
438
439<div class="viewcode-block" id="remove_old"><a class="viewcode-back" href="../api.html#get_mars_data.remove_old">[docs]</a><span class="k">def</span> <span class="nf">remove_old</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">inputdir</span><span class="p">):</span>
440    <span class="sd">&#39;&#39;&#39;Deletes old retrieval files from current input directory</span>
441<span class="sd">    matching the pattern.</span>
442
443<span class="sd">    Parameters</span>
444<span class="sd">    ----------</span>
445<span class="sd">    pattern : str</span>
446<span class="sd">        The sub string pattern which identifies the files to be deleted.</span>
447
448<span class="sd">    inputdir : str, optional</span>
449<span class="sd">        Path to the directory where the retrieved data is stored.</span>
450
451<span class="sd">    Return</span>
452<span class="sd">    ------</span>
453
454<span class="sd">    &#39;&#39;&#39;</span>
455    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... removing old content of &#39;</span> <span class="o">+</span> <span class="n">inputdir</span><span class="p">)</span>
456
457    <span class="n">tobecleaned</span> <span class="o">=</span> <span class="n">UioFiles</span><span class="p">(</span><span class="n">inputdir</span><span class="p">,</span> <span class="n">pattern</span><span class="p">)</span>
458    <span class="n">tobecleaned</span><span class="o">.</span><span class="n">delete_files</span><span class="p">()</span>
459
460    <span class="k">return</span></div>
461
462
463<div class="viewcode-block" id="do_retrievement"><a class="viewcode-back" href="../api.html#get_mars_data.do_retrievement">[docs]</a><span class="k">def</span> <span class="nf">do_retrievement</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">server</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">delta_t</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
464    <span class="sd">&#39;&#39;&#39;Divides the complete retrieval period in smaller chunks and</span>
465<span class="sd">    retrieves the data from MARS.</span>
466
467<span class="sd">    Parameters</span>
468<span class="sd">    ----------</span>
469<span class="sd">    c : ControlFile</span>
470<span class="sd">        Contains all the parameters of CONTROL file and</span>
471<span class="sd">        command line.</span>
472
473<span class="sd">    server : ECMWFService or ECMWFDataServer</span>
474<span class="sd">            The server connection to ECMWF.</span>
475
476<span class="sd">    start : datetime</span>
477<span class="sd">        The start date of the retrieval.</span>
478
479<span class="sd">    end : datetime</span>
480<span class="sd">        The end date of the retrieval.</span>
481
482<span class="sd">    delta_t : datetime</span>
483<span class="sd">        Delta_t + 1 is the maximal time period of a single</span>
484<span class="sd">        retrieval.</span>
485
486<span class="sd">    fluxes : boolean, optional</span>
487<span class="sd">        Decides if the flux parameters are to be retrieved or</span>
488<span class="sd">        the rest of the parameter list.</span>
489<span class="sd">        Default value is False.</span>
490
491<span class="sd">    Return</span>
492<span class="sd">    ------</span>
493
494<span class="sd">    &#39;&#39;&#39;</span>
495
496    <span class="c1"># since actual day also counts as one day,</span>
497    <span class="c1"># we only need to add datechunk - 1 days to retrieval</span>
498    <span class="c1"># for a period</span>
499    <span class="n">delta_t_m1</span> <span class="o">=</span> <span class="n">delta_t</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
500
501    <span class="n">day</span> <span class="o">=</span> <span class="n">start</span>
502    <span class="k">while</span> <span class="n">day</span> <span class="o">&lt;=</span> <span class="n">end</span><span class="p">:</span>
503        <span class="n">flexpart</span> <span class="o">=</span> <span class="n">EcFlexpart</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fluxes</span><span class="p">)</span>
504        <span class="n">tmpday</span> <span class="o">=</span> <span class="n">day</span> <span class="o">+</span> <span class="n">delta_t_m1</span>
505        <span class="k">if</span> <span class="n">tmpday</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">:</span>
506            <span class="n">dates</span> <span class="o">=</span> <span class="n">day</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;/to/&quot;</span> <span class="o">+</span> \
507                    <span class="n">tmpday</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
508        <span class="k">else</span><span class="p">:</span>
509            <span class="n">dates</span> <span class="o">=</span> <span class="n">day</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;/to/&quot;</span> <span class="o">+</span> \
510                    <span class="n">end</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
511
512        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... retrieve &quot;</span> <span class="o">+</span> <span class="n">dates</span> <span class="o">+</span> <span class="s2">&quot; in dir &quot;</span> <span class="o">+</span> <span class="n">c</span><span class="o">.</span><span class="n">inputdir</span><span class="p">)</span>
513
514        <span class="k">try</span><span class="p">:</span>
515            <span class="n">flexpart</span><span class="o">.</span><span class="n">retrieve</span><span class="p">(</span><span class="n">server</span><span class="p">,</span> <span class="n">dates</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">public</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">request</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">inputdir</span><span class="p">)</span>
516        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
517            <span class="n">my_error</span><span class="p">(</span><span class="s1">&#39;MARS request failed&#39;</span><span class="p">)</span>
518
519        <span class="n">day</span> <span class="o">+=</span> <span class="n">delta_t</span>
520
521    <span class="k">return</span></div>
522
523<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
524    <span class="n">main</span><span class="p">()</span>
525</pre></div>
526
527           </div>
528           
529          </div>
530          <footer>
531 
532
533  <hr/>
534
535  <div role="contentinfo">
536    <p>
537        &copy; Copyright 2018, Anne Philipp and Leopold Haimberger.
538
539    </p>
540  </div>
541  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
542
543</footer>
544
545        </div>
546      </div>
547
548    </section>
549
550  </div>
551 
552
553
554 
555
556    <script type="text/javascript">
557        var DOCUMENTATION_OPTIONS = {
558            URL_ROOT:'../',
559            VERSION:'7.1 alpha',
560            LANGUAGE:'None',
561            COLLAPSE_INDEX:false,
562            FILE_SUFFIX:'.html',
563            HAS_SOURCE:  true,
564            SOURCELINK_SUFFIX: '.txt'
565        };
566    </script>
567      <script type="text/javascript" src="../_static/jquery.js"></script>
568      <script type="text/javascript" src="../_static/underscore.js"></script>
569      <script type="text/javascript" src="../_static/doctools.js"></script>
570      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
571
572 
573
574  <script type="text/javascript" src="../_static/js/theme.js"></script>
575
576  <script type="text/javascript">
577      jQuery(function () {
578          SphinxRtdTheme.Navigation.enable(true);
579      });
580  </script> 
581
582</body>
583</html>
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG