Changeset 02c8c50 in flex_extract.git for python/prepareFLEXPART.py


Ignore:
Timestamp:
Mar 18, 2018, 6:27:28 PM (6 years ago)
Author:
Anne Philipp <bscannephilipp@…>
Branches:
master, ctbto, dev
Children:
efdb01a
Parents:
64cf353
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/prepareFLEXPART.py

    r64cf353 r02c8c50  
    11#!/usr/bin/env python
    2 #
    3 # This software is licensed under the terms of the Apache Licence Version 2.0
    4 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
    5 #
    6 # Functionality provided: Prepare input 3D-wind fields in hybrid coordinates + surface fields for FLEXPART runs
    7 #
    8 # Creation: October  2014 - Anne Fouilloux - University of Oslo
    9 # Extension November 2015 - Leopold Haimberger - University of Vienna for:
    10 # - using the WebAPI also for general MARS retrievals
    11 # - job submission on ecgate and cca
    12 # - job templates suitable for twice daily operational dissemination
    13 # - dividing retrievals of longer periods into digestable chunks
    14 # - retrieve also longer term forecasts, not only analyses and short term forecast data
    15 # - conversion into GRIB2
    16 # - conversion into .fp format for faster execution of FLEXPART
    17 #
    18 # Requirements:
    19 # in addition to a standard python 2.6 or 2.7 installation the following packages need to be installed
    20 # ECMWF WebMARS, gribAPI with python enabled, emoslib, ecaccess web toolkit,
    21 # all available from https://software.ecmwf.int/
    22 # dateutils
    23 # matplotlib (optional, for debugging)
    24 #
     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
    2555import calendar
    2656import shutil
     
    2959import os,inspect,sys
    3060import socket
    31 from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter
     61from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    3262# add path to submit.py to pythonpath so that python finds its buddies
    3363localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
     
    3767#from string import strip
    3868from GribTools import GribTools
    39 from FlexpartTools import EIFlexpart, Control, interpret_args_and_control, cleanup
     69from FlexpartTools import ECFlexpart, Control, interpret_args_and_control, cleanup
    4070
    4171hostname=socket.gethostname()
     
    4979
    5080
    51 def prepareFLEXPART(args,c):
     81def prepareFLEXPART(args, c):
     82    '''
     83    @Description:
    5284
    5385
     86    @Input:
     87        args: instance of ArgumentParser
     88            Contains the commandline arguments from script/program call.
    5489
    55     namelist='fort.4'
     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
     99
     100            For more information about format and content of the parameter
     101            see documentation.
     102
     103    @Return:
     104        <nothing>
     105    '''
    56106
    57107    if not args.ppid:
    58         c.ppid=str(os.getppid())
     108        c.ppid = str(os.getppid())
    59109    else:
    60         c.ppid=args.ppid
     110        c.ppid = args.ppid
    61111
    62     c.ecapi=ecapi
     112    c.ecapi = ecapi
    63113
    64     syear=int(c.start_date[:4])
    65     smonth=int(c.start_date[4:6])
    66     sday=int(c.start_date[6:])
    67     start = datetime.date( year = syear, month = smonth, day = sday )
    68     eyear=int(c.end_date[:4])
    69     emonth=int(c.end_date[4:6])
    70     eday=int(c.end_date[6:])
     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:]))
    71118
    72     end = datetime.date( year = eyear, month = emonth, day = eday )
     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:]))
    73122
    74     inputfiles=UIOFiles(['.grib', '.grb', '.grib1', '.grib2', '.grb1','.grb2'])
     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)
    75128
    76     startm1=start- datetime.timedelta(days=1)
    77     endp1=end + datetime.timedelta(days=1)
     129    # get all files with flux data to be deaccumulated
     130    inputfiles = UIOFiles(['.grib', '.grb', '.grib1',
     131                           '.grib2', '.grb1', '.grb2'])
    78132
    79     inputfiles.listFiles(c.inputdir, '*OG_acc_SL*.'+c.ppid+'.*')
     133    inputfiles.listFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*')
     134
     135    # create output dir if necessary
    80136    if not os.path.exists(c.outputdir):
    81     os.makedirs(c.outputdir)
     137        os.makedirs(c.outputdir)
    82138
    83     flexpart = EIFlexpart(c,fluxes=True)
    84     flexpart.create_namelist(c,'fort.4')
     139    # deaccumulate the flux data
     140    flexpart = ECFlexpart(c, fluxes=True)
     141    flexpart.write_namelist(c, 'fort.4')
    85142    flexpart.deacc_fluxes(inputfiles, c)
    86143
    87     inputfiles=UIOFiles(['.grib', '.grb', '.grib1', '.grib2', '.grb1','.grb2'])
     144    print('Prepare ' + start.strftime("%Y%m%d") +
     145          "/to/" + end.strftime("%Y%m%d"))
    88146
    89     print 'Prepare '+start.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
    90     # we will make the list of files from the root inputdir
    91     inputfiles.listFiles(c.inputdir, '????__??.*'+c.ppid+'.*')
     147    # get a list of all files from the root inputdir
     148    inputfiles = UIOFiles(['.grib', '.grb', '.grib1',
     149                           '.grib2', '.grb1', '.grb2'])
    92150
    93     if c.basetime=='00':
    94         start=startm1
    95     flexpart = EIFlexpart(c)
    96     flexpart.create(inputfiles, c) # produce FLEXPART-ready GRIB files
    97     flexpart.process_output(c) # process GRIB files - copy/transfer/interpolate them or make them GRIB2
     151    inputfiles.listFiles(c.inputdir, '????__??.*' + c.ppid + '.*')
    98152
    99     if int(c.debug)!=0:
    100     print('Temporary files left intact')
     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')
    101166    else:
    102     cleanup(c)
     167        cleanup(c)
     168
     169    return
    103170
    104171if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG