如何在Python中发送自动下载请求并退出下载?

2024-05-06 12:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python向ERA5(Data description)发送下载请求,ERA5(Data description)承载气象数据供下载。我的目标是发送一个请求,该请求将为我想要下载的数据生成URL,但不会让整个下载过程发生,因为我不想在互联网上花费时间。一旦我有了URL,我就可以使用我的institute下载服务并使用wget -i url.txt下载

然而,问题是,我在本地机器(利用本地互联网)上运行的代码将一直运行,直到下载完成,这与目的背道而驰,因为我只希望发送下载请求以生成URL,而不是下载数据本身

到目前为止,我一直尝试使用有时间限制的while循环,但一旦发送请求,它就不遵守循环。 我的代码如下:

import cdsapi  # required module for API request
import time    # for tracking time
years = ['1980','1981','1982','1984','1985','1986','1989','1990','1991','1992','1993','1995','1996','1997','1998','1999','2000','2001','2003','2004','2005','2006','2007','2008','2010','2011','2012','2013']   # required years
variables = ['u_component_of_wind','v_component_of_wind','geopotential', 'temperature', 'vertical_velocity','vorticity']  # required meteorological variables
def callrequest(year,vary,counter):    # function which take year, variable and a counter value
    if counter == 0:   # to make sure only one request is sent for a combination of year and variable
        c = cdsapi.Client()   # these are required steps and info for request
        c.retrieve(
            'reanalysis-era5-pressure-levels',
        {
            'product_type': 'reanalysis',
            'variable': vary,        # stating which variable is required out of all variables
            'pressure_level': [
                '50', '100', '150',
                '200', '250', '300',
                '350', '400', '450',
                '500', '550', '600',
                '650', '700', '750',
                '800', '850', '900',
                '950', '1000',
            ],
            'year': year,   # stating which year out of all years
            'month': [
                '01', '02', '03',
                '04', '05', '06',
                '07', '08', '09',
                '10', '11', '12',
            ],
            'day': [
                '01', '02', '03',
                '04', '05', '06',
                '07', '08', '09',
                '10', '11', '12',
                '13', '14', '15',
                '16', '17', '18',
                '19', '20', '21',
                '22', '23', '24',
                '25', '26', '27',
                '28', '29', '30',
                '31',
            ],
            'time': [
                '00:00', '06:00', '12:00',
                '18:00',
            ],
            'area': [
                40, 50, -10,
                120,
            ],

            'format': 'netcdf',
            },
            'download.nc')


duration = 0.001
present_time = time.time()
counter = 0
for i in range(len(years)):
    for j in range(len(variables)):
       counter = 0
       while time.time()<present_time + duration:  # an attempt to run while loop for some time only and process only one request
           callrequest(years[i],variables[j],counter)
           counter = counter + 1

现行守则的缺点:

  1. while循环确保请求已发送,但不会在给定的持续时间内终止,终端将一直运行到下载完成

  2. 不确定是否会选择下一个年份和变量组合

我想要的代码将使用一个值Year和Variable,发送请求,以便在处理和下载请求之前生成并终止URL(发送后大约需要30-40分钟),然后跳到下一年和Variable组合。我将感谢所有帮助我并使它变得更好的人


Tags: andof数据代码urlfortimerequest