Question about CPT unit modifier (+U<unit>)

,

Hello GMT developers and users, I have a question regarding the unit modifier for Color Palette Tables (CPT).
For reference, I am using Ubuntu 24.04 LTS and GMT 6.5.0, installed via apt.
First, please consider the following script:

gmt begin question png
    gmt basemap -R110/155/-45/-10 -JM10c -B
    gmt grdimage @earth_relief_10m -Cgeo+Uk
    gmt colorbar -DJBC+w4c -B1000
gmt end

I expected that by adding the +Uk modifier to -Cgeo, the elevation (z-values) would be converted from meters to kilometers. However, the resulting colorbar still shows values in meters.


Thinking that perhaps I needed to explicitly provide a CPT file, I also tried the following:

gmt begin question png
    gmt makecpt -Cgeo -T-7000/2000 -H >question.cpt
    gmt basemap -R110/155/-45/-10 -JM10c -B
    gmt grdimage @earth_relief_10m -Cquestion.cpt+Uk
    gmt colorbar -Cquestion.cpt+Uk -DJBC+w4c -B1000
gmt end

In this case, it seems that +Uk is interpreted as part of the filename, resulting in the following error:

grdimage [ERROR]: GMT_Read_Data: File not found: question.cpt+Uk
[Session gmt (0)]: Error returned from GMT API: GMT_PTR_IS_NULL (75)
grdimage [ERROR]: Unable to save current CPT file to /home/.../gmt.cpt !
grdimage [ERROR]: Failed to read CPT question.cpt+Uk.

According to the documentation, the +U modifier should allow conversion of CPT units (e.g., from meters to kilometers).
Am I using the syntax incorrectly? Or could this be a bug?

Thank you very much for your continued efforts in developing and maintaining GMT.

But the data are still in thousands right ? Can you do a grdimage @earth_relief+d1000 ?

I didn’t know about that +U option. I usually used -W instead to plot the scale in km instead of m.

gmt begin question png
    gmt makecpt -Cgeo -T-7000/2000 -H > question.cpt
    gmt basemap -R110/155/-45/-10 -JM10c -B
    gmt grdimage @earth_relief_10m -Cquestion.cpt
    gmt colorbar -Cquestion.cpt -DJBC+w4c -B1 -W0.001
gmt end

1 Like
gmt begin question png
    gmt basemap -R110/155/-45/-10 -JM10c -B
    gmt grdimage @earth_relief_10m+d1000 -Cgeo
    gmt colorbar -DJBC+w4c -B1
gmt end


It seems that the unit has been converted to kilometers, but the CPT range has also changed unexpectedly. I only intended to divide the grid z-values by 1000…

For now, this seems to be the best workaround available.

according to the docs, +Uk is supposed to work when a master cpt is specified, not with a .cpt file prepared by the user.

On the other hand, +Uk is apparently ignored in combination with a master cpt, as -Cgeo+Uk

When I’m trying to save the palette using -Cgeo+smy.cpt or -Cgeo+Uk+smy.cpt, gmt grdimage crashes leaving behind an open gmt session. A valid my.cpt is created in both cases, identical, values in meters, that is, +Uk is apparently ignored.
E.g.

gmt grdimage @earth_relief_10m -Cgeo+Uk+smy.cpt -Vd -R110/155/-45/-10 -JM10c -B -png Australia -Vd
...
grdimage [INFORMATION]: gmt_grd_project: Output grid extrema [-12885.5/1658] exceed extrema of input grid [-6762/1619] due to resampling
grdimage [INFORMATION]: gmt_grd_project: Output grid clipped to input grid extrema
grdimage [DEBUG]: GMT_Destroy_Data: freed memory for a Grid for object 0
grdimage [DEBUG]: gmtlib_unregister_io: Unregistering object no 0 [n_objects = 3]
grdimage [DEBUG]: gmtlib_unregister_io: Object no 0 has non-NULL resource pointer
grdimage [INFORMATION]: Evaluate image pixel colors
grdimage [INFORMATION]: Basic z(x,y) -> color image with no illumination.
grdimage [INFORMATION]: Plotting 24-bit color image
PSL: Too many colors to make colormap - using 24-bit direct color instead.
PSL: DEFLATE compressed 172992 to 96673 bytes (44.1% savings at compression level 5)
grdimage [DEBUG]: Current size of half-baked PS file /home/mkononets/.gmt/sessions/gmt_session.3134949/gmt_0.ps- = 147221.
grdimage [DEBUG]: GMT_Destroy_Data: freed memory for a Grid for object 3
grdimage [DEBUG]: gmtlib_unregister_io: Unregistering object no 3 [n_objects = 2]
grdimage [DEBUG]: gmtlib_unregister_io: Object no 3 has non-NULL resource pointer
grdimage [DEBUG]: Object ID 4 : Registered CPT File my.cpt as an Output resource with geometry Volume [n_objects = 3]
grdimage [DEBUG]: gmtapi_begin_io: Output resource access is now enabled [container]
grdimage [DEBUG]: gmtapi_export_palette: Passed ID = 4 and mode = 0
grdimage [INFORMATION]: Write CPT to File my.cpt
grdimage [DEBUG]: Writing CPT to my.cpt
grdimage [DEBUG]: GMT_End_IO: Output resource access is now disabled
grdimage [DEBUG]: GMT_Destroy_Data: freed memory for a CPT for object 1
grdimage [DEBUG]: gmtlib_unregister_io: Unregistering object no 1 [n_objects = 2]
grdimage [DEBUG]: gmtlib_unregister_io: Object no 1 has non-NULL resource pointer
free(): invalid pointer
Aborted

Ah, I checked with grdinfo, and it seems the issue was actually caused by the 0.5 scale factor already applied to the @earth_relief_10m grid file.

gmt grdinfo @earth_relief_10m
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: Title: SRTM15 Earth Relief v2.7 at 10 arc minutes
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: Command: 
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: Remark: Reduced by Gaussian Cartesian filtering (52.4 km fullwidth) from SRTM15_V2.7.nc [Tozer et al., 2019; https://doi.org/10.1029/2019EA000658]
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: Gridline node registration used [Geographic grid]
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: Grid file format: ns = GMT netCDF format (16-bit integer), CF-1.7
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: x_min: -180 x_max: 180 x_inc: 0.166666666667 (10 min) name: longitude n_columns: 2161
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: y_min: -90 y_max: 90 y_inc: 0.166666666667 (10 min) name: latitude n_rows: 1081
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: v_min: -9732.5 v_max: 6118.5 name: elevation (m)
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: scale_factor: 0.5 add_offset: 0 packed z-range: [-19465,12237]
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: format: netCDF-4 chunk_size: 136,136 shuffle: on deflation_level: 9
~/.gmt/server/earth/earth_relief/earth_relief_10m_g.grd: Default CPT:

Yes, I also tried saving it as a CPT file using the same code as yours, and it results in an abort on my side as well…

The crash is a bug in itself.

Here’s a github bug report

And the second one

I confess I have difficulties in understanding what +Uk is supposed to do here. If the cpt was scaled by 0.001 than image would be almost empty because data (the grid) is in meters. I understand that the desired effect is at the time of the colorbar plotting but the cpt is, rightfully, saved unscaled (otherwise image would screw as said above). How would it know when doing the colorbar plot that the cpt should be scaled by 0.001?

No idea what should be fixed here and not very keen to let another issue open for perpetuity.

I fully agree with your explanation regarding this issue. That said, setting this specific case aside, what is the original purpose of the +u|U option in CPT?

I don’t know and I’m afraid we can’t answer that question because who could do is no longer with us. Probably the best is to remove it from docs.

Maybe that thing could have a look into the code and try explaining us?

The free thing can give a clue for sure. I just have no time, credits and priority interests to all of theis recent activity.