Skip to content

Commit

Permalink
attempt to add a test for frenctools
Browse files Browse the repository at this point in the history
  • Loading branch information
uramirez8707 committed Sep 11, 2024
1 parent dcbfc10 commit 38b1209
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
with_mpi: ['','--with-mpi']
enable_quad_precision: ['', '--enable-quad-precision']
container:
image: noaagfdl/fre-nctools-base:2.0.0-focal
image: ghcr.io/noaa-gfdl/fre-nctools-ci-rocky-gnu:14.2.0
env:
MPI: ${{ matrix.with_mpi }}
QUAD_P: ${{ matrix.enable_quad_precision }}
Expand Down
3 changes: 2 additions & 1 deletion t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ TESTS = Test01-check_programs_exist.sh \
Test29-make_vgrid.sh \
Test31-fregrid_stretched.sh \
Test32-fregrid_no_stretched.sh \
Test33-reference_make_hgrid.sh
Test33-reference_make_hgrid.sh \
Test34-timavg.sh

EXTRA_DIST = $(TESTS) \
Test02-input\
Expand Down
27 changes: 27 additions & 0 deletions t/Test34-timavg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/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" {
python $top_srcdir/t/test_time_avg.py
}
51 changes: 51 additions & 0 deletions t/test_time_avg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3

import xarray as xr
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 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"}
ds1 = xr.Dataset(
data_vars={
## This is backwards #FORTRAN4LYFE
"a": (("Time", "y", "x"), a),
"Time": (("Time", np.arange(nt)*1.0+1, Time_attrs))
},
coords={
"x": np.arange(nx)*1.0,
"y": np.arange(ny)*1.0,
},
)
ds1.to_netcdf("20120101.ice_shelf.nc", unlimited_dims="Time")


def check_output(self):
out_file = xr.open_dataset("wut.nc")
if not (out_file.a.values == np.average(self.a, axis=0)).all():
raise Exception("Test failed")


test_class = TestTimeAvg()
test_class.create_input()
os.system(os.path.dirname(os.path.realpath(__file__))+"/../postprocessing/timavg/timavg.csh -m -o wut.nc 20120101.ice_shelf.nc")
test_class.check_output()

0 comments on commit 38b1209

Please sign in to comment.