Skip to content

Commit 278f21e

Browse files
committed
Refactor load_remote_dataset using the special read module
1 parent 62872d3 commit 278f21e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

pygmt/datasets/load_remote_dataset.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
from typing import TYPE_CHECKING, ClassVar, NamedTuple
88

9+
from pygmt.clib import Session
910
from pygmt.exceptions import GMTInvalidInput
10-
from pygmt.helpers import kwargs_to_strings
11-
from pygmt.io import load_dataarray
12-
from pygmt.src import grdcut, which
11+
from pygmt.helpers import build_arg_list, kwargs_to_strings
12+
from pygmt.src import which
1313

1414
if TYPE_CHECKING:
1515
import xarray as xr
@@ -406,18 +406,27 @@ def _load_remote_dataset(
406406
)
407407
reg = f"_{registration[0]}"
408408

409-
# different ways to load tiled and non-tiled grids.
410-
# Known issue: tiled grids don't support slice operation
411-
# See https://github.com/GenericMappingTools/pygmt/issues/524
412-
if region is None:
413-
if dataset.resolutions[resolution].tiled:
409+
fname = f"@{dataset_prefix}{resolution}{reg}"
410+
if dataset.resolutions[resolution].tiled:
411+
if region is None:
414412
raise GMTInvalidInput(
415413
f"'region' is required for {dataset.title} resolution '{resolution}'."
416414
)
417-
fname = which(f"@{dataset_prefix}{resolution}{reg}", download="a")
418-
grid = load_dataarray(fname, engine="netcdf4")
415+
fullname = None
419416
else:
420-
grid = grdcut(f"@{dataset_prefix}{resolution}{reg}", region=region)
417+
fullname = which(fname, download="a")
418+
419+
# Currently, only grids are supported. Should support images in the future.
420+
kwdict = {"T": "g", "R": region} # region can be None
421+
with Session() as lib:
422+
with lib.virtualfile_out(kind="grid") as voutgrd:
423+
lib.call_module(
424+
module="read",
425+
args=[fname, voutgrd, *build_arg_list(kwdict)],
426+
)
427+
grid = lib.virtualfile_to_raster(outgrid=None, vfname=voutgrd)
428+
if fullname is not None:
429+
grid.encoding["source"] = fullname
421430

422431
# Add some metadata to the grid
423432
grid.name = dataset.name

pygmt/tests/test_accessor.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ def test_accessor_grid_source_file_not_exist():
115115
# Registration and gtype are correct
116116
assert grid.gmt.registration == 1
117117
assert grid.gmt.gtype == 1
118-
# The source grid file is defined but doesn't exist
119-
assert grid.encoding["source"].endswith(".nc")
120-
assert not Path(grid.encoding["source"]).exists()
118+
# The source grid file is undefined.
119+
assert grid.encoding.get("source") is None
121120

122121
# For a sliced grid, fallback to default registration and gtype,
123122
# because the source grid file doesn't exist.

0 commit comments

Comments
 (0)