source: flex_extract.git/python/prepare_flexpart.py @ 2fb99de

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

introduced config with path definitions and changed py files accordingly; Installation works; some tests were added for tarball making; Problems in submission to ecgate

  • Property mode set to 100755
File size: 6.7 KB
RevLine 
[d69b677]1#!/usr/bin/env python
[02c8c50]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):
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
[ff99eae]24#        - BUG: removed call of clean_up-Function after call of
[991df6a]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
[ff99eae]28#          arguments and prepare_flexpart into it
[991df6a]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
[ff99eae]39#    via get_mars_data by doing for example the necessary conversion to get
[991df6a]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
[ff99eae]47#    - prepare_flexpart
[991df6a]48#
49#*******************************************************************************
50
[efdb01a]51# ------------------------------------------------------------------------------
52# MODULES
53# ------------------------------------------------------------------------------
[d69b677]54import datetime
[efdb01a]55import os
56import inspect
57import sys
[d69b677]58import socket
59
[ff99eae]60# software specific classes and modules from flex_extract
[2fb99de]61import _config
[ff99eae]62from UioFiles import UioFiles
[54a8a01]63from tools import clean_up, get_cmdline_arguments, read_ecenv
[ff99eae]64from EcFlexpart import EcFlexpart
65
66ecapi = 'ecmwf' not in socket.gethostname()
[d69b677]67try:
68    if ecapi:
69        import ecmwfapi
70except ImportError:
[64cf353]71    ecapi = False
72
[efdb01a]73# ------------------------------------------------------------------------------
74# FUNCTION
75# ------------------------------------------------------------------------------
[991df6a]76def main():
[02c8c50]77    '''
78    @Description:
[ff99eae]79        If prepare_flexpart is called from command line, this function controls
[991df6a]80        the program flow and calls the argumentparser function and
[ff99eae]81        the prepare_flexpart function for preparation of GRIB data for FLEXPART.
[991df6a]82
83    @Input:
84        <nothing>
[02c8c50]85
[991df6a]86    @Return:
87        <nothing>
88    '''
[54a8a01]89
90    args = get_cmdline_arguments()
91
92    try:
93        c = ControlFile(args.controlfile)
94    except IOError:
[2fb99de]95        print('Could not read CONTROL file "' + args.controlfile + '"')
96        print('Either it does not exist or its syntax is wrong.')
97        print('Try "' + sys.argv[0].split('/')[-1] + \
98              ' -h" to print usage information')
99        sys.exit(1)
100
101    env_parameter = read_ecenv(_config.PATH_ECMWF_ENV)
[54a8a01]102    c.assign_args_to_control(args, env_parameter)
103    c.assign_envs_to_control(env_parameter)
[2fb99de]104    c.check_conditions(args.queue)
[54a8a01]105    prepare_flexpart(args.ppid, c)
[991df6a]106
107    return
108
[54a8a01]109def prepare_flexpart(ppid, c):
[991df6a]110    '''
111    @Description:
[ff99eae]112        Lists all grib files retrieved from MARS with get_mars_data and
[991df6a]113        uses prepares data for the use in FLEXPART. Specific data fields
114        are converted to a different grid and the flux data are going to be
115        disaggregated. The data fields are collected by hour and stored in
116        a file with a specific FLEXPART relevant naming convention.
[d69b677]117
[02c8c50]118    @Input:
[54a8a01]119        ppid: int
120            Contains the ppid number of the current ECMWF job. If it is called
121            from this script, it is "None".
[d69b677]122
[991df6a]123        c: instance of class ControlFile
124            Contains all the parameters of CONTROL file, which are e.g.:
[02c8c50]125            DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME,
126            STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT,
127            LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY,
128            OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT,
129            ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR,
130            MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME
131            DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS
[d69b677]132
[02c8c50]133            For more information about format and content of the parameter
134            see documentation.
135
136    @Return:
137        <nothing>
138    '''
[64cf353]139
[54a8a01]140    if not ppid:
[02c8c50]141        c.ppid = str(os.getppid())
[d69b677]142    else:
[54a8a01]143        c.ppid = ppid
[02c8c50]144
145    c.ecapi = ecapi
[d69b677]146
[02c8c50]147    # create the start and end date
148    start = datetime.date(year=int(c.start_date[:4]),
149                          month=int(c.start_date[4:6]),
150                          day=int(c.start_date[6:]))
[d69b677]151
[02c8c50]152    end = datetime.date(year=int(c.end_date[:4]),
153                        month=int(c.end_date[4:6]),
154                        day=int(c.end_date[6:]))
[d69b677]155
[54a8a01]156    # assign starting date minus 1 day
157    # since for basetime 00 we need the 12 hours upfront
158    # (the day before from 12 UTC to current day 00 UTC)
159    if c.basetime == '00':
160        start = start - datetime.timedelta(days=1)
[d69b677]161
[2fb99de]162    print('Prepare ' + start.strftime("%Y%m%d") +
163           "/to/" + end.strftime("%Y%m%d"))
[d69b677]164
[02c8c50]165    # create output dir if necessary
[d69b677]166    if not os.path.exists(c.outputdir):
[02c8c50]167        os.makedirs(c.outputdir)
[64cf353]168
[54a8a01]169    # get all files with flux data to be deaccumulated
170    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*')
171
[02c8c50]172    # deaccumulate the flux data
[ff99eae]173    flexpart = EcFlexpart(c, fluxes=True)
[2fb99de]174    flexpart.write_namelist(c, _config.FILE_NAMELIST)
[d69b677]175    flexpart.deacc_fluxes(inputfiles, c)
176
[02c8c50]177    # get a list of all files from the root inputdir
[54a8a01]178    inputfiles = UioFiles(c.inputdir, '????__??.*' + c.ppid + '.*')
[d69b677]179
[54a8a01]180    # produce FLEXPART-ready GRIB files and process them -
[02c8c50]181    # copy/transfer/interpolate them or make them GRIB2
[ff99eae]182    flexpart = EcFlexpart(c, fluxes=False)
[02c8c50]183    flexpart.create(inputfiles, c)
184    flexpart.process_output(c)
185
186    # check if in debugging mode, then store all files
[efdb01a]187    # otherwise delete temporary files
[02c8c50]188    if int(c.debug) != 0:
[2fb99de]189        print('\nTemporary files left intact')
[d69b677]190    else:
[ff99eae]191        clean_up(c)
[02c8c50]192
193    return
[d69b677]194
195if __name__ == "__main__":
[991df6a]196    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG