source: flex_extract.git/python/prepareFLEXPART.py @ 991df6a

ctbtodev
Last change on this file since 991df6a was 991df6a, checked in by Anne Philipp <anne.philipp@…>, 6 years ago

finished documentation (except plot_retrieved)

  • Property mode set to 100755
File size: 7.0 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#************************************************************************
4# TODO AP
5# - wieso cleanup in main wenn es in prepareflexpart bereits abgefragt wurde?
6#   doppelt gemoppelt?
7# - wieso start=startm1 wenn basetime = 0 ?  wenn die fluxes nicht mehr
8#   relevant sind? verstehe ich nicht
9#************************************************************************
10#*******************************************************************************
11# @Author: Anne Fouilloux (University of Oslo)
12#
13# @Date: October 2014
14#
15# @Change History:
16#
17#    November 2015 - Leopold Haimberger (University of Vienna):
18#        - using the WebAPI also for general MARS retrievals
19#        - job submission on ecgate and cca
20#        - job templates suitable for twice daily operational dissemination
21#        - dividing retrievals of longer periods into digestable chunks
22#        - retrieve also longer term forecasts, not only analyses and
23#          short term forecast data
24#        - conversion into GRIB2
25#        - conversion into .fp format for faster execution of FLEXPART
26#
27#    February 2018 - Anne Philipp (University of Vienna):
28#        - applied PEP8 style guide
29#        - added documentation
30#        - minor changes in programming style for consistence
31#        - BUG: removed call of cleanup-Function after call of
32#               prepareFlexpart in main since it is already called in
33#               prepareFlexpart at the end!
34#        - created function main and moved the two function calls for
35#          arguments and prepareFLEXPART into it
36#
37# @License:
38#    (C) Copyright 2014-2018.
39#
40#    This software is licensed under the terms of the Apache Licence Version 2.0
41#    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
42#
43# @Program Functionality:
44#    This program prepares the final version of the grib files which are
45#    then used by FLEXPART. It converts the bunch of grib files extracted
46#    via getMARSdata by doing for example the necessary conversion to get
47#    consistent grids or the disaggregation of flux data. Finally, the
48#    program combines the data fields in files per available hour with the
49#    naming convention xxYYMMDDHH, where xx should be 2 arbitrary letters
50#    (mostly xx is chosen to be "EN").
51#
52# @Program Content:
53#    - main
54#    - prepareFLEXPART
55#
56#*******************************************************************************
57
58# ------------------------------------------------------------------------------
59# MODULES
60# ------------------------------------------------------------------------------
61import shutil
62import datetime
63#import time
64import os
65import inspect
66import sys
67import socket
68from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
69
70hostname = socket.gethostname()
71ecapi = 'ecmwf' not in hostname
72try:
73    if ecapi:
74        import ecmwfapi
75except ImportError:
76    ecapi = False
77
78# add path to pythonpath so that python finds its buddies
79localpythonpath = os.path.dirname(os.path.abspath(
80    inspect.getfile(inspect.currentframe())))
81if localpythonpath not in sys.path:
82    sys.path.append(localpythonpath)
83
84# software specific classes and modules from flex_extract
85from UIOFiles import UIOFiles
86from Tools import interpret_args_and_control, cleanup
87from ECFlexpart import ECFlexpart
88# ------------------------------------------------------------------------------
89# FUNCTION
90# ------------------------------------------------------------------------------
91def main():
92    '''
93    @Description:
94        If prepareFLEXPART is called from command line, this function controls
95        the program flow and calls the argumentparser function and
96        the prepareFLEXPART function for preparation of GRIB data for FLEXPART.
97
98    @Input:
99        <nothing>
100
101    @Return:
102        <nothing>
103    '''
104    args, c = interpret_args_and_control()
105    prepareFLEXPART(args, c)
106
107    return
108
109def prepareFLEXPART(args, c):
110    '''
111    @Description:
112        Lists all grib files retrieved from MARS with getMARSdata and
113        uses prepares data for the use in FLEXPART. Specific data fields
114        are converted to a different grid and the flux data are going to be
115        disaggregated. The data fields are collected by hour and stored in
116        a file with a specific FLEXPART relevant naming convention.
117
118    @Input:
119        args: instance of ArgumentParser
120            Contains the commandline arguments from script/program call.
121
122        c: instance of class ControlFile
123            Contains all the parameters of CONTROL file, which are e.g.:
124            DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME,
125            STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT,
126            LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY,
127            OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT,
128            ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR,
129            MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME
130            DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS
131
132            For more information about format and content of the parameter
133            see documentation.
134
135    @Return:
136        <nothing>
137    '''
138
139    if not args.ppid:
140        c.ppid = str(os.getppid())
141    else:
142        c.ppid = args.ppid
143
144    c.ecapi = ecapi
145
146    # create the start and end date
147    start = datetime.date(year=int(c.start_date[:4]),
148                          month=int(c.start_date[4:6]),
149                          day=int(c.start_date[6:]))
150
151    end = datetime.date(year=int(c.end_date[:4]),
152                        month=int(c.end_date[4:6]),
153                        day=int(c.end_date[6:]))
154
155    # to deaccumulate the fluxes correctly
156    # one day ahead of the start date and
157    # one day after the end date is needed
158    startm1 = start - datetime.timedelta(days=1)
159    endp1 = end + datetime.timedelta(days=1)
160
161    # get all files with flux data to be deaccumulated
162    inputfiles = UIOFiles('*OG_acc_SL*.' + c.ppid + '.*')
163    inputfiles.listFiles(c.inputdir)
164
165    # create output dir if necessary
166    if not os.path.exists(c.outputdir):
167        os.makedirs(c.outputdir)
168
169    # deaccumulate the flux data
170    flexpart = ECFlexpart(c, fluxes=True)
171    flexpart.write_namelist(c, 'fort.4')
172    flexpart.deacc_fluxes(inputfiles, c)
173
174    print('Prepare ' + start.strftime("%Y%m%d") +
175          "/to/" + end.strftime("%Y%m%d"))
176
177    # get a list of all files from the root inputdir
178    inputfiles = UIOFiles('????__??.*' + c.ppid + '.*')
179    inputfiles.listFiles(c.inputdir)
180
181    # produce FLEXPART-ready GRIB files and
182    # process GRIB files -
183    # copy/transfer/interpolate them or make them GRIB2
184    if c.basetime == '00':
185        start = startm1
186
187    flexpart = ECFlexpart(c, fluxes=False)
188    flexpart.create(inputfiles, c)
189    flexpart.process_output(c)
190
191    # check if in debugging mode, then store all files
192    # otherwise delete temporary files
193    if int(c.debug) != 0:
194        print('Temporary files left intact')
195    else:
196        cleanup(c)
197
198    return
199
200if __name__ == "__main__":
201    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG