source: flex_extract.git/source/python/mods/prepare_flexpart.py @ 9aefaad

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

renamed function get_cmdline_args

  • Property mode set to 100755
File size: 5.9 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
61
62sys.path.append(os.path.dirname(os.path.abspath(
63    inspect.getfile(inspect.currentframe()))) + '/../')
64import _config
65from checks import check_ppid
66from classes.UioFiles import UioFiles
67from classes.ControlFile import ControlFile
68from tools import clean_up, get_cmdline_args, read_ecenv, make_dir
69from classes.EcFlexpart import EcFlexpart
70
71ecapi = 'ecmwf' not in socket.gethostname()
72try:
73    if ecapi:
74        import ecmwfapi
75except ImportError:
76    ecapi = False
77
78# ------------------------------------------------------------------------------
79# FUNCTION
80# ------------------------------------------------------------------------------
81def main():
82    '''Controls the program to prepare flexpart input files from mars data.
83
84    This is done if it is called directly from command line.
85    Then it also takes program call arguments and control file input.
86
87    Parameters
88    ----------
89
90    Return
91    ------
92
93    '''
94
95    args = get_cmdline_args()
96    c = ControlFile(args.controlfile)
97
98    env_parameter = read_ecenv(_config.PATH_ECMWF_ENV)
99    c.assign_args_to_control(args)
100    c.assign_envs_to_control(env_parameter)
101    c.check_conditions(args.queue)
102
103    prepare_flexpart(args.ppid, c)
104
105    return
106
107def prepare_flexpart(ppid, c):
108    '''Converts the mars data into flexpart ready input files.
109
110    Specific data fields are converted to a different grid and the flux
111    data are going to be disaggregated. The data fields are collected by
112    hour and stored in a file with a specific FLEXPART relevant naming
113    convention.
114
115    Parameters
116    ----------
117    ppid : :obj:`int`
118        Contains the ppid number of the current ECMWF job. It will be None if
119        the method was called within this module.
120
121    c : :obj:`ControlFile`
122        Contains all the parameters of CONTROL file and
123        command line.
124
125    Return
126    ------
127
128    '''
129    check_ppid(c, ppid)
130
131    c.ecapi = ecapi
132
133    # create the start and end date
134    start = datetime.date(year=int(c.start_date[:4]),
135                          month=int(c.start_date[4:6]),
136                          day=int(c.start_date[6:]))
137
138    end = datetime.date(year=int(c.end_date[:4]),
139                        month=int(c.end_date[4:6]),
140                        day=int(c.end_date[6:]))
141
142    # if basetime is 00
143    # assign starting date minus 1 day
144    # since we need the 12 hours upfront
145    # (the day before from 12 UTC to current day 00 UTC)
146    if c.basetime == '00':
147        start = start - datetime.timedelta(days=1)
148
149    print('Prepare ' + start.strftime("%Y%m%d") +
150           "/to/" + end.strftime("%Y%m%d"))
151
152    # create output dir if necessary
153    if not os.path.exists(c.outputdir):
154        make_dir(c.outputdir)
155
156    # get all files with flux data to be deaccumulated
157    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + str(c.ppid) + '.*')
158
159    # deaccumulate the flux data
160    flexpart = EcFlexpart(c, fluxes=True)
161    flexpart.write_namelist(c)
162    flexpart.deacc_fluxes(inputfiles, c)
163
164    # get a list of all other files
165    inputfiles = UioFiles(c.inputdir, '????__??.*' + str(c.ppid) + '.*')
166
167    # produce FLEXPART-ready GRIB files and process them -
168    # copy/transfer/interpolate them or make them GRIB2
169    flexpart = EcFlexpart(c, fluxes=False)
170    flexpart.create(inputfiles, c)
171    flexpart.process_output(c)
172
173    # make use of a possible conversion to a
174    # specific flexpart binary format
175    if c.grib2flexpart:
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