|
23 | 23 | ConventionViolationError, ConventionViolationWarning |
24 | 24 | ) |
25 | 25 | from emsarray.operations import geometry |
26 | | -from tests.utils import assert_property_not_cached |
| 26 | +from tests.utils import assert_property_not_cached, filter_warning |
27 | 27 |
|
28 | 28 |
|
29 | 29 | def make_faces(width: int, height, fill_value: int) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: |
@@ -239,10 +239,28 @@ def make_dataset( |
239 | 239 | }, |
240 | 240 | ) |
241 | 241 |
|
| 242 | + one_day = np.timedelta64(1, 'D').astype('timedelta64[ns]') |
| 243 | + period = xr.DataArray( |
| 244 | + data=np.concatenate([ |
| 245 | + np.arange(cell_size - 2, dtype=int) * one_day, |
| 246 | + [np.timedelta64('nat', 'ns')] * 2, |
| 247 | + ]), |
| 248 | + dims=[face_dimension], |
| 249 | + name="period", |
| 250 | + attrs={ |
| 251 | + "long_name": "Some variable counting days", |
| 252 | + }, |
| 253 | + ) |
| 254 | + period.encoding.update({ |
| 255 | + "units": "days", |
| 256 | + "_FillValue": np.int16(-1), |
| 257 | + "dtype": np.dtype('int16'), |
| 258 | + }) |
| 259 | + |
242 | 260 | dataset = xr.Dataset( |
243 | 261 | data_vars={var.name: var for var in [ |
244 | 262 | mesh, face_node_connectivity, node_x, node_y, |
245 | | - t, z, botz, eta, temp |
| 263 | + t, z, botz, eta, temp, period, |
246 | 264 | ]}, |
247 | 265 | attrs={ |
248 | 266 | 'title': "COMPAS defalt version", |
@@ -713,7 +731,22 @@ def test_apply_clip_mask(tmp_path): |
713 | 731 |
|
714 | 732 | def test_make_and_apply_clip_mask(tmp_path): |
715 | 733 | dataset = make_dataset(width=5) |
716 | | - dataset.ems.to_netcdf(tmp_path / "original.nc") |
| 734 | + |
| 735 | + # When saving a dataset to disk, xarray.coding.times.cast_to_int_if_safe |
| 736 | + # will check if it is possible to encode a timedelta64 using integer values |
| 737 | + # by casting the values and checking for equality. |
| 738 | + # Recent versions of numpy will emit warnings |
| 739 | + # when casting a data array with dtype timedelta64 to int |
| 740 | + # if it contains NaT (not a time) values. |
| 741 | + # xarray will fix this eventually, but for now... |
| 742 | + # See https://github.com/pydata/xarray/issues/7942 |
| 743 | + with filter_warning( |
| 744 | + 'ignore', category=RuntimeWarning, |
| 745 | + message='invalid value encountered in cast', |
| 746 | + module=r'xarray\.coding\.times', |
| 747 | + ): |
| 748 | + dataset.ems.to_netcdf(tmp_path / "original.nc") |
| 749 | + |
717 | 750 | geometry.write_geojson(dataset, tmp_path / 'original.geojson') |
718 | 751 |
|
719 | 752 | polygon = Polygon([[3.4, 1], [3.4, -1], [6, -1], [6, 1], [3.4, 1]]) |
|
0 commit comments