Skip to content

Commit

Permalink
Revert "Subsample checks (#282)" (#283)
Browse files Browse the repository at this point in the history
This reverts commit 98469ed.
  • Loading branch information
Miguel Jimenez authored Dec 8, 2022
1 parent 98469ed commit 8c8350c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 109 deletions.
80 changes: 8 additions & 72 deletions oceanspy/llc_rearrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,20 @@ def arctic_crown(
DS = shift_dataset(DS, dims_c.X, dims_g.X)
DS = shift_dataset(DS, dims_c.Y, dims_g.Y)

if type(DSFacet34) == int:
DS = _reorder_ds(DS, dims_c, dims_g).persist()

if drop:
DS = _LLC_check_sizes(DS)

# if len(DS.X) == len(DS.Xp1):
# if len(DS.Y) == len(DS.Yp1):
DS = DS.isel(X=slice(0, -1), Y=slice(0, -1))
# else:
# DS = DS.isel(X=slice(0, -1))
# elif len(DS.Y) == len(DS.Yp1):
# DS = DS.isel(Y=slice(0, -1))
#
# rechunk data. In the ECCO data this is done automatically
if chunks:
DS = DS.chunk(chunks)

if XRange is not None and YRange is not None:
# drop copy var = 'nYg' (line 101)
DS = DS.drop_vars(_var_)
return DS

Expand Down Expand Up @@ -969,72 +971,6 @@ def slice_datasets(_DSfacet, dims_c, dims_g, _edges, _axis):
return _DSFacet


def _LLC_check_sizes(_DS):
"""
Checks and asserts len of center and corner points are in agreement.
"""
YG = _DS["YG"].dropna("Yp1", "all")
y0 = int(YG["Yp1"][0])
y1 = int(YG["Yp1"][-1]) + 1
_DS = _copy.deepcopy(_DS.isel(Yp1=slice(y0, y1)))

DIMS = [dim for dim in _DS["XC"].dims]
dims_c = Dims(DIMS[::-1])

DIMS = [dim for dim in _DS["XG"].dims]
dims_g = Dims(DIMS[::-1])

# total number of scalar points.
Nx_c = len(_DS[dims_c.X])
Ny_c = len(_DS[dims_c.Y])
Nx_g = len(_DS[dims_g.X])
Ny_g = len(_DS[dims_g.Y])

if Nx_c == Nx_g:
arg = {dims_c.X: slice(0, -1)}
_DS = _copy.deepcopy(_DS.isel(**arg))
else:
delta = Nx_g - Nx_c
if delta < 0:
raise ValueError(
"Inconsistent sizes at corner (_g) and center (_c) points"
"after cutout `len(_g) < len(_c)."
)
else:
if delta == 2: # len(_g) = len(_c)+2. Can but shouldn't happen.
arg = {dims_g: slice(0, -1)}
_DS = _copy.deepcopy(_DS.isel(**arg))
if Ny_c == Ny_g:
arg = {dims_c.Y: slice(0, -1)}
_DS = _copy.deepcopy(_DS.isel(**arg))

return _DS


def _reorder_ds(_ds, dims_c, dims_g):
"""Only needed when Pacific -centered data. Corrects the ordering
of y-dim and transposes the data, all lazily."""

_DS = _copy.deepcopy(_ds)
for _dim in [dims_c.Y, dims_g.Y]:
_DS["n" + _dim] = -(_DS[_dim] - (int(_DS[_dim][0].data)))
_DS = (
_DS.swap_dims({_dim: "n" + _dim})
.drop_vars([_dim])
.rename({"n" + _dim: _dim})
)

for var in _ds.data_vars:
DIMS = [dim for dim in _ds[var].dims]
dims = Dims(DIMS[::-1])
if len(dims) > 1 and "nv" not in DIMS:
dtr = list(dims)[::-1]
dtr[-1], dtr[-2] = dtr[-2], dtr[-1]
_da = _ds[var].transpose(*dtr)[::-1, :]
_DS[var] = _da
return _DS


class Dims:
axes = "XYZT" # shortcut axis names

Expand Down
16 changes: 5 additions & 11 deletions oceanspy/subsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,16 @@ def cutout(
}
dsnew = _llc_trans.arctic_crown(**arg)
dsnew = dsnew.set_coords(co_list)
# grid_coords = od.grid_coords
grid_coords = {
"Y": {"Y": None, "Yp1": 0.5},
"X": {"X": None, "Xp1": 0.5},
"Z": {"Z": None, "Zp1": 0.5, "Zu": 0.5, "Zl": -0.5},
"time": {"time": -0.5},
}
# if len(grid_coords["time"]) > 1:
# grid_coords["time"].pop("time_midp", None)
grid_coords = {"add_midp": True, "overwrite": True, "grid_coords": grid_coords}
grid_coords = od.grid_coords
od._ds = dsnew
manipulate_coords = {"coordsUVfromG": True}
new_face_connections = {"face_connections": {None: {None, None}}}
od = od.set_face_connections(**new_face_connections)
od = od.manipulate_coords(**manipulate_coords)
od = od.set_grid_coords(**grid_coords)
if len(grid_coords["time"]) > 1:
grid_coords["time"].pop("time_midp", None)
grid_coords = {"add_midp": True, "grid_coords": grid_coords}
od = od.set_grid_coords(**grid_coords, overwrite=True)
od._ds.attrs["OceanSpy_description"] = "Cutout of"
"simulation, with simple topology (face not a dimension)"
# Unpack the new dataset without face as dimension
Expand Down
42 changes: 21 additions & 21 deletions oceanspy/tests/test_llc_rearrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2267,13 +2267,13 @@ def test_arc_connect(
(od, [2, 5, 7, 10], varList, None, None, 0, 359, 0, 89),
(od, [1, 4, 8, 11], varList, None, None, 0, 359, 0, 89),
(od, [0, 3, 9, 12], varList, None, None, 0, 359, 0, 89),
(od, [6, 7, 8, 9], varList, None, None, 0, 89, 0, 314),
(od, [6, 10, 11, 12], varList, None, None, 0, 89, 0, 314),
(od, [6, 7, 8, 9], varList, None, None, 0, 89, 314, 0),
(od, [6, 10, 11, 12], varList, None, None, 0, 89, 314, 0),
(od, [0, 1, 2, 6], varList, None, None, 0, 89, 0, 314),
(od, [3, 4, 5, 6], varList, None, None, 0, 89, 0, 314),
(od, [2, 6, 10], varList, None, None, 0, 179, 0, 134),
(od, [6, 7, 10], varList, None, None, 0, 179, 0, 134),
(od, [6, 7, 8, 10, 11], varList, None, None, 0, 179, 0, 224),
(od, [6, 7, 10], varList, None, None, 0, 179, 134, 0),
(od, [6, 7, 8, 10, 11], varList, None, None, 0, 179, 224, 0),
(
od,
[4, 5, 6, 7, 8, 10, 11],
Expand All @@ -2285,12 +2285,12 @@ def test_arc_connect(
0,
224,
),
(od, [7, 8, 10, 11], varList, None, None, 0, 179, 0, 179),
(od, [7, 8, 10, 11], varList, None, None, 0, 179, 179, 0),
(od, [1, 2, 10, 11], varList, None, None, 0, 179, 0, 179),
(od, [8, 9, 11, 12], varList, None, None, 0, 179, 0, 179),
(od, [8, 9, 11, 12], varList, None, None, 0, 179, 179, 0),
(od, [0, 1, 11, 12], varList, None, None, 0, 179, 0, 179),
(od, [0, 1, 3, 4], varList, None, None, 0, 179, 0, 179),
(od, [9, 12], varList, None, None, 0, 179, 0, 89),
(od, [9, 12], varList, None, None, 0, 179, 89, 0),
(od, [0, 12], varList, None, None, 0, 179, 0, 89),
(od, [0, 3], varList, None, None, 0, 179, 0, 89),
(od, [0, 9, 12], varList, None, None, 0, 269, 0, 89),
Expand All @@ -2301,39 +2301,39 @@ def test_arc_connect(
(od, [3], varList[0], None, None, 0, 89, 0, 89),
(od, [4], varList[0], None, None, 0, 89, 0, 89),
(od, [5], varList[0], None, None, 0, 89, 0, 89),
(od, [7], varList[0], None, None, 0, 89, 0, 89),
(od, [8], varList[0], None, None, 0, 89, 0, 89),
(od, [9], varList[0], None, None, 0, 89, 0, 89),
(od, [10], varList[0], None, None, 0, 89, 0, 89),
(od, [11], varList[0], None, None, 0, 89, 0, 89),
(od, [12], varList[0], None, None, 0, 89, 0, 89),
(od, [7], varList[0], None, None, 0, 89, 89, 0),
(od, [8], varList[0], None, None, 0, 89, 89, 0),
(od, [9], varList[0], None, None, 0, 89, 89, 0),
(od, [10], varList[0], None, None, 0, 89, 89, 0),
(od, [11], varList[0], None, None, 0, 89, 89, 0),
(od, [12], varList[0], None, None, 0, 89, 89, 0),
(od, None, varList, _XRanges_[0], _YRanges_[0], 0, 44, 0, 24),
(od, None, varList, _XRanges_[1], _YRanges_[1], 0, 68, 0, 8),
(od, None, varList, _XRanges_[2], _YRanges_[2], 0, 67, 0, 6),
(od, None, varList, _XRanges_[3], _YRanges_[3], 0, 66, 0, 7),
(od, None, varList, _XRanges_[4], _YRanges_[4], 0, 71, 0, 72),
(od, None, varList, _XRanges_[5], _YRanges_[5], 0, 21, 0, 179),
(od, None, varList, _XRanges_[6], _YRanges_[6], 0, 39, 0, 55),
(od, None, varList, _XRanges_[7], _YRanges_[7], 0, 72, 0, 6),
(od, None, varList, _XRanges_[7], _YRanges_[7], 0, 72, 6, 0),
(od, None, varList, _XRanges_[8], _YRanges_[8], 0, 112, 0, 8),
(od, None, varList, _XRanges_[9], _YRanges_[9], 0, 151, 0, 6),
(od, None, varList, _XRanges_[10], _YRanges_[10], 0, 109, 0, 5),
(od, None, varList, _XRanges_[10], _YRanges_[10], 0, 109, 5, 0),
(od, None, varList, _XRanges_[11], _YRanges_[11], 0, 12, 0, 56),
(od, None, varList, _XRanges_[12], _YRanges_[12], 0, 16, 0, 84),
(od, None, varList, _XRanges_[13], _YRanges_[13], 0, 13, 0, 127),
(od, None, varList, _XRanges_[14], _YRanges_[14], 0, 7, 0, 191),
(od, None, varList, _XRanges_[12], _YRanges_[12], 0, 16, 84, 0),
(od, None, varList, _XRanges_[13], _YRanges_[13], 0, 13, 127, 0),
(od, None, varList, _XRanges_[14], _YRanges_[14], 0, 7, 191, 0),
(od, None, varList, _XRanges_[15], _YRanges_[15], 0, 87, 0, 9),
(od, None, varList, _XRanges_[16], _YRanges_[16], 0, 6, 0, 68),
(od, None, varList, _XRanges_[17], _YRanges_[17], 0, 20, 0, 118),
(od, None, varList, [-31, 25], [58, 85.2], 0, 60, 0, 71),
(od, None, varList, [-120, -60], [58, 85.2], 0, 64, 0, 70),
(od, None, varList, [160, -150], [58, 85.2], 0, 54, 0, 70),
(od, None, varList, [-120, -60], [58, 85.2], 0, 64, 70, 0),
(od, None, varList, [160, -150], [58, 85.2], 0, 54, 70, 0),
(od, None, varList, [60, 130], [58, 85.2], 0, 74, 0, 70),
],
)
def test_transformation(od, faces, varList, XRange, YRange, X0, X1, Y0, Y1):
"""Test the transformation fn by checking the final dimensions."""
ds = od._ds.reset_coords().drop_vars({"hFacC", "hFacS", "hFacW"})
ds = od._ds.reset_coords()
args = {
"ds": ds,
"varList": varList,
Expand Down
10 changes: 7 additions & 3 deletions oceanspy/tests/test_subsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,14 @@ def test_reduce_variables(od, varList):

@pytest.mark.parametrize("od", [ECCOod])
@pytest.mark.parametrize(
"XRange, YRange, ZRange, varList, NZ, NY, NX",
"XRange, YRange, ZRange, varList, centered, NZ, NY, NX",
[
(
[-90, 20],
[20, 60],
None,
["T"],
"Atlantic",
50,
56,
110,
Expand All @@ -277,13 +278,14 @@ def test_reduce_variables(od, varList):
[20, 60],
None,
["U", "V"],
"Atlantic",
50,
56,
359,
),
# ([-31, -2], [58, 68.2], None, ["T"], "Atlantic", 50), # has ARCTIC DATA
(None, None, None, ["T"], 50, 314, 359),
(None, None, None, None, 50, 314, 359),
(None, None, None, ["T"], "Atlantic", 50, 314, 359),
(None, None, None, None, None, 50, 314, 359),
],
)
def test_cutout_faces(
Expand All @@ -292,6 +294,7 @@ def test_cutout_faces(
YRange,
ZRange,
varList,
centered,
NZ,
NY,
NX,
Expand All @@ -301,6 +304,7 @@ def test_cutout_faces(
"XRange": XRange,
"YRange": YRange,
"ZRange": ZRange,
"centered": centered,
}
new_od = od.subsample.cutout(**args)
old_dims = od.dataset.dims
Expand Down
5 changes: 3 additions & 2 deletions sciserver_catalogs/catalog_xarray.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ sources:
k_u: Zu
k_l: Zl
rename:
Theta: THETA
Salt: SALT
Theta: Temp
Salt: S
i: X
i_g: Xp1
j: Y
Expand Down Expand Up @@ -726,6 +726,7 @@ sources:
args:
urlpath: '/home/idies/workspace/poseidon/data01_01/llc4320_grid/'
engine: zarr
# drop_variables: ['CS', 'SN', 'PHrefC', 'PHrefF']
metadata:
swap_dims:
k: Z
Expand Down

0 comments on commit 8c8350c

Please sign in to comment.