source: flex_extract.git/source/python/mods/prepare_flexpart.py @ 631ba13

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

refactoring large function get_mars_data into several smaller functions

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