Bill Ladwig 6 years ago
parent
commit
f7417030a6
  1. 5
      test/ci_tests/make_test_file.py
  2. 44
      test/ci_tests/utests.py

5
test/ci_tests/make_test_file.py

@ -61,7 +61,7 @@ def copy_and_reduce(opts):
continue continue
outvar = outfile.createVariable(name, variable.datatype, outvar = outfile.createVariable(name, variable.datatype,
variable.dimensions, zlib=True) variable.dimensions, zlib=True)
in_slices = tuple(slice(0, dimsize) for dimsize in outvar.shape) in_slices = tuple(slice(0, dimsize) for dimsize in outvar.shape)
@ -131,7 +131,7 @@ def make_result_file(opts):
tk = getvar(infile, "temp", units="k") tk = getvar(infile, "temp", units="k")
interp_levels = [200,300,500,1000] interp_levels = [200, 300, 500, 1000]
field = vinterp(infile, field = vinterp(infile,
field=tk, field=tk,
@ -159,6 +159,7 @@ def make_result_file(opts):
ll = xy_to_ll(infile, x_s[0], y_s[0]) ll = xy_to_ll(infile, x_s[0], y_s[0])
add_to_ncfile(outfile, ll, "ll") add_to_ncfile(outfile, ll, "ll")
def main(opts): def main(opts):
copy_and_reduce(opts) copy_and_reduce(opts)
make_result_file(opts) make_result_file(opts)

44
test/ci_tests/utests.py

@ -2,7 +2,8 @@ import unittest as ut
import numpy.testing as nt import numpy.testing as nt
import numpy as np import numpy as np
import numpy.ma as ma import numpy.ma as ma
import os, sys import os
import sys
import subprocess import subprocess
from wrf import (getvar, interplevel, interpline, vertcross, vinterp, from wrf import (getvar, interplevel, interpline, vertcross, vinterp,
@ -20,6 +21,7 @@ REF_FILE = "ci_result_file.nc"
if sys.version_info > (3,): if sys.version_info > (3,):
xrange = range xrange = range
# Using helpful information at: # Using helpful information at:
# http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases # http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases
def make_test(varname, wrf_in, referent, multi=False, repeat=3, pynio=False): def make_test(varname, wrf_in, referent, multi=False, repeat=3, pynio=False):
@ -34,19 +36,17 @@ def make_test(varname, wrf_in, referent, multi=False, repeat=3, pynio=False):
multiproduct = varname in ("uvmet", "uvmet10", "cape_2d", "cape_3d", multiproduct = varname in ("uvmet", "uvmet10", "cape_2d", "cape_3d",
"cfrac") "cfrac")
ref_vals = refnc.variables[varname][:] ref_vals = refnc.variables[varname][:]
if (varname == "tc"): if (varname == "tc"):
my_vals = getvar(in_wrfnc, "temp", timeidx=timeidx, units="c") my_vals = getvar(in_wrfnc, "temp", timeidx=timeidx, units="c")
tol = 1/100. tol = 1/100.
atol = .1 # Note: NCL uses 273.16 as conversion for some reason atol = .1 # Note: NCL uses 273.16 as conversion for some reason
nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol) nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
elif (varname == "pw"): elif (varname == "pw"):
my_vals = getvar(in_wrfnc, "pw", timeidx=timeidx) my_vals = getvar(in_wrfnc, "pw", timeidx=timeidx)
tol = .5/100.0 tol = .5/100.0
atol = 0 # NCL uses different constants and doesn't use same atol = 0
# handrolled virtual temp in method
nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol) nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
elif (varname == "cape_2d"): elif (varname == "cape_2d"):
cape_2d = getvar(in_wrfnc, varname, timeidx=timeidx) cape_2d = getvar(in_wrfnc, varname, timeidx=timeidx)
@ -56,7 +56,7 @@ def make_test(varname, wrf_in, referent, multi=False, repeat=3, pynio=False):
# merged back in to NCL. The modifications to the R and CP # merged back in to NCL. The modifications to the R and CP
# changes TK enough that non-lifting parcels could lift, thus # changes TK enough that non-lifting parcels could lift, thus
# causing wildly different values in LCL # causing wildly different values in LCL
nt.assert_allclose(to_np(cape_2d[0,:]), ref_vals[0,:], tol, atol) nt.assert_allclose(to_np(cape_2d[0, :]), ref_vals[0, :], tol, atol)
elif (varname == "cape_3d"): elif (varname == "cape_3d"):
cape_3d = getvar(in_wrfnc, varname, timeidx=timeidx) cape_3d = getvar(in_wrfnc, varname, timeidx=timeidx)
# Changing the R and CP constants, while keeping TK within # Changing the R and CP constants, while keeping TK within
@ -67,18 +67,18 @@ def make_test(varname, wrf_in, referent, multi=False, repeat=3, pynio=False):
tol = 0/100. tol = 0/100.
atol = 200.0 atol = 200.0
#print np.amax(np.abs(to_np(cape_3d[0,:]) - ref_vals[0,:])) # print np.amax(np.abs(to_np(cape_3d[0,:]) - ref_vals[0,:]))
nt.assert_allclose(to_np(cape_3d), ref_vals, tol, atol) nt.assert_allclose(to_np(cape_3d), ref_vals, tol, atol)
else: else:
my_vals = getvar(in_wrfnc, varname, timeidx=timeidx) my_vals = getvar(in_wrfnc, varname, timeidx=timeidx)
tol = 2/100. tol = 2/100.
atol = 0.1 atol = 0.1
#print (np.amax(np.abs(to_np(my_vals) - ref_vals))) # print (np.amax(np.abs(to_np(my_vals) - ref_vals)))
nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol) nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
return test return test
def _get_refvals(referent, varname, repeat, multi): def _get_refvals(referent, varname, repeat, multi):
from netCDF4 import Dataset as NetCDF from netCDF4 import Dataset as NetCDF
@ -88,6 +88,7 @@ def _get_refvals(referent, varname, repeat, multi):
return ref_vals return ref_vals
def make_interp_test(varname, wrf_in, referent, multi=False, def make_interp_test(varname, wrf_in, referent, multi=False,
repeat=3, pynio=False): repeat=3, pynio=False):
def test(self): def test(self):
@ -103,7 +104,7 @@ def make_interp_test(varname, wrf_in, referent, multi=False,
# Check that it works with numpy arrays # Check that it works with numpy arrays
hts_850 = interplevel(to_np(hts), p, 850) hts_850 = interplevel(to_np(hts), p, 850)
#print (hts_850) # print (hts_850)
hts_850 = interplevel(hts, p, 850) hts_850 = interplevel(hts, p, 850)
nt.assert_allclose(to_np(hts_850), ref_ht_850) nt.assert_allclose(to_np(hts_850), ref_ht_850)
@ -118,12 +119,11 @@ def make_interp_test(varname, wrf_in, referent, multi=False,
# Check that it works with numpy arrays # Check that it works with numpy arrays
ht_cross = vertcross(to_np(hts), to_np(p), ht_cross = vertcross(to_np(hts), to_np(p),
pivot_point=pivot_point, angle=90.) pivot_point=pivot_point, angle=90.)
#print (ht_cross) # print (ht_cross)
ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.) ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.)
nt.assert_allclose(to_np(ht_cross), ref_ht_cross, rtol=.01) nt.assert_allclose(to_np(ht_cross), ref_ht_cross, rtol=.01)
elif (varname == "interpline"): elif (varname == "interpline"):
ref_t2_line = _get_refvals(referent, "interpline", repeat, multi) ref_t2_line = _get_refvals(referent, "interpline", repeat, multi)
@ -134,7 +134,7 @@ def make_interp_test(varname, wrf_in, referent, multi=False,
# Check that it works with numpy arrays # Check that it works with numpy arrays
t2_line1 = interpline(to_np(t2), pivot_point=pivot_point, t2_line1 = interpline(to_np(t2), pivot_point=pivot_point,
angle=90.0) angle=90.0)
#print (t2_line1) # print (t2_line1)
t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0) t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)
nt.assert_allclose(to_np(t2_line1), ref_t2_line) nt.assert_allclose(to_np(t2_line1), ref_t2_line)
@ -146,7 +146,7 @@ def make_interp_test(varname, wrf_in, referent, multi=False,
tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k") tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k")
interp_levels = [200,300,500,1000] interp_levels = [200, 300, 500, 1000]
# Check that it works with numpy arrays # Check that it works with numpy arrays
field = vinterp(in_wrfnc, field = vinterp(in_wrfnc,
@ -157,7 +157,7 @@ def make_interp_test(varname, wrf_in, referent, multi=False,
field_type="tk", field_type="tk",
timeidx=timeidx, timeidx=timeidx,
log_p=True) log_p=True)
#print (field) # print (field)
field = vinterp(in_wrfnc, field = vinterp(in_wrfnc,
field=tk, field=tk,
@ -200,14 +200,13 @@ def make_latlon_test(testid, wrf_in, referent, single, multi=False, repeat=3,
nt.assert_allclose(to_np(xy), ref_vals) nt.assert_allclose(to_np(xy), ref_vals)
else: else:
# Since this domain is not moving, the reference values are the # Since this domain is not moving, the reference values are the
# same whether there are multiple or single files # same whether there are multiple or single files
ref_vals = refnc.variables["ll"][:] ref_vals = refnc.variables["ll"][:]
# i_s, j_s taken from NCL script, just hard-coding for now # i_s, j_s taken from NCL script, just hard-coding for now
# NCL uses 1-based indexing for this, so need to subtract 1 # NCL uses 1-based indexing for this, so need to subtract 1
x_s = np.asarray([10, 50, 90], int) x_s = np.asarray([10, 50, 90], int)
y_s = np.asarray([10, 50, 90], int) y_s = np.asarray([10, 50, 90], int)
@ -215,15 +214,17 @@ def make_latlon_test(testid, wrf_in, referent, single, multi=False, repeat=3,
nt.assert_allclose(to_np(ll), ref_vals) nt.assert_allclose(to_np(ll), ref_vals)
return test return test
class WRFVarsTest(ut.TestCase): class WRFVarsTest(ut.TestCase):
longMessage = True longMessage = True
class WRFInterpTest(ut.TestCase): class WRFInterpTest(ut.TestCase):
longMessage = True longMessage = True
class WRFLatLonTest(ut.TestCase): class WRFLatLonTest(ut.TestCase):
longMessage = True longMessage = True
@ -261,10 +262,7 @@ if __name__ == "__main__":
repeat=3, pynio=False) repeat=3, pynio=False)
multistr = "" if not multi else "_multi" multistr = "" if not multi else "_multi"
singlestr = "_nosingle" if not single else "_single" singlestr = "_nosingle" if not single else "_single"
test_name = "test_{}{}{}".format(testid, singlestr, test_name = "test_{}{}{}".format(testid, singlestr, multistr)
multistr)
setattr(WRFLatLonTest, test_name, test_ll_func) setattr(WRFLatLonTest, test_name, test_ll_func)
ut.main() ut.main()
Loading…
Cancel
Save