-
Notifications
You must be signed in to change notification settings - Fork 235
GMT_IMAGE: Implement the GMT_IMAGE.to_dataarray method for 3-band images #3128
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
Changes from 7 commits
bcf43f0
a052a1a
56a6d65
f71e79c
4cce4a2
e02b650
f3d4b1f
4390136
5f25669
a3c6c14
5888e10
798e658
3dbf2f2
6b860bf
0bf9368
7d437be
268e34e
86765e1
86f3ffa
cc28247
1e2c973
734dc28
919dc00
b1eacf1
aa4fdc9
c87a3ec
a20d8a2
926427b
c73328e
fb97daa
15b8d53
8433e78
3e3a6f3
12ef40a
f64fbb8
4f2ae48
d49afed
f70bec0
2fd13fb
9972ba1
62f0ce0
f715aee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||||||||||
| """ | ||||||||||||||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| Wrapper for the GMT_IMAGE data type. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| import ctypes as ctp | ||||||||||||||
| from typing import ClassVar | ||||||||||||||
|
|
||||||||||||||
| import numpy as np | ||||||||||||||
| import xarray as xr | ||||||||||||||
| from pygmt.datatypes.header import _GMT_GRID_HEADER | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| class _GMT_IMAGE(ctp.Structure): # noqa: N801 | ||||||||||||||
| """ | ||||||||||||||
| GMT image data structure. | ||||||||||||||
| Examples | ||||||||||||||
| -------- | ||||||||||||||
| >>> from pygmt.clib import Session | ||||||||||||||
| >>> import numpy as np | ||||||||||||||
| >>> import xarray as xr | ||||||||||||||
| >>> import rioxarray | ||||||||||||||
| >>> with Session() as lib: | ||||||||||||||
| ... with lib.virtualfile_out(kind="image") as voutimg: | ||||||||||||||
| ... lib.call_module("read", f"@earth_day_01d {voutimg} -Ti") | ||||||||||||||
| ... ds = lib.read_virtualfile(vfname=voutimg, kind="image").contents | ||||||||||||||
| ... header = ds.header.contents | ||||||||||||||
| ... pad = header.pad[:] | ||||||||||||||
| ... print(ds.type, header.n_bands, header.n_rows, header.n_columns) | ||||||||||||||
| ... print(header.pad[:]) | ||||||||||||||
| ... data = np.reshape( | ||||||||||||||
| ... ds.data[: header.n_bands * header.mx * header.my], | ||||||||||||||
| ... (header.my, header.mx, header.n_bands), | ||||||||||||||
| ... ) | ||||||||||||||
| ... data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1], :] | ||||||||||||||
| ... x = ds.x[: header.n_columns] | ||||||||||||||
| ... y = ds.y[: header.n_rows] | ||||||||||||||
| >>> da = xr.DataArray( | ||||||||||||||
| ... data=data, | ||||||||||||||
| ... dims=["y", "x", "band"], | ||||||||||||||
| ... coords={"y": y, "x": x, "band": [1, 2, 3]}, | ||||||||||||||
| ... ) | ||||||||||||||
| >>> da = da.transpose("band", "y", "x") | ||||||||||||||
| >>> da = da.sortby(list(data.dims)) | ||||||||||||||
| >>> da.plot.imshow() | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| _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), | ||||||||||||||
| ] | ||||||||||||||
|
|
||||||||||||||
| def to_dataarray(self) -> xr.DataArray: | ||||||||||||||
| """ | ||||||||||||||
| Convert a _GMT_IMAGE object to an :class:`xarray.DataArray` object. | ||||||||||||||
| Returns | ||||||||||||||
| ------- | ||||||||||||||
| dataarray | ||||||||||||||
| A :class:`xarray.DataArray` object. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| # Get image header | ||||||||||||||
| header: _GMT_GRID_HEADER = self.header.contents | ||||||||||||||
|
|
||||||||||||||
| # Get DataArray without padding | ||||||||||||||
| pad = header.pad[:] | ||||||||||||||
| data: np.ndarray = np.reshape( | ||||||||||||||
| a=self.data[: header.n_bands * header.mx * header.my], | ||||||||||||||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| newshape=(header.my, header.mx, header.n_bands), | ||||||||||||||
| )[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1], :] | ||||||||||||||
|
|
||||||||||||||
| # Get x and y coordinates | ||||||||||||||
| coords: dict[str, list | np.ndarray] = { | ||||||||||||||
| "x": self.x[: header.n_columns], | ||||||||||||||
| "y": self.y[: header.n_rows], | ||||||||||||||
| "band": np.array([0, 1, 2], dtype=np.uint8), | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| # Create the xarray.DataArray object | ||||||||||||||
| image = xr.DataArray( | ||||||||||||||
| data=data, | ||||||||||||||
| coords=coords, | ||||||||||||||
| dims=("y", "x", "band"), | ||||||||||||||
| name=header.name, | ||||||||||||||
| attrs=header.data_attrs, | ||||||||||||||
|
Comment on lines
+204
to
+205
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The image pygmt/pygmt/datatypes/header.py Lines 193 to 198 in ac44706
The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree they make no sense, but they're consistent with the behavior in GMT. The GMT's image support was likely added by Joaquim so that you may ping him for more information. |
||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| return image | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.