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

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

added CDS API support for ERA5 instead of ECMWFAPI / refactored setup of CONTROL parameter because for local version it wanted to use not installed ECMWF_ENV file.

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