You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

page under construction ------------------------------


The CDS API is a Python service that enables the access to CEMS-Flood data on the CDS.  It is ideal for users that retrieve large volumes of data or need to automate tasks. This page collects a number of scripts that can work as blueprint for more user-specific requests.


CDS API Installation

Instructions about the installation and set up of the CDS API can be found in How to use the CDS API.

How to run the scripts:

You should copy the content of the script into a python file (ex: retrieve_<dataset>.py) and then launch it from a terminal:

user@host:~$ python retrieve_<dataset>.py



API script examples:

EFAS Medium-range climatology

## === retrieve EFAS Medium-Range Climatology === 
import cdsapi


if __name__ == '__main__':

	c = cdsapi.Client()


	VARIABLES = [
			'river_discharge_in_the_last_6_hours', 'snow_depth_water_equivalent',
	]


	YEARS = ['%02d'%(mn) for mn in range(1991,2022)]

	MONTHS = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
	DAYS = ['%02d'%(mn) for mn in range(1,32)]


	for variable in VARIABLES:
		for year in YEARS:
			c.retrieve(
				'efas-historical',
				{
					'system_version': 'version_4_0',
					'variable': variable,
					'model_levels': 'surface_level',
					'hyear': '1991',
					'hmonth': MONTHS,
					'hday': DAYS,
					'time': '00:00',
					'format': 'grib',
				},
					f'efas_historical_{variable}_{year}.grib')

EFAS Medium-range forecast

## === retrieve EFAS Medium-Range Forecast === 

import cdsapi
import datetime



def compute_dates_range(start_date,end_date,loop_days=True):


    start_date = datetime.date(*[int(x) for x in start_date.split('-')])
    
    end_date = datetime.date(*[int(x) for x in end_date.split('-')])
    
    ndays =  (end_date - start_date).days + 1
    
    dates = []
    for d in range(ndays):
        dates.append(start_date + datetime.timedelta(d))
    
    if not loop_days:
        dates = [i for i in dates if i.day == 1]
    else:
        pass
    return dates



if __name__ == '__main__':


    # start the client
    c = cdsapi.Client()


    # user inputs
    START_DATE = '2020-10-14' # first date with available data

    END_DATE = '2021-02-28' 

    LEADTIMES =  [str(lt) for lt in range(0,372,6)]


    # loop over dates and save to disk

    dates = compute_dates_range(START_DATE,END_DATE)

    for date in dates:

        year  = date.strftime('%Y')
        month = date.strftime('%m')
        day   = date.strftime('%d')

        print(f"RETRIEVING: {year}-{month}-{day}")

        c.retrieve('efas-forecast',
            {
                'format': 'grib',
                'originating_centre':'ecmwf',
                'product_type':'ensemble_perturbed_forecasts',
                'variable': 'river_discharge_in_the_last_6_hours',
                'model_levels': 'surface_level',
                'year': year,
                'month': month,
                'day': day,
                'leadtime_hour':LEADTIMES,
                'time': '12:00',
            },
            f'efas_forecast_{year}_{month}_{day}.grib')

GloFAS Medium-range climatology

## === retrieve GloFAS Medium-Range Climatology === 

import cdsapi


if __name__ == '__main__':
    c = cdsapi.Client()


    YEARS  = ['%02d'%(mn) for mn in range(1979,2021)]

    MONTHS = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
    DAYS   = ['%02d'%(mn) for mn in range(1,32)]


    for year in YEARS:
        c.retrieve(
            'cems-glofas-historical',
            {
                'system_version':'version_2_1',
                'product_type': 'consolidated',
                'hydrological_model': 'htessel_lisflood',
                'variable': 'river_discharge_in_the_last_24_hours',
                'hyear': year,,
                'hmonth': MONTHS,
                'hday': DAYS,,
                'format': 'grib',
            },
            f'glofas_historical_{year}.grib')

GloFAS Medium-range forecast

## === retrieve GloFAS Medium-Range Forecast === 

import cdsapi
import datetime
import warnings



def compute_dates_range(start_date,end_date,loop_days=True):


    start_date = datetime.date(*[int(x) for x in start_date.split('-')])
    
    end_date = datetime.date(*[int(x) for x in end_date.split('-')])
    
    ndays =  (end_date - start_date).days + 1
    
    dates = []
    for d in range(ndays):
        dates.append(start_date + datetime.timedelta(d))
    
    if not loop_days:
        dates = [i for i in dates if i.day == 1]
    else:
        pass
    return dates



if __name__ == '__main__':


    # start the client
    c = cdsapi.Client()


    # user inputs
    START_DATE = '2019-11-05' # first date with available data

    END_DATE = '2021-03-15' 

    LEADTIMES =  [str(lt) for lt in range(24,744,24)]


    # loop over dates and save to disk

    dates = compute_dates_range(START_DATE,END_DATE)

    for date in dates:

        year  = date.strftime('%Y')
        month = date.strftime('%m')
        day   = date.strftime('%d')

        print(f"RETRIEVING: {year}-{month}-{day}")

        c.retrieve(
            'cems-glofas-forecast',
            {
                'format': 'grib',
                'system_version':'operational',
                'hydrological_model': 'htessel_lisflood',
                'product_type':'ensemble_perturbed_forecasts',
                'variable': 'river_discharge_in_the_last_24_hours',
                'year': year,
                'month': month,
                'day': day,
                'leadtime_hour':LEADTIMES
            },
            f'glofas_forecast_{year}_{month}_{day}.grib')


  • No labels