Skip to content
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

Add placeholder codes for wrapping GMT_IMAGE/GMT_CUBE #3181

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
vectors_to_arrays,
)
from pygmt.clib.loading import load_libgmt
from pygmt.datatypes import _GMT_DATASET, _GMT_GRID
from pygmt.datatypes import _GMT_CUBE, _GMT_DATASET, _GMT_GRID, _GMT_IMAGE
from pygmt.exceptions import (
GMTCLibError,
GMTCLibNoSessionError,
Expand Down Expand Up @@ -1649,7 +1649,9 @@ def virtualfile_in( # noqa: PLR0912

@contextlib.contextmanager
def virtualfile_out(
self, kind: Literal["dataset", "grid"] = "dataset", fname: str | None = None
self,
kind: Literal["dataset", "grid", "image", "cube"] = "dataset",
fname: str | None = None,
):
r"""
Create a virtual file or an actual file for storing output data.
Expand All @@ -1662,8 +1664,8 @@ def virtualfile_out(
Parameters
----------
kind
The data kind of the virtual file to create. Valid values are ``"dataset"``
and ``"grid"``. Ignored if ``fname`` is specified.
The data kind of the virtual file to create. Valid values are ``"dataset"``,
``"grid"``, ``"image"`` and ``"cube"``. Ignored if ``fname`` is specified.
fname
The name of the actual file to write the output data. No virtual file will
be created.
Expand Down Expand Up @@ -1706,6 +1708,8 @@ def virtualfile_out(
family, geometry = {
"dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"),
"grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"),
"image": ("GMT_IS_IMAGE", "GMT_IS_SURFACE"),
"cube": ("GMT_IS_CUBE", "GMT_IS_VOLUME"),
}[kind]
with self.open_virtualfile(family, geometry, "GMT_OUT", None) as vfile:
yield vfile
Expand Down Expand Up @@ -1753,7 +1757,8 @@ def read_virtualfile(
Name of the virtual file to read.
kind
Cast the data into a GMT data container. Valid values are ``"dataset"``,
``"grid"`` and ``None``. If ``None``, will return a ctypes void pointer.
``"grid"``, ``"image"``, ``"cube"``, and ``None``. If ``None``, will return
a ctypes void pointer.

Examples
--------
Expand Down Expand Up @@ -1801,9 +1806,12 @@ def read_virtualfile(
# _GMT_DATASET).
if kind is None: # Return the ctypes void pointer
return pointer
if kind in ["image", "cube"]:
raise NotImplementedError(f"kind={kind} is not supported yet.")
dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind]
dtype = {
"dataset": _GMT_DATASET,
"grid": _GMT_GRID,
"image": _GMT_IMAGE,
"cube": _GMT_CUBE,
}[kind]
Comment on lines -1805 to +1814
Copy link
Member

Choose a reason for hiding this comment

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

If we merge this PR for PyGMT v0.12.0, then NotImplementedError won't be raised right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but since it's a low-level API function, I guess users won't call it anyway and we maintainers should know what we're doing.

Copy link
Member

@weiji14 weiji14 Apr 19, 2024

Choose a reason for hiding this comment

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

But kind='image' and kind='cube' is showing up in the API docs at https://pygmt-dev--3181.org.readthedocs.build/en/3181/api/generated/pygmt.clib.Session.virtualfile_out.html. Can we push this PR to v0.13.0? There's no real benefit to users in having this merged anyway without GMT_IMAGE/GMT_CUBE being wrapped right?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we can close this PR, since it's likely we will finish wrapping GMT_IMAGE first and then GMT_CUBE later, so we only need to deal with the conflicts in PR #3150.

return ctp.cast(pointer, ctp.POINTER(dtype))

def virtualfile_to_dataset(
Expand Down
2 changes: 2 additions & 0 deletions pygmt/datatypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
Wrappers for GMT data types.
"""

from pygmt.datatypes.cube import _GMT_CUBE
from pygmt.datatypes.dataset import _GMT_DATASET
from pygmt.datatypes.grid import _GMT_GRID
from pygmt.datatypes.image import _GMT_IMAGE
9 changes: 9 additions & 0 deletions pygmt/datatypes/cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Wrapper for the GMT_CUBE data type.
"""

import ctypes as ctp


class _GMT_CUBE(ctp.Structure): # noqa: N801
pass
9 changes: 9 additions & 0 deletions pygmt/datatypes/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Wrapper for the GMT_IMAGE data type.
"""

import ctypes as ctp


class _GMT_IMAGE(ctp.Structure): # noqa: N801
pass
Loading