Skip to content

Commit

Permalink
tests for interp routine, plus new vector routine
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpus committed Feb 8, 2022
1 parent c2a0a02 commit 790767d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions eht_met_forecast/am.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
import sys
import math

from .constants import LEVELS, GFS_DAY, LATLON_DELTA
from .latlon import box
Expand Down Expand Up @@ -42,6 +43,17 @@ def grid_interp(a, u, v):
+ a[0][1] * (1.0 - u) * v + a[1][1] * u * v )


def grid_interp_vector(a, b, u, v):
c = [None, None]
c[0] = [None, None]
c[1] = [None, None]
for i in range(0, 2):
for j in range(0, 2):
c[i][j] = math.sqrt(a[i][j]**2 + b[i][j]**2)

return grid_interp(c, u, v)


# Numerical and physical constants
BADVAL = -99999. # placeholder for missing or undefined data
BADVAL_TEST = -99998.
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/test_am.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pytest import approx
from math import sqrt

from eht_met_forecast import am


def test_grid_interp():
t = [[1, 2], [3, 4]]
u, v = .1, .1
assert am.grid_interp(t, u, v) == approx(1.3)

t = [[0, 1], [0, 1]]
u, v = .1, .1
# 0 + 0 + 1*.9*.1 + 1*.1*.1 = .09+.01
assert am.grid_interp(t, u, v) == approx(0.1)
u, v = .1, .2
# 0 + 0 + 1*.9*.2 + 1*.1*.2 = .18+.02 = 0.2
assert am.grid_interp(t, u, v) == approx(0.2)
u, v = .2, .1
# 0 + 0 + 1*.8*.1 + 1*.2*.1 = .08 + .02 = 0.1
assert am.grid_interp(t, u, v) == approx(0.1)

assert am.grid_interp(t, 0.1, 0.1) * 2.0 == approx(am.grid_interp(t, 0.2, 0.2)), 'diagonal is a line'

t = [[0, 1], [1, 1]] # symmetric in u,v
u, v = .1, .3
assert am.grid_interp(t, u, v) == approx(am.grid_interp(t, v, u)), 'symmetric, is symmetric'
assert am.grid_interp(t, 0.1, 0.0) * 2.0 == approx(am.grid_interp(t, 0.2, 0.0)), 'symmetric, edges are lines'

t = [[0, 1], [1, 2]] # flat
u, v = .1, .3
assert am.grid_interp(t, u, v) == approx(am.grid_interp(t, v, u)), 'flat, is symmetric'

assert am.grid_interp(t, 0.1, 0.1) * 2.0 == approx(am.grid_interp(t, 0.2, 0.2)), 'flat, diagonal is a line'
assert am.grid_interp(t, 0.1, 0.0) * 2.0 == approx(am.grid_interp(t, 0.2, 0.0)), 'flat, edges are lines'


def test_grid_interp_vector():
t1 = [[1, 2], [3, 4]]
t2 = [[1, 2], [3, 4]]
u, v = .1, .1
assert am.grid_interp_vector(t1, t2, u, v) == approx(1.8384776)

c = [[sqrt(2.), sqrt(8.)], [sqrt(18.), sqrt(32.)]]
assert am.grid_interp(c, u, v) == approx(1.8384776)

assert am.grid_interp_vector(t1, t2, u, v) == am.grid_interp(c, u, v)

t1 = [[0, 1], [0, 1]]
t2 = [[0, 1], [0, 1]]
u, v = .1, .1
assert am.grid_interp_vector(t1, t2, u, v) == approx(0.1 * sqrt(2.))

0 comments on commit 790767d

Please sign in to comment.