from queue import Queue
from threading import Thread
import cdsapi
from time import time
import datetime
import os
def downloadonefile(riqi):
ts = time()
filename="./temperature_pressure/era5.TEM."+riqi+".grib"
if(os.path.isfile(filename)): #如果存在文件名则返回
print("ok",filename)
else:
print(filename)
c = cdsapi.Client()
c.retrieve(
'reanalysis-era5-pressure-levels',
{
'product_type' : 'reanalysis',
'format': 'grib',
'pressure_level': [
'50', '70', '100',
'125', '150', '175',
'200', '225', '250',
'300', '350', '400',
'450', '500', '550',
'600', '650', '700',
'750', '775', '800',
'825', '850', '875',
'900', '925', '950',
'975', '1000',
],
'variable': 'temperature',
'year' : riqi[0:4],
'month' : riqi[-4:-2],
'day' : riqi[-2:],
'time':['00:00','06:00','12:00','18:00',],
'area' : [60, -10, 50, 2], # North, West, South, East. Default: global
'grid' : [1.0, 1.0], # Latitude/longitude grid: east-west (longitude) and north-south resolution (latitude). Default: 0.25 x 0.25
},
filename)
下载脚本
class DownloadWorker(Thread):
def init(self, queue):
Thread.init(self)
self.queue = queue
def run(self):
while True:
# 从队列中获取任务并扩展tuple
riqi = self.queue.get()
downloadonefile(riqi)
self.queue.task_done()
主程序
def main():
起始时间
ts = time()
起始日期
begin = datetime.date(2000,1,1)
end = datetime.date(2022,6,30)
d=begin
delta = datetime.timedelta(days=1)
建立下载日期序列
links = []
while d <= end:
riqi=d.strftime("%Y%m%d")
links.append(str(riqi))
d += delta
创建一个主进程与工作进程通信
queue = Queue()
20191119更新# 新的请求规则 https://cds.climate.copernicus.eu/live/limits
注意,每个用户同时最多接受4个request https://cds.climate.copernicus.eu/vision
创建四个工作线程
for x in range(4):
worker = DownloadWorker(queue)
#将daemon设置为True将会使主线程退出,即使所有worker都阻塞了
worker.daemon = True
worker.start()
将任务以tuple的形式放入队列中
for link in links:
queue.put((link))
让主线程等待队列完成所有的任务
queue.join()
print('Took {}'.format(time() - ts))
if name == 'main':
main()
网友评论