From 7db85d4f0bf742d1b1962103e1f2a6702c04e118 Mon Sep 17 00:00:00 2001 From: giumas Date: Sun, 27 Oct 2024 09:54:28 +0100 Subject: [PATCH] fixed introduced bug in oceanography and improved RTOFS test --- examples/soundspeed/ex_read_and_write.py | 8 +++++++- hyo2/ssm2/lib/profile/oceanography.py | 22 ++++++++++++---------- hyo2/ssm2/lib/profile/profile.py | 2 ++ tests/soundspeed/atlas/test_rtofs.py | 17 ++--------------- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/examples/soundspeed/ex_read_and_write.py b/examples/soundspeed/ex_read_and_write.py index 0ef8cd6..bcbab82 100644 --- a/examples/soundspeed/ex_read_and_write.py +++ b/examples/soundspeed/ex_read_and_write.py @@ -27,7 +27,7 @@ # test readers/writers logger.info('test: *** START ***') -filters = ["valeport", ] +filters = ["aml", ] formats = ["caris", "csv", "elac", "hypack", "ixblue", "asvp/ssp", "qps", "sonardyne", "unb", ] data_outputs = dict() for format in formats: @@ -35,10 +35,16 @@ tests = testing.input_dict_test_files(inclusive_filters=filters) # print(tests) +reduced_testing = True + # import each identified file for idx, testfile in enumerate(tests.keys()): logger.info("test: * New profile: #%03d *" % idx) + if reduced_testing and (os.path.basename(testfile)[0] != "_"): + logger.debug("Skip!") + continue + # import lib.import_data(data_path=testfile, data_format=tests[testfile].name) diff --git a/hyo2/ssm2/lib/profile/oceanography.py b/hyo2/ssm2/lib/profile/oceanography.py index eb302f0..12f9c63 100644 --- a/hyo2/ssm2/lib/profile/oceanography.py +++ b/hyo2/ssm2/lib/profile/oceanography.py @@ -28,7 +28,8 @@ class Oceanography: # ### PRESSURE/DEPTH METHODS ### @classmethod - def p2d(cls, p, lat: float = 30.0, dyn_height: int | None = None, debug: bool = False) -> float: + def p2d(cls, p: float | np.ndarray, lat: float = 30.0, dyn_height: int | None = None, debug: bool = False) \ + -> float | np.ndarray: """Convert pressure to depth""" try: return cls.p2d_gsw(p=p, lat=lat, dyn_height=dyn_height) @@ -39,14 +40,14 @@ def p2d(cls, p, lat: float = 30.0, dyn_height: int | None = None, debug: bool = return cls.p2d_backup(p=p, lat=lat) @classmethod - def p2d_gsw(cls, p, lat: float, dyn_height: int | None) -> float: + def p2d_gsw(cls, p: float | np.ndarray, lat: float, dyn_height: int | None) -> float | np.ndarray: if not isinstance(p, np.ndarray): p = np.array(p, ndmin=1, copy=False) if dyn_height is None: depth = -gsw.conversions.z_from_p(p=p, lat=lat) - return depth[0] + return depth depth = -gsw.conversions.z_from_p(p=p, lat=lat, geo_strf_dyn_height=dyn_height) for val in depth: @@ -54,10 +55,10 @@ def p2d_gsw(cls, p, lat: float, dyn_height: int | None) -> float: logger.info("nan in gsw.conversions.z_from_p with dyn_height") return -gsw.conversions.z_from_p(p=p, lat=lat) - return depth[0] + return depth @classmethod - def p2d_backup(cls, p, lat: float) -> float: + def p2d_backup(cls, p: float | np.ndarray, lat: float) -> float | np.ndarray: """Convert pressure to depth If the latitude is not passed, a default value of 30.0 is used. @@ -84,7 +85,8 @@ def p2d_backup(cls, p, lat: float) -> float: return d / g @classmethod - def d2p(cls, d, lat: float = 30.0, dyn_height: int | None = None, debug: bool = False) -> float: + def d2p(cls, d: float | np.ndarray, lat: float = 30.0, dyn_height: int | None = None, debug: bool = False) \ + -> float | np.ndarray: """Convert pressure to depth""" try: return cls.d2p_gsw(d=d, lat=lat, dyn_height=dyn_height) @@ -95,14 +97,14 @@ def d2p(cls, d, lat: float = 30.0, dyn_height: int | None = None, debug: bool = return cls.d2p_backup(d=d, lat=lat) @classmethod - def d2p_gsw(cls, d, lat: float, dyn_height: int | None) -> float: + def d2p_gsw(cls, d: float | np.ndarray, lat: float, dyn_height: int | None) -> float | np.ndarray: if not isinstance(d, np.ndarray): d = np.array(d, ndmin=1, copy=False) if dyn_height is None: pressure = gsw.conversions.p_from_z(z=-d, lat=lat) - return pressure[0] + return pressure pressure = gsw.conversions.p_from_z(z=-d, lat=lat, geo_strf_dyn_height=dyn_height) for val in pressure: @@ -110,10 +112,10 @@ def d2p_gsw(cls, d, lat: float, dyn_height: int | None) -> float: logger.info("nan in gsw.conversions.p_from_z with dyn_height") return gsw.conversions.p_from_z(z=-d, lat=lat) - return pressure[0] + return pressure @classmethod - def d2p_backup(cls, d, lat: float) -> float: + def d2p_backup(cls, d: float | np.ndarray, lat: float) -> float | np.ndarray: """Convert depth to pressure ref: Leroy and Parthiot(1998) diff --git a/hyo2/ssm2/lib/profile/profile.py b/hyo2/ssm2/lib/profile/profile.py index f3de813..b57e29f 100644 --- a/hyo2/ssm2/lib/profile/profile.py +++ b/hyo2/ssm2/lib/profile/profile.py @@ -840,6 +840,8 @@ def insert_sis_speed(self, depth, speed, src=Dicts.sources['user'], temp=None, c user_invalid = self.sis.flag == Dicts.flags['user'] # user-invalidate samples possible = np.logical_or(valid, user_invalid) # possible samples ip = np.indices(self.sis.flag.shape)[0][possible] # indices of possible samples + if len(ip) < 2: + raise RuntimeError("Too few valid samples: %d! Check cast direction" % len(ip)) # find depth index both in the valid and in the possible samples try: diff --git a/tests/soundspeed/atlas/test_rtofs.py b/tests/soundspeed/atlas/test_rtofs.py index 7516a24..aaf6b7d 100644 --- a/tests/soundspeed/atlas/test_rtofs.py +++ b/tests/soundspeed/atlas/test_rtofs.py @@ -1,6 +1,4 @@ import unittest -import os -import shutil import logging from hyo2.ssm2.lib.atlas.rtofs import Rtofs @@ -11,26 +9,15 @@ class TestSoundSpeedAtlasRtofs(unittest.TestCase): - def setUp(self): - self.cur_dir = os.path.abspath(os.path.dirname(__file__)) - - def tearDown(self): - dir_items = os.listdir(self.cur_dir) - for item in dir_items: - if item.split('.')[-1] == 'db': - os.remove(os.path.join(self.cur_dir, item)) - if item == 'atlases': - shutil.rmtree(os.path.join(self.cur_dir, item)) - def test_creation_of_Rtofs(self): - prj = SoundSpeedLibrary(data_folder=self.cur_dir) + prj = SoundSpeedLibrary() rtofs = Rtofs(data_folder=prj.rtofs_folder, prj=prj) self.assertTrue('rtofs' in rtofs.data_folder) self.assertFalse(rtofs.is_present()) prj.close() def test_download_db_from_Rtofs(self): - prj = SoundSpeedLibrary(data_folder=self.cur_dir) + prj = SoundSpeedLibrary() rtofs = Rtofs(data_folder=prj.data_folder, prj=prj) rtofs.download_db(server_mode=True)