Skip to content

Commit

Permalink
fixed introduced bug in oceanography and improved RTOFS test
Browse files Browse the repository at this point in the history
  • Loading branch information
giumas committed Oct 27, 2024
1 parent b9070f2 commit 7db85d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
8 changes: 7 additions & 1 deletion examples/soundspeed/ex_read_and_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@

# 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:
data_outputs[format] = data_output
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)

Expand Down
22 changes: 12 additions & 10 deletions hyo2/ssm2/lib/profile/oceanography.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,25 +40,25 @@ 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:
if np.isnan(val):
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.
Expand All @@ -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)
Expand All @@ -95,25 +97,25 @@ 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:
if np.isnan(val):
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)
Expand Down
2 changes: 2 additions & 0 deletions hyo2/ssm2/lib/profile/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 2 additions & 15 deletions tests/soundspeed/atlas/test_rtofs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import unittest
import os
import shutil
import logging

from hyo2.ssm2.lib.atlas.rtofs import Rtofs
Expand All @@ -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)

Expand Down

0 comments on commit 7db85d4

Please sign in to comment.