How to read numerical values out of a NetCDF file and convert them into logical values?

I have a land mask that is created using Matlab. Because NetCDF does not allow the use of “logical” data type, I had to store them as float values.

My question is in Julia, how do I convert these 1 and 0 values on a m x n grid back into its original logical values? These values will be used as a land mask. For example, I would have a gridded value Z in the same m x n sized grid. If these logical values are stored in a variable named M. I want to be able to do the below:

Z(M) = NaNs.

Many thanks!

I figured it out myself. The below will turn the numerical values into logical values.

M = .!iszero.(M);

FWIW, the standard way to obtain a value of a certain type T is to call the type’s constructor, but in this case: BitMatrix(M) performs much slower than using iszero().

You should probably change the OP title to “How to convert matrix of integers to boolean?”

Maybe you are interested in GMT.jl’s program grdlandmask (Note, I’m pointing to the hardcore GMT syntax manual because the Julia wrapper still does not have one for this module).


using GMT

# Create a global mask at crude shoreline resolution and with a grid increment of 1 degree 
#  "earth_relief_01d" grid data is automatically provided by GMT
lmask = grdlandmask(region="@earth_relief_01d", resolution=:crude, mask_geog=(NaN,1));

# Load the global grid so we can mask it after
earth = gmtread("@earth_relief_01d", grid=true);

# Mask and display it
masked = earth * lmask;
imshow(masked, shade=true)