批量下载历史预报数据
以下载某次历史预报结果为例,涵盖行政区划、自定义点位、区域平均三种方式
需求:批量下载某一次历史预报运行的结果数据,用于预报检验、模型评估等场景。
历史预报数据模型:archive_ifs(25km,360小时)
支持的历史预报数据源及要素:历史预报数据API
全部支持的逐日数据要素查看:逐日要素支持
以 Python 代码为例,分四步完成:
note
历史预报数据的时间范围使用 forecast 类型,需指定预报的起报时间(start_hour)和预报时效(forecast_hours),与历史数据使用的 date_range 类型不同。方式一:使用行政区划
适合按省/市/区县行政区划批量下载,adcode 可从平台提供的 CSV 文件中查询:省/直辖市使用 province.csv,地级市使用 city.csv,区县使用 district.csv。
第一步:获取区划 adcode 列表
import io
import time
import zipfile
import requests
import pandas as pd
API_KEY = "your_api_key"
BASE_URL = "https://api.mirror-earth.com"
# 下载区县表(也可替换为 province.csv / city.csv)
res = requests.get("https://open.mirror-earth.com/district.csv")
res.encoding = "utf-8"
df = pd.read_csv(io.StringIO(res.text))
adcodes = df["adcode"].astype(str).tolist()
print(f"共 {len(adcodes)} 个区县")
第二步:创建批量下载任务
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json",
}
payload = {
"domain": "archive_ifs",
"hourly": ["temperature_2m", "precipitation"],
"daily": [],
"monthly": [],
"time_range": {
"type": "forecast",
"start_hour": "2025-05-15T00:00", # 起报时间(世界时)
"forecast_hours": 48, # 预报时效(小时数)
},
"area": {
"type": "point",
"adcodes": adcodes,
},
"timezone": "Asia/Shanghai",
"area_average": False,
}
res = requests.post(f"{BASE_URL}/api/tasks/export", json=payload, headers=headers)
res.raise_for_status()
task_id = res.json()["data"]["task_id"]
print(f"任务已创建,task_id: {task_id}")
方式二:使用自定义点位
适合已有一批经纬度坐标的场景,id 和 name 为可选字段,用于结果关联和标识。
第一步:准备自定义点位列表
import time
import zipfile
import requests
API_KEY = "your_api_key"
BASE_URL = "https://api.mirror-earth.com"
locations = [
{"id": "1", "name": "北京", "lon": 116.4, "lat": 39.9},
{"id": "2", "name": "上海", "lon": 121.5, "lat": 31.2},
{"id": "3", "name": "广州", "lon": 113.3, "lat": 23.1},
]
print(f"共 {len(locations)} 个自定义点位")
第二步:创建批量下载任务
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json",
}
payload = {
"domain": "archive_ifs",
"hourly": ["temperature_2m", "precipitation"],
"daily": [],
"monthly": [],
"time_range": {
"type": "forecast",
"start_hour": "2025-05-15T00:00",
"forecast_hours": 48,
},
"area": {
"type": "point",
"locations": locations,
},
"timezone": "Asia/Shanghai",
"area_average": False,
}
res = requests.post(f"{BASE_URL}/api/tasks/export", json=payload, headers=headers)
res.raise_for_status()
task_id = res.json()["data"]["task_id"]
print(f"任务已创建,task_id: {task_id}")
方式三:使用区域平均
将行政区划内所有格点的数值取空间平均,输出为一行一个区划的时序数据,适合区域级预报精度评估。仅支持以 adcodes 指定范围。
第一步:创建批量下载任务
import time
import zipfile
import requests
API_KEY = "your_api_key"
BASE_URL = "https://api.mirror-earth.com"
# 以各省为例(可从 province.csv 中获取全部 adcode)
adcodes = ["110000", "310000", "440000"] # 北京、上海、广东
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json",
}
payload = {
"domain": "archive_ifs",
"hourly": ["temperature_2m", "precipitation"],
"daily": [],
"monthly": [],
"time_range": {
"type": "forecast",
"start_hour": "2025-05-15T00:00",
"forecast_hours": 48,
},
"area": {
"type": "point",
"adcodes": adcodes,
},
"timezone": "Asia/Shanghai",
"area_average": True, # 开启区域平均
}
res = requests.post(f"{BASE_URL}/api/tasks/export", json=payload, headers=headers)
res.raise_for_status()
task_id = res.json()["data"]["task_id"]
print(f"任务已创建,task_id: {task_id}")
第三步:轮询等待任务完成
以上三种方式创建任务后,均使用相同方式轮询进度:
output_file = None
while True:
res = requests.get(f"{BASE_URL}/api/tasks/{task_id}/status", headers=headers)
res.raise_for_status()
data = res.json()["data"]
status = data["status"]
progress = data.get("progress_percent", 0)
print(f"任务状态:{status},进度:{progress}%")
if status == "completed":
output_file = data["output_file"]
break
elif status in ("failed", "cancelled"):
raise RuntimeError(f"任务异常,状态:{status}")
time.sleep(10)
第四步:下载并解压任务结果
download_url = f"{BASE_URL}/user_downloads/{output_file}"
res = requests.get(download_url, headers=headers, stream=True)
res.raise_for_status()
zip_path = "archive_forecast_result.zip"
with open(zip_path, "wb") as f:
for chunk in res.iter_content(chunk_size=8192):
f.write(chunk)
with zipfile.ZipFile(zip_path, "r") as z:
z.extractall("archive_forecast_result")
print("下载完成,数据已解压到 archive_forecast_result/")
切换其他历史预报模型
只需修改 domain 字段即可切换数据源,全部历史预报模型请查看 可用数据模型:
| domain | 来源 | 分辨率 | 最大预报时效 | 可用时间范围 |
|---|---|---|---|---|
archive_ifs | 欧洲(ECMWF) | 25km | 360h | 2023-01-01 至今 |
archive_hres | 欧洲(ECMWF) | 9km | 360h | 2019-01-01 至今 |
archive_gfs | 美国(NOAA) | 25km | 384h | 2023-07-01 至今 |
archive_cma | 中国(CMA) | 12.5km | 108h | 2025-09-19 至今 |
archive_icon | 德国(DWD) | 11km | 168h | 2025-09-19 至今 |
archive_gem | 加拿大(GEM) | 15km | 240h | 2025-09-19 至今 |
archive_aifs | 欧洲 AI 模型 | 25km | 360h | 2025-09-19 至今 |
archive_graphcast | 美国 AI 模型 | 25km | 384h | 2025-09-19 至今 |