-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap GMT's standard data type GMT_IMAGE for images #3338
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
921674c
Wrap GMT's standard data type GMT_IMAGE for images
seisman c01d6ab
Use GMT_OUT|GMT_IS_REFERENCE for images and GMT_OUT for others
seisman 36430c8
Add image support to Session.read_data
seisman a918cfd
Add tests for reading images
seisman 1983b7e
Improve image doctest
seisman 84cbd11
Fix one test
seisman f887a23
Merge branch 'main' into wrapper/gmtimage
seisman 95275b0
Fix tests
seisman d43f2e9
Revert "Fix tests"
seisman 47d4e4a
Update pygmt/tests/test_clib_read_data.py
seisman bb0efe3
Temporarily add libgdal-hdf5
seisman 994de3a
Fix one failure
seisman 8d9c2ae
Merge branch 'main' into wrapper/gmtimage
seisman 75c0f7e
Merge branch 'main' into wrapper/gmtimage
seisman dc1a649
Revert "Temporarily add libgdal-hdf5"
seisman 0638dab
Remove the test_clib_read_data_image_actual_grid test
seisman 7080d8b
Revert "Remove the test_clib_read_data_image_actual_grid test"
seisman 8692161
Reapply "Temporarily add libgdal-hdf5"
seisman aeb2ea3
Do not check header.wesn
seisman dad88ce
Merge branch 'main' into wrapper/gmtimage
seisman 3f2f6bf
Merge branch 'main' into wrapper/gmtimage
seisman 74b889f
Remove the test for reading grid as image
seisman bf7f0b0
Merge branch 'main' into wrapper/gmtimage
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Wrapper for the GMT_IMAGE data type. | ||
""" | ||
|
||
import ctypes as ctp | ||
from typing import ClassVar | ||
|
||
from pygmt.datatypes.grid import _GMT_GRID_HEADER | ||
|
||
|
||
class _GMT_IMAGE(ctp.Structure): # noqa: N801 | ||
""" | ||
GMT image data structure. | ||
|
||
Examples | ||
-------- | ||
>>> import numpy as np | ||
>>> from pygmt.clib import Session | ||
>>> with Session() as lib: | ||
... with lib.virtualfile_out(kind="image") as voutimg: | ||
... lib.call_module("read", ["@earth_day_01d", voutimg, "-Ti"]) | ||
... # Read the image from the virtual file | ||
... image = lib.read_virtualfile(vfname=voutimg, kind="image").contents | ||
... # The image header | ||
... header = image.header.contents | ||
... # Access the header properties | ||
... print(header.n_rows, header.n_columns, header.registration) | ||
... print(header.wesn[:], header.inc[:]) | ||
... print(header.z_scale_factor, header.z_add_offset) | ||
... print(header.x_units, header.y_units, header.z_units) | ||
... print(header.title) | ||
... print(header.command) | ||
... print(header.remark) | ||
... print(header.nm, header.size, header.complex_mode) | ||
... print(header.type, header.n_bands, header.mx, header.my) | ||
... print(header.pad[:]) | ||
... print(header.mem_layout, header.nan_value, header.xy_off) | ||
... # Image-specific attributes. | ||
... print(image.type, image.n_indexed_colors) | ||
... # The x and y coordinates | ||
... x = image.x[: header.n_columns] | ||
... y = image.y[: header.n_rows] | ||
... # The data array (with paddings) | ||
... data = np.reshape( | ||
... image.data[: header.n_bands * header.mx * header.my], | ||
... (header.my, header.mx, header.n_bands), | ||
... ) | ||
... # The data array (without paddings) | ||
... pad = header.pad[:] | ||
... data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1], :] | ||
180 360 1 | ||
[-180.0, 180.0, -90.0, 90.0] [1.0, 1.0] | ||
1.0 0.0 | ||
b'x' b'y' b'z' | ||
b'' | ||
b'' | ||
b'' | ||
64800 66976 0 | ||
0 3 364 184 | ||
[2, 2, 2, 2] | ||
b'BRPa' 0.0 0.5 | ||
1 0 | ||
>>> x | ||
[-179.5, -178.5, ..., 178.5, 179.5] | ||
>>> y | ||
[89.5, 88.5, ..., -88.5, -89.5] | ||
>>> data.shape | ||
(180, 360, 3) | ||
>>> data.min(), data.max() | ||
(10, 255) | ||
""" | ||
|
||
_fields_: ClassVar = [ | ||
# Data type, e.g. GMT_FLOAT | ||
("type", ctp.c_int), | ||
# Array with color lookup values | ||
("colormap", ctp.POINTER(ctp.c_int)), | ||
# Number of colors in a paletted image | ||
("n_indexed_colors", ctp.c_int), | ||
# Pointer to full GMT header for the image | ||
("header", ctp.POINTER(_GMT_GRID_HEADER)), | ||
# Pointer to actual image | ||
("data", ctp.POINTER(ctp.c_ubyte)), | ||
# Pointer to an optional transparency layer stored in a separate variable | ||
("alpha", ctp.POINTER(ctp.c_ubyte)), | ||
# Color interpolation | ||
("color_interp", ctp.c_char_p), | ||
# Pointer to the x-coordinate vector | ||
("x", ctp.POINTER(ctp.c_double)), | ||
# Pointer to the y-coordinate vector | ||
("y", ctp.POINTER(ctp.c_double)), | ||
# Book-keeping variables "hidden" from the API | ||
("hidden", ctp.c_void_p), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
header.z_min and header.z_max are not checked here, because they have invalid values like 1.79769313486231570000e+308 (i.e.,
DBL_MAX
).