Skip to content

Commit 5648846

Browse files
committed
Refactor load_remote_dataset using the special read module
1 parent 809880c commit 5648846

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

pygmt/datasets/load_remote_dataset.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
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
1312

1413
if TYPE_CHECKING:
1514
import xarray as xr
@@ -406,18 +405,24 @@ def _load_remote_dataset(
406405
)
407406
reg = f"_{registration[0]}"
408407

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:
414-
raise GMTInvalidInput(
415-
f"'region' is required for {dataset.title} resolution '{resolution}'."
408+
if region is None and dataset.resolutions[resolution].tiled:
409+
raise GMTInvalidInput(
410+
f"'region' is required for {dataset.title} resolution '{resolution}'."
411+
)
412+
413+
# Currently, only grids are supported. Should support images in the future.
414+
kwdict = {"T": "g", "R": region} # region can be None
415+
with Session() as lib:
416+
with lib.virtualfile_out(kind="grid") as voutgrd:
417+
lib.call_module(
418+
module="read",
419+
args=[
420+
f"@{dataset_prefix}{resolution}{reg}",
421+
voutgrd,
422+
*build_arg_list(kwdict),
423+
],
416424
)
417-
fname = which(f"@{dataset_prefix}{resolution}{reg}", download="a")
418-
grid = load_dataarray(fname, engine="netcdf4")
419-
else:
420-
grid = grdcut(f"@{dataset_prefix}{resolution}{reg}", region=region)
425+
grid = lib.virtualfile_to_raster(outgrid=None, vfname=voutgrd)
421426

422427
# Add some metadata to the grid
423428
grid.name = dataset.name

0 commit comments

Comments
 (0)