source: flex_extract.git/source/python/mods/prepare_flexpart.py @ e811e1a

ctbtodevfeature/makefilesorigin/task/language-editingtask/language-editing
Last change on this file since e811e1a was e811e1a, checked in by Anne Philipp <anne.philipp@…>, 2 years ago

adapted calc_extra_elda parameter list

  • Property mode set to 100755
File size: 6.4 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
[6f951ca]24#        - BUGFIX: 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:
[6f951ca]31#    (C) Copyright 2014-2019.
32#    Anne Philipp, Leopold Haimberger
[991df6a]33#
[6f951ca]34#    This work is licensed under the Creative Commons Attribution 4.0
35#    International License. To view a copy of this license, visit
36#    http://creativecommons.org/licenses/by/4.0/ or send a letter to
37#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
[991df6a]38#*******************************************************************************
[6f951ca]39'''This script prepares the final version of the grib files which are
40then used by FLEXPART.
41
42It converts the bunch of grib files extracted via get_mars_data before,
43by doing the necessary conversion to get consistent grids or the
44disaggregation of flux data. Finally, the data fields are combined
45in files per available hour with the naming convention xxYYMMDDHH,
46where xx should be 2 arbitrary letters (mostly xx is chosen to be "EN").
47
48This file can also be imported as a module which then contains the following
49functions:
50
51    * main
52    * prepare_flexpart
53
54Type: prepare_flexpart.py --help
55to get information about command line parameters.
56Read the documentation for usage instructions.
57'''
[991df6a]58
[efdb01a]59# ------------------------------------------------------------------------------
60# MODULES
61# ------------------------------------------------------------------------------
[d69b677]62import datetime
[efdb01a]63import os
64import inspect
65import sys
[d69b677]66import socket
67
[ff99eae]68# software specific classes and modules from flex_extract
[6f951ca]69# add path to local main python path for flex_extract to get full access
[70fee58]70sys.path.append(os.path.dirname(os.path.abspath(
71    inspect.getfile(inspect.currentframe()))) + '/../')
[2fb99de]72import _config
[3f36e42]73from checks import check_ppid
[25b14be]74from classes.UioFiles import UioFiles
[ca867de]75from classes.ControlFile import ControlFile
[9aefaad]76from tools import clean_up, get_cmdline_args, read_ecenv, make_dir
[25b14be]77from classes.EcFlexpart import EcFlexpart
[ff99eae]78
79ecapi = 'ecmwf' not in socket.gethostname()
[d69b677]80try:
81    if ecapi:
82        import ecmwfapi
83except ImportError:
[64cf353]84    ecapi = False
85
[efdb01a]86# ------------------------------------------------------------------------------
87# FUNCTION
88# ------------------------------------------------------------------------------
[991df6a]89def main():
[274f9ef]90    '''Controls the program to prepare flexpart input files from mars data.
91
92    This is done if it is called directly from command line.
93    Then it also takes program call arguments and control file input.
94
95    Parameters
96    ----------
[991df6a]97
[274f9ef]98    Return
99    ------
[02c8c50]100
[991df6a]101    '''
[54a8a01]102
[9aefaad]103    args = get_cmdline_args()
[4971f63]104    c = ControlFile(args.controlfile)
[2fb99de]105
106    env_parameter = read_ecenv(_config.PATH_ECMWF_ENV)
[295ff45]107    c.assign_args_to_control(args)
[54a8a01]108    c.assign_envs_to_control(env_parameter)
[2fb99de]109    c.check_conditions(args.queue)
[295ff45]110
[54a8a01]111    prepare_flexpart(args.ppid, c)
[991df6a]112
113    return
114
[54a8a01]115def prepare_flexpart(ppid, c):
[274f9ef]116    '''Converts the mars data into flexpart ready input files.
117
118    Specific data fields are converted to a different grid and the flux
119    data are going to be disaggregated. The data fields are collected by
120    hour and stored in a file with a specific FLEXPART relevant naming
121    convention.
122
123    Parameters
124    ----------
[6f951ca]125    ppid : int
[274f9ef]126        Contains the ppid number of the current ECMWF job. It will be None if
127        the method was called within this module.
128
[6f951ca]129    c : ControlFile
[274f9ef]130        Contains all the parameters of CONTROL file and
131        command line.
132
133    Return
134    ------
135
[02c8c50]136    '''
[3f36e42]137    check_ppid(c, ppid)
[02c8c50]138
139    c.ecapi = ecapi
[d69b677]140
[02c8c50]141    # create the start and end date
142    start = datetime.date(year=int(c.start_date[:4]),
143                          month=int(c.start_date[4:6]),
144                          day=int(c.start_date[6:]))
[d69b677]145
[02c8c50]146    end = datetime.date(year=int(c.end_date[:4]),
147                        month=int(c.end_date[4:6]),
148                        day=int(c.end_date[6:]))
[d69b677]149
[38e83ba]150    # if basetime is 00
[54a8a01]151    # assign starting date minus 1 day
[38e83ba]152    # since we need the 12 hours upfront
[54a8a01]153    # (the day before from 12 UTC to current day 00 UTC)
[d4696e0]154    if c.basetime == 0:
[54a8a01]155        start = start - datetime.timedelta(days=1)
[d69b677]156
[2fb99de]157    print('Prepare ' + start.strftime("%Y%m%d") +
158           "/to/" + end.strftime("%Y%m%d"))
[d69b677]159
[02c8c50]160    # create output dir if necessary
[d69b677]161    if not os.path.exists(c.outputdir):
[5bad6ec]162        make_dir(c.outputdir)
[64cf353]163
[54a8a01]164    # get all files with flux data to be deaccumulated
[70fee58]165    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + str(c.ppid) + '.*')
[54a8a01]166
[02c8c50]167    # deaccumulate the flux data
[ff99eae]168    flexpart = EcFlexpart(c, fluxes=True)
[c5074d2]169    flexpart.write_namelist(c)
[d69b677]170    flexpart.deacc_fluxes(inputfiles, c)
171
[38e83ba]172    # get a list of all other files
[70fee58]173    inputfiles = UioFiles(c.inputdir, '????__??.*' + str(c.ppid) + '.*')
[d69b677]174
[54a8a01]175    # produce FLEXPART-ready GRIB files and process them -
[02c8c50]176    # copy/transfer/interpolate them or make them GRIB2
[ff99eae]177    flexpart = EcFlexpart(c, fluxes=False)
[02c8c50]178    flexpart.create(inputfiles, c)
[45b99e6]179    if c.stream.lower() == 'elda':
[e811e1a]180        flexpart.calc_extra_elda(c.inputdir, c.prefix)
[02c8c50]181    flexpart.process_output(c)
[38e83ba]182
183    # make use of a possible conversion to a
184    # specific flexpart binary format
[27fe969]185    if c.grib2flexpart:
186        flexpart.prepare_fp_files(c)
[02c8c50]187
188    # check if in debugging mode, then store all files
[efdb01a]189    # otherwise delete temporary files
[27fe969]190    if c.debug:
[2fb99de]191        print('\nTemporary files left intact')
[d69b677]192    else:
[ff99eae]193        clean_up(c)
[02c8c50]194
195    return
[d69b677]196
197if __name__ == "__main__":
[991df6a]198    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG