Skip to content

Commit

Permalink
updated roms tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kthyng committed Mar 28, 2024
1 parent 8f4b10a commit d0a1245
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
4 changes: 2 additions & 2 deletions tests/models/test_stranding.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def test_stranding_3d(self):

# Check calculated trajectory lengths and speeds
total_length, distances, speeds = o.get_trajectory_lengths()
self.assertAlmostEqual(total_length.max(), 15719.5, 1)
self.assertAlmostEqual(total_length.max(), 15722.7, 1)
self.assertAlmostEqual(total_length.min(), 1225.2, 1)
self.assertAlmostEqual(speeds.max(), 0.132, 1)
self.assertAlmostEqual(distances.max(), 2859.5, 1)
self.assertAlmostEqual(distances.max(), 2859.0, 1)

def test_stranding_roms(self):
o = PelagicEggDrift(loglevel=20)
Expand Down
16 changes: 8 additions & 8 deletions tests/readers/test_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def test_sdepth_zeta_zero_Vtransform1(self):

expected_output = np.array([-0.98333333, -0.5, -0.01666667])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)

# zeta 0s array
zeta = np.zeros((self.NY, self.NX))
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)

expected_output = np.array([-0.98333333, -0.5, -0.01666667])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)


def test_sdepth_zeta_zero_Vtransform2(self):
Expand All @@ -46,14 +46,14 @@ def test_sdepth_zeta_zero_Vtransform2(self):
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)
expected_output = np.array([-0.98484848, -0.5, -0.01515152])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)

# zeta 0s array
zeta = np.zeros((self.NY, self.NX))
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)
expected_output = np.array([-0.98484848, -0.5, -0.01515152])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)

def test_sdepth_zeta_nonzero_Vtransform1(self):
"""test nonzero zeta in sdepth, Vtransform 1."""
Expand All @@ -65,14 +65,14 @@ def test_sdepth_zeta_nonzero_Vtransform1(self):
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)
expected_output = np.array([-0.96666667, 0., 0.96666667])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)

# zeta 1s array
zeta = np.ones((self.NY, self.NX))
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)
expected_output = np.array([-0.96666667, 0., 0.96666667])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)


def test_sdepth_zeta_nonzero_Vtransform2(self):
Expand All @@ -85,14 +85,14 @@ def test_sdepth_zeta_nonzero_Vtransform2(self):
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)
expected_output = np.array([-0.96969697, 0., 0.96969697])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)

# zeta 1s array
zeta = np.ones((self.NY, self.NX))
z_rho = depth.sdepth(self.Htot, zeta, self.hc, self.Cs_r, Vtransform=Vtransform)
expected_output = np.array([-0.96969697, 0., 0.96969697])
assert z_rho.shape == (self.NZ, self.NY, self.NX)
np.allclose(z_rho[:,0,0], expected_output)
assert np.allclose(z_rho[:,0,0], expected_output)



Expand Down
4 changes: 4 additions & 0 deletions tests/readers/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def test_MFDataset(self):
'2Feb2016_Nordic_sigma_3d/Nordic_subset.nc')
lon = 14.0
lat = 67.3

# nordicMF is missing angle
nordicMF.Dataset["angle"] = nordicMF_all.Dataset.angle

#nordic3d = reader_ROMS_native.Reader(o.test_data_folder() +
# '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc')
#o.add_reader(nordic3d) # Slightly different results
Expand Down
42 changes: 39 additions & 3 deletions tests/readers/test_roms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
class TestROMSReader(unittest.TestCase):
def setUp(self):

zeta = np.array([[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], [[0.7, 0.8, 0.9], [1.0, 1.1, 1.2]]])
# zeta = np.zeros((2,2,3))
h = np.ones((2, 3))*10

u_eastward = np.array([[[[1.0, 1.1, 1.2], [1.3, 1.4, 1.5]], [[1.6, 1.7, 1.8], [1.9, 2.0, 2.1]], [[2.2, 2.3, 2.4], [2.5, 2.6, 2.7]]], [[[2.8, 2.9, 3.0], [3.1, 3.2, 3.3]], [[3.4, 3.5, 3.6], [3.7, 3.8, 3.9]], [[4.0, 4.1, 4.2], [4.3, 4.4, 4.5]]]])

Expand All @@ -33,12 +32,13 @@ def setUp(self):
"lon_v": (["eta_v", "xi_v"], [[-152.0, -151.9, -151.8]]),
"lat_v": (["eta_v", "xi_v"], [[60.45, 60.45, 60.45]]),
"mask_rho": (["eta_rho", "xi_rho"], mask_rho),
"h": (["eta_rho", "xi_rho"], h),
"wetdry_mask_rho": (["ocean_time", "eta_rho", "xi_rho"], wetdry_mask_rho),
"angle": (["eta_rho", "xi_rho"], angle),
"mask_u": (["eta_u", "xi_u"], mask_u),
"mask_v": (["eta_v", "xi_v"], mask_v),
"ocean_time": ("ocean_time", [0, 1], {"units": "seconds since 1970-01-01"}),
"zeta": (["ocean_time", "eta_rho", "xi_rho"], zeta),
# "zeta": (["ocean_time", "eta_rho", "xi_rho"], zeta),
"s_rho": (["s_rho"], Cs_r),
"Cs_r": (["s_rho"], Cs_r),
"hc": 16,
Expand Down Expand Up @@ -123,6 +123,42 @@ def test_get_variables_u_mask_u(self):
assert "mask_rho" not in masks


def test_get_variables_u_depth_zeta_zero(self):
"""get a variable from the dataset and verify in depth."""
standard_mapping = {'u': 'x_sea_water_velocity'}
reader = reader_ROMS_native.Reader(self.ds, standard_name_mapping=standard_mapping)
# drop wetdry_mask_rho to test that mask_rho is used
reader.Dataset = reader.Dataset.drop_vars("wetdry_mask_rho")
var, key = "u", "x_sea_water_velocity"
zeta = np.zeros((2,2,3))
reader.Dataset["zeta"] = (["ocean_time", "eta_rho", "xi_rho"], zeta)

output = reader.get_variables(key, datetime(1970, 1, 1), 0, 0, [-5], testing=False)

assert key in output
assert reader.do_not_rotate == []
# those expected values at 0.5, 0.6 from the input array
assert np.allclose(output[key].data[1,0,:], self.ds[var][0,1,0,:].values)


def test_get_variables_u_depth_zeta_nonzero(self):
"""get a variable from the dataset and verify in depth."""
standard_mapping = {'u': 'x_sea_water_velocity'}
reader = reader_ROMS_native.Reader(self.ds, standard_name_mapping=standard_mapping)
# drop wetdry_mask_rho to test that mask_rho is used
reader.Dataset = reader.Dataset.drop_vars("wetdry_mask_rho")
var, key = "u", "x_sea_water_velocity"
zeta = np.ones((2,2,3))*2
reader.Dataset["zeta"] = (["ocean_time", "eta_rho", "xi_rho"], zeta)

output = reader.get_variables(key, datetime(1970, 1, 1), 0, 0, [-5], testing=False)

u_expected = [0.64285714, 0.74285714]
assert key in output
assert reader.do_not_rotate == []
assert np.allclose(output[key].data[1,0,:], u_expected)


class TestROMSReaderRotation(unittest.TestCase):
def setUp(self):

Expand Down

0 comments on commit d0a1245

Please sign in to comment.