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 | |
---|
33 | @License: |
---|
34 | (C) Copyright 2014. |
---|
35 | |
---|
36 | This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
37 | which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
38 | |
---|
39 | @Requirements: |
---|
40 | - A standard python 2.6 or 2.7 installation |
---|
41 | - dateutils |
---|
42 | - matplotlib (optional, for debugging) |
---|
43 | - ECMWF specific packages, all available from https://software.ecmwf.int/ |
---|
44 | ECMWF WebMARS, gribAPI with python enabled, emoslib and |
---|
45 | ecaccess web toolkit |
---|
46 | |
---|
47 | @Description: |
---|
48 | Further documentation may be obtained from www.flexpart.eu. |
---|
49 | |
---|
50 | Functionality provided: |
---|
51 | Prepare input 3D-wind fields in hybrid coordinates + |
---|
52 | surface fields for FLEXPART runs |
---|
53 | """ |
---|
54 | |
---|
55 | import calendar |
---|
56 | import shutil |
---|
57 | import datetime |
---|
58 | import time |
---|
59 | import os,inspect,sys |
---|
60 | import socket |
---|
61 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
62 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
63 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
64 | if localpythonpath not in sys.path: |
---|
65 | sys.path.append(localpythonpath) |
---|
66 | from UIOTools import UIOFiles |
---|
67 | #from string import strip |
---|
68 | from GribTools import GribTools |
---|
69 | from FlexpartTools import ECFlexpart, Control, interpret_args_and_control, cleanup |
---|
70 | |
---|
71 | hostname=socket.gethostname() |
---|
72 | ecapi= 'ecmwf' not in hostname |
---|
73 | |
---|
74 | try: |
---|
75 | if ecapi: |
---|
76 | import ecmwfapi |
---|
77 | except ImportError: |
---|
78 | ecapi = False |
---|
79 | |
---|
80 | |
---|
81 | def prepareFLEXPART(args, c): |
---|
82 | ''' |
---|
83 | @Description: |
---|
84 | |
---|
85 | |
---|
86 | @Input: |
---|
87 | args: instance of ArgumentParser |
---|
88 | Contains the commandline arguments from script/program call. |
---|
89 | |
---|
90 | c: instance of class Control |
---|
91 | Contains all the parameters of control files, which are e.g.: |
---|
92 | DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, |
---|
93 | STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, |
---|
94 | LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY, |
---|
95 | OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT, |
---|
96 | ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR, |
---|
97 | MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME |
---|
98 | DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
99 | |
---|
100 | For more information about format and content of the parameter |
---|
101 | see documentation. |
---|
102 | |
---|
103 | @Return: |
---|
104 | <nothing> |
---|
105 | ''' |
---|
106 | |
---|
107 | if not args.ppid: |
---|
108 | c.ppid = str(os.getppid()) |
---|
109 | else: |
---|
110 | c.ppid = args.ppid |
---|
111 | |
---|
112 | c.ecapi = ecapi |
---|
113 | |
---|
114 | # create the start and end date |
---|
115 | start = datetime.date(year=int(c.start_date[:4]), |
---|
116 | month=int(c.start_date[4:6]), |
---|
117 | day=int(c.start_date[6:])) |
---|
118 | |
---|
119 | end = datetime.date(year=int(c.end_date[:4]), |
---|
120 | month=int(c.end_date[4:6]), |
---|
121 | day=int(c.end_date[6:])) |
---|
122 | |
---|
123 | # to deaccumulated the fluxes correctly |
---|
124 | # one day ahead of the start date and |
---|
125 | # one day after the end date is needed |
---|
126 | startm1 = start - datetime.timedelta(days=1) |
---|
127 | endp1 = end + datetime.timedelta(days=1) |
---|
128 | |
---|
129 | # get all files with flux data to be deaccumulated |
---|
130 | inputfiles = UIOFiles(['.grib', '.grb', '.grib1', |
---|
131 | '.grib2', '.grb1', '.grb2']) |
---|
132 | |
---|
133 | inputfiles.listFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*') |
---|
134 | |
---|
135 | # create output dir if necessary |
---|
136 | if not os.path.exists(c.outputdir): |
---|
137 | os.makedirs(c.outputdir) |
---|
138 | |
---|
139 | # deaccumulate the flux data |
---|
140 | flexpart = ECFlexpart(c, fluxes=True) |
---|
141 | flexpart.write_namelist(c, 'fort.4') |
---|
142 | flexpart.deacc_fluxes(inputfiles, c) |
---|
143 | |
---|
144 | print('Prepare ' + start.strftime("%Y%m%d") + |
---|
145 | "/to/" + end.strftime("%Y%m%d")) |
---|
146 | |
---|
147 | # get a list of all files from the root inputdir |
---|
148 | inputfiles = UIOFiles(['.grib', '.grb', '.grib1', |
---|
149 | '.grib2', '.grb1', '.grb2']) |
---|
150 | |
---|
151 | inputfiles.listFiles(c.inputdir, '????__??.*' + c.ppid + '.*') |
---|
152 | |
---|
153 | # produce FLEXPART-ready GRIB files and |
---|
154 | # process GRIB files - |
---|
155 | # copy/transfer/interpolate them or make them GRIB2 |
---|
156 | if c.basetime == '00': |
---|
157 | start = startm1 |
---|
158 | |
---|
159 | flexpart = ECFlexpart(c, fluxes=False) |
---|
160 | flexpart.create(inputfiles, c) |
---|
161 | flexpart.process_output(c) |
---|
162 | |
---|
163 | # check if in debugging mode, then store all files |
---|
164 | if int(c.debug) != 0: |
---|
165 | print('Temporary files left intact') |
---|
166 | else: |
---|
167 | cleanup(c) |
---|
168 | |
---|
169 | return |
---|
170 | |
---|
171 | if __name__ == "__main__": |
---|
172 | args, c = interpret_args_and_control() |
---|
173 | prepareFLEXPART(args, c) |
---|
174 | cleanup(c) |
---|
175 | |
---|