如何下载指定时间范围的1°分辨率的FNL数据
个人信息
公众号:气python风雨
关注我获取更多学习资料,第一时间收到我的Python学习资料,也可获取我的联系方式沟通合作
温馨提示
由于可视化代码过长隐藏,可点击运行Fork查看
若没有成功加载可视化图,点击运行可以查看
ps:隐藏代码在【代码已被隐藏】所在行,点击所在行,可以看到该行的最右角,会出现个三角形,点击查看即可
前言
近来有读者来信说我之前写的fnl下载脚本不管用了
我测试一下,确实如此
去官网一看是网址改了,于是有了这期
近来真是挺多新闻的,昨天韩国上演第五共和国复刻活动,今天又刷到琼瑶轻生啥的
那么言归正传
项目目标
本项目旨在解决下载指定时间范围的FNL数据
官方的示例下载脚本如下
#!/usr/bin/env python
"""
Python script to download selected files from rda.ucar.edu.
After you save the file, don't forget to make it executable
i.e. - "chmod 755 <name_of_script>"
"""
import sys, os
from urllib.request import build_opener
opener = build_opener()
filelist = [
'https://data.rda.ucar.edu/d083002/grib2/2024/2024.09/fnl_20240901_00_00.grib2',
'https://data.rda.ucar.edu/d083002/grib2/2024/2024.09/fnl_20240901_06_00.grib2',
'https://data.rda.ucar.edu/d083002/grib2/2024/2024.09/fnl_20240901_12_00.grib2',
'https://data.rda.ucar.edu/d083002/grib2/2024/2024.09/fnl_20240901_18_00.grib2'
]
for file in filelist:
ofile = os.path.basename(file)
sys.stdout.write("downloading " + ofile + " ... ")
sys.stdout.flush()
infile = opener.open(file)
outfile = open(ofile, "wb")
outfile.write(infile.read())
outfile.close()
sys.stdout.write("done\n")
那么我们只需要根据其网址编写字符,变动一下日期即可
脚本
import sys
import os
import datetime
from urllib.request import urlretrieve
def generate_gdas1_urls(start_date, end_date):
"""生成指定日期范围内的GDAS1数据文件URL列表"""
base_url = "https://data.rda.ucar.edu/d083002/grib2/{year}/{year}.{month:02d}/fnl_{year}{month:02d}{day:02d}_{hour:02d}_00.grib2"
urls = []
current_date = start_date
while current_date <= end_date:
# 每6小时一个文件,分别对应00, 06, 12, 18小时的文件
for hour in [0, 6, 12, 18]:
url = base_url.format(year=current_date.year, month=current_date.month, day=current_date.day, hour=hour)
urls.append(url)
print(url)
current_date += datetime.timedelta(days=1) # 每天一个日期
return urls
def download_files(urls):
"""下载文件列表中的所有文件"""
for url in urls:
filename = os.path.basename(url)
print(f"Downloading {filename} ... ", end='', flush=True)
try:
urlretrieve(url, filename)
print("done")
except Exception as e:
print(f"Failed: {e}")
if __name__ == "__main__":
start_date = datetime.datetime(2024, 9, 1) # 下载开始日期
end_date = datetime.datetime(2024, 9, 2) # 下载结束日期
file_urls = generate_gdas1_urls(start_date, end_date)
download_files(file_urls)
传参型脚本
那么在服务器上大家会习惯使用传参进行下载,这样方便很多
用法: python download_gdas1.py <start_date> <end_date>
日期格式: YYYYMMDDHH, for example: 2024090100
import sys
import os
from datetime import datetime, timedelta
from urllib.request import urlretrieve
def generate_gdas1_urls(start_date, end_date):
"""Generate URLs for the 1° resolution GDAS1 files within the specified date range."""
base_url = "https://data.rda.ucar.edu/d083002/grib2/{year}/{year}.{month:02d}/fnl_{year}{month:02d}{day:02d}_{hour:02d}_00.grib2"
urls = []
current_date = start_date
while current_date <= end_date:
# 每6小时一个文件,分别对应00, 06, 12, 18小时的文件
for hour in [0, 6, 12, 18]:
if current_date.hour == hour:
url = base_url.format(
year=current_date.year,
month=current_date.month,
day=current_date.day,
hour=hour
)
urls.append(url)
print(url)
current_date += timedelta(hours=6) # 每6小时一个时间点
return urls
def download_files(urls):
"""Download all files listed in the URLs."""
for url in urls:
filename = os.path.basename(url)
print(f"Downloading {filename} ... ", end='', flush=True)
try:
urlretrieve(url, filename)
print("done")
except Exception as e:
print(f"Failed: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python download_gdas1.py <start_date> <end_date>")
print("Date format should be YYYYMMDDHH, for example: 2024090100")
sys.exit(1)
start_str = sys.argv[1]
end_str = sys.argv[2]
try:
# 解析命令行参数为datetime对象
start_date = datetime.strptime(start_str, "%Y%m%d%H")
end_date = datetime.strptime(end_str, "%Y%m%d%H")
# 确保开始日期不超过结束日期
if start_date > end_date:
print("Error: Start date must not be later than end date.")
sys.exit(1)
file_urls = generate_gdas1_urls(start_date, end_date)
download_files(file_urls)
except ValueError as e:
print(f"Invalid date format: {e}")
print("Date format should be YYYYMMDDHH, for example: 2024090100")
sys.exit(1)
小结
官方的下载链接时不时会变动,根据其网址进行灵活改动即可
下载速度取决于网络如何,文件也不大