Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/ssolson/MHKiT-Python int…
Browse files Browse the repository at this point in the history
…o develop
  • Loading branch information
ssolson committed Nov 29, 2023
2 parents d09e606 + 7614867 commit 35a917c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 67 deletions.
1 change: 0 additions & 1 deletion mhkit/river/io/d3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def get_layer_data(data, variable, layer_index=-1, time_index=-1):
else:
if type(var[0][0]) != np.float64:
raise TypeError('data not recognized')

dimensions = 2
v = np.ma.getdata(var[time_index, :], False)

Expand Down
50 changes: 17 additions & 33 deletions mhkit/tests/wave/test_contours.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
from os.path import abspath, dirname, join, isfile, normpath, relpath
import unittest
import pickle
import json
import os

from pandas.testing import assert_frame_equal
from numpy.testing import assert_allclose
from scipy.interpolate import interp1d
from random import seed, randint
import matplotlib.pylab as plt
from datetime import datetime
import xarray.testing as xrt
import mhkit.wave as wave
from io import StringIO
import pandas as pd
import numpy as np

import mhkit.wave as wave
import contextlib
import unittest
import netCDF4
import inspect
import pickle
import time
import json
import sys
import os


testdir = dirname(abspath(__file__))
Expand Down Expand Up @@ -45,10 +54,6 @@ def setUpClass(self):
self.wdrt_dt = 3600
self.wdrt_period = 50

# `samples_contour`Example data
self.hs_contour = np.array([8.56637939, 9.27612515, 8.70427774])
self.te_contour = np.array([10, 15, 20])

@classmethod
def tearDownClass(self):
pass
Expand Down Expand Up @@ -235,28 +240,7 @@ def test_kde_copulas(self):
self.wdrt_copulas['bivariate_KDE_log_x2'])]
self.assertTrue(all(close))

def test_samples_contours_type_validation(self):
with self.assertRaises(TypeError):
wave.contours.samples_contour(
'not an array', self.te_contour, self.hs_contour)
with self.assertRaises(TypeError):
wave.contours.samples_contour(
self.te_contour, 'not an array', self.hs_contour)
with self.assertRaises(TypeError):
wave.contours.samples_contour(
self.te_contour, self.hs_contour, 'not an array')

def test_samples_contours_length_mismatch(self):
with self.assertRaises(ValueError):
wave.contours.samples_contour(
self.te_contour, self.hs_contour, np.array([1, 2]))

def test_samples_contours_range_validation(self):
with self.assertRaises(ValueError):
wave.contours.samples_contour(
np.array([5, 25]), self.te_contour, self.hs_contour)

def test_samples_contours_correct_interpolation(self):
def test_samples_contours(self):
te_samples = np.array([10, 15, 20])
hs_samples_0 = np.array([8.56637939, 9.27612515, 8.70427774])
hs_contour = np.array(self.wdrt_copulas["gaussian_x1"])
Expand Down
43 changes: 10 additions & 33 deletions mhkit/wave/contours.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,51 +1792,28 @@ def samples_contour(t_samples, t_contour, hs_contour):
raise TypeError(f't_contour must be of type np.ndarray. Got: {type(t_contour)}')
if not isinstance(hs_contour, np.ndarray):
raise TypeError(f'hs_contour must be of type np.ndarray. Got: {type(hs_contour)}')
if len(t_contour) != len(hs_contour):
raise ValueError(
"t_contour and hs_contour must be of the same length.")
if np.any(t_samples < np.min(t_contour)) or np.any(t_samples > np.max(t_contour)):
raise ValueError(
"All t_samples must be within the range of t_contour.")



# Find minimum and maximum energy period values
# finds minimum and maximum energy period values
amin = np.argmin(t_contour)
amax = np.argmax(t_contour)
aamin = np.min([amin, amax])
aamax = np.max([amin, amax])

# Separate points along the contour into upper & lower half
# finds points along the contour
w1 = hs_contour[aamin:aamax]
w2 = np.concatenate((hs_contour[aamax:], hs_contour[:aamin]))

# Get samples min and max
t_min, t_max = np.min(t_samples), np.max(t_samples)

# Choose the half of the contour with the largest wave height
if np.max(w1) > np.max(w2):
# Check if the max or min Tp values are within the contour half
include_aamax = t_max >= t_contour[aamax] or t_min <= t_contour[aamin]
# Set the x and y values for interpolation
x1 = t_contour[aamin:aamax + int(include_aamax)]
y1 = hs_contour[aamin:aamax + int(include_aamax)]
if (np.max(w1) > np.max(w2)):
x1 = t_contour[aamin:aamax]
y1 = hs_contour[aamin:aamax]
else:
# Check if the max or min Tp values are within the contour half
include_aamin = t_max >= t_contour[aamin] or t_min <= t_contour[aamax]
# Set the x and y values for interpolation
x1 = np.concatenate(
(t_contour[aamax:], t_contour[:aamin + int(include_aamin)]))
y1 = np.concatenate(
(hs_contour[aamax:], hs_contour[:aamin + int(include_aamin)]))

# Sort data based on the max and min Tp values
x1 = np.concatenate((t_contour[aamax:], t_contour[:aamin]))
y1 = np.concatenate((hs_contour[aamax:], hs_contour[:aamin]))
# sorts data based on the max and min energy period values
ms = np.argsort(x1)
x = x1[ms]
y = y1[ms]
# Interpolation function
# interpolates the sorted data
si = interp.interp1d(x, y)
# Interpolate Tp samples values to get Hs values
# finds the wave height based on the user specified energy period values
hs_samples = si(t_samples)

return hs_samples
Expand Down

0 comments on commit 35a917c

Please sign in to comment.