[d69b677] | 1 | #!/usr/bin/env python |
---|
[efdb01a] | 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
| 5 | # |
---|
| 6 | # @Date: October 2014 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # |
---|
| 10 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
[ff99eae] | 11 | # - moved the getEIdata program into a function "get_mars_data" |
---|
[991df6a] | 12 | # - moved the AgurmentParser into a seperate function |
---|
| 13 | # - adatpted the function for the use in flex_extract |
---|
[ff99eae] | 14 | # - renamed file to get_mars_data |
---|
[991df6a] | 15 | # |
---|
| 16 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 17 | # - applied PEP8 style guide |
---|
| 18 | # - added structured documentation |
---|
| 19 | # - minor changes in programming style for consistence |
---|
| 20 | # - added function main and moved function calls vom __main__ there |
---|
| 21 | # (necessary for better documentation with docstrings for later |
---|
| 22 | # online documentation) |
---|
| 23 | # - use of UIFiles class for file selection and deletion |
---|
| 24 | |
---|
| 25 | # |
---|
| 26 | # @License: |
---|
| 27 | # (C) Copyright 2014-2018. |
---|
| 28 | # |
---|
| 29 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 30 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 31 | # |
---|
| 32 | # @Program Functionality: |
---|
| 33 | # This program can be used as a module in the whole flex_extract process |
---|
| 34 | # or can be run by itself to just extract MARS data from ECMWF. To do so, |
---|
| 35 | # a couple of necessary parameters has to be passed with the program call. |
---|
| 36 | # See documentation for more details. |
---|
| 37 | # |
---|
| 38 | # @Program Content: |
---|
| 39 | # - main |
---|
[ff99eae] | 40 | # - get_mars_data |
---|
[991df6a] | 41 | # |
---|
| 42 | #******************************************************************************* |
---|
| 43 | |
---|
[efdb01a] | 44 | # ------------------------------------------------------------------------------ |
---|
| 45 | # MODULES |
---|
| 46 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 47 | import os |
---|
| 48 | import sys |
---|
| 49 | import datetime |
---|
| 50 | import inspect |
---|
[d69b677] | 51 | try: |
---|
[ff99eae] | 52 | ecapi = True |
---|
[d69b677] | 53 | import ecmwfapi |
---|
| 54 | except ImportError: |
---|
[ff99eae] | 55 | ecapi = False |
---|
| 56 | |
---|
| 57 | # software specific classes and modules from flex_extract |
---|
| 58 | from tools import my_error, normal_exit, interpret_args_and_control |
---|
| 59 | from EcFlexpart import EcFlexpart |
---|
| 60 | from UioFiles import UioFiles |
---|
[d69b677] | 61 | |
---|
[991df6a] | 62 | # add path to pythonpath so that python finds its buddies |
---|
[ff99eae] | 63 | LOCAL_PYTHON_PATH = os.path.dirname(os.path.abspath( |
---|
[991df6a] | 64 | inspect.getfile(inspect.currentframe()))) |
---|
[ff99eae] | 65 | if LOCAL_PYTHON_PATH not in sys.path: |
---|
| 66 | sys.path.append(LOCAL_PYTHON_PATH) |
---|
[64cf353] | 67 | |
---|
[efdb01a] | 68 | # ------------------------------------------------------------------------------ |
---|
| 69 | # FUNCTION |
---|
| 70 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 71 | def main(): |
---|
| 72 | ''' |
---|
| 73 | @Description: |
---|
[ff99eae] | 74 | If get_mars_data is called from command line, this function controls |
---|
[991df6a] | 75 | the program flow and calls the argumentparser function and |
---|
[ff99eae] | 76 | the get_mars_data function for retrieving EC data. |
---|
[991df6a] | 77 | |
---|
| 78 | @Input: |
---|
| 79 | <nothing> |
---|
| 80 | |
---|
| 81 | @Return: |
---|
| 82 | <nothing> |
---|
| 83 | ''' |
---|
| 84 | args, c = interpret_args_and_control() |
---|
[ff99eae] | 85 | get_mars_data(c) |
---|
| 86 | normal_exit(c) |
---|
[d69b677] | 87 | |
---|
[991df6a] | 88 | return |
---|
| 89 | |
---|
[ff99eae] | 90 | def get_mars_data(c): |
---|
[991df6a] | 91 | ''' |
---|
| 92 | @Description: |
---|
| 93 | Retrieves the EC data needed for a FLEXPART simulation. |
---|
| 94 | Start and end dates for retrieval period is set. Retrievals |
---|
| 95 | are divided into smaller periods if necessary and datechunk parameter |
---|
| 96 | is set. |
---|
| 97 | |
---|
| 98 | @Input: |
---|
| 99 | c: instance of class ControlFile |
---|
| 100 | Contains all the parameters of CONTROL file, which are e.g.: |
---|
| 101 | DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, |
---|
| 102 | STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, |
---|
| 103 | LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY, |
---|
| 104 | OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT, |
---|
| 105 | ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR, |
---|
| 106 | MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME |
---|
| 107 | DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
| 108 | |
---|
| 109 | For more information about format and content of the parameter |
---|
| 110 | see documentation. |
---|
| 111 | |
---|
| 112 | @Return: |
---|
| 113 | <nothing> |
---|
| 114 | ''' |
---|
[d69b677] | 115 | |
---|
| 116 | if not os.path.exists(c.inputdir): |
---|
| 117 | os.makedirs(c.inputdir) |
---|
[991df6a] | 118 | |
---|
[ff99eae] | 119 | print "Retrieving EC data!" |
---|
| 120 | print "start date %s " % (c.start_date) |
---|
| 121 | print "end date %s " % (c.end_date) |
---|
[d69b677] | 122 | |
---|
| 123 | if ecapi: |
---|
| 124 | server = ecmwfapi.ECMWFService("mars") |
---|
| 125 | else: |
---|
| 126 | server = False |
---|
| 127 | |
---|
[64cf353] | 128 | c.ecapi = ecapi |
---|
[991df6a] | 129 | print 'ecapi: ', c.ecapi |
---|
| 130 | |
---|
| 131 | # set start date of retrieval period |
---|
| 132 | start = datetime.date(year=int(c.start_date[:4]), |
---|
| 133 | month=int(c.start_date[4:6]), |
---|
| 134 | day=int(c.start_date[6:])) |
---|
[64cf353] | 135 | startm1 = start - datetime.timedelta(days=1) |
---|
| 136 | if c.basetime == '00': |
---|
| 137 | start = startm1 |
---|
[efdb01a] | 138 | |
---|
[991df6a] | 139 | # set end date of retrieval period |
---|
| 140 | end = datetime.date(year=int(c.end_date[:4]), |
---|
| 141 | month=int(c.end_date[4:6]), |
---|
| 142 | day=int(c.end_date[6:])) |
---|
[64cf353] | 143 | if c.basetime == '00' or c.basetime == '12': |
---|
| 144 | endp1 = end + datetime.timedelta(days=1) |
---|
[d69b677] | 145 | else: |
---|
[64cf353] | 146 | endp1 = end + datetime.timedelta(days=2) |
---|
| 147 | |
---|
[991df6a] | 148 | # set time period of one single retrieval |
---|
[64cf353] | 149 | datechunk = datetime.timedelta(days=int(c.date_chunk)) |
---|
[efdb01a] | 150 | |
---|
[991df6a] | 151 | # -------------- flux data ------------------------------------------------ |
---|
| 152 | print 'removing old flux content of ' + c.inputdir |
---|
[ff99eae] | 153 | tobecleaned = UioFiles('*_acc_*.' + str(os.getppid()) + '.*.grb') |
---|
| 154 | tobecleaned.list_files(c.inputdir) |
---|
| 155 | tobecleaned.delete_files() |
---|
[64cf353] | 156 | |
---|
[efdb01a] | 157 | # if forecast for maximum one day (upto 23h) are to be retrieved, |
---|
| 158 | # collect accumulation data (flux data) |
---|
| 159 | # with additional days in the beginning and at the end |
---|
| 160 | # (used for complete disaggregation of original period) |
---|
| 161 | if c.maxstep < 24: |
---|
| 162 | day = startm1 |
---|
| 163 | while day < endp1: |
---|
[991df6a] | 164 | # retrieve MARS data for the whole period |
---|
[ff99eae] | 165 | flexpart = EcFlexpart(c, fluxes=True) |
---|
[991df6a] | 166 | tmpday = day + datechunk - datetime.timedelta(days=1) |
---|
| 167 | if tmpday < endp1: |
---|
| 168 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 169 | tmpday.strftime("%Y%m%d") |
---|
| 170 | else: |
---|
| 171 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 172 | end.strftime("%Y%m%d") |
---|
[64cf353] | 173 | |
---|
[991df6a] | 174 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
[efdb01a] | 175 | |
---|
[991df6a] | 176 | try: |
---|
| 177 | flexpart.retrieve(server, dates, c.inputdir) |
---|
| 178 | except IOError: |
---|
[ff99eae] | 179 | my_error(c, 'MARS request failed') |
---|
[64cf353] | 180 | |
---|
[991df6a] | 181 | day += datechunk |
---|
[efdb01a] | 182 | |
---|
| 183 | # if forecast data longer than 24h are to be retrieved, |
---|
| 184 | # collect accumulation data (flux data) |
---|
| 185 | # with the exact start and end date |
---|
| 186 | # (disaggregation will be done for the |
---|
| 187 | # exact time period with boundary conditions) |
---|
[d69b677] | 188 | else: |
---|
[efdb01a] | 189 | day = start |
---|
| 190 | while day <= end: |
---|
[991df6a] | 191 | # retrieve MARS data for the whole period |
---|
[ff99eae] | 192 | flexpart = EcFlexpart(c, fluxes=True) |
---|
[991df6a] | 193 | tmpday = day + datechunk - datetime.timedelta(days=1) |
---|
| 194 | if tmpday < end: |
---|
| 195 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 196 | tmpday.trftime("%Y%m%d") |
---|
| 197 | else: |
---|
| 198 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 199 | end.strftime("%Y%m%d") |
---|
| 200 | |
---|
| 201 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
| 202 | |
---|
| 203 | try: |
---|
| 204 | flexpart.retrieve(server, dates, c.inputdir) |
---|
| 205 | except IOError: |
---|
[ff99eae] | 206 | my_error(c, 'MARS request failed') |
---|
[991df6a] | 207 | |
---|
| 208 | day += datechunk |
---|
| 209 | |
---|
| 210 | # -------------- non flux data -------------------------------------------- |
---|
| 211 | print 'removing old non flux content of ' + c.inputdir |
---|
[ff99eae] | 212 | tobecleaned = UioFiles('*__*.' + str(os.getppid()) + '.*.grb') |
---|
| 213 | tobecleaned.list_files(c.inputdir) |
---|
| 214 | tobecleaned.delete_files() |
---|
[991df6a] | 215 | |
---|
[efdb01a] | 216 | day = start |
---|
| 217 | while day <= end: |
---|
[ff99eae] | 218 | # retrieve all non flux MARS data for the whole period |
---|
| 219 | flexpart = EcFlexpart(c, fluxes=False) |
---|
| 220 | tmpday = day + datechunk - datetime.timedelta(days=1) |
---|
| 221 | if tmpday < end: |
---|
| 222 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 223 | tmpday.strftime("%Y%m%d") |
---|
| 224 | else: |
---|
| 225 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 226 | end.strftime("%Y%m%d") |
---|
[efdb01a] | 227 | |
---|
[ff99eae] | 228 | print "retrieve " + dates + " in dir " + c.inputdir |
---|
[64cf353] | 229 | |
---|
[ff99eae] | 230 | try: |
---|
| 231 | flexpart.retrieve(server, dates, c.inputdir) |
---|
| 232 | except IOError: |
---|
| 233 | my_error(c, 'MARS request failed') |
---|
[efdb01a] | 234 | |
---|
[ff99eae] | 235 | day += datechunk |
---|
[64cf353] | 236 | |
---|
[efdb01a] | 237 | return |
---|
[d69b677] | 238 | |
---|
| 239 | if __name__ == "__main__": |
---|
[991df6a] | 240 | main() |
---|