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