Project

General

Profile

multiplying 2D mask with 3D dataset

Added by Anne Moree about 2 years ago

Hello CDO,

I am multiplying (cdo mul) a 2D mask (lonxlat, values are 1 or missing) with a 3D dataset (lonxlatxdepth). I would thus expect the result to only have values where the mask is 1. However I find that some values exist also in other places and I have not been able to find a good reason why. Could there be a problem with cdo mul?

I do cdo mul woa18_00_pO2_mbar_05deg.nc presence_600646.nc out.nc (see the attached files)

If you look at deeper depths (for example index 40), notice that the black sea shows in the out.nc data but not in the mask. Here I noted that the values of the 3D dataset are zero. Is thus zero*NaN=zero according to operator mul? If I do setctomiss,0 the issue is gone but I would prefer not to do that as values of 0 should be able to exist also within the masked region.

Thank you in advance for your help,
Anne


Replies (4)

RE: multiplying 2D mask with 3D dataset - Added by Uwe Schulzweida about 2 years ago

Hello Anne,

Yes this may not be intuitive but the definition is zero*NaN=zero. When working with masks it is therefore often useful to use the div operator instead of mul. The definition is zero/NaN=NaN (see: https://code.mpimet.mpg.de/projects/cdo/embedded/index.html#x1-400001.9).

Cheers,
Uwe

RE: multiplying 2D mask with 3D dataset - Added by Anne Moree about 2 years ago

Hello Uwe,

Thank you so much for this clarification. This is definitely not intuitive and easily overlooked when taking e.g. a fldsum of a mask*field. I find the table you refer to hard to understand but I will make sure to use div when working with masks from now on! I think it would be useful to stress this in the User Guide in the arithmetic section.

Thank you again,

Anne

RE: multiplying 2D mask with 3D dataset - Added by Ralf Mueller about 2 years ago

hi Anne!

just a side note: What I found also very useful is using the div operator to create missing values by division of zero (<any number>/0 = NaN). This can be very helpful when working with 0-1-masks.

cheers
ralf

RE: multiplying 2D mask with 3D dataset - Added by Anne Moree about 2 years ago

Hi Ralf,

Thank you for thinking along! Yes this is what I tried to do with mul in a sense (such that I could do e.g. fldmean afterwards and I would get the mean only in the mask=1 areas), but will be using div or Python's numpy from now on.

Cheers,
Anne

    (1-4/4)