'''
批量下载每小时平均ECMWF风场数据
'''

# -*- coding: utf-8 -*-
import cdsapi
# from ecmwfapi import ECMWFDataServer
import calendar
#在运行前先安装cdsapi包

# 创建 ECMWF 数据服务器客户端
# server = ECMWFDataServer()

c = cdsapi.Client()

# dic = {
#     'product_type': 'reanalysis', #选择数据集
#     'format': 'netcdf',  #选择数据格式
#      'variable': [
#          'Mean_sea_level_pressure', 'Sea_surface_temperature','2m_temperature',
#          '100m_u_component_of_wind', '100m_v_component_of_wind', '10m_u_component_of_neutral_wind',
#          '10m_u_component_of_wind', '10m_v_component_of_neutral_wind', '10m_v_component_of_wind',
#          '10m_wind_gust_since_previous_post_processing', 'instantaneous_10m_wind_gust',
#         ],
#     'year': '',  
#     'month': '',  
#     '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'
#     ]
# }

dic = {
    'product_type': 'reanalysis', #选择数据集
    'format': 'netcdf',  #选择数据格式
    'download_format': 'unarchived',
    'variable': [
         'mean_sea_level_pressure', 'sea_surface_temperature','2m_temperature',
           '100m_u_component_of_wind', '100m_v_component_of_wind', '10m_u_component_of_neutral_wind',
           '10m_u_component_of_wind', '10m_v_component_of_neutral_wind', '10m_v_component_of_wind',
           # '10m_wind_gust_since_previous_post_processing', 'instantaneous_10m_wind_gust',
    ],
    'year': [],  
    'month': [],  
    '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'
    ]
}

        
 # 批量下载1979年到2021年所有月份数据
for i in range(2022, 2023):  # 2025年数据
    for j in range(2, 3):  # 2月数据
        day_num = calendar.monthrange(i, j)[1]  # 根据年月，获取当月日数
        for day_indx in range(day_num):
            dic['year'] = [str(i)]
            dic['month'] = [str(j).zfill(2)]
            dic['day'] = [str(day_indx+1).zfill(2)]
            filename = 'F:\\ECMWF\\once_time\\total_precipitation_' + str(i) + str(j).zfill(2)+str(day_indx+1).zfill(2) + '.nc'  # 文件存储路径
            print(filename)

            # try:
            #     c.retrieve('reanalysis-era5-single-levels', dic, filename).download()
            # except Exception as e:
            #     print(f"An error occurred while downloading data for {i}-{j}: {e}")
            c.retrieve('reanalysis-era5-single-levels', dic, filename)  # 下载数据
 
# 仅下载2022年6月份数据（某一年的某一个月数据）        
# i = 2022 
# j = 6 
# day_num = calendar.monthrange(i, j)[1]  # 根据年月，获取当月日数
# dic['year'] = str(i)
# dic['month'] = str(j).zfill(2)
# dic['day'] = [str(d).zfill(2) for d in range(1, day_num + 1)]
# filename = 'G:\\data_ecmwf\\once_time\\total_precipitation' + str(i) + str(j).zfill(2) + '.nc'  # 文件存储路径
# c.retrieve('reanalysis-era5-single-levels', dic, filename)  # 下载数据         
        
        
        