# Gmtbinstats: cells containing points are painted NaN in grdimage

**URL:** https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737
**Category:** Q&A
**Created:** [March 8, 2023, 11:43am UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737 "2023-03-08T11:43:05Z")
**Posts on this page:** 17
**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: [March 8, 2023, 11:43am UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/1 "2023-03-08T11:43:05Z")

</div>

Simple case: count the number of earthquakes in each 1° bin.

- the grid produced is correct (checked in qgis); the number of points in each cell is correct (got confused at first since default is gridline, not pixel node registration)
- when plotting this grid with `grdimage`, many of cells containing quakes (black dots) are painted gray, i.e. `NaN`. Why is this? I tried playing with `makecpt` (hence the funky colorbar), but to no avail.
- Script and data included.

```auto
gmt begin forumpost png

gmt binstats $(gmt info eq-2000-2021.txt -I1) -Gquakes.nc -I1 -Cn -Tr eq-2000-2021.txt -rp
gmt makecpt -T-10/100/10 -Cjet
gmt grdimage quakes.nc -C -B0 -R-5/53/90/81r -JU+34/20c
gmt coast -W -Dc
gmt plot eq-2000-2021.txt -Sc0.05c -Gblack -l"earthquake"
gmt colorbar

gmt end show

```

 ![forumpost](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/3/36efd759b196de68b3bdc843fe1fdc2208cb4835.png) [eq-2000-2021.txt](https://forum.generic-mapping-tools.org/uploads/short-url/dfvTfnw9A8vTD3nEmZRlfISgrIs.txt) (170.7 KB)

---

<div class="post-metadata">

### Author: ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)
#### Post date: [March 8, 2023, 12:16pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/2 "2023-03-08T12:16:02Z")

</div>

Changing `-I` step solves the problem. (Try `-I33k`).

If I were to guess, I’d say that `-I1(d)` near the pole behaves “as if” it was a rectangular 1º cell (~111x111km). But it’s just a guess.

---

<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: [March 8, 2023, 12:28pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/3 "2023-03-08T12:28:36Z")

</div>

Thanks @PlanetGus, but that still does not explain why the cells _with values_ are painted gray (`NaN`) by **grdimage**?

---

<div class="post-metadata">

### Author: ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)
#### Post date: [March 8, 2023, 12:36pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/4 "2023-03-08T12:36:10Z")

</div>

Except if I’m right about the cell size. Once a point is counted in a cell, it is not in other overlapping ones.

In that case, `-S` would be better than `-T` (again, this is just my guess)

---

<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: [March 8, 2023, 1:42pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/5 "2023-03-08T13:42:42Z")

</div>

I don’t get that _NaNification_ effect

```auto
julia> G = binstats("eq-2000-2021.txt", R="-24/51/50/88", I="33k", C=:n, T=:r, r=:p);
julia> imshow("quakes.nc", proj=:guess, coast=true, colorbar=true)

```

 ![GMTjl_tmp](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/e/ef3731c59f6e696246fd6289d75740cc4a62bdef.png)

---

<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: [March 8, 2023, 1:59pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/6 "2023-03-08T13:59:49Z")

</div>

You’re right; changing from `-I1` to `-I33k` improves the matter, but I’m still getting gray cells that should be colored. See att. figure - I’ve highlighted _some_ of the area that have gray cells that should have colored cells.

And why the heck does `-I33k` improve the matter? I want `-I1`!

 ![quakecount](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/9/9e5cb8eb3e44a176b9a2f42880bc4a3186c6d67f.png)

---

<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: [March 8, 2023, 2:04pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/7 "2023-03-08T14:04:34Z")

</div>

If grdimage does any reproduction to turn your grid into a clipped rectangular image then NaNs will bleed. See -nn to avoid that.

---

<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: [March 8, 2023, 2:11pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/8 "2023-03-08T14:11:23Z")

</div>

Ah, yes. I have `-n+a` by default.

---

<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: [March 8, 2023, 2:22pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/9 "2023-03-08T14:22:33Z")

</div>

Still getting lots of NaNs with `-nn`, `-n+a` and `-nn+a` (where I should not have NaNs).

---

<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: [March 8, 2023, 9:54pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/10 "2023-03-08T21:54:52Z")

</div>

Teaching moment:

1. Replace your **grdimage** command with `gmt grdview quakes.nc -C -B0 -R-5/53/90/81r -JU+34/20c -T` and realise what 1x2 degree cells look like to the north, Imagine grdimage creating an equidistant image to fill that. Even try -B0g1 to see the grid.
2. Decide you need finer resampling of the image by adding` -E600` to your current **grdimage** command and note how it changes,

---

<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: [March 9, 2023, 9:49am UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/11 "2023-03-09T09:49:29Z")

</div>

Thanks Paul for teaching moment; both of these give reasonable results.  
I prefer the **grdview** though, as **grdimage -E600** looks smeared.  
I know that 1x1 degree cells will be very thin towards the pole, but that’s OK.

So,

- There is not a corresponding `-T` option for **grdimage**? Why not? The only difference, in appearance at least, between the **grdimage** and **grdview** command, is that the **grdview** versions looks like what I expect (and want) **grdimage** to do.

---

<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: [March 9, 2023, 10:13am UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/12 "2023-03-09T10:13:48Z")

</div>

Not sure, perhaps we decided at one time to not overload **grdimage** with too many options. But, your near-pole cases do suggest that perhaps the standard results (your first plots) are not a good result for users, and the remedy may require users to just know too much. Pinging our southern guru @Joaquim on what he thinks about

1. Add **-T** as in **grdview** to **grdimage** [pretty easy to do: just make a function out of what **grdview** does and call them from those two programs]. Since **grdview** was written to do 3-D perspective it may not easily come to mind to use for your 2-D case.
2. Detect cases where creating a _nx_ by _ny_ rectangular image either needs a boosting (e.g., a **-E** under the hood in a way), some warning educates the users, or we decide plotting via **-T** is the best default?

---

<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: [March 9, 2023, 10:37am UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/13 "2023-03-09T10:37:24Z")

</div>

I don’t have much experience with **grdview** , so as always; pardon my ignorance.

I use **grdimage** 99% of the time, but my impression has always been that **grdview** and **grdimage** does have a great overlap of use cases - sometimes some rarely used feature forces me to use **grdview**. In that regard I have thought about **grdview** as being a superset of **grdimage**. And, in the mentality of Occam’s razor, you choose the simplest tool for the job.

---

<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: [March 9, 2023, 1:13pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/14 "2023-03-09T13:13:32Z")

</div>

I still fail to see what are those cells that are wrongly painted as NaNs. Bellow is a grdimage of `-I1` and the same grid displayed in Mirone. They look the same to me.

 ![GMTjl_tmp](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/9/945b495202f3a88843cab248a65e09478f4f30b6.png)

 ![image](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/1/1faea9281c10a12ede8b44a2098f1d1e1ece0b87.png)

---

<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: [March 9, 2023, 1:41pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/15 "2023-03-09T13:41:46Z")

</div>

And this now is with the new **-T** in grdimage. Almost equal to before, except that we now see faint outline of cells.

```auto
julia> imshow("quakes.nc", T=true, coast=true, colorbar=true, Vd=1)
        ["grdimage quakes.nc -JX14c/0 -R-24/51/50/88 -Baf -BWSen -n+a -T -C -P -K > C:\\TEMP\\GMTjl_tmp.ps", "pscoast -J -Rquakes.nc -A200/0/2 -W0.5 -Da -K -O >> C:\\TEMP\\GMTjl_tmp.ps", "psscale -J -Rquakes.nc -Baf -DJMR -C -K -O >> C:\\TEMP\\GMTjl_tmp.ps"]

```

 ![GMTjl_tmp](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/5/52cc596e8179a7a1eef8094f79a1b01463b0a246.png)

---

<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: [March 9, 2023, 1:45pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/16 "2023-03-09T13:45:47Z")

</div>

Well, just a linear projection. Then of course it look’s right. Try Andreas’s projection and then the problem shows

---

<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: [March 9, 2023, 1:57pm UTC](https://forum.generic-mapping-tools.org/t/gmtbinstats-cells-containing-points-are-painted-nan-in-grdimage/3737/17 "2023-03-09T13:57:33Z")

</div>

Ah, in my projected version, I was not plotting the points (and not even paying attention to them) to compare with colors.
