Skip to content

Commit

Permalink
Add Unittest for meteolib
Browse files Browse the repository at this point in the history
  • Loading branch information
mammatus95 committed Sep 22, 2024
1 parent e6c3450 commit fd873c5
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .jenkins/RunMaps
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ pipeline {
steps {
sh 'export PYTHONPATH=$(pwd) && echo $PYTHONPATH'
sh 'ls -al'
sh 'mkdir -p ./src/images'
sh './run_script.sh 12'
}
}
}
post {
always {

// Archive images and diff for review
archiveArtifacts artifacts: './images/hodographmap_ICON_Nest.png, ./images/example.png, ./diff.png', allowEmptyArchive: true
}
Expand Down
34 changes: 20 additions & 14 deletions src/meteolib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python3
################################
# ---------------------------------------------------------------------------------------------------------------------
#
# Meteorology library
# Meteorology Library
#
################################
# ---------------------------------------------------------------------------------------------------------------------

import numpy as np

Expand All @@ -16,17 +16,6 @@
TOL = 1e-10 # Floating Point Tolerance


def uv2spddir(u, v):

direction = np.rad2deg(np.arctan2(-u, v))

if isinstance(direction, np.ndarray):
direction = np.remainder(direction + 360, 360)
else:
direction = (direction + 360) % 360

return (np.deg2rad(direction), np.sqrt(u*u + v*v))

# ---------------------------------------------------------------------------------------------------------------------
# thermodynamics

Expand Down Expand Up @@ -71,6 +60,23 @@ def temp_at_mixrat(w, p):
# ---------------------------------------------------------------------------------------------------------------------
# kinematics

def uv2spddir(u, v):
direction = np.rad2deg(np.arctan2(-u, -v))
if isinstance(direction, np.ndarray):
direction = np.remainder(direction + 360, 360)
else:
direction = (direction + 360) % 360

wind_speed = np.sqrt(np.square(u) + np.square(v))
if type(wind_speed) is not np.ndarray:
if wind_speed == 0:
direction = np.nan
else:
direction[np.where(wind_speed == 0)] = np.nan

return (direction, wind_speed)


def mean_wind(u, v, ps, stu=0, stv=0):
"""
Parameter:
Expand Down
Binary file modified test/.coverage
Binary file not shown.
111 changes: 111 additions & 0 deletions test/test_meteolib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/python3
import unittest
import numpy as np
# project modul
from src.meteolib import temp_at_mixrat, uv2spddir

# ----------------------------------------------------------------------------------------------------------------------------


class TestTempAtMixrat(unittest.TestCase):
def test_valid_inputs(self):
w = np.array([1, 2, 3]) # mixing ratio (g/kg)
p = np.array([1000, 900, 800]) # pressure (hPa)
expected_result = np.array([256.06, 263.25, 266.93]) # expected temperature (K)
result = temp_at_mixrat(w, p)
np.testing.assert_allclose(result, expected_result, atol=0.01)

def test_edge_cases(self):
w = np.array([30, 1e-6]) # edge cases for mixing ratio
p = np.array([1000, 1000]) # constant pressure
result = temp_at_mixrat(w, p)
#self.assertTrue(np.isnan(result[0])) # expect NaN for zero mixing ratio
#self.assertGreater(result[1], -100) # expect finite temperature for small mixing ratio

# ----------------------------------------------------------------------------------------------------------------------------

class TestUV2SPDDIR(unittest.TestCase):

def setUp(self):

# absolute allowed difference esimate and expected value
self.delta = 0.1 # degree

def test_scalar_inputs(self):
u = 1.0
v = 2.0
direction, speed = uv2spddir(u, v)
self.assertIsInstance(direction, float)
self.assertIsInstance(speed, float)

def test_numpy_array_inputs(self):
u = np.array([1.0, 2.0, 3.0])
v = np.array([4.0, 5.0, 6.0])
direction, speed = uv2spddir(u, v)
self.assertIsInstance(direction, np.ndarray)
self.assertIsInstance(speed, np.ndarray)

def test_zero_inputs(self):
u = 0.0
v = 0.0
direction, speed = uv2spddir(u, v)
self.assertTrue(np.isnan(direction))
self.assertEqual(speed, 0.0)

def test_east_wind(self):
u = -10.0
v = 0.0
expected_wdir = 90.0
excepted_speed = 10.0
direction, speed = uv2spddir(u, v)
self.assertAlmostEqual(direction, expected_wdir, delta=self.delta)
self.assertEqual(speed, excepted_speed)

def test_west_wind(self):
u = 10.0
v = 0.0
expected_wdir = 270.0
excepted_speed = 10.0
direction, speed = uv2spddir(u, v)
self.assertAlmostEqual(direction, expected_wdir, delta=self.delta)
self.assertEqual(speed, excepted_speed)

def test_south_wind(self):
u = 0.0
v = 10.0
expected_wdir = 180.0
excepted_speed = 10.0
direction, speed = uv2spddir(u, v)
self.assertAlmostEqual(direction, expected_wdir, delta=self.delta)
self.assertEqual(speed, excepted_speed)

def test_north_wind(self):
u = 0.0
v = -10.0
expected_wdir = 0.0
excepted_speed = 10.0
direction, speed = uv2spddir(u, v)
self.assertAlmostEqual(direction, expected_wdir, delta=self.delta)
self.assertEqual(speed, excepted_speed)

def test_southwest_wind(self):
u = 3.0
v = 4.0
expected_wdir = 216.87
excepted_speed = 5.0
direction, speed = uv2spddir(u, v)
self.assertAlmostEqual(direction, expected_wdir, delta=self.delta)
self.assertEqual(speed, excepted_speed)

def test_northwest_wind(self):
u = 5.0
v = -3.0
expected_wdir = 300.96
direction, _ = uv2spddir(u, v)
self.assertAlmostEqual(direction, expected_wdir, delta=self.delta)

# ----------------------------------------------------------------------------------------------------------------------------


if __name__ == '__main__':
unittest.main()

0 comments on commit fd873c5

Please sign in to comment.