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

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

new build of sphinx for updated api

  • Property mode set to 100644
File size: 36.6 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">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">ecapi</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">ecapi</span> <span class="o">=</span> <span class="kc">False</span>
233<span class="c1"># ------------------------------------------------------------------------------</span>
234<span class="c1"># FUNCTION</span>
235<span class="c1"># ------------------------------------------------------------------------------</span>
236<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>
237    <span class="sd">&#39;&#39;&#39;Controls the program to get data out of mars.</span>
238
239<span class="sd">    This is done if it is called directly from command line.</span>
240<span class="sd">    Then it also takes program call arguments and control file input.</span>
241
242<span class="sd">    Parameters</span>
243<span class="sd">    ----------</span>
244
245<span class="sd">    Return</span>
246<span class="sd">    ------</span>
247
248<span class="sd">    &#39;&#39;&#39;</span>
249
250    <span class="n">args</span> <span class="o">=</span> <span class="n">get_cmdline_args</span><span class="p">()</span>
251    <span class="n">c</span> <span class="o">=</span> <span class="n">ControlFile</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">controlfile</span><span class="p">)</span>
252
253    <span class="n">env_parameter</span> <span class="o">=</span> <span class="n">read_ecenv</span><span class="p">(</span><span class="n">_config</span><span class="o">.</span><span class="n">PATH_ECMWF_ENV</span><span class="p">)</span>
254    <span class="n">c</span><span class="o">.</span><span class="n">assign_args_to_control</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
255    <span class="n">c</span><span class="o">.</span><span class="n">assign_envs_to_control</span><span class="p">(</span><span class="n">env_parameter</span><span class="p">)</span>
256    <span class="n">c</span><span class="o">.</span><span class="n">check_conditions</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">queue</span><span class="p">)</span>
257
258    <span class="n">get_mars_data</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
259    <span class="n">normal_exit</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">mailops</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">queue</span><span class="p">,</span> <span class="s1">&#39;Done!&#39;</span><span class="p">)</span>
260
261    <span class="k">return</span></div>
262
263<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>
264    <span class="sd">&#39;&#39;&#39;Retrieves the EC data needed for a FLEXPART simulation.</span>
265
266<span class="sd">    Start and end dates for retrieval period is set. Retrievals</span>
267<span class="sd">    are divided into smaller periods if necessary and datechunk parameter</span>
268<span class="sd">    is set.</span>
269
270<span class="sd">    Parameters</span>
271<span class="sd">    ----------</span>
272<span class="sd">    c : ControlFile</span>
273<span class="sd">        Contains all the parameters of CONTROL file and</span>
274<span class="sd">        command line.</span>
275
276<span class="sd">    Return</span>
277<span class="sd">    ------</span>
278
279<span class="sd">    &#39;&#39;&#39;</span>
280    <span class="n">c</span><span class="o">.</span><span class="n">ecapi</span> <span class="o">=</span> <span class="n">ecapi</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 server connection if ECMWF WebAPI is available.</span>
338
339<span class="sd">    Parameters</span>
340<span class="sd">    ----------</span>
341<span class="sd">    c : ControlFile</span>
342<span class="sd">        Contains all the parameters of CONTROL file and</span>
343<span class="sd">        command line.</span>
344
345<span class="sd">    Return</span>
346<span class="sd">    ------</span>
347<span class="sd">    server : ECMWFDataServer or ECMWFService</span>
348<span class="sd">        Connection to ECMWF server via python interface ECMWF WebAPI.</span>
349
350<span class="sd">    &#39;&#39;&#39;</span>
351    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">ecapi</span><span class="p">:</span>
352        <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">public</span><span class="p">:</span>
353            <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>
354        <span class="k">else</span><span class="p">:</span>
355            <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>
356    <span class="k">else</span><span class="p">:</span>
357        <span class="n">server</span> <span class="o">=</span> <span class="kc">False</span>
358
359    <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">ecapi</span><span class="p">))</span>
360
361    <span class="k">return</span> <span class="n">server</span></div>
362
363
364<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>
365    <span class="sd">&#39;&#39;&#39;Prepares start and end date depending on flux or non flux data.</span>
366
367<span class="sd">    If forecast for maximum one day (upto 24h) are to be retrieved, then</span>
368<span class="sd">    collect accumulation data (flux data) with additional days in the</span>
369<span class="sd">    beginning and at the end (used for complete disaggregation of</span>
370<span class="sd">    original period)</span>
371
372<span class="sd">    If forecast data longer than 24h are to be retrieved, then</span>
373<span class="sd">    collect accumulation data (flux data) with the exact start and end date</span>
374<span class="sd">    (disaggregation will be done for the exact time period with</span>
375<span class="sd">    boundary conditions)</span>
376
377<span class="sd">    Since for basetime the extraction contains the 12 hours upfront,</span>
378<span class="sd">    if basetime is 0, the starting date has to be the day before and</span>
379
380<span class="sd">    Parameters</span>
381<span class="sd">    ----------</span>
382<span class="sd">    c : ControlFile</span>
383<span class="sd">        Contains all the parameters of CONTROL file and</span>
384<span class="sd">        command line.</span>
385
386<span class="sd">    fluxes : boolean, optional</span>
387<span class="sd">        Decides if the flux parameter settings are stored or</span>
388<span class="sd">        the rest of the parameter list.</span>
389<span class="sd">        Default value is False.</span>
390
391<span class="sd">    Return</span>
392<span class="sd">    ------</span>
393<span class="sd">    start : datetime</span>
394<span class="sd">        The start date of the retrieving data set.</span>
395
396<span class="sd">    end : datetime</span>
397<span class="sd">        The end date of the retrieving data set.</span>
398
399<span class="sd">    chunk : datetime</span>
400<span class="sd">        Time period in days for one single mars retrieval.</span>
401
402<span class="sd">    &#39;&#39;&#39;</span>
403    <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>
404    <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>
405    <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>
406
407    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">basetime</span><span class="p">:</span>
408        <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="s1">&#39;00&#39;</span><span class="p">:</span>
409            <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>
410
411    <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="p">:</span>
412        <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>
413        <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>
414
415    <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>
416
417<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>
418    <span class="sd">&#39;&#39;&#39;Deletes old retrieval files from current input directory</span>
419<span class="sd">    matching the pattern.</span>
420
421<span class="sd">    Parameters</span>
422<span class="sd">    ----------</span>
423<span class="sd">    pattern : str</span>
424<span class="sd">        The sub string pattern which identifies the files to be deleted.</span>
425
426<span class="sd">    inputdir : str, optional</span>
427<span class="sd">        Path to the directory where the retrieved data is stored.</span>
428
429<span class="sd">    Return</span>
430<span class="sd">    ------</span>
431
432<span class="sd">    &#39;&#39;&#39;</span>
433    <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>
434
435    <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>
436    <span class="n">tobecleaned</span><span class="o">.</span><span class="n">delete_files</span><span class="p">()</span>
437
438    <span class="k">return</span></div>
439
440
441<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>
442    <span class="sd">&#39;&#39;&#39;Divides the complete retrieval period in smaller chunks and</span>
443<span class="sd">    retrieves the data from MARS.</span>
444
445<span class="sd">    Parameters</span>
446<span class="sd">    ----------</span>
447<span class="sd">    c : ControlFile</span>
448<span class="sd">        Contains all the parameters of CONTROL file and</span>
449<span class="sd">        command line.</span>
450
451<span class="sd">    server : ECMWFService or ECMWFDataServer</span>
452<span class="sd">            The server connection to ECMWF.</span>
453
454<span class="sd">    start : datetime</span>
455<span class="sd">        The start date of the retrieval.</span>
456
457<span class="sd">    end : datetime</span>
458<span class="sd">        The end date of the retrieval.</span>
459
460<span class="sd">    delta_t : datetime</span>
461<span class="sd">        Delta_t + 1 is the maximal time period of a single</span>
462<span class="sd">        retrieval.</span>
463
464<span class="sd">    fluxes : boolean, optional</span>
465<span class="sd">        Decides if the flux parameters are to be retrieved or</span>
466<span class="sd">        the rest of the parameter list.</span>
467<span class="sd">        Default value is False.</span>
468
469<span class="sd">    Return</span>
470<span class="sd">    ------</span>
471
472<span class="sd">    &#39;&#39;&#39;</span>
473
474    <span class="c1"># since actual day also counts as one day,</span>
475    <span class="c1"># we only need to add datechunk - 1 days to retrieval</span>
476    <span class="c1"># for a period</span>
477    <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>
478
479    <span class="n">day</span> <span class="o">=</span> <span class="n">start</span>
480    <span class="k">while</span> <span class="n">day</span> <span class="o">&lt;=</span> <span class="n">end</span><span class="p">:</span>
481        <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>
482        <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>
483        <span class="k">if</span> <span class="n">tmpday</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">:</span>
484            <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> \
485                    <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>
486        <span class="k">else</span><span class="p">:</span>
487            <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> \
488                    <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>
489
490        <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>
491
492        <span class="k">try</span><span class="p">:</span>
493            <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>
494        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
495            <span class="n">my_error</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">mailfail</span><span class="p">,</span> <span class="s1">&#39;MARS request failed&#39;</span><span class="p">)</span>
496
497        <span class="n">day</span> <span class="o">+=</span> <span class="n">delta_t</span>
498
499    <span class="k">return</span></div>
500
501<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>
502    <span class="n">main</span><span class="p">()</span>
503</pre></div>
504
505           </div>
506           
507          </div>
508          <footer>
509 
510
511  <hr/>
512
513  <div role="contentinfo">
514    <p>
515        &copy; Copyright 2018, Anne Philipp and Leopold Haimberger
516
517    </p>
518  </div>
519  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>.
520
521</footer>
522
523        </div>
524      </div>
525
526    </section>
527
528  </div>
529 
530
531
532 
533
534   
535   
536      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
537        <script type="text/javascript" src="../_static/jquery.js"></script>
538        <script type="text/javascript" src="../_static/underscore.js"></script>
539        <script type="text/javascript" src="../_static/doctools.js"></script>
540   
541
542 
543
544  <script type="text/javascript" src="../_static/js/theme.js"></script>
545
546  <script type="text/javascript">
547      jQuery(function () {
548          SphinxRtdTheme.Navigation.enable(true);
549      });
550  </script> 
551
552</body>
553</html>
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG