爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 3965|回复: 5

[netcdf] ERA5逐日数据下载,python脚本报错中断问题解决方案

[复制链接]

新浪微博达人勋

发表于 2022-3-18 11:14:17 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
第一次发帖,有不对的地方欢迎指出和讨论。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2022-3-18 11:22:07 | 显示全部楼层
这两天需要下载ERA5逐日数据,有幸看到了iovedys大佬的逐日数据资料下载方法,详见https://bbs.06climate.com/forum. ... &highlight=ERA5 ,python脚本可以工作,但程序偶尔会报错,一报错下载就中断,可能是网络问题,需要手动重下。C:\Users\DELL\Desktop\1.pngC:\Users\DELL\Desktop\2.png
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-3-18 11:24:10 | 显示全部楼层
这图咋发不出去...
主要报错截图如下
1.png
2.png
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-3-18 11:29:18 | 显示全部楼层
解决方案:
# -*- coding: utf-8 -*-
"""
下载ERA5 daily数据
"""

import cdsapi
import requests
import time
# 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 = '10m_u_component_of_wind'

# Select the required statistic, valid names given in link above
stat = "daily_mean"                                                            # daily_maximum, daily_minimum, daily_mean

# Loop over years and months

for yr in years:
    for mn in months:
        while True:
            try:
                result = c.service(
                "tool.toolbox.orchestrator.workflow",
                params={
                     "realm": "c3s",
                     "project": "app-c3s-daily-era5-statistics",
                     "version": "master",
                     "kwargs": {
                         "dataset": 'reanalysis-era5-land',
                         #"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.5/0.5",                                         # 0.1/0.1, 0.25/0.25, 0.5/0.5, 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()

            except:
                print('下载出错4S后重试')
                print('------------------------------------------------------------------------------------')
                time.sleep(4)
                continue
            else:
                time.sleep(4)
                break                      #下载成功就跳出循环
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-3-18 11:32:35 | 显示全部楼层
主要就是while true: + try: /except: 语句来控制下载,报错了就重新提交下载直到成功,昨天挂了一晚上,下了14年的数据,速度确实不快,但可以稳定运行了
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-3-18 11:34:43 | 显示全部楼层
我也是python新手,大伙有更好的解决方案也欢迎交流
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表