Changeset 64cf353 in flex_extract.git for python/getMARSdata.py


Ignore:
Timestamp:
Feb 8, 2018, 9:54:05 PM (6 years ago)
Author:
Anne Philipp <bscannephilipp@…>
Branches:
master, ctbto, dev
Children:
02c8c50
Parents:
6180177
Message:

pep8 changes + documentation added + minor code style changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/getMARSdata.py

    rd69b677 r64cf353  
    22#
    33# 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 # 
     4# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
     5#
    66# Functionality provided: Prepare input 3D-wind fields in hybrid coordinates + surface fields for FLEXPART runs
    77#
    88# Creation: October  2014 - Anne Fouilloux - University of Oslo
    9 # Extension November 2015 - Leopold Haimberger - University of Vienna for: 
     9# Extension November 2015 - Leopold Haimberger - University of Vienna for:
    1010# - using the WebAPI also for general MARS retrievals
    1111# - job submission on ecgate and cca
     
    1515# - conversion into GRIB2
    1616# - conversion into .fp format for faster execution of FLEXPART
    17 # 
     17#
    1818#
    1919# Further documentation may be obtained from www.flexpart.eu
    20 # 
    21 # Requirements: 
     20#
     21# Requirements:
    2222# in addition to a standard python 2.6 or 2.7 installation the following packages need to be installed
    2323# ECMWF WebMARS, gribAPI with python enabled, emoslib, ecaccess web toolkit, all available from https://software.ecmwf.int/
     
    2626#
    2727# Get MARS GRIB fields from ECMWF for FLEXPART
    28 # 
     28#
    2929
    3030#import socket
     
    5151
    5252from FlexpartTools import MARSretrieval, EIFlexpart, silentremove, \
    53                           Control,myerror,normalexit, interpret_args_and_control
     53                          Control, myerror, normalexit, \
     54                          interpret_args_and_control
    5455
    5556
    56 def getMARSdata(args,c):
    57    
    58        
     57def getMARSdata(args, c):
     58
     59
    5960    if not os.path.exists(c.inputdir):
    6061        os.makedirs(c.inputdir)
    61     print "start date %s "%(c.start_date)
    62     print "end date %s "%(c.end_date)
     62    print("start date %s " % (c.start_date))
     63    print("end date %s " % (c.end_date))
    6364
    64    
    65 #    server = ECMWFDataServer()
    6665    if ecapi:
    6766        server = ecmwfapi.ECMWFService("mars")
     
    6968        server = False
    7069
    71     c.ecapi=ecapi
    72     print 'ecapi:',c.ecapi
     70    c.ecapi = ecapi
     71    print 'ecapi:', c.ecapi
     72
    7373# Retrieve ERA interim data for running flexpart
     74#AP change this variant to correct format conversion with datetime
     75#AP import datetime and timedelta explicitly
     76    syear = int(c.start_date[:4])
     77    smonth = int(c.start_date[4:6])
     78    sday = int(c.start_date[6:])
     79    start = datetime.date(year=syear, month=smonth, day=sday)
     80    startm1 = start - datetime.timedelta(days=1)
     81    if c.basetime == '00':
     82        start = startm1
     83    eyear = int(c.end_date[:4])
     84    emonth = int(c.end_date[4:6])
     85    eday = int(c.end_date[6:])
     86    end = datetime.date(year=eyear, month=emonth, day=eday)
     87    if c.basetime == '00' or c.basetime == '12':
     88        endp1 = end + datetime.timedelta(days=1)
     89    else:
     90        endp1 = end + datetime.timedelta(days=2)
    7491
    75     syear=int(c.start_date[:4])
    76     smonth=int(c.start_date[4:6])
    77     sday=int(c.start_date[6:])
    78     start = datetime.date( year = syear, month = smonth, day = sday )
    79     startm1=start- datetime.timedelta(days=1)
    80     if c.basetime=='00':
    81         start=startm1
    82     eyear=int(c.end_date[:4])
    83     emonth=int(c.end_date[4:6])
    84     eday=int(c.end_date[6:])
    85 
    86     end = datetime.date( year = eyear, month = emonth, day = eday )
    87     if c.basetime=='00' or c.basetime=='12':
    88         endp1=end+ datetime.timedelta(days=1)
    89     else:
    90         endp1=end+ datetime.timedelta(days=2)
    91    
    92     datechunk=datetime.timedelta(days=int(c.date_chunk))
    93     print 'removing content of '+c.inputdir
    94     tobecleaned=glob.glob(c.inputdir+'/*_acc_*.'+str(os.getppid())+'.*.grb')
     92    datechunk = datetime.timedelta(days=int(c.date_chunk))
     93    print 'removing content of ' + c.inputdir
     94    tobecleaned = glob.glob(c.inputdir + '/*_acc_*.' + str(os.getppid()) + '.*.grb')
    9595    for f in tobecleaned:
    9696        os.remove(f)
    97        
     97
    9898    times=None
    9999    if c.maxstep<24:
     
    103103                flexpart = EIFlexpart(c,fluxes=True)
    104104                if day+datechunk-datetime.timedelta(days=1)<endp1:
    105                     dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") 
     105                    dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d")
    106106                else:
    107                     dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") 
    108                    
     107                    dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
     108
    109109                print "retrieve " + dates + " in dir " + c.inputdir
    110110                try:
     
    112112                except IOError:
    113113                    myerror(c,'MARS request failed')
    114                    
     114
    115115                day+=datechunk
    116116    else:
     
    120120                flexpart = EIFlexpart(c,fluxes=True)
    121121                if day+datechunk-datetime.timedelta(days=1)<end:
    122                     dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") 
     122                    dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d")
    123123                else:
    124                     dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") 
    125                    
     124                    dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
     125
    126126                print "retrieve " + dates + " in dir " + c.inputdir
    127127                flexpart.retrieve(server, dates, times, c.inputdir)
    128128                day+=datechunk
    129        
     129
    130130
    131131
     
    136136    times=None
    137137    while day<=end:
    138        
     138
    139139               # we need to retrieve MARS data for this period (maximum one month)
    140140            flexpart = EIFlexpart(c)
    141141            if day+datechunk-datetime.timedelta(days=1)<end:
    142                 dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") 
     142                dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d")
    143143            else:
    144                 dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") 
     144                dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
    145145            print "retrieve " + dates + " in dir " + c.inputdir
    146            
     146
    147147            flexpart.retrieve(server, dates, times, c.inputdir)
    148148            day+=datechunk
    149    
     149
    150150
    151151if __name__ == "__main__":
    152    
     152
    153153    args,c=interpret_args_and_control()
    154154    getMARSdata(args,c)
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG