source: flex_extract.git/python/prepareFLEXPART.py @ 02c8c50

ctbtodev
Last change on this file since 02c8c50 was 02c8c50, checked in by Anne Philipp <bscannephilipp@…>, 6 years ago

more changes in PEP8 style and slight modifications in coding style and naming. More documentation of functions.

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[d69b677]1#!/usr/bin/env python
[02c8c50]2# -*- coding: utf-8 -*-
3#************************************************************************
4# TODO AP
5#AP
6# - Change History ist nicht angepasst ans File! Original geben lassen
7# - wieso cleanup in main wenn es in prepareflexpart bereits abgefragt wurde?
8#   doppelt gemoppelt?
9# - wieso start=startm1 wenn basetime = 0 ?  wenn die fluxes nicht mehr
10#   relevant sind? verstehe ich nicht
11#************************************************************************
12"""
13@Author: Anne Fouilloux (University of Oslo)
14
15@Date: October 2014
16
17@ChangeHistory:
18    November 2015 - Leopold Haimberger (University of Vienna):
19        - using the WebAPI also for general MARS retrievals
20        - job submission on ecgate and cca
21        - job templates suitable for twice daily operational dissemination
22        - dividing retrievals of longer periods into digestable chunks
23        - retrieve also longer term forecasts, not only analyses and
24          short term forecast data
25        - conversion into GRIB2
26        - conversion into .fp format for faster execution of FLEXPART
27
28    February 2018 - Anne Philipp (University of Vienna):
29        - applied PEP8 style guide
30        - added documentation
31        - minor changes in programming style for consistence
32
33@License:
34    (C) Copyright 2014.
35
36    This software is licensed under the terms of the Apache Licence Version 2.0
37    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
38
39@Requirements:
40    - A standard python 2.6 or 2.7 installation
41    - dateutils
42    - matplotlib (optional, for debugging)
43    - ECMWF specific packages, all available from https://software.ecmwf.int/
44        ECMWF WebMARS, gribAPI with python enabled, emoslib and
45        ecaccess web toolkit
46
47@Description:
48    Further documentation may be obtained from www.flexpart.eu.
49
50    Functionality provided:
51        Prepare input 3D-wind fields in hybrid coordinates +
52        surface fields for FLEXPART runs
53"""
54
[d69b677]55import calendar
56import shutil
57import datetime
58import time
59import os,inspect,sys
60import socket
[02c8c50]61from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
[d69b677]62# add path to submit.py to pythonpath so that python finds its buddies
63localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
64if localpythonpath not in sys.path:
65    sys.path.append(localpythonpath)
66from UIOTools import UIOFiles
67#from string import strip
68from GribTools import GribTools
[02c8c50]69from FlexpartTools import ECFlexpart, Control, interpret_args_and_control, cleanup
[d69b677]70
71hostname=socket.gethostname()
72ecapi= 'ecmwf' not in hostname
73
74try:
75    if ecapi:
76        import ecmwfapi
77except ImportError:
[64cf353]78    ecapi = False
79
[d69b677]80
[02c8c50]81def prepareFLEXPART(args, c):
82    '''
83    @Description:
84
[d69b677]85
[02c8c50]86    @Input:
87        args: instance of ArgumentParser
88            Contains the commandline arguments from script/program call.
[d69b677]89
[02c8c50]90        c: instance of class Control
91            Contains all the parameters of control files, which are e.g.:
92            DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME,
93            STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT,
94            LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY,
95            OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT,
96            ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR,
97            MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME
98            DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS
[d69b677]99
[02c8c50]100            For more information about format and content of the parameter
101            see documentation.
102
103    @Return:
104        <nothing>
105    '''
[64cf353]106
[d69b677]107    if not args.ppid:
[02c8c50]108        c.ppid = str(os.getppid())
[d69b677]109    else:
[02c8c50]110        c.ppid = args.ppid
111
112    c.ecapi = ecapi
[d69b677]113
[02c8c50]114    # create the start and end date
115    start = datetime.date(year=int(c.start_date[:4]),
116                          month=int(c.start_date[4:6]),
117                          day=int(c.start_date[6:]))
[d69b677]118
[02c8c50]119    end = datetime.date(year=int(c.end_date[:4]),
120                        month=int(c.end_date[4:6]),
121                        day=int(c.end_date[6:]))
[d69b677]122
[02c8c50]123    # to deaccumulated the fluxes correctly
124    # one day ahead of the start date and
125    # one day after the end date is needed
126    startm1 = start - datetime.timedelta(days=1)
127    endp1 = end + datetime.timedelta(days=1)
[d69b677]128
[02c8c50]129    # get all files with flux data to be deaccumulated
130    inputfiles = UIOFiles(['.grib', '.grb', '.grib1',
131                           '.grib2', '.grb1', '.grb2'])
[d69b677]132
[02c8c50]133    inputfiles.listFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*')
[d69b677]134
[02c8c50]135    # create output dir if necessary
[d69b677]136    if not os.path.exists(c.outputdir):
[02c8c50]137        os.makedirs(c.outputdir)
[64cf353]138
[02c8c50]139    # deaccumulate the flux data
140    flexpart = ECFlexpart(c, fluxes=True)
141    flexpart.write_namelist(c, 'fort.4')
[d69b677]142    flexpart.deacc_fluxes(inputfiles, c)
143
[02c8c50]144    print('Prepare ' + start.strftime("%Y%m%d") +
145          "/to/" + end.strftime("%Y%m%d"))
[d69b677]146
[02c8c50]147    # get a list of all files from the root inputdir
148    inputfiles = UIOFiles(['.grib', '.grb', '.grib1',
149                           '.grib2', '.grb1', '.grb2'])
[d69b677]150
[02c8c50]151    inputfiles.listFiles(c.inputdir, '????__??.*' + c.ppid + '.*')
[d69b677]152
[02c8c50]153    # produce FLEXPART-ready GRIB files and
154    # process GRIB files -
155    # copy/transfer/interpolate them or make them GRIB2
156    if c.basetime == '00':
157        start = startm1
158
159    flexpart = ECFlexpart(c, fluxes=False)
160    flexpart.create(inputfiles, c)
161    flexpart.process_output(c)
162
163    # check if in debugging mode, then store all files
164    if int(c.debug) != 0:
165        print('Temporary files left intact')
[d69b677]166    else:
[02c8c50]167        cleanup(c)
168
169    return
[d69b677]170
171if __name__ == "__main__":
[64cf353]172    args, c = interpret_args_and_control()
173    prepareFLEXPART(args, c)
[d69b677]174    cleanup(c)
[64cf353]175
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG