Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Last active February 27, 2024 12:36
Show Gist options
  • Save bennyistanto/9acd336cbcb2e5f91aed1c35dcf6ebd7 to your computer and use it in GitHub Desktop.
Save bennyistanto/9acd336cbcb2e5f91aed1c35dcf6ebd7 to your computer and use it in GitHub Desktop.
Mask NetCDF time series data from a shapefile in Python
#!/usr/bin/python
# Source: https://gis.stackexchange.com/a/354798/97103
import geopandas
import rioxarray
import xarray
from shapely.geometry import mapping
CHIRPS_daily = xarray.open_dataarray('Z:\Temp\CHIRPS\NC\chirps-v2.0.1981.days_p05.nc')
CHIRPS_daily.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
CHIRPS_daily.rio.write_crs("epsg:4326", inplace=True)
Shapefile = geopandas.read_file('Z:\Temp\CHIRPS\Subset\idn_bnd_adm2_lite_chirps_diss_a.shp', crs="epsg:4326")
clipped = CHIRPS_daily.rio.clip(Shapefile.geometry.apply(mapping), Shapefile.crs, drop=False)
clipped.rio.to_raster("clipped.tif", compress='LZMA', tiled=True, dtype="int32")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment