Skip to content

[BREAKING] Raise GMTTypeError exception for unsupported image dtypes#4673

Merged
seisman merged 1 commit into
GenericMappingTools:mainfrom
seisman:raise-on-unsupported-image-dtype
Jun 12, 2026
Merged

[BREAKING] Raise GMTTypeError exception for unsupported image dtypes#4673
seisman merged 1 commit into
GenericMappingTools:mainfrom
seisman:raise-on-unsupported-image-dtype

Conversation

@seisman

@seisman seisman commented Jun 12, 2026

Copy link
Copy Markdown
Member

GMT only supports images with the uint8 data type. Previously, GMT would emit the following errors for unsupported image data types but would not stop execution:

Error: e [ERROR]: Using this data type (Int8) is not implemented
grdimage (gmtapi_import_image): Bad measurement unit.  Choose among c|i|p [/tmp/pygmt-6rst4f_m.tif]
[Session pygmt-session (1729)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
[Session pygmt-session (1729)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)

As a result, unsupported images were still read and plotted, although the output was likely incorrect. PyGMT would then issue a warning about the unsupported data type.

It is unclear when this behavior changed, but the GMT dev version now produces the following error instead (xref: https://github.com/GenericMappingTools/pygmt/actions/runs/27249793607/job/80471539938):

Error: e [ERROR]: Using this data type (Int8) is not implemented
pygmt-session (gmtapi_import_image): Bad measurement unit.  Choose among c|i|p [/tmp/pygmt-87amr_xv.tif]
[Session pygmt-session (1729)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
[Session pygmt-session (1729)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
Error: e [ERROR]: Option -R parsing failure. Correct syntax:
...
Error: e [ERROR]: Offending option -R0/0/0/0

Apparently, GMT now fails to read images with unsupported data types altogether, causing our tests to fail. I can't find the exact changes that lead to the behavior changes, but I feel it's likely some changes in GDAL.

This PR addresses the issue by raising a GMTTypeError whenever an unsupported image data type is encountered.

@seisman seisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. run/test-gmt-dev Trigger the GMT Dev Tests workflow in PR labels Jun 12, 2026
@seisman seisman added this to the 0.19.0 milestone Jun 12, 2026
@seisman seisman changed the title Raise for unsupported image dtypes [BREAKING] Raise GMTTypeError exception for unsupported image dtypes Jun 12, 2026

@weiji14 weiji14 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error only shows up for the GMT dev version right? Not the current stable GMT 6.6.0 release? Ok with raising an error for GMT 6.5 and 6.6 users, since they should really rescale to 0-255 anyway.

@seisman

seisman commented Jun 12, 2026

Copy link
Copy Markdown
Member Author

The error only shows up for the GMT dev version right? Not the current stable GMT 6.6.0 release?

The error also shows up for GMT 6.6.0 but GMT can still read it:

>>> import pygmt
>>> image = pygmt.datasets.load_blue_marble()
>>> image = image.astype(dtype="int8")
>>> fig = pygmt.Figure()
>>> fig.grdimage(grid=image)
/home/seisman/OSS/gmt/pygmt/pygmt/helpers/decorators.py:842: RuntimeWarning: Input image has dtype: int8 which is unsupported, and may result in an incorrect output. Please recast image to a uint8 dtype and/or scale to 0-255 range, e.g. using a histogram equalization function like skimage.exposure.equalize_hist.
  return module_func(*args, **kwargs)
grdimage [ERROR]: Using this data type (Int8) is not implemented
grdimage (gmtapi_import_image): Bad measurement unit.  Choose among c|i|p [/tmp/pygmt-vpfsm9ze.tif]
[Session pygmt-session (5)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
[Session pygmt-session (5)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
grdimage [ERROR]: Using this data type (Int8) is not implemented
grdimage (gmtapi_import_image): Bad measurement unit.  Choose among c|i|p [/tmp/pygmt-vpfsm9ze.tif]
[Session pygmt-session (5)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
[Session pygmt-session (5)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)

>>> fig.show()  # The image is incorrectly plotted.

@weiji14

weiji14 commented Jun 12, 2026

Copy link
Copy Markdown
Member

OK. If image is incorrect, then error is good.

@seisman seisman merged commit 6eea101 into GenericMappingTools:main Jun 12, 2026
31 of 40 checks passed
@seisman seisman removed needs review This PR has higher priority and needs review. run/test-gmt-dev Trigger the GMT Dev Tests workflow in PR labels Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improving an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants