1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #******************************************************************************* |
---|
4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
5 | # |
---|
6 | # @Date: October 2014 |
---|
7 | # |
---|
8 | # @Change History: |
---|
9 | # |
---|
10 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
11 | # - moved the getEIdata program into a function "get_mars_data" |
---|
12 | # - moved the AgurmentParser into a seperate function |
---|
13 | # - adatpted the function for the use in flex_extract |
---|
14 | # - renamed file to get_mars_data |
---|
15 | # |
---|
16 | # February 2018 - Anne Philipp (University of Vienna): |
---|
17 | # - applied PEP8 style guide |
---|
18 | # - added structured documentation |
---|
19 | # - minor changes in programming style for consistence |
---|
20 | # - added function main and moved function calls vom __main__ there |
---|
21 | # (necessary for better documentation with docstrings for later |
---|
22 | # online documentation) |
---|
23 | # - use of UIFiles class for file selection and deletion |
---|
24 | # |
---|
25 | # |
---|
26 | # @License: |
---|
27 | # (C) Copyright 2014-2018. |
---|
28 | # |
---|
29 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
30 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
31 | # |
---|
32 | # @Program Functionality: |
---|
33 | # This program can be used as a module in the whole flex_extract process |
---|
34 | # or can be run by itself to just extract MARS data from ECMWF. To do so, |
---|
35 | # a couple of necessary parameters has to be passed with the program call. |
---|
36 | # See documentation for more details. |
---|
37 | # |
---|
38 | # @Program Content: |
---|
39 | # - main |
---|
40 | # - get_mars_data |
---|
41 | # - do_retrievement |
---|
42 | # |
---|
43 | #******************************************************************************* |
---|
44 | |
---|
45 | # ------------------------------------------------------------------------------ |
---|
46 | # MODULES |
---|
47 | # ------------------------------------------------------------------------------ |
---|
48 | import os |
---|
49 | import sys |
---|
50 | import inspect |
---|
51 | from datetime import datetime, timedelta |
---|
52 | |
---|
53 | # software specific classes and modules from flex_extract |
---|
54 | sys.path.append(os.path.dirname(os.path.abspath( |
---|
55 | inspect.getfile(inspect.currentframe()))) + '/../') |
---|
56 | import _config |
---|
57 | from tools import (my_error, normal_exit, get_cmdline_arguments, |
---|
58 | read_ecenv, make_dir) |
---|
59 | from classes.EcFlexpart import EcFlexpart |
---|
60 | from classes.UioFiles import UioFiles |
---|
61 | |
---|
62 | try: |
---|
63 | ecapi = True |
---|
64 | import ecmwfapi |
---|
65 | except ImportError: |
---|
66 | ecapi = False |
---|
67 | # ------------------------------------------------------------------------------ |
---|
68 | # FUNCTION |
---|
69 | # ------------------------------------------------------------------------------ |
---|
70 | def main(): |
---|
71 | '''Controls the program to get data out of mars. |
---|
72 | |
---|
73 | This is done if it is called directly from command line. |
---|
74 | Then it also takes program call arguments and control file input. |
---|
75 | |
---|
76 | Parameters |
---|
77 | ---------- |
---|
78 | |
---|
79 | Return |
---|
80 | ------ |
---|
81 | |
---|
82 | ''' |
---|
83 | |
---|
84 | args = get_cmdline_arguments() |
---|
85 | c = ControlFile(args.controlfile) |
---|
86 | |
---|
87 | env_parameter = read_ecenv(_config.PATH_ECMWF_ENV) |
---|
88 | c.assign_args_to_control(args) |
---|
89 | c.assign_envs_to_control(env_parameter) |
---|
90 | c.check_conditions(args.queue) |
---|
91 | |
---|
92 | get_mars_data(c) |
---|
93 | normal_exit(c.mailfail, 'Done!') |
---|
94 | |
---|
95 | return |
---|
96 | |
---|
97 | def get_mars_data(c): |
---|
98 | '''Retrieves the EC data needed for a FLEXPART simulation. |
---|
99 | |
---|
100 | Start and end dates for retrieval period is set. Retrievals |
---|
101 | are divided into smaller periods if necessary and datechunk parameter |
---|
102 | is set. |
---|
103 | |
---|
104 | Parameters |
---|
105 | ---------- |
---|
106 | c : :obj:`ControlFile` |
---|
107 | Contains all the parameters of CONTROL file and |
---|
108 | command line. |
---|
109 | |
---|
110 | Return |
---|
111 | ------ |
---|
112 | |
---|
113 | ''' |
---|
114 | |
---|
115 | if not os.path.exists(c.inputdir): |
---|
116 | make_dir(c.inputdir) |
---|
117 | |
---|
118 | if c.request == 0 or c.request == 2: |
---|
119 | print("Retrieving EC data!") |
---|
120 | elif c.request == 1: |
---|
121 | print("Printing mars requests!") |
---|
122 | |
---|
123 | print("start date %s " % (c.start_date)) |
---|
124 | print("end date %s " % (c.end_date)) |
---|
125 | |
---|
126 | if ecapi: |
---|
127 | if c.public: |
---|
128 | server = ecmwfapi.ECMWFDataServer() |
---|
129 | else: |
---|
130 | server = ecmwfapi.ECMWFService("mars") |
---|
131 | else: |
---|
132 | server = False |
---|
133 | |
---|
134 | c.ecapi = ecapi |
---|
135 | print('Using ECMWF WebAPI: ' + str(c.ecapi)) |
---|
136 | |
---|
137 | # basetime geht rückwärts |
---|
138 | |
---|
139 | # if basetime 00 |
---|
140 | # dann wird von 12 am vortag bis 00 am start tag geholt |
---|
141 | # aber ohne 12 selbst sondern 12 + step |
---|
142 | |
---|
143 | # if basetime 12 |
---|
144 | # dann wird von 00 + step bis 12 am start tag geholt |
---|
145 | |
---|
146 | # purer forecast wird vorwärts bestimmt. |
---|
147 | # purer forecast mode ist dann wenn größer 24 stunden |
---|
148 | # wie kann das noch festgestellt werden ???? |
---|
149 | # nur FC und steps mehr als 24 ? |
---|
150 | # die einzige problematik beim reinen forecast ist die benennung der files! |
---|
151 | # also sobald es Tagesüberschneidungen gibt |
---|
152 | # allerdings ist das relevant und ersichtlich an den NICHT FLUSS DATEN |
---|
153 | |
---|
154 | start = datetime.strptime(c.start_date, '%Y%m%d') |
---|
155 | end = datetime.strptime(c.end_date, '%Y%m%d') |
---|
156 | # time period for one single retrieval |
---|
157 | datechunk = timedelta(days=int(c.date_chunk)) |
---|
158 | |
---|
159 | if c.basetime == '00': |
---|
160 | start = start - timedelta(days=1) |
---|
161 | |
---|
162 | if c.maxstep <= 24: |
---|
163 | startm1 = start - timedelta(days=1) |
---|
164 | |
---|
165 | if c.basetime == '00' or c.basetime == '12': |
---|
166 | # endp1 = end + timedelta(days=1) |
---|
167 | endp1 = end |
---|
168 | else: |
---|
169 | # endp1 = end + timedelta(days=2) |
---|
170 | endp1 = end + timedelta(days=1) |
---|
171 | |
---|
172 | # -------------- flux data ------------------------------------------------ |
---|
173 | if c.request == 0 or c.request == 2: |
---|
174 | print('... removing old flux content of ' + c.inputdir) |
---|
175 | tobecleaned = UioFiles(c.inputdir, |
---|
176 | '*_acc_*.' + str(os.getppid()) + '.*.grb') |
---|
177 | tobecleaned.delete_files() |
---|
178 | |
---|
179 | # if forecast for maximum one day (upto 24h) are to be retrieved, |
---|
180 | # collect accumulation data (flux data) |
---|
181 | # with additional days in the beginning and at the end |
---|
182 | # (used for complete disaggregation of original period) |
---|
183 | if c.maxstep <= 24: |
---|
184 | do_retrievement(c, server, startm1, endp1, datechunk, fluxes=True) |
---|
185 | |
---|
186 | # if forecast data longer than 24h are to be retrieved, |
---|
187 | # collect accumulation data (flux data) |
---|
188 | # with the exact start and end date |
---|
189 | # (disaggregation will be done for the |
---|
190 | # exact time period with boundary conditions) |
---|
191 | else: |
---|
192 | do_retrievement(c, server, start, end, datechunk, fluxes=True) |
---|
193 | |
---|
194 | # -------------- non flux data -------------------------------------------- |
---|
195 | if c.request == 0 or c.request == 2: |
---|
196 | print('... removing old non flux content of ' + c.inputdir) |
---|
197 | tobecleaned = UioFiles(c.inputdir, |
---|
198 | '*__*.' + str(os.getppid()) + '.*.grb') |
---|
199 | tobecleaned.delete_files() |
---|
200 | |
---|
201 | do_retrievement(c, server, start, end, datechunk, fluxes=False) |
---|
202 | |
---|
203 | return |
---|
204 | |
---|
205 | def do_retrievement(c, server, start, end, delta_t, fluxes=False): |
---|
206 | '''Divides the complete retrieval period in smaller chunks and |
---|
207 | retrieves the data from MARS. |
---|
208 | |
---|
209 | Parameters |
---|
210 | ---------- |
---|
211 | c : :obj:`ControlFile` |
---|
212 | Contains all the parameters of CONTROL file and |
---|
213 | command line. |
---|
214 | |
---|
215 | server : :obj:`ECMWFService` |
---|
216 | The server connection to ECMWF. |
---|
217 | |
---|
218 | start : :obj:`datetime` |
---|
219 | The start date of the retrieval. |
---|
220 | |
---|
221 | end : :obj:`datetime` |
---|
222 | The end date of the retrieval. |
---|
223 | |
---|
224 | delta_t : :obj:`datetime` |
---|
225 | Delta_t + 1 is the maximal time period of a single |
---|
226 | retrieval. |
---|
227 | |
---|
228 | fluxes : :obj:`boolean`, optional |
---|
229 | Decides if the flux parameters are to be retrieved or |
---|
230 | the rest of the parameter list. |
---|
231 | Default value is False. |
---|
232 | |
---|
233 | Return |
---|
234 | ------ |
---|
235 | |
---|
236 | ''' |
---|
237 | |
---|
238 | # since actual day also counts as one day, |
---|
239 | # we only need to add datechunk - 1 days to retrieval |
---|
240 | # for a period |
---|
241 | delta_t_m1 = delta_t - timedelta(days=1) |
---|
242 | |
---|
243 | day = start |
---|
244 | while day <= end: |
---|
245 | flexpart = EcFlexpart(c, fluxes) |
---|
246 | tmpday = day + delta_t_m1 |
---|
247 | if tmpday < end: |
---|
248 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
249 | tmpday.strftime("%Y%m%d") |
---|
250 | else: |
---|
251 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
252 | end.strftime("%Y%m%d") |
---|
253 | |
---|
254 | print("... retrieve " + dates + " in dir " + c.inputdir) |
---|
255 | |
---|
256 | try: |
---|
257 | flexpart.retrieve(server, dates, c.public, c.request, c.inputdir) |
---|
258 | except IOError: |
---|
259 | my_error(c.mailfail, 'MARS request failed') |
---|
260 | |
---|
261 | day += delta_t |
---|
262 | |
---|
263 | return |
---|
264 | |
---|
265 | if __name__ == "__main__": |
---|
266 | main() |
---|