I have the following shapefile and netcdf file.
I would like to extract data from the netcdf file that are contained within the boundaries of the shapefile.
Do you have any suggestion on how I can achieve this?
The shapefile corresponds to SREX region 11 North Europe (NEU) and the netcdf file is an example of CMIP6 climate model data output (ua variable). My desired output has to be in netcdf format.
Update
So far I tried to create a netcdf mask using NCL and CDO, and apply this mask to the original netcdf dataset. Here below the steps (and NCL scripts):
#################
## remove plev dimension from netcdf file
cdo --reduce_dim -copy nc_file.nc nc_file2.nc
## convert longitude to -180, 180
cdo sellonlatbox,-180,180,-90,90 nc_file2.nc nc_file3.nc
## create mask
ncl create_nc_mask.ncl
## apply mask
cdo div nc_file3.nc shape1_mask.nc nc_file4.nc
#################
The output is almost correct. See picture below. But the southern boundaries of the shapefile (SREX 11, NEU) are not captured correctly. So I suppose there is something wrong in the NCL script that generates the netcdf mask.
Re-using some old scripts/code, I quickly came up with this for a Python solution. It basically just loops over all grid points, and checks whether each grid point is inside or outside the polygon from the shape file. The result is the variable
mask
(array withTrue/False
), which can be used to mask your NetCDF variables.Note: this uses Numba (all the
@jit
lines) to accelerate the code, although that is not really necessary in this case. You can just comment them out if you don't have Numba.EDIT
To write the mask to NetCDF, something like this can be used: