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

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

new build of sphinx for updated api

  • Property mode set to 100644
File size: 82.4 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>tools &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>tools</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 tools</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 Philipp (University of Vienna)</span>
158<span class="c1">#</span>
159<span class="c1"># @Date: May 2018</span>
160<span class="c1">#</span>
161<span class="c1"># @Change History:</span>
162<span class="c1">#    October 2014 - Anne Fouilloux (University of Oslo)</span>
163<span class="c1">#        - created functions silent_remove and product (taken from ECMWF)</span>
164<span class="c1">#</span>
165<span class="c1">#    November 2015 - Leopold Haimberger (University of Vienna)</span>
166<span class="c1">#        - created functions: interpret_args_and_control, clean_up</span>
167<span class="c1">#          my_error, normal_exit, init128, to_param_id</span>
168<span class="c1">#</span>
169<span class="c1">#    April - December 2018 - Anne Philipp (University of Vienna):</span>
170<span class="c1">#        - applied PEP8 style guide</span>
171<span class="c1">#        - added documentation</span>
172<span class="c1">#        - moved all non class methods from former file Flexparttools in here</span>
173<span class="c1">#        - seperated args and control interpretation</span>
174<span class="c1">#        - added functions get_list_as_string, read_ecenv, send_mail, make_dir,</span>
175<span class="c1">#          put_file_to_ecserver, submit_job_to_ecserver, get_informations,</span>
176<span class="c1">#          get_dimensions, execute_subprocess, none_or_int, none_or_str</span>
177<span class="c1">#</span>
178<span class="c1"># @License:</span>
179<span class="c1">#    (C) Copyright 2014-2019.</span>
180<span class="c1">#    Anne Philipp, Leopold Haimberger</span>
181<span class="c1">#</span>
182<span class="c1">#    This work is licensed under the Creative Commons Attribution 4.0</span>
183<span class="c1">#    International License. To view a copy of this license, visit</span>
184<span class="c1">#    http://creativecommons.org/licenses/by/4.0/ or send a letter to</span>
185<span class="c1">#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.</span>
186<span class="c1">#</span>
187<span class="c1"># @Methods:</span>
188<span class="c1">#    none_or_str</span>
189<span class="c1">#    none_or_int</span>
190<span class="c1">#    get_cmdline_args</span>
191<span class="c1">#    read_ecenv</span>
192<span class="c1">#    clean_up</span>
193<span class="c1">#    my_error</span>
194<span class="c1">#    send_mail</span>
195<span class="c1">#    normal_exit</span>
196<span class="c1">#    product</span>
197<span class="c1">#    silent_remove</span>
198<span class="c1">#    init128</span>
199<span class="c1">#    to_param_id</span>
200<span class="c1">#    get_list_as_string</span>
201<span class="c1">#    make_dir</span>
202<span class="c1">#    put_file_to_ecserver</span>
203<span class="c1">#    submit_job_to_ecserver</span>
204<span class="c1">#    get_informations</span>
205<span class="c1">#    get_dimensions</span>
206<span class="c1">#    execute_subprocess</span>
207<span class="c1">#*******************************************************************************</span>
208<span class="sd">&#39;&#39;&#39;This module contains a collection of diverse tasks within flex_extract.</span>
209<span class="sd">&#39;&#39;&#39;</span>
210
211<span class="c1"># ------------------------------------------------------------------------------</span>
212<span class="c1"># MODULES</span>
213<span class="c1"># ------------------------------------------------------------------------------</span>
214<span class="kn">import</span> <span class="nn">os</span>
215<span class="kn">import</span> <span class="nn">errno</span>
216<span class="kn">import</span> <span class="nn">sys</span>
217<span class="kn">import</span> <span class="nn">glob</span>
218<span class="kn">import</span> <span class="nn">subprocess</span>
219<span class="kn">import</span> <span class="nn">traceback</span>
220<span class="kn">import</span> <span class="nn">exceptions</span>
221<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
222<span class="kn">from</span> <span class="nn">argparse</span> <span class="k">import</span> <span class="n">ArgumentParser</span><span class="p">,</span> <span class="n">ArgumentDefaultsHelpFormatter</span>
223
224<span class="c1"># ------------------------------------------------------------------------------</span>
225<span class="c1"># METHODS</span>
226<span class="c1"># ------------------------------------------------------------------------------</span>
227
228<div class="viewcode-block" id="none_or_str"><a class="viewcode-back" href="../api.html#tools.none_or_str">[docs]</a><span class="k">def</span> <span class="nf">none_or_str</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
229    <span class="sd">&#39;&#39;&#39;Converts the input string into pythons None-type if the string</span>
230<span class="sd">    contains string &quot;None&quot;.</span>
231
232<span class="sd">    Parameters</span>
233<span class="sd">    ----------</span>
234<span class="sd">    value : str</span>
235<span class="sd">        String to be checked for the &quot;None&quot; word.</span>
236
237<span class="sd">    Return</span>
238<span class="sd">    ------</span>
239<span class="sd">    None or value:</span>
240<span class="sd">        Return depends on the content of the input value. If it was &quot;None&quot;,</span>
241<span class="sd">        then the python type None is returned. Otherwise the string itself.</span>
242<span class="sd">    &#39;&#39;&#39;</span>
243    <span class="k">if</span> <span class="n">value</span> <span class="o">==</span> <span class="s1">&#39;None&#39;</span><span class="p">:</span>
244        <span class="k">return</span> <span class="kc">None</span>
245    <span class="k">return</span> <span class="n">value</span></div>
246
247<div class="viewcode-block" id="none_or_int"><a class="viewcode-back" href="../api.html#tools.none_or_int">[docs]</a><span class="k">def</span> <span class="nf">none_or_int</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
248    <span class="sd">&#39;&#39;&#39;Converts the input string into pythons None-type if the string</span>
249<span class="sd">    contains string &quot;None&quot;. Otherwise it is converted to an integer value.</span>
250
251<span class="sd">    Parameters</span>
252<span class="sd">    ----------</span>
253<span class="sd">    value : str</span>
254<span class="sd">        String to be checked for the &quot;None&quot; word.</span>
255
256<span class="sd">    Return</span>
257<span class="sd">    ------</span>
258<span class="sd">    None or int(value):</span>
259<span class="sd">        Return depends on the content of the input value. If it was &quot;None&quot;,</span>
260<span class="sd">        then the python type None is returned. Otherwise the string is</span>
261<span class="sd">        converted into an integer value.</span>
262<span class="sd">    &#39;&#39;&#39;</span>
263    <span class="k">if</span> <span class="n">value</span> <span class="o">==</span> <span class="s1">&#39;None&#39;</span><span class="p">:</span>
264        <span class="k">return</span> <span class="kc">None</span>
265    <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span></div>
266
267<div class="viewcode-block" id="get_cmdline_args"><a class="viewcode-back" href="../api.html#tools.get_cmdline_args">[docs]</a><span class="k">def</span> <span class="nf">get_cmdline_args</span><span class="p">():</span>
268    <span class="sd">&#39;&#39;&#39;Decomposes the command line arguments and assigns them to variables.</span>
269<span class="sd">    Apply default values for non mentioned arguments.</span>
270
271<span class="sd">    Parameters</span>
272<span class="sd">    ----------</span>
273
274<span class="sd">    Return</span>
275<span class="sd">    ------</span>
276<span class="sd">    args : Namespace</span>
277<span class="sd">        Contains the commandline arguments from script/program call.</span>
278<span class="sd">    &#39;&#39;&#39;</span>
279
280    <span class="n">parser</span> <span class="o">=</span> <span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Retrieve FLEXPART input from </span><span class="se">\</span>
281<span class="s1">                                ECMWF MARS archive&#39;</span><span class="p">,</span>
282                            <span class="n">formatter_class</span><span class="o">=</span><span class="n">ArgumentDefaultsHelpFormatter</span><span class="p">)</span>
283
284    <span class="c1"># control parameters that override control file values</span>
285    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--start_date&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;start_date&quot;</span><span class="p">,</span>
286                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
287                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;start date YYYYMMDD&quot;</span><span class="p">)</span>
288    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--end_date&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;end_date&quot;</span><span class="p">,</span>
289                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
290                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;end_date YYYYMMDD&quot;</span><span class="p">)</span>
291    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--date_chunk&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;date_chunk&quot;</span><span class="p">,</span>
292                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
293                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;# of days to be retrieved at once&quot;</span><span class="p">)</span>
294    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--job_chunk&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;job_chunk&quot;</span><span class="p">,</span>
295                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
296                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;# of days to be retrieved within a single job&quot;</span><span class="p">)</span>
297    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--controlfile&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;controlfile&quot;</span><span class="p">,</span>
298                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s1">&#39;CONTROL.temp&#39;</span><span class="p">,</span>
299                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;file with CONTROL parameters&quot;</span><span class="p">)</span>
300    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--basetime&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;basetime&quot;</span><span class="p">,</span>
301                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
302                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;base such as 00 or 12 (for half day retrievals)&quot;</span><span class="p">)</span>
303    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--step&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;step&quot;</span><span class="p">,</span>
304                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
305                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;steps such as 00/to/48&quot;</span><span class="p">)</span>
306    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--levelist&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;levelist&quot;</span><span class="p">,</span>
307                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
308                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Vertical levels to be retrieved, e.g. 30/to/60&quot;</span><span class="p">)</span>
309    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--area&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;area&quot;</span><span class="p">,</span>
310                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
311                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;area defined as north/west/south/east&quot;</span><span class="p">)</span>
312
313    <span class="c1"># some switches</span>
314    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--debug&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;debug&quot;</span><span class="p">,</span>
315                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
316                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;debug mode - leave temporary files intact&quot;</span><span class="p">)</span>
317    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--request&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;request&quot;</span><span class="p">,</span>
318                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
319                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;list all mars requests in file mars_requests.dat&quot;</span><span class="p">)</span>
320    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--public&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;public&quot;</span><span class="p">,</span>
321                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
322                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;public mode - retrieves the public datasets&quot;</span><span class="p">)</span>
323    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--rrint&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;rrint&quot;</span><span class="p">,</span>
324                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
325                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;select old or new precipitation interpolation </span><span class="se">\</span>
326<span class="s2">                        0 - old method</span><span class="se">\</span>
327<span class="s2">                        1 - new method (additional subgrid points)&quot;</span><span class="p">)</span>
328
329    <span class="c1"># set directories</span>
330    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--inputdir&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;inputdir&quot;</span><span class="p">,</span>
331                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
332                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;root directory for storing intermediate files&quot;</span><span class="p">)</span>
333    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--outputdir&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;outputdir&quot;</span><span class="p">,</span>
334                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
335                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;root directory for storing output files&quot;</span><span class="p">)</span>
336    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--flexpartdir&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;flexpartdir&quot;</span><span class="p">,</span>
337                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
338                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;FLEXPART root directory (to find grib2flexpart </span><span class="se">\</span>
339<span class="s2">                        and COMMAND file)</span><span class="se">\n</span><span class="s2"> Normally flex_extract resides in </span><span class="se">\</span>
340<span class="s2">                        the scripts directory of the FLEXPART distribution&quot;</span><span class="p">)</span>
341
342    <span class="c1"># this is only used by prepare_flexpart.py to rerun a postprocessing step</span>
343    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--ppid&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;ppid&quot;</span><span class="p">,</span>
344                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
345                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;specify parent process id for </span><span class="se">\</span>
346<span class="s2">                        rerun of prepare_flexpart&quot;</span><span class="p">)</span>
347
348    <span class="c1"># arguments for job submission to ECMWF, only needed by submit.py</span>
349    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--job_template&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;job_template&#39;</span><span class="p">,</span>
350                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s2">&quot;job.temp&quot;</span><span class="p">,</span>
351                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;job template file for submission to ECMWF&quot;</span><span class="p">)</span>
352    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--queue&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;queue&quot;</span><span class="p">,</span>
353                        <span class="nb">type</span><span class="o">=</span><span class="n">none_or_str</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
354                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;queue for submission to ECMWF </span><span class="se">\</span>
355<span class="s2">                        (e.g. ecgate or cca )&quot;</span><span class="p">)</span>
356
357    <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
358
359    <span class="k">return</span> <span class="n">args</span></div>
360
361<div class="viewcode-block" id="read_ecenv"><a class="viewcode-back" href="../api.html#tools.read_ecenv">[docs]</a><span class="k">def</span> <span class="nf">read_ecenv</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span>
362    <span class="sd">&#39;&#39;&#39;Reads the file into a dictionary where the key values are the parameter</span>
363<span class="sd">    names.</span>
364
365<span class="sd">    Parameters</span>
366<span class="sd">    ----------</span>
367<span class="sd">    filepath : str</span>
368<span class="sd">        Path to file where the ECMWF environment parameters are stored.</span>
369
370<span class="sd">    Return</span>
371<span class="sd">    ------</span>
372<span class="sd">    envs : dict</span>
373<span class="sd">        Contains the environment parameter ecuid, ecgid, gateway</span>
374<span class="sd">        and destination for ECMWF server environments.</span>
375<span class="sd">    &#39;&#39;&#39;</span>
376    <span class="n">envs</span><span class="o">=</span> <span class="p">{}</span>
377    <span class="k">try</span><span class="p">:</span>
378        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filepath</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
379            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
380                <span class="n">data</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
381                <span class="n">envs</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">])]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
382    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
383        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span><span class="p">))</span>
384        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">))</span>
385
386        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">... Error occured while trying to read ECMWF_ENV &#39;</span>
387                     <span class="s1">&#39;file: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">filepath</span><span class="p">))</span>
388
389    <span class="k">return</span> <span class="n">envs</span></div>
390
391<div class="viewcode-block" id="clean_up"><a class="viewcode-back" href="../api.html#tools.clean_up">[docs]</a><span class="k">def</span> <span class="nf">clean_up</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
392    <span class="sd">&#39;&#39;&#39;Remove files from the intermediate directory (inputdir).</span>
393
394<span class="sd">    It keeps the final FLEXPART input files if program runs without</span>
395<span class="sd">    ECMWF Api and keywords &quot;ectrans&quot; or &quot;ecstorage&quot; are set to &quot;1&quot;.</span>
396
397<span class="sd">    Parameters</span>
398<span class="sd">    ----------</span>
399<span class="sd">    c : ControlFile</span>
400<span class="sd">        Contains all the parameters of CONTROL file and</span>
401<span class="sd">        command line.</span>
402
403<span class="sd">    Return</span>
404<span class="sd">    ------</span>
405
406<span class="sd">    &#39;&#39;&#39;</span>
407
408    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... clean inputdir!&quot;</span><span class="p">)</span>
409
410    <span class="n">cleanlist</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</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="s2">&quot;*&quot;</span><span class="p">))</span>
411
412    <span class="k">if</span> <span class="n">cleanlist</span><span class="p">:</span>
413        <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">cleanlist</span><span class="p">:</span>
414            <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">prefix</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">element</span><span class="p">:</span>
415                <span class="n">silent_remove</span><span class="p">(</span><span class="n">element</span><span class="p">)</span>
416            <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">ecapi</span> <span class="ow">is</span> <span class="kc">False</span> <span class="ow">and</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">ectrans</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">c</span><span class="o">.</span><span class="n">ecstorage</span> <span class="o">==</span> <span class="mi">1</span><span class="p">):</span>
417                <span class="n">silent_remove</span><span class="p">(</span><span class="n">element</span><span class="p">)</span>
418        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... done!&quot;</span><span class="p">)</span>
419    <span class="k">else</span><span class="p">:</span>
420        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... nothing to clean!&quot;</span><span class="p">)</span>
421
422    <span class="k">return</span></div>
423
424
425<div class="viewcode-block" id="my_error"><a class="viewcode-back" href="../api.html#tools.my_error">[docs]</a><span class="k">def</span> <span class="nf">my_error</span><span class="p">(</span><span class="n">users</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="s1">&#39;ERROR&#39;</span><span class="p">):</span>
426    <span class="sd">&#39;&#39;&#39;Prints a specified error message which can be passed to the function</span>
427<span class="sd">    before exiting the program.</span>
428
429<span class="sd">    Parameters</span>
430<span class="sd">    ----------</span>
431<span class="sd">    user : list of str</span>
432<span class="sd">        Contains all email addresses which should be notified.</span>
433<span class="sd">        It might also contain just the ecmwf user name which wil trigger</span>
434<span class="sd">        mailing to the associated email address for this user.</span>
435
436<span class="sd">    message : str, optional</span>
437<span class="sd">        Error message. Default value is &quot;ERROR&quot;.</span>
438
439<span class="sd">    Return</span>
440<span class="sd">    ------</span>
441
442<span class="sd">    &#39;&#39;&#39;</span>
443
444    <span class="n">trace</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_stack</span><span class="p">())</span>
445    <span class="n">full_message</span> <span class="o">=</span> <span class="n">message</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">trace</span>
446
447    <span class="nb">print</span><span class="p">(</span><span class="n">full_message</span><span class="p">)</span>
448
449    <span class="n">send_mail</span><span class="p">(</span><span class="n">users</span><span class="p">,</span> <span class="s1">&#39;ERROR&#39;</span><span class="p">,</span> <span class="n">full_message</span><span class="p">)</span>
450
451    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
452
453    <span class="k">return</span></div>
454
455
456<div class="viewcode-block" id="send_mail"><a class="viewcode-back" href="../api.html#tools.send_mail">[docs]</a><span class="k">def</span> <span class="nf">send_mail</span><span class="p">(</span><span class="n">users</span><span class="p">,</span> <span class="n">success_mode</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
457    <span class="sd">&#39;&#39;&#39;Prints a specific exit message which can be passed to the function.</span>
458
459<span class="sd">    Parameters</span>
460<span class="sd">    ----------</span>
461<span class="sd">    users : list of str</span>
462<span class="sd">        Contains all email addresses which should be notified.</span>
463<span class="sd">        It might also contain just the ecmwf user name which wil trigger</span>
464<span class="sd">        mailing to the associated email address for this user.</span>
465
466<span class="sd">    success_mode : str</span>
467<span class="sd">        States the exit mode of the program to put into</span>
468<span class="sd">        the mail subject line.</span>
469
470<span class="sd">    message : str, optional</span>
471<span class="sd">        Message for exiting program. Default value is &quot;Done!&quot;.</span>
472
473<span class="sd">    Return</span>
474<span class="sd">    ------</span>
475
476<span class="sd">    &#39;&#39;&#39;</span>
477
478    <span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="n">users</span><span class="p">:</span>
479        <span class="k">if</span> <span class="s1">&#39;$</span><span class="si">{USER}</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
480            <span class="n">user</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s1">&#39;USER&#39;</span><span class="p">)</span>
481        <span class="k">try</span><span class="p">:</span>
482            <span class="n">p</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">([</span><span class="s1">&#39;mail&#39;</span><span class="p">,</span> <span class="s1">&#39;-s flex_extract_v7.1 &#39;</span> <span class="o">+</span>
483                                  <span class="n">success_mode</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">expandvars</span><span class="p">(</span><span class="n">user</span><span class="p">)],</span>
484                                 <span class="n">stdin</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
485                                 <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
486                                 <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
487                                 <span class="n">bufsize</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
488            <span class="n">pout</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">message</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
489        <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
490            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
491            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... Email could not be sent!&#39;</span><span class="p">)</span>
492        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
493            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span><span class="p">))</span>
494            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">))</span>
495            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... Email could not be sent!&#39;</span><span class="p">)</span>
496        <span class="k">else</span><span class="p">:</span>
497            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Email sent to &#39;</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expandvars</span><span class="p">(</span><span class="n">user</span><span class="p">))</span>
498
499    <span class="k">return</span></div>
500
501
502<div class="viewcode-block" id="normal_exit"><a class="viewcode-back" href="../api.html#tools.normal_exit">[docs]</a><span class="k">def</span> <span class="nf">normal_exit</span><span class="p">(</span><span class="n">message</span><span class="o">=</span><span class="s1">&#39;Done!&#39;</span><span class="p">):</span>
503    <span class="sd">&#39;&#39;&#39;Prints a specific exit message which can be passed to the function.</span>
504
505<span class="sd">    Parameters</span>
506<span class="sd">    ----------</span>
507<span class="sd">    message : str, optional</span>
508<span class="sd">        Message for exiting program. Default value is &quot;Done!&quot;.</span>
509
510<span class="sd">    Return</span>
511<span class="sd">    ------</span>
512
513<span class="sd">    &#39;&#39;&#39;</span>
514
515    <span class="nb">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
516
517    <span class="k">return</span></div>
518
519
520<div class="viewcode-block" id="product"><a class="viewcode-back" href="../api.html#tools.product">[docs]</a><span class="k">def</span> <span class="nf">product</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
521    <span class="sd">&#39;&#39;&#39;Creates combinations of all passed arguments.</span>
522
523<span class="sd">    This method combines the single characters of the passed arguments</span>
524<span class="sd">    with each other. So that each character of each argument value</span>
525<span class="sd">    will be combined with each character of the other arguments as a tuple.</span>
526
527<span class="sd">    Note</span>
528<span class="sd">    ----</span>
529<span class="sd">    This method is taken from an example at the ECMWF wiki website.</span>
530<span class="sd">    https://software.ecmwf.int/wiki/display/GRIB/index.py; 2018-03-16</span>
531
532<span class="sd">    Example</span>
533<span class="sd">    -------</span>
534<span class="sd">    product(&#39;ABCD&#39;, &#39;xy&#39;) --&gt; Ax Ay Bx By Cx Cy Dx Dy</span>
535
536<span class="sd">    product(range(2), repeat = 3) --&gt; 000 001 010 011 100 101 110 111</span>
537
538<span class="sd">    Parameters</span>
539<span class="sd">    ----------</span>
540<span class="sd">    \*args : list or str</span>
541<span class="sd">        Positional arguments (arbitrary number).</span>
542
543<span class="sd">    \*\*kwds : dict</span>
544<span class="sd">        Contains all the keyword arguments from \*args.</span>
545
546<span class="sd">    Return</span>
547<span class="sd">    ------</span>
548<span class="sd">    prod : :obj:`tuple`</span>
549<span class="sd">        Return will be done with &quot;yield&quot;. A tuple of combined arguments.</span>
550<span class="sd">        See example in description above.</span>
551<span class="sd">    &#39;&#39;&#39;</span>
552    <span class="k">try</span><span class="p">:</span>
553        <span class="n">pools</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">*</span> <span class="n">kwds</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;repeat&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
554        <span class="n">result</span> <span class="o">=</span> <span class="p">[[]]</span>
555        <span class="k">for</span> <span class="n">pool</span> <span class="ow">in</span> <span class="n">pools</span><span class="p">:</span>
556            <span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="o">+</span> <span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">result</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">pool</span><span class="p">]</span>
557        <span class="k">for</span> <span class="n">prod</span> <span class="ow">in</span> <span class="n">result</span><span class="p">:</span>
558            <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">prod</span><span class="p">)</span>
559    <span class="k">except</span> <span class="ne">TypeError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
560        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... PRODUCT GENERATION FAILED!&#39;</span><span class="p">)</span>
561
562    <span class="k">return</span></div>
563
564
565<div class="viewcode-block" id="silent_remove"><a class="viewcode-back" href="../api.html#tools.silent_remove">[docs]</a><span class="k">def</span> <span class="nf">silent_remove</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
566    <span class="sd">&#39;&#39;&#39;Remove file if it exists.</span>
567<span class="sd">    The function does not fail if the file does not exist.</span>
568
569<span class="sd">    Parameters</span>
570<span class="sd">    ----------</span>
571<span class="sd">    filename : str</span>
572<span class="sd">        The name of the file to be removed without notification.</span>
573
574<span class="sd">    Return</span>
575<span class="sd">    ------</span>
576
577<span class="sd">    &#39;&#39;&#39;</span>
578    <span class="k">try</span><span class="p">:</span>
579        <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
580    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
581        <span class="c1"># errno.ENOENT  =  no such file or directory</span>
582        <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span><span class="p">:</span>
583            <span class="k">pass</span>
584        <span class="k">else</span><span class="p">:</span>
585            <span class="k">raise</span>  <span class="c1"># re-raise exception if a different error occured</span>
586
587    <span class="k">return</span></div>
588
589
590<div class="viewcode-block" id="init128"><a class="viewcode-back" href="../api.html#tools.init128">[docs]</a><span class="k">def</span> <span class="nf">init128</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span>
591    <span class="sd">&#39;&#39;&#39;Opens and reads the grib file with table 128 information.</span>
592
593<span class="sd">    Parameters</span>
594<span class="sd">    ----------</span>
595<span class="sd">    filepath : str</span>
596<span class="sd">        Path to file of ECMWF grib table number 128.</span>
597
598<span class="sd">    Return</span>
599<span class="sd">    ------</span>
600<span class="sd">    table128 : dict</span>
601<span class="sd">        Contains the ECMWF grib table 128 information.</span>
602<span class="sd">        The key is the parameter number and the value is the</span>
603<span class="sd">        short name of the parameter.</span>
604<span class="sd">    &#39;&#39;&#39;</span>
605    <span class="n">table128</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
606    <span class="k">try</span><span class="p">:</span>
607        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filepath</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
608            <span class="n">fdata</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
609    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
610        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span><span class="p">))</span>
611        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">))</span>
612
613        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">... Error occured while trying to read parameter &#39;</span>
614                 <span class="s1">&#39;table file: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">filepath</span><span class="p">))</span>
615    <span class="k">else</span><span class="p">:</span>
616        <span class="k">for</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">fdata</span><span class="p">:</span>
617            <span class="k">if</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39;!&#39;</span><span class="p">:</span>
618                <span class="n">table128</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">59</span><span class="p">:</span><span class="mi">64</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
619
620    <span class="k">return</span> <span class="n">table128</span></div>
621
622
623<div class="viewcode-block" id="to_param_id"><a class="viewcode-back" href="../api.html#tools.to_param_id">[docs]</a><span class="k">def</span> <span class="nf">to_param_id</span><span class="p">(</span><span class="n">pars</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
624    <span class="sd">&#39;&#39;&#39;Transform parameter names to parameter ids with ECMWF grib table 128.</span>
625
626<span class="sd">    Parameters</span>
627<span class="sd">    ----------</span>
628<span class="sd">    pars : str</span>
629<span class="sd">        Addpar argument from CONTROL file in the format of</span>
630<span class="sd">        parameter names instead of ids. The parameter short</span>
631<span class="sd">        names are sepearted with &quot;/&quot; and they are passed as</span>
632<span class="sd">        one single string.</span>
633
634<span class="sd">    table : dict</span>
635<span class="sd">        Contains the ECMWF grib table 128 information.</span>
636<span class="sd">        The key is the parameter number and the value is the</span>
637<span class="sd">        short name of the parameter.</span>
638
639<span class="sd">    Return</span>
640<span class="sd">    ------</span>
641<span class="sd">    ipar : list of int</span>
642<span class="sd">        List of addpar parameters from CONTROL file transformed to</span>
643<span class="sd">        parameter ids in the format of integer.</span>
644<span class="sd">    &#39;&#39;&#39;</span>
645    <span class="k">if</span> <span class="ow">not</span> <span class="n">pars</span><span class="p">:</span>
646        <span class="k">return</span> <span class="p">[]</span>
647    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pars</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
648        <span class="n">pars</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">pars</span><span class="p">)</span>
649
650    <span class="n">cpar</span> <span class="o">=</span> <span class="n">pars</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
651    <span class="n">ipar</span> <span class="o">=</span> <span class="p">[]</span>
652    <span class="k">for</span> <span class="n">par</span> <span class="ow">in</span> <span class="n">cpar</span><span class="p">:</span>
653        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">table</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
654            <span class="k">if</span> <span class="n">par</span> <span class="o">==</span> <span class="n">k</span> <span class="ow">or</span> <span class="n">par</span> <span class="o">==</span> <span class="n">v</span><span class="p">:</span>
655                <span class="n">ipar</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">))</span>
656                <span class="k">break</span>
657        <span class="k">else</span><span class="p">:</span>
658            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Warning: par &#39;</span> <span class="o">+</span> <span class="n">par</span> <span class="o">+</span> <span class="s1">&#39; not found in table 128&#39;</span><span class="p">)</span>
659
660    <span class="k">return</span> <span class="n">ipar</span></div>
661
662<div class="viewcode-block" id="get_list_as_string"><a class="viewcode-back" href="../api.html#tools.get_list_as_string">[docs]</a><span class="k">def</span> <span class="nf">get_list_as_string</span><span class="p">(</span><span class="n">list_obj</span><span class="p">,</span> <span class="n">concatenate_sign</span><span class="o">=</span><span class="s1">&#39;, &#39;</span><span class="p">):</span>
663    <span class="sd">&#39;&#39;&#39;Converts a list of arbitrary content into a single string.</span>
664
665<span class="sd">    Parameters</span>
666<span class="sd">    ----------</span>
667<span class="sd">    list_obj : list of *</span>
668<span class="sd">        A list with arbitrary content.</span>
669
670<span class="sd">    concatenate_sign : str, optional</span>
671<span class="sd">        A string which is used to concatenate the single</span>
672<span class="sd">        list elements. Default value is &quot;, &quot;.</span>
673
674<span class="sd">    Return</span>
675<span class="sd">    ------</span>
676<span class="sd">    str_of_list : str</span>
677<span class="sd">        The content of the list as a single string.</span>
678<span class="sd">    &#39;&#39;&#39;</span>
679
680    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">list_obj</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
681        <span class="n">list_obj</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">list_obj</span><span class="p">)</span>
682    <span class="n">str_of_list</span> <span class="o">=</span> <span class="n">concatenate_sign</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">l</span><span class="p">)</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">list_obj</span><span class="p">)</span>
683
684    <span class="k">return</span> <span class="n">str_of_list</span></div>
685
686<div class="viewcode-block" id="make_dir"><a class="viewcode-back" href="../api.html#tools.make_dir">[docs]</a><span class="k">def</span> <span class="nf">make_dir</span><span class="p">(</span><span class="n">directory</span><span class="p">):</span>
687    <span class="sd">&#39;&#39;&#39;Creates a directory.</span>
688
689<span class="sd">    It gives a warning if the directory already exists and skips process.</span>
690<span class="sd">    The program stops only if there is another problem.</span>
691
692<span class="sd">    Parameters</span>
693<span class="sd">    ----------</span>
694<span class="sd">    directory : str</span>
695<span class="sd">        The path to directory which should be created.</span>
696
697<span class="sd">    Return</span>
698<span class="sd">    ------</span>
699
700<span class="sd">    &#39;&#39;&#39;</span>
701    <span class="k">try</span><span class="p">:</span>
702        <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">directory</span><span class="p">)</span>
703    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
704        <span class="c1"># errno.EEXIST = directory already exists</span>
705        <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EEXIST</span><span class="p">:</span>
706            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;WARNING: Directory </span><span class="si">{0}</span><span class="s1"> already exists!&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">directory</span><span class="p">))</span>
707        <span class="k">else</span><span class="p">:</span>
708            <span class="k">raise</span> <span class="c1"># re-raise exception if a different error occured</span>
709
710    <span class="k">return</span></div>
711
712<div class="viewcode-block" id="put_file_to_ecserver"><a class="viewcode-back" href="../api.html#tools.put_file_to_ecserver">[docs]</a><span class="k">def</span> <span class="nf">put_file_to_ecserver</span><span class="p">(</span><span class="n">ecd</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">ecuid</span><span class="p">,</span> <span class="n">ecgid</span><span class="p">):</span>
713    <span class="sd">&#39;&#39;&#39;Uses the ecaccess-file-put command to send a file to the ECMWF servers.</span>
714
715<span class="sd">    Note</span>
716<span class="sd">    ----</span>
717<span class="sd">    The return value is just for testing reasons. It does not have</span>
718<span class="sd">    to be used from the calling function since the whole error handling</span>
719<span class="sd">    is done in here.</span>
720
721<span class="sd">    Parameters</span>
722<span class="sd">    ----------</span>
723<span class="sd">    ecd : str</span>
724<span class="sd">        The path were the file is stored.</span>
725
726<span class="sd">    filename : str</span>
727<span class="sd">        The name of the file to send to the ECMWF server.</span>
728
729<span class="sd">    target : str</span>
730<span class="sd">        The target queue where the file should be sent to.</span>
731
732<span class="sd">    ecuid : str</span>
733<span class="sd">        The user id on ECMWF server.</span>
734
735<span class="sd">    ecgid : str</span>
736<span class="sd">        The group id on ECMWF server.</span>
737
738<span class="sd">    Return</span>
739<span class="sd">    ------</span>
740
741<span class="sd">    &#39;&#39;&#39;</span>
742
743    <span class="k">try</span><span class="p">:</span>
744        <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;ecaccess-file-put&#39;</span><span class="p">,</span>
745                                 <span class="n">ecd</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">,</span>
746                                 <span class="n">target</span> <span class="o">+</span> <span class="s1">&#39;:/home/ms/&#39;</span> <span class="o">+</span>
747                                 <span class="n">ecgid</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">ecuid</span> <span class="o">+</span>
748                                 <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">],</span>
749                                <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">STDOUT</span><span class="p">)</span>
750    <span class="k">except</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">CalledProcessError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
751        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">returncode</span><span class="p">))</span>
752        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
753
754        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">... Do you have a valid ecaccess certification key?&#39;</span><span class="p">)</span>
755        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... ECACCESS-FILE-PUT FAILED!&#39;</span><span class="p">)</span>
756    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
757        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span><span class="p">))</span>
758        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">))</span>
759
760        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">... Most likely the ECACCESS library is not available!&#39;</span><span class="p">)</span>
761        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... ECACCESS-FILE-PUT FAILED!&#39;</span><span class="p">)</span>
762
763    <span class="k">return</span></div>
764
765<div class="viewcode-block" id="submit_job_to_ecserver"><a class="viewcode-back" href="../api.html#tools.submit_job_to_ecserver">[docs]</a><span class="k">def</span> <span class="nf">submit_job_to_ecserver</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">jobname</span><span class="p">):</span>
766    <span class="sd">&#39;&#39;&#39;Uses ecaccess-job-submit command to submit a job to the ECMWF server.</span>
767
768<span class="sd">    Note</span>
769<span class="sd">    ----</span>
770<span class="sd">    The return value is just for testing reasons. It does not have</span>
771<span class="sd">    to be used from the calling function since the whole error handling</span>
772<span class="sd">    is done in here.</span>
773
774<span class="sd">    Parameters</span>
775<span class="sd">    ----------</span>
776<span class="sd">    target : str</span>
777<span class="sd">        The target where the file should be sent to, e.g. the queue.</span>
778
779<span class="sd">    jobname : str</span>
780<span class="sd">        The name of the jobfile to be submitted to the ECMWF server.</span>
781
782<span class="sd">    Return</span>
783<span class="sd">    ------</span>
784<span class="sd">    job_id : int</span>
785<span class="sd">        The id number of the job as a reference at the ecmwf server.</span>
786<span class="sd">    &#39;&#39;&#39;</span>
787
788    <span class="k">try</span><span class="p">:</span>
789        <span class="n">job_id</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;ecaccess-job-submit&#39;</span><span class="p">,</span> <span class="s1">&#39;-queueName&#39;</span><span class="p">,</span>
790                                          <span class="n">target</span><span class="p">,</span> <span class="n">jobname</span><span class="p">])</span>
791
792    <span class="k">except</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">CalledProcessError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
793        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">returncode</span><span class="p">))</span>
794        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
795
796        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">... Do you have a valid ecaccess certification key?&#39;</span><span class="p">)</span>
797        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... ECACCESS-JOB-SUBMIT FAILED!&#39;</span><span class="p">)</span>
798    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
799        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span><span class="p">))</span>
800        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">))</span>
801
802        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">... Most likely the ECACCESS library is not available!&#39;</span><span class="p">)</span>
803        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... ECACCESS-JOB-SUBMIT FAILED!&#39;</span><span class="p">)</span>
804
805    <span class="k">return</span> <span class="n">job_id</span></div>
806
807
808<div class="viewcode-block" id="get_informations"><a class="viewcode-back" href="../api.html#tools.get_informations">[docs]</a><span class="k">def</span> <span class="nf">get_informations</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
809    <span class="sd">&#39;&#39;&#39;Gets basic information from an example grib file.</span>
810
811<span class="sd">    These information are important for later use and the</span>
812<span class="sd">    initialization of numpy arrays for data storing.</span>
813
814<span class="sd">    Parameters</span>
815<span class="sd">    ----------</span>
816<span class="sd">    filename : str</span>
817<span class="sd">            Name of the file which will be opened to extract basic information.</span>
818
819<span class="sd">    Return</span>
820<span class="sd">    ------</span>
821<span class="sd">    data : dict</span>
822<span class="sd">        Contains basic informations of the ECMWF grib files, e.g.</span>
823<span class="sd">        &#39;Ni&#39;, &#39;Nj&#39;, &#39;latitudeOfFirstGridPointInDegrees&#39;,</span>
824<span class="sd">        &#39;longitudeOfFirstGridPointInDegrees&#39;, &#39;latitudeOfLastGridPointInDegrees&#39;,</span>
825<span class="sd">        &#39;longitudeOfLastGridPointInDegrees&#39;, &#39;jDirectionIncrementInDegrees&#39;,</span>
826<span class="sd">        &#39;iDirectionIncrementInDegrees&#39;, &#39;missingValue&#39;</span>
827<span class="sd">    &#39;&#39;&#39;</span>
828    <span class="kn">from</span> <span class="nn">eccodes</span> <span class="k">import</span> <span class="n">codes_grib_new_from_file</span><span class="p">,</span> <span class="n">codes_get</span><span class="p">,</span> <span class="n">codes_release</span>
829
830    <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
831
832    <span class="c1"># --- open file ---</span>
833    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Opening file for getting information data --- </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">filename</span><span class="p">)</span>
834    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
835        <span class="c1"># load first message from file</span>
836        <span class="n">gid</span> <span class="o">=</span> <span class="n">codes_grib_new_from_file</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
837
838        <span class="c1"># information needed from grib message</span>
839        <span class="n">keys</span> <span class="o">=</span> <span class="p">[</span>
840                <span class="s1">&#39;Ni&#39;</span><span class="p">,</span>
841                <span class="s1">&#39;Nj&#39;</span><span class="p">,</span>
842                <span class="s1">&#39;latitudeOfFirstGridPointInDegrees&#39;</span><span class="p">,</span>
843                <span class="s1">&#39;longitudeOfFirstGridPointInDegrees&#39;</span><span class="p">,</span>
844                <span class="s1">&#39;latitudeOfLastGridPointInDegrees&#39;</span><span class="p">,</span>
845                <span class="s1">&#39;longitudeOfLastGridPointInDegrees&#39;</span><span class="p">,</span>
846                <span class="s1">&#39;jDirectionIncrementInDegrees&#39;</span><span class="p">,</span>
847                <span class="s1">&#39;iDirectionIncrementInDegrees&#39;</span><span class="p">,</span>
848                <span class="s1">&#39;missingValue&#39;</span><span class="p">,</span>
849               <span class="p">]</span>
850
851        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Informations are: &#39;</span><span class="p">)</span>
852        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
853            <span class="c1"># Get the value of the key in a grib message.</span>
854            <span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">codes_get</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span><span class="n">key</span><span class="p">)</span>
855            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> = </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]))</span>
856
857        <span class="c1"># Free the memory for the message referred as gribid.</span>
858        <span class="n">codes_release</span><span class="p">(</span><span class="n">gid</span><span class="p">)</span>
859
860    <span class="k">return</span> <span class="n">data</span></div>
861
862
863<div class="viewcode-block" id="get_dimensions"><a class="viewcode-back" href="../api.html#tools.get_dimensions">[docs]</a><span class="k">def</span> <span class="nf">get_dimensions</span><span class="p">(</span><span class="n">info</span><span class="p">,</span> <span class="n">purefc</span><span class="p">,</span> <span class="n">dtime</span><span class="p">,</span> <span class="n">index_vals</span><span class="p">,</span> <span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">):</span>
864    <span class="sd">&#39;&#39;&#39;This function specifies the correct dimensions for x, y and t.</span>
865
866<span class="sd">    Parameters</span>
867<span class="sd">    ----------</span>
868<span class="sd">    info : dict</span>
869<span class="sd">        Contains basic informations of the ECMWF grib files, e.g.</span>
870<span class="sd">        &#39;Ni&#39;, &#39;Nj&#39;, &#39;latitudeOfFirstGridPointInDegrees&#39;,</span>
871<span class="sd">        &#39;longitudeOfFirstGridPointInDegrees&#39;, &#39;latitudeOfLastGridPointInDegrees&#39;,</span>
872<span class="sd">        &#39;longitudeOfLastGridPointInDegrees&#39;, &#39;jDirectionIncrementInDegrees&#39;,</span>
873<span class="sd">        &#39;iDirectionIncrementInDegrees&#39;, &#39;missingValue&#39;</span>
874
875<span class="sd">    purefc : int</span>
876<span class="sd">        Switch for definition of pure forecast mode or not.</span>
877
878<span class="sd">    dtime : str</span>
879<span class="sd">        Time step in hours.</span>
880
881<span class="sd">    index_vals : list of list of str</span>
882<span class="sd">        Contains the values from the keys used for a distinct selection</span>
883<span class="sd">        of grib messages in processing  the grib files.</span>
884<span class="sd">        Content looks like e.g.:</span>
885<span class="sd">        index_vals[0]: (&#39;20171106&#39;, &#39;20171107&#39;, &#39;20171108&#39;) ; date</span>
886<span class="sd">        index_vals[1]: (&#39;0&#39;, &#39;1200&#39;, &#39;1800&#39;, &#39;600&#39;) ; time</span>
887<span class="sd">        index_vals[2]: (&#39;0&#39;, &#39;12&#39;, &#39;3&#39;, &#39;6&#39;, &#39;9&#39;) ; stepRange</span>
888
889<span class="sd">    start_date : str</span>
890<span class="sd">        The start date of the retrieval job.</span>
891
892<span class="sd">    end_date : str</span>
893<span class="sd">        The end date of the retrieval job.</span>
894
895<span class="sd">    Return</span>
896<span class="sd">    ------</span>
897<span class="sd">    (ix, jy, it) : tuple of int</span>
898<span class="sd">        Dimension in x-direction, y-direction and in time.</span>
899<span class="sd">    &#39;&#39;&#39;</span>
900
901    <span class="n">ix</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="s1">&#39;Ni&#39;</span><span class="p">]</span>
902
903    <span class="n">jy</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="s1">&#39;Nj&#39;</span><span class="p">]</span>
904
905    <span class="k">if</span> <span class="ow">not</span> <span class="n">purefc</span><span class="p">:</span>
906        <span class="n">it</span> <span class="o">=</span> <span class="p">((</span><span class="n">end_date</span> <span class="o">-</span> <span class="n">start_date</span><span class="p">)</span><span class="o">.</span><span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">24</span><span class="o">/</span><span class="nb">int</span><span class="p">(</span><span class="n">dtime</span><span class="p">)</span>
907    <span class="k">else</span><span class="p">:</span>
908        <span class="c1"># #no of step * #no of times * #no of days</span>
909        <span class="n">it</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_vals</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_vals</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
910
911    <span class="k">return</span> <span class="p">(</span><span class="n">ix</span><span class="p">,</span> <span class="n">jy</span><span class="p">,</span> <span class="n">it</span><span class="p">)</span></div>
912
913
914<div class="viewcode-block" id="execute_subprocess"><a class="viewcode-back" href="../api.html#tools.execute_subprocess">[docs]</a><span class="k">def</span> <span class="nf">execute_subprocess</span><span class="p">(</span><span class="n">cmd_list</span><span class="p">,</span> <span class="n">error_msg</span><span class="o">=</span><span class="s1">&#39;SUBPROCESS FAILED!&#39;</span><span class="p">):</span>
915    <span class="sd">&#39;&#39;&#39;Executes a command line instruction via a subprocess.</span>
916
917<span class="sd">    Error handling is done if an error occures.</span>
918
919<span class="sd">    Parameters</span>
920<span class="sd">    ----------</span>
921<span class="sd">    cmd_list : list of str</span>
922<span class="sd">        A list of the components for the command line execution. Each</span>
923<span class="sd">        list entry is a single part of the command which is seperated from</span>
924<span class="sd">        the rest by a blank space.</span>
925<span class="sd">        E.g. [&#39;mv&#39;, file1, file2]</span>
926
927<span class="sd">    Return</span>
928<span class="sd">    ------</span>
929<span class="sd">    error_msg : str, optional</span>
930<span class="sd">        The possible error message if the subprocess failed.</span>
931<span class="sd">        By default it will just tell &quot;SUBPROCESS FAILED!&quot;.</span>
932<span class="sd">    &#39;&#39;&#39;</span>
933
934    <span class="k">try</span><span class="p">:</span>
935        <span class="n">subprocess</span><span class="o">.</span><span class="n">check_call</span><span class="p">(</span><span class="n">cmd_list</span><span class="p">)</span>
936    <span class="k">except</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">CalledProcessError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
937        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">returncode</span><span class="p">))</span>
938        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
939
940        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... &#39;</span> <span class="o">+</span> <span class="n">error_msg</span><span class="p">)</span>
941    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
942        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR CODE: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span><span class="p">))</span>
943        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... ERROR MESSAGE:</span><span class="se">\n</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">))</span>
944
945        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s1">&#39;... &#39;</span> <span class="o">+</span> <span class="n">error_msg</span><span class="p">)</span>
946
947    <span class="k">return</span></div>
948</pre></div>
949
950           </div>
951           
952          </div>
953          <footer>
954 
955
956  <hr/>
957
958  <div role="contentinfo">
959    <p>
960        &copy; Copyright 2018, Anne Philipp and Leopold Haimberger
961
962    </p>
963  </div>
964  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>.
965
966</footer>
967
968        </div>
969      </div>
970
971    </section>
972
973  </div>
974 
975
976
977 
978
979   
980   
981      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
982        <script type="text/javascript" src="../_static/jquery.js"></script>
983        <script type="text/javascript" src="../_static/underscore.js"></script>
984        <script type="text/javascript" src="../_static/doctools.js"></script>
985   
986
987 
988
989  <script type="text/javascript" src="../_static/js/theme.js"></script>
990
991  <script type="text/javascript">
992      jQuery(function () {
993          SphinxRtdTheme.Navigation.enable(true);
994      });
995  </script> 
996
997</body>
998</html>
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG