Changeset efdb01a in flex_extract.git for python/getMARSdata.py


Ignore:
Timestamp:
May 9, 2018, 12:15:00 PM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
991df6a
Parents:
02c8c50
Message:

whole bunch of modifications due to new structure of ECMWFDATA, added basics of documentation, minor programming corrections

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/getMARSdata.py

    • Property mode changed from 100644 to 100755
    r02c8c50 refdb01a  
    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 #
    19 # Further documentation may be obtained from www.flexpart.eu
    20 #
    21 # Requirements:
    22 # in addition to a standard python 2.6 or 2.7 installation the following packages need to be installed
    23 # ECMWF WebMARS, gribAPI with python enabled, emoslib, ecaccess web toolkit, all available from https://software.ecmwf.int/
    24 # dateutils
    25 # matplotlib (optional, for debugging)
    26 #
    27 # Get MARS GRIB fields from ECMWF for FLEXPART
    28 #
    29 
    30 #import socket
    31 
    32 #hostname=socket.gethostname()
    33 #ecapi= 'ecmwf' not in hostname
     2# -*- coding: utf-8 -*-
     3#************************************************************************
     4# TODO AP
     5# - Change History ist nicht angepasst ans File!
     6# - add file description
     7#************************************************************************
     8"""
     9@Author: Anne Fouilloux (University of Oslo)
     10
     11@Date: October 2014
     12
     13@ChangeHistory:
     14    November 2015 - Leopold Haimberger (University of Vienna):
     15        - using the WebAPI also for general MARS retrievals
     16        - job submission on ecgate and cca
     17        - job templates suitable for twice daily operational dissemination
     18        - dividing retrievals of longer periods into digestable chunks
     19        - retrieve also longer term forecasts, not only analyses and
     20          short term forecast data
     21        - conversion into GRIB2
     22        - conversion into .fp format for faster execution of FLEXPART
     23
     24    February 2018 - Anne Philipp (University of Vienna):
     25        - applied PEP8 style guide
     26        - added documentation
     27        - minor changes in programming style for consistence
     28
     29@License:
     30    (C) Copyright 2014-2018.
     31
     32    This software is licensed under the terms of the Apache Licence Version 2.0
     33    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
     34
     35@Requirements:
     36    - A standard python 2.6 or 2.7 installation
     37    - dateutils
     38    - ECMWF specific packages, all available from https://software.ecmwf.int/
     39        ECMWF WebMARS, gribAPI with python enabled, emoslib and
     40        ecaccess web toolkit
     41
     42@Description:
     43    Further documentation may be obtained from www.flexpart.eu.
     44
     45"""
     46# ------------------------------------------------------------------------------
     47# MODULES
     48# ------------------------------------------------------------------------------
    3449try:
    3550    ecapi=True
     
    4257import datetime
    4358import time
    44 import os,glob,sys,inspect
    45 #from string import strip
    46 from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter
     59import os
     60import glob
     61import sys
     62import inspect
    4763# add path to submit.py to pythonpath so that python finds its buddies
    4864localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
     
    5066    sys.path.append(localpythonpath)
    5167
    52 from FlexpartTools import ECFlexpart,  \
    53                           Control, myerror, normalexit, \
     68from Control import Control
     69from Tools import myerror, normalexit, \
    5470                          interpret_args_and_control
    55 
    56 
     71from ECFlexpart import ECFlexpart
     72
     73# ------------------------------------------------------------------------------
     74# FUNCTION
     75# ------------------------------------------------------------------------------
    5776def getMARSdata(args, c):
    5877
     
    7190    print 'ecapi:', c.ecapi
    7291
    73 # Retrieve ERA interim data for running flexpart
     92# Retrieve EC data for running flexpart
    7493#AP change this variant to correct format conversion with datetime
    7594#AP import datetime and timedelta explicitly
     
    81100    if c.basetime == '00':
    82101        start = startm1
     102
    83103    eyear = int(c.end_date[:4])
    84104    emonth = int(c.end_date[4:6])
     
    91111
    92112    datechunk = datetime.timedelta(days=int(c.date_chunk))
     113
     114    # retrieving of accumulated data fields (flux data), (maximum one month)
     115
     116    # remove old files
    93117    print 'removing content of ' + c.inputdir
    94     tobecleaned = glob.glob(c.inputdir + '/*_acc_*.' + str(os.getppid()) + '.*.grb')
     118    tobecleaned = glob.glob(c.inputdir + '/*_acc_*.' + \
     119                            str(os.getppid()) + '.*.grb')
    95120    for f in tobecleaned:
    96121        os.remove(f)
    97122
    98     times=None
    99     if c.maxstep<24:
    100         day=startm1
    101         while day<endp1:
    102                 # we need to retrieve MARS data for this period (maximum one month)
    103                 flexpart = ECFlexpart(c,fluxes=True)
    104                 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")
     123    times = None
     124    # if forecast for maximum one day (upto 23h) are to be retrieved,
     125    # collect accumulation data (flux data)
     126    # with additional days in the beginning and at the end
     127    # (used for complete disaggregation of original period)
     128    if c.maxstep < 24:
     129        day = startm1
     130        while day < endp1:
     131                # retrieve MARS data for the whole period
     132                flexpart = ECFlexpart(c, fluxes=True)
     133                tmpday = day + datechunk - datetime.timedelta(days=1)
     134                if tmpday < endp1:
     135                    dates = day.strftime("%Y%m%d") + "/to/" + \
     136                            tmpday.strftime("%Y%m%d")
    106137                else:
    107                     dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
     138                    dates = day.strftime("%Y%m%d") + "/to/" + \
     139                            end.strftime("%Y%m%d")
    108140
    109141                print "retrieve " + dates + " in dir " + c.inputdir
     142
    110143                try:
    111144                    flexpart.retrieve(server, dates, times, c.inputdir)
     
    113146                    myerror(c,'MARS request failed')
    114147
    115                 day+=datechunk
     148                day += datechunk
     149
     150    # if forecast data longer than 24h are to be retrieved,
     151    # collect accumulation data (flux data)
     152    # with the exact start and end date
     153    # (disaggregation will be done for the
     154    # exact time period with boundary conditions)
    116155    else:
    117         day=start
    118         while day<=end:
    119                 # we need to retrieve MARS data for this period (maximum one month)
    120                 flexpart = ECFlexpart(c,fluxes=True)
    121                 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")
     156        day = start
     157        while day <= end:
     158                # retrieve MARS data for the whole period
     159                flexpart = ECFlexpart(c, fluxes=True)
     160                tmpday = day + datechunk - datetime.timedelta(days=1)
     161                if tmpday < end:
     162                    dates = day.strftime("%Y%m%d") + "/to/" + \
     163                            tmpday.trftime("%Y%m%d")
    123164                else:
    124                     dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
     165                    dates = day.strftime("%Y%m%d") + "/to/" + \
     166                            end.strftime("%Y%m%d")
    125167
    126168                print "retrieve " + dates + " in dir " + c.inputdir
    127                 flexpart.retrieve(server, dates, times, c.inputdir)
    128                 day+=datechunk
    129 
    130 
    131 
    132     tobecleaned=glob.glob(c.inputdir+'/*__*.'+str(os.getppid())+'.*.grb')
     169
     170                try:
     171                    flexpart.retrieve(server, dates, times, c.inputdir)
     172                except IOError:
     173                    myerror(c, 'MARS request failed')
     174
     175                day += datechunk
     176
     177    # retrieving of normal data fields (non flux data), (maximum one month)
     178
     179    # remove old *__* files
     180    tobecleaned = glob.glob(c.inputdir + '/*__*.' +
     181                            str(os.getppid()) + '.*.grb')
    133182    for f in tobecleaned:
    134183        os.remove(f)
    135     day=start
    136     times=None
    137     while day<=end:
    138 
    139                # we need to retrieve MARS data for this period (maximum one month)
    140             flexpart = ECFlexpart(c)
    141             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")
     184    day = start
     185    times = None
     186    while day <= end:
     187            # retrieve MARS data for the whole period
     188            flexpart = ECFlexpart(c, fluxes=False)
     189            tmpday = day+datechunk-datetime.timedelta(days=1)
     190            if tmpday < end:
     191                dates = day.strftime("%Y%m%d") + "/to/" + \
     192                        tmpday.strftime("%Y%m%d")
    143193            else:
    144                 dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d")
     194                dates = day.strftime("%Y%m%d") + "/to/" + \
     195                        end.strftime("%Y%m%d")
     196
    145197            print "retrieve " + dates + " in dir " + c.inputdir
    146198
    147             flexpart.retrieve(server, dates, times, c.inputdir)
    148             day+=datechunk
    149 
     199            try:
     200                flexpart.retrieve(server, dates, times, c.inputdir)
     201            except IOError:
     202                myerror(c, 'MARS request failed')
     203
     204            day += datechunk
     205
     206    return
    150207
    151208if __name__ == "__main__":
    152209
    153     args,c=interpret_args_and_control()
    154     getMARSdata(args,c)
     210    args, c = interpret_args_and_control()
     211    getMARSdata(args, c)
    155212    normalexit(c)
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG