From d6360515e3be8b69178baae702fc19ca67ecb5aa Mon Sep 17 00:00:00 2001 From: Uriel Ramirez Date: Mon, 16 Sep 2024 14:58:46 -0400 Subject: [PATCH] finish setting up the tests --- configure.ac | 8 +- postprocessing/timavg/time_average.f90 | 2 + t/Makefile.am | 7 ++ t/Test34-timavg.sh | 21 +++-- t/test_time_avg.py | 115 +++++++++++++++++-------- 5 files changed, 107 insertions(+), 46 deletions(-) diff --git a/configure.ac b/configure.ac index 53ae6fb2..06a71ebb 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/postprocessing/timavg/time_average.f90 b/postprocessing/timavg/time_average.f90 index 0ee5afa1..e444ef60 100644 --- a/postprocessing/timavg/time_average.f90 +++ b/postprocessing/timavg/time_average.f90 @@ -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 & diff --git a/t/Makefile.am b/t/Makefile.am index ea1da269..cf51ae9d 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -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; diff --git a/t/Test34-timavg.sh b/t/Test34-timavg.sh index e5d4828c..b8f28298 100755 --- a/t/Test34-timavg.sh +++ b/t/Test34-timavg.sh @@ -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 } diff --git a/t/test_time_avg.py b/t/test_time_avg.py index 7b39a9f0..35fc6d26 100644 --- a/t/test_time_avg.py +++ b/t/test_time_avg.py @@ -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()