83 lines
1.4 KiB
Markdown
83 lines
1.4 KiB
Markdown
# NavSea Weather Server
|
||
Step-2 测试:GFS Downloader
|
||
|
||
创建文件:
|
||
|
||
gfs_downloader.py
|
||
|
||
------------------------------------------------
|
||
|
||
import os
|
||
import requests
|
||
from datetime import datetime
|
||
|
||
BASE_URL = "https://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod"
|
||
|
||
FORECAST_HOURS = [
|
||
0,3,6,9,12,15,18,21,24,
|
||
27,30,33,36,39,42,45,
|
||
48,51,54,57,60,63,66,
|
||
69,72
|
||
]
|
||
|
||
OUTPUT_DIR = "data/grib"
|
||
|
||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||
|
||
|
||
def get_cycle():
|
||
|
||
now = datetime.utcnow()
|
||
|
||
hour = (now.hour // 6) * 6
|
||
|
||
cycle = f"{hour:02d}"
|
||
|
||
date = now.strftime("%Y%m%d")
|
||
|
||
return date, cycle
|
||
|
||
|
||
def download_file(url, path):
|
||
|
||
if os.path.exists(path):
|
||
print("skip", path)
|
||
return
|
||
|
||
print("downloading", url)
|
||
|
||
r = requests.get(url, stream=True)
|
||
|
||
if r.status_code != 200:
|
||
print("failed", url)
|
||
return
|
||
|
||
with open(path, "wb") as f:
|
||
for chunk in r.iter_content(1024*1024):
|
||
f.write(chunk)
|
||
|
||
|
||
def main():
|
||
|
||
date, cycle = get_cycle()
|
||
|
||
print("cycle:", date, cycle)
|
||
|
||
for fh in FORECAST_HOURS:
|
||
|
||
fh_str = f"{fh:03d}"
|
||
|
||
filename = f"gfs.t{cycle}z.pgrb2.0p25.f{fh_str}"
|
||
|
||
url = f"{BASE_URL}/gfs.{date}/{cycle}/atmos/{filename}"
|
||
|
||
output = os.path.join(
|
||
OUTPUT_DIR,
|
||
f"{date}_{cycle}_f{fh_str}.grib2"
|
||
)
|
||
|
||
download_file(url, output)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |