source: flex_extract.git/Source/Python/Mods/get_mars_data.py @ 8209738

ctbtodev
Last change on this file since 8209738 was 8209738, checked in by anphi <anne.philipp@…>, 4 years ago

language corrections in comment sections and print commands

  • Property mode set to 100755
File size: 11.4 KB
Line 
1#!/usr/bin/env python3
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 separate function
13#        - adapted the function for use in flex_extract
14#        - renamed source 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#        - separated get_mars_data function into several smaller pieces:
25#          write_reqheader, mk_server, mk_dates, remove_old, do_retrievment
26#
27# @License:
28#    (C) Copyright 2014-2019.
29#    Anne Philipp, Leopold Haimberger
30#
31#    SPDX-License-Identifier: CC-BY-4.0
32#
33#    This work is licensed under the Creative Commons Attribution 4.0
34#    International License. To view a copy of this license, visit
35#    http://creativecommons.org/licenses/by/4.0/ or send a letter to
36#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
37#*******************************************************************************
38'''This script extracts MARS data from ECMWF.
39
40At first, the necessary parameters from command line and CONTROL files are
41extracted. They define the data set to be extracted from MARS.
42
43This file can also be imported as a module and contains the following
44functions:
45
46    * main            - the main function of the script
47    * get_mars_data   - overall control of ECMWF data retrievment
48    * write_reqheader - writes the header into the mars_request file
49    * mk_server       - creates the server connection to ECMWF servers
50    * mk_dates        - defines the start and end date
51    * remove_old      - deletes old retrieved grib files
52    * do_retrieval    - creates individual retrievals
53
54Type get_mars_data.py --help
55to get information about command line parameters.
56Read the documentation for usage instructions.
57'''
58# ------------------------------------------------------------------------------
59# MODULES
60# ------------------------------------------------------------------------------
61from __future__ import print_function
62
63import os
64import sys
65import inspect
66from datetime import datetime, timedelta
67
68# software-specific classes and modules from flex_extract
69# add path to local main Python path for flex_extract to get full access
70sys.path.append(os.path.dirname(os.path.abspath(
71    inspect.getfile(inspect.currentframe()))) + '/../')
72# pylint: disable=wrong-import-position
73import _config
74from Mods.tools import (setup_controldata, my_error, normal_exit, make_dir)
75from Classes.EcFlexpart import EcFlexpart
76from Classes.UioFiles import UioFiles
77from Classes.MarsRetrieval import MarsRetrieval
78# pylint: enable=wrong-import-position
79# pylint: disable=invalid-name
80try:
81    ec_api = True
82    import ecmwfapi
83except ImportError:
84    ec_api = False
85
86try:
87    cds_api = True
88    import cdsapi
89except ImportError:
90    cds_api = False
91# pylint: enable=invalid-name
92# ------------------------------------------------------------------------------
93# FUNCTION
94# ------------------------------------------------------------------------------
95def main():
96    '''Controls the program to retrieve data from MARS.
97
98    This is done if called directly from command line.
99    Then, arguments and control file are taken as input.
100
101    Parameters
102    ----------
103
104    Return
105    ------
106
107    '''
108
109    c, _, _, _ = setup_controldata()
110    get_mars_data(c)
111    normal_exit('Retrieving MARS data: Done!')
112
113    return
114
115def get_mars_data(c):
116    '''Retrieves the ECMWF data required for a FLEXPART simulation.
117
118    Start and end dates for retrieval period are set. Retrievals
119    are divided into shorter periods if necessary and if datechunk parameter
120    is set.
121
122    Parameters
123    ----------
124    c : ControlFile
125        Contains all the parameters of CONTROL file and
126        command line.
127
128    Return
129    ------
130
131    '''
132    c.ec_api = ec_api
133    c.cds_api = cds_api
134
135    if not os.path.exists(c.inputdir):
136        make_dir(c.inputdir)
137
138    if c.request == 0:
139        print("Retrieving ECMWF data!")
140    else:
141        if c.request == 1:
142            print("Printing MARS requests!")
143        elif c.request == 2:
144            print("Retrieving ECMWF data and printing MARS request!")
145        write_reqheader(os.path.join(c.inputdir, _config.FILE_MARS_REQUESTS))
146
147    print("start date %s " % (c.start_date))
148    print("end date %s " % (c.end_date))
149
150    server = mk_server(c)
151
152    # if data are to be retrieved, clean up any old grib files
153    if c.request == 0 or c.request == 2:
154        remove_old('*grb', c.inputdir)
155
156    # --------------  flux data ------------------------------------------------
157    start, end, datechunk = mk_dates(c, fluxes=True)
158    do_retrievement(c, server, start, end, datechunk, fluxes=True)
159
160    # --------------  non flux data --------------------------------------------
161    start, end, datechunk = mk_dates(c, fluxes=False)
162    do_retrievement(c, server, start, end, datechunk, fluxes=False)
163
164    return
165
166def write_reqheader(marsfile):
167    '''Writes header with column names into MARS request file.
168
169    Parameters
170    ----------
171    marsfile : str
172        Path to the MARS request file.
173
174    Return
175    ------
176
177    '''
178    MR = MarsRetrieval(None, None)
179    attrs = vars(MR).copy()
180    del attrs['server']
181    del attrs['public']
182    with open(marsfile, 'w') as f:
183        f.write('request_number' + ', ')
184        f.write(', '.join(str(key) for key in sorted(attrs.keys())))
185        f.write('\n')
186
187    return
188
189def mk_server(c):
190    '''Creates a server connection with available Python API.
191
192    The API selected depends on availability and the data set to be retrieved.
193    The CDS API is used for ERA5 data, no matter whether the user is a
194    member-state or a public user.
195    ECMWF WebAPI is used for all other available datasets.
196
197    Parameters
198    ----------
199    c : ControlFile
200        Contains all the parameters of CONTROL file and
201        command line.
202
203    Return
204    ------
205    server : ECMWFDataServer, ECMWFService or Client
206        Connection to ECMWF server via python interface ECMWF WebAPI or CDS API.
207
208    '''
209    if cds_api and (c.marsclass.upper() == 'EA'):
210        server = cdsapi.Client()
211        c.ec_api = False
212    elif c.ec_api:
213        if c.public:
214            server = ecmwfapi.ECMWFDataServer()
215        else:
216            server = ecmwfapi.ECMWFService("mars")
217        c.cds_api = False
218    else:
219        server = False
220
221    print('Using ECMWF WebAPI: ' + str(c.ec_api))
222    print('Using CDS API: ' + str(c.cds_api))
223
224    return server
225
226
227def check_dates_for_nonflux_fc_times(types, times):
228    '''Checks if the time 18UTC corresponds to forecast field.
229
230    Parameters
231    ----------
232    types : list of str
233        List of field types.
234
235    times : list of str or str
236        The time in hours of the field.
237
238    Return
239    ------
240    True or False
241
242    '''
243    for ty, ti in zip(types, times):
244        if ty.upper() == 'FC' and int(ti) == 18:
245            return True
246    return False
247
248
249def mk_dates(c, fluxes):
250    '''Prepares start and end date depending on flux or non-flux type of data.
251
252    If forecasts for a maximum of one day (24 h) are to be retrieved, then
253    collect accumulation data (flux data) with additional days in the
254    beginning and at the end (needed for complete disaggregation of
255    original period)
256
257    If forecast data for more than +24 h are to be retrieved, then
258    collect accumulation data (flux data) with the exact start and end date
259    (disaggregation will be done for the exact time period with
260    boundary conditions)
261
262    Since for basetime the extraction contains the 12 hours upfront,
263    if basetime is 0, the starting date has to be the day before
264
265    Parameters
266    ----------
267    c : ControlFile
268        Contains all the parameters of CONTROL file and
269        command line.
270
271    fluxes : boolean, optional
272        Decides if the flux parameter settings are stored or
273        the rest of the parameter list.
274        Default value is False.
275
276    Return
277    ------
278    start : datetime
279        The start date of the retrieving data set.
280
281    end : datetime
282        The end date of the retrieving data set.
283
284    chunk : datetime
285        Time period in days for one single mars retrieval.
286
287    '''
288    start = datetime.strptime(c.start_date, '%Y%m%d')
289    end = datetime.strptime(c.end_date, '%Y%m%d')
290    chunk = timedelta(days=int(c.date_chunk))
291
292    if c.basetime == 0:
293        start = start - timedelta(days=1)
294
295    if c.purefc and fluxes and c.maxstep < 24:
296        start = start - timedelta(days=1)
297        end = end + timedelta(days=1)
298
299    if not c.purefc and fluxes and not c.basetime == 0:
300        start = start - timedelta(days=1)
301        end = end + timedelta(days=1)
302
303    # if we have non-flux forecast data starting at 18 UTC
304    # we need to start retrieving data one day in advance
305    if not fluxes and check_dates_for_nonflux_fc_times(c.type, c.time):
306        start = start - timedelta(days=1)
307
308    return start, end, chunk
309
310def remove_old(pattern, inputdir):
311    '''Deletes old retrieval files from current input directory
312    matching the pattern.
313
314    Parameters
315    ----------
316    pattern : str
317        The substring pattern which identifies the files to be deleted.
318
319    inputdir : str, optional
320        Path to the directory where the retrieved data are stored.
321
322    Return
323    ------
324
325    '''
326    print('... removing old files in ' + inputdir)
327
328    tobecleaned = UioFiles(inputdir, pattern)
329    tobecleaned.delete_files()
330
331    return
332
333
334def do_retrievement(c, server, start, end, delta_t, fluxes=False):
335    '''Divides the total retrieval period into smaller chunks and
336    retrieves the data from MARS.
337
338    Parameters
339    ----------
340    c : ControlFile
341        Contains all the parameters of CONTROL file and
342        command line.
343
344    server : ECMWFService or ECMWFDataServer
345            The server connection to ECMWF.
346
347    start : datetime
348        The start date of the retrieval.
349
350    end : datetime
351        The end date of the retrieval.
352
353    delta_t : datetime
354        Delta_t + 1 is the maximum time period of a single retrieval.
355
356    fluxes : boolean, optional
357        Decides if the flux parameters are to be retrieved or
358        the rest of the parameter list.
359        Default value is False.
360
361    Return
362    ------
363
364    '''
365
366    # since actual day also counts as one day,
367    # we only need to add datechunk - 1 days to retrieval for a period
368    delta_t_m1 = delta_t - timedelta(days=1)
369
370    day = start
371    while day <= end:
372        flexpart = EcFlexpart(c, fluxes)
373        tmpday = day + delta_t_m1
374        if tmpday < end:
375            dates = day.strftime("%Y%m%d") + "/to/" + \
376                    tmpday.strftime("%Y%m%d")
377        else:
378            dates = day.strftime("%Y%m%d") + "/to/" + \
379                    end.strftime("%Y%m%d")
380
381        print("... retrieve " + dates + " in dir " + c.inputdir)
382
383        try:
384            flexpart.retrieve(server, dates, c.public, c.request, c.inputdir)
385        except IOError:
386            my_error('MARS request failed')
387
388        day += delta_t
389
390    return
391
392if __name__ == "__main__":
393    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG