python downloads ECMWF European Center data in batches

The api provided by ECMWF European Center can only be downloaded in a single time. I have changed it a little and can download it in batches according to the time. Detailed steps are as follows:

1. Registered account obtains key.

Website:
https://cds.climate.copernicus.eu/#!/home

The url and key are stored in the txt file in the following format:

Then rename the txt file and change the affix. Name changed to:.cdsapirc

Note here that the suffix of.txt should only be changed to. (The name is actually. cdsapirc.)

The final file name is:.cdsapirc

Put the file in the path C: Users*** (according to your user name)

2. Installing cdsapi
Direct pip install cdsapi

3. Download data

Officially provided methods:

#!/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')

Here you can only download a single time, and the data name is‘ download.nc'.

I want to save the time I want to download into TXT (time file inputpar.txt), download directly in batches, and change the form of data storage to CDS + time. The code is as follows:

# 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()

The results are as follows:

Welcome to exchange, good luck!

                                                                                                                                                          2019.07.27

Keywords: Programming pip Python

Added by kobmat on Wed, 31 Jul 2019 18:34:56 +0300