This question is already posed here: Create a NetCDF file with data masked to retain land points only
Solution 1:
So basically you can build a land-sea mask using the built in topography function, and then set all the sea points to missing:
cdo -f nc setctomiss,0 -gtc,0 -remapcon,your_data_file.nc -topo seamask.nc
- topo : generates a topography file
- remapcon : remaps it to your input
file resolution
- gtc,0 : sets to 1 for all topographical points above
sea level, 0 otherwise
- setctomiss: the zeros for the sea points are
converted to "missing"
You can now use this to mask your datafile:
cdo mul your_data_file.nc seamask.nc masked_datafile.nc
However, in some circumstances I have found that the remapping process leaves traces of "ocean" data around the edges, in this case to be safer you can use the second method:
Solution 2:
Download the netcdf data file for "distance to ocean" at 1km resolution from this thredds server: https://pae-paha.pacioos.hawaii.edu/thredds/ncss/dist2coast_1deg_land/dataset.html
Then you can mask out any points within a certain distance of the ocean to play it safe, at the expense of possibly masking out a small amount of land data.
I remapped the distance file to the target resolution first:
cdo remapbil,your_data.nc distance.nc remap_dist.nc
then mask (e.g. in this case all points within 5km of the coast, sea points are already "missing" in this file) and multiply
cdo mul your_data.nc -gtc,5 remap_dist.nc masked_data.nc
As said, this is a little safer, a little more longwinded, but may mask some land data.