ECMWF歐洲中心提供的api只能根據單時間下載,我稍微改動了一下,可以根據時間進行批量下載。詳細步驟如下:
1、註冊賬戶獲得key。
網址:
https://cds.climate.copernicus.eu/#!/home
將url和key先存儲在txt文件中,格式如下:
之後,重命名txt文件,同時更改綴。命名改爲:.cdsapirc
這裏要注意,要把.txt的後綴只改爲.(命名其實爲.cdsapirc.)
文件名最終爲:.cdsapirc
將文件放入路徑C:\Users***(根據自己的用戶名)
2、安裝cdsapi
直接 pip install cdsapi
3、下載數據
官方提供的方法:
#!/usr/bin/env python
import cdsapi
c = cdsapi.Client()
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type':'reanalysis',
'variable':[
'total_column_ozone','total_column_water_vapour'
],
'year':'2019',
'month':'01',
'day':'23',
'time':[
'00:00','01:00','02:00',
'03:00','04:00','05:00',
'06:00','07:00','08:00',
'09:00','10:00','11:00',
'12:00','13:00','14:00',
'15:00','16:00','17:00',
'18:00','19:00','20:00',
'21:00','22:00','23:00'
],
'format':'netcdf'
},
'download.nc')
這裏只能下載單一時間,而且數據名爲 ‘download.nc’。
我希望將我想下載的時間存入txt(時間文件inputpar.txt),直接批量下載,數據存儲形式改爲CDS+時間,代碼如下:
# author = yc
import cdsapi
def download_cds():
c = cdsapi.Client()
lines = open("inputpar.txt", 'r').readlines()
for i in range(len(lines)):
if i%2==0:
fields = lines[i].split(' ') #split data
year = fields[0]
month = fields[1]
day = fields[2]
print('Download %s-%s-%s data'% (year,month,day))
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type': 'reanalysis',
'variable': [
'total_column_ozone', 'total_column_water_vapour'
],
'year': year,
'month': month,
'day': day,
'time': [
'00:00', '01:00', '02:00',
'03:00', '04:00', '05:00',
'06:00', '07:00', '08:00',
'09:00', '10:00', '11:00',
'12:00', '13:00', '14:00',
'15:00', '16:00', '17:00',
'18:00', '19:00', '20:00',
'21:00', '22:00', '23:00'
],
'format': 'netcdf'
},
'CDS%s%s%s'% (year,month,day)+'.nc')
print('Datafile CDS%s%s%s'% (year,month,day)+'.nc download successful')
else:
pass
if __name__ == '__main__':
download_cds()
結果如下:
歡迎交流,祝好!
2019.07.27