Changeset ff99eae in flex_extract.git for python/get_mars_data.py


Ignore:
Timestamp:
Jun 1, 2018, 8:34:59 PM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
e1228f3
Parents:
ccab809
Message:

completed application of pep8 style guide and pylint investigations. added documentation almost everywhere

File:
1 moved

Legend:

Unmodified
Added
Removed
  • python/get_mars_data.py

    r991df6a rff99eae  
    11#!/usr/bin/env python
    22# -*- coding: utf-8 -*-
    3 #************************************************************************
    4 # TODO AP
    5 # - add function docstrings!!!!
    6 #************************************************************************
    73#*******************************************************************************
    84# @Author: Anne Fouilloux (University of Oslo)
     
    139#
    1410#    November 2015 - Leopold Haimberger (University of Vienna):
    15 #        - moved the getEIdata program into a function "getMARSdata"
     11#        - moved the getEIdata program into a function "get_mars_data"
    1612#        - moved the AgurmentParser into a seperate function
    1713#        - adatpted the function for the use in flex_extract
    18 #        - renamed file to getMARSdata
     14#        - renamed file to get_mars_data
    1915#
    2016#    February 2018 - Anne Philipp (University of Vienna):
     
    4238# @Program Content:
    4339#    - main
    44 #    - getMARSdata
     40#    - get_mars_data
    4541#
    4642#*******************************************************************************
     
    5450import inspect
    5551try:
    56     ecapi=True
     52    ecapi = True
    5753    import ecmwfapi
    5854except ImportError:
    59     ecapi=False
     55    ecapi = False
     56
     57# software specific classes and modules from flex_extract
     58from tools import my_error, normal_exit, interpret_args_and_control
     59from EcFlexpart import EcFlexpart
     60from UioFiles import UioFiles
    6061
    6162# add path to pythonpath so that python finds its buddies
    62 localpythonpath = os.path.dirname(os.path.abspath(
     63LOCAL_PYTHON_PATH = os.path.dirname(os.path.abspath(
    6364    inspect.getfile(inspect.currentframe())))
    64 if localpythonpath not in sys.path:
    65     sys.path.append(localpythonpath)
    66 
    67 # software specific classes and modules from flex_extract
    68 from ControlFile import ControlFile
    69 from Tools import myerror, normalexit, \
    70                           interpret_args_and_control
    71 from ECFlexpart import ECFlexpart
    72 from UIOFiles import UIOFiles
     65if LOCAL_PYTHON_PATH not in sys.path:
     66    sys.path.append(LOCAL_PYTHON_PATH)
    7367
    7468# ------------------------------------------------------------------------------
     
    7872    '''
    7973    @Description:
    80         If getMARSdata is called from command line, this function controls
     74        If get_mars_data is called from command line, this function controls
    8175        the program flow and calls the argumentparser function and
    82         the getMARSdata function for retrieving EC data.
     76        the get_mars_data function for retrieving EC data.
    8377
    8478    @Input:
     
    8983    '''
    9084    args, c = interpret_args_and_control()
    91     getMARSdata(args, c)
    92     normalexit(c)
     85    get_mars_data(c)
     86    normal_exit(c)
    9387
    9488    return
    9589
    96 def getMARSdata(args, c):
     90def get_mars_data(c):
    9791    '''
    9892    @Description:
     
    10397
    10498    @Input:
    105         args: instance of ArgumentParser
    106             Contains the commandline arguments from script/program call.
    107 
    10899        c: instance of class ControlFile
    109100            Contains all the parameters of CONTROL file, which are e.g.:
     
    126117        os.makedirs(c.inputdir)
    127118
    128     print("Retrieving EC data!")
    129     print("start date %s " % (c.start_date))
    130     print("end date %s " % (c.end_date))
     119    print "Retrieving EC data!"
     120    print "start date %s " % (c.start_date)
     121    print "end date %s " % (c.end_date)
    131122
    132123    if ecapi:
     
    160151    # --------------  flux data ------------------------------------------------
    161152    print 'removing old flux content of ' + c.inputdir
    162     tobecleaned = UIOFiles('*_acc_*.' + str(os.getppid()) + '.*.grb')
    163     tobecleaned.listFiles(c.inputdir)
    164     tobecleaned.deleteFiles()
     153    tobecleaned = UioFiles('*_acc_*.' + str(os.getppid()) + '.*.grb')
     154    tobecleaned.list_files(c.inputdir)
     155    tobecleaned.delete_files()
    165156
    166157    # if forecast for maximum one day (upto 23h) are to be retrieved,
     
    172163        while day < endp1:
    173164            # retrieve MARS data for the whole period
    174             flexpart = ECFlexpart(c, fluxes=True)
     165            flexpart = EcFlexpart(c, fluxes=True)
    175166            tmpday = day + datechunk - datetime.timedelta(days=1)
    176167            if tmpday < endp1:
     
    186177                flexpart.retrieve(server, dates, c.inputdir)
    187178            except IOError:
    188                 myerror(c, 'MARS request failed')
     179                my_error(c, 'MARS request failed')
    189180
    190181            day += datechunk
     
    199190        while day <= end:
    200191            # retrieve MARS data for the whole period
    201             flexpart = ECFlexpart(c, fluxes=True)
     192            flexpart = EcFlexpart(c, fluxes=True)
    202193            tmpday = day + datechunk - datetime.timedelta(days=1)
    203194            if tmpday < end:
     
    213204                flexpart.retrieve(server, dates, c.inputdir)
    214205            except IOError:
    215                 myerror(c, 'MARS request failed')
     206                my_error(c, 'MARS request failed')
    216207
    217208            day += datechunk
     
    219210    # --------------  non flux data --------------------------------------------
    220211    print 'removing old non flux content of ' + c.inputdir
    221     tobecleaned = UIOFiles('*__*.' + str(os.getppid()) + '.*.grb')
    222     tobecleaned.listFiles(c.inputdir)
    223     tobecleaned.deleteFiles()
     212    tobecleaned = UioFiles('*__*.' + str(os.getppid()) + '.*.grb')
     213    tobecleaned.list_files(c.inputdir)
     214    tobecleaned.delete_files()
    224215
    225216    day = start
    226217    while day <= end:
    227             # retrieve all non flux MARS data for the whole period
    228             flexpart = ECFlexpart(c, fluxes=False)
    229             tmpday = day + datechunk - datetime.timedelta(days=1)
    230             if tmpday < end:
    231                 dates = day.strftime("%Y%m%d") + "/to/" + \
    232                         tmpday.strftime("%Y%m%d")
    233             else:
    234                 dates = day.strftime("%Y%m%d") + "/to/" + \
    235                         end.strftime("%Y%m%d")
    236 
    237             print "retrieve " + dates + " in dir " + c.inputdir
    238 
    239             try:
    240                 flexpart.retrieve(server, dates, c.inputdir)
    241             except IOError:
    242                 myerror(c, 'MARS request failed')
    243 
    244             day += datechunk
     218        # retrieve all non flux MARS data for the whole period
     219        flexpart = EcFlexpart(c, fluxes=False)
     220        tmpday = day + datechunk - datetime.timedelta(days=1)
     221        if tmpday < end:
     222            dates = day.strftime("%Y%m%d") + "/to/" + \
     223                    tmpday.strftime("%Y%m%d")
     224        else:
     225            dates = day.strftime("%Y%m%d") + "/to/" + \
     226                    end.strftime("%Y%m%d")
     227
     228        print "retrieve " + dates + " in dir " + c.inputdir
     229
     230        try:
     231            flexpart.retrieve(server, dates, c.inputdir)
     232        except IOError:
     233            my_error(c, 'MARS request failed')
     234
     235        day += datechunk
    245236
    246237    return
     
    248239if __name__ == "__main__":
    249240    main()
    250 
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG