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.
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.
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…
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
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?