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?
Asked
Active
Viewed 689 times
0

ClimateUnboxed
- 7,106
- 3
- 41
- 86

Amr Talaat
- 81
- 2
- 10
2 Answers
2
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

Charlie Zender
- 5,929
- 14
- 19
-
Thanks, it works successfully. However, masking is being applied only on the first time step of the variable `temp` . To clarify, the `mask.nc` has only one timestep, while the `data.nc` has multi time-steps . So, how can I apply the masking for the whole time-steps of the `temp` variable in `data.nc` – Amr Talaat May 27 '22 at 13:09
-
As the manual explains, where() only operates on a hyperslab of the same size as the variable in the condition statement. So you first need to broadcase tmask to be the same size as temp. Clarified above. – Charlie Zender May 29 '22 at 18:25
1
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.

ClimateUnboxed
- 7,106
- 3
- 41
- 86