Skip to content

Commit c833f88

Browse files
willschlitzerseismanweiji14
authored
Set gridline (if available) as the default grid registration for remote datasets (#2266)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 016ab9a commit c833f88

14 files changed

+184
-195
lines changed

pygmt/datasets/earth_age.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def load_earth_age(resolution="01d", region=None, registration=None):
4444
4545
registration : str
4646
Grid registration type. Either ``"pixel"`` for pixel registration or
47-
``"gridline"`` for gridline registration. Default is ``None``, where
48-
a pixel-registered grid is returned unless only the
49-
gridline-registered grid is available.
47+
``"gridline"`` for gridline registration. Default is ``"gridline"``.
5048
5149
Returns
5250
-------

pygmt/datasets/earth_free_air_anomaly.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ def load_earth_free_air_anomaly(resolution="01d", region=None, registration=None
4444
4545
registration : str
4646
Grid registration type. Either ``"pixel"`` for pixel registration or
47-
``"gridline"`` for gridline registration. Default is ``None``, where
48-
a pixel-registered grid is returned unless only the
49-
gridline-registered grid is available.
47+
``"gridline"`` for gridline registration. Default is ``"gridline"``
48+
for all resolutions except ``"01m"`` which is ``"pixel"`` only.
5049
5150
Returns
5251
-------

pygmt/datasets/earth_geoid.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def load_earth_geoid(resolution="01d", region=None, registration=None):
4444
4545
registration : str
4646
Grid registration type. Either ``"pixel"`` for pixel registration or
47-
``"gridline"`` for gridline registration. Default is ``None``, where
48-
a pixel-registered grid is returned unless only the
49-
gridline-registered grid is available.
47+
``"gridline"`` for gridline registration. Default is ``"gridline"``.
5048
5149
Returns
5250
-------

pygmt/datasets/earth_magnetic_anomaly.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ def load_earth_magnetic_anomaly(
4949
5050
registration : str
5151
Grid registration type. Either ``"pixel"`` for pixel registration or
52-
``"gridline"`` for gridline registration. Default is ``None``, where
53-
a pixel-registered grid is returned unless only the
54-
gridline-registered grid is available.
52+
``"gridline"`` for gridline registration. Default is ``"gridline"``
53+
for all resolutions except ``"02m"`` which is ``"pixel"`` only.
5554
5655
mag4km : bool
5756
Choose the data version to use. The default is ``False``, which is

pygmt/datasets/earth_relief.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ def load_earth_relief(
5757
5858
registration : str
5959
Grid registration type. Either ``"pixel"`` for pixel registration or
60-
``"gridline"`` for gridline registration. Default is ``None``, where
61-
a gridline-registered grid is returned unless only the pixel-registered
62-
grid is available.
63-
64-
**Note**: For GMT 6.3, ``registration=None`` returns a pixel-registered
65-
grid by default unless only the gridline-registered grid is available.
60+
``"gridline"`` for gridline registration. Default is ``"gridline"``
61+
for all resolutions except ``"15s"`` which is ``"pixel"`` only.
6662
6763
data_source : str
6864
Select the source for the Earth relief data.

pygmt/datasets/earth_vertical_gravity_gradient.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ def load_earth_vertical_gravity_gradient(
4545
4646
registration : str
4747
Grid registration type. Either ``"pixel"`` for pixel registration or
48-
``"gridline"`` for gridline registration. Default is ``None``, where
49-
a pixel-registered grid is returned unless only the
50-
gridline-registered grid is available.
48+
``"gridline"`` for gridline registration. Default is ``"gridline"``
49+
for all resolutions except ``"01m"`` which is ``"pixel"`` only.
5150
5251
Returns
5352
-------

pygmt/datasets/load_remote_dataset.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,26 @@ def _load_remote_dataset(
230230
The returned :class:`xarray.DataArray` doesn't support slice operation for
231231
tiled grids.
232232
"""
233-
234-
if registration in ("pixel", "gridline", None):
235-
# If None, let GMT decide on Pixel/Gridline type
236-
reg = f"_{registration[0]}" if registration else ""
233+
dataset = datasets[dataset_name]
234+
if resolution not in dataset.resolutions.keys():
235+
raise GMTInvalidInput(f"Invalid resolution '{resolution}'.")
236+
if registration is None:
237+
# Check if "gridline" is an available registration for the resolution
238+
if "gridline" in dataset.resolutions[resolution].registrations:
239+
# Use default of gridline registration if available
240+
registration = "gridline"
241+
else:
242+
registration = "pixel"
243+
if registration in ("pixel", "gridline"):
244+
reg = f"_{registration[0]}"
237245
else:
238246
raise GMTInvalidInput(
239247
f"Invalid grid registration: '{registration}', should be either "
240248
"'pixel', 'gridline' or None. Default is None, where a "
241-
"pixel-registered grid is returned unless only the "
242-
"gridline-registered grid is available."
249+
"gridline-registered grid is returned unless only the "
250+
"pixel-registered grid is available."
243251
)
244-
dataset = datasets[dataset_name]
245-
if resolution not in dataset.resolutions.keys():
246-
raise GMTInvalidInput(f"Invalid resolution '{resolution}'.")
252+
247253
if registration and (
248254
registration not in dataset.resolutions[resolution].registrations
249255
):

pygmt/helpers/testing.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,26 @@ def download_test_data():
175175
"@N37W120.earth_relief_03s_g.nc",
176176
"@N00W090.earth_relief_03m_p.nc",
177177
"@N00E135.earth_relief_30s_g.nc",
178+
"@N00W010.earth_relief_15s_p.nc", # Specific grid for 15s test
179+
"@N04W010.earth_relief_03s_g.nc", # Specific grid for 03s test
178180
# Earth synbath relief grids
179181
"@S15W105.earth_synbath_30s_p.nc",
180182
# Earth seafloor age grids
181183
"@earth_age_01d_g",
182-
"@S90W180.earth_age_05m_g.nc", # Specific grid for 05m test
184+
"@N00W030.earth_age_01m_g.nc", # Specific grid for 01m test
183185
# Earth geoid grids
184186
"@earth_geoid_01d_g",
185-
"@S90W180.earth_geoid_05m_g.nc", # Specific grid for 05m test
187+
"@N00W030.earth_geoid_01m_g.nc", # Specific grid for 01m test
186188
# Earth magnetic anomaly grids
187189
"@earth_mag_01d_g",
188-
"@S90W180.earth_mag_05m_g.nc", # Specific grid for 05m test
190+
"@S30W060.earth_mag_02m_p.nc", # Specific grid for 02m test
189191
"@earth_mag4km_01d_g",
190-
"@S90W180.earth_mag4km_05m_g.nc", # Specific grid for 05m test
191192
# Earth free-air anomaly grids
192193
"@earth_faa_01d_g",
193-
"@S90W180.earth_faa_05m_g.nc", # Specific grid for 05m test
194+
"@N00W030.earth_faa_01m_p.nc", # Specific grid for 01m test
194195
# Earth vertical gravity gradient grids
195196
"@earth_vgg_01d_g",
196-
"@S90W180.earth_vgg_05m_g.nc", # Specific grid for 05m test
197+
"@N00W030.earth_vgg_01m_p.nc", # Specific grid for 01m test
197198
# Other cache files
198199
"@capitals.gmt",
199200
"@earth_relief_20m_holes.grd",

pygmt/tests/test_datasets_earth_age.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ def test_earth_age_01d():
3131
"""
3232
Test some properties of the earth age 01d data.
3333
"""
34-
data = load_earth_age(resolution="01d", registration="gridline")
34+
data = load_earth_age(resolution="01d")
3535
assert data.name == "seafloor_age"
3636
assert data.attrs["units"] == "Myr"
3737
assert data.attrs["long_name"] == "age of seafloor crust"
3838
assert data.attrs["horizontal_datum"] == "WGS84"
3939
assert data.shape == (181, 361)
40+
assert data.gmt.registration == 0
4041
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
4142
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
4243
npt.assert_allclose(data.min(), 0.167381, rtol=1e-5)
@@ -47,39 +48,21 @@ def test_earth_age_01d_with_region():
4748
"""
4849
Test loading low-resolution earth age with 'region'.
4950
"""
50-
data = load_earth_age(
51-
resolution="01d", region=[-10, 10, -5, 5], registration="gridline"
52-
)
51+
data = load_earth_age(resolution="01d", region=[-10, 10, -5, 5])
5352
assert data.shape == (11, 21)
53+
assert data.gmt.registration == 0
5454
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
5555
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
5656
npt.assert_allclose(data.min(), 11.293945)
5757
npt.assert_allclose(data.max(), 125.1189)
5858

5959

60-
def test_earth_age_05m_with_region():
61-
"""
62-
Test loading a subregion of high-resolution earth age.
63-
"""
64-
data = load_earth_age(
65-
resolution="05m", region=[-50, -40, 20, 30], registration="gridline"
66-
)
67-
assert data.coords["lat"].data.min() == 20.0
68-
assert data.coords["lat"].data.max() == 30.0
69-
assert data.coords["lon"].data.min() == -50.0
70-
assert data.coords["lon"].data.max() == -40.0
71-
npt.assert_allclose(data.data.min(), 0.040000916)
72-
npt.assert_allclose(data.data.max(), 46.530003)
73-
assert data.sizes["lat"] == 121
74-
assert data.sizes["lon"] == 121
75-
76-
77-
def test_earth_age_05m_without_region():
60+
def test_earth_age_01m_without_region():
7861
"""
7962
Test loading high-resolution earth age without passing 'region'.
8063
"""
8164
with pytest.raises(GMTInvalidInput):
82-
load_earth_age("05m")
65+
load_earth_age("01m")
8366

8467

8568
def test_earth_age_incorrect_resolution_registration():
@@ -89,3 +72,19 @@ def test_earth_age_incorrect_resolution_registration():
8972
"""
9073
with pytest.raises(GMTInvalidInput):
9174
load_earth_age(resolution="01m", region=[0, 1, 3, 5], registration="pixel")
75+
76+
77+
def test_earth_age_01m_default_registration():
78+
"""
79+
Test that the grid returned by default for the 1 arc-minute resolution has
80+
a "gridline" registration.
81+
"""
82+
data = load_earth_age(resolution="01m", region=[-10, -9, 3, 5])
83+
assert data.shape == (121, 61)
84+
assert data.gmt.registration == 0
85+
assert data.coords["lat"].data.min() == 3.0
86+
assert data.coords["lat"].data.max() == 5.0
87+
assert data.coords["lon"].data.min() == -10.0
88+
assert data.coords["lon"].data.max() == -9.0
89+
npt.assert_allclose(data.min(), 88.63)
90+
npt.assert_allclose(data.max(), 125.25)

pygmt/tests/test_datasets_earth_free_air_anomaly.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ def test_earth_faa_01d():
3131
"""
3232
Test some properties of the free air anomaly 01d data.
3333
"""
34-
data = load_earth_free_air_anomaly(resolution="01d", registration="gridline")
34+
data = load_earth_free_air_anomaly(resolution="01d")
3535
assert data.name == "free_air_anomaly"
3636
assert data.attrs["long_name"] == "IGPP Global Earth Free-Air Anomaly"
3737
assert data.attrs["units"] == "mGal"
3838
assert data.attrs["horizontal_datum"] == "WGS84"
3939
assert data.shape == (181, 361)
40+
assert data.gmt.registration == 0
4041
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
4142
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
4243
npt.assert_allclose(data.min(), -275.75)
@@ -47,36 +48,35 @@ def test_earth_faa_01d_with_region():
4748
"""
4849
Test loading low-resolution earth free air anomaly with 'region'.
4950
"""
50-
data = load_earth_free_air_anomaly(
51-
resolution="01d", region=[-10, 10, -5, 5], registration="gridline"
52-
)
51+
data = load_earth_free_air_anomaly(resolution="01d", region=[-10, 10, -5, 5])
5352
assert data.shape == (11, 21)
53+
assert data.gmt.registration == 0
5454
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
5555
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
5656
npt.assert_allclose(data.min(), -58.75)
5757
npt.assert_allclose(data.max(), 69.524994)
5858

5959

60-
def test_earth_faa_05m_with_region():
60+
def test_earth_faa_01m_without_region():
6161
"""
62-
Test loading a subregion of high-resolution earth free air anomaly data.
62+
Test loading high-resolution earth free air anomaly without passing
63+
'region'.
6364
"""
64-
data = load_earth_free_air_anomaly(
65-
resolution="05m", region=[-115, -112, 4, 6], registration="gridline"
66-
)
67-
assert data.shape == (25, 37)
68-
assert data.lat.min() == 4
69-
assert data.lat.max() == 6
70-
assert data.lon.min() == -115
71-
assert data.lon.max() == -112
72-
npt.assert_allclose(data.min(), -20.5)
73-
npt.assert_allclose(data.max(), -3.9500122)
65+
with pytest.raises(GMTInvalidInput):
66+
load_earth_free_air_anomaly("01m")
7467

7568

76-
def test_earth_faa_05m_without_region():
69+
def test_earth_faa_01m_default_registration():
7770
"""
78-
Test loading high-resolution earth free air anomaly without passing
79-
'region'.
71+
Test that the grid returned by default for the 1 arc-minute resolution has
72+
a "pixel" registration.
8073
"""
81-
with pytest.raises(GMTInvalidInput):
82-
load_earth_free_air_anomaly("05m")
74+
data = load_earth_free_air_anomaly(resolution="01m", region=[-10, -9, 3, 5])
75+
assert data.shape == (120, 60)
76+
assert data.gmt.registration == 1
77+
npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333)
78+
npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666)
79+
npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666)
80+
npt.assert_allclose(data.coords["lon"].data.max(), -9.00833333)
81+
npt.assert_allclose(data.min(), -51)
82+
npt.assert_allclose(data.max(), 113.675)

0 commit comments

Comments
 (0)