2
2
Internal function to load GMT remote datasets.
3
3
"""
4
4
5
- from __future__ import annotations
6
-
7
- from typing import TYPE_CHECKING , ClassVar , Literal , NamedTuple
5
+ from collections .abc import Sequence
6
+ from typing import Any , Literal , NamedTuple
8
7
8
+ import xarray as xr
9
9
from pygmt .clib import Session
10
10
from pygmt .exceptions import GMTInvalidInput
11
11
from pygmt .helpers import build_arg_list , kwargs_to_strings
12
12
from pygmt .src import which
13
13
14
- if TYPE_CHECKING :
15
- from collections .abc import Sequence
16
-
17
- import xarray as xr
18
-
19
14
20
15
class Resolution (NamedTuple ):
21
16
"""
22
17
Resolution code, the available grid registrations and whether it is tiled.
23
18
24
19
Attributes
25
20
----------
26
- code : str
21
+ code
27
22
The resolution code. E.g., "01d", "30m", "01s".
28
- registrations : list
23
+ registrations
29
24
A list of the accepted registrations for a given resolution. Can be either
30
25
"pixel" or "gridline".
31
- tiled : bool
26
+ tiled
32
27
States if the grid is tiled, which requires an argument for ``region``.
33
28
"""
34
29
35
30
code : str
36
- registrations : ClassVar [ list ] = ["gridline" , "pixel" ]
31
+ registrations : Sequence [ str ] = ["gridline" , "pixel" ]
37
32
tiled : bool = False
38
33
39
34
@@ -43,20 +38,20 @@ class GMTRemoteDataset(NamedTuple):
43
38
44
39
Attributes
45
40
----------
46
- description : str
41
+ description
47
42
The name assigned as an attribute to the DataArray.
48
- units : str, None
43
+ units
49
44
The units of the values in the DataArray.
50
- resolutions : dict
45
+ resolutions
51
46
Dictionary of available resolution as keys and Resolution objects as values.
52
- extra_attributes : dict
47
+ extra_attributes
53
48
A dictionary of extra or unique attributes of the dataset.
54
49
"""
55
50
56
51
description : str
57
52
units : str | None
58
53
resolutions : dict [str , Resolution ]
59
- extra_attributes : dict
54
+ extra_attributes : dict [ str , Any ]
60
55
61
56
62
57
datasets = {
@@ -389,9 +384,8 @@ def _load_remote_dataset(
389
384
The grid resolution. The suffix ``d``, ``m``, and ``s`` stand for arc-degrees,
390
385
arc-minutes, and arc-seconds, respectively.
391
386
region
392
- The subregion of the grid to load, in the form of a list
393
- [*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*.
394
- Required for tiled grids.
387
+ The subregion of the grid to load, in the form of a sequence [*xmin*, *xmax*,
388
+ *ymin*, *ymax*] or an ISO country code. Required for tiled grids.
395
389
registration
396
390
Grid registration type. Either ``"pixel"`` for pixel registration or
397
391
``"gridline"`` for gridline registration. Default is ``None``, where
@@ -417,41 +411,39 @@ def _load_remote_dataset(
417
411
418
412
# Check resolution
419
413
if resolution not in dataset .resolutions :
420
- raise GMTInvalidInput (
414
+ msg = (
421
415
f"Invalid resolution '{ resolution } ' for { dataset .description } dataset. "
422
416
f"Available resolutions are: { ', ' .join (dataset .resolutions )} ."
423
417
)
418
+ raise GMTInvalidInput (msg )
424
419
resinfo = dataset .resolutions [resolution ]
425
420
426
421
# Check registration
427
- if registration is None :
428
- # Use gridline registration unless only pixel registration is available
429
- registration = "gridline" if "gridline" in resinfo .registrations else "pixel"
430
- elif registration in {"pixel" , "gridline" }:
431
- if registration not in resinfo .registrations :
432
- raise GMTInvalidInput (
433
- f"{ registration } registration is not available for the "
434
- f"{ resolution } { dataset .description } dataset. Only "
435
- f"{ resinfo .registrations [0 ]} registration is available."
422
+ match registration :
423
+ case None :
424
+ # Use gridline registration unless only pixel registration is available
425
+ reg = "g" if "gridline" in resinfo .registrations else "p"
426
+ case x if x not in resinfo .registrations :
427
+ msg = (
428
+ f"Invalid grid registration '{ registration } ' for the { resolution } "
429
+ f"{ dataset .description } dataset. Should be either 'pixel', 'gridline' "
430
+ "or None. Default is None, where a gridline-registered grid is "
431
+ "returned unless only the pixel-registered grid is available."
436
432
)
437
- else :
438
- raise GMTInvalidInput (
439
- f"Invalid grid registration: '{ registration } ', should be either 'pixel', "
440
- "'gridline' or None. Default is None, where a gridline-registered grid is "
441
- "returned unless only the pixel-registered grid is available."
442
- )
433
+ raise GMTInvalidInput (msg )
434
+ case _:
435
+ reg = registration [0 ]
443
436
444
- fname = f"@{ prefix } _{ resolution } _{ registration [0 ]} " # type: ignore[index]
445
437
if resinfo .tiled and region is None :
446
- raise GMTInvalidInput (
447
- f"'region' is required for { dataset .description } resolution '{ resolution } '."
438
+ msg = (
439
+ f"The 'region' parameter is required for { dataset .description } "
440
+ f"resolution '{ resolution } '."
448
441
)
442
+ raise GMTInvalidInput (msg )
449
443
444
+ fname = f"@{ prefix } _{ resolution } _{ reg } "
450
445
kind = "image" if name in {"earth_day" , "earth_night" } else "grid"
451
- kwdict = {
452
- "R" : region , # region can be None
453
- "T" : "i" if kind == "image" else "g" ,
454
- }
446
+ kwdict = {"R" : region , "T" : {"grid" : "g" , "image" : "i" }[kind ]}
455
447
with Session () as lib :
456
448
with lib .virtualfile_out (kind = kind ) as voutgrd :
457
449
lib .call_module (
0 commit comments