@ -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
import glob
import glob
@ -14,7 +15,6 @@ from wrf.util import is_multi_file
NCL_EXE = " /Users/ladwig/miniconda2/envs/ncl_build/bin/ncl "
NCL_EXE = " /Users/ladwig/miniconda2/envs/ncl_build/bin/ncl "
NCARG_ROOT = " /Users/ladwig/miniconda2/envs/ncl_build "
NCARG_ROOT = " /Users/ladwig/miniconda2/envs/ncl_build "
#TEST_FILE = "/Users/ladwig/Documents/wrf_files/wrfout_d01_2010-06-13_21:00:00"
DIRS = [ " /Users/ladwig/Documents/wrf_files/wrf_vortex_multi/moving_nest " ,
DIRS = [ " /Users/ladwig/Documents/wrf_files/wrf_vortex_multi/moving_nest " ,
" /Users/ladwig/Documents/wrf_files/wrf_vortex_multi/static_nest " ]
" /Users/ladwig/Documents/wrf_files/wrf_vortex_multi/static_nest " ]
PATTERN = " wrfout_d02_* "
PATTERN = " wrfout_d02_* "
@ -25,22 +25,21 @@ NEST = ["moving", "static"]
if sys . version_info > ( 3 , ) :
if sys . version_info > ( 3 , ) :
xrange = range
xrange = range
def setUpModule ( ) :
def setUpModule ( ) :
# ncarg_root = os.environ.get("NCARG_ROOT", None)
# ncarg_root = os.environ.get("NCARG_ROOT", None)
# if ncarg_root is None:
# if ncarg_root is None:
# raise RuntimeError("$NCARG_ROOT environment variable not set")
# raise RuntimeError("$NCARG_ROOT environment variable not set")
os . environ [ " NCARG_ROOT " ] = NCARG_ROOT
os . environ [ " NCARG_ROOT " ] = NCARG_ROOT
os . environ [ " NCARG_NCARG " ] = os . path . join ( NCARG_ROOT , " lib " , " ncarg " )
os . environ [ " NCARG_NCARG " ] = os . path . join ( NCARG_ROOT , " lib " , " ncarg " )
os . environ [ " OMP_NUM_THREADS " ] = " 4 "
os . environ [ " OMP_NUM_THREADS " ] = " 4 "
this_path = os . path . realpath ( __file__ )
this_path = os . path . realpath ( __file__ )
ncl_script = os . path . join ( os . path . dirname ( this_path ) ,
ncl_script = os . path . join ( os . path . dirname ( this_path ) ,
" ncl_get_var.ncl " )
" ncl_get_var.ncl " )
for dir , outfile in zip ( DIRS , REF_NC_FILES ) :
for dir , outfile in zip ( DIRS , REF_NC_FILES ) :
cmd = " %s %s ' dir= \" %s \" ' ' pattern= \" %s \" ' ' out_file= \" %s \" ' " % (
cmd = " {} {} ' dir= \" {} \" ' ' pattern= \" {} \" ' ' out_file= \" {} \" ' " . format (
NCL_EXE ,
NCL_EXE ,
ncl_script ,
ncl_script ,
dir ,
dir ,
@ -54,19 +53,20 @@ def setUpModule():
if ( status != 0 ) :
if ( status != 0 ) :
raise RuntimeError ( " NCL script failed. Could not set up test. " )
raise RuntimeError ( " NCL script failed. Could not set up test. " )
# 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 , dir , pattern , referent , multi = False , pynio = False ) :
def make_test ( varname , dir , pattern , referent , multi = False , pynio = False ) :
def test ( self ) :
def test ( self ) :
try :
try :
from netCDF4 import Dataset as NetCDF
from netCDF4 import Dataset as NetCDF
except :
except ImportError :
pass
pass
try :
try :
import Nio
import Nio
except :
except ImportError :
pass
pass
timeidx = 0 if not multi else None
timeidx = 0 if not multi else None
@ -80,7 +80,7 @@ def make_test(varname, dir, pattern, referent, multi=False, pynio=False):
f = NetCDF ( fname )
f = NetCDF ( fname )
try :
try :
f . set_always_mask ( False )
f . set_always_mask ( False )
except :
except AttributeError :
pass
pass
wrfin . append ( f )
wrfin . append ( f )
else :
else :
@ -94,7 +94,7 @@ def make_test(varname, dir, pattern, referent, multi=False, pynio=False):
refnc = NetCDF ( referent )
refnc = NetCDF ( referent )
try :
try :
refnc . set_auto_mask ( False )
refnc . set_auto_mask ( False )
except :
except AttributeError :
pass
pass
# These have a left index that defines the product type
# These have a left index that defines the product type
@ -120,66 +120,28 @@ def make_test(varname, dir, pattern, referent, multi=False, pynio=False):
else :
else :
ref_vals = refnc . variables [ varname ] [ : ]
ref_vals = refnc . variables [ varname ] [ : ]
if ( varname == " tc " ) :
if ( varname == " zstag " or varname == " geopt_stag " ) :
my_vals = getvar ( wrfin , " temp " , timeidx = timeidx , units = " c " )
tol = 1 / 100.
atol = .1 # Note: NCL uses 273.16 as conversion for some reason
nt . assert_allclose ( to_np ( my_vals ) , ref_vals , tol , atol )
elif ( varname == " height_agl " ) :
# Change the vert_type to height_agl when NCL gets updated.
my_vals = getvar ( wrfin , " z " , timeidx = timeidx , msl = False )
tol = 1 / 100.
atol = .1 # Note: NCL uses 273.16 as conversion for some reason
nt . assert_allclose ( to_np ( my_vals ) , ref_vals , tol , atol )
elif ( varname == " cfrac " ) :
# Change the vert_type to height_agl when NCL gets updated.
my_vals = getvar ( wrfin , " cfrac " , timeidx = timeidx )
tol = 1 / 100.
atol = .1 # Note: NCL uses 273.16 as conversion for some reason
nt . assert_allclose ( to_np ( my_vals ) , ref_vals , tol , atol )
elif ( varname == " pw " ) :
my_vals = getvar ( wrfin , " pw " , timeidx = timeidx )
tol = .5 / 100.0
atol = 0 # NCL uses different constants and doesn't use same
# handrolled virtual temp in method
try :
nt . assert_allclose ( to_np ( my_vals ) , ref_vals , tol , atol )
except AssertionError :
print ( np . amax ( np . abs ( to_np ( my_vals ) - ref_vals ) ) )
raise
elif ( varname == " cape_2d " ) :
cape_2d = getvar ( wrfin , varname , timeidx = timeidx )
tol = 0 / 100.
atol = 200.0
# Let's only compare CAPE values until the F90 changes are
# merged back in to NCL. The modifications to the R and CP
# changes TK enough that non-lifting parcels could lift, thus
# causing wildly different values in LCL
nt . assert_allclose ( to_np ( cape_2d [ 0 , : ] ) , ref_vals [ 0 , : ] , tol , atol )
elif ( varname == " cape_3d " ) :
cape_3d = getvar ( wrfin , varname , timeidx = timeidx )
# Changing the R and CP constants, while keeping TK within
# 2%, can lead to some big changes in CAPE. Tolerances
# have been set wide when comparing the with the original
# NCL. Change back when the F90 code is merged back with
# NCL
tol = 0 / 100.
atol = 200.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 )
elif ( varname == " zstag " or varname == " geopt_stag " ) :
v = getvar ( wrfin , varname , timeidx = timeidx )
v = getvar ( wrfin , varname , timeidx = timeidx )
# For now, only make sure it runs without crashing since no NCL
# For now, only make sure it runs without crashing since no NCL
# to compare with yet.
# to compare with yet.0
else :
else :
if varname == " cape_2d " or varname == " cape_3d " :
# Cape still has a small floating point issue that
# hasn't been completely resolved, so for now check
# that cape is within 50.
my_vals = getvar ( wrfin , varname , timeidx = timeidx )
my_vals = getvar ( wrfin , varname , timeidx = timeidx )
tol = 2 / 100.
rtol = 0
atol = 0.1
atol = 50.
#print (np.amax(np.abs(to_np(my_vals) - ref_vals)))
else :
# All other tests should be within .001 of each other
my_vals = getvar ( wrfin , varname , timeidx = timeidx )
rtol = 0
atol = 1.0E-3
try :
try :
nt . assert_allclose ( to_np ( my_vals ) , ref_vals , tol , atol )
nt . assert_allclose ( to_np ( my_vals ) , ref_vals , rtol , atol )
except :
except AssertionError :
absdiff = np . abs ( to_np ( my_vals ) - ref_vals )
absdiff = np . abs ( to_np ( my_vals ) - ref_vals )
maxdiff = np . amax ( absdiff )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( maxdiff )
@ -187,24 +149,23 @@ def make_test(varname, dir, pattern, referent, multi=False, pynio=False):
raise
raise
return test
return test
def _get_refvals ( referent , varname , multi ) :
def _get_refvals ( referent , varname , multi ) :
try :
try :
from netCDF4 import Dataset as NetCDF
from netCDF4 import Dataset as NetCDF
except :
except ImportError :
pass
pass
refnc = NetCDF ( referent )
refnc = NetCDF ( referent )
try :
try :
pass
pass
#refnc.set_auto_mask(False)
except ImportError :
except :
pass
pass
multi2d = ( " uvmet10 " , " wspd_wdir10 " , " uvmet10_wspd_wdir " ,
multi2d = ( " uvmet10 " , " wspd_wdir10 " , " uvmet10_wspd_wdir " , " cape_2d " ,
" cape_2d " , " c frac " )
" cfrac " )
multi3d = ( " uvmet " , " wspd_wdir " , " uvmet_wspd_wdir " , " cape_3d " )
multi3d = ( " uvmet " , " wspd_wdir " , " uvmet_wspd_wdir " , " cape_3d " )
interpline = ( " t2_line " , " t2_line2 " , " t2_line3 " )
interpline = ( " t2_line " , " t2_line2 " , " t2_line3 " )
@ -227,17 +188,18 @@ def _get_refvals(referent, varname, multi):
return ref_vals
return ref_vals
def make_interp_test ( varname , dir , pattern , referent , multi = False ,
def make_interp_test ( varname , dir , pattern , referent , multi = False ,
pynio = False ) :
pynio = False ) :
def test ( self ) :
def test ( self ) :
try :
try :
from netCDF4 import Dataset as NetCDF
from netCDF4 import Dataset as NetCDF
except :
except ImportError :
pass
pass
try :
try :
import Nio
import Nio
except :
except ImportError :
pass
pass
timeidx = 0 if not multi else None
timeidx = 0 if not multi else None
@ -251,7 +213,7 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
f = NetCDF ( fname )
f = NetCDF ( fname )
try :
try :
f . set_always_mask ( False )
f . set_always_mask ( False )
except :
except AttributeError :
pass
pass
wrfin . append ( f )
wrfin . append ( f )
else :
else :
@ -292,7 +254,6 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
p_5000 = interplevel ( to_np ( p ) , to_np ( hts ) , 5000 )
p_5000 = interplevel ( to_np ( p ) , to_np ( hts ) , 5000 )
p_5000 = interplevel ( p , hts , 5000 )
p_5000 = interplevel ( p , hts , 5000 )
nt . assert_allclose ( to_np ( p_5000 ) , ref_p_5000 )
nt . assert_allclose ( to_np ( p_5000 ) , ref_p_5000 )
nt . assert_allclose ( to_np ( p_5000 ) , ref_p2_5000 )
nt . assert_allclose ( to_np ( p_5000 ) , ref_p2_5000 )
@ -318,13 +279,11 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
# Just make sure these run below
# Just make sure these run below
wspd_wdir_500 = interplevel ( to_np ( wspd_wdir ) , to_np ( p ) , 500 )
wspd_wdir_500 = interplevel ( to_np ( wspd_wdir ) , to_np ( p ) , 500 )
wspd_wdir_500 = interplevel ( wspd_wdir , p , 500 )
wspd_wdir_500 = interplevel ( wspd_wdir , p , 500 )
#print(wspd_wdir_500)
wspd_wdir_multi = interplevel ( to_np ( wspd_wdir ) ,
wspd_wdir_multi = interplevel ( to_np ( wspd_wdir ) , to_np ( p ) ,
to_np ( p ) , [ 1000 , 500 , 250 ] )
[ 1000 , 500 , 250 ] )
wdpd_wdir_multi = interplevel ( wspd_wdir , p , [ 1000 , 500 , 250 ] )
wdpd_wdir_multi = interplevel ( wspd_wdir , p , [ 1000 , 500 , 250 ] )
wspd_wdir_pblh = interplevel ( to_np ( wspd_wdir ) , to_np ( hts ) , pblh )
wspd_wdir_pblh = interplevel ( to_np ( wspd_wdir ) , to_np ( hts ) , pblh )
wspd_wdir_pblh = interplevel ( wspd_wdir , hts , pblh )
wspd_wdir_pblh = interplevel ( wspd_wdir , hts , pblh )
@ -344,7 +303,6 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
nt . assert_allclose ( to_np ( wspd_wdir_pblh_2 [ : , - 1 , : ] ) ,
nt . assert_allclose ( to_np ( wspd_wdir_pblh_2 [ : , - 1 , : ] ) ,
to_np ( wspd_wdir_pblh [ : , 0 , : ] ) )
to_np ( wspd_wdir_pblh [ : , 0 , : ] ) )
elif ( varname == " vertcross " ) :
elif ( varname == " vertcross " ) :
ref_ht_cross = _get_refvals ( referent , " ht_cross " , multi )
ref_ht_cross = _get_refvals ( referent , " ht_cross " , multi )
ref_p_cross = _get_refvals ( referent , " p_cross " , multi )
ref_p_cross = _get_refvals ( referent , " p_cross " , multi )
@ -377,7 +335,14 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
ht_cross = vertcross ( hts , p , pivot_point = pivot_point , angle = 90. ,
ht_cross = vertcross ( hts , p , pivot_point = pivot_point , angle = 90. ,
autolevels = p_autolevels )
autolevels = p_autolevels )
nt . assert_allclose ( to_np ( ht_cross ) , ref_ht_cross , rtol = .01 )
try :
nt . assert_allclose ( to_np ( ht_cross ) , ref_ht_cross , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( ht_cross ) - ref_ht_cross )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
lats = hts . coords [ " XLAT " ]
lats = hts . coords [ " XLAT " ]
lons = hts . coords [ " XLONG " ]
lons = hts . coords [ " XLONG " ]
@ -400,55 +365,88 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
v1 = vertcross ( hts , p , wrfin = wrfin , pivot_point = pivot_point ,
v1 = vertcross ( hts , p , wrfin = wrfin , pivot_point = pivot_point ,
angle = 90.0 )
angle = 90.0 )
v2 = vertcross ( hts , p , projection = hts . attrs [ " projection " ] ,
v2 = vertcross ( hts , p , projection = hts . attrs [ " projection " ] ,
ll_point = ll_point ,
ll_point = ll_point , pivot_point = pivot _point ,
pivot_point = pivot_point , angle = 90. )
angle = 90. )
nt . assert_allclose ( to_np ( v1 ) , to_np ( v2 ) , rtol = .01 )
try :
nt . assert_allclose ( to_np ( v1 ) , to_np ( v2 ) , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( v1 ) - to_np ( v2 ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Test opposite
# Test opposite
p_cross1 = vertcross ( p , hts , pivot_point = pivot_point , angle = 90.0 )
p_cross1 = vertcross ( p , hts , pivot_point = pivot_point , angle = 90.0 )
nt . assert_allclose ( to_np ( p_cross1 ) ,
try :
ref_p_cross ,
nt . assert_allclose ( to_np ( p_cross1 ) , ref_p_cross , atol = .0001 )
rtol = .01 )
except AssertionError :
absdiff = np . abs ( to_np ( p_cross1 ) - ref_p_cross )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Test point to point
# Test point to point
start_point = CoordPair ( 0 , hts . shape [ - 2 ] / 2 )
start_point = CoordPair ( 0 , hts . shape [ - 2 ] / 2 )
end_point = CoordPair ( - 1 , hts . shape [ - 2 ] / 2 )
end_point = CoordPair ( - 1 , hts . shape [ - 2 ] / 2 )
p_cross2 = vertcross ( p , hts , start_point = start_point ,
p_cross2 = vertcross ( p , hts , start_point = start_point ,
end_point = end_point )
end_point = end_point )
nt . assert_allclose ( to_np ( p_cross1 ) ,
try :
to_np ( p_cross2 ) )
nt . assert_allclose ( to_np ( p_cross1 ) , to_np ( p_cross2 ) ,
atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( p_cross1 ) - to_np ( p_cross2 ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Check the new vertcross routine
# Check the new vertcross routine
pivot_point = CoordPair ( hts . shape [ - 1 ] / 2 , hts . shape [ - 2 ] / 2 )
pivot_point = CoordPair ( hts . shape [ - 1 ] / 2 , hts . shape [ - 2 ] / 2 )
ht_cross = vertcross ( hts , p ,
ht_cross = vertcross ( hts , p , pivot_point = pivot_point , angle = 90. ,
pivot_point = pivot_point , angle = 90. ,
latlon = True )
latlon = True )
try :
nt . assert_allclose ( to_np ( ht_cross ) ,
nt . assert_allclose ( to_np ( ht_cross ) ,
to_np ( ref_ht_vertcross1 ) , atol = .01 )
to_np ( ref_ht_vertcross1 ) , atol = .005 )
except AssertionError :
absdiff = np . abs ( to_np ( ht_cross ) - to_np ( ref_ht_vertcross1 ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
levels = [ 1000. , 850. , 700. , 500. , 250. ]
levels = [ 1000. , 850. , 700. , 500. , 250. ]
ht_cross = vertcross ( hts , p ,
ht_cross = vertcross ( hts , p , pivot_point = pivot_point , angle = 90. ,
pivot_point = pivot_point , angle = 90. ,
levels = levels , latlon = True )
levels = levels , latlon = True )
nt . assert_allclose ( to_np ( ht_cross ) ,
try :
to_np ( ref_ht_vertcross2 ) , atol = .01 )
nt . assert_allclose ( to_np ( ht_cross ) , to_np ( ref_ht_vertcross2 ) ,
atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( ht_cross ) - to_np ( ref_ht_vertcross2 ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
idxs = ( 0 , slice ( None ) ) if lats . ndim > 2 else ( slice ( None ) , )
idxs = ( 0 , slice ( None ) ) if lats . ndim > 2 else ( slice ( None ) , )
start_lat = np . amin ( lats [ idxs ] ) + .25 * ( np . amax ( lats [ idxs ] ) - np . amin ( lats [ idxs ] ) )
start_lat = np . amin ( lats [ idxs ] ) + .25 * ( np . amax ( lats [ idxs ] )
end_lat = np . amin ( lats [ idxs ] ) + .65 * ( np . amax ( lats [ idxs ] ) - np . amin ( lats [ idxs ] ) )
- np . amin ( lats [ idxs ] ) )
end_lat = np . amin ( lats [ idxs ] ) + .65 * ( np . amax ( lats [ idxs ] )
- np . amin ( lats [ idxs ] ) )
start_lon = np . amin ( lons [ idxs ] ) + .25 * ( np . amax ( lons [ idxs ] ) - np . amin ( lons [ idxs ] ) )
start_lon = np . amin ( lons [ idxs ] ) + .25 * ( np . amax ( lons [ idxs ] )
end_lon = np . amin ( lons [ idxs ] ) + .65 * ( np . amax ( lons [ idxs ] ) - np . amin ( lons [ idxs ] ) )
- np . amin ( lons [ idxs ] ) )
end_lon = np . amin ( lons [ idxs ] ) + .65 * ( np . amax ( lons [ idxs ] )
- np . amin ( lons [ idxs ] ) )
start_point = CoordPair ( lat = start_lat , lon = start_lon )
start_point = CoordPair ( lat = start_lat , lon = start_lon )
end_point = CoordPair ( lat = end_lat , lon = end_lon )
end_point = CoordPair ( lat = end_lat , lon = end_lon )
@ -463,10 +461,15 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
latlon = True ,
latlon = True ,
autolevels = 1000 )
autolevels = 1000 )
try :
nt . assert_allclose ( to_np ( ht_cross ) ,
nt . assert_allclose ( to_np ( ht_cross ) , to_np ( ref_ht_vertcross3 ) ,
to_np ( ref_ht_vertcross3 ) ,
atol = .01 )
rtol = .01 )
except AssertionError :
absdiff = np . abs ( to_np ( ht_cross ) - to_np ( ref_ht_vertcross3 ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
if multi :
if multi :
ntimes = hts . shape [ 0 ]
ntimes = hts . shape [ 0 ]
@ -475,20 +478,23 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
hts = getvar ( wrfin , " z " , timeidx = t )
hts = getvar ( wrfin , " z " , timeidx = t )
p = getvar ( wrfin , " pressure " , timeidx = t )
p = getvar ( wrfin , " pressure " , timeidx = t )
ht_cross = vertcross ( hts , p ,
ht_cross = vertcross (
start_point = start_point ,
hts , p , start_point = start_point , end_point = end_point ,
end_point = end_point ,
wrfin = wrfin , timeidx = t , latlon = True , autolevels = 1000 )
wrfin = wrfin ,
timeidx = t ,
latlon = True ,
autolevels = 1000 )
refname = " ht_vertcross_t {} " . format ( t )
refname = " ht_vertcross_t {} " . format ( t )
ref_ht_vertcross = _get_refvals ( referent , refname , False )
ref_ht_vertcross = _get_refvals ( referent , refname , False )
try :
nt . assert_allclose ( to_np ( ht_cross ) ,
nt . assert_allclose ( to_np ( ht_cross ) ,
to_np ( ref_ht_vertcross ) , rtol = .02 )
to_np ( ref_ht_vertcross ) , atol = .01 )
except AssertionError :
absdiff = np . abs ( to_np ( ht_cross ) -
to_np ( ref_ht_vertcross ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
elif ( varname == " interpline " ) :
elif ( varname == " interpline " ) :
@ -507,7 +513,14 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
nt . assert_allclose ( to_np ( t2_line1 ) , ref_t2_line )
nt . assert_allclose ( to_np ( t2_line1 ) , ref_t2_line )
# Test the new NCL wrf_user_interplevel result
# Test the new NCL wrf_user_interplevel result
try :
nt . assert_allclose ( to_np ( t2_line1 ) , ref_t2_line2 )
nt . assert_allclose ( to_np ( t2_line1 ) , ref_t2_line2 )
except AssertionError :
absdiff = np . abs ( to_np ( t2_line1 ) - ref_t2_line2 )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Test the manual projection method with lat/lon
# Test the manual projection method with lat/lon
lats = t2 . coords [ " XLAT " ]
lats = t2 . coords [ " XLAT " ]
@ -528,9 +541,16 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
angle = 90.0 )
angle = 90.0 )
l2 = interpline ( t2 , projection = t2 . attrs [ " projection " ] ,
l2 = interpline ( t2 , projection = t2 . attrs [ " projection " ] ,
ll_point = ll_point ,
ll_point = ll_point , pivot_point = pivot_point ,
pivot_point = pivot_point , angle = 90. )
angle = 90. )
nt . assert_allclose ( to_np ( l1 ) , to_np ( l2 ) , rtol = .01 )
try :
nt . assert_allclose ( to_np ( l1 ) , to_np ( l2 ) )
except AssertionError :
absdiff = np . abs ( to_np ( l1 ) - to_np ( l2 ) )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Test point to point
# Test point to point
start_point = CoordPair ( 0 , t2 . shape [ - 2 ] / 2 )
start_point = CoordPair ( 0 , t2 . shape [ - 2 ] / 2 )
@ -539,7 +559,14 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
t2_line2 = interpline ( t2 , start_point = start_point ,
t2_line2 = interpline ( t2 , start_point = start_point ,
end_point = end_point )
end_point = end_point )
try :
nt . assert_allclose ( to_np ( t2_line1 ) , to_np ( t2_line2 ) )
nt . assert_allclose ( to_np ( t2_line1 ) , to_np ( t2_line2 ) )
except AssertionError :
absdiff = np . abs ( to_np ( t2_line1 ) - t2_line2 )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Now test the start/end with lat/lon points
# Now test the start/end with lat/lon points
@ -560,8 +587,14 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
start_point = start_point ,
start_point = start_point ,
end_point = end_point , latlon = True )
end_point = end_point , latlon = True )
try :
nt . assert_allclose ( to_np ( t2_line3 ) , ref_t2_line3 , rtol = .01 )
nt . assert_allclose ( to_np ( t2_line3 ) , ref_t2_line3 , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( t2_line3 ) - ref_t2_line3 )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Test all time steps
# Test all time steps
if multi :
if multi :
@ -571,15 +604,22 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
for t in range ( ntimes ) :
for t in range ( ntimes ) :
t2 = getvar ( wrfin , " T2 " , timeidx = t )
t2 = getvar ( wrfin , " T2 " , timeidx = t )
line = interpline ( t2 , wrfin = wrfin , timeidx = t ,
line = interpline (
start_point = start_point ,
t2 , wrfin = wrfin , timeidx = t , start_point = start_point ,
end_point = end_point , latlon = True )
end_point = end_point , latlon = True )
refname = " t2_line_t {} " . format ( t )
refname = " t2_line_t {} " . format ( t )
refline = refnc . variables [ refname ] [ : ]
refline = refnc . variables [ refname ] [ : ]
nt . assert_allclose ( to_np ( line ) ,
try :
to_np ( refline ) , rtol = .005 )
nt . assert_allclose ( to_np ( line ) , to_np ( refline ) ,
atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( line ) - refline )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
refnc . close ( )
refnc . close ( )
@ -593,8 +633,16 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
start_point = start_point ,
start_point = start_point ,
end_point = end_point , latlon = True )
end_point = end_point , latlon = True )
nt . assert_allclose ( to_np ( line ) ,
try :
to_np ( ref_t2_line4 ) , rtol = .005 )
nt . assert_allclose ( to_np ( line ) , to_np ( ref_t2_line4 ) ,
atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( line ) - ref_t2_line4 )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
finally :
refnc . close ( )
refnc . close ( )
elif ( varname == " vinterp " ) :
elif ( varname == " vinterp " ) :
@ -625,12 +673,19 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
timeidx = timeidx ,
timeidx = timeidx ,
log_p = True )
log_p = True )
tol = 5 / 100.
tol = 0 / 100.
atol = 0.0001
atol = 0.0001
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_tk_theta)))
nt . assert_allclose ( to_np ( field ) , fld_tk_theta , tol , atol )
try :
nt . assert_allclose ( to_np ( field ) , fld_tk_theta )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_tk_theta )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Tk to theta-e
# Tk to theta-e
fld_tk_theta_e = _get_refvals ( referent , " fld_tk_theta_e " , multi )
fld_tk_theta_e = _get_refvals ( referent , " fld_tk_theta_e " , multi )
@ -647,12 +702,15 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
timeidx = timeidx ,
timeidx = timeidx ,
log_p = True )
log_p = True )
tol = 3 / 100.
atol = 50.0001
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_tk_theta_e)/fld_tk_theta_e)*100)
try :
nt . assert_allclose ( to_np ( field ) , fld_tk_theta_e , tol , atol )
nt . assert_allclose ( to_np ( field ) , fld_tk_theta_e , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_tk_theta_e )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Tk to pressure
# Tk to pressure
fld_tk_pres = _get_refvals ( referent , " fld_tk_pres " , multi )
fld_tk_pres = _get_refvals ( referent , " fld_tk_pres " , multi )
@ -671,8 +729,14 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_tk_pres)))
try :
nt . assert_allclose ( to_np ( field ) , fld_tk_pres , tol , atol )
nt . assert_allclose ( to_np ( field ) , fld_tk_pres , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_tk_pres )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Tk to geoht_msl
# Tk to geoht_msl
fld_tk_ght_msl = _get_refvals ( referent , " fld_tk_ght_msl " , multi )
fld_tk_ght_msl = _get_refvals ( referent , " fld_tk_ght_msl " , multi )
@ -689,8 +753,14 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
log_p = True )
log_p = True )
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_tk_ght_msl)))
try :
nt . assert_allclose ( to_np ( field ) , fld_tk_ght_msl , tol , atol )
nt . assert_allclose ( to_np ( field ) , fld_tk_ght_msl , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_tk_ght_msl )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Tk to geoht_agl
# Tk to geoht_agl
fld_tk_ght_agl = _get_refvals ( referent , " fld_tk_ght_agl " , multi )
fld_tk_ght_agl = _get_refvals ( referent , " fld_tk_ght_agl " , multi )
@ -707,8 +777,15 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
log_p = True )
log_p = True )
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_tk_ght_agl)))
nt . assert_allclose ( to_np ( field ) , fld_tk_ght_agl , tol , atol )
try :
nt . assert_allclose ( to_np ( field ) , fld_tk_ght_agl , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_tk_ght_agl )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Hgt to pressure
# Hgt to pressure
fld_ht_pres = _get_refvals ( referent , " fld_ht_pres " , multi )
fld_ht_pres = _get_refvals ( referent , " fld_ht_pres " , multi )
@ -726,8 +803,15 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
log_p = True )
log_p = True )
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_ht_pres)))
nt . assert_allclose ( to_np ( field ) , fld_ht_pres , tol , atol )
try :
nt . assert_allclose ( to_np ( field ) , fld_ht_pres , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_ht_pres )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Pressure to theta
# Pressure to theta
fld_pres_theta = _get_refvals ( referent , " fld_pres_theta " , multi )
fld_pres_theta = _get_refvals ( referent , " fld_pres_theta " , multi )
@ -745,8 +829,15 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
log_p = True )
log_p = True )
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_pres_theta)))
nt . assert_allclose ( to_np ( field ) , fld_pres_theta , tol , atol )
try :
nt . assert_allclose ( to_np ( field ) , fld_pres_theta , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_pres_theta )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
# Theta-e to pres
# Theta-e to pres
fld_thetae_pres = _get_refvals ( referent , " fld_thetae_pres " , multi )
fld_thetae_pres = _get_refvals ( referent , " fld_thetae_pres " , multi )
@ -764,11 +855,19 @@ def make_interp_test(varname, dir, pattern, referent, multi=False,
log_p = True )
log_p = True )
field = np . squeeze ( field )
field = np . squeeze ( field )
#print (np.amax(np.abs(to_np(field) - fld_thetae_pres)))
nt . assert_allclose ( to_np ( field ) , fld_thetae_pres , tol , atol )
try :
nt . assert_allclose ( to_np ( field ) , fld_thetae_pres , atol = .0001 )
except AssertionError :
absdiff = np . abs ( to_np ( field ) - fld_thetae_pres )
maxdiff = np . amax ( absdiff )
print ( maxdiff )
print ( np . argwhere ( absdiff == maxdiff ) )
raise
return test
return test
def extract_proj_params ( wrfnc , timeidx = 0 ) :
def extract_proj_params ( wrfnc , timeidx = 0 ) :
attrs = extract_global_attrs ( wrfnc , ( " MAP_PROJ " , " TRUELAT1 " , " TRUELAT2 " ,
attrs = extract_global_attrs ( wrfnc , ( " MAP_PROJ " , " TRUELAT1 " , " TRUELAT2 " ,
" STAND_LON " , " POLE_LAT " , " POLE_LON " ,
" STAND_LON " , " POLE_LAT " , " POLE_LON " ,
@ -792,17 +891,18 @@ def extract_proj_params(wrfnc, timeidx=0):
return result
return result
def make_latlon_test ( testid , dir , pattern , referent , single ,
def make_latlon_test ( testid , dir , pattern , referent , single ,
multi = False , pynio = False ) :
multi = False , pynio = False ) :
def test ( self ) :
def test ( self ) :
try :
try :
from netCDF4 import Dataset as NetCDF
from netCDF4 import Dataset as NetCDF
except :
except ImportError :
pass
pass
try :
try :
import Nio
import Nio
except :
except ImportError :
pass
pass
timeidx = 0 if not multi else None
timeidx = 0 if not multi else None
@ -813,7 +913,7 @@ def make_latlon_test(testid, dir, pattern, referent, single,
refnc = NetCDF ( referent )
refnc = NetCDF ( referent )
try :
try :
refnc . set_always_mask ( False )
refnc . set_always_mask ( False )
except :
except AttributeError :
pass
pass
wrfin = [ ]
wrfin = [ ]
@ -822,7 +922,7 @@ def make_latlon_test(testid, dir, pattern, referent, single,
f = NetCDF ( fname )
f = NetCDF ( fname )
try :
try :
f . set_auto_mask ( False )
f . set_auto_mask ( False )
except :
except AttributeError :
pass
pass
wrfin . append ( f )
wrfin . append ( f )
else :
else :
@ -852,13 +952,11 @@ def make_latlon_test(testid, dir, pattern, referent, single,
# Next make sure the 'proj' version works
# Next make sure the 'proj' version works
projparams = extract_proj_params ( wrfin , timeidx = timeidx )
projparams = extract_proj_params ( wrfin , timeidx = timeidx )
xy_proj = ll_to_xy_proj ( lats [ 0 ] , lons [ 0 ] ,
xy_proj = ll_to_xy_proj ( lats [ 0 ] , lons [ 0 ] , as_int = True ,
as_int = True ,
* * projparams )
* * projparams )
nt . assert_allclose ( to_np ( xy_proj ) , to_np ( xy ) )
nt . assert_allclose ( to_np ( xy_proj ) , to_np ( xy ) )
else :
else :
ref_vals = refnc . variables [ " xy1 " ] [ : ]
ref_vals = refnc . variables [ " xy1 " ] [ : ]
xy = ll_to_xy ( wrfin , lats , lons , timeidx = None , as_int = False )
xy = ll_to_xy ( wrfin , lats , lons , timeidx = None , as_int = False )
@ -909,7 +1007,6 @@ def make_latlon_test(testid, dir, pattern, referent, single,
nt . assert_allclose ( to_np ( ll_proj ) , to_np ( ll ) )
nt . assert_allclose ( to_np ( ll_proj ) , to_np ( ll ) )
else :
else :
ref_vals = refnc . variables [ " ll1 " ] [ : ]
ref_vals = refnc . variables [ " ll1 " ] [ : ]
ll = xy_to_ll ( wrfin , x_s , y_s , timeidx = None )
ll = xy_to_ll ( wrfin , x_s , y_s , timeidx = None )
@ -939,12 +1036,15 @@ def make_latlon_test(testid, dir, pattern, referent, single,
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
@ -978,9 +1078,12 @@ if __name__ == "__main__":
continue
continue
test_func1 = make_test ( var , dir , PATTERN , ref_nc_file )
test_func1 = make_test ( var , dir , PATTERN , ref_nc_file )
test_func2 = make_test ( var , dir , PATTERN , ref_nc_file , multi = True )
test_func2 = make_test ( var , dir , PATTERN , ref_nc_file ,
setattr ( WRFVarsTest , ' test_ {0} _ {1} ' . format ( nest , var ) , test_func1 )
multi = True )
setattr ( WRFVarsTest , ' test_ {0} _multi_ {1} ' . format ( nest , var ) , test_func2 )
setattr ( WRFVarsTest , ' test_ {0} _ {1} ' . format ( nest , var ) ,
test_func1 )
setattr ( WRFVarsTest , ' test_ {0} _multi_ {1} ' . format ( nest , var ) ,
test_func2 )
for method in interp_methods :
for method in interp_methods :
test_interp_func1 = make_interp_test ( method , dir , PATTERN ,
test_interp_func1 = make_interp_test ( method , dir , PATTERN ,
@ -989,7 +1092,8 @@ if __name__ == "__main__":
ref_nc_file , multi = True )
ref_nc_file , multi = True )
setattr ( WRFInterpTest , ' test_ {0} _ {1} ' . format ( nest , method ) ,
setattr ( WRFInterpTest , ' test_ {0} _ {1} ' . format ( nest , method ) ,
test_interp_func1 )
test_interp_func1 )
setattr ( WRFInterpTest , ' test_ {0} _multi_ {1} ' . format ( nest , method ) ,
setattr ( WRFInterpTest ,
' test_ {0} _multi_ {1} ' . format ( nest , method ) ,
test_interp_func2 )
test_interp_func2 )
for testid in latlon_tests :
for testid in latlon_tests :
@ -1002,7 +1106,8 @@ if __name__ == "__main__":
pynio = False )
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 ( nest , testid , singlestr ,
test_name = " test_ {} _ {} {} {} " . format ( nest , testid ,
singlestr ,
multistr )
multistr )
setattr ( WRFLatLonTest , test_name , test_ll_func )
setattr ( WRFLatLonTest , test_name , test_ll_func )
@ -1015,22 +1120,26 @@ if __name__ == "__main__":
if var in ignore_vars :
if var in ignore_vars :
continue
continue
test_func1 = make_test ( var , dir , PATTERN , ref_nc_file , pynio = True )
test_func1 = make_test ( var , dir , PATTERN , ref_nc_file ,
test_func2 = make_test ( var , dir , PATTERN , ref_nc_file , multi = True ,
pynio = True )
pynio = True )
setattr ( WRFVarsTest , ' test_pynio_ {0} _ {1} ' . format ( nest , var ) , test_func1 )
test_func2 = make_test ( var , dir , PATTERN , ref_nc_file ,
setattr ( WRFVarsTest , ' test_pynio_ {0} _multi_ {1} ' . format ( nest , var ) ,
multi = True ,
test_func2 )
pynio = True )
setattr ( WRFVarsTest , ' test_pynio_ {0} _ {1} ' . format (
nest , var ) , test_func1 )
setattr ( WRFVarsTest , ' test_pynio_ {0} _multi_ {1} ' . format (
nest , var ) , test_func2 )
for method in interp_methods :
for method in interp_methods :
test_interp_func1 = make_interp_test ( method , dir , PATTERN ,
test_interp_func1 = make_interp_test ( method , dir , PATTERN ,
ref_nc_file )
ref_nc_file )
test_interp_func2 = make_interp_test ( method , dir , PATTERN ,
test_interp_func2 = make_interp_test ( method , dir , PATTERN ,
ref_nc_file , multi = True )
ref_nc_file , multi = True )
setattr ( WRFInterpTest , ' test_pynio_ {0} _ {1} ' . format ( nest , method ) ,
setattr ( WRFInterpTest , ' test_pynio_ {0} _ {1} ' . format ( nest ,
method ) ,
test_interp_func1 )
test_interp_func1 )
setattr ( WRFInterpTest , ' test_pynio_ {0} _multi_ {1} ' . format ( nest , method ) ,
setattr ( WRFInterpTest , ' test_pynio_ {0} _multi_ {1} ' . format (
test_interp_func2 )
nest , method ) , test_interp_func2 )
for testid in latlon_tests :
for testid in latlon_tests :
for single in ( True , False ) :
for single in ( True , False ) :
@ -1042,10 +1151,8 @@ if __name__ == "__main__":
pynio = False )
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_pynio_ {} _ {} {} {} " . format ( nest , testid ,
test_name = " test_pynio_ {} _ {} {} {} " . format (
singlestr ,
nest , testid , singlestr , multistr )
multistr )
setattr ( WRFLatLonTest , test_name , test_ll_func )
setattr ( WRFLatLonTest , test_name , test_ll_func )
ut . main ( )
ut . main ( )