Skip to content

Commit 72ce692

Browse files
Simplify the load_earth_magnetic_anomaly and load_earth_relief functions (#3203)
Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent 9d234fe commit 72ce692

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

pygmt/datasets/earth_magnetic_anomaly.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,20 @@ def load_earth_magnetic_anomaly(
136136
... resolution="20m", registration="gridline", data_source="wdmam"
137137
... )
138138
"""
139-
magnetic_anomaly_sources = {
139+
# Map data source to prefix
140+
prefix = {
140141
"emag2": "earth_mag",
141142
"emag2_4km": "earth_mag4km",
142143
"wdmam": "earth_wdmam",
143-
}
144-
if data_source not in magnetic_anomaly_sources:
144+
}.get(data_source)
145+
if prefix is None:
145146
raise GMTInvalidInput(
146147
f"Invalid earth magnetic anomaly data source '{data_source}'. "
147148
"Valid values are 'emag2', 'emag2_4km', and 'wdmam'."
148149
)
149-
dataset_prefix = magnetic_anomaly_sources[data_source]
150-
dataset_name = "earth_wdmam" if data_source == "wdmam" else "earth_mag"
151150
grid = _load_remote_dataset(
152-
name=dataset_name,
153-
prefix=dataset_prefix,
151+
name="earth_wdmam" if data_source == "wdmam" else "earth_mag",
152+
prefix=prefix,
154153
resolution=resolution,
155154
region=region,
156155
registration=registration,

pygmt/datasets/earth_relief.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,37 +139,35 @@ def load_earth_relief(
139139
# resolutions of original land-only SRTM tiles from NASA
140140
land_only_srtm_resolutions = ["03s", "01s"]
141141

142-
earth_relief_sources = {
142+
# Map data source to prefix
143+
prefix = {
143144
"igpp": "earth_relief",
144145
"gebco": "earth_gebco",
145146
"gebcosi": "earth_gebcosi",
146147
"synbath": "earth_synbath",
147-
}
148-
if data_source not in earth_relief_sources:
148+
}.get(data_source)
149+
if prefix is None:
149150
raise GMTInvalidInput(
150151
f"Invalid earth relief data source '{data_source}'. "
151-
"Valid values are 'igpp', 'gebco', 'gebcosi' and 'synbath'."
152+
"Valid values are 'igpp', 'gebco', 'gebcosi', and 'synbath'."
152153
)
153-
# Choose earth relief data prefix
154+
# Use SRTM or not.
154155
if use_srtm and resolution in land_only_srtm_resolutions:
155-
if data_source == "igpp":
156-
dataset_prefix = "srtm_relief"
157-
else:
156+
if data_source != "igpp":
158157
raise GMTInvalidInput(
159158
f"Option 'use_srtm=True' doesn't work with data source '{data_source}'."
160159
" Please set 'data_source' to 'igpp'."
161160
)
162-
else:
163-
dataset_prefix = earth_relief_sources[data_source]
161+
prefix = "srtm_relief"
164162
# Choose earth relief dataset
165163
match data_source:
166164
case "igpp" | "synbath":
167-
dataset_name = "earth_igpp"
165+
name = "earth_igpp"
168166
case "gebco" | "gebcosi":
169-
dataset_name = "earth_gebco"
167+
name = "earth_gebco"
170168
grid = _load_remote_dataset(
171-
name=dataset_name,
172-
prefix=dataset_prefix,
169+
name=name,
170+
prefix=prefix,
173171
resolution=resolution,
174172
region=region,
175173
registration=registration,

0 commit comments

Comments
 (0)