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

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

capitalize all directory names and adapt pathes in files

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