-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from uramirez8707/add_test
Fix time_average for instantaneous output
- Loading branch information
Showing
10 changed files
with
240 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# =========================================================================== | ||
# http://www.gnu.org/software/autoconf-archive/ax_python_module.html | ||
# =========================================================================== | ||
# | ||
# SYNOPSIS | ||
# | ||
# AX_PYTHON_MODULE(modname[, fatal, python]) | ||
# | ||
# DESCRIPTION | ||
# | ||
# Checks for Python module. | ||
# | ||
# If fatal is non-empty then absence of a module will trigger an error. | ||
# The third parameter can either be "python" for Python 2 or "python3" for | ||
# Python 3; defaults to Python 2. | ||
# | ||
# LICENSE | ||
# | ||
# Copyright (c) 2008 Andrew Collier | ||
# | ||
# Copying and distribution of this file, with or without modification, are | ||
# permitted in any medium without royalty provided the copyright notice | ||
# and this notice are preserved. This file is offered as-is, without any | ||
# warranty. | ||
|
||
#serial 8 | ||
|
||
AU_ALIAS([AC_PYTHON_MODULE], [AX_PYTHON_MODULE]) | ||
AC_DEFUN([AX_PYTHON_MODULE],[ | ||
if test -z "$3"; | ||
then | ||
PYTHON_TEST="python2" | ||
else | ||
PYTHON_TEST="$3" | ||
fi | ||
PYTHON_NAME=`basename $PYTHON_TEST` | ||
AC_MSG_CHECKING($PYTHON_NAME module: $1) | ||
$PYTHON_TEST -c "import $1" 2>/dev/null | ||
if test $? -eq 0; | ||
then | ||
AC_MSG_RESULT(yes) | ||
eval AS_TR_CPP(HAVE_PYMOD_$1)=yes | ||
else | ||
AC_MSG_RESULT(no) | ||
eval AS_TR_CPP(HAVE_PYMOD_$1)=no | ||
# | ||
if test -n "$2" | ||
then | ||
AC_MSG_ERROR(failed to find required module $1) | ||
exit 1 | ||
fi | ||
fi | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env bats | ||
|
||
#*********************************************************************** | ||
# GNU Lesser General Public License | ||
# | ||
# This file is part of the GFDL FRE NetCDF tools package (FRE-NCTools). | ||
# | ||
# FRE-NCTools is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or (at | ||
# your option) any later version. | ||
# | ||
# FRE-NCTools is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
# for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with FRE-NCTools. If not, see | ||
# <http://www.gnu.org/licenses/>. | ||
#*********************************************************************** | ||
|
||
load test_utils | ||
|
||
@test "Test timavg" { | ||
if [ -z "$skipflag" ]; then | ||
cat <<_EOF > instantaneous.nml | ||
&input | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import xarray as xr | ||
import numpy as np | ||
import os | ||
|
||
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" | ||
|
||
|
||
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"), self.a), | ||
"Time": (("Time", self.time_data, Time_attrs)), | ||
"time_bnds": (("Time", "nv"), self.time_bnds_data) | ||
}, | ||
coords={ | ||
"x": np.arange(self.nx)*1.0, | ||
"y": np.arange(self.ny)*1.0, | ||
"nv": np.arange(2)*1.0+1, | ||
}, | ||
) | ||
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(self.Out_file) | ||
if not (out_file.a.values == np.average(self.a, axis=0)).all(): | ||
raise Exception(self.Error_msg + ":: answers were not the expected result!") | ||
|
||
|
||
# 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() | ||
|
||
# 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() |