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

pygmt.grdcut: Refactor to store output in virtualfiles for grids #3115

Draft
wants to merge 86 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
bcf43f0
Wrap GMT's standard data type GMT_IMAGE for images
seisman Mar 18, 2024
a052a1a
Initial implementation of to_dataarray method for _GMT_IMAGE class
weiji14 Mar 20, 2024
59d523c
pygmt.grdcut: Support both grid and image output
seisman Apr 16, 2024
56a6d65
Merge branch 'main' into datatypes/gmtimage
seisman Apr 17, 2024
3315324
Merge branch 'main' into gmtimage
seisman Apr 19, 2024
cea3374
Fix
seisman Apr 19, 2024
80d9837
Refactor
seisman Apr 19, 2024
22fba56
fix
seisman Apr 19, 2024
f71e79c
Merge branch 'main' into datatypes/gmtimage
weiji14 Jun 18, 2024
4cce4a2
Small typo fixes and add output type-hint for to_dataarray
weiji14 Jun 18, 2024
e02b650
Fix mypy error using np.array([0, 1, 2]) instead of np.arange
weiji14 Jun 18, 2024
f3d4b1f
Parse name and data_attrs from grid/image header
weiji14 Jun 18, 2024
4390136
Transpose array to (band, y, x) order and add doctest for to_dataarray
weiji14 Jun 20, 2024
5f25669
Set registration and gtype from header
weiji14 Jun 20, 2024
a3c6c14
Print basic shape and padding info in _GMT_IMAGE doctest
weiji14 Jun 20, 2024
5888e10
Only set Conventions = CF-1.7 attribute for NetCDF grid type
weiji14 Jun 20, 2024
798e658
Merge branch 'main' into datatypes/gmtimage
weiji14 Jun 20, 2024
3dbf2f2
Remove rioxarray import
weiji14 Jun 20, 2024
3a24ebd
Apply suggestions from code review
seisman Jun 20, 2024
4eee7e6
Merge branch 'main' into gmtimage
seisman Jun 20, 2024
5e390d4
Address reviewer's comments
seisman Jun 20, 2024
003383d
Fix GMT_OUT
seisman Jun 21, 2024
606ac7e
Merge branch 'main' into gmtimage
seisman Jun 21, 2024
c6cdcc8
Merge branch 'main' into gmtimage
seisman Jul 7, 2024
377941a
Revert changes for _GMT_IMAGE
seisman Jul 7, 2024
20617f5
Use rioxarray.open_rasterio for loading images
seisman Jul 7, 2024
a998718
Check if rioxarray is installed
seisman Jul 7, 2024
86cab44
Improve grdcut
seisman Jul 7, 2024
6031bab
Fix typos in grdcut
seisman Jul 7, 2024
eb0af2d
Add tests for grdcut images
seisman Jul 7, 2024
7f6ca7d
Fix one failing test
seisman Jul 7, 2024
21b194a
Fix open_rasterio
seisman Jul 7, 2024
e7eaf5c
Fix open_rasterio
seisman Jul 7, 2024
e3c8569
Make sure the image is loaded
seisman Jul 7, 2024
1c8312c
Update pygmt/clib/session.py
seisman Jul 7, 2024
3913430
Use rioxarray.open_rasterio in a context manager
seisman Jul 8, 2024
812a225
Merge branch 'main' into gmtimage
seisman Jul 8, 2024
90bd29e
Merge remote-tracking branch 'origin/gmtimage' into gmtimage
seisman Jul 8, 2024
ab77187
Fix mypy errors
seisman Jul 8, 2024
6f3e474
Move grdcut image tests to a separate test file
seisman Jul 8, 2024
5b07dd9
Fix copy & paste errors
seisman Jul 8, 2024
31272ab
Run codspeed benchmark for test_grdcut_image_dataarray
seisman Jul 8, 2024
6b860bf
Merge branch 'main' into datatypes/gmtimage
seisman Jul 27, 2024
5a09329
Merge branch 'main' into gmtimage
seisman Aug 5, 2024
279595b
Add the raster_kind function to determine the raster kind
seisman Aug 5, 2024
7def4b5
Simplify the grdcut function
seisman Aug 5, 2024
be175d8
Merge branch 'main' into gmtimage
seisman Sep 19, 2024
0bf9368
Merge branch 'main' into datatypes/gmtimage
seisman Sep 19, 2024
7d437be
Use enum for grid ids
seisman Sep 19, 2024
268e34e
Fix the band. Starting from 1
seisman Sep 19, 2024
86765e1
Refactor the tests for images
seisman Sep 19, 2024
86f3ffa
In np.reshape, a is a position-only parameter
seisman Sep 20, 2024
cc28247
Improve tests
seisman Sep 20, 2024
1e2c973
Fix one failing doctest due to xarray changes
seisman Sep 20, 2024
734dc28
The np.reshape's newshape parameter is deprecated
seisman Sep 20, 2024
919dc00
Define grid IDs using IntEnum instead of Enum
seisman Sep 20, 2024
b1eacf1
Pass the new shape as a positional parameter
seisman Sep 20, 2024
aa4fdc9
Fix failing tests
seisman Sep 20, 2024
c87a3ec
One more fix
seisman Sep 20, 2024
a20d8a2
One more fix
seisman Sep 20, 2024
926427b
Simplify a doctest
seisman Sep 20, 2024
c73328e
Improve the tests
seisman Sep 20, 2024
2825eae
Merge branch 'datatypes/gmtimage' into gmtimage
seisman Sep 20, 2024
bf9275c
Remove the workaround for images
seisman Sep 20, 2024
fb97daa
Convert ctypes array to numpy array using np.ctypeslib.as_array
seisman Sep 20, 2024
15b8d53
Fix the incorrect value due to floating number conversion in sphinter…
seisman Sep 20, 2024
8433e78
Merge branch 'ctypesarray' into datatypes/gmtimage
seisman Sep 20, 2024
3e3a6f3
Update the to_dataarray method to match the codes in GMT_GRID
seisman Sep 20, 2024
12ef40a
image data should has uint8 dtype
seisman Sep 20, 2024
f64fbb8
Further improve the tests
seisman Sep 21, 2024
e9cb0a5
Merge branch 'datatypes/gmtimage' into gmtimage
seisman Sep 21, 2024
4f2ae48
Merge branch 'main' into datatypes/gmtimage
seisman Sep 24, 2024
d49afed
Add a note that currently only 3-band images are supported
seisman Sep 24, 2024
a97d0b3
Apply suggestions from code review
seisman Sep 28, 2024
f70bec0
Merge branch 'main' into datatypes/gmtimage
seisman Sep 28, 2024
2fd13fb
Remove the old GMTGridID enums from pygmt/datatypes/header.py
seisman Sep 28, 2024
9972ba1
A minor fix
seisman Sep 28, 2024
ac6b7c3
Merge branch 'datatypes/gmtimage' into gmtimage
seisman Sep 28, 2024
7c32d41
Merge branch 'main' into gmtimage
seisman Sep 29, 2024
9ec00be
Let _raster_kind return grid by default
seisman Sep 29, 2024
f3a2f8e
Simplify the grdcut image tests
seisman Sep 29, 2024
3c12e2b
Add one more test for file in & file out
seisman Sep 29, 2024
f852b0d
Fix typos
seisman Sep 29, 2024
5f7683c
Merge branch 'main' into gmtimage
seisman Sep 30, 2024
bb1a0b0
Use the new load_blue_marble function
seisman Sep 30, 2024
584b5af
Drop the spatial_ref coord
seisman Sep 30, 2024
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
27 changes: 20 additions & 7 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
vectors_to_arrays,
)
from pygmt.clib.loading import load_libgmt
from pygmt.datatypes import _GMT_DATASET, _GMT_GRID
from pygmt.datatypes import _GMT_DATASET, _GMT_GRID, _GMT_IMAGE
from pygmt.exceptions import (
GMTCLibError,
GMTCLibNoSessionError,
GMTInvalidInput,
GMTVersionError,
)
from pygmt.helpers import (
GMTTempFile,
data_kind,
tempfile_from_geojson,
tempfile_from_image,
Expand Down Expand Up @@ -1697,7 +1698,9 @@

@contextlib.contextmanager
def virtualfile_out(
self, kind: Literal["dataset", "grid"] = "dataset", fname: str | None = None
self,
kind: Literal["dataset", "grid", "image"] = "dataset",
seisman marked this conversation as resolved.
Show resolved Hide resolved
fname: str | None = None,
):
r"""
Create a virtual file or an actual file for storing output data.
Expand All @@ -1710,8 +1713,8 @@
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"`` and ``"image"``. 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 @@ -1754,8 +1757,10 @@
family, geometry = {
"dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"),
"grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"),
"image": ("GMT_IS_IMAGE", "GMT_IS_SURFACE"),
}[kind]
with self.open_virtualfile(family, geometry, "GMT_OUT", None) as vfile:
direction = "GMT_OUT|GMT_IS_REFERENCE" if kind == "image" else "GMT_OUT"
with self.open_virtualfile(family, geometry, direction, None) as vfile:
yield vfile

def inquire_virtualfile(self, vfname: str) -> int:
Expand Down Expand Up @@ -1849,9 +1854,13 @@
# _GMT_DATASET).
if kind is None: # Return the ctypes void pointer
return pointer
if kind in ["image", "cube"]:
if kind == "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,
}[kind]
return ctp.cast(pointer, ctp.POINTER(dtype))

def virtualfile_to_dataset(
Expand Down Expand Up @@ -2066,6 +2075,10 @@
self["GMT_IS_IMAGE"]: "image",
self["GMT_IS_CUBE"]: "cube",
}[family]
if kind == "image":
with GMTTempFile(suffix=".tif") as tmpfile:
self.call_module("write", f"{vfname} {tmpfile.name} -Ti")
return xr.load_dataarray(tmpfile.name)

Check warning on line 2081 in pygmt/clib/session.py

View check run for this annotation

Codecov / codecov/patch

pygmt/clib/session.py#L2079-L2081

Added lines #L2079 - L2081 were not covered by tests
seisman marked this conversation as resolved.
Show resolved Hide resolved
return self.read_virtualfile(vfname, kind=kind).contents.to_dataarray()

def extract_region(self):
Expand Down
1 change: 1 addition & 0 deletions pygmt/datatypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

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/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
weiji14 marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 23 additions & 14 deletions pygmt/src/grdcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@

from pygmt.clib import Session
from pygmt.helpers import (
GMTTempFile,
build_arg_list,
data_kind,
fmt_docstring,
kwargs_to_strings,
use_alias,
)
from pygmt.io import load_dataarray
from pygmt.src.which import which

__doctest_skip__ = ["grdcut"]


@fmt_docstring
@use_alias(
G="outgrid",
R="region",
J="projection",
N="extend",
Expand All @@ -27,9 +26,9 @@
f="coltypes",
)
@kwargs_to_strings(R="sequence")
def grdcut(grid, **kwargs):
def grdcut(grid, outgrid: str | None = None, **kwargs):
r"""
Extract subregion from a grid.
Extract subregion from a grid or image.

Produce a new ``outgrid`` file which is a subregion of ``grid``. The
subregion is specified with ``region``; the specified range must not exceed
Expand Down Expand Up @@ -99,13 +98,23 @@
>>> # 12° E to 15° E and a latitude range of 21° N to 24° N
>>> new_grid = pygmt.grdcut(grid=grid, region=[12, 15, 21, 24])
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdcut", args=build_arg_list(kwargs, infile=vingrd)
)
inkind = data_kind(grid)
match inkind:
case "image" | "grid":
outkind = inkind
case "file":
realpath = which(grid, download="a")
if isinstance(realpath, list):
realpath = realpath[0]
outkind = "image" if realpath.endswith(".tif") else "grid"

Check warning on line 109 in pygmt/src/grdcut.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/grdcut.py#L106-L109

Added lines #L106 - L109 were not covered by tests
seisman marked this conversation as resolved.
Show resolved Hide resolved

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
with Session() as lib:
with (
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
lib.virtualfile_out(kind=outkind, fname=outgrid) as voutgrd,
):
kwargs["G"] = voutgrd
lib.call_module(module="grdcut", args=build_arg_list(kwargs, infile=vingrd))
return lib.virtualfile_to_raster(
outgrid=outgrid, kind=outkind, vfname=voutgrd
)
seisman marked this conversation as resolved.
Show resolved Hide resolved