Issue to automize download of data with cds api

566 views Asked by At

I'm trying to download data from CMIP6 climate projections ( https://cds.climate.copernicus.eu/cdsapp#!/dataset/projections-cmip6?tab=form ) with the cds.api extension on python.

The problem is that I don't get how to automatize the code in order to download successively the data from different existing models with a loop.


import __main__
import cdsapi
import sys
#import cdstoolbox 
import numpy as np


c = cdsapi.Client()

model=['cnrm_cm6_1','cmcc_cm2_sr5']
  
    
for k in range(len(model)):
        c.retrieve(
            'projections-cmip6',
            {
                'format': 'zip',
                'experiment': 'historical',
                'temporal_resolution': 'monthly',
                'variable': 'precipitation',
                'model': model[k],
                'year': '2010',
                'month': '01',
                'area': [
                    -2, 29, -3,
                    30,
                    ],
                },
                'download.zip')

The process takes place if I put only the name of a model outside the loop, instead of model[k]. But if I try to automate this query with a list, I am unable to get results.

Here is what I get in the Console:

2023-01-17 09:11:17,601 INFO Sending request 
to https://download-0000.copernicus-climate.eu/api/v2/resources/projections-cmip6
2023-01-17 09:11:17,607 WARNING Recovering from connection error [HTTPSConnectionPool
(host='download-0000.copernicus-climate.eu', port=443): 
Max retries exceeded with url: /api/v2/resources/projections-cmip6 
(Caused by NewConnectionError('<urllib3.connection.HTTPSConnection 
object at 0x00000222E751B3D0>: Failed to establish 
a new connection: [Errno 11001] getaddrinfo failed'))],
 attemps 0 of 500
2023-01-17 09:11:17,608 WARNING Retrying in 120 seconds
1

There are 1 answers

0
SIGHUP On

Revised code that works with a list of models:

import cdsapi
import urllib3

urllib3.disable_warnings()

c = cdsapi.Client()

models = ['cnrm_cm6_1', 'cmcc_cm2_sr5']
params = {
    'format': 'zip',
    'experiment': 'historical',
    'temporal_resolution': 'monthly',
    'variable': 'precipitation',
    'year': '2010',
    'month': '01',
    'area': [-2, 29, -3, 30]
}


for model in models:
    params['model'] = model
    c.retrieve('projections-cmip6', params, f'{model}.zip')