[d69b677] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
[64cf353] | 4 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 5 | # |
---|
[d69b677] | 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 |
---|
[64cf353] | 9 | # Extension November 2015 - Leopold Haimberger - University of Vienna for: |
---|
[d69b677] | 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 |
---|
[64cf353] | 17 | # |
---|
[d69b677] | 18 | # |
---|
| 19 | # Further documentation may be obtained from www.flexpart.eu |
---|
[64cf353] | 20 | # |
---|
| 21 | # Requirements: |
---|
[d69b677] | 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 |
---|
[64cf353] | 28 | # |
---|
[d69b677] | 29 | |
---|
| 30 | #import socket |
---|
| 31 | |
---|
| 32 | #hostname=socket.gethostname() |
---|
| 33 | #ecapi= 'ecmwf' not in hostname |
---|
| 34 | try: |
---|
| 35 | ecapi=True |
---|
| 36 | import ecmwfapi |
---|
| 37 | except ImportError: |
---|
| 38 | ecapi=False |
---|
| 39 | |
---|
| 40 | import calendar |
---|
| 41 | import shutil |
---|
| 42 | import datetime |
---|
| 43 | import time |
---|
| 44 | import os,glob,sys,inspect |
---|
| 45 | #from string import strip |
---|
| 46 | from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter |
---|
| 47 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
| 48 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
| 49 | if localpythonpath not in sys.path: |
---|
| 50 | sys.path.append(localpythonpath) |
---|
| 51 | |
---|
| 52 | from FlexpartTools import MARSretrieval, EIFlexpart, silentremove, \ |
---|
[64cf353] | 53 | Control, myerror, normalexit, \ |
---|
| 54 | interpret_args_and_control |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | def getMARSdata(args, c): |
---|
[d69b677] | 58 | |
---|
| 59 | |
---|
| 60 | if not os.path.exists(c.inputdir): |
---|
| 61 | os.makedirs(c.inputdir) |
---|
[64cf353] | 62 | print("start date %s " % (c.start_date)) |
---|
| 63 | print("end date %s " % (c.end_date)) |
---|
[d69b677] | 64 | |
---|
| 65 | if ecapi: |
---|
| 66 | server = ecmwfapi.ECMWFService("mars") |
---|
| 67 | else: |
---|
| 68 | server = False |
---|
| 69 | |
---|
[64cf353] | 70 | c.ecapi = ecapi |
---|
| 71 | print 'ecapi:', c.ecapi |
---|
[d69b677] | 72 | |
---|
[64cf353] | 73 | # 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) |
---|
[d69b677] | 89 | else: |
---|
[64cf353] | 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') |
---|
[d69b677] | 95 | for f in tobecleaned: |
---|
| 96 | os.remove(f) |
---|
[64cf353] | 97 | |
---|
[d69b677] | 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 = EIFlexpart(c,fluxes=True) |
---|
| 104 | if day+datechunk-datetime.timedelta(days=1)<endp1: |
---|
[64cf353] | 105 | dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") |
---|
[d69b677] | 106 | else: |
---|
[64cf353] | 107 | dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
| 108 | |
---|
[d69b677] | 109 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
| 110 | try: |
---|
| 111 | flexpart.retrieve(server, dates, times, c.inputdir) |
---|
| 112 | except IOError: |
---|
| 113 | myerror(c,'MARS request failed') |
---|
[64cf353] | 114 | |
---|
[d69b677] | 115 | day+=datechunk |
---|
| 116 | else: |
---|
| 117 | day=start |
---|
| 118 | while day<=end: |
---|
| 119 | # we need to retrieve MARS data for this period (maximum one month) |
---|
| 120 | flexpart = EIFlexpart(c,fluxes=True) |
---|
| 121 | if day+datechunk-datetime.timedelta(days=1)<end: |
---|
[64cf353] | 122 | dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") |
---|
[d69b677] | 123 | else: |
---|
[64cf353] | 124 | dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
| 125 | |
---|
[d69b677] | 126 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
| 127 | flexpart.retrieve(server, dates, times, c.inputdir) |
---|
| 128 | day+=datechunk |
---|
[64cf353] | 129 | |
---|
[d69b677] | 130 | |
---|
| 131 | |
---|
| 132 | tobecleaned=glob.glob(c.inputdir+'/*__*.'+str(os.getppid())+'.*.grb') |
---|
| 133 | for f in tobecleaned: |
---|
| 134 | os.remove(f) |
---|
| 135 | day=start |
---|
| 136 | times=None |
---|
| 137 | while day<=end: |
---|
[64cf353] | 138 | |
---|
[d69b677] | 139 | # we need to retrieve MARS data for this period (maximum one month) |
---|
| 140 | flexpart = EIFlexpart(c) |
---|
| 141 | if day+datechunk-datetime.timedelta(days=1)<end: |
---|
[64cf353] | 142 | dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") |
---|
[d69b677] | 143 | else: |
---|
[64cf353] | 144 | dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
[d69b677] | 145 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
[64cf353] | 146 | |
---|
[d69b677] | 147 | flexpart.retrieve(server, dates, times, c.inputdir) |
---|
| 148 | day+=datechunk |
---|
[64cf353] | 149 | |
---|
[d69b677] | 150 | |
---|
| 151 | if __name__ == "__main__": |
---|
[64cf353] | 152 | |
---|
[d69b677] | 153 | args,c=interpret_args_and_control() |
---|
| 154 | getMARSdata(args,c) |
---|
| 155 | normalexit(c) |
---|