1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # TODO AP |
---|
5 | #AP |
---|
6 | # - Change History ist nicht angepasst ans File! Original geben lassen |
---|
7 | # - wieso cleanup in main wenn es in prepareflexpart bereits abgefragt wurde? |
---|
8 | # doppelt gemoppelt? |
---|
9 | # - wieso start=startm1 wenn basetime = 0 ? wenn die fluxes nicht mehr |
---|
10 | # relevant sind? verstehe ich nicht |
---|
11 | #************************************************************************ |
---|
12 | """ |
---|
13 | @Author: Anne Fouilloux (University of Oslo) |
---|
14 | |
---|
15 | @Date: October 2014 |
---|
16 | |
---|
17 | @ChangeHistory: |
---|
18 | November 2015 - Leopold Haimberger (University of Vienna): |
---|
19 | - using the WebAPI also for general MARS retrievals |
---|
20 | - job submission on ecgate and cca |
---|
21 | - job templates suitable for twice daily operational dissemination |
---|
22 | - dividing retrievals of longer periods into digestable chunks |
---|
23 | - retrieve also longer term forecasts, not only analyses and |
---|
24 | short term forecast data |
---|
25 | - conversion into GRIB2 |
---|
26 | - conversion into .fp format for faster execution of FLEXPART |
---|
27 | |
---|
28 | February 2018 - Anne Philipp (University of Vienna): |
---|
29 | - applied PEP8 style guide |
---|
30 | - added documentation |
---|
31 | - minor changes in programming style for consistence |
---|
32 | - BUG: removed call of cleanup-Function after call of prepareFlexpart |
---|
33 | since it is already called in prepareFlexpart at the end! |
---|
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 | @Requirements: |
---|
42 | - A standard python 2.6 or 2.7 installation |
---|
43 | - dateutils |
---|
44 | - ECMWF specific packages, all available from https://software.ecmwf.int/ |
---|
45 | ECMWF WebMARS, gribAPI with python enabled, emoslib and |
---|
46 | ecaccess web toolkit |
---|
47 | |
---|
48 | @Description: |
---|
49 | Further documentation may be obtained from www.flexpart.eu. |
---|
50 | |
---|
51 | Functionality provided: |
---|
52 | Prepare input 3D-wind fields in hybrid coordinates + |
---|
53 | surface fields for FLEXPART runs |
---|
54 | """ |
---|
55 | # ------------------------------------------------------------------------------ |
---|
56 | # MODULES |
---|
57 | # ------------------------------------------------------------------------------ |
---|
58 | import calendar |
---|
59 | import shutil |
---|
60 | import datetime |
---|
61 | import time |
---|
62 | import os |
---|
63 | import inspect |
---|
64 | import sys |
---|
65 | import socket |
---|
66 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
67 | import UIOFiles |
---|
68 | import Control |
---|
69 | import Tools |
---|
70 | import ECFlexpart |
---|
71 | |
---|
72 | hostname = socket.gethostname() |
---|
73 | ecapi = 'ecmwf' not in hostname |
---|
74 | try: |
---|
75 | if ecapi: |
---|
76 | import ecmwfapi |
---|
77 | except ImportError: |
---|
78 | ecapi = False |
---|
79 | |
---|
80 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
81 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
82 | if localpythonpath not in sys.path: |
---|
83 | sys.path.append(localpythonpath) |
---|
84 | # ------------------------------------------------------------------------------ |
---|
85 | # FUNCTION |
---|
86 | # ------------------------------------------------------------------------------ |
---|
87 | def prepareFLEXPART(args, c): |
---|
88 | ''' |
---|
89 | @Description: |
---|
90 | |
---|
91 | |
---|
92 | @Input: |
---|
93 | args: instance of ArgumentParser |
---|
94 | Contains the commandline arguments from script/program call. |
---|
95 | |
---|
96 | c: instance of class Control |
---|
97 | Contains all the parameters of control files, which are e.g.: |
---|
98 | DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, |
---|
99 | STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, |
---|
100 | LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY, |
---|
101 | OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT, |
---|
102 | ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR, |
---|
103 | MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME |
---|
104 | DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
105 | |
---|
106 | For more information about format and content of the parameter |
---|
107 | see documentation. |
---|
108 | |
---|
109 | @Return: |
---|
110 | <nothing> |
---|
111 | ''' |
---|
112 | |
---|
113 | if not args.ppid: |
---|
114 | c.ppid = str(os.getppid()) |
---|
115 | else: |
---|
116 | c.ppid = args.ppid |
---|
117 | |
---|
118 | c.ecapi = ecapi |
---|
119 | |
---|
120 | # create the start and end date |
---|
121 | start = datetime.date(year=int(c.start_date[:4]), |
---|
122 | month=int(c.start_date[4:6]), |
---|
123 | day=int(c.start_date[6:])) |
---|
124 | |
---|
125 | end = datetime.date(year=int(c.end_date[:4]), |
---|
126 | month=int(c.end_date[4:6]), |
---|
127 | day=int(c.end_date[6:])) |
---|
128 | |
---|
129 | # to deaccumulate the fluxes correctly |
---|
130 | # one day ahead of the start date and |
---|
131 | # one day after the end date is needed |
---|
132 | startm1 = start - datetime.timedelta(days=1) |
---|
133 | endp1 = end + datetime.timedelta(days=1) |
---|
134 | |
---|
135 | # get all files with flux data to be deaccumulated |
---|
136 | inputfiles = UIOFiles.UIOFiles(['.grib', '.grb', '.grib1', |
---|
137 | '.grib2', '.grb1', '.grb2']) |
---|
138 | |
---|
139 | inputfiles.listFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*') |
---|
140 | |
---|
141 | # create output dir if necessary |
---|
142 | if not os.path.exists(c.outputdir): |
---|
143 | os.makedirs(c.outputdir) |
---|
144 | |
---|
145 | # deaccumulate the flux data |
---|
146 | flexpart = ECFlexpart.ECFlexpart(c, fluxes=True) |
---|
147 | flexpart.write_namelist(c, 'fort.4') |
---|
148 | flexpart.deacc_fluxes(inputfiles, c) |
---|
149 | |
---|
150 | print('Prepare ' + start.strftime("%Y%m%d") + |
---|
151 | "/to/" + end.strftime("%Y%m%d")) |
---|
152 | |
---|
153 | # get a list of all files from the root inputdir |
---|
154 | inputfiles = UIOFiles.UIOFiles(['.grib', '.grb', '.grib1', |
---|
155 | '.grib2', '.grb1', '.grb2']) |
---|
156 | |
---|
157 | inputfiles.listFiles(c.inputdir, '????__??.*' + c.ppid + '.*') |
---|
158 | |
---|
159 | # produce FLEXPART-ready GRIB files and |
---|
160 | # process GRIB files - |
---|
161 | # copy/transfer/interpolate them or make them GRIB2 |
---|
162 | if c.basetime == '00': |
---|
163 | start = startm1 |
---|
164 | |
---|
165 | flexpart = ECFlexpart.ECFlexpart(c, fluxes=False) |
---|
166 | flexpart.create(inputfiles, c) |
---|
167 | flexpart.process_output(c) |
---|
168 | |
---|
169 | # check if in debugging mode, then store all files |
---|
170 | # otherwise delete temporary files |
---|
171 | if int(c.debug) != 0: |
---|
172 | print('Temporary files left intact') |
---|
173 | else: |
---|
174 | Tools.cleanup(c) |
---|
175 | |
---|
176 | return |
---|
177 | |
---|
178 | if __name__ == "__main__": |
---|
179 | args, c = Tools.interpret_args_and_control() |
---|
180 | prepareFLEXPART(args, c) |
---|