source: flex_extract.git/Source/Python/Mods/prepare_flexpart.py @ 44174de

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

commented out the WRF parts since they are still under construction and added License SPDX tags

  • 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'''This script prepares the final version of the grib files which are
42then used by FLEXPART.
43
44It converts the bunch of grib files extracted via get_mars_data before,
45by doing the necessary conversion to get consistent grids or the
46disaggregation of flux data. Finally, the data fields are combined
47in files per available hour with the naming convention xxYYMMDDHH,
48where xx should be 2 arbitrary letters (mostly xx is chosen to be "EN").
49
50This file can also be imported as a module which then contains the following
51functions:
52
53    * main
54    * prepare_flexpart
55
56Type: prepare_flexpart.py --help
57to get information about command line parameters.
58Read the documentation for usage instructions.
59'''
60
61# ------------------------------------------------------------------------------
62# MODULES
63# ------------------------------------------------------------------------------
64from __future__ import print_function
65
66import datetime
67import os
68import inspect
69import sys
70import socket
71
72# software specific classes and modules from flex_extract
73# add path to local main python path for flex_extract to get full access
74sys.path.append(os.path.dirname(os.path.abspath(
75    inspect.getfile(inspect.currentframe()))) + '/../')
76import _config
77from Mods.checks import check_ppid
78from Classes.UioFiles import UioFiles
79from Classes.ControlFile import ControlFile
80from Mods.tools import (setup_controldata, clean_up, get_cmdline_args,
81                        read_ecenv, make_dir, normal_exit)
82from Classes.EcFlexpart import EcFlexpart
83
84# ------------------------------------------------------------------------------
85# FUNCTION
86# ------------------------------------------------------------------------------
87def main():
88    '''Controls the program to prepare flexpart input files from mars data.
89
90    This is done if it is called directly from command line.
91    Then it also takes program call arguments and control file input.
92
93    Parameters
94    ----------
95
96    Return
97    ------
98
99    '''
100
101    c, ppid, _, _ = setup_controldata()
102    prepare_flexpart(ppid, c)
103    normal_exit('Preparing FLEXPART output files: Done!')
104
105    return
106
107def prepare_flexpart(ppid, c):
108    '''Converts the mars data into flexpart ready input files.
109
110    Specific data fields are converted to a different grid and the flux
111    data are going to be disaggregated. The data fields are collected by
112    hour and stored in a file with a specific FLEXPART relevant naming
113    convention.
114
115    Parameters
116    ----------
117    ppid : int
118        Contains the ppid number of the current ECMWF job. It will be None if
119        the method was called within this module.
120
121    c : ControlFile
122        Contains all the parameters of CONTROL file and
123        command line.
124
125    Return
126    ------
127
128    '''
129    check_ppid(c, ppid)
130
131    # create the start and end date
132    start = datetime.date(year=int(c.start_date[:4]),
133                          month=int(c.start_date[4:6]),
134                          day=int(c.start_date[6:]))
135
136    end = datetime.date(year=int(c.end_date[:4]),
137                        month=int(c.end_date[4:6]),
138                        day=int(c.end_date[6:]))
139
140    # if basetime is 00
141    # assign starting date minus 1 day
142    # since we need the 12 hours upfront
143    # (the day before from 12 UTC to current day 00 UTC)
144    if c.basetime == 0:
145        start = start - datetime.timedelta(days=1)
146
147    print('Prepare ' + start.strftime("%Y%m%d") +
148           "/to/" + end.strftime("%Y%m%d"))
149
150    # create output dir if necessary
151    if not os.path.exists(c.outputdir):
152        make_dir(c.outputdir)
153
154    # get all files with flux data to be deaccumulated
155    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + str(c.ppid) + '.*')
156
157    # deaccumulate the flux data
158    flexpart = EcFlexpart(c, fluxes=True)
159    flexpart.write_namelist(c)
160    flexpart.deacc_fluxes(inputfiles, c)
161
162    # get a list of all other files
163    inputfiles = UioFiles(c.inputdir, '????__??.*' + str(c.ppid) + '.*')
164
165    # produce FLEXPART-ready GRIB files and process them -
166    # copy/transfer/interpolate them or make them GRIB2
167    flexpart = EcFlexpart(c, fluxes=False)
168    flexpart.create(inputfiles, c)
169    if c.stream.lower() == 'elda' and c.doubleelda:
170        flexpart.calc_extra_elda(c.inputdir, c.prefix)
171    flexpart.process_output(c)
172
173    # make use of a possible conversion to a
174    # specific flexpart binary format
175    if c.grib2flexpart:
176        flexpart.prepare_fp_files(c)
177
178    # check if in debugging mode, then store all files
179    # otherwise delete temporary files
180    if c.debug:
181        print('\nTemporary files left intact')
182    else:
183        clean_up(c)
184
185    return
186
187if __name__ == "__main__":
188    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG