def test_maybe_convert_vertex_to_bounds(): # create a ds with verticies lon = np.arange(0, 10) lat = np.arange(20, 30) data = np.random.rand(len(lon), len(lat)) ds = xr.DataArray(data, dims=["x", "y"], coords={ "x": lon, "y": lat }).to_dataset(name="test") ds.coords["lon"] = ds.x * xr.ones_like(ds.y) ds.coords["lat"] = xr.ones_like(ds.x) * ds.y ds.coords["lon_verticies"] = ( xr.DataArray([-0.1, -0.1, 0.1, 0.1], dims=["vertex"]) + ds["lon"]) ds.coords["lat_verticies"] = ( xr.DataArray([-0.1, 0.1, 0.1, -0.1], dims=["vertex"]) + ds["lat"]) ds = promote_empty_dims(ds) # create expected dataset ds_expected = ds.copy() for va in ["lon", "lat"]: ds_expected.coords[va + "_bounds"] = ( xr.DataArray([-0.1, 0.1], dims=["bnds"]) + ds_expected[va]) ds_expected = promote_empty_dims(ds_expected) ds_test = maybe_convert_vertex_to_bounds(ds) xr.testing.assert_identical(ds_expected, ds_test) # check that datasets that already conform to this are not changed xr.testing.assert_identical(ds_expected, maybe_convert_vertex_to_bounds(ds_expected)) assert np.all(ds_test.lon_bounds.diff("bnds") > 0) assert np.all(ds_test.lat_bounds.diff("bnds") > 0)
def test_promote_empty_dims(): xlen, ylen, zlen = (10, 5, 6) ds = create_test_ds("x", "y", "z", xlen, ylen, zlen) ds = ds.drop(["x", "y", "z"]) ds_promoted = promote_empty_dims(ds) assert set(["x", "y", "z"]).issubset(set(ds_promoted.coords))