发布使用cds api自动下载数据



我正试图用cd从CMIP6气候预测(https://cds.climate.copernicus.eu/cdsapp#!/dataset/projections-cmip6?tab=form)下载数据。python的API扩展。

问题是,我不知道如何自动化代码,以便通过循环依次下载来自不同现有模型的数据。


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')

如果我在循环外只放置模型的名称,而不是模型[k],则该过程将发生。但是,如果我尝试使用列表自动执行此查询,我将无法获得结果。

下面是我在控制台得到的结果:

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

与模型列表一起工作的修订代码:

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')

最新更新