Changeset 0a75335 in flex_extract.git for Source/Python/Classes/MarsRetrieval.py


Ignore:
Timestamp:
Jul 10, 2020, 8:20:31 AM (4 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
8028176
Parents:
bbbe1e1
Message:

changed cds api retrieval for single levels to retrieve from CS3 disks instead of MARS - severe performance boost

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Source/Python/Classes/MarsRetrieval.py

    r026b359 r0a75335  
    446446
    447447        return
     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
    448517
    449518    def data_retrieve(self):
     
    479548            del attrs['target']
    480549        print('target: ' + target)
    481 
     550       
    482551        # find all keys without a value and convert all other values to strings
    483552        empty_keys = []
     
    498567            try:
    499568                if cds_api and isinstance(self.server, cdsapi.Client):
     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)
    500576                    print('RETRIEVE ERA5 WITH CDS API!')
    501                     self.server.retrieve(_config.CDS_DATASET,
     577                    self.server.retrieve(dataset,
    502578                                         attrs, target)
    503579                elif ec_api and isinstance(self.server, ecmwfapi.ECMWFDataServer):
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG