Skip to content

Commit

Permalink
finish setting up the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uramirez8707 committed Sep 16, 2024
1 parent 621696c commit d636051
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 46 deletions.
8 changes: 6 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ AC_CHECK_PROGS(BATS, [bats])
AC_CHECK_PROGS(PROVE, [prove])
AM_CONDITIONAL([WITH_CHECK_PROGS], [test -n "$PROVE" -a -n "$BATS"])

AC_CHECK_PROGS(python3, [required])
AC_PYTHON_MODULE(xarray, required, python3)
AC_PYTHON_MODULE(xarray, [], python3)
if test ${HAVE_PYMOD_XARRAY} = yes; then
AM_CONDITIONAL([SKIP_XARRAY_TESTS], true )
else
AM_CONDITIONAL([SKIP_XARRAY_TESTS], false )
fi

# Check if openacc.h exists
if test "$enable_acc" = yes ; then
Expand Down
2 changes: 2 additions & 0 deletions postprocessing/timavg/time_average.f90
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ program time_average
if (itime > 0) then
istat = NF90_GET_VAR (ncid_in, varid_recdim, time, start=(/itime/))
if (istat /= NF90_NOERR) call error_handler ('getting time coord value', ncode=istat)
endif

if (do_avg .and. itime > 0) then
!--- read time bnds_info
istat = NF90_GET_VAR (ncid_in, time_bnds_id, tavg(1:2), (/1, itime/))
if (istat /= NF90_NOERR) call error_handler &
Expand Down
7 changes: 7 additions & 0 deletions t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ else
skip_MPI="skip"
endif

if SKIP_XARRAY_TESTS
skipflag="skip"
else
skipflag=""
endif

TESTS_ENVIRONMENT = top_srcdir=$(abs_top_srcdir); export top_srcdir;\
export skip_mpi=$(skip_MPI); \
export skip_xarray=$(skipflag); \
for d in $$( find $(abs_top_builddir)/postprocessing $(abs_top_builddir)/tools -mindepth 1 -not -path '*/\.*' -type d ); do \
PATH="$$d"'$(PATH_SEPARATOR)'"$$PATH"; \
done;
Expand Down
21 changes: 13 additions & 8 deletions t/Test34-timavg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
load test_utils

@test "Test timavg" {
cat <<_EOF > input.nml
if [ -z "$skip_mpi" ]; then
cat <<_EOF > instantaneous.nml
&input
file_names(1) = '20120101.ice_shelf.nc' ,
file_name_out = 'new.nc' ,
use_end_time = .false. ,
verbose = .false. ,
add_cell_methods = .false. ,
skip_tavg_errors = .false. ,
suppress_warnings = .false.,
file_names(1) = 'timavg_instantaneous_in.nc' ,
file_name_out = 'timavg_instantaneous_out.nc' ,
&end
_EOF

cat <<_EOF > standard.nml
&input
file_names(1) = 'timavg_standard_in.nc' ,
file_name_out = 'timavg_standard_out.nc' ,
&end
_EOF

python3 $top_srcdir/t/test_time_avg.py
fi
}
115 changes: 79 additions & 36 deletions t/test_time_avg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,98 @@
import numpy as np
import os

def init_fake_data(a, count=0., factor=1.):
with np.nditer(a, op_flags=['readwrite']) as it:
for x in it:
count = count + 1
x[...] = count * factor
class TestTimeAvg:
def __init__(self, Test_case):
self.Test_case = Test_case
if self.Test_case == 1:
self.In_file = "timavg_instantaneous_in.nc"
self.Out_file = "timavg_instantaneous_out.nc"
self.Namelist_file = "instantaneous.nml"
self.Error_msg = "timavg with instantaneous input"
elif self.Test_case == 2:
self.In_file = "timavg_standard_in.nc"
self.Out_file = "timavg_standard_out.nc"
self.Namelist_file = "standard.nml"
self.Error_msg = "timavg with standard input"

