You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用Python requests库下载NOAA的GFS与CFSV2数据?

Great to hear your NOAA NCDC API script is working smoothly! For accessing GFS or CFSv2 data directly via Python (similar to how you use Google Earth Engine), here are some reliable options that let you skip the GEE interface:

1. NOAA NOMADS API (Official Source)

Since GFS and CFSv2 are both NOAA products, their NOMADS (National Operational Model Archive and Distribution System) API is the most straightforward official option. You can fetch forecast or reanalysis data directly via HTTP requests, and parse it with libraries like xarray or pandas.

Here's a quick example for GFS data:

import requests
import xarray as xr
from io import BytesIO

# Fetch GFS 0.25-degree resolution forecast data (2m temperature example)
url = "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl?file=gfs.t00z.pgrb2.0p25.f000&lev_2_m_above_ground=on&var_TMP=on&dir=%2Fgfs.20240520%2F00%2Fatmos"

response = requests.get(url)
# Load GRIB data into an xarray Dataset
ds = xr.open_dataset(BytesIO(response.content), engine="cfgrib")
# Convert to pandas DataFrame if needed
df = ds.to_dataframe().reset_index()
print(df.head())
  • Notes: You can customize URL parameters (resolution, variables, forecast hour, date) by navigating the NOMADS web interface first to generate the right filter link. For CFSv2, use the dedicated NOMADS CFS endpoints (look for cfs directories in the NOMADS structure).

2. xarray + cfgrib (Simplified Data Handling)

If you prefer a streamlined workflow for meteorological data, xarray paired with cfgrib (a GRIB file parser) lets you directly load GFS/CFSv2 data without manual HTTP setup in many cases:

import xarray as xr

# Example: Load CFSv2 reanalysis surface precipitation rate
cfs_url = "https://nomads.ncep.noaa.gov/pub/data/nccf/com/cfs/prod/cfs.20240520/6hrly/grib2/geavg.t06z.pgrb2a.0p50.f000"
ds = xr.open_dataset(
    cfs_url, 
    engine="cfgrib", 
    backend_kwargs={"filter_by_keys": {"typeOfLevel": "surface", "shortName": "prate"}}
)
print(ds)
  • This approach shines for working with spatial-temporal data natively in xarray, which is far more powerful than pandas for weather/climate datasets.

3. PyNIO (NOAA's Native Data Library)

NOAA's PyNIO library is built specifically for reading/writing meteorological data formats like GRIB and netCDF. It’s more low-level but offers full support for CFSv2 and GFS data:

import PyNIO as Nio

# Open a CFSv2 GRIB file directly from the NOAA server
file_path = "https://nomads.ncep.noaa.gov/pub/data/nccf/com/cfs/prod/cfs.20240520/6hrly/grib2/geavg.t06z.pgrb2a.0p50.f000"
data = Nio.open_file(file_path, mode="r")
# Extract precipitation rate variable
prate = data.variables["prate"][:]
print(prate.shape)
  • Note: PyNIO requires extra setup (like installing GRIB support dependencies), but it’s a robust choice for NOAA-specific datasets.

All these methods let you pull GFS/CFSv2 data directly into Python without relying on Google Earth Engine, matching the workflow you use for NCDC data.

内容的提问来源于stack exchange,提问作者user88484

火山引擎 最新活动