[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 |
---|
[54a8a01] | 24 | # |
---|
[991df6a] | 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 |
---|
[54a8a01] | 41 | # - do_retrievement |
---|
[991df6a] | 42 | # |
---|
| 43 | #******************************************************************************* |
---|
[96e1533] | 44 | """ToDo: Name of litte program |
---|
[991df6a] | 45 | |
---|
[96e1533] | 46 | ToDo: Add desccription |
---|
| 47 | |
---|
| 48 | ToDo: Add Conditions |
---|
| 49 | |
---|
| 50 | This script requires that `...` be installed within the Python |
---|
| 51 | environment you are running this script in. |
---|
| 52 | |
---|
| 53 | This file can also be imported as a module and contains the following |
---|
| 54 | functions: |
---|
| 55 | |
---|
| 56 | * get_mars_data - |
---|
| 57 | * do_retrievement - |
---|
| 58 | * main - the main function of the script |
---|
| 59 | """ |
---|
[efdb01a] | 60 | # ------------------------------------------------------------------------------ |
---|
| 61 | # MODULES |
---|
| 62 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 63 | import os |
---|
| 64 | import sys |
---|
| 65 | import inspect |
---|
[ca867de] | 66 | from datetime import datetime, timedelta |
---|
[ff99eae] | 67 | |
---|
| 68 | # software specific classes and modules from flex_extract |
---|
[70fee58] | 69 | sys.path.append(os.path.dirname(os.path.abspath( |
---|
| 70 | inspect.getfile(inspect.currentframe()))) + '/../') |
---|
[2fb99de] | 71 | import _config |
---|
[5bad6ec] | 72 | from tools import (my_error, normal_exit, get_cmdline_arguments, |
---|
| 73 | read_ecenv, make_dir) |
---|
[25b14be] | 74 | from classes.EcFlexpart import EcFlexpart |
---|
| 75 | from classes.UioFiles import UioFiles |
---|
[ca867de] | 76 | |
---|
| 77 | try: |
---|
| 78 | ecapi = True |
---|
| 79 | import ecmwfapi |
---|
| 80 | except ImportError: |
---|
| 81 | ecapi = False |
---|
[efdb01a] | 82 | # ------------------------------------------------------------------------------ |
---|
| 83 | # FUNCTION |
---|
| 84 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 85 | def main(): |
---|
[274f9ef] | 86 | '''Controls the program to get data out of mars. |
---|
| 87 | |
---|
| 88 | This is done if it is called directly from command line. |
---|
| 89 | Then it also takes program call arguments and control file input. |
---|
[295ff45] | 90 | |
---|
[274f9ef] | 91 | Parameters |
---|
| 92 | ---------- |
---|
[991df6a] | 93 | |
---|
[274f9ef] | 94 | Return |
---|
| 95 | ------ |
---|
[991df6a] | 96 | |
---|
| 97 | ''' |
---|
[54a8a01] | 98 | |
---|
| 99 | args = get_cmdline_arguments() |
---|
[4971f63] | 100 | c = ControlFile(args.controlfile) |
---|
[54a8a01] | 101 | |
---|
[2fb99de] | 102 | env_parameter = read_ecenv(_config.PATH_ECMWF_ENV) |
---|
[295ff45] | 103 | c.assign_args_to_control(args) |
---|
[54a8a01] | 104 | c.assign_envs_to_control(env_parameter) |
---|
[2fb99de] | 105 | c.check_conditions(args.queue) |
---|
[54a8a01] | 106 | |
---|
[ff99eae] | 107 | get_mars_data(c) |
---|
[96e1533] | 108 | normal_exit(c.mailops, c.queue, 'Done!') |
---|
[d69b677] | 109 | |
---|
[991df6a] | 110 | return |
---|
| 111 | |
---|
[ff99eae] | 112 | def get_mars_data(c): |
---|
[274f9ef] | 113 | '''Retrieves the EC data needed for a FLEXPART simulation. |
---|
| 114 | |
---|
| 115 | Start and end dates for retrieval period is set. Retrievals |
---|
| 116 | are divided into smaller periods if necessary and datechunk parameter |
---|
| 117 | is set. |
---|
| 118 | |
---|
| 119 | Parameters |
---|
| 120 | ---------- |
---|
| 121 | c : :obj:`ControlFile` |
---|
| 122 | Contains all the parameters of CONTROL file and |
---|
| 123 | command line. |
---|
| 124 | |
---|
| 125 | Return |
---|
| 126 | ------ |
---|
| 127 | |
---|
[991df6a] | 128 | ''' |
---|
[d69b677] | 129 | |
---|
| 130 | if not os.path.exists(c.inputdir): |
---|
[5bad6ec] | 131 | make_dir(c.inputdir) |
---|
[991df6a] | 132 | |
---|
[2fb99de] | 133 | if c.request == 0 or c.request == 2: |
---|
| 134 | print("Retrieving EC data!") |
---|
| 135 | elif c.request == 1: |
---|
| 136 | print("Printing mars requests!") |
---|
| 137 | |
---|
| 138 | print("start date %s " % (c.start_date)) |
---|
| 139 | print("end date %s " % (c.end_date)) |
---|
[d69b677] | 140 | |
---|
| 141 | if ecapi: |
---|
[5bad6ec] | 142 | if c.public: |
---|
| 143 | server = ecmwfapi.ECMWFDataServer() |
---|
| 144 | else: |
---|
| 145 | server = ecmwfapi.ECMWFService("mars") |
---|
[d69b677] | 146 | else: |
---|
| 147 | server = False |
---|
| 148 | |
---|
[64cf353] | 149 | c.ecapi = ecapi |
---|
[2fb99de] | 150 | print('Using ECMWF WebAPI: ' + str(c.ecapi)) |
---|
[991df6a] | 151 | |
---|
[54a8a01] | 152 | # basetime geht rückwärts |
---|
| 153 | |
---|
| 154 | # if basetime 00 |
---|
| 155 | # dann wird von 12 am vortag bis 00 am start tag geholt |
---|
| 156 | # aber ohne 12 selbst sondern 12 + step |
---|
| 157 | |
---|
| 158 | # if basetime 12 |
---|
| 159 | # dann wird von 00 + step bis 12 am start tag geholt |
---|
| 160 | |
---|
| 161 | # purer forecast wird vorwärts bestimmt. |
---|
| 162 | # purer forecast mode ist dann wenn größer 24 stunden |
---|
| 163 | # wie kann das noch festgestellt werden ???? |
---|
| 164 | # nur FC und steps mehr als 24 ? |
---|
| 165 | # die einzige problematik beim reinen forecast ist die benennung der files! |
---|
| 166 | # also sobald es Tagesüberschneidungen gibt |
---|
| 167 | # allerdings ist das relevant und ersichtlich an den NICHT FLUSS DATEN |
---|
| 168 | |
---|
[ca867de] | 169 | start = datetime.strptime(c.start_date, '%Y%m%d') |
---|
| 170 | end = datetime.strptime(c.end_date, '%Y%m%d') |
---|
| 171 | # time period for one single retrieval |
---|
| 172 | datechunk = timedelta(days=int(c.date_chunk)) |
---|
[efdb01a] | 173 | |
---|
[54a8a01] | 174 | if c.basetime == '00': |
---|
[ca867de] | 175 | start = start - timedelta(days=1) |
---|
| 176 | |
---|
| 177 | if c.maxstep <= 24: |
---|
| 178 | startm1 = start - timedelta(days=1) |
---|
[54a8a01] | 179 | |
---|
| 180 | if c.basetime == '00' or c.basetime == '12': |
---|
[ca867de] | 181 | # endp1 = end + timedelta(days=1) |
---|
[54a8a01] | 182 | endp1 = end |
---|
| 183 | else: |
---|
[ca867de] | 184 | # endp1 = end + timedelta(days=2) |
---|
| 185 | endp1 = end + timedelta(days=1) |
---|
[54a8a01] | 186 | |
---|
[991df6a] | 187 | # -------------- flux data ------------------------------------------------ |
---|
[2fb99de] | 188 | if c.request == 0 or c.request == 2: |
---|
| 189 | print('... removing old flux content of ' + c.inputdir) |
---|
| 190 | tobecleaned = UioFiles(c.inputdir, |
---|
| 191 | '*_acc_*.' + str(os.getppid()) + '.*.grb') |
---|
| 192 | tobecleaned.delete_files() |
---|
[64cf353] | 193 | |
---|
[54a8a01] | 194 | # if forecast for maximum one day (upto 24h) are to be retrieved, |
---|
[efdb01a] | 195 | # collect accumulation data (flux data) |
---|
| 196 | # with additional days in the beginning and at the end |
---|
| 197 | # (used for complete disaggregation of original period) |
---|
[54a8a01] | 198 | if c.maxstep <= 24: |
---|
| 199 | do_retrievement(c, server, startm1, endp1, datechunk, fluxes=True) |
---|
[efdb01a] | 200 | |
---|
| 201 | # if forecast data longer than 24h are to be retrieved, |
---|
| 202 | # collect accumulation data (flux data) |
---|
| 203 | # with the exact start and end date |
---|
| 204 | # (disaggregation will be done for the |
---|
| 205 | # exact time period with boundary conditions) |
---|
[d69b677] | 206 | else: |
---|
[54a8a01] | 207 | do_retrievement(c, server, start, end, datechunk, fluxes=True) |
---|
[991df6a] | 208 | |
---|
| 209 | # -------------- non flux data -------------------------------------------- |
---|
[2fb99de] | 210 | if c.request == 0 or c.request == 2: |
---|
| 211 | print('... removing old non flux content of ' + c.inputdir) |
---|
| 212 | tobecleaned = UioFiles(c.inputdir, |
---|
| 213 | '*__*.' + str(os.getppid()) + '.*.grb') |
---|
| 214 | tobecleaned.delete_files() |
---|
[991df6a] | 215 | |
---|
[54a8a01] | 216 | do_retrievement(c, server, start, end, datechunk, fluxes=False) |
---|
| 217 | |
---|
| 218 | return |
---|
| 219 | |
---|
| 220 | def do_retrievement(c, server, start, end, delta_t, fluxes=False): |
---|
[274f9ef] | 221 | '''Divides the complete retrieval period in smaller chunks and |
---|
| 222 | retrieves the data from MARS. |
---|
| 223 | |
---|
| 224 | Parameters |
---|
| 225 | ---------- |
---|
| 226 | c : :obj:`ControlFile` |
---|
| 227 | Contains all the parameters of CONTROL file and |
---|
| 228 | command line. |
---|
[54a8a01] | 229 | |
---|
[274f9ef] | 230 | server : :obj:`ECMWFService` |
---|
| 231 | The server connection to ECMWF. |
---|
[54a8a01] | 232 | |
---|
[274f9ef] | 233 | start : :obj:`datetime` |
---|
| 234 | The start date of the retrieval. |
---|
[54a8a01] | 235 | |
---|
[274f9ef] | 236 | end : :obj:`datetime` |
---|
| 237 | The end date of the retrieval. |
---|
[54a8a01] | 238 | |
---|
[274f9ef] | 239 | delta_t : :obj:`datetime` |
---|
| 240 | Delta_t + 1 is the maximal time period of a single |
---|
| 241 | retrieval. |
---|
[54a8a01] | 242 | |
---|
[274f9ef] | 243 | fluxes : :obj:`boolean`, optional |
---|
| 244 | Decides if the flux parameters are to be retrieved or |
---|
| 245 | the rest of the parameter list. |
---|
| 246 | Default value is False. |
---|
[54a8a01] | 247 | |
---|
[274f9ef] | 248 | Return |
---|
| 249 | ------ |
---|
[54a8a01] | 250 | |
---|
| 251 | ''' |
---|
| 252 | |
---|
| 253 | # since actual day also counts as one day, |
---|
| 254 | # we only need to add datechunk - 1 days to retrieval |
---|
| 255 | # for a period |
---|
[ca867de] | 256 | delta_t_m1 = delta_t - timedelta(days=1) |
---|
[54a8a01] | 257 | |
---|
[efdb01a] | 258 | day = start |
---|
| 259 | while day <= end: |
---|
[54a8a01] | 260 | flexpart = EcFlexpart(c, fluxes) |
---|
| 261 | tmpday = day + delta_t_m1 |
---|
[ff99eae] | 262 | if tmpday < end: |
---|
| 263 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 264 | tmpday.strftime("%Y%m%d") |
---|
| 265 | else: |
---|
| 266 | dates = day.strftime("%Y%m%d") + "/to/" + \ |
---|
| 267 | end.strftime("%Y%m%d") |
---|
[efdb01a] | 268 | |
---|
[2fb99de] | 269 | print("... retrieve " + dates + " in dir " + c.inputdir) |
---|
[64cf353] | 270 | |
---|
[ff99eae] | 271 | try: |
---|
[5bad6ec] | 272 | flexpart.retrieve(server, dates, c.public, c.request, c.inputdir) |
---|
[ff99eae] | 273 | except IOError: |
---|
[54a8a01] | 274 | my_error(c.mailfail, 'MARS request failed') |
---|
[efdb01a] | 275 | |
---|
[54a8a01] | 276 | day += delta_t |
---|
[64cf353] | 277 | |
---|
[efdb01a] | 278 | return |
---|
[d69b677] | 279 | |
---|
| 280 | if __name__ == "__main__": |
---|
[991df6a] | 281 | main() |
---|