python批量下载ECMWF欧洲中心数据

ECMWF欧洲中心提供的api只能根据单时间下载,我稍微改动了一下,可以根据时间进行批量下载。详细步骤如下:

1、注册账户获得key。

网址:
https://cds.climate.copernicus.eu/#!/home

将url和key先存储在txt文件中,格式如下:
key
之后,重命名txt文件,同时更改缀。命名改为:.cdsapirc

这里要注意,要把.txt的后缀只改为.(命名其实为.cdsapirc.

文件名最终为:.cdsapirc

将文件放入路径C:\Users***(根据自己的用户名)

2、安装cdsapi
直接 pip install cdsapi

3、下载数据

官方提供的方法:

#!/usr/bin/env python

import cdsapi

c = cdsapi.Client()

c.retrieve(
    'reanalysis-era5-single-levels',
    {
        'product_type':'reanalysis',
        'variable':[
            'total_column_ozone','total_column_water_vapour'
        ],
        'year':'2019',
        'month':'01',
        'day':'23',
        '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'
        ],
        'format':'netcdf'
    },
    'download.nc')

这里只能下载单一时间,而且数据名为 ‘download.nc’。

我希望将我想下载的时间存入txt(时间文件inputpar.txt),直接批量下载,数据存储形式改为CDS+时间,代码如下:

# author = yc

import cdsapi

def  download_cds():

    c = cdsapi.Client()
    lines = open("inputpar.txt", 'r').readlines()

    for i in range(len(lines)):
        if i%2==0:
            fields = lines[i].split(' ') #split data
            year = fields[0]
            month = fields[1]
            day = fields[2]

            print('Download %s-%s-%s data'% (year,month,day))
            c.retrieve(
                'reanalysis-era5-single-levels',
                {
                    'product_type': 'reanalysis',
                    'variable': [
                        'total_column_ozone', 'total_column_water_vapour'
                    ],
                    'year': year,
                    'month': month,
                    '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'
                    ],
                    'format': 'netcdf'
                },
            'CDS%s%s%s'% (year,month,day)+'.nc')
            print('Datafile CDS%s%s%s'% (year,month,day)+'.nc download successful')
        else:
            pass

if __name__ == '__main__':
    download_cds()

结果如下:
下载速度还不错
欢迎交流,祝好!

                                                                                                                                                          2019.07.27
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值