source: flex_extract.git/source/python/mods/get_mars_data.py @ ca867de

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

refactored functions in EcFlexpart? and did some minor changes

  • Property mode set to 100755
File size: 8.3 KB
RevLine 
[d69b677]1#!/usr/bin/env python
[efdb01a]2# -*- coding: utf-8 -*-
[991df6a]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):
[ff99eae]11#        - moved the getEIdata program into a function "get_mars_data"
[991df6a]12#        - moved the AgurmentParser into a seperate function
13#        - adatpted the function for the use in flex_extract
[ff99eae]14#        - renamed file to get_mars_data
[991df6a]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
[54a8a01]24#
[991df6a]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
[ff99eae]40#    - get_mars_data
[54a8a01]41#    - do_retrievement
[991df6a]42#
43#*******************************************************************************
44
[efdb01a]45# ------------------------------------------------------------------------------
46# MODULES
47# ------------------------------------------------------------------------------
[991df6a]48import os
49import sys
50import inspect
[ca867de]51from datetime import datetime, timedelta
[ff99eae]52
53# software specific classes and modules from flex_extract
[ca867de]54sys.path.append('../')
[2fb99de]55import _config
[54a8a01]56from tools import my_error, normal_exit, get_cmdline_arguments, read_ecenv
[25b14be]57from classes.EcFlexpart import EcFlexpart
58from classes.UioFiles import UioFiles
[ca867de]59
60try:
61    ecapi = True
62    import ecmwfapi
63except ImportError:
64    ecapi = False
[efdb01a]65# ------------------------------------------------------------------------------
66# FUNCTION
67# ------------------------------------------------------------------------------
[991df6a]68def main():
69    '''
70    @Description:
[295ff45]71        If get_mars_data is called directly from command line,
72
[991df6a]73        the program flow and calls the argumentparser function and
[ff99eae]74        the get_mars_data function for retrieving EC data.
[991df6a]75
76    @Input:
77        <nothing>
78
79    @Return:
80        <nothing>
81    '''
[54a8a01]82
83    args = get_cmdline_arguments()
[4971f63]84    c = ControlFile(args.controlfile)
[54a8a01]85
[2fb99de]86    env_parameter = read_ecenv(_config.PATH_ECMWF_ENV)
[295ff45]87    c.assign_args_to_control(args)
[54a8a01]88    c.assign_envs_to_control(env_parameter)
[2fb99de]89    c.check_conditions(args.queue)
[54a8a01]90
[ff99eae]91    get_mars_data(c)
[54a8a01]92    normal_exit(c.mailfail, 'Done!')
[d69b677]93
[991df6a]94    return
95
[ff99eae]96def get_mars_data(c):
[991df6a]97    '''
98    @Description:
99        Retrieves the EC data needed for a FLEXPART simulation.
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    @Input:
105        c: instance of class ControlFile
[27fe969]106            Contains all the parameters of CONTROL file and
107            command line.
[991df6a]108            For more information about format and content of the parameter
109            see documentation.
110
111    @Return:
112        <nothing>
113    '''
[d69b677]114
115    if not os.path.exists(c.inputdir):
116        os.makedirs(c.inputdir)
[991df6a]117
[2fb99de]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))
[d69b677]125
126    if ecapi:
127        server = ecmwfapi.ECMWFService("mars")
128    else:
129        server = False
130
[64cf353]131    c.ecapi = ecapi
[2fb99de]132    print('Using ECMWF WebAPI: ' + str(c.ecapi))
[991df6a]133
[54a8a01]134    # basetime geht rückwärts
135
136    # if basetime 00
137    # dann wird von 12 am vortag bis 00 am start tag geholt
138    # aber ohne 12 selbst sondern 12 + step
139
140    # if basetime 12
141    # dann wird von 00 + step bis 12 am start tag geholt
142
143    # purer forecast wird vorwärts bestimmt.
144    # purer forecast mode ist dann wenn  größer 24 stunden
145    # wie kann das noch festgestellt werden ????
146    # nur FC und steps mehr als 24 ?
147    # die einzige problematik beim reinen forecast ist die benennung der files!
148    # also sobald es Tagesüberschneidungen gibt
149    # allerdings ist das relevant und ersichtlich an den NICHT FLUSS DATEN
150
[ca867de]151    start = datetime.strptime(c.start_date, '%Y%m%d')
152    end = datetime.strptime(c.end_date, '%Y%m%d')
153    # time period for one single retrieval
154    datechunk = timedelta(days=int(c.date_chunk))
[efdb01a]155
[54a8a01]156    if c.basetime == '00':
[ca867de]157        start = start - timedelta(days=1)
158
159    if c.maxstep <= 24:
160        startm1 = start - timedelta(days=1)
[54a8a01]161
162    if c.basetime == '00' or c.basetime == '12':
[ca867de]163        # endp1 = end + timedelta(days=1)
[54a8a01]164        endp1 = end
165    else:
[ca867de]166        # endp1 = end + timedelta(days=2)
167        endp1 = end + timedelta(days=1)
[54a8a01]168
[991df6a]169    # --------------  flux data ------------------------------------------------
[2fb99de]170    if c.request == 0 or c.request == 2:
171        print('... removing old flux content of ' + c.inputdir)
172        tobecleaned = UioFiles(c.inputdir,
173                               '*_acc_*.' + str(os.getppid()) + '.*.grb')
174        tobecleaned.delete_files()
[64cf353]175
[54a8a01]176    # if forecast for maximum one day (upto 24h) are to be retrieved,
[efdb01a]177    # collect accumulation data (flux data)
178    # with additional days in the beginning and at the end
179    # (used for complete disaggregation of original period)
[54a8a01]180    if c.maxstep <= 24:
181        do_retrievement(c, server, startm1, endp1, datechunk, fluxes=True)
[efdb01a]182
183    # if forecast data longer than 24h are to be retrieved,
184    # collect accumulation data (flux data)
185    # with the exact start and end date
186    # (disaggregation will be done for the
187    # exact time period with boundary conditions)
[d69b677]188    else:
[54a8a01]189        do_retrievement(c, server, start, end, datechunk, fluxes=True)
[991df6a]190
191    # --------------  non flux data --------------------------------------------
[2fb99de]192    if c.request == 0 or c.request == 2:
193        print('... removing old non flux content of ' + c.inputdir)
194        tobecleaned = UioFiles(c.inputdir,
195                               '*__*.' + str(os.getppid()) + '.*.grb')
196        tobecleaned.delete_files()
[991df6a]197
[54a8a01]198    do_retrievement(c, server, start, end, datechunk, fluxes=False)
199
200    return
201
202def do_retrievement(c, server, start, end, delta_t, fluxes=False):
203    '''
204    @Description:
205        Divides the complete retrieval period in smaller chunks and
206        retrieves the data from MARS.
207
208    @Input:
[27fe969]209        c: instance of class ControlFile
210            Contains all the parameters of CONTROL file and
211            command line.
[54a8a01]212            For more information about format and content of the parameter
213            see documentation.
214
215        server: instance of ECMWFService
216            The server connection to ECMWF
217
218        start: instance of datetime
219            The start date of the retrieval.
220
221        end: instance of datetime
222            The end date of the retrieval.
223
224        delta_t: instance of datetime
225            Delta_t +1 is the maximal time period of a single
226            retrieval.
227
228        fluxes: 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        <nothing>
235    '''
236
237    # since actual day also counts as one day,
238    # we only need to add datechunk - 1 days to retrieval
239    # for a period
[ca867de]240    delta_t_m1 = delta_t - timedelta(days=1)
[54a8a01]241
[efdb01a]242    day = start
243    while day <= end:
[54a8a01]244        flexpart = EcFlexpart(c, fluxes)
245        tmpday = day + delta_t_m1
[ff99eae]246        if tmpday < end:
247            dates = day.strftime("%Y%m%d") + "/to/" + \
248                    tmpday.strftime("%Y%m%d")
249        else:
250            dates = day.strftime("%Y%m%d") + "/to/" + \
251                    end.strftime("%Y%m%d")
[efdb01a]252
[2fb99de]253        print("... retrieve " + dates + " in dir " + c.inputdir)
[64cf353]254
[ff99eae]255        try:
[2fb99de]256            flexpart.retrieve(server, dates, c.request, c.inputdir)
[ff99eae]257        except IOError:
[54a8a01]258            my_error(c.mailfail, 'MARS request failed')
[efdb01a]259
[54a8a01]260        day += delta_t
[64cf353]261
[efdb01a]262    return
[d69b677]263
264if __name__ == "__main__":
[991df6a]265    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG