Hi,
Is there any efficient way to mask the Xarray’s datasets for a specific country and save that output for further analyses?
GeoCAT suggested two ways “cover” or “clip”. But the output is not suitable to save it as a netcdf file “.to_netcdf”. It happens as the output is kind of raster.
https://geocat-examples.readthedocs.io/en/latest/gallery/Overlays/NCL_overlay_11a.html#sphx-glr-gallery-overlays-ncl-overlay-11a-py
Alternatives are suggested like : Geopandas.clip, rioxarray, salem, regionmask.
xray dataset shapefile mask.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
This file has been truncated. show original
opened 02:49AM - 30 Jul 15 UTC
closed 01:42AM - 29 Dec 16 UTC
usage question
Can we set a shapefile as a mask for each netcdf file and run xray methods for v… alues within the shapefile region?
for example if I want to create a timeseries of monthly mean temperature for 'mystate' from a netcdf file that contains data for the whole country:
filepath = r"DATA/temp/_/_temp.nc"
shapefile = r"DATA/mystate.shp"
ds=xray.open_mfdataset(filepath)
ds_variable=ds['temp']
monthlymean=ds_variable.resample('1MS', dim='time', how='mean')
meanmonthlyofmystate=monthlymean.groupby('time').mean() #add somewhere here the shapefile
meanmonthlyofmystate.to_pandas().plot()
Here we need to install different packages which take lots of time. I would appreciate any idea/link/example on more direct solution.
Here I found the answer; one fantastic example using rioxarray which worked wonderfully!
1 Like