python -- 下载ERA5数据的脚本

  • 分享一下常用的下载ERA5单层数据的脚本,多层的在此基础上简单修改即可实现
  • 下载的数据时间分辨率为hourly
  • 该脚本的优点在于:自定义年份、自定义空间分辨率、任意选择变量、自定义下载区域、
"""
Created on Mon Nov 27 17:05:39 2023

@author: jianpu

@blog :  https://blog.csdn.net/weixin_44237337?spm=1000.2115.3001.5343

@email: [email protected]

introduction : keep learning althongh walk slowly
"""
import cdsapi
import calendar as cal
from subprocess import call
import os
import numpy as np
varnames = ['10m_u_component_of_neutral_wind', 
            '10m_u_component_of_wind', 
            '10m_v_component_of_neutral_wind',
            '10m_v_component_of_wind',]

varshorts = ["u10","v10","u10_neutral","v10_neutral",]

startYear = 2019
endYear = 2020
startMonth = 1
endMonth = 12

latN = 40
latS = -40
lonW = -180
lonE = 180

grid = ['0.5','0.5']

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',
        ] # hourly data is available

datadir = "I:/ERA5/"

if  not os.path.exists(datadir):
    print('path not exist')
    
    os.makedirs(datadir)
    print('path create')
    
os.chdir(datadir)


area=[latN,lonW,latS,lonE] # North, West, South, East. Default: global
years= np.arange(startYear,endYear+1)
months= np.arange(startMonth,endMonth+1)

c = cdsapi.Client()



dataInfoDict={
    'product_type':'reanalysis',
    'format':'netcdf',
    # 'pressure_level':'',
    'variable':'',
    'year':'',
    'month':'',
    'day':'',
    'area':'',
    'grid':'',
    'time':''
    }


for i in np.arange(0,4,1):
    varname = varnames[i]
    varshort = varshorts[i]

    pp = './'+(varshort)+'/'
    
    if  os.path.exists(pp):
        
        print('yes')
         
        for year in years:
            for month in months:
                month = int(month)
                year  = int(year )
                weekday, daysInMonth= cal.monthrange(year,month)
                days= np.arange(1,daysInMonth+1)
                dayStr = ','.join(str(days[i]).zfill(2) for i in days-1)
                dayList=dayStr.split(',')
                filename = pp+varshort+"_"+str(year)+str(month).zfill(2)+".nc"
                print(filename)
                if not os.path.isfile(filename):
                    dataInfoDict["year"]=int(year)
                    dataInfoDict["month"]=int(month) # convert np.int64 to regular int type
                    # dataInfoDict["pressure_level"]=level # convert np.int64 to regular int type
                    dataInfoDict["variable"]=varname # convert np.int64 to regular int type
                    dataInfoDict["day"] =dayList
                    dataInfoDict["area"]=area
                    dataInfoDict["grid"]=grid
                    dataInfoDict["time"]=time
    
                    print(dataInfoDict)
    
                    c.retrieve(
                      'reanalysis-era5-single-levels',dataInfoDict,filename)
                del days
                del dayList
    else:
       
       os.makedirs(pp)
       
       print(pp+'have created')   
       for year in years:
            for month in months:
                month = int(month)
                year  = int(year )
                weekday, daysInMonth= cal.monthrange(year,month)
                days= np.arange(1,daysInMonth+1)
                dayStr = ','.join(str(days[i]).zfill(2) for i in days-1)
                dayList=dayStr.split(',')
                filename = pp+varshort+"."+str(year)+str(month).zfill(2)+".nc"
                print(filename)
                if not os.path.isfile(filename):
                    dataInfoDict["year"]=int(year)
                    dataInfoDict["month"]=int(month) # convert np.int64 to regular int type
                    # dataInfoDict["pressure_level"]=level # convert np.int64 to regular int type
                    dataInfoDict["variable"]=varname # convert np.int64 to regular int type
                    dataInfoDict["day"] =dayList
                    dataInfoDict["area"]=area
                    dataInfoDict["grid"]=grid
                    dataInfoDict["time"]=time
    
                    print(dataInfoDict)
    
                    c.retrieve(
                       'reanalysis-era5-single-levels',dataInfoDict,filename)
                del days
                del dayList
        
        
quit() 

你可能感兴趣的:(python常用脚本,python,开发语言)