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

ctbtodev
Last change on this file since 0f89116 was 0f89116, checked in by Anne Philipp <anne.philipp@…>, 4 years ago

diverse changes due to PEP8 style guide and eliminating grib2flexpart; removed unused parameter

  • 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 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#        - seperated 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 servers.
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_retrievement - 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 get data out of mars.
97
98    This is done if it is called directly from command line.
99    Then it also takes program call arguments and control file 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 EC data needed for a FLEXPART simulation.
117
118    Start and end dates for retrieval period is set. Retrievals
119    are divided into smaller periods if necessary and 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 EC data!")
140    else:
141        if c.request == 1:
142            print("Printing mars requests!")
143        elif c.request == 2:
144            print("Retrieving EC 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    Which API is used depends on availability and the dataset to be retrieved.
193    The CDS API is used for ERA5 dataset no matter if the user is a member or
194    a public user. ECMWF WebAPI is used for all other available datasets.
195
196    Parameters
197    ----------
198    c : ControlFile
199        Contains all the parameters of CONTROL file and
200        command line.
201
202    Return
203    ------
204    server : ECMWFDataServer, ECMWFService or Client
205        Connection to ECMWF server via python interface ECMWF WebAPI or CDS API.
206
207    '''
208    if cds_api and (c.marsclass.upper() == 'EA'):
209        server = cdsapi.Client()
210        c.ec_api = False
211    elif c.ec_api:
212        if c.public:
213            server = ecmwfapi.ECMWFDataServer()
214        else:
215            server = ecmwfapi.ECMWFService("mars")
216        c.cds_api = False
217    else:
218        server = False
219
220    print('Using ECMWF WebAPI: ' + str(c.ec_api))
221    print('Using CDS API: ' + str(c.cds_api))
222
223    return server
224
225
226def check_dates_for_nonflux_fc_times(types, times):
227    '''Checks if the time 18UTC corresponds to forecast field.
228
229    Parameters
230    ----------
231    types : list of str
232        List of field types.
233
234    times : list of str or str
235        The time in hours of the field.
236
237    Return
238    ------
239    True or False
240
241    '''
242    for ty, ti in zip(types, times):
243        if ty.upper() == 'FC' and int(ti) == 18:
244            return True
245    return False
246
247
248def mk_dates(c, fluxes):
249    '''Prepares start and end date depending on flux or non flux data.
250
251    If forecast for maximum one day (upto 24h) are to be retrieved, then
252    collect accumulation data (flux data) with additional days in the
253    beginning and at the end (used for complete disaggregation of
254    original period)
255
256    If forecast data longer than 24h are to be retrieved, then
257    collect accumulation data (flux data) with the exact start and end date
258    (disaggregation will be done for the exact time period with
259    boundary conditions)
260
261    Since for basetime the extraction contains the 12 hours upfront,
262    if basetime is 0, the starting date has to be the day before
263
264    Parameters
265    ----------
266    c : ControlFile
267        Contains all the parameters of CONTROL file and
268        command line.
269
270    fluxes : boolean, optional
271        Decides if the flux parameter settings are stored or
272        the rest of the parameter list.
273        Default value is False.
274
275    Return
276    ------
277    start : datetime
278        The start date of the retrieving data set.
279
280    end : datetime
281        The end date of the retrieving data set.
282
283    chunk : datetime
284        Time period in days for one single mars retrieval.
285
286    '''
287    start = datetime.strptime(c.start_date, '%Y%m%d')
288    end = datetime.strptime(c.end_date, '%Y%m%d')
289    chunk = timedelta(days=int(c.date_chunk))
290
291    if c.basetime == 0:
292        start = start - timedelta(days=1)
293
294    if c.purefc and fluxes and c.maxstep < 24:
295        start = start - timedelta(days=1)
296        end = end + timedelta(days=1)
297
298    if not c.purefc and fluxes and not c.basetime == 0:
299        start = start - timedelta(days=1)
300        end = end + timedelta(days=1)
301
302    # if we have non-flux forecast data starting at 18 UTC
303    # we need to start retrieving data one day in advance
304    if not fluxes and check_dates_for_nonflux_fc_times(c.type, c.time):
305        start = start - timedelta(days=1)
306
307    return start, end, chunk
308
309def remove_old(pattern, inputdir):
310    '''Deletes old retrieval files from current input directory
311    matching the pattern.
312
313    Parameters
314    ----------
315    pattern : str
316        The sub string pattern which identifies the files to be deleted.
317
318    inputdir : str, optional
319        Path to the directory where the retrieved data is stored.
320
321    Return
322    ------
323
324    '''
325    print('... removing old content of ' + inputdir)
326
327    tobecleaned = UioFiles(inputdir, pattern)
328    tobecleaned.delete_files()
329
330    return
331
332
333def do_retrievement(c, server, start, end, delta_t, fluxes=False):
334    '''Divides the complete retrieval period in smaller chunks and
335    retrieves the data from MARS.
336
337    Parameters
338    ----------
339    c : ControlFile
340        Contains all the parameters of CONTROL file and
341        command line.
342
343    server : ECMWFService or ECMWFDataServer
344            The server connection to ECMWF.
345
346    start : datetime
347        The start date of the retrieval.
348
349    end : datetime
350        The end date of the retrieval.
351
352    delta_t : datetime
353        Delta_t + 1 is the maximal time period of a single
354        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
368    # for a period
369    delta_t_m1 = delta_t - timedelta(days=1)
370
371    day = start
372    while day <= end:
373        flexpart = EcFlexpart(c, fluxes)
374        tmpday = day + delta_t_m1
375        if tmpday < end:
376            dates = day.strftime("%Y%m%d") + "/to/" + \
377                    tmpday.strftime("%Y%m%d")
378        else:
379            dates = day.strftime("%Y%m%d") + "/to/" + \
380                    end.strftime("%Y%m%d")
381
382        print("... retrieve " + dates + " in dir " + c.inputdir)
383
384        try:
385            flexpart.retrieve(server, dates, c.public, c.request, c.inputdir)
386        except IOError:
387            my_error('MARS request failed')
388
389        day += delta_t
390
391    return
392
393if __name__ == "__main__":
394    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG