python批量下载ERA5气象数据

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 23 11:19:18 2023

@author: sun'xiang'yan
"""

import time 
import cdsapi
import requests
import multiprocessing
import numpy as np
import calendar
import os


# CDS API script to use CDS service to retrieve daily ERA5* variables and iterate over
# all months in the specified years.
 
# Requires:
# 1) the CDS API to be installed and working on your system
# 2) You have agreed to the ERA5 Licence (via the CDS web page)
# 3) Selection of required variable, daily statistic, etc
 
# Output:
# 1) separate netCDF file for chosen daily statistic/variable for each month
 
# Uncomment years as required
# For valid keywords, see Table 2 of:
# https://datastore.copernicus-climate.eu/documents/app-c3s-daily-era5-statistics/C3S_Application-Documentation_ERA5-daily-statistics-v2.pdf
# select your variable; name must be a valid ERA5 CDS API name.
# Select the required statistic, valid names given in link above

# define directory in which data shall be stored
os.chdir("E:/ERA5")

#账号的url\uid\key
#c = cdsapi.Client(url= "https://cds.climate.copernicus.eu/api/v2",
   #               key = "XXX")

# =============================================================================
# define variable to download (eg. u component 100m wind)
# var = [ '10m_u_component_of_wind',
#         '10m_v_component_of_wind',
#         '2m_temperature',
#         '2m_dewpoint_temperature',
#         'Snowfall',
#         'Surface_pressure',
#         'Total_precipitation']
# =============================================================================

var = [ '2m_temperature',
        'total_precipitation']


# define the years you want to download
yearstart = 2016
yearend = 2016
# define the start and end month you want to download
monthstart = 1
monthend = 12
# define the start and end day you want to download
daystart = 1
dayend = 31
# define spatial limits of download (eg. around Austria)
lon1 = 100
lon2 = 107
lat1 = 28
lat2 = 35

# create lists
years = np.array(range(yearstart,yearend+1),dtype="str")
area = [lat2, lon1, lat1, lon2]

c = cdsapi.Client()

for year in years:
    if (int(year)==yearstart) and (int(year)==yearend):
        months = np.array(range(monthstart,monthend+1),dtype="str")
    elif (year == yearstart) :
        months = np.array(range(monthstart,13),dtype="str")
    elif (year == yearend):
        months = np.array(range(1,monthend + 1),dtype="str")
    else:
        months = np.array(range(1,13),dtype="str")
               
    for month in months:
        m = '{:0>2}'.format(str(month))
        # if int(month) < 10:
        #     m = '0' + month
        # else:
        #     m = month
        daysofmonth=calendar.monthrange(int(year),int(month))
        daysday=daysofmonth[1]
        # if(int(year) == yearstart) and (int(year) == yearend) and (int(month) == monthstart) and (int(month) == monthend):
        #         days = list(np.array(range(daystart,dayend+1),dtype="str"))
        # elif (int(year) == yearstart) and (int(month) == monthstart):
        #     days = list(np.array(range(daystart,calendar.monthrange(int(year),int(month))[1]+1),dtype="str"))
        # elif (int(year) == yearend) and (int(month) == monthend):
        #     days = list(np.array(range(1,dayend+1),dtype="str"))
        # else:
        #     days = list(np.array(range(1,calendar.monthrange(int(year),int(month))[1]+1),dtype="str"))
        day=['01']
        for daytemp in range(daysday-1):
            d = '{:0>2}'.format(str(daytemp+2))
            day.append(d)
        # day=day+','
        c.retrieve(
            'reanalysis-era5-land',
            {
                'variable': var,
                'year': year,
                'month': m,
                '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',
                ],
                'area': area,
                # 'format': 'netcdf',
                'format': 'grib',
                'grid':'0.1/0.1',
            },
            'era5_' + '_' + year + m+ '.grib')

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值