How to set the colors of NaN with grdview?

I think that is not possible to set the color of the NaN values with grdview.

I set the NaN to red. When I plot my grid with grdimage (top image) I got red but I can’t with grdview. Am I missing something? Is this a bug?

Full script

DOMAIN_volc=-R-61.7713244711/-61.5554875289/15.9359105289/16.1517474711
gmt grdcut @earth_relief_01s ${DOMAIN_volc} -Gisland.nc
gmt grdclip island.nc -Gtopo_island.nc -Sb0/NaN -Sr0/NAN
max_alt=$(gmt grdinfo topo_island.nc -Mf | grep "v_max" | awk '{print $5}')

gmt begin q png
gmt makecpt -Coleron -T0/${max_alt} -M --COLOR_NAN=red
gmt grdview topo_island.nc -C -JM10c -JZ1c -Qc -I+d -Bafg -p135/20
gmt grdimage topo_island.nc -C -Yh
gmt end

Hello @Esteban82,

just one spontaneous idea: For grdview, the elevation is not only represented by the color-coding but also along the vertical or z-axis. So, there is the question at which position of the z-axis NaN values should be plotted, as there is no NaN value on the z-axis. Probably GMT does not know where to plot the NaN part of the data (vertically) and simply skips it.

1 Like

For this case, maybe using the background can serve as a work around:
(Edit 2024/01/17: Correction corresponding to the post by @Esteban82 below)

DOMAIN_volc=-R-61.7713244711/-61.5554875289/15.9359105289/16.1517474711
gmt grdcut @earth_relief_01s ${DOMAIN_volc} -Gisland.nc
gmt grdclip island.nc -Gtopo_island.nc -Sb0/-1 -Sr0/-1

gmt begin q png
gmt makecpt -Coleron -T0/2000 -M --COLOR_BACKGROUND=red
gmt grdview topo_island.nc -C -JM10c -JZ1c -Qc -I+d -Bafg -p135/20
gmt grdimage topo_island.nc -C -Yh
gmt end show

Output figure:

Sure, yes, that must be it. So I have no choice but to apply your solution.

BTW, is this command complete? Or do you add something like -Sb0/-0.1?

In principle, it should be working. But if you also want 0 to be in red, then you need something like -Sb0/-0.1, as the colormap starts at zero (T0/2000).

I think that you must add something like -Sb0/-0.01 -Sr0/-0.01

Otherwise I got this messagge:

grdclip [ERROR]: Option -S: Must specify at least one of -Sa, -Sb, -Si, -Sr

Ah, yes your are right! Must have missed this part somehow when copying the code :slightly_frowning_face: (locally I have gmt grdclip island.nc -Gtopo_island.nc -Sb0/-1 -Sr0/-1).
Updated my post above to avoid further confusions.

The issue of using NaN colors in grdview is probably something that could be improved and revisited. Right now, I get read NaNs only with -Qs (surface) and only without -p. This is due to various if-test checks. For -Q we rasterize the grid to an image and set the image pixels to whatever color (but not the NaN color).

I suspect we will need a modifier or option to insist that we use the NaN color, whatever it is, on the NaN pixels or NaN nodes or NaN patches. Perhaps the simplest syntax might be a new modifier +n to -Q which says "paint any NaN node or pixel with the NaN color in the CPT (requires -C)?

1 Like

I think that this is the best way to do it.

  1. Create two grids: one with NaN and another with 0 for the bathymetry.
  2. I made a 2D map with the NaN grid.
  3. Finally, I drape the map from step 2 over the relief with 0.

Full script:

DOMAIN_volc=-R-61.7713244711/-61.5554875289/15.9359105289/16.1517474711
gmt grdcut @earth_relief_01s ${DOMAIN_volc} -Gisland.nc
gmt grdclip island.nc -Gisland_NaN.nc -Sb0/NaN -Sr0/NaN
gmt grdclip island.nc -Gtopo_island.nc -Sb0/0

gmt begin q png
gmt makecpt -Coleron -T0/2000 -M --COLOR_NAN=lightblue
gmt grdimage island_NaN.nc -C
gmt end

gmt grdview topo_island.nc -Gq.png -Bf -JM10c -JZ1c -Qc -p135/20 -png q_drape

Seeing a duplicate the grid makes ruffle my hair. Looks like the right thing here is to make a NaN/no-NaN mask and add it as an alpha layer to the image. Then the transparency and transparent color machinery should apply. But we didn’t test the alpha machinery to grdview before.

Yes, grdview needs some love and care. Just posted a PR to do some standardization on syntax with grdimage at least.