[e18f4b5] | 1 | #!/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 |
---|
| 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, \ |
---|
| 53 | Control,myerror,normalexit, interpret_args_and_control |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | def getMARSdata(args,c): |
---|
| 57 | |
---|
| 58 | if not os.path.exists(c.inputdir): |
---|
| 59 | os.makedirs(c.inputdir) |
---|
[a9c7f3e] | 60 | |
---|
| 61 | |
---|
| 62 | if c.request == 0: |
---|
| 63 | print("Retrieving EC data!") |
---|
| 64 | else: |
---|
| 65 | if c.request == 1: |
---|
| 66 | print("Printing mars requests!") |
---|
| 67 | elif c.request == 2: |
---|
| 68 | print("Retrieving EC data and printing mars request!") |
---|
| 69 | # first, write header with the mars parameter to file |
---|
| 70 | # create a dummy MarsRetrieval to get parameter |
---|
| 71 | MR = MARSretrieval(None, None) |
---|
| 72 | attrs = vars(MR).copy() |
---|
| 73 | del attrs['server'] |
---|
| 74 | del attrs['public'] |
---|
| 75 | marsfile = os.path.join(c.inputdir, 'mars_request.csv') |
---|
| 76 | with open(marsfile, 'w') as f: |
---|
| 77 | f.write('request_number' + ', ') |
---|
| 78 | f.write(', '.join(str(key) for key in sorted(attrs.iterkeys()))) |
---|
| 79 | f.write('\n') |
---|
| 80 | |
---|
[e18f4b5] | 81 | print "start date %s "%(c.start_date) |
---|
| 82 | print "end date %s "%(c.end_date) |
---|
| 83 | |
---|
| 84 | if ecapi: |
---|
| 85 | if int(c.public): |
---|
| 86 | server = ecmwfapi.ECMWFDataServer() |
---|
| 87 | else: |
---|
| 88 | server = ecmwfapi.ECMWFService("mars") |
---|
| 89 | else: |
---|
| 90 | server = False |
---|
| 91 | |
---|
| 92 | c.ecapi=ecapi |
---|
| 93 | print 'ecapi:',c.ecapi |
---|
| 94 | # Retrieve ERA interim data for running flexpart |
---|
| 95 | |
---|
| 96 | syear=int(c.start_date[:4]) |
---|
| 97 | smonth=int(c.start_date[4:6]) |
---|
| 98 | sday=int(c.start_date[6:]) |
---|
| 99 | start = datetime.date( year = syear, month = smonth, day = sday ) |
---|
| 100 | startm1=start- datetime.timedelta(days=1) |
---|
| 101 | if c.basetime=='00': |
---|
| 102 | start=startm1 |
---|
| 103 | eyear=int(c.end_date[:4]) |
---|
| 104 | emonth=int(c.end_date[4:6]) |
---|
| 105 | eday=int(c.end_date[6:]) |
---|
| 106 | |
---|
| 107 | end = datetime.date( year = eyear, month = emonth, day = eday ) |
---|
| 108 | if c.basetime=='00' or c.basetime=='12': |
---|
| 109 | endp1=end+ datetime.timedelta(days=1) |
---|
| 110 | else: |
---|
| 111 | endp1=end+ datetime.timedelta(days=2) |
---|
| 112 | |
---|
| 113 | datechunk=datetime.timedelta(days=int(c.date_chunk)) |
---|
[a9c7f3e] | 114 | if c.request == '0' or c.request == '2': |
---|
[51f9853] | 115 | print 'removing content of '+c.inputdir |
---|
| 116 | tobecleaned=glob.glob(c.inputdir+'/*_acc_*.'+str(os.getppid())+'.*.grb') |
---|
| 117 | for f in tobecleaned: |
---|
| 118 | os.remove(f) |
---|
[e18f4b5] | 119 | |
---|
| 120 | times=None |
---|
| 121 | if c.maxstep<=24: |
---|
| 122 | day=startm1 |
---|
| 123 | while day<endp1: |
---|
| 124 | # we need to retrieve MARS data for this period (maximum one month) |
---|
| 125 | flexpart = EIFlexpart(c,fluxes=True) |
---|
| 126 | if day+datechunk-datetime.timedelta(days=1)<endp1: |
---|
| 127 | dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") |
---|
| 128 | else: |
---|
| 129 | dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
| 130 | |
---|
| 131 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
| 132 | try: |
---|
[51f9853] | 133 | flexpart.retrieve(server, c.public, dates, c.request, times, c.inputdir) |
---|
[e18f4b5] | 134 | except IOError: |
---|
| 135 | myerror(c,'MARS request failed') |
---|
| 136 | |
---|
| 137 | day+=datechunk |
---|
| 138 | else: |
---|
| 139 | day=start |
---|
| 140 | while day<=end: |
---|
| 141 | # we need to retrieve MARS data for this period (maximum one month) |
---|
| 142 | flexpart = EIFlexpart(c,fluxes=True) |
---|
| 143 | if day+datechunk-datetime.timedelta(days=1)<end: |
---|
| 144 | dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") |
---|
| 145 | else: |
---|
| 146 | dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
| 147 | |
---|
| 148 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
[51f9853] | 149 | flexpart.retrieve(server, c.public, dates, c.request, times, c.inputdir) |
---|
[e18f4b5] | 150 | day+=datechunk |
---|
| 151 | |
---|
| 152 | |
---|
[51f9853] | 153 | if c.request == '0' or c.request == '2': |
---|
[a9c7f3e] | 154 | print 'removing content of '+c.inputdir |
---|
[51f9853] | 155 | tobecleaned=glob.glob(c.inputdir+'/*__*.'+str(os.getppid())+'.*.grb') |
---|
| 156 | for f in tobecleaned: |
---|
| 157 | os.remove(f) |
---|
[a9c7f3e] | 158 | |
---|
[e18f4b5] | 159 | day=start |
---|
| 160 | times=None |
---|
| 161 | while day<=end: |
---|
| 162 | |
---|
| 163 | # we need to retrieve MARS data for this period (maximum one month) |
---|
| 164 | flexpart = EIFlexpart(c) |
---|
| 165 | if day+datechunk-datetime.timedelta(days=1)<end: |
---|
| 166 | dates= day.strftime("%Y%m%d") + "/to/" + (day+datechunk-datetime.timedelta(days=1)).strftime("%Y%m%d") |
---|
| 167 | else: |
---|
| 168 | dates= day.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
| 169 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
| 170 | |
---|
[51f9853] | 171 | flexpart.retrieve(server, c.public, dates, c.request, times, c.inputdir) |
---|
[e18f4b5] | 172 | day+=datechunk |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | if __name__ == "__main__": |
---|
| 176 | |
---|
| 177 | args,c=interpret_args_and_control() |
---|
| 178 | getMARSdata(args,c) |
---|
| 179 | normalexit(c) |
---|