NCO : Masking netcdf file using another netcdf mask file with (0 - 1) values

963 views Asked by At

I have two .nc files data.nc and mask.nc. Where: data.nc contain a variable called temp unmasked, while mask.nc contain the mask within a variable called tmask with (0 - 1) values. Using NCO, how can I apply the masking for the data.nc file, such that zero mask values are set to missing, and unity mask values are retained unchanged?

2

There are 2 answers

2
Charlie Zender On

It's unclear what you wish to do with the mask. Here is a common procedure, use the mask to replace the actual values with missing values:

ncks -A -v tmask mask.nc data.nc
ncap2 -s 'where(tmask == 0) temp=temp.get_miss()' data.nc out.nc

Documentation for where and get_miss is in the manual.

If temp has more records than tmask then make the where() condition operate on a copy of tmask that has been broadcast to the size of temp:

ncap2 -s '*big_mask=0*temp+tmask;where(big_mask == 0) temp=temp.get_miss()' data.nc out.nc
0
ClimateUnboxed On

To do the same operation in cdo you could try this, which sets zero to missing in the mask first before taking the product:

cdo setctomiss,0 mask.nc maskm.nc
cdo mul data.nc maskm.nc masked_data.nc

cdo automatically repeats the mask to make it the same length in time as the data file, known as data "broadcasting".

I have a youtube video on masking here for further guidance, and other material on temporal broadcasting.