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