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


Ignore:
Timestamp:
Oct 5, 2018, 5:20:48 PM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
ae88f7d
Parents:
ca867de
Message:

added possibility to extract public datasets via an logical public parameter

File:
1 edited

Legend:

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

    rca867de r5bad6ec  
    8383    '''
    8484
    85     def __init__(self, server, marsclass="ei", type="", levtype="",
    86                  levelist="", repres="", date="", resol="", stream="",
    87                  area="", time="", step="", expver="1", number="",
    88                  accuracy="", grid="", gaussian="", target="",
     85    def __init__(self, server, public, marsclass="ei", dataset="", type="",
     86                 levtype="", levelist="", repres="", date="", resol="",
     87                 stream="", area="", time="", step="", expver="1",
     88                 number="", accuracy="", grid="", gaussian="", target="",
    8989                 param=""):
    9090        '''
     
    106106                It is needed for the pythonic access of ECMWF data.
    107107
     108            public: integer
     109                Decides which Web API version is used:
     110                0: member-state users and full archive access
     111                1: public access and limited access to the public server and
     112                   datasets. Needs the parameter dataset.
     113                Default is "0" and for member-state users.
     114
    108115            marsclass: string, optional
    109116                Characterisation of dataset. E.g. EI (ERA-Interim),
    110117                E4 (ERA40), OD (Operational archive), ea (ERA5).
    111118                Default is the ERA-Interim dataset "ei".
     119
     120            dataset: string, optional
     121                For public datasets there is the specific naming and parameter
     122                dataset which has to be used to characterize the type of
     123                data. Usually there is less data available, either in times,
     124                domain or parameter.
     125                Default is an empty string.
    112126
    113127            type: string, optional
     
    289303
    290304        self.server = server
     305        self.public = public
    291306        self.marsclass = marsclass
     307        self.dataset = dataset
    292308        self.type = type
    293309        self.levtype = levtype
     
    325341        '''
    326342        # Get all class attributes and their values as a dictionary
    327         attrs = vars(self)
     343        attrs = vars(self).copy()
    328344
    329345        # iterate through all attributes and print them
    330346        # with their corresponding values
    331347        for item in attrs.items():
    332             if item[0] in 'server':
     348            if item[0] in ['server', 'public']:
    333349                pass
    334350            else:
     
    358374        '''
    359375        # Get all class attributes and their values as a dictionary
    360         attrs = vars(self)
     376        attrs = vars(self).copy()
    361377
    362378        # open a file to store all requests to
     
    367383            # with their corresponding values
    368384            for item in attrs.items():
    369                 if item[0] in 'server':
     385                if item[0] in ['server', 'public']:
    370386                    pass
    371387                else:
     
    396412
    397413        # Get all class attributes and their values as a dictionary
    398         attrs = vars(self)
     414        attrs = vars(self).copy()
    399415        del attrs['server']
     416        del attrs['public']
    400417
    401418        # open a file to store all requests to
     
    425442        '''
    426443        # Get all class attributes and their values as a dictionary
    427         attrs = vars(self)
    428 
    429         # convert the dictionary of attributes into a comma
    430         # seperated list of attributes with their values
    431         # needed for the retrieval call
    432         s = 'ret'
    433         for k, v in attrs.iteritems():
    434             if k in 'server':
    435                 continue
    436             if k == 'marsclass':
    437                 k = 'class'
    438             if v == '':
    439                 continue
    440             if k.lower() == 'target':
    441                 target = v
     444        attrs = vars(self).copy()
     445
     446        # eliminate unnecessary attributes from the dictionary attrs
     447        del attrs['server']
     448        del attrs['public']
     449
     450        # exchange parameter name for marsclass
     451        mclass = attrs.get('marsclass')
     452        del attrs['marsclass']
     453        attrs['class'] = mclass
     454
     455        # prepare target variable as needed for the Web API mode
     456        # within the dictionary for full access
     457        # as a single variable for public access
     458        target = attrs.get('target')
     459        if not int(self.public):
     460            del attrs['target']
     461        print('target: ' + target)
     462
     463        # find all keys without a value and convert all other values to strings
     464        empty_keys = []
     465        for key, value in attrs.itteritems():
     466            if value == '':
     467                empty_keys.append(str(key))
    442468            else:
    443                 s = s + ',' + k + '=' + str(v)
     469                attrs[key] = str(value)
     470
     471        # delete all empty parameter from the dictionary
     472        for key in empty_keys:
     473            del attrs[key]
    444474
    445475        # MARS request via Python script
    446         if self.server is not False:
     476        if self.server:
    447477            try:
    448                 self.server.execute(s, target)
     478                if self.public:
     479                    print('RETRIEVE PUBLIC DATA!')
     480                    self.server.retrieve(attrs)
     481                else:
     482                    print('EXECUTE NON-PUBLIC RETRIEVAL!')
     483                    self.server.execute(attrs, target)
    449484            except:
    450                 print('MARS Request failed, \
    451                       have you already registered at apps.ecmwf.int?')
     485                e = sys.exc_info()[0]
     486                print("ERROR: ", e)
     487                print('MARS Request failed!')
     488            if not self.public and os.stat(target).st_size == 0:
     489                print('MARS Request returned no data - please check request')
    452490                raise IOError
    453             if os.stat(target).st_size == 0:
    454                 print('MARS Request returned no data - please check request')
     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:
    455496                raise IOError
    456497        # MARS request via extra process in shell
    457498        else:
    458             s += ',target = "' + target + '"'
    459             p = subprocess.Popen(['mars'], stdin=subprocess.PIPE,
     499            request_str = 'ret'
     500            for key, value in attrs.iteritems():
     501                request_str = request_str + ',' + key + '=' + str(value)
     502            request_str += ',target="' + target + '"'
     503            p = subprocess.Popen(['mars'],
     504                                 stdin=subprocess.PIPE,
    460505                                 stdout=subprocess.PIPE,
    461                                  stderr=subprocess.PIPE, bufsize=1)
    462             pout = p.communicate(input=s)[0]
     506                                 stderr=subprocess.PIPE,
     507                                 bufsize=1)
     508            pout = p.communicate(input=request_str)[0]
    463509            print(pout.decode())
    464510
     
    466512                print('MARS Request failed - please check request')
    467513                raise IOError
    468 
    469             if os.stat(target).st_size == 0:
     514            elif os.stat(target).st_size == 0:
    470515                print('MARS Request returned no data - please check request')
    471516                raise IOError
     517            else:
     518                raise
    472519
    473520        return
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG