# Interpolation used in pixel-to-node transformations

**URL:** <https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055>\
**Category:** Q&A\
**Created:** [June 20, 2023, 11:20pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055 "2023-06-20T23:20:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjg](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jjg/32/2822_2.png) [@jjg](https://forum.generic-mapping-tools.org/u/jjg)\
**Post date:** [June 20, 2023, 11:20pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/1 "2023-06-20T23:20:23Z")

</div>

I’m looking at performing a pixel-aligned to node-aligned transformation in a program of mine, so i thought I’d first look at how GMT does it (since it does so many things well). My test script is

```auto
gmt xyz2grd -Gpixel.grd -rp -I1 -R0/2/0/2 <<EOF
0.5, 0.5, 1.0
1.5, 0.5, 2.0
1.5, 1.5, 3.0
EOF
gmt grd2xyz pixel.grd
gmt grdsample pixel.grd -Gnode.grd -T
echo '--'
gmt grd2xyz node.grd

```

so creates a pixel-aligned grid, z = x + y, one missing value, converts it to node-aligned and prints the two out: it produces

```auto
0.5	1.5	NaN
1.5	1.5	3
0.5	0.5	1
1.5	0.5	2
--
0	2	NaN
1	2	3.66891884804
2	2	4.0859375
0	1	0.27702704072
1	1	2
2	1	2.75598096848
0	0	0.4453125
1	0	1.20574164391
2	0	2.11764717102

```

The results look good, in fact too good – the default interpolation is bicubic right? so I’d expect a lot more NaN propagation than that. Possibly `grdsample -T` uses a different default? I had a look around the source but couldn’t see anything obvious. Could anyone enlighten me (a reference to the literature and/or location in the source code)? Thanks in advance!

---

<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:** [June 21, 2023, 12:49am UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/2 "2023-06-21T00:49:57Z")

</div>

Yes, by default `grdsample` does a bicubic interpolation (man says so).  
When I want to see what a program does I fire the debugger and hit F10 & F11 many times. For present case I think a good entry point is

> <https://github.com/GenericMappingTools/gmt/blob/master/src/grdsample.c#L395>

---

<div class="post-metadata">

**Author:** ![jjg](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jjg/32/2822_2.png) [@jjg](https://forum.generic-mapping-tools.org/u/jjg)\
**Post date:** [June 21, 2023, 12:57pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/3 "2023-06-21T12:57:57Z")

</div>

Thanks Joaquim, that leads [here](https://github.com/GenericMappingTools/gmt/blob/master/src/gmt_bcr.c#L279), there is a link in that file (but it 404s), but it all looks pretty standard convolution kernel stuff, still I’m a bit puzzled by the lack of NaN propagation, possibly [this](https://github.com/GenericMappingTools/gmt/blob/master/src/gmt_bcr.c#L63) is the reason:

> Because of the size of the convolution kernels, these routines assume that  
> the grid is padded with appropriate (periodic or natural) boundary conditions

that assumption seems to be applied non-explicitly, would that be a reasonable summary?

---

<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:** [June 21, 2023, 2:12pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/4 "2023-06-21T14:12:25Z")

</div>

Your case is probably caught up in an ongoing bug situation you can read about [here](https://github.com/GenericMappingTools/gmt/issues/7453). I can certainly see why you might get a NaN on the right side. I would use another interpolator tha nbicubic here, say bilinear.

---

<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:** [June 21, 2023, 2:14pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/5 "2023-06-21T14:14:16Z")

</div>

Could it be that [https://docs.generic-mapping-tools.org/dev/grdfilter.html#n](https://docs.generic-mapping-tools.org/dev/grdfilter.html#n) is working under the hood?

---

<div class="post-metadata">

**Author:** ![jjg](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jjg/32/2822_2.png) [@jjg](https://forum.generic-mapping-tools.org/u/jjg)\
**Post date:** [June 21, 2023, 2:45pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/6 "2023-06-21T14:45:27Z")

</div>

On that issue there is “introduced in 2023”, but I’m using the stock 6.4.0 (compiled locally) who’s tag is June 2022, so predates that.

---

<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:** [June 21, 2023, 4:02pm UTC](https://forum.generic-mapping-tools.org/t/interpolation-used-in-pixel-to-node-transformations/4055/7 "2023-06-21T16:02:13Z")

</div>

Pretty sure bug is older
