[8463d78] | 1 | #!/usr/bin/env python3 |
---|
[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 | # - optimized display_info |
---|
| 12 | # - optimized data_retrieve and seperate between python and shell |
---|
[991df6a] | 13 | # script call |
---|
| 14 | # |
---|
| 15 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 16 | # - applied PEP8 style guide |
---|
| 17 | # - added documentation |
---|
| 18 | # - applied some minor modifications in programming style/structure |
---|
[6f951ca] | 19 | # - added writing of mars request attributes to a csv file |
---|
[991df6a] | 20 | # |
---|
| 21 | # @License: |
---|
[026b359] | 22 | # (C) Copyright 2014-2020. |
---|
[6f951ca] | 23 | # Anne Philipp, Leopold Haimberger |
---|
[991df6a] | 24 | # |
---|
[44174de] | 25 | # SPDX-License-Identifier: CC-BY-4.0 |
---|
| 26 | # |
---|
[6f951ca] | 27 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 28 | # International License. To view a copy of this license, visit |
---|
| 29 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 30 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[991df6a] | 31 | #******************************************************************************* |
---|
| 32 | |
---|
| 33 | # ------------------------------------------------------------------------------ |
---|
[efdb01a] | 34 | # MODULES |
---|
| 35 | # ------------------------------------------------------------------------------ |
---|
[b957ef7] | 36 | from __future__ import print_function |
---|
| 37 | |
---|
[efdb01a] | 38 | import os |
---|
[ca867de] | 39 | import sys |
---|
| 40 | import subprocess |
---|
[f20af73] | 41 | import traceback |
---|
[efdb01a] | 42 | |
---|
[ca867de] | 43 | # software specific classes and modules from flex_extract |
---|
[0f89116] | 44 | #pylint: disable=wrong-import-position |
---|
[ca867de] | 45 | sys.path.append('../') |
---|
[2fb99de] | 46 | import _config |
---|
[0f89116] | 47 | #pylint: disable=invalid-name |
---|
[f20af73] | 48 | try: |
---|
| 49 | ec_api = True |
---|
| 50 | import ecmwfapi |
---|
| 51 | except ImportError: |
---|
| 52 | ec_api = False |
---|
| 53 | |
---|
| 54 | try: |
---|
| 55 | cds_api = True |
---|
| 56 | import cdsapi |
---|
| 57 | except ImportError: |
---|
| 58 | cds_api = False |
---|
[0f89116] | 59 | #pylint: enable=invalid-name |
---|
| 60 | #pylint: enable=wrong-import-position |
---|
[efdb01a] | 61 | # ------------------------------------------------------------------------------ |
---|
| 62 | # CLASS |
---|
| 63 | # ------------------------------------------------------------------------------ |
---|
[ff99eae] | 64 | class MarsRetrieval(object): |
---|
[6f951ca] | 65 | '''Specific syntax and content for submission of MARS retrievals. |
---|
| 66 | |
---|
| 67 | A MARS revtrieval has a specific syntax with a selection of keywords and |
---|
| 68 | their corresponding values. This class provides the necessary functions |
---|
| 69 | by displaying the selected parameters and their values and the actual |
---|
| 70 | retrievement of the data through a mars request or a Python web api |
---|
| 71 | interface. The initialization already expects all the keyword values. |
---|
[efdb01a] | 72 | |
---|
| 73 | A description of MARS keywords/arguments and examples of their |
---|
| 74 | values can be found here: |
---|
| 75 | https://software.ecmwf.int/wiki/display/UDOC/\ |
---|
| 76 | Identification+keywords#Identificationkeywords-class |
---|
[6f951ca] | 77 | |
---|
| 78 | Attributes |
---|
| 79 | ---------- |
---|
| 80 | server : ECMWFService or ECMWFDataServer |
---|
| 81 | This is the connection to the ECMWF data servers. |
---|
| 82 | |
---|
| 83 | public : int |
---|
| 84 | Decides which Web API Server version is used. |
---|
| 85 | |
---|
| 86 | marsclass : str, optional |
---|
| 87 | Characterisation of dataset. |
---|
| 88 | |
---|
| 89 | dataset : str, optional |
---|
| 90 | For public datasets there is the specific naming and parameter |
---|
| 91 | dataset which has to be used to characterize the type of |
---|
| 92 | data. |
---|
| 93 | |
---|
| 94 | type : str, optional |
---|
| 95 | Determines the type of fields to be retrieved. |
---|
| 96 | |
---|
| 97 | levtype : str, optional |
---|
| 98 | Denotes type of level. |
---|
| 99 | |
---|
| 100 | levelist : str, optional |
---|
| 101 | Specifies the required levels. |
---|
| 102 | |
---|
| 103 | repres : str, optional |
---|
| 104 | Selects the representation of the archived data. |
---|
| 105 | |
---|
| 106 | date : str, optional |
---|
| 107 | Specifies the Analysis date, the Forecast base date or |
---|
| 108 | Observations date. |
---|
| 109 | |
---|
| 110 | resol : str, optional |
---|
| 111 | Specifies the desired triangular truncation of retrieved data, |
---|
| 112 | before carrying out any other selected post-processing. |
---|
| 113 | |
---|
| 114 | stream : str, optional |
---|
| 115 | Identifies the forecasting system used to generate the data. |
---|
| 116 | |
---|
| 117 | area : str, optional |
---|
| 118 | Specifies the desired sub-area of data to be extracted. |
---|
| 119 | |
---|
| 120 | time : str, optional |
---|
| 121 | Specifies the time of the data in hours and minutes. |
---|
| 122 | |
---|
| 123 | step : str, optional |
---|
| 124 | Specifies the forecast time step from forecast base time. |
---|
| 125 | |
---|
| 126 | expver : str, optional |
---|
| 127 | The version of the dataset. |
---|
| 128 | |
---|
| 129 | number : str, optional |
---|
| 130 | Selects the member in ensemble forecast run. |
---|
| 131 | |
---|
| 132 | accuracy : str, optional |
---|
| 133 | Specifies the number of bits per value to be used in the |
---|
| 134 | generated GRIB coded fields. |
---|
| 135 | |
---|
| 136 | grid : str, optional |
---|
| 137 | Specifies the output grid which can be either a Gaussian grid |
---|
| 138 | or a Latitude/Longitude grid. |
---|
| 139 | |
---|
| 140 | gaussian : str, optional |
---|
| 141 | This parameter is deprecated and should no longer be used. |
---|
| 142 | Specifies the desired type of Gaussian grid for the output. |
---|
| 143 | |
---|
| 144 | target : str, optional |
---|
| 145 | Specifies a file into which data is to be written after |
---|
| 146 | retrieval or manipulation. |
---|
| 147 | |
---|
| 148 | param : str, optional |
---|
| 149 | Specifies the meteorological parameter. |
---|
[efdb01a] | 150 | ''' |
---|
| 151 | |
---|
[f20af73] | 152 | def __init__(self, server, public, marsclass="EA", dataset="", type="", |
---|
[5bad6ec] | 153 | levtype="", levelist="", repres="", date="", resol="", |
---|
| 154 | stream="", area="", time="", step="", expver="1", |
---|
| 155 | number="", accuracy="", grid="", gaussian="", target="", |
---|
[ff99eae] | 156 | param=""): |
---|
[274f9ef] | 157 | '''Initialises the instance of the MarsRetrieval class and |
---|
| 158 | defines and assigns a set of the necessary retrieval parameters |
---|
| 159 | for the FLEXPART input data. |
---|
| 160 | A description of MARS keywords/arguments, their dependencies |
---|
| 161 | on each other and examples of their values can be found here: |
---|
| 162 | |
---|
| 163 | https://software.ecmwf.int/wiki/display/UDOC/MARS+keywords |
---|
| 164 | |
---|
| 165 | Parameters |
---|
| 166 | ---------- |
---|
[6f951ca] | 167 | server : ECMWFService or ECMWFDataServer |
---|
[274f9ef] | 168 | This is the connection to the ECMWF data servers. |
---|
| 169 | It is needed for the pythonic access of ECMWF data. |
---|
| 170 | |
---|
[6f951ca] | 171 | public : int |
---|
[274f9ef] | 172 | Decides which Web API version is used: |
---|
| 173 | 0: member-state users and full archive access |
---|
| 174 | 1: public access and limited access to the public server and |
---|
| 175 | datasets. Needs the parameter dataset. |
---|
| 176 | Default is "0" and for member-state users. |
---|
| 177 | |
---|
[6f951ca] | 178 | marsclass : str, optional |
---|
[274f9ef] | 179 | Characterisation of dataset. E.g. EI (ERA-Interim), |
---|
[f20af73] | 180 | E4 (ERA40), OD (Operational archive), EA (ERA5). |
---|
| 181 | Default is the ERA5 dataset "EA". |
---|
[274f9ef] | 182 | |
---|
[6f951ca] | 183 | dataset : str, optional |
---|
[274f9ef] | 184 | For public datasets there is the specific naming and parameter |
---|
| 185 | dataset which has to be used to characterize the type of |
---|
| 186 | data. Usually there is less data available, either in times, |
---|
| 187 | domain or parameter. |
---|
| 188 | Default is an empty string. |
---|
| 189 | |
---|
[6f951ca] | 190 | type : str, optional |
---|
[274f9ef] | 191 | Determines the type of fields to be retrieved. |
---|
| 192 | Selects between observations, images or fields. |
---|
| 193 | Examples for fields: Analysis (an), Forecast (fc), |
---|
| 194 | Perturbed Forecast (pf), Control Forecast (cf) and so on. |
---|
| 195 | Default is an empty string. |
---|
| 196 | |
---|
[6f951ca] | 197 | levtype : str, optional |
---|
[274f9ef] | 198 | Denotes type of level. Has a direct implication on valid |
---|
| 199 | levelist values! |
---|
| 200 | E.g. model level (ml), pressure level (pl), surface (sfc), |
---|
| 201 | potential vorticity (pv), potential temperature (pt) |
---|
| 202 | and depth (dp). |
---|
| 203 | Default is an empty string. |
---|
| 204 | |
---|
[6f951ca] | 205 | levelist : str, optional |
---|
[274f9ef] | 206 | Specifies the required levels. It has to have a valid |
---|
| 207 | correspondence to the selected levtype. |
---|
| 208 | Examples: model level: 1/to/137, pressure levels: 500/to/1000 |
---|
| 209 | Default is an empty string. |
---|
| 210 | |
---|
[6f951ca] | 211 | repres : str, optional |
---|
[274f9ef] | 212 | Selects the representation of the archived data. |
---|
| 213 | E.g. sh - spherical harmonics, gg - Gaussian grid, |
---|
| 214 | ll - latitude/longitude, ... |
---|
| 215 | Default is an empty string. |
---|
| 216 | |
---|
[6f951ca] | 217 | date : str, optional |
---|
[274f9ef] | 218 | Specifies the Analysis date, the Forecast base date or |
---|
| 219 | Observations date. Valid formats are: |
---|
| 220 | Absolute as YYYY-MM-DD or YYYYMMDD. |
---|
| 221 | Default is an empty string. |
---|
| 222 | |
---|
[6f951ca] | 223 | resol : str, optional |
---|
[274f9ef] | 224 | Specifies the desired triangular truncation of retrieved data, |
---|
| 225 | before carrying out any other selected post-processing. |
---|
| 226 | The default is automatic truncation (auto), by which the lowest |
---|
| 227 | resolution compatible with the value specified in grid is |
---|
| 228 | automatically selected for the retrieval. |
---|
| 229 | Users wanting to perform post-processing from full spectral |
---|
| 230 | resolution should specify Archived Value (av). |
---|
| 231 | The following are examples of existing resolutions found in |
---|
| 232 | the archive: 63, 106, 159, 213, 255, 319, 399, 511, 799 or 1279. |
---|
| 233 | This keyword has no meaning/effect if the archived data is |
---|
| 234 | not in spherical harmonics representation. |
---|
| 235 | The best selection can be found here: |
---|
| 236 | https://software.ecmwf.int/wiki/display/UDOC/\ |
---|
| 237 | Retrieve#Retrieve-Truncationbeforeinterpolation |
---|
| 238 | Default is an empty string. |
---|
| 239 | |
---|
[6f951ca] | 240 | stream : str, optional |
---|
[274f9ef] | 241 | Identifies the forecasting system used to generate the data. |
---|
| 242 | E.g. oper (Atmospheric model), enfo (Ensemble forecats), ... |
---|
| 243 | Default is an empty string. |
---|
| 244 | |
---|
[6f951ca] | 245 | area : str, optional |
---|
[274f9ef] | 246 | Specifies the desired sub-area of data to be extracted. |
---|
| 247 | Areas can be defined to wrap around the globe. |
---|
| 248 | |
---|
| 249 | Latitude values must be given as signed numbers, with: |
---|
| 250 | north latitudes (i.e. north of the equator) |
---|
| 251 | being positive (e.g: 40.5) |
---|
| 252 | south latitutes (i.e. south of the equator) |
---|
| 253 | being negative (e.g: -50.5) |
---|
| 254 | Longtitude values must be given as signed numbers, with: |
---|
| 255 | east longitudes (i.e. east of the 0 degree meridian) |
---|
| 256 | being positive (e.g: 35.0) |
---|
| 257 | west longitudes (i.e. west of the 0 degree meridian) |
---|
| 258 | being negative (e.g: -20.5) |
---|
| 259 | |
---|
| 260 | E.g.: North/West/South/East |
---|
| 261 | Default is an empty string. |
---|
| 262 | |
---|
[6f951ca] | 263 | time : str, optional |
---|
[274f9ef] | 264 | Specifies the time of the data in hours and minutes. |
---|
| 265 | Valid values depend on the type of data: Analysis time, |
---|
| 266 | Forecast base time or First guess verification time |
---|
| 267 | (all usually at synoptic hours: 00, 06, 12 and 18 ). |
---|
| 268 | Observation time (any combination in hours and minutes is valid, |
---|
| 269 | subject to data availability in the archive). |
---|
| 270 | The syntax is HHMM or HH:MM. If MM is omitted it defaults to 00. |
---|
| 271 | Default is an empty string. |
---|
| 272 | |
---|
[6f951ca] | 273 | step : str, optional |
---|
[274f9ef] | 274 | Specifies the forecast time step from forecast base time. |
---|
| 275 | Valid values are hours (HH) from forecast base time. It also |
---|
| 276 | specifies the length of the forecast which verifies at |
---|
| 277 | First Guess time. |
---|
| 278 | E.g. 1/3/6-hourly |
---|
| 279 | Default is an empty string. |
---|
| 280 | |
---|
[6f951ca] | 281 | expver : str, optional |
---|
[274f9ef] | 282 | The version of the dataset. Each experiment is assigned a |
---|
| 283 | unique code (version). Production data is assigned 1 or 2, |
---|
| 284 | and experimental data in Operations 11, 12 ,... |
---|
| 285 | Research or Member State's experiments have a four letter |
---|
| 286 | experiment identifier. |
---|
| 287 | Default is "1". |
---|
| 288 | |
---|
[6f951ca] | 289 | number : str, optional |
---|
[274f9ef] | 290 | Selects the member in ensemble forecast run. (Only then it |
---|
| 291 | is necessary.) It has a different meaning depending on |
---|
| 292 | the type of data. |
---|
| 293 | E.g. Perturbed Forecasts: specifies the Ensemble forecast member |
---|
| 294 | Default is an empty string. |
---|
| 295 | |
---|
[6f951ca] | 296 | accuracy : str, optional |
---|
[274f9ef] | 297 | Specifies the number of bits per value to be used in the |
---|
| 298 | generated GRIB coded fields. |
---|
| 299 | A positive integer may be given to specify the preferred number |
---|
| 300 | of bits per packed value. This must not be greater than the |
---|
| 301 | number of bits normally used for a Fortran integer on the |
---|
| 302 | processor handling the request (typically 32 or 64 bit). |
---|
| 303 | Within a compute request the accuracy of the original fields |
---|
| 304 | can be passed to the result field by specifying accuracy=av. |
---|
| 305 | Default is an empty string. |
---|
| 306 | |
---|
[6f951ca] | 307 | grid : str, optional |
---|
[274f9ef] | 308 | Specifies the output grid which can be either a Gaussian grid |
---|
| 309 | or a Latitude/Longitude grid. MARS requests specifying |
---|
| 310 | grid=av will return the archived model grid. |
---|
| 311 | |
---|
| 312 | Lat/Lon grid: The grid spacing needs to be an integer |
---|
| 313 | fraction of 90 degrees e.g. grid = 0.5/0.5 |
---|
| 314 | |
---|
| 315 | Gaussian grid: specified by a letter denoting the type of |
---|
| 316 | Gaussian grid followed by an integer (the grid number) |
---|
| 317 | representing the number of lines between the Pole and Equator, |
---|
| 318 | e.g. |
---|
| 319 | grid = F160 - full (or regular) Gaussian grid with |
---|
| 320 | 160 latitude lines between the pole and equator |
---|
| 321 | grid = N320 - ECMWF original reduced Gaussian grid with |
---|
| 322 | 320 latitude lines between the pole and equator, |
---|
| 323 | see Reduced Gaussian Grids for grid numbers used at ECMWF |
---|
| 324 | grid = O640 - ECMWF octahedral (reduced) Gaussian grid with |
---|
| 325 | 640 latitude lines between the pole and equator |
---|
| 326 | Default is an empty string. |
---|
| 327 | |
---|
[6f951ca] | 328 | gaussian : str, optional |
---|
[274f9ef] | 329 | This parameter is deprecated and should no longer be used. |
---|
| 330 | Specifies the desired type of Gaussian grid for the output. |
---|
| 331 | Valid Gaussian grids are quasi-regular (reduced) or regular. |
---|
| 332 | Keyword gaussian can only be specified together with |
---|
| 333 | keyword grid. Gaussian without grid has no effect. |
---|
| 334 | Default is an empty string. |
---|
| 335 | |
---|
[6f951ca] | 336 | target : str, optional |
---|
[274f9ef] | 337 | Specifies a file into which data is to be written after |
---|
| 338 | retrieval or manipulation. Path names should always be |
---|
| 339 | enclosed in double quotes. The MARS client supports automatic |
---|
| 340 | generation of multiple target files using MARS keywords |
---|
| 341 | enclosed in square brackets [ ]. If the environment variable |
---|
| 342 | MARS_MULTITARGET_STRICT_FORMAT is set to 1 before calling mars, |
---|
| 343 | the keyword values will be used in the filename as shown by |
---|
| 344 | the ecCodes GRIB tool grib_ls -m, e.g. with |
---|
| 345 | MARS_MULTITARGET_STRICT_FORMAT set to 1 the keywords time, |
---|
| 346 | expver and param will be formatted as 0600, 0001 and 129.128 |
---|
| 347 | rather than 600, 1 and 129. |
---|
| 348 | Default is an empty string. |
---|
| 349 | |
---|
[6f951ca] | 350 | param : str, optional |
---|
[274f9ef] | 351 | Specifies the meteorological parameter. |
---|
| 352 | The list of meteorological parameters in MARS is extensive. |
---|
| 353 | Their availability is directly related to their meteorological |
---|
| 354 | meaning and, therefore, the rest of directives specified |
---|
| 355 | in the MARS request. |
---|
| 356 | Meteorological parameters can be specified by their |
---|
| 357 | GRIB code (param=130), their mnemonic (param=t) or |
---|
| 358 | full name (param=temperature). |
---|
| 359 | The list of parameter should be seperated by a "/"-sign. |
---|
| 360 | E.g. 130/131/133 |
---|
| 361 | Default is an empty string. |
---|
| 362 | |
---|
| 363 | Return |
---|
| 364 | ------ |
---|
| 365 | |
---|
[efdb01a] | 366 | ''' |
---|
| 367 | |
---|
| 368 | self.server = server |
---|
[5bad6ec] | 369 | self.public = public |
---|
[efdb01a] | 370 | self.marsclass = marsclass |
---|
[5bad6ec] | 371 | self.dataset = dataset |
---|
[54a8a01] | 372 | self.type = type |
---|
[efdb01a] | 373 | self.levtype = levtype |
---|
| 374 | self.levelist = levelist |
---|
| 375 | self.repres = repres |
---|
| 376 | self.date = date |
---|
| 377 | self.resol = resol |
---|
| 378 | self.stream = stream |
---|
| 379 | self.area = area |
---|
| 380 | self.time = time |
---|
| 381 | self.step = step |
---|
| 382 | self.expver = expver |
---|
| 383 | self.number = number |
---|
| 384 | self.accuracy = accuracy |
---|
| 385 | self.grid = grid |
---|
| 386 | self.gaussian = gaussian |
---|
| 387 | self.target = target |
---|
| 388 | self.param = param |
---|
| 389 | |
---|
| 390 | return |
---|
| 391 | |
---|
| 392 | |
---|
[ff99eae] | 393 | def display_info(self): |
---|
[274f9ef] | 394 | '''Prints all class attributes and their values to the |
---|
| 395 | standard output. |
---|
| 396 | |
---|
| 397 | Parameters |
---|
| 398 | ---------- |
---|
[efdb01a] | 399 | |
---|
[274f9ef] | 400 | Return |
---|
| 401 | ------ |
---|
[efdb01a] | 402 | |
---|
| 403 | ''' |
---|
| 404 | # Get all class attributes and their values as a dictionary |
---|
[5bad6ec] | 405 | attrs = vars(self).copy() |
---|
[efdb01a] | 406 | |
---|
| 407 | # iterate through all attributes and print them |
---|
| 408 | # with their corresponding values |
---|
| 409 | for item in attrs.items(): |
---|
[5bad6ec] | 410 | if item[0] in ['server', 'public']: |
---|
[efdb01a] | 411 | pass |
---|
| 412 | else: |
---|
[2fb99de] | 413 | print(item[0] + ': ' + str(item[1])) |
---|
| 414 | |
---|
| 415 | return |
---|
| 416 | |
---|
| 417 | |
---|
[295ff45] | 418 | def print_infodata_csv(self, inputdir, request_number): |
---|
[274f9ef] | 419 | '''Write all request parameter in alpabetical order into a "csv" file. |
---|
[295ff45] | 420 | |
---|
[274f9ef] | 421 | Parameters |
---|
| 422 | ---------- |
---|
[6f951ca] | 423 | inputdir : str |
---|
[274f9ef] | 424 | The path where all data from the retrievals are stored. |
---|
[295ff45] | 425 | |
---|
[6f951ca] | 426 | request_number : int |
---|
[274f9ef] | 427 | Number of mars requests for flux and non-flux data. |
---|
[295ff45] | 428 | |
---|
[274f9ef] | 429 | Return |
---|
| 430 | ------ |
---|
[295ff45] | 431 | |
---|
| 432 | ''' |
---|
| 433 | |
---|
| 434 | # Get all class attributes and their values as a dictionary |
---|
[5bad6ec] | 435 | attrs = vars(self).copy() |
---|
[295ff45] | 436 | del attrs['server'] |
---|
[5bad6ec] | 437 | del attrs['public'] |
---|
[295ff45] | 438 | |
---|
| 439 | # open a file to store all requests to |
---|
| 440 | with open(os.path.join(inputdir, |
---|
[70fee58] | 441 | _config.FILE_MARS_REQUESTS), 'a') as f: |
---|
[295ff45] | 442 | f.write(str(request_number) + ', ') |
---|
| 443 | f.write(', '.join(str(attrs[key]) |
---|
[79729d5] | 444 | for key in sorted(attrs.keys()))) |
---|
[295ff45] | 445 | f.write('\n') |
---|
| 446 | |
---|
| 447 | return |
---|
[0a75335] | 448 | |
---|
| 449 | def _convert_to_cdsera5_sfc_request(self, attrs): |
---|
| 450 | ''' |
---|
| 451 | The keywords and values for the single level download |
---|
| 452 | with CDS API is different from MARS. This function |
---|
| 453 | converts the old request keywords to the new ones. |
---|
| 454 | |
---|
| 455 | Example request for single level downloads in CDS API |
---|
| 456 | |
---|
| 457 | retrieve( |
---|
| 458 | 'reanalysis-era5-single-levels', |
---|
| 459 | { |
---|
| 460 | 'product_type': 'reanalysis', |
---|
| 461 | 'variable': 'total_precipitation', |
---|
| 462 | 'year': '2019', |
---|
| 463 | 'month': '01', |
---|
| 464 | 'day': '01', |
---|
| 465 | 'time': '00:00', |
---|
| 466 | 'format': 'grib', |
---|
| 467 | 'grid':[1.0, 1.0], |
---|
| 468 | 'area': [ |
---|
| 469 | 45, 0, 43, |
---|
| 470 | 12, |
---|
| 471 | ], |
---|
| 472 | }, |
---|
| 473 | 'download.grib') |
---|
| 474 | |
---|
| 475 | Parameters |
---|
| 476 | ---------- |
---|
| 477 | attrs : dict |
---|
| 478 | Dictionary of the mars request parameters. |
---|
| 479 | |
---|
| 480 | Return |
---|
| 481 | ------ |
---|
| 482 | |
---|
| 483 | ''' |
---|
| 484 | from datetime import datetime, timedelta |
---|
| 485 | newattrs = {} |
---|
| 486 | |
---|
| 487 | if '/' in attrs['date']: |
---|
| 488 | year = set() |
---|
| 489 | month = set() |
---|
| 490 | day = set() |
---|
| 491 | start,end = attrs['date'].split('/')[::2] |
---|
| 492 | sdate = datetime.strptime(start, '%Y%m%d') |
---|
| 493 | edate = datetime.strptime(end, '%Y%m%d') |
---|
| 494 | date = sdate |
---|
| 495 | while date <= edate: |
---|
| 496 | year.add(date.year) |
---|
| 497 | month.add(date.month) |
---|
| 498 | day.add(date.day) |
---|
| 499 | date = date + timedelta(days=1) |
---|
| 500 | newattrs['year'] =list(year) |
---|
| 501 | newattrs['month'] = list(month) |
---|
| 502 | newattrs['day'] = list(day) |
---|
| 503 | else: |
---|
| 504 | date = datetime.strptime(attrs['date'], '%Y%m%d') |
---|
| 505 | newattrs['year'] = date.year |
---|
| 506 | newattrs['month'] = date.month |
---|
| 507 | newattrs['day'] = date.day |
---|
| 508 | |
---|
| 509 | newattrs['product_type'] = 'reanalysis' |
---|
| 510 | newattrs['area'] = attrs['area'].split('/') |
---|
| 511 | newattrs['grid'] = list(map(float,attrs['grid'].split('/'))) |
---|
| 512 | newattrs['param'] = attrs['param'].split('/') |
---|
| 513 | newattrs['time'] = list(map(str,range(0,24,3))) |
---|
| 514 | newattrs['format'] = 'grib' |
---|
| 515 | |
---|
| 516 | return newattrs |
---|
[295ff45] | 517 | |
---|
[ff99eae] | 518 | def data_retrieve(self): |
---|
[274f9ef] | 519 | '''Submits a MARS retrieval. Depending on the existence of |
---|
[f20af73] | 520 | ECMWF Web-API or CDS API it is submitted via Python or a |
---|
[274f9ef] | 521 | subprocess in the Shell. The parameter for the mars retrieval |
---|
| 522 | are taken from the defined class attributes. |
---|
| 523 | |
---|
| 524 | Parameters |
---|
| 525 | ---------- |
---|
| 526 | |
---|
| 527 | Return |
---|
| 528 | ------ |
---|
| 529 | |
---|
[efdb01a] | 530 | ''' |
---|
| 531 | # Get all class attributes and their values as a dictionary |
---|
[5bad6ec] | 532 | attrs = vars(self).copy() |
---|
| 533 | |
---|
| 534 | # eliminate unnecessary attributes from the dictionary attrs |
---|
| 535 | del attrs['server'] |
---|
| 536 | del attrs['public'] |
---|
| 537 | |
---|
| 538 | # exchange parameter name for marsclass |
---|
| 539 | mclass = attrs.get('marsclass') |
---|
| 540 | del attrs['marsclass'] |
---|
| 541 | attrs['class'] = mclass |
---|
| 542 | |
---|
[f20af73] | 543 | # prepare target variable as needed for the Web API or CDS API mode |
---|
[5bad6ec] | 544 | # within the dictionary for full access |
---|
| 545 | # as a single variable for public access |
---|
| 546 | target = attrs.get('target') |
---|
| 547 | if not int(self.public): |
---|
| 548 | del attrs['target'] |
---|
| 549 | print('target: ' + target) |
---|
[0a75335] | 550 | |
---|
[5bad6ec] | 551 | # find all keys without a value and convert all other values to strings |
---|
| 552 | empty_keys = [] |
---|
[79729d5] | 553 | for key, value in attrs.items(): |
---|
[5bad6ec] | 554 | if value == '': |
---|
| 555 | empty_keys.append(str(key)) |
---|
[efdb01a] | 556 | else: |
---|
[5bad6ec] | 557 | attrs[key] = str(value) |
---|
| 558 | |
---|
| 559 | # delete all empty parameter from the dictionary |
---|
| 560 | for key in empty_keys: |
---|
| 561 | del attrs[key] |
---|
[efdb01a] | 562 | |
---|
[c77630a] | 563 | # attrs['ppengine'] = 'emos' |
---|
[b957ef7] | 564 | |
---|
[efdb01a] | 565 | # MARS request via Python script |
---|
[5bad6ec] | 566 | if self.server: |
---|
[efdb01a] | 567 | try: |
---|
[f20af73] | 568 | if cds_api and isinstance(self.server, cdsapi.Client): |
---|
[0a75335] | 569 | # distinguish between model (ECMWF MARS access) |
---|
| 570 | # and surface level (CS3 online access) |
---|
| 571 | if attrs['levtype'].lower() == 'ml': |
---|
| 572 | dataset = _config.CDS_DATASET_ML |
---|
| 573 | else: |
---|
| 574 | dataset = _config.CDS_DATASET_SFC |
---|
| 575 | attrs = self._convert_to_cdsera5_sfc_request(attrs) |
---|
[f20af73] | 576 | print('RETRIEVE ERA5 WITH CDS API!') |
---|
[0a75335] | 577 | self.server.retrieve(dataset, |
---|
[f20af73] | 578 | attrs, target) |
---|
| 579 | elif ec_api and isinstance(self.server, ecmwfapi.ECMWFDataServer): |
---|
| 580 | print('RETRIEVE PUBLIC DATA (NOT ERA5)!') |
---|
[5bad6ec] | 581 | self.server.retrieve(attrs) |
---|
[f20af73] | 582 | elif ec_api and isinstance(self.server, ecmwfapi.ECMWFService): |
---|
| 583 | print('EXECUTE NON-PUBLIC RETRIEVAL (NOT ERA5)!') |
---|
[5bad6ec] | 584 | self.server.execute(attrs, target) |
---|
[0e576fc] | 585 | else: |
---|
[f20af73] | 586 | print('ERROR:') |
---|
| 587 | print('No match for Web API instance!') |
---|
[0e576fc] | 588 | raise IOError |
---|
[f20af73] | 589 | except Exception as e: |
---|
| 590 | print('\n\nMARS Request failed!') |
---|
| 591 | print(e) |
---|
| 592 | print(traceback.format_exc()) |
---|
| 593 | sys.exit() |
---|
| 594 | |
---|
| 595 | # MARS request via call in shell |
---|
[efdb01a] | 596 | else: |
---|
[5bad6ec] | 597 | request_str = 'ret' |
---|
[79729d5] | 598 | for key, value in attrs.items(): |
---|
[5bad6ec] | 599 | request_str = request_str + ',' + key + '=' + str(value) |
---|
| 600 | request_str += ',target="' + target + '"' |
---|
[c77630a] | 601 | p = subprocess.Popen(['mars'], #'-e'], |
---|
[5bad6ec] | 602 | stdin=subprocess.PIPE, |
---|
[efdb01a] | 603 | stdout=subprocess.PIPE, |
---|
[5bad6ec] | 604 | stderr=subprocess.PIPE, |
---|
| 605 | bufsize=1) |
---|
[79729d5] | 606 | pout = p.communicate(input=request_str.encode())[0] |
---|
[2fb99de] | 607 | print(pout.decode()) |
---|
[efdb01a] | 608 | |
---|
| 609 | if 'Some errors reported' in pout.decode(): |
---|
[2fb99de] | 610 | print('MARS Request failed - please check request') |
---|
[efdb01a] | 611 | raise IOError |
---|
[5bad6ec] | 612 | elif os.stat(target).st_size == 0: |
---|
[2fb99de] | 613 | print('MARS Request returned no data - please check request') |
---|
[efdb01a] | 614 | raise IOError |
---|
| 615 | |
---|
| 616 | return |
---|