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

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

new build of sphinx for updated api

  • Property mode set to 100644
File size: 23.0 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>prepare_flexpart &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>prepare_flexpart</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 prepare_flexpart</h1><div class="highlight"><pre>
154<span></span><span class="ch">#!/usr/bin/env python</span>
155<span class="c1"># -*- coding: utf-8 -*-</span>
156<span class="c1">#*******************************************************************************</span>
157<span class="c1"># @Author: Anne Fouilloux (University of Oslo)</span>
158<span class="c1">#</span>
159<span class="c1"># @Date: October 2014</span>
160<span class="c1">#</span>
161<span class="c1"># @Change History:</span>
162<span class="c1">#</span>
163<span class="c1">#    November 2015 - Leopold Haimberger (University of Vienna):</span>
164<span class="c1">#        - using the WebAPI also for general MARS retrievals</span>
165<span class="c1">#        - job submission on ecgate and cca</span>
166<span class="c1">#        - job templates suitable for twice daily operational dissemination</span>
167<span class="c1">#        - dividing retrievals of longer periods into digestable chunks</span>
168<span class="c1">#        - retrieve also longer term forecasts, not only analyses and</span>
169<span class="c1">#          short term forecast data</span>
170<span class="c1">#        - conversion into GRIB2</span>
171<span class="c1">#        - conversion into .fp format for faster execution of FLEXPART</span>
172<span class="c1">#</span>
173<span class="c1">#    February 2018 - Anne Philipp (University of Vienna):</span>
174<span class="c1">#        - applied PEP8 style guide</span>
175<span class="c1">#        - added documentation</span>
176<span class="c1">#        - minor changes in programming style for consistence</span>
177<span class="c1">#        - BUGFIX: removed call of clean_up-Function after call of</span>
178<span class="c1">#               prepareFlexpart in main since it is already called in</span>
179<span class="c1">#               prepareFlexpart at the end!</span>
180<span class="c1">#        - created function main and moved the two function calls for</span>
181<span class="c1">#          arguments and prepare_flexpart into it</span>
182<span class="c1">#</span>
183<span class="c1"># @License:</span>
184<span class="c1">#    (C) Copyright 2014-2019.</span>
185<span class="c1">#    Anne Philipp, Leopold Haimberger</span>
186<span class="c1">#</span>
187<span class="c1">#    This work is licensed under the Creative Commons Attribution 4.0</span>
188<span class="c1">#    International License. To view a copy of this license, visit</span>
189<span class="c1">#    http://creativecommons.org/licenses/by/4.0/ or send a letter to</span>
190<span class="c1">#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.</span>
191<span class="c1">#*******************************************************************************</span>
192<span class="sd">&#39;&#39;&#39;This script prepares the final version of the grib files which are</span>
193<span class="sd">then used by FLEXPART.</span>
194
195<span class="sd">It converts the bunch of grib files extracted via get_mars_data before,</span>
196<span class="sd">by doing the necessary conversion to get consistent grids or the</span>
197<span class="sd">disaggregation of flux data. Finally, the data fields are combined</span>
198<span class="sd">in files per available hour with the naming convention xxYYMMDDHH,</span>
199<span class="sd">where xx should be 2 arbitrary letters (mostly xx is chosen to be &quot;EN&quot;).</span>
200
201<span class="sd">This file can also be imported as a module which then contains the following</span>
202<span class="sd">functions:</span>
203
204<span class="sd">    * main</span>
205<span class="sd">    * prepare_flexpart</span>
206
207<span class="sd">Type: prepare_flexpart.py --help</span>
208<span class="sd">to get information about command line parameters.</span>
209<span class="sd">Read the documentation for usage instructions.</span>
210<span class="sd">&#39;&#39;&#39;</span>
211
212<span class="c1"># ------------------------------------------------------------------------------</span>
213<span class="c1"># MODULES</span>
214<span class="c1"># ------------------------------------------------------------------------------</span>
215<span class="kn">import</span> <span class="nn">datetime</span>
216<span class="kn">import</span> <span class="nn">os</span>
217<span class="kn">import</span> <span class="nn">inspect</span>
218<span class="kn">import</span> <span class="nn">sys</span>
219<span class="kn">import</span> <span class="nn">socket</span>
220
221<span class="c1"># software specific classes and modules from flex_extract</span>
222<span class="c1"># add path to local main python path for flex_extract to get full access</span>
223<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span>
224    <span class="n">inspect</span><span class="o">.</span><span class="n">getfile</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">currentframe</span><span class="p">())))</span> <span class="o">+</span> <span class="s1">&#39;/../&#39;</span><span class="p">)</span>
225<span class="kn">import</span> <span class="nn">_config</span>
226<span class="kn">from</span> <span class="nn">checks</span> <span class="k">import</span> <span class="n">check_ppid</span>
227<span class="kn">from</span> <span class="nn">classes.UioFiles</span> <span class="k">import</span> <span class="n">UioFiles</span>
228<span class="kn">from</span> <span class="nn">classes.ControlFile</span> <span class="k">import</span> <span class="n">ControlFile</span>
229<span class="kn">from</span> <span class="nn">tools</span> <span class="k">import</span> <span class="n">clean_up</span><span class="p">,</span> <span class="n">get_cmdline_args</span><span class="p">,</span> <span class="n">read_ecenv</span><span class="p">,</span> <span class="n">make_dir</span>
230<span class="kn">from</span> <span class="nn">classes.EcFlexpart</span> <span class="k">import</span> <span class="n">EcFlexpart</span>
231
232<span class="n">ecapi</span> <span class="o">=</span> <span class="s1">&#39;ecmwf&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">socket</span><span class="o">.</span><span class="n">gethostname</span><span class="p">()</span>
233<span class="k">try</span><span class="p">:</span>
234    <span class="k">if</span> <span class="n">ecapi</span><span class="p">:</span>
235        <span class="kn">import</span> <span class="nn">ecmwfapi</span>
236<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
237    <span class="n">ecapi</span> <span class="o">=</span> <span class="kc">False</span>
238
239<span class="c1"># ------------------------------------------------------------------------------</span>
240<span class="c1"># FUNCTION</span>
241<span class="c1"># ------------------------------------------------------------------------------</span>
242<div class="viewcode-block" id="main"><a class="viewcode-back" href="../api.html#prepare_flexpart.main">[docs]</a><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
243    <span class="sd">&#39;&#39;&#39;Controls the program to prepare flexpart input files from mars data.</span>
244
245<span class="sd">    This is done if it is called directly from command line.</span>
246<span class="sd">    Then it also takes program call arguments and control file input.</span>
247
248<span class="sd">    Parameters</span>
249<span class="sd">    ----------</span>
250
251<span class="sd">    Return</span>
252<span class="sd">    ------</span>
253
254<span class="sd">    &#39;&#39;&#39;</span>
255
256    <span class="n">args</span> <span class="o">=</span> <span class="n">get_cmdline_args</span><span class="p">()</span>
257    <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>
258
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_args_to_control</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
261    <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>
262    <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>
263
264    <span class="n">prepare_flexpart</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">c</span><span class="p">)</span>
265
266    <span class="k">return</span></div>
267
268<div class="viewcode-block" id="prepare_flexpart"><a class="viewcode-back" href="../api.html#prepare_flexpart.prepare_flexpart">[docs]</a><span class="k">def</span> <span class="nf">prepare_flexpart</span><span class="p">(</span><span class="n">ppid</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span>
269    <span class="sd">&#39;&#39;&#39;Converts the mars data into flexpart ready input files.</span>
270
271<span class="sd">    Specific data fields are converted to a different grid and the flux</span>
272<span class="sd">    data are going to be disaggregated. The data fields are collected by</span>
273<span class="sd">    hour and stored in a file with a specific FLEXPART relevant naming</span>
274<span class="sd">    convention.</span>
275
276<span class="sd">    Parameters</span>
277<span class="sd">    ----------</span>
278<span class="sd">    ppid : int</span>
279<span class="sd">        Contains the ppid number of the current ECMWF job. It will be None if</span>
280<span class="sd">        the method was called within this module.</span>
281
282<span class="sd">    c : ControlFile</span>
283<span class="sd">        Contains all the parameters of CONTROL file and</span>
284<span class="sd">        command line.</span>
285
286<span class="sd">    Return</span>
287<span class="sd">    ------</span>
288
289<span class="sd">    &#39;&#39;&#39;</span>
290    <span class="n">check_ppid</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">ppid</span><span class="p">)</span>
291
292    <span class="n">c</span><span class="o">.</span><span class="n">ecapi</span> <span class="o">=</span> <span class="n">ecapi</span>
293
294    <span class="c1"># create the start and end date</span>
295    <span class="n">start</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">start_date</span><span class="p">[:</span><span class="mi">4</span><span class="p">]),</span>
296                          <span class="n">month</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">start_date</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">6</span><span class="p">]),</span>
297                          <span class="n">day</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">start_date</span><span class="p">[</span><span class="mi">6</span><span class="p">:]))</span>
298
299    <span class="n">end</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">end_date</span><span class="p">[:</span><span class="mi">4</span><span class="p">]),</span>
300                        <span class="n">month</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">end_date</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">6</span><span class="p">]),</span>
301                        <span class="n">day</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">end_date</span><span class="p">[</span><span class="mi">6</span><span class="p">:]))</span>
302
303    <span class="c1"># if basetime is 00</span>
304    <span class="c1"># assign starting date minus 1 day</span>
305    <span class="c1"># since we need the 12 hours upfront</span>
306    <span class="c1"># (the day before from 12 UTC to current day 00 UTC)</span>
307    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">basetime</span> <span class="o">==</span> <span class="s1">&#39;00&#39;</span><span class="p">:</span>
308        <span class="n">start</span> <span class="o">=</span> <span class="n">start</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
309
310    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Prepare &#39;</span> <span class="o">+</span> <span class="n">start</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span> <span class="o">+</span>
311           <span class="s2">&quot;/to/&quot;</span> <span class="o">+</span> <span class="n">end</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">))</span>
312
313    <span class="c1"># create output dir if necessary</span>
314    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">outputdir</span><span class="p">):</span>
315        <span class="n">make_dir</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">outputdir</span><span class="p">)</span>
316
317    <span class="c1"># get all files with flux data to be deaccumulated</span>
318    <span class="n">inputfiles</span> <span class="o">=</span> <span class="n">UioFiles</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="s1">&#39;*OG_acc_SL*.&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">ppid</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;.*&#39;</span><span class="p">)</span>
319
320    <span class="c1"># deaccumulate the flux data</span>
321    <span class="n">flexpart</span> <span class="o">=</span> <span class="n">EcFlexpart</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
322    <span class="n">flexpart</span><span class="o">.</span><span class="n">write_namelist</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
323    <span class="n">flexpart</span><span class="o">.</span><span class="n">deacc_fluxes</span><span class="p">(</span><span class="n">inputfiles</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
324
325    <span class="c1"># get a list of all other files</span>
326    <span class="n">inputfiles</span> <span class="o">=</span> <span class="n">UioFiles</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="s1">&#39;????__??.*&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">ppid</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;.*&#39;</span><span class="p">)</span>
327
328    <span class="c1"># produce FLEXPART-ready GRIB files and process them -</span>
329    <span class="c1"># copy/transfer/interpolate them or make them GRIB2</span>
330    <span class="n">flexpart</span> <span class="o">=</span> <span class="n">EcFlexpart</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">fluxes</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
331    <span class="n">flexpart</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">inputfiles</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
332    <span class="n">flexpart</span><span class="o">.</span><span class="n">process_output</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
333
334    <span class="c1"># make use of a possible conversion to a</span>
335    <span class="c1"># specific flexpart binary format</span>
336    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">grib2flexpart</span><span class="p">:</span>
337        <span class="n">flexpart</span><span class="o">.</span><span class="n">prepare_fp_files</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
338
339    <span class="c1"># check if in debugging mode, then store all files</span>
340    <span class="c1"># otherwise delete temporary files</span>
341    <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">debug</span><span class="p">:</span>
342        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Temporary files left intact&#39;</span><span class="p">)</span>
343    <span class="k">else</span><span class="p">:</span>
344        <span class="n">clean_up</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
345
346    <span class="k">return</span></div>
347
348<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
349    <span class="n">main</span><span class="p">()</span>
350</pre></div>
351
352           </div>
353           
354          </div>
355          <footer>
356 
357
358  <hr/>
359
360  <div role="contentinfo">
361    <p>
362        &copy; Copyright 2018, Anne Philipp and Leopold Haimberger
363
364    </p>
365  </div>
366  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>.
367
368</footer>
369
370        </div>
371      </div>
372
373    </section>
374
375  </div>
376 
377
378
379 
380
381   
382   
383      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
384        <script type="text/javascript" src="../_static/jquery.js"></script>
385        <script type="text/javascript" src="../_static/underscore.js"></script>
386        <script type="text/javascript" src="../_static/doctools.js"></script>
387   
388
389 
390
391  <script type="text/javascript" src="../_static/js/theme.js"></script>
392
393  <script type="text/javascript">
394      jQuery(function () {
395          SphinxRtdTheme.Navigation.enable(true);
396      });
397  </script> 
398
399</body>
400</html>
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG