source: flex_extract.git/source/python/mods/prepare_flexpart.py @ 5bad6ec

dev
Last change on this file since 5bad6ec was 5bad6ec, checked in by Anne Philipp <anne.philipp@…>, 12 months ago

added possibility to extract public datasets via an logical public parameter

  • Property mode set to 100755
File size: 6.2 KB
Line 
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#        - using the WebAPI also for general MARS retrievals
12#        - job submission on ecgate and cca
13#        - job templates suitable for twice daily operational dissemination
14#        - dividing retrievals of longer periods into digestable chunks
15#        - retrieve also longer term forecasts, not only analyses and
16#          short term forecast data
17#        - conversion into GRIB2
18#        - conversion into .fp format for faster execution of FLEXPART
19#
20#    February 2018 - Anne Philipp (University of Vienna):
21#        - applied PEP8 style guide
22#        - added documentation
23#        - minor changes in programming style for consistence
24#        - BUG: removed call of clean_up-Function after call of
25#               prepareFlexpart in main since it is already called in
26#               prepareFlexpart at the end!
27#        - created function main and moved the two function calls for
28#          arguments and prepare_flexpart into it
29#
30# @License:
31#    (C) Copyright 2014-2018.
32#
33#    This software is licensed under the terms of the Apache Licence Version 2.0
34#    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
35#
36# @Program Functionality:
37#    This program prepares the final version of the grib files which are
38#    then used by FLEXPART. It converts the bunch of grib files extracted
39#    via get_mars_data by doing for example the necessary conversion to get
40#    consistent grids or the disaggregation of flux data. Finally, the
41#    program combines the data fields in files per available hour with the
42#    naming convention xxYYMMDDHH, where xx should be 2 arbitrary letters
43#    (mostly xx is chosen to be "EN").
44#
45# @Program Content:
46#    - main
47#    - prepare_flexpart
48#
49#*******************************************************************************
50
51# ------------------------------------------------------------------------------
52# MODULES
53# ------------------------------------------------------------------------------
54import datetime
55import os
56import inspect
57import sys
58import socket
59
60# software specific classes and modules from flex_extract
61sys.path.append('../')
62import _config
63from classes.UioFiles import UioFiles
64from classes.ControlFile import ControlFile
65from tools import clean_up, get_cmdline_arguments, read_ecenv, make_dir
66from classes.EcFlexpart import EcFlexpart
67
68ecapi = 'ecmwf' not in socket.gethostname()
69try:
70    if ecapi:
71        import ecmwfapi
72except ImportError:
73    ecapi = False
74
75# ------------------------------------------------------------------------------
76# FUNCTION
77# ------------------------------------------------------------------------------
78def main():
79    '''
80    @Description:
81        If prepare_flexpart is called from command line, this function controls
82        the program flow and calls the argumentparser function and
83        the prepare_flexpart function for preparation of GRIB data for FLEXPART.
84
85    @Input:
86        <nothing>
87
88    @Return:
89        <nothing>
90    '''
91
92    args = get_cmdline_arguments()
93    c = ControlFile(args.controlfile)
94
95    env_parameter = read_ecenv(_config.PATH_ECMWF_ENV)
96    c.assign_args_to_control(args)
97    c.assign_envs_to_control(env_parameter)
98    c.check_conditions(args.queue)
99
100    prepare_flexpart(args.ppid, c)
101
102    return
103
104def prepare_flexpart(ppid, c):
105    '''
106    @Description:
107        Lists all grib files retrieved from MARS with get_mars_data and
108        uses prepares data for the use in FLEXPART. Specific data fields
109        are converted to a different grid and the flux data are going to be
110        disaggregated. The data fields are collected by hour and stored in
111        a file with a specific FLEXPART relevant naming convention.
112
113    @Input:
114        ppid: int
115            Contains the ppid number of the current ECMWF job. If it is called
116            from this script, it is "None".
117
118        c: instance of class ControlFile
119            Contains all the parameters of CONTROL file and
120            command line.
121            For more information about format and content of the parameter
122            see documentation.
123
124    @Return:
125        <nothing>
126    '''
127
128    if not ppid:
129        c.ppid = str(os.getppid())
130    else:
131        c.ppid = ppid
132
133    c.ecapi = ecapi
134
135    # create the start and end date
136    start = datetime.date(year=int(c.start_date[:4]),
137                          month=int(c.start_date[4:6]),
138                          day=int(c.start_date[6:]))
139
140    end = datetime.date(year=int(c.end_date[:4]),
141                        month=int(c.end_date[4:6]),
142                        day=int(c.end_date[6:]))
143
144    # assign starting date minus 1 day
145    # since for basetime 00 we need the 12 hours upfront
146    # (the day before from 12 UTC to current day 00 UTC)
147    if c.basetime == '00':
148        start = start - datetime.timedelta(days=1)
149
150    print('Prepare ' + start.strftime("%Y%m%d") +
151           "/to/" + end.strftime("%Y%m%d"))
152
153    # create output dir if necessary
154    if not os.path.exists(c.outputdir):
155        make_dir(c.outputdir)
156
157    # get all files with flux data to be deaccumulated
158    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*')
159
160    # deaccumulate the flux data
161    flexpart = EcFlexpart(c, fluxes=True)
162    flexpart.write_namelist(c, _config.FILE_NAMELIST)
163    flexpart.deacc_fluxes(inputfiles, c)
164
165    # get a list of all files from the root inputdir
166    inputfiles = UioFiles(c.inputdir, '????__??.*' + c.ppid + '.*')
167
168    # produce FLEXPART-ready GRIB files and process them -
169    # copy/transfer/interpolate them or make them GRIB2
170    flexpart = EcFlexpart(c, fluxes=False)
171    flexpart.create(inputfiles, c)
172    flexpart.process_output(c)
173    if c.grib2flexpart:
174        # prepare environment for a FLEXPART run
175        # to convert grib to flexpart binary format
176        flexpart.prepare_fp_files(c)
177
178    # check if in debugging mode, then store all files
179    # otherwise delete temporary files
180    if c.debug:
181        print('\nTemporary files left intact')
182    else:
183        clean_up(c)
184
185    return
186
187if __name__ == "__main__":
188    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG