Skip to content

Commit 108266e

Browse files
Test interp nominal lon (#365)
1 parent 6a9179f commit 108266e

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CITATION.cff

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ authors:
1212
orcid: "https://orcid.org/0000-0002-1660-7822"
1313
- family-names: "Nicholas"
1414
given-names: "Thomas"
15-
orcid: "0000-0002-2176-0530"
15+
orcid: "https://orcid.org/0000-0002-2176-0530"
1616
- family-names: "Magin"
1717
given-names: "Justus"
1818
orcid: "https://orcid.org/0000-0002-4254-8002"
@@ -21,6 +21,10 @@ authors:
2121
given-names: "Markus"
2222
orcid: "https://orcid.org/0000-0001-7464-7075"
2323
affiliation: "Universität Hamburg, Germany"
24+
- family-names: "Angevaare"
25+
given-names: "Joran J. R."
26+
orcid: "https://orcid.org/0000-0003-3392-8123"
27+
affiliation: "KNMI"
2428

2529
title: "xMIP"
2630
url: "https://github.com/jbusecke/xMIP"

tests/test_preprocessing.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
rename_cmip6,
2121
replace_x_y_nominal_lat_lon,
2222
sort_vertex_order,
23+
_interp_nominal_lon,
2324
)
2425

2526

@@ -191,6 +192,44 @@ def test_replace_x_y_nominal_lat_lon(dask, nans):
191192
assert set(replaced_ds.lat.dims) == set(["x", "y"])
192193

193194

195+
def test_interp_nominal_lon():
196+
"""
197+
Check that https://github.com/jbusecke/xMIP/issues/295 was fixed in https://github.com/jbusecke/xMIP/pull/296
198+
199+
In https://github.com/jbusecke/xMIP/blob/0270f4b4977d512adc2337d4a547b39e25d2f2da/tests/test_preprocessing.py,
200+
the old issue was replicated (and illustrated that the tests would have failed then).
201+
"""
202+
203+
def _get_dummy_longitude() -> np.ndarray:
204+
# Totally arbitrary data (although len(lon) has to be > 360 to see the issue)
205+
lon = np.linspace(0, 360, 513)[:-1]
206+
207+
# Add some NaN values just as an example
208+
lon[2 + 30 : len(lon) // 2 + 50] = np.nan
209+
return lon
210+
211+
def _lons_parsed_make_sense(
212+
input_lons: np.ndarray, lons_parsed: np.ndarray
213+
) -> bool:
214+
"""
215+
Check if the parsed longitudes make sense.
216+
Since we know that the input-lons are all monotonically increasing, the parsed lons should also do that.
217+
"""
218+
accepted_differences_between_lon_coords = np.unique(np.diff(input_lons))
219+
if len(accepted_differences_between_lon_coords) not in [1, 2]:
220+
raise RuntimeError(
221+
f"Cannot work with changed format of inputdata {accepted_differences_between_lon_coords}"
222+
)
223+
diff_pars_lons = np.unique(np.diff(lons_parsed))
224+
return np.all(
225+
[x in accepted_differences_between_lon_coords for x in diff_pars_lons]
226+
)
227+
228+
lons = _get_dummy_longitude()
229+
lons_parsed = _interp_nominal_lon(lons)
230+
assert _lons_parsed_make_sense(lons, lons_parsed)
231+
232+
194233
@pytest.mark.parametrize(
195234
"coord",
196235
[

xmip/preprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def broadcast_lonlat(ds, verbose=True):
137137
return ds
138138

139139

140-
def _interp_nominal_lon(lon_1d):
140+
def _interp_nominal_lon(lon_1d: np.ndarray) -> np.ndarray:
141141
x = np.arange(len(lon_1d))
142142
idx = np.isnan(lon_1d)
143143
# Assume that longitudes are cyclic (i.e. that the period equals the length of lon)

0 commit comments

Comments
 (0)