一、配置ERA5的cdsapi
Catalogue — Climate Data StoreERA5数据集
使用Python下载ECMWF的ERA-5数据_python下载era5数据-CSDN博客
1.注册ERA5账号
2.获取个人API并配置API
复制url和key,并在c/用户名/.cdsapirc 改后缀新建此文件,并将内容写入文件中。
3.安装cdsapi第三方库
conda install cdsapi
二、配置IDM
IDM下载及配置参考IDM 免费安装教程(已支持最新版 6.42)_idm免费-CSDN博客
1.下载IDM
2.下载crack软件并运行,重启后就可以了
三、数据批量下载
按照下面代码进行修改即可
import cdsapi
import os
from subprocess import call
def idmDownloader(task_url, folder_path, file_name):
"""
IDM下载器
:param task_url: 下载任务地址
:param folder_path: 存放文件夹
:param file_name: 文件名
:return:
"""
# IDM安装目录
idm_engine = "C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe"
# 将任务添加至队列
call([idm_engine, '/d', task_url, '/p', folder_path, '/f', file_name, '/a'])
# 开始任务队列
call([idm_engine, '/s'])
c = cdsapi.Client()
yearStart = 2001
yearEnd = 2020
monStrt = 1
monEnd = 12
grid = 0.25
variable = 'uwnd' #后面还是得变
base_path = r'E:\data\ERA5_4xdaily'
path = os.path.join(base_path, variable) # 存放文件夹
years = range(yearStart, yearEnd+1)
yr = [str(i) for i in years]
months = range(monStrt, monEnd+1)
mo = [str(j).zfill(2) for j in months]
for iyr in range(len(yr)):
year_path = os.path.join(path, str(yr[iyr])) # 创建每年的文件夹
os.makedirs(year_path, exist_ok=True) # 如果文件夹不存在则创建
for imo in range(len(mo)):
filename = variable + f'.6h.global.{grid}deg.{yr[iyr]}.{mo[imo]}.nc'
output_file_path = os.path.join(year_path, filename)
# 检查文件是否已存在
if os.path.exists(output_file_path):
print(f'File {output_file_path} already exists. Skipping download.')
continue
print('======= date: ' + yr[iyr] + '.' + mo[imo] + ' =======')
r = c.retrieve(
'reanalysis-era5-pressure-levels',
{
'variable': ['u_component_of_wind'],
'pressure_level': [
'1', '3', '5',
'7', '10', '30',
'50', '70', '100',
'150', '200', '250',
'300', '400', '500',
'600', '700', '850',
'925', '975', '1000'
],
'product_type': ['reanalysis'],
'year': [yr[iyr]],
'month': [mo[imo]],
'day': [
'01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
'13', '14', '15',
'16', '17', '18',
'19', '20', '21',
'22', '23', '24',
'25', '26', '27',
'28', '29', '30',
'31'
],
'time': [
'00:00', '06:00', '12:00', '18:00'
],
'data_format': 'netcdf',
'grid': str(grid) + '/' + str(grid),
'area': [60, 59, 0, 140],
'download_format': 'unarchived'
}, )
url = r.location # 获取文件下载地址
idmDownloader(url, year_path, filename) # 添加进IDM中下载