@@ -5089,6 +5089,135 @@ def test_extreme_absmax(self):
50895089 assert extreme_out == pytest .approx (extreme_expect )
50905090
50915091
5092+ def test_reshape (self ):
5093+ a = 7
5094+ b = 6
5095+
5096+ yp = np .linspace (0.0 , 2.0 , 20 )
5097+ xp = np .linspace (0.0 , 359.0 , 10 )
5098+ vp = np .array ([[a * x_i + b * y_i for x_i in xp ] for y_i in yp ])
5099+ spectrum = DirectionalBinSpectrum (yp , xp , vp , freq_hz = True , degrees = True )
5100+
5101+ y = np .linspace (0.5 , 1.0 , 20 )
5102+ grid_reshaped = spectrum .reshape (y , freq_hz = True )
5103+
5104+ freq_expect = (2.0 * np .pi ) * y
5105+ dirs_expect = (np .pi / 180.0 ) * xp
5106+ vals_expect = np .array ([[a * x_i + b * y_i for x_i in xp ] for y_i in y ]) / (2.0 * np .pi )
5107+
5108+ freq_out = grid_reshaped ._freq
5109+ dirs_out = grid_reshaped ._dirs
5110+ vals_out = grid_reshaped ._vals
5111+
5112+ np .testing .assert_array_almost_equal (freq_out , freq_expect )
5113+ np .testing .assert_array_almost_equal (dirs_out , dirs_expect )
5114+ np .testing .assert_array_almost_equal (vals_out , vals_expect )
5115+
5116+ def test_reshape2 (self ):
5117+ a = 7
5118+ b = 6
5119+
5120+ yp = np .linspace (0.0 , 2.0 , 20 )
5121+ xp = np .linspace (0.0 , 359.0 , 10 )
5122+ vp = np .array ([[a * x_i + b * y_i for x_i in xp ] for y_i in yp ])
5123+ spectrum = DirectionalBinSpectrum (yp , xp , vp , freq_hz = True , degrees = True )
5124+
5125+ y = np .linspace (0.5 , 1.0 , 20 )
5126+ y_ = (2.0 * np .pi ) * y
5127+ grid_reshaped = spectrum .reshape (y_ , freq_hz = False )
5128+
5129+ freq_expect = (2.0 * np .pi ) * y
5130+ dirs_expect = (np .pi / 180.0 ) * xp
5131+ vals_expect = np .array ([[a * x_i + b * y_i for x_i in xp ] for y_i in y ]) / (2.0 * np .pi )
5132+
5133+ freq_out = grid_reshaped ._freq
5134+ dirs_out = grid_reshaped ._dirs
5135+ vals_out = grid_reshaped ._vals
5136+
5137+ np .testing .assert_array_almost_equal (freq_out , freq_expect )
5138+ np .testing .assert_array_almost_equal (dirs_out , dirs_expect )
5139+ np .testing .assert_array_almost_equal (vals_out , vals_expect )
5140+
5141+ def test_reshape_complex_rectangular (self ):
5142+ a_real = 7
5143+ b_real = 6
5144+ a_imag = 3
5145+ b_imag = 9
5146+
5147+ yp = np .linspace (0.0 , 2.0 , 20 )
5148+ xp = np .linspace (0.0 , 359.0 , 10 )
5149+ vp_real = np .array ([[a_real * x_i + b_real * y_i for x_i in xp ] for y_i in yp ])
5150+ vp_imag = np .array ([[a_imag * x_i + b_imag * y_i for x_i in xp ] for y_i in yp ])
5151+ vp = vp_real + 1j * vp_imag
5152+ spectrum = DirectionalBinSpectrum (yp , xp , vp , freq_hz = True , degrees = True )
5153+
5154+ y = np .linspace (0.5 , 1.0 , 20 )
5155+ grid_reshaped = spectrum .reshape (
5156+ y , freq_hz = True , complex_convert = "rectangular"
5157+ )
5158+
5159+ freq_out = grid_reshaped ._freq
5160+ dirs_out = grid_reshaped ._dirs
5161+ vals_out = grid_reshaped ._vals
5162+
5163+ freq_expect = (2.0 * np .pi ) * y
5164+ dirs_expect = (np .pi / 180.0 ) * xp
5165+ vals_real_expect = np .array (
5166+ [[a_real * x_i + b_real * y_i for x_i in xp ] for y_i in y ]
5167+ )
5168+ vals_imag_expect = np .array (
5169+ [[a_imag * x_i + b_imag * y_i for x_i in xp ] for y_i in y ]
5170+ )
5171+ vals_expect = (vals_real_expect + 1j * vals_imag_expect ) / (2.0 * np .pi )
5172+
5173+ np .testing .assert_array_almost_equal (freq_out , freq_expect )
5174+ np .testing .assert_array_almost_equal (dirs_out , dirs_expect )
5175+ np .testing .assert_array_almost_equal (vals_out , vals_expect )
5176+
5177+ def test_reshape_complex_polar (self ):
5178+ a_amp = 7
5179+ b_amp = 6
5180+ a_phase = 0.01
5181+ b_phase = 0.03
5182+
5183+ yp = np .linspace (0.0 , 2.0 , 20 )
5184+ xp = np .linspace (0.0 , 359.0 , 10 )
5185+ vp_amp = np .array ([[a_amp * x_i + b_amp * y_i for x_i in xp ] for y_i in yp ])
5186+ vp_phase = np .array (
5187+ [[a_phase * x_i + b_phase * y_i for x_i in xp ] for y_i in yp ]
5188+ )
5189+ vp = vp_amp * (np .cos (vp_phase ) + 1j * np .sin (vp_phase ))
5190+ spectrum = DirectionalBinSpectrum (yp , xp , vp , freq_hz = True , degrees = True )
5191+
5192+ y = np .linspace (0.5 , 1.0 , 20 )
5193+ grid_reshaped = spectrum .reshape (
5194+ y , freq_hz = True , complex_convert = "polar"
5195+ )
5196+
5197+ freq_out = grid_reshaped ._freq
5198+ dirs_out = grid_reshaped ._dirs
5199+ vals_out = grid_reshaped ._vals
5200+
5201+ freq_expect = (2.0 * np .pi ) * y
5202+ dirs_expect = (np .pi / 180.0 ) * xp
5203+ vals_amp_expect = np .array (
5204+ [[a_amp * x_i + b_amp * y_i for x_i in xp ] for y_i in y ]
5205+ ) / (2.0 * np .pi )
5206+ x_ , y_ = np .meshgrid (xp , y , indexing = "ij" , sparse = True )
5207+ vals_phase_cos_expect = RGI ((xp , yp ), np .cos (vp_phase ).T )((x_ , y_ )).T
5208+ vals_phase_sin_expect = RGI ((xp , yp ), np .sin (vp_phase ).T )((x_ , y_ )).T
5209+
5210+ vals_expect = (
5211+ vals_amp_expect
5212+ * (vals_phase_cos_expect + 1j * vals_phase_sin_expect )
5213+ / np .abs (vals_phase_cos_expect + 1j * vals_phase_sin_expect )
5214+ )
5215+
5216+ np .testing .assert_array_almost_equal (freq_out , freq_expect )
5217+ np .testing .assert_array_almost_equal (dirs_out , dirs_expect )
5218+ np .testing .assert_array_almost_equal (vals_out , vals_expect )
5219+
5220+
50925221class Test_WaveSpectrum :
50935222 def test__init__ (self ):
50945223 freq = np .linspace (0 , 1.0 , 10 )
0 commit comments