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

**URL:** <https://discourse.julialang.org/t/how-to-read-numerical-values-out-of-a-netcdf-file-and-convert-them-into-logical-values/69153>\
**Category:** General Usage\
**Tags:** question, type, convert\
**Created:** [October 3, 2021, 10:44pm UTC](https://discourse.julialang.org/t/how-to-read-numerical-values-out-of-a-netcdf-file-and-convert-them-into-logical-values/69153 "2021-10-03T22:44:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 3, 2021, 10:44pm UTC](https://discourse.julialang.org/t/how-to-read-numerical-values-out-of-a-netcdf-file-and-convert-them-into-logical-values/69153/1 "2021-10-03T22:44:15Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 4, 2021, 1:29am UTC](https://discourse.julialang.org/t/how-to-read-numerical-values-out-of-a-netcdf-file-and-convert-them-into-logical-values/69153/2 "2021-10-04T01:29:23Z")

</div>

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

M = .!iszero.(M);

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 4, 2021, 8:10am UTC](https://discourse.julialang.org/t/how-to-read-numerical-values-out-of-a-netcdf-file-and-convert-them-into-logical-values/69153/3 "2021-10-04T08:10:36Z")

</div>

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?”

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 4, 2021, 5:01pm UTC](https://discourse.julialang.org/t/how-to-read-numerical-values-out-of-a-netcdf-file-and-convert-them-into-logical-values/69153/4 "2021-10-04T17:01:07Z")

</div>

Maybe you are interested in [GMT.jl](https://github.com/GenericMappingTools/GMT.jl)’s program [`grdlandmask`](https://docs.generic-mapping-tools.org/dev/grdlandmask.html) (Note, I’m pointing to the _hardcore_ GMT syntax manual because the Julia wrapper still does not have one for this module).

Example

```julia
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)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c6fa0d2a65bf518128bfe2cc6b19c9fdeee42a6.png)
