Skip to content

Instantly share code, notes, and snippets.

@ashiklom
Created March 19, 2025 19:42
Show Gist options
  • Save ashiklom/3ee696f4a408561390b2f70978bab5df to your computer and use it in GitHub Desktop.
Save ashiklom/3ee696f4a408561390b2f70978bab5df to your computer and use it in GitHub Desktop.
Streaming GMAO data using fsspec and Xarray
#!/usr/bin/env python
import fsspec
from fsspec.implementations.http import HTTPFile, HTTPFileSystem
import xarray as xr
url = "https://portal.nccs.nasa.gov/datashare/gmao/geos-fp/das/Y2025/M03/D18/GEOS.fp.asm.inst1_2d_lfo_Nx.20250318_0000.V01.nc4"
fs = HTTPFileSystem()
file = fs.open(url)
ds = xr.open_dataset(file, engine="h5netcdf")
# Select a value at a location
ds.isel(lon=500, lat=360)["PS"].values
################################################################################
# Eventually, we would build API structured like this
def open_geospf_data_https(start_date, end_date) -> xr.Dataset:
# Generate URLs
if cache:
return read_from_cache()
urls = generate_urls_from_dates(start_date, end_date)
files = [fs.open(url) for url in urls]
# ds = xr.open_mfdataset(files, engine="h5netcdf")
ds = fancy_rust_implementation_of_mfdataset(files) #...
return ds
@fastapi(...)
def get_geospf_timeseries(ds: xr.Dataset, start_date, end_date, lon, lat):
return ds.sel(lon=lon, lat=lat, time=slice(start_date, end_date)).values
@fastapi(...)
def get_geospf_map(ds: xr.Dataset, bbox, datetime):
# Generate URLs
...
@fastapi(...)
def get_geospf_map(datetime, bbox):
# Generate URLs
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment