Last active
February 27, 2024 12:36
-
-
Save bennyistanto/9acd336cbcb2e5f91aed1c35dcf6ebd7 to your computer and use it in GitHub Desktop.
Mask NetCDF time series data from a shapefile in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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