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

ctbtodev
Last change on this file since 8209738 was 8209738, checked in by anphi <anne.philipp@…>, 4 years ago

language corrections in comment sections and print commands

  • Property mode set to 100755
File size: 6.1 KB
RevLine 
[8463d78]1#!/usr/bin/env python3
[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
[8209738]15#        - retrieve also longer-term forecasts, not only analyses and
16#          short-term forecast data
[991df6a]17#        - conversion into GRIB2
[8209738]18#       
[991df6a]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#
[44174de]34#    SPDX-License-Identifier: CC-BY-4.0
35#
[6f951ca]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.
[0f89116]40# *******************************************************************************
41# pylint: disable=ungrouped-imports
42# not necessary that we group the imports
[6f951ca]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,
[8209738]47by doing the necessary conversion to obtain consistent grids or the
[6f951ca]48disaggregation of flux data. Finally, the data fields are combined
[8209738]49in files per hour available with the naming convention xxYYMMDDHH,
[6f951ca]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'''
[991df6a]62
[efdb01a]63# ------------------------------------------------------------------------------
64# MODULES
65# ------------------------------------------------------------------------------
[0e08483]66from __future__ import print_function
67
[d69b677]68import datetime
[efdb01a]69import os
70import inspect
71import sys
[d69b677]72
[ff99eae]73# software specific classes and modules from flex_extract
[6f951ca]74# add path to local main python path for flex_extract to get full access
[70fee58]75sys.path.append(os.path.dirname(os.path.abspath(
76    inspect.getfile(inspect.currentframe()))) + '/../')
[0f89116]77# pylint: disable=wrong-import-position
78#import _config
[ba99230]79from Mods.checks import check_ppid
80from Classes.UioFiles import UioFiles
[0f89116]81#from Classes.ControlFile import ControlFile
82from Mods.tools import (setup_controldata, clean_up, make_dir, normal_exit)
[ba99230]83from Classes.EcFlexpart import EcFlexpart
[0f89116]84# pylint: enable=wrong-import-position
[ff99eae]85
[efdb01a]86# ------------------------------------------------------------------------------
87# FUNCTION
88# ------------------------------------------------------------------------------
[991df6a]89def main():
[8209738]90    '''Controls the program to prepare FLEXPART input files from MARS data.
[274f9ef]91
[8209738]92    This is done if called directly from the command line.
93    Then, arguments and control file are taken as input.
[274f9ef]94
95    Parameters
96    ----------
[991df6a]97
[274f9ef]98    Return
99    ------
[02c8c50]100
[991df6a]101    '''
[54a8a01]102
[f20af73]103    c, ppid, _, _ = setup_controldata()
104    prepare_flexpart(ppid, c)
105    normal_exit('Preparing FLEXPART output files: Done!')
[991df6a]106
107    return
108
[54a8a01]109def prepare_flexpart(ppid, c):
[8209738]110    '''Converts the MARS data into files ready as input for FLEXPART.
[274f9ef]111
[8209738]112    Certain fields are converted to a different grid and the flux
113    data are disaggregated. Fields are collected by hour and stored in a file
114    with a specific naming convention.
[274f9ef]115
116    Parameters
117    ----------
[6f951ca]118    ppid : int
[274f9ef]119        Contains the ppid number of the current ECMWF job. It will be None if
120        the method was called within this module.
121
[6f951ca]122    c : ControlFile
[274f9ef]123        Contains all the parameters of CONTROL file and
124        command line.
125
126    Return
127    ------
128
[02c8c50]129    '''
[3f36e42]130    check_ppid(c, ppid)
[02c8c50]131
132    # create the start and end date
133    start = datetime.date(year=int(c.start_date[:4]),
134                          month=int(c.start_date[4:6]),
135                          day=int(c.start_date[6:]))
[d69b677]136
[02c8c50]137    end = datetime.date(year=int(c.end_date[:4]),
138                        month=int(c.end_date[4:6]),
139                        day=int(c.end_date[6:]))
[d69b677]140
[38e83ba]141    # if basetime is 00
[54a8a01]142    # assign starting date minus 1 day
[38e83ba]143    # since we need the 12 hours upfront
[54a8a01]144    # (the day before from 12 UTC to current day 00 UTC)
[d4696e0]145    if c.basetime == 0:
[54a8a01]146        start = start - datetime.timedelta(days=1)
[d69b677]147
[2fb99de]148    print('Prepare ' + start.strftime("%Y%m%d") +
[0f89116]149          '/to/' + end.strftime("%Y%m%d"))
[d69b677]150
[02c8c50]151    # create output dir if necessary
[d69b677]152    if not os.path.exists(c.outputdir):
[5bad6ec]153        make_dir(c.outputdir)
[64cf353]154
[54a8a01]155    # get all files with flux data to be deaccumulated
[70fee58]156    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + str(c.ppid) + '.*')
[54a8a01]157
[02c8c50]158    # deaccumulate the flux data
[ff99eae]159    flexpart = EcFlexpart(c, fluxes=True)
[c5074d2]160    flexpart.write_namelist(c)
[d69b677]161    flexpart.deacc_fluxes(inputfiles, c)
162
[38e83ba]163    # get a list of all other files
[70fee58]164    inputfiles = UioFiles(c.inputdir, '????__??.*' + str(c.ppid) + '.*')
[d69b677]165
[54a8a01]166    # produce FLEXPART-ready GRIB files and process them -
[02c8c50]167    # copy/transfer/interpolate them or make them GRIB2
[ff99eae]168    flexpart = EcFlexpart(c, fluxes=False)
[02c8c50]169    flexpart.create(inputfiles, c)
[90a1ca0]170    if c.stream.lower() == 'elda' and c.doubleelda:
[e811e1a]171        flexpart.calc_extra_elda(c.inputdir, c.prefix)
[02c8c50]172    flexpart.process_output(c)
[38e83ba]173
[02c8c50]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