|
20 | 20 | rename_cmip6,
|
21 | 21 | replace_x_y_nominal_lat_lon,
|
22 | 22 | sort_vertex_order,
|
| 23 | + _interp_nominal_lon, |
23 | 24 | )
|
24 | 25 |
|
25 | 26 |
|
@@ -191,6 +192,44 @@ def test_replace_x_y_nominal_lat_lon(dask, nans):
|
191 | 192 | assert set(replaced_ds.lat.dims) == set(["x", "y"])
|
192 | 193 |
|
193 | 194 |
|
| 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 | + |
194 | 233 | @pytest.mark.parametrize(
|
195 | 234 | "coord",
|
196 | 235 | [
|
|
0 commit comments