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

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

deleted jobscripts for tracking and updated Sphinx doc

  • Property mode set to 100644
File size: 89.8 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
225
226<span class="c1"># ------------------------------------------------------------------------------</span>
227<span class="c1"># METHODS</span>
228<span class="c1"># ------------------------------------------------------------------------------</span>
229
230<div class="viewcode-block" id="setup_controldata"><a class="viewcode-back" href="../api.html#tools.setup_controldata">[docs]</a><span class="k">def</span> <span class="nf">setup_controldata</span><span class="p">():</span>
231    <span class="sd">&#39;&#39;&#39;Collects, stores and checks controlling arguments from command line,</span>
232<span class="sd">    CONTROL file and ECMWF_ENV file.</span>
233
234<span class="sd">    Parameters</span>
235<span class="sd">    ----------</span>
236
237<span class="sd">    Return</span>
238<span class="sd">    ------</span>
239<span class="sd">    c : ControlFile</span>
240<span class="sd">        Contains all the parameters of CONTROL file and</span>
241<span class="sd">        command line.</span>
242
243<span class="sd">    ppid : str</span>
244<span class="sd">        Parent process id.</span>
245
246<span class="sd">    queue : str</span>
247<span class="sd">        Name of queue for submission to ECMWF (e.g. ecgate or cca )</span>
248
249<span class="sd">    job_template : str</span>
250<span class="sd">        Name of the job template file for submission to ECMWF server.</span>
251<span class="sd">    &#39;&#39;&#39;</span>
252    <span class="kn">import</span> <span class="nn">_config</span>
253    <span class="kn">from</span> <span class="nn">classes.ControlFile</span> <span class="k">import</span> <span class="n">ControlFile</span>
254
255    <span class="n">args</span> <span class="o">=</span> <span class="n">get_cmdline_args</span><span class="p">()</span>
256    <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>
257    <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>
258    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</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>
259        <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>
260        <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>
261    <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>
262
263    <span class="k">return</span> <span class="n">c</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">ppid</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">queue</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">job_template</span></div>
264
265<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>
266    <span class="sd">&#39;&#39;&#39;Converts the input string into pythons None-type if the string</span>
267<span class="sd">    contains string &quot;None&quot;.</span>
268
269<span class="sd">    Parameters</span>
270<span class="sd">    ----------</span>
271<span class="sd">    value : str</span>
272<span class="sd">        String to be checked for the &quot;None&quot; word.</span>
273
274<span class="sd">    Return</span>
275<span class="sd">    ------</span>
276<span class="sd">    None or value:</span>
277<span class="sd">        Return depends on the content of the input value. If it was &quot;None&quot;,</span>
278<span class="sd">        then the python type None is returned. Otherwise the string itself.</span>
279<span class="sd">    &#39;&#39;&#39;</span>
280    <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>
281        <span class="k">return</span> <span class="kc">None</span>
282    <span class="k">return</span> <span class="n">value</span></div>
283
284<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>
285    <span class="sd">&#39;&#39;&#39;Converts the input string into pythons None-type if the string</span>
286<span class="sd">    contains string &quot;None&quot;. Otherwise it is converted to an integer value.</span>
287
288<span class="sd">    Parameters</span>
289<span class="sd">    ----------</span>
290<span class="sd">    value : str</span>
291<span class="sd">        String to be checked for the &quot;None&quot; word.</span>
292
293<span class="sd">    Return</span>
294<span class="sd">    ------</span>
295<span class="sd">    None or int(value):</span>
296<span class="sd">        Return depends on the content of the input value. If it was &quot;None&quot;,</span>
297<span class="sd">        then the python type None is returned. Otherwise the string is</span>
298<span class="sd">        converted into an integer value.</span>
299<span class="sd">    &#39;&#39;&#39;</span>
300    <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>
301        <span class="k">return</span> <span class="kc">None</span>
302    <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span></div>
303
304<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>
305    <span class="sd">&#39;&#39;&#39;Decomposes the command line arguments and assigns them to variables.</span>
306<span class="sd">    Apply default values for non mentioned arguments.</span>
307
308<span class="sd">    Parameters</span>
309<span class="sd">    ----------</span>
310
311<span class="sd">    Return</span>
312<span class="sd">    ------</span>
313<span class="sd">    args : Namespace</span>
314<span class="sd">        Contains the commandline arguments from script/program call.</span>
315<span class="sd">    &#39;&#39;&#39;</span>
316
317    <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>
318<span class="s1">                                ECMWF MARS archive&#39;</span><span class="p">,</span>
319                            <span class="n">formatter_class</span><span class="o">=</span><span class="n">ArgumentDefaultsHelpFormatter</span><span class="p">)</span>
320
321    <span class="c1"># control parameters that override control file values</span>
322    <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>
323                        <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>
324                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;start date YYYYMMDD&quot;</span><span class="p">)</span>
325    <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>
326                        <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>
327                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;end_date YYYYMMDD&quot;</span><span class="p">)</span>
328    <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>
329                        <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>
330                        <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>
331    <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>
332                        <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>
333                        <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>
334    <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>
335                        <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>
336                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;file with CONTROL parameters&quot;</span><span class="p">)</span>
337    <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>
338                        <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>
339                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;base such as 0 or 12 (for half day retrievals)&quot;</span><span class="p">)</span>
340    <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>
341                        <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>
342                        <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>
343    <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>
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;Vertical levels to be retrieved, e.g. 30/to/60&quot;</span><span class="p">)</span>
346    <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>
347                        <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>
348                        <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>
349
350    <span class="c1"># some switches</span>
351    <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>
352                        <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>
353                        <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>
354    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--oper&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;oper&quot;</span><span class="p">,</span>
355                        <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>
356                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;operational mode - prepares dates with </span><span class="se">\</span>
357<span class="s2">                        environment variables&quot;</span><span class="p">)</span>
358    <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>
359                        <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>
360                        <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>
361    <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>
362                        <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>
363                        <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>
364    <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>
365                        <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>
366                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;select old or new precipitation interpolation </span><span class="se">\</span>
367<span class="s2">                        0 - old method</span><span class="se">\</span>
368<span class="s2">                        1 - new method (additional subgrid points)&quot;</span><span class="p">)</span>
369
370    <span class="c1"># set directories</span>
371    <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>
372                        <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>
373                        <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>
374    <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>
375                        <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>
376                        <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>
377    <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>
378                        <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>
379                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;FLEXPART root directory (to find grib2flexpart </span><span class="se">\</span>
380<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>
381<span class="s2">                        the scripts directory of the FLEXPART distribution&quot;</span><span class="p">)</span>
382
383    <span class="c1"># this is only used by prepare_flexpart.py to rerun a postprocessing step</span>
384    <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>
385                        <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>
386                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;specify parent process id for </span><span class="se">\</span>
387<span class="s2">                        rerun of prepare_flexpart&quot;</span><span class="p">)</span>
388
389    <span class="c1"># arguments for job submission to ECMWF, only needed by submit.py</span>
390    <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>
391                        <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>
392                        <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>
393    <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>
394                        <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>
395                        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;queue for submission to ECMWF </span><span class="se">\</span>
396<span class="s2">                        (e.g. ecgate or cca )&quot;</span><span class="p">)</span>
397
398    <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>
399
400    <span class="k">return</span> <span class="n">args</span></div>
401
402<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>
403    <span class="sd">&#39;&#39;&#39;Reads the file into a dictionary where the key values are the parameter</span>
404<span class="sd">    names.</span>
405
406<span class="sd">    Parameters</span>
407<span class="sd">    ----------</span>
408<span class="sd">    filepath : str</span>
409<span class="sd">        Path to file where the ECMWF environment parameters are stored.</span>
410
411<span class="sd">    Return</span>
412<span class="sd">    ------</span>
413<span class="sd">    envs : dict</span>
414<span class="sd">        Contains the environment parameter ecuid, ecgid, gateway</span>
415<span class="sd">        and destination for ECMWF server environments.</span>
416<span class="sd">    &#39;&#39;&#39;</span>
417    <span class="n">envs</span><span class="o">=</span> <span class="p">{}</span>
418    <span class="k">try</span><span class="p">:</span>
419        <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>
420            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
421                <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>
422                <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>
423    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
424        <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>
425        <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>
426
427        <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>
428                     <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>
429
430    <span class="k">return</span> <span class="n">envs</span></div>
431
432<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>
433    <span class="sd">&#39;&#39;&#39;Remove files from the intermediate directory (inputdir).</span>
434
435<span class="sd">    It keeps the final FLEXPART input files if program runs without</span>
436<span class="sd">    ECMWF Api and keywords &quot;ectrans&quot; or &quot;ecstorage&quot; are set to &quot;1&quot;.</span>
437
438<span class="sd">    Parameters</span>
439<span class="sd">    ----------</span>
440<span class="sd">    c : ControlFile</span>
441<span class="sd">        Contains all the parameters of CONTROL file and</span>
442<span class="sd">        command line.</span>
443
444<span class="sd">    Return</span>
445<span class="sd">    ------</span>
446
447<span class="sd">    &#39;&#39;&#39;</span>
448
449    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... clean inputdir!&quot;</span><span class="p">)</span>
450
451    <span class="n">cleanlist</span> <span class="o">=</span> <span class="p">[</span><span class="n">file</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</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>
452                 <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">basename</span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">prefix</span><span class="p">)]</span>
453
454    <span class="k">if</span> <span class="n">cleanlist</span><span class="p">:</span>
455        <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">cleanlist</span><span class="p">:</span>
456            <span class="n">silent_remove</span><span class="p">(</span><span class="n">element</span><span class="p">)</span>
457        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... done!&quot;</span><span class="p">)</span>
458    <span class="k">else</span><span class="p">:</span>
459        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;... nothing to clean!&quot;</span><span class="p">)</span>
460
461    <span class="k">return</span></div>
462
463
464<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">message</span><span class="o">=</span><span class="s1">&#39;ERROR&#39;</span><span class="p">):</span>
465    <span class="sd">&#39;&#39;&#39;Prints a specified error message which can be passed to the function</span>
466<span class="sd">    before exiting the program.</span>
467
468<span class="sd">    Parameters</span>
469<span class="sd">    ----------</span>
470<span class="sd">    message : str, optional</span>
471<span class="sd">        Error message. Default value is &quot;ERROR&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="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>
479    <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>
480
481    <span class="nb">print</span><span class="p">(</span><span class="n">full_message</span><span class="p">)</span>
482
483    <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>
484
485    <span class="k">return</span></div>
486
487
488<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>
489    <span class="sd">&#39;&#39;&#39;Prints a specific exit message which can be passed to the function.</span>
490
491<span class="sd">    Parameters</span>
492<span class="sd">    ----------</span>
493<span class="sd">    users : list of str</span>
494<span class="sd">        Contains all email addresses which should be notified.</span>
495<span class="sd">        It might also contain just the ecmwf user name which wil trigger</span>
496<span class="sd">        mailing to the associated email address for this user.</span>
497
498<span class="sd">    success_mode : str</span>
499<span class="sd">        States the exit mode of the program to put into</span>
500<span class="sd">        the mail subject line.</span>
501
502<span class="sd">    message : str, optional</span>
503<span class="sd">        Message for exiting program. Default value is &quot;Done!&quot;.</span>
504
505<span class="sd">    Return</span>
506<span class="sd">    ------</span>
507
508<span class="sd">    &#39;&#39;&#39;</span>
509
510    <span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="n">users</span><span class="p">:</span>
511        <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>
512            <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>
513        <span class="k">try</span><span class="p">:</span>
514            <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>
515                                  <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>
516                                 <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>
517                                 <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>
518                                 <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>
519                                 <span class="n">bufsize</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
520            <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>
521        <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
522            <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>
523            <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>
524        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
525            <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>
526            <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>
527            <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>
528        <span class="k">else</span><span class="p">:</span>
529            <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>
530
531    <span class="k">return</span></div>
532
533
534<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>
535    <span class="sd">&#39;&#39;&#39;Prints a specific exit message which can be passed to the function.</span>
536
537<span class="sd">    Parameters</span>
538<span class="sd">    ----------</span>
539<span class="sd">    message : str, optional</span>
540<span class="sd">        Message for exiting program. Default value is &quot;Done!&quot;.</span>
541
542<span class="sd">    Return</span>
543<span class="sd">    ------</span>
544
545<span class="sd">    &#39;&#39;&#39;</span>
546
547    <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>
548
549    <span class="k">return</span></div>
550
551
552<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>
553    <span class="sd">&#39;&#39;&#39;Creates combinations of all passed arguments.</span>
554
555<span class="sd">    This method combines the single characters of the passed arguments</span>
556<span class="sd">    with each other. So that each character of each argument value</span>
557<span class="sd">    will be combined with each character of the other arguments as a tuple.</span>
558
559<span class="sd">    Note</span>
560<span class="sd">    ----</span>
561<span class="sd">    This method is taken from an example at the ECMWF wiki website.</span>
562<span class="sd">    https://software.ecmwf.int/wiki/display/GRIB/index.py; 2018-03-16</span>
563
564<span class="sd">    Example</span>
565<span class="sd">    -------</span>
566<span class="sd">    product(&#39;ABCD&#39;, &#39;xy&#39;) --&gt; Ax Ay Bx By Cx Cy Dx Dy</span>
567
568<span class="sd">    product(range(2), repeat = 3) --&gt; 000 001 010 011 100 101 110 111</span>
569
570<span class="sd">    Parameters</span>
571<span class="sd">    ----------</span>
572<span class="sd">    \*args : list or str</span>
573<span class="sd">        Positional arguments (arbitrary number).</span>
574
575<span class="sd">    \*\*kwds : dict</span>
576<span class="sd">        Contains all the keyword arguments from \*args.</span>
577
578<span class="sd">    Return</span>
579<span class="sd">    ------</span>
580<span class="sd">    prod : :obj:`tuple`</span>
581<span class="sd">        Return will be done with &quot;yield&quot;. A tuple of combined arguments.</span>
582<span class="sd">        See example in description above.</span>
583<span class="sd">    &#39;&#39;&#39;</span>
584    <span class="k">try</span><span class="p">:</span>
585        <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>
586        <span class="n">result</span> <span class="o">=</span> <span class="p">[[]]</span>
587        <span class="k">for</span> <span class="n">pool</span> <span class="ow">in</span> <span class="n">pools</span><span class="p">:</span>
588            <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>
589        <span class="k">for</span> <span class="n">prod</span> <span class="ow">in</span> <span class="n">result</span><span class="p">:</span>
590            <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">prod</span><span class="p">)</span>
591    <span class="k">except</span> <span class="ne">TypeError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
592        <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>
593
594    <span class="k">return</span></div>
595
596
597<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>
598    <span class="sd">&#39;&#39;&#39;Remove file if it exists.</span>
599<span class="sd">    The function does not fail if the file does not exist.</span>
600
601<span class="sd">    Parameters</span>
602<span class="sd">    ----------</span>
603<span class="sd">    filename : str</span>
604<span class="sd">        The name of the file to be removed without notification.</span>
605
606<span class="sd">    Return</span>
607<span class="sd">    ------</span>
608
609<span class="sd">    &#39;&#39;&#39;</span>
610    <span class="k">try</span><span class="p">:</span>
611        <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>
612    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
613        <span class="c1"># errno.ENOENT  =  no such file or directory</span>
614        <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>
615            <span class="k">pass</span>
616        <span class="k">else</span><span class="p">:</span>
617            <span class="k">raise</span>  <span class="c1"># re-raise exception if a different error occured</span>
618
619    <span class="k">return</span></div>
620
621
622<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>
623    <span class="sd">&#39;&#39;&#39;Opens and reads the grib file with table 128 information.</span>
624
625<span class="sd">    Parameters</span>
626<span class="sd">    ----------</span>
627<span class="sd">    filepath : str</span>
628<span class="sd">        Path to file of ECMWF grib table number 128.</span>
629
630<span class="sd">    Return</span>
631<span class="sd">    ------</span>
632<span class="sd">    table128 : dict</span>
633<span class="sd">        Contains the ECMWF grib table 128 information.</span>
634<span class="sd">        The key is the parameter number and the value is the</span>
635<span class="sd">        short name of the parameter.</span>
636<span class="sd">    &#39;&#39;&#39;</span>
637    <span class="n">table128</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
638    <span class="k">try</span><span class="p">:</span>
639        <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>
640            <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>
641    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
642        <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>
643        <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>
644
645        <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>
646                 <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>
647    <span class="k">else</span><span class="p">:</span>
648        <span class="k">for</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">fdata</span><span class="p">:</span>
649            <span class="k">if</span> <span class="n">data</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span> <span class="ow">and</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>
650                <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">65</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
651
652    <span class="k">return</span> <span class="n">table128</span></div>
653
654
655<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>
656    <span class="sd">&#39;&#39;&#39;Transform parameter names to parameter ids with ECMWF grib table 128.</span>
657
658<span class="sd">    Parameters</span>
659<span class="sd">    ----------</span>
660<span class="sd">    pars : str</span>
661<span class="sd">        Addpar argument from CONTROL file in the format of</span>
662<span class="sd">        parameter names instead of ids. The parameter short</span>
663<span class="sd">        names are sepearted with &quot;/&quot; and they are passed as</span>
664<span class="sd">        one single string.</span>
665
666<span class="sd">    table : dict</span>
667<span class="sd">        Contains the ECMWF grib table 128 information.</span>
668<span class="sd">        The key is the parameter number and the value is the</span>
669<span class="sd">        short name of the parameter.</span>
670
671<span class="sd">    Return</span>
672<span class="sd">    ------</span>
673<span class="sd">    ipar : list of int</span>
674<span class="sd">        List of addpar parameters from CONTROL file transformed to</span>
675<span class="sd">        parameter ids in the format of integer.</span>
676<span class="sd">    &#39;&#39;&#39;</span>
677    <span class="k">if</span> <span class="ow">not</span> <span class="n">pars</span><span class="p">:</span>
678        <span class="k">return</span> <span class="p">[]</span>
679    <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>
680        <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>
681
682    <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>
683    <span class="n">ipar</span> <span class="o">=</span> <span class="p">[]</span>
684    <span class="k">for</span> <span class="n">par</span> <span class="ow">in</span> <span class="n">cpar</span><span class="p">:</span>
685        <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>
686            <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>
687                <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>
688                <span class="k">break</span>
689        <span class="k">else</span><span class="p">:</span>
690            <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>
691
692    <span class="k">return</span> <span class="n">ipar</span></div>
693
694<div class="viewcode-block" id="to_param_id_with_tablenumber"><a class="viewcode-back" href="../api.html#tools.to_param_id_with_tablenumber">[docs]</a><span class="k">def</span> <span class="nf">to_param_id_with_tablenumber</span><span class="p">(</span><span class="n">pars</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
695    <span class="sd">&#39;&#39;&#39;Transform parameter names to parameter ids and add table id.</span>
696
697<span class="sd">    Conversion with ECMWF grib table 128.</span>
698
699<span class="sd">    Parameters</span>
700<span class="sd">    ----------</span>
701<span class="sd">    pars : str</span>
702<span class="sd">        Addpar argument from CONTROL file in the format of</span>
703<span class="sd">        parameter names instead of ids. The parameter short</span>
704<span class="sd">        names are sepearted with &quot;/&quot; and they are passed as</span>
705<span class="sd">        one single string.</span>
706
707<span class="sd">    table : dict</span>
708<span class="sd">        Contains the ECMWF grib table 128 information.</span>
709<span class="sd">        The key is the parameter number and the value is the</span>
710<span class="sd">        short name of the parameter.</span>
711
712<span class="sd">    Return</span>
713<span class="sd">    ------</span>
714<span class="sd">    spar : str</span>
715<span class="sd">        List of addpar parameters from CONTROL file transformed to</span>
716<span class="sd">        parameter ids in the format of integer.</span>
717<span class="sd">    &#39;&#39;&#39;</span>
718    <span class="k">if</span> <span class="ow">not</span> <span class="n">pars</span><span class="p">:</span>
719        <span class="k">return</span> <span class="p">[]</span>
720    <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>
721        <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>
722
723    <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>
724    <span class="n">spar</span> <span class="o">=</span> <span class="p">[]</span>
725    <span class="k">for</span> <span class="n">par</span> <span class="ow">in</span> <span class="n">cpar</span><span class="p">:</span>
726        <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>
727            <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>
728                <span class="n">spar</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="s1">&#39;.128&#39;</span><span class="p">)</span>
729                <span class="k">break</span>
730        <span class="k">else</span><span class="p">:</span>
731            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n\n\t\t</span><span class="s1">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</span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="p">)</span>
732
733    <span class="k">return</span> <span class="s1">&#39;/&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">spar</span><span class="p">)</span></div>
734
735<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>
736    <span class="sd">&#39;&#39;&#39;Converts a list of arbitrary content into a single string.</span>
737
738<span class="sd">    Parameters</span>
739<span class="sd">    ----------</span>
740<span class="sd">    list_obj : list of *</span>
741<span class="sd">        A list with arbitrary content.</span>
742
743<span class="sd">    concatenate_sign : str, optional</span>
744<span class="sd">        A string which is used to concatenate the single</span>
745<span class="sd">        list elements. Default value is &quot;, &quot;.</span>
746
747<span class="sd">    Return</span>
748<span class="sd">    ------</span>
749<span class="sd">    str_of_list : str</span>
750<span class="sd">        The content of the list as a single string.</span>
751<span class="sd">    &#39;&#39;&#39;</span>
752
753    <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>
754        <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>
755    <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>
756
757    <span class="k">return</span> <span class="n">str_of_list</span></div>
758
759<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>
760    <span class="sd">&#39;&#39;&#39;Creates a directory.</span>
761
762<span class="sd">    It gives a warning if the directory already exists and skips process.</span>
763<span class="sd">    The program stops only if there is another problem.</span>
764
765<span class="sd">    Parameters</span>
766<span class="sd">    ----------</span>
767<span class="sd">    directory : str</span>
768<span class="sd">        The path to directory which should be created.</span>
769
770<span class="sd">    Return</span>
771<span class="sd">    ------</span>
772
773<span class="sd">    &#39;&#39;&#39;</span>
774    <span class="k">try</span><span class="p">:</span>
775        <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>
776    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
777        <span class="c1"># errno.EEXIST = directory already exists</span>
778        <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>
779            <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>
780        <span class="k">else</span><span class="p">:</span>
781            <span class="k">raise</span> <span class="c1"># re-raise exception if a different error occured</span>
782
783    <span class="k">return</span></div>
784
785<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>
786    <span class="sd">&#39;&#39;&#39;Uses the ecaccess-file-put command to send a file to the ECMWF servers.</span>
787
788<span class="sd">    Note</span>
789<span class="sd">    ----</span>
790<span class="sd">    The return value is just for testing reasons. It does not have</span>
791<span class="sd">    to be used from the calling function since the whole error handling</span>
792<span class="sd">    is done in here.</span>
793
794<span class="sd">    Parameters</span>
795<span class="sd">    ----------</span>
796<span class="sd">    ecd : str</span>
797<span class="sd">        The path were the file is stored.</span>
798
799<span class="sd">    filename : str</span>
800<span class="sd">        The name of the file to send to the ECMWF server.</span>
801
802<span class="sd">    target : str</span>
803<span class="sd">        The target queue where the file should be sent to.</span>
804
805<span class="sd">    ecuid : str</span>
806<span class="sd">        The user id on ECMWF server.</span>
807
808<span class="sd">    ecgid : str</span>
809<span class="sd">        The group id on ECMWF server.</span>
810
811<span class="sd">    Return</span>
812<span class="sd">    ------</span>
813
814<span class="sd">    &#39;&#39;&#39;</span>
815
816    <span class="k">try</span><span class="p">:</span>
817        <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>
818                                 <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>
819                                 <span class="n">target</span> <span class="o">+</span> <span class="s1">&#39;:/home/ms/&#39;</span> <span class="o">+</span>
820                                 <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>
821                                 <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">],</span>
822                                <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>
823    <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>
824        <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>
825        <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>
826
827        <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>
828        <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>
829    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
830        <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>
831        <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>
832
833        <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>
834        <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>
835
836    <span class="k">return</span></div>
837
838<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>
839    <span class="sd">&#39;&#39;&#39;Uses ecaccess-job-submit command to submit a job to the ECMWF server.</span>
840
841<span class="sd">    Note</span>
842<span class="sd">    ----</span>
843<span class="sd">    The return value is just for testing reasons. It does not have</span>
844<span class="sd">    to be used from the calling function since the whole error handling</span>
845<span class="sd">    is done in here.</span>
846
847<span class="sd">    Parameters</span>
848<span class="sd">    ----------</span>
849<span class="sd">    target : str</span>
850<span class="sd">        The target where the file should be sent to, e.g. the queue.</span>
851
852<span class="sd">    jobname : str</span>
853<span class="sd">        The name of the jobfile to be submitted to the ECMWF server.</span>
854
855<span class="sd">    Return</span>
856<span class="sd">    ------</span>
857<span class="sd">    job_id : int</span>
858<span class="sd">        The id number of the job as a reference at the ecmwf server.</span>
859<span class="sd">    &#39;&#39;&#39;</span>
860
861    <span class="k">try</span><span class="p">:</span>
862        <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>
863                                          <span class="n">target</span><span class="p">,</span> <span class="n">jobname</span><span class="p">])</span>
864
865    <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>
866        <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>
867        <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>
868
869        <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>
870        <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>
871    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
872        <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>
873        <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>
874
875        <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>
876        <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>
877
878    <span class="k">return</span> <span class="n">job_id</span></div>
879
880
881<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>
882    <span class="sd">&#39;&#39;&#39;Gets basic information from an example grib file.</span>
883
884<span class="sd">    These information are important for later use and the</span>
885<span class="sd">    initialization of numpy arrays for data storing.</span>
886
887<span class="sd">    Parameters</span>
888<span class="sd">    ----------</span>
889<span class="sd">    filename : str</span>
890<span class="sd">            Name of the file which will be opened to extract basic information.</span>
891
892<span class="sd">    Return</span>
893<span class="sd">    ------</span>
894<span class="sd">    data : dict</span>
895<span class="sd">        Contains basic informations of the ECMWF grib files, e.g.</span>
896<span class="sd">        &#39;Ni&#39;, &#39;Nj&#39;, &#39;latitudeOfFirstGridPointInDegrees&#39;,</span>
897<span class="sd">        &#39;longitudeOfFirstGridPointInDegrees&#39;, &#39;latitudeOfLastGridPointInDegrees&#39;,</span>
898<span class="sd">        &#39;longitudeOfLastGridPointInDegrees&#39;, &#39;jDirectionIncrementInDegrees&#39;,</span>
899<span class="sd">        &#39;iDirectionIncrementInDegrees&#39;, &#39;missingValue&#39;</span>
900<span class="sd">    &#39;&#39;&#39;</span>
901    <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>
902
903    <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
904
905    <span class="c1"># --- open file ---</span>
906    <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>
907    <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>
908        <span class="c1"># load first message from file</span>
909        <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>
910
911        <span class="c1"># information needed from grib message</span>
912        <span class="n">keys</span> <span class="o">=</span> <span class="p">[</span>
913                <span class="s1">&#39;Ni&#39;</span><span class="p">,</span>
914                <span class="s1">&#39;Nj&#39;</span><span class="p">,</span>
915                <span class="s1">&#39;latitudeOfFirstGridPointInDegrees&#39;</span><span class="p">,</span>
916                <span class="s1">&#39;longitudeOfFirstGridPointInDegrees&#39;</span><span class="p">,</span>
917                <span class="s1">&#39;latitudeOfLastGridPointInDegrees&#39;</span><span class="p">,</span>
918                <span class="s1">&#39;longitudeOfLastGridPointInDegrees&#39;</span><span class="p">,</span>
919                <span class="s1">&#39;jDirectionIncrementInDegrees&#39;</span><span class="p">,</span>
920                <span class="s1">&#39;iDirectionIncrementInDegrees&#39;</span><span class="p">,</span>
921                <span class="s1">&#39;missingValue&#39;</span><span class="p">,</span>
922               <span class="p">]</span>
923
924        <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>
925        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
926            <span class="c1"># Get the value of the key in a grib message.</span>
927            <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>
928            <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>
929
930        <span class="c1"># Free the memory for the message referred as gribid.</span>
931        <span class="n">codes_release</span><span class="p">(</span><span class="n">gid</span><span class="p">)</span>
932
933    <span class="k">return</span> <span class="n">data</span></div>
934
935
936<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>
937    <span class="sd">&#39;&#39;&#39;This function specifies the correct dimensions for x, y and t.</span>
938
939<span class="sd">    Parameters</span>
940<span class="sd">    ----------</span>
941<span class="sd">    info : dict</span>
942<span class="sd">        Contains basic informations of the ECMWF grib files, e.g.</span>
943<span class="sd">        &#39;Ni&#39;, &#39;Nj&#39;, &#39;latitudeOfFirstGridPointInDegrees&#39;,</span>
944<span class="sd">        &#39;longitudeOfFirstGridPointInDegrees&#39;, &#39;latitudeOfLastGridPointInDegrees&#39;,</span>
945<span class="sd">        &#39;longitudeOfLastGridPointInDegrees&#39;, &#39;jDirectionIncrementInDegrees&#39;,</span>
946<span class="sd">        &#39;iDirectionIncrementInDegrees&#39;, &#39;missingValue&#39;</span>
947
948<span class="sd">    purefc : int</span>
949<span class="sd">        Switch for definition of pure forecast mode or not.</span>
950
951<span class="sd">    dtime : str</span>
952<span class="sd">        Time step in hours.</span>
953
954<span class="sd">    index_vals : list of list of str</span>
955<span class="sd">        Contains the values from the keys used for a distinct selection</span>
956<span class="sd">        of grib messages in processing  the grib files.</span>
957<span class="sd">        Content looks like e.g.:</span>
958<span class="sd">        index_vals[0]: (&#39;20171106&#39;, &#39;20171107&#39;, &#39;20171108&#39;) ; date</span>
959<span class="sd">        index_vals[1]: (&#39;0&#39;, &#39;1200&#39;, &#39;1800&#39;, &#39;600&#39;) ; time</span>
960<span class="sd">        index_vals[2]: (&#39;0&#39;, &#39;12&#39;, &#39;3&#39;, &#39;6&#39;, &#39;9&#39;) ; stepRange</span>
961
962<span class="sd">    start_date : str</span>
963<span class="sd">        The start date of the retrieval job.</span>
964
965<span class="sd">    end_date : str</span>
966<span class="sd">        The end date of the retrieval job.</span>
967
968<span class="sd">    Return</span>
969<span class="sd">    ------</span>
970<span class="sd">    (ix, jy, it) : tuple of int</span>
971<span class="sd">        Dimension in x-direction, y-direction and in time.</span>
972<span class="sd">    &#39;&#39;&#39;</span>
973
974    <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>
975
976    <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>
977
978    <span class="k">if</span> <span class="ow">not</span> <span class="n">purefc</span><span class="p">:</span>
979        <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>
980    <span class="k">else</span><span class="p">:</span>
981        <span class="c1"># #no of step * #no of times * #no of days</span>
982        <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>
983
984    <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>
985
986
987<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>
988    <span class="sd">&#39;&#39;&#39;Executes a command line instruction via a subprocess.</span>
989
990<span class="sd">    Error handling is done if an error occures.</span>
991
992<span class="sd">    Parameters</span>
993<span class="sd">    ----------</span>
994<span class="sd">    cmd_list : list of str</span>
995<span class="sd">        A list of the components for the command line execution. Each</span>
996<span class="sd">        list entry is a single part of the command which is seperated from</span>
997<span class="sd">        the rest by a blank space.</span>
998<span class="sd">        E.g. [&#39;mv&#39;, file1, file2]</span>
999
1000<span class="sd">    Return</span>
1001<span class="sd">    ------</span>
1002<span class="sd">    error_msg : str, optional</span>
1003<span class="sd">        The possible error message if the subprocess failed.</span>
1004<span class="sd">        By default it will just tell &quot;SUBPROCESS FAILED!&quot;.</span>
1005<span class="sd">    &#39;&#39;&#39;</span>
1006
1007    <span class="k">try</span><span class="p">:</span>
1008        <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>
1009    <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>
1010        <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>
1011        <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>
1012
1013        <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>
1014    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
1015        <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>
1016        <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>
1017
1018        <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>
1019
1020    <span class="k">return</span></div>
1021</pre></div>
1022
1023           </div>
1024           
1025          </div>
1026          <footer>
1027 
1028
1029  <hr/>
1030
1031  <div role="contentinfo">
1032    <p>
1033        &copy; Copyright 2018, Anne Philipp and Leopold Haimberger.
1034
1035    </p>
1036  </div>
1037  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>.
1038
1039</footer>
1040
1041        </div>
1042      </div>
1043
1044    </section>
1045
1046  </div>
1047 
1048
1049
1050 
1051
1052    <script type="text/javascript">
1053        var DOCUMENTATION_OPTIONS = {
1054            URL_ROOT:'../',
1055            VERSION:'7.1 alpha',
1056            LANGUAGE:'None',
1057            COLLAPSE_INDEX:false,
1058            FILE_SUFFIX:'.html',
1059            HAS_SOURCE:  true,
1060            SOURCELINK_SUFFIX: '.txt'
1061        };
1062    </script>
1063      <script type="text/javascript" src="../_static/jquery.js"></script>
1064      <script type="text/javascript" src="../_static/underscore.js"></script>
1065      <script type="text/javascript" src="../_static/doctools.js"></script>
1066      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
1067
1068 
1069
1070  <script type="text/javascript" src="../_static/js/theme.js"></script>
1071
1072  <script type="text/javascript">
1073      jQuery(function () {
1074          SphinxRtdTheme.Navigation.enable(true);
1075      });
1076  </script> 
1077
1078</body>
1079</html>
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG