如何使用cdsapi下载ERA5/ERA5-Land日产品数据

  • 自行安装cdsapi、requests的包(提示:cdsapi · PyPI
  • 获取下载授权:在How to use the CDS API |中注册完毕,右下方会显示下载链接以及授权,新建一个txt,并重命名为:.cdsapirc,把右侧两行代码复制粘贴进去保存。(参考https://www.sogou.com/link?url=hedJjaC291OB0PrGj_c3jLEFOfDkmI-hK2Rzm35wU9wOliP4FOJBNbU6DtrNzhozV4f66Q0_08oqtLrychrmIA..)

  •  运行以下代码:

## 代码来源:

Retrieve daily ERA5/ERA5-Land data using the CDS API - Copernicus User Support Forum - ECMWF Confluence Wiki

## 参数说明:

https://datastore.copernicus-climate.eu/documents/app-c3s-daily-era5-statistics/C3S_Application-Documentation_ERA5-daily-statistics-v2.pdf

import cdsapi

import requests


# 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



c = cdsapi.Client(timeout=300)


# Uncomment years as required


years =  [

            '1979'

#           ,'1980', '1981',

#            '1982', '1983', '1984',

#            '1985', '1986', '1987',

#            '1988', '1989', '1990',

#            '1991', '1992', '1993',

#            '1994', '1995', '1996',

#            '1997', '1998', '1999',

#            '2000', '2001', '2002',

#            '2003', '2004', '2005',

#            '2006', '2007', '2008',

#            '2009', '2010', '2011',

#            '2012', '2013', '2014',

#            '2015', '2016', '2017',

#            '2018', '2019', '2020',

#            '2021'

]





# Retrieve all months for a given year.



months = ['01', '02', '03',

            '04', '05', '06',

            '07', '08', '09',

            '10', '11', '12']


# 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.

var = "2m_temperature"



# Select the required statistic, valid names given in link above

stat = "daily_mean"


# Loop over years and months


for yr in years:

    for mn in months:

        result = c.service(

        "tool.toolbox.orchestrator.workflow",

        params={

             "realm": "user-apps",

             "project": "app-c3s-daily-era5-statistics",

             "version": "master",

             "kwargs": {

                 "dataset": "reanalysis-era5-single-levels", ##选择数据集

                 "product_type": "reanalysis",

                 "variable": var,

                 "statistic": stat,

                 "year": yr,

                 "month": mn,

                 "time_zone": "UTC+00:0", ##重要,修改时区,影响日合成的时刻选择

                 "frequency": "1-hourly",

#

# Users can change the output grid resolution and selected area

#

#                "grid": "1.0/1.0", ##分辨率

#                "area":{"lat": [10, 60], "lon": [65, 140]}



                 },

        "workflow_name": "application"

        })

        

# set name of output file for each month (statistic, variable, year, month    



        file_name = "download_" + stat + "_" + var + "_" + yr + "_" + mn + ".nc"

        

        location=result[0]['location']

        res = requests.get(location, stream = True)

        print("Writing data to " + file_name)

        with open(file_name,'wb') as fh:

            for r in res.iter_content(chunk_size = 1024):

                fh.write(r)

        fh.close()
  • 运行该代码会报错:Exception: Missing/incomplete configuration file: C:\Users\User/.cdsapirc,“C:\Users\User”是自己的路径,这时候把刚刚制作好的.cdsapirc文件复制到该文件夹下即可。
  • 再次运行第3步中的代码,文件默认以.nc的格式下载到该程序所在的文件夹。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值