Changeset f20af73 in flex_extract.git for source/python/classes/MarsRetrieval.py


Ignore:
Timestamp:
Mar 8, 2019, 10:05:20 AM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
82c2959
Parents:
b4a4777
Message:

added CDS API support for ERA5 instead of ECMWFAPI / refactored setup of CONTROL parameter because for local version it wanted to use not installed ECMWF_ENV file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/python/classes/MarsRetrieval.py

    r6f951ca rf20af73  
    3535import sys
    3636import subprocess
     37import traceback
    3738
    3839# software specific classes and modules from flex_extract
    3940sys.path.append('../')
    4041import _config
     42try:
     43    ec_api = True
     44    import ecmwfapi
     45except ImportError:
     46    ec_api = False
     47
     48try:
     49    cds_api = True
     50    import cdsapi
     51except ImportError:
     52    cds_api = False
    4153# ------------------------------------------------------------------------------
    4254# CLASS
     
    130142    '''
    131143
    132     def __init__(self, server, public, marsclass="ei", dataset="", type="",
     144    def __init__(self, server, public, marsclass="EA", dataset="", type="",
    133145                 levtype="", levelist="", repres="", date="", resol="",
    134146                 stream="", area="", time="", step="", expver="1",
     
    158170        marsclass : str, optional
    159171            Characterisation of dataset. E.g. EI (ERA-Interim),
    160             E4 (ERA40), OD (Operational archive), ea (ERA5).
    161             Default is the ERA-Interim dataset "ei".
     172            E4 (ERA40), OD (Operational archive), EA (ERA5).
     173            Default is the ERA5 dataset "EA".
    162174
    163175        dataset : str, optional
     
    429441    def data_retrieve(self):
    430442        '''Submits a MARS retrieval. Depending on the existence of
    431         ECMWF Web-API it is submitted via Python or a
     443        ECMWF Web-API or CDS API it is submitted via Python or a
    432444        subprocess in the Shell. The parameter for the mars retrieval
    433445        are taken from the defined class attributes.
     
    452464        attrs['class'] = mclass
    453465
    454         # prepare target variable as needed for the Web API mode
     466        # prepare target variable as needed for the Web API or CDS API mode
    455467        # within the dictionary for full access
    456468        # as a single variable for public access
     
    475487        if self.server:
    476488            try:
    477                 if self.public:
    478                     print('RETRIEVE PUBLIC DATA!')
     489                if cds_api and isinstance(self.server, cdsapi.Client):
     490                    print('RETRIEVE ERA5 WITH CDS API!')
     491                    self.server.retrieve(_config.CDS_DATASET,
     492                                         attrs, target)
     493                elif ec_api and isinstance(self.server, ecmwfapi.ECMWFDataServer):
     494                    print('RETRIEVE PUBLIC DATA (NOT ERA5)!')
    479495                    self.server.retrieve(attrs)
     496                elif ec_api and isinstance(self.server, ecmwfapi.ECMWFService):
     497                    print('EXECUTE NON-PUBLIC RETRIEVAL (NOT ERA5)!')
     498                    self.server.execute(attrs, target)
    480499                else:
    481                     print('EXECUTE NON-PUBLIC RETRIEVAL!')
    482                     self.server.execute(attrs, target)
    483             except:
    484                 e = sys.exc_info()[0]
    485                 print("ERROR: ", e)
    486                 print('MARS Request failed!')
    487                 if not self.public and os.stat(target).st_size == 0:
    488                     print('MARS Request returned no data - '
    489                           'please check request')
     500                    print('ERROR:')
     501                    print('No match for Web API instance!')
    490502                    raise IOError
    491                 elif self.public and os.stat(target).st_size == 0:
    492                     print('Public MARS Request returned no data - '
    493                           'please check request')
    494                     raise IOError
    495                 else:
    496                     raise IOError
    497         # MARS request via extra process in shell
     503            except Exception as e:
     504                print('\n\nMARS Request failed!')
     505                print(e)
     506                tb = sys.exc_info()[2]
     507                print(traceback.format_exc())
     508                sys.exit()
     509
     510        # MARS request via call in shell
    498511        else:
    499512            request_str = 'ret'
     
    501514                request_str = request_str + ',' + key + '=' + str(value)
    502515            request_str += ',target="' + target + '"'
    503             p = subprocess.Popen(['mars'],
     516            p = subprocess.Popen(['mars', '-p'],
    504517                                 stdin=subprocess.PIPE,
    505518                                 stdout=subprocess.PIPE,
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG