Project

General

Profile

Convert Mask to Boolean and then apply to data

Added by Oliver Angelil almost 8 years ago

This is a 2-step question. I'd like to know if this is possible with CDO:

1) The Mask: I'd first like to convert a 2D netcdf file of floats to boolean. This file consists of floats from 0 to 1 depending on the fraction of land within each grid cell. I'd like all values above 0.5 to be False (unmasked) and all values below to be True (masked out).
2) Apply to data: I'd then like to apply this mask to, say a netcdf file (which has data everywhere) consisting of 3D arrays of temperature data, such that I can take the field mean of the unmasked region.

Are either of these steps possible? This would be handy because currently, to calculate area-weighted field means of any irregular shaped region, my colleagues and I perform this manually in Python by generating "weighting matrices", and then applying this to masked numpy arrays of the data.

Regards,
Oliver


Replies (4)

RE: Convert Mask to Boolean and then apply to data - Added by Oliver Angelil almost 8 years ago

I believe I answered my own question, although please let me know if there's a better way.

This will create a netcdf file that only includes values where 0.5 or more over each grid cell is land:

cdo setmissval,0 -mul -gec,0.5 mask.nc data.nc masked_data.nc

Now I could view a fieldmean:

cdo output -fldmean masked_data.nc

RE: Convert Mask to Boolean and then apply to data - Added by Uwe Schulzweida almost 8 years ago

Your CDO chain is ok, as far as there are no missing values in your data file. Here is an alternative version:

cdo div data.nc -gec,0.5 mask.nc  masked_data.nc
The advantage of this version is that you don't need to take care of missing values.

RE: Convert Mask to Boolean and then apply to data - Added by Ray Bell about 7 years ago

Hi Oliver,

I am doing to do something similar and I am wondering what your mask.nc file and how you created it?

Cheers,
Ray

    (1-4/4)