masknetcdfnetcdf4cdo-climatetopography

How to apply a Sea mask over a NetCDF file using CDO


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?


Solution

  • 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
    

    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, the underlying dataset is only at 0.5 degree resolution, and there are issues in some regions that are below sea level. If you prefer a more accurate answer, 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.