0

Let's say I have made a netCDF file, which has a lot of zeros. I want to apply a sea mask to the data, such that only the regions in the sea are really masked and the data on the land is retained.

The data that I have has a lot of zeros on land (which is correct), but also has a lot of zeros in the sea (which is not correct).

I could have used cdo setmissval,nan input.nc output.nc but that would have also changed the value on the land to NaN.

Does someone have any (good) solution to that?

Adrian Tompkins
  • 4,261
  • 1
  • 23
  • 56
Ep1c1aN
  • 495
  • 4
  • 17
  • 1
    Can you provide some example data? I assume it is not easy to do with one-liner, but in any case, it would already help if you have also the seamask/landmask data available. – msi_gerva Jun 18 '19 at 14:36
  • Does this answer your question? [Create a NetCDF file with data masked to retain land points only](https://stackoverflow.com/questions/39058260/create-a-netcdf-file-with-data-masked-to-retain-land-points-only) – Adrian Tompkins Jan 14 '20 at 08:05
  • @Ep1c1aN, I know I was late, but did the answer below help? – Adrian Tompkins Jan 17 '20 at 10:52
  • Yes, it did (Solution 1). I did not try Solution 2 though, but I suppose it would be more precise since it is at 1km resolution. Thank you for your help. – Ep1c1aN Jan 18 '20 at 12:50

1 Answers1

0

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.

Adrian Tompkins
  • 4,261
  • 1
  • 23
  • 56