source: flex_extract.git/Source/Python/Mods/prepare_flexpart.py @ 0f89116

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

diverse changes due to PEP8 style guide and eliminating grib2flexpart; removed unused parameter

  • Property mode set to 100755
File size: 6.2 KB
Line 
1#!/usr/bin/env python3
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#        - BUGFIX: 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-2019.
32#    Anne Philipp, Leopold Haimberger
33#
34#    SPDX-License-Identifier: CC-BY-4.0
35#
36#    This work is licensed under the Creative Commons Attribution 4.0
37#    International License. To view a copy of this license, visit
38#    http://creativecommons.org/licenses/by/4.0/ or send a letter to
39#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
40# *******************************************************************************
41# pylint: disable=ungrouped-imports
42# not necessary that we group the imports
43'''This script prepares the final version of the grib files which are
44then used by FLEXPART.
45
46It converts the bunch of grib files extracted via get_mars_data before,
47by doing the necessary conversion to get consistent grids or the
48disaggregation of flux data. Finally, the data fields are combined
49in files per available hour with the naming convention xxYYMMDDHH,
50where xx should be 2 arbitrary letters (mostly xx is chosen to be "EN").
51
52This file can also be imported as a module which then contains the following
53functions:
54
55    * main
56    * prepare_flexpart
57
58Type: prepare_flexpart.py --help
59to get information about command line parameters.
60Read the documentation for usage instructions.
61'''
62
63# ------------------------------------------------------------------------------
64# MODULES
65# ------------------------------------------------------------------------------
66from __future__ import print_function
67
68import datetime
69import os
70import inspect
71import sys
72
73# software specific classes and modules from flex_extract
74# add path to local main python path for flex_extract to get full access
75sys.path.append(os.path.dirname(os.path.abspath(
76    inspect.getfile(inspect.currentframe()))) + '/../')
77# pylint: disable=wrong-import-position
78#import _config
79from Mods.checks import check_ppid
80from Classes.UioFiles import UioFiles
81#from Classes.ControlFile import ControlFile
82from Mods.tools import (setup_controldata, clean_up, make_dir, normal_exit)
83from Classes.EcFlexpart import EcFlexpart
84# pylint: enable=wrong-import-position
85
86# ------------------------------------------------------------------------------
87# FUNCTION
88# ------------------------------------------------------------------------------
89def main():
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    ----------
97
98    Return
99    ------
100
101    '''
102
103    c, ppid, _, _ = setup_controldata()
104    prepare_flexpart(ppid, c)
105    normal_exit('Preparing FLEXPART output files: Done!')
106
107    return
108
109def prepare_flexpart(ppid, c):
110    '''Converts the mars data into flexpart ready input files.
111
112    Specific data fields are converted to a different grid and the flux
113    data are going to be disaggregated. The data fields are collected by
114    hour and stored in a file with a specific FLEXPART relevant naming
115    convention.
116
117    Parameters
118    ----------
119    ppid : int
120        Contains the ppid number of the current ECMWF job. It will be None if
121        the method was called within this module.
122
123    c : ControlFile
124        Contains all the parameters of CONTROL file and
125        command line.
126
127    Return
128    ------
129
130    '''
131    check_ppid(c, ppid)
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 == 0:
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    if c.stream.lower() == 'elda' and c.doubleelda:
172        flexpart.calc_extra_elda(c.inputdir, c.prefix)
173    flexpart.process_output(c)
174
175    # check if in debugging mode, then store all files
176    # otherwise delete temporary files
177    if c.debug:
178        print('\nTemporary files left intact')
179    else:
180        clean_up(c)
181
182    return
183
184if __name__ == "__main__":
185    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG