# Grdmath: invert NAN operator

**URL:** <https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593>\
**Category:** Q&A\
**Created:** [January 9, 2023, 8:56am UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593 "2023-01-09T08:56:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [January 9, 2023, 8:56am UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/1 "2023-01-09T08:56:15Z")

</div>

Problem: I need to set everything _except_ a specified value to NaN to be able to dump relevant grid nodes with `grd2xyz -s`.

- Using [grdmath](https://docs.generic-mapping-tools.org/dev/grdmath.html), is there a way of “inverting” the NAN operator (NaN if A == B, else A) such that the result is “A if A == B, else NaN”?

- Alternatively, if [grd2xyz](https://docs.generic-mapping-tools.org/dev/grd2xyz.html) had an argument/option/directive to only output grid nodes containing a specified value. I don’t think this exists (`grep`-ing kind of works, but clutters). A [-s](https://docs.generic-mapping-tools.org/dev/gmt.html#s-full) idea could be to add: To only output grid nodes containing the number -5: `grd2xyz grid.nc -s+r+n-5`.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [January 9, 2023, 9:13am UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/2 "2023-01-09T09:13:09Z")

</div>

Maybe -qo can help, e.g. -qo-5.00000001/-4.9999999+c2 or similar

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [January 9, 2023, 9:30am UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/3 "2023-01-09T09:30:53Z")

</div>

If you want to turn -5 in a grid to NaN then I would do

`gmt grdmath my.grd DUP -5 NEQ 0 NAN = new.grd`

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [January 9, 2023, 9:49am UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/4 "2023-01-09T09:49:12Z")

</div>

Thanks for good tips, Paul. I ended up using a simple **EQ** to get the values I want.

---

<div class="post-metadata">

**Author:** ![John\_Geodesy](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/john_geodesy/32/116_2.png) [@John\_Geodesy](https://forum.generic-mapping-tools.org/u/John_Geodesy)\
**Post date:** [August 10, 2023, 3:39pm UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/5 "2023-08-10T15:39:29Z")

</div>

I must’ve missed this one, because the description for the NAN option, in grdmath, has changed.

Was: `NAN: NaN if A = B; else A`  
Now: `NAN: Replace NaNs in A with values from B`

That description indicates a very different operation.

Did the NAN operator change? If not, then please update the on-line documentation to reflect what it really does.

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [August 10, 2023, 4:28pm UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/6 "2023-08-10T16:28:49Z")

</div>

I have a suspicion. Will check later.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [August 10, 2023, 4:50pm UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/7 "2023-08-10T16:50:35Z")

</div>

Code comments remain

/\*OPERATOR: NAN 2 1 NaN if A == B, else A. \*/

and no change in the code. But docs have changed, maybe during the reformatting of table?

Replace NaNs in A with values from B

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [August 10, 2023, 8:22pm UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/8 "2023-08-10T20:22:21Z")

</div>

Hmm, the DNAN operator was introduced at 8/12/2015

SHA-1: c7a233b4a9c37eb1061f719760780f845704f604

- Added DENAN operator to g\*math

and had already the `Replace NaNs in A with values from B`

EDIT: Sorry my mistake the **NAN** operator descrition indeed changed in

SHA-1: d7982ca98cef31648d34a7b4e5c5e4689fd67ab9

- Update grdmath.rst table of operators (#7500)

(Fixed directly in master)

---

<div class="post-metadata">

**Author:** ![John\_Geodesy](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/john_geodesy/32/116_2.png) [@John\_Geodesy](https://forum.generic-mapping-tools.org/u/John_Geodesy)\
**Post date:** [August 10, 2023, 8:49pm UTC](https://forum.generic-mapping-tools.org/t/grdmath-invert-nan-operator/3593/9 "2023-08-10T20:49:18Z")

</div>

Thanks guys! I use that NAN operator quite a bit, and when I was perusing the latest documentation, it threw me! Thanks for confirming it does what it always did!
