Skip to content
Snippets Groups Projects
Commit fcabd10f authored by Bruce Flynn's avatar Bruce Flynn
Browse files

Add tests for and fix winds.

parent c7094fc0
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import numpy as np ...@@ -7,6 +7,7 @@ import numpy as np
from .time import to_unix_timestamp from .time import to_unix_timestamp
from .wind import mean_wind_vector_degrees from .wind import mean_wind_vector_degrees
def dewpoint(tempC, relhum): def dewpoint(tempC, relhum):
""" """
Algorithm from Tom Whittaker tempC is the temperature in degrees Celsius, Algorithm from Tom Whittaker tempC is the temperature in degrees Celsius,
......
import unittest import unittest
class MeanVectorAverageTests(unittest.TestCase): class MeanWindVectorTests(unittest.TestCase):
def _fut(self, input_windspd, input_winddir): def _fut(self, winddir, windspd=None):
from aosstower.wind import mean_wind_vector from aosstower.wind import mean_wind_vector
windspd = windspd or [1]*len(winddir)
return mean_wind_vector(input_windspd, input_winddir) return mean_wind_vector(windspd, winddir)[0]
def test_scalar_input(self): def test_spanning_0_degrees(self):
winddir = self._fut([315, 45])
for wdir, wspd in [(0, 20), (90, 20), (180, 20), (270, 20)]: self.assertAlmostEqual(winddir, 0)
output_speed, output_dir = self._fut(wspd, wdir)
self.assertEqual(wdir, output_dir) def test_spanning_cardinal_directions(self):
self.assertEqual(wspd, 20) self.assertAlmostEqual(self._fut([45, 135]), 90)
self.assertAlmostEqual(self._fut([135, 225]), 180)
def test_array_input(self): self.assertAlmostEqual(self._fut([225, 315]), 270)
self.assertAlmostEqual(self._fut([315, 45]), 0)
for wdir, wspd in [([0, 90, 180, 270],[10, 10, 10, 10])]:
output_speed, output_winddir = self._fut(wspd, wdir) def test_all_zeros(self):
self.assertEqual(wdir, output_winddir.tolist()) self.assertAlmostEqual(self._fut([0, 0]), 0)
self.assertEqual(wspd, output_speed.tolist())
def test_zero_windspd(self):
self.assertAlmostEqual(self._fut([0, 0], windspd=[0, 0]), 0)
def test_45s(self):
self.assertAlmostEqual(self._fut([0, 90]), 45)
self.assertAlmostEqual(self._fut([90, 180]), 135)
self.assertAlmostEqual(self._fut([180, 270]), 225)
self.assertAlmostEqual(self._fut([270, 0]), 315)
"""
See: Campbell Scientific CR1000 Manual Section 7.8.5.2.2.
"""
import numpy as np import numpy as np
def mean_wind_vector_components(windspd, winddir): def wind_vector_components(windspd, winddir):
"""Decompose scalar or list/array wind direction and speed data into the """Decompose scalar or list/array wind direction and speed data into the
corresponding horizontal and vertical direction components and speed corresponding horizontal and vertical direction components and speed
vector. vector.
Inputs can be scalar or arrays.
""" """
dir_rad = np.deg2rad(winddir) dir_rad = np.deg2rad(winddir)
V_e = windspd * np.sin(dir_rad) spd_arr = np.array(windspd)
V_n = windspd * np.cos(dir_rad) V_e = spd_arr * np.sin(dir_rad)
U_spd = (V_e**2 + V_n**2)**0.5 V_n = spd_arr * np.cos(dir_rad)
U_spd = np.sqrt(pow(V_e, 2) + pow(V_n, 2))
return V_e, V_n, U_spd return V_e, V_n, U_spd
def mean_wind_vector_degrees(vector_east, vector_north): def wind_vector_degrees(vector_east, vector_north):
"""Re-compose horizontal (east/west) and vertical (north/south) vector """Re-compose horizontal (east/west) and vertical (north/south) vector
components into wind direction in degrees. components into wind direction in degrees.
Inputs can be scalar or arrays.
""" """
rads = np.arctan2(vector_east, vector_north) rads = np.arctan2(vector_east, vector_north)
winddir = np.rad2deg(rads) winddir = np.rad2deg(rads)
...@@ -24,14 +31,11 @@ def mean_wind_vector_degrees(vector_east, vector_north): ...@@ -24,14 +31,11 @@ def mean_wind_vector_degrees(vector_east, vector_north):
winddir[np.less(winddir, 0)] += 360 winddir[np.less(winddir, 0)] += 360
elif winddir < 0: elif winddir < 0:
winddir += 360 winddir += 360
return winddir return winddir % 360
def mean_wind_vector(windspd, winddir): def mean_wind_vector(windspd, winddir):
"""Compute the mean wind vector. V_e, V_n, V_spd = wind_vector_components(windspd, winddir)
avg_dir = wind_vector_degrees(np.mean(V_e), np.mean(V_n))
See: Campbell Scientific CR1000 Manual Section 7.8.5.2.2. return avg_dir, np.mean(V_spd)
"""
vector_east, vector_north, vector_speed = \
mean_wind_vector_components(windspd, winddir)
return vector_speed, mean_wind_vector_degrees(vector_east, vector_north)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment