Skip to content

Commit

Permalink
Test interp nominal lon (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoranAngevaare authored Jul 9, 2024
1 parent 6a9179f commit 108266e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors:
orcid: "https://orcid.org/0000-0002-1660-7822"
- family-names: "Nicholas"
given-names: "Thomas"
orcid: "0000-0002-2176-0530"
orcid: "https://orcid.org/0000-0002-2176-0530"
- family-names: "Magin"
given-names: "Justus"
orcid: "https://orcid.org/0000-0002-4254-8002"
Expand All @@ -21,6 +21,10 @@ authors:
given-names: "Markus"
orcid: "https://orcid.org/0000-0001-7464-7075"
affiliation: "Universität Hamburg, Germany"
- family-names: "Angevaare"
given-names: "Joran J. R."
orcid: "https://orcid.org/0000-0003-3392-8123"
affiliation: "KNMI"

title: "xMIP"
url: "https://github.com/jbusecke/xMIP"
Expand Down
39 changes: 39 additions & 0 deletions tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
rename_cmip6,
replace_x_y_nominal_lat_lon,
sort_vertex_order,
_interp_nominal_lon,
)


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


def test_interp_nominal_lon():
"""
Check that https://github.com/jbusecke/xMIP/issues/295 was fixed in https://github.com/jbusecke/xMIP/pull/296
In https://github.com/jbusecke/xMIP/blob/0270f4b4977d512adc2337d4a547b39e25d2f2da/tests/test_preprocessing.py,
the old issue was replicated (and illustrated that the tests would have failed then).
"""

def _get_dummy_longitude() -> np.ndarray:
# Totally arbitrary data (although len(lon) has to be > 360 to see the issue)
lon = np.linspace(0, 360, 513)[:-1]

# Add some NaN values just as an example
lon[2 + 30 : len(lon) // 2 + 50] = np.nan
return lon

def _lons_parsed_make_sense(
input_lons: np.ndarray, lons_parsed: np.ndarray
) -> bool:
"""
Check if the parsed longitudes make sense.
Since we know that the input-lons are all monotonically increasing, the parsed lons should also do that.
"""
accepted_differences_between_lon_coords = np.unique(np.diff(input_lons))
if len(accepted_differences_between_lon_coords) not in [1, 2]:
raise RuntimeError(
f"Cannot work with changed format of inputdata {accepted_differences_between_lon_coords}"
)
diff_pars_lons = np.unique(np.diff(lons_parsed))
return np.all(
[x in accepted_differences_between_lon_coords for x in diff_pars_lons]
)

lons = _get_dummy_longitude()
lons_parsed = _interp_nominal_lon(lons)
assert _lons_parsed_make_sense(lons, lons_parsed)


@pytest.mark.parametrize(
"coord",
[
Expand Down
2 changes: 1 addition & 1 deletion xmip/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def broadcast_lonlat(ds, verbose=True):
return ds


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

0 comments on commit 108266e

Please sign in to comment.