Scale and truncate CPTs with soft hinge

Is it possible to activate a soft hinge at 0, truncate the master CPT from -0.2 to 1.0, and then scale it to -2 to 10 with foreground/background colors the same as the min/max values in the truncated and scaled color bar? I can do this manually if I truncate the full (scaled) CPT with awk, but based on the docs, I think makecpt should be able to do this (but I could be completely wrong).

In the example below, using the vik master CPT, the top color bar, A, is the full original CPT. The middle bar, B, has about the range of colors I expect but does not keep the hinge at 0 (using +h3.5 instead of +h0 here pushes the hinge close to 0, I clearly don’t understand how the hinge works). The bottom bar, C, is the result of scaling the full CPT and only truncating the color bar, so the bg/fg color is dark blue instead of pale blue, and data values below -2 would have the full range of blues. I can pass the CPT from part C through awk to set the correct background color and manually truncate it. I’m trying to get B with the correct hinge using makecpt, is this possible?

gmt begin test_cpt_pt1
  gmt plot -R0/22/-2/4 -JX22c/6c -T #-Bxa1g0.5 -Bya1g0.5

  # original, unscaled vik
  gmt makecpt -Cvik+h0
  gmt colorbar -Dg17/3+w10c/1c+jBC+h+e+mal -Bxa.2g.2+L"A. original"

  # truncate and scale vik
  gmt makecpt -Cvik+h0     -G-0.2/NaN -T-2/10/2 -Do
  gmt colorbar -Dg17/2+w10c/1c+jTC+h+e -Bxa2g2+L"B. scale + trunc"

  # scale, truncate colorbar
  gmt makecpt -Cvik+h0    -T-10/10/2
  gmt colorbar -Dg17/-0.5+w10c/1c+jTC+h+e -G-2/NaN -Bxa2g2+L"C. scaled, bar truncated"

  #red lines omitted
gmt end show

TLDR: I compute difference and ratio grids for sensitivity data compared to a baseline (testA.grd - base.grd and testA.grd / base.grd). For example, I plot difference and ratio maps side by side to show the effect of a new ground motion model on Cascadia subduction zone ground motions. In some comparisons, very small differences can result in high ratios (e.g. ground motions far away from the fault may be close to zero), so, for example, I’d truncate the upper end of the ratio color scale to focus the colorbar on the range of ratios where the differences are significant. I may have hundreds of comparison maps (multiple models, spectral periods, and hazard levels) and I try to use as few different color scales as possible so there is some consistency between maps. If anyone has any suggestions on how to visualize this sort of comparison, I’d love to hear it!

Is it what you want ?

(I just removed +h to keep the default “0” from your B example)

gmt begin test png
gmt makecpt -Cvik -T-2/10/0.1 -G-0.2/1 -D 
gmt colorbar -DjBL+h+e -Bxafg -R0/1/0/1  
gmt end show  

You would have achieved the same result with +h4.

My take from the mechanism :
-G-0.2/1 => hinge at 0.4 “in master”
Once scaled (-T-2/10), the “0.4 hinge” becomes “4”, thus the shift.

It is indeed a little counter-intuitive that +h0 doesn’t correspond to the final scale… Maybe @pwessel has a good reason for that?

That does seem to do it, thank you! Oddly, I have to add -Z to your makecpt command to get a continuous color bar. I wonder if this is something to do with macos Preview.

Omitting +h# works for both ends, and moving the hinge to 1, which isn’t what I expected. Playing with +h# further, I can’t figure out how to set the hinge with a non-uniformly scaled CPT. e.g. truncating from -0.5 to 1 and scaling to -0.5 to 0.5 with the hinge at 0.0.

  • Without +h#, the hinge looks it’s near -.2, which seems to make sense
  • with +h0.2, it almost looks, to my eye (does anyone else see this?), that there are two hinges, one between -0.1 and 0 and a second near 0.1
  • with +h0.25, causes a GMT_CPT_READ_ERROR (8)

Your solution works for what I’m trying to do, but I don’t understand what is happening with +h in general.

gmt begin test_cpt_pt2 png
gmt makecpt -Cvik -G-1/0.2 -T-1/0.2/.2 -D -Z
gmt colorbar -DjBC+h+e -Bxafg -R0/1/0/1 -By+L"OK"

gmt makecpt -Cvik -G-0.2/1 -T0.8/2/0.2 -D -Z
gmt colorbar -DjBC+h+e -Bxafg -Y-1.5c -By+L"OK"

gmt makecpt -Cvik -G-0.5/1 -T-.5/.5/0.1 -D -Z
gmt colorbar -DJBC+h+e+ml -Bxa.1fg+L"non-uniform" -Y-.5c -By+L"OK?"

gmt makecpt -Cvik+h0.2 -G-0.5/1 -T-.5/.5/0.1 -D -Z
gmt colorbar -DJBC+h+e+ml -Bxa.1fg -Y-1.5c -By+L"?!?"

gmt makecpt -Cvik+h0.25 -G-0.5/1 -T-.5/.5/0.1 -D -Z
gmt colorbar -DJBC+h+e+ml -Bxa.1fg -Y-1.5c -By+L"Err"
gmt end show