服务器上用python,下载ERA5数据,

Posted on 2023-03-15 20:18  South_snow  阅读(1503)  评论(0编辑  收藏  举报
目前ERA-5和ERA-5 Land仅提供逐月和逐小时资料,官网提供了日平均资料Daily statistics calculated from ERA5 data (copernicus.eu),但是单次仅能下载一个月,鄙人找到了用Python可以下载多个月份的方法,具体如下:1、cdsapi库的安装。2、运行批量下载的Python程序。

1、cdsapi库的安装
Windows和Linux系统下可以在Python使用cdsapi库来实现批量下载,cdsapi库的安装可以移步官方教程How to install and use CDS API on Windows - Copernicus Knowledge Base - ECMWF Confluence Wiki,下面介绍如何在Windows下安装cdsapi库,有两种方法:(1)、使用Anaconda安装;或(2)、使用pip安装。

1)、使用Anaconda安装最为简便,只需要在Anaconda Prompt的命令行中依次输入:
conda config --add channels conda-forge
conda install cdsapi
然后修改.cdsapirc文件(该文件通常在C:\Users\Username folder\  或 C:\用户\Windows用户名\  或$HOME/.cdsapirc),将文件中的url和key替换成自己的,自己的url和key在此网站中:How to use the CDS API | Copernicus Climate Data Store,将右侧黑框中的url和key复制过来即可,注意需要先登录才会显示自己的url和key。


或2)、使用pip安装的话,输入
pip install cdsapi  # for Python 2.7
pip3 install cdsapi # for Python 3
修改.cdsapirc文件的方法同1)中所述。


2、运行批量下载的Python程序
Python程序放在附件中,该程序修改自官网(Retrieve daily ERA5/ERA5-Land data using the CDS API - Copernicus User Support Forum - ECMWF Confluence Wiki),其中每个月存储为一个nc文件,有需要的同学可以自己修改程序。
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  6 14:54:38 2021

@author: Dong
"""

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 =  [
            '2010'
#           ,'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 = [
            # '07', '08'
            '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 = "total_cloud_cover"     
#fraction_of_cloud_cover / Mean surface direct short-wave radiation flux / surface_latent_heat_flux / surface_sensible_heat_flux
#total_cloud_cover / total_precipitation / u_component_of_wind / v_component_of_wind


# 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": "c3s",
             "project": "app-c3s-daily-era5-statistics",
             "version": "master",
             "kwargs": {
                 "dataset": "reanalysis-era5-pressure-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": "0.25/0.25",
               "area":{"lat": [10, 60], "lon": [70, 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()

 


程序中以"reanalysis-era5-single-levels"的"2m_temperature"为例,可以自己更改数据集和变量名等参数,这些参数的获取可以参考官网ERA-5和ERA-5 Land下载页面最下方提供的Show API request按钮或者参考Daily statistics calculated from ERA5 data (copernicus.eu)中的Source code。

——原帖在气象家园——ERA5逐日资料下载方法-数据资料-气象家园_气象人自己的家园 (06climate.com)