[5763793] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | # (C) Copyright 2014 UIO. |
---|
| 4 | # |
---|
| 5 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 6 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 7 | # |
---|
| 8 | # Creation: July 2014 - Anne Fouilloux - University of Oslo |
---|
| 9 | # |
---|
| 10 | # Get ERA-Interim GRIB fields from ECMWF to prepare WINDFIELDS for running |
---|
| 11 | # FLEXPART examples. |
---|
| 12 | # |
---|
| 13 | # To run this example, you need to self-register at ECMWF data server and |
---|
| 14 | # get an API key available from https://api.ecmwf.int/v1/key/ |
---|
| 15 | # |
---|
| 16 | # Inputs parameters: |
---|
| 17 | # start_date: starting date for retrieving ECMWF data |
---|
| 18 | # end_date : end date for retrieving ECMWF data; set to start_date if not specified |
---|
| 19 | # times : forecast or analysis time for MARS retrievals |
---|
| 20 | # levels : number of levels; set to 60 by default (for ERA-interim) |
---|
| 21 | # area : you can define your area with --area=north/west/south/east (values are in degrees) |
---|
| 22 | # outputdir : location where you want to store ERA-Interim outputs |
---|
| 23 | # |
---|
| 24 | from ecmwfapi import ECMWFDataServer |
---|
| 25 | import calendar |
---|
| 26 | import shutil |
---|
| 27 | import datetime |
---|
| 28 | import time |
---|
| 29 | import os |
---|
| 30 | from string import strip |
---|
| 31 | from optparse import OptionParser |
---|
| 32 | from FlexpartTools import MARSretrieval, EIFlexpart, daterange, mkdir_p, silentremove, months_between |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | def main(): |
---|
| 36 | usage = """usage: %prog --start_date=YYYYMMDD [--end_date=YYYYMMDD] [--times=tt1/tt2/tt3] [--levels=nlevels] |
---|
| 37 | [--area=north/west/south/east] [--outputdir=output_directory] """ |
---|
| 38 | parser = OptionParser(usage=usage) |
---|
| 39 | parser.add_option("--start_date", dest="start_date", |
---|
| 40 | help="start date YYYYMMDD", metavar="start_date" ) |
---|
| 41 | parser.add_option( "--end_date", dest="end_date", |
---|
| 42 | help="end_date YYYYMMDD", metavar="end_date") |
---|
| 43 | parser.add_option("--times", dest="times", |
---|
| 44 | default="00/03/06/09/12/15/18/21", help="times such as 00/12", metavar="times") |
---|
| 45 | parser.add_option("--levels", dest="levels", |
---|
| 46 | default="60",help="number of vertical levels", metavar="levels") |
---|
| 47 | parser.add_option("--area", dest="area", |
---|
| 48 | default="90.0/-179.0/-90.0/180.0",help="area defined as north/west/south/east with default 90.0/-179.0/-90.0/180.0", metavar="area") |
---|
| 49 | parser.add_option("--outputdir", dest="outputdir", |
---|
| 50 | help="root directory for storing output files", metavar="outputdir") |
---|
| 51 | (options, args) = parser.parse_args() |
---|
| 52 | |
---|
| 53 | if not options.start_date: |
---|
| 54 | parser.error("start date must be specified!") |
---|
| 55 | else: |
---|
| 56 | start_date=options.start_date |
---|
| 57 | |
---|
| 58 | if not options.end_date: |
---|
| 59 | end_date=start_date |
---|
| 60 | else: |
---|
| 61 | end_date=options.end_date |
---|
| 62 | |
---|
| 63 | if not options.outputdir: |
---|
| 64 | # if WORKDIR is defined, we will use it otherwise files |
---|
| 65 | # will be stored in the current directory |
---|
| 66 | outputdir=os.environ.get("WORKDIR",".") |
---|
| 67 | else: |
---|
| 68 | outputdir=options.outputdir |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | print "start date %s "%(start_date) |
---|
| 72 | print "end date %s "%(end_date) |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | server = ECMWFDataServer() |
---|
| 76 | |
---|
| 77 | # Retrieve ERA interim data for running flexpart |
---|
| 78 | |
---|
| 79 | syear=int(start_date[:4]) |
---|
| 80 | smonth=int(start_date[4:6]) |
---|
| 81 | sday=int(start_date[6:]) |
---|
| 82 | start = datetime.date( year = syear, month = smonth, day = sday ) |
---|
| 83 | eyear=int(end_date[:4]) |
---|
| 84 | emonth=int(end_date[4:6]) |
---|
| 85 | eday=int(end_date[6:]) |
---|
| 86 | |
---|
| 87 | end = datetime.date( year = eyear, month = emonth, day = eday ) |
---|
| 88 | |
---|
| 89 | current_ym = "" |
---|
| 90 | ir_date = start |
---|
| 91 | retrieve="no" |
---|
| 92 | for date in daterange( start, end ): |
---|
| 93 | # if new year & month then we create a new directory to store output files |
---|
[0e29ef4] | 94 | if date.strftime("%Y%m") != current_ym and current_ym != "": |
---|
[5763793] | 95 | retrieve="yes" |
---|
| 96 | |
---|
| 97 | if date == end: |
---|
| 98 | retrieve="yes" |
---|
| 99 | |
---|
| 100 | if retrieve == "yes": |
---|
| 101 | # we need to retrieve MARS data for this period (maximum one month) |
---|
| 102 | flexpart = EIFlexpart() |
---|
| 103 | dates= ir_date.strftime("%Y%m%d") + "/to/" + er_date.strftime("%Y%m%d") |
---|
[0e29ef4] | 104 | current_outputdir = outputdir + "/" + ir_date.strftime("%Y") + '/' + ir_date.strftime("%m") + '/' |
---|
| 105 | mkdir_p(current_outputdir) |
---|
| 106 | print "retrieve " + dates + " in dir " + current_outputdir |
---|
[5763793] | 107 | flexpart.retrieve(server, dates, options.times, options.area, options.levels, current_outputdir) |
---|
[0e29ef4] | 108 | ir_date = date |
---|
| 109 | retrieve="no" |
---|
| 110 | |
---|
| 111 | er_date = date |
---|
[5763793] | 112 | |
---|
| 113 | current_ym = date.strftime("%Y%m") |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | if __name__ == "__main__": |
---|
| 118 | main() |
---|