import xarray as xr, salem cntry_name = 'Thailand' shdf = salem.read_shapefile(salem.get_demo_file('world_borders.shp')) shdf = shdf[ shdf['CNTRY_NAME']==cntry_name ] ofile = f'chirps.p05.{cntry_name}.nc' data_name = 'precip' # mask the grids outside of the country da = xr.open_dataset(ofile)[data_name].salem.roi(shape=shdf) # save the masked data to the ofile da.to_dataset().to_netcdf(ofile.replace(cntry_name, cntry_name+'.masked'), unlimited_dims='time')