class TestTimeAvg():
def create_input(self):
print("Creating input files to test with")
nx = 96
ny = 96
nt = 12

a = np.zeros([nt, ny, nx])
for k in range(0,nt):
for j in range(0,ny):
for i in range(0,nx):
a[k, j, i] = (i+1.)*1000 + (j+1.)*10 + (k+1.)/100.
self.a = a

Time_attrs = {"units": "days since 2000-01-01", "axis": "T", "calendar_type": "JULIAN", "calendar": "julian"}

def create_instantaneous(self):
Time_attrs = {"units": "days since 2000-01-01", "axis": "T", "calendar_type": "JULIAN", "calendar": "julian"}
ds1 = xr.Dataset(
data_vars={
## This is backwards #FORTRAN4LYFE
"a": (("Time", "y", "x"), self.a),
"Time": (("Time", self.time_data, Time_attrs))
},
coords={
"x": np.arange(self.nx)*1.0,
"y": np.arange(self.ny)*1.0,
},
)
ds1.to_netcdf(self.In_file, unlimited_dims="Time")

def create_standard(self):
Time_attrs = {"units": "days since 2000-01-01", "axis": "T", "calendar_type": "JULIAN", "calendar": "julian",
"bounds": "time_bnds"}
ds1 = xr.Dataset(
data_vars={
## This is backwards #FORTRAN4LYFE
"a": (("Time", "y", "x"), a),
"Time": (("Time", np.arange(nt)*1.0+1, Time_attrs))
"a": (("Time", "y", "x"), self.a),
"Time": (("Time", self.time_data, Time_attrs)),
"time_bnds": (("Time", "nv"), self.time_bnds_data)
},
coords={
"x": np.arange(nx)*1.0,
"y": np.arange(ny)*1.0,
"x": np.arange(self.nx)*1.0,
"y": np.arange(self.ny)*1.0,
"nv": np.arange(2)*1.0+1,
},
)
ds1.to_netcdf("20120101.ice_shelf.nc", unlimited_dims="Time")
ds1.to_netcdf(self.In_file, unlimited_dims="Time")


def create_input(self):
self.nx = 96
self.ny = 96
self.nt = 12
self.time_data = np.arange(self.nt)*1.0+1

self.a = np.zeros([self.nt, self.ny, self.nx])
for k in range(0,self.nt):
for j in range(0,self.ny):
for i in range(0,self.nx):
self.a[k, j, i] = (i+1.)*1000 + (j+1.)*10 + (k+1.)/100.

if self.Test_case == 1:
self.create_instantaneous()
elif self.Test_case == 2:
self.time_bnds_data = np.zeros([self.nt, 2])
self.time_bnds_data[:,1] = self.time_data[0:]
self.time_bnds_data[1:,0] = self.time_data[0:-1]
self.create_standard()


def run_test(self):
exit_code = os.system("TAVG.exe < " + self.Namelist_file)
if exit_code != 0 :
raise Exception(self.Error_msg + ":: Failed to complete")


def check_output(self):
out_file = xr.open_dataset("new.nc")
out_file = xr.open_dataset(self.Out_file)
if not (out_file.a.values == np.average(self.a, axis=0)).all():
raise Exception("Test failed")
raise Exception(self.Error_msg + ":: answers were not the expected result!")


test_class = TestTimeAvg()
# Test time_average when the input is instantaneous (so no time bounds)
Test_Instantaneous = 1
test_class = TestTimeAvg(Test_Instantaneous)
test_class.create_input()
test_class.run_test()
test_class.check_output()

exit_code = os.system("TAVG.exe < input.nml")
if exit_code != 0 :
raise Exception("Failed to run timavg")

try:
test_class.check_output()
except:
raise Exception("Failed to check the timavg output")
# Test time_average when the input is standard (with time_bounds, no average_* variables)
Test_Standard = 2
test_class = TestTimeAvg(Test_Standard)
test_class.create_input()
test_class.run_test()
test_class.check_output()

0 comments on commit d636051

Please sign in to comment.