From 129c504342dd89f00035ea301ea3daa9023f7d13 Mon Sep 17 00:00:00 2001
From: thopri <thopri@noc.ac.uk>
Date: Wed, 18 Mar 2020 11:32:18 +0000
Subject: [PATCH] unit test script now generating data and testing constant
 temp

---
 unit_tests/gen_tools.py           |  8 +++----
 unit_tests/namelist_unit_test.bdy |  4 ++--
 unit_tests/test_gen.py            | 27 +++++++++++++----------
 unit_tests/unit_test.py           | 36 ++++++++++++++++++++++---------
 4 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/unit_tests/gen_tools.py b/unit_tests/gen_tools.py
index 38ba686..b7ed9b9 100644
--- a/unit_tests/gen_tools.py
+++ b/unit_tests/gen_tools.py
@@ -518,8 +518,8 @@ def write_mask(fileout,grid_h,grid_z):
     nav_lat[:, :] = grid_h['latt'].T
     nav_lev[:] = grid_z['dept_1d']
 
-    threeD_mask = np.zeros(np.shape(grid_z['e3t']))
-    twoD_mask = np.zeros(np.shape(grid_h['e1t']))
+    threeD_mask = np.ones(np.shape(grid_z['e3t']))
+    twoD_mask = np.ones(np.shape(grid_h['e1t']))
 
     fmask[:,:,:] = threeD_mask.T
     fmaskutil[:,:] = twoD_mask.T
@@ -550,7 +550,7 @@ def write_parameter(fileout, grid_h,grid_z,params):
 
     # Get input size and create appropriate dimensions
     # TODO: add some sort of error handling
-    nt = 2
+    nt = 31
     nx, ny, nz = np.shape(grid_z['e3t'])
     dataset.createDimension('x', nx)
     dataset.createDimension('y', ny)
@@ -576,7 +576,7 @@ def write_parameter(fileout, grid_h,grid_z,params):
     longitude[:, :] = grid_h['lont'].T
     latitude[:, :] = grid_h['latt'].T
     depth[:] = grid_z['dept_1d']
-    time_counter[:] = (587340.00,588060.00)
+    time_counter[:] = np.linspace(587340.00,588060.00,31)
     parameter = dataset.createVariable(str(params['name']), np.float64, ('time','z', 'y', 'x'))
     parameter.units, parameter.long_name = str(params['units']), str(params['longname'])
     value_fill = np.ones(np.shape(grid_z['e3t']))
diff --git a/unit_tests/namelist_unit_test.bdy b/unit_tests/namelist_unit_test.bdy
index 68539ce..d07f1db 100644
--- a/unit_tests/namelist_unit_test.bdy
+++ b/unit_tests/namelist_unit_test.bdy
@@ -35,7 +35,7 @@
    sn_dst_hgr = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/test_dst_hgr_zps.nc'
    sn_dst_zgr = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/test_dst_zgr_zps.nc'
    sn_src_msk = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/mask.nc'
-   sn_bathy   = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/dst_bathy.nc'
+   sn_bathy   = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/test_dst_bathy.nc'
 
 !------------------------------------------------------------------------------
 !  I/O 
@@ -45,7 +45,7 @@
    sn_fn      = 'unit_test'             ! prefix for output files
    nn_fv      = -1e20                 !  set fill value for output files
    nn_src_time_adj = 0                ! src time adjustment
-   sn_dst_metainfo = 'Benchmarking Data'
+   sn_dst_metainfo = 'Unit Test Data'
 
 !------------------------------------------------------------------------------
 !  CMEMS Data Source Configuration
diff --git a/unit_tests/test_gen.py b/unit_tests/test_gen.py
index 15ec96e..8e8cd19 100644
--- a/unit_tests/test_gen.py
+++ b/unit_tests/test_gen.py
@@ -48,7 +48,10 @@ def _main():
     grid_z2 = gt.set_zgrid(grid_h2,jpk,max_dep,min_dep,z_end_dim)
     write_coord_H = gt.write_coord_H(h_fname,grid_h2)
     write_coord_Z = gt.write_coord_Z(z_fname,grid_h2,grid_z2)
-    if write_coord_H + write_coord_Z == 0:
+    # write bathy files (constant bathy)
+    bathy_fname = 'unit_tests/test_data/test_dst_bathy.nc'
+    bathy = gt.write_bathy(bathy_fname,grid_h2,grid_z2)
+    if write_coord_H + write_coord_Z + bathy == 0:
         print("Success!")
 
     # set rotation and origin point
@@ -66,7 +69,10 @@ def _main():
     grid_rot['latf'], grid_rot['lonf'] = gt.rotate_around_point(grid_h2['latf'], grid_h2['lonf'], theta, origin)
     write_coord_H = gt.write_coord_H(rot_h_fname,grid_rot)
     write_coord_Z = gt.write_coord_Z(rot_z_fname,grid_rot,grid_z2)
-    if write_coord_H + write_coord_Z == 0:
+    # write bathy files (constant bathy)
+    bathy_fname = 'unit_tests/test_data/test_rot_dst_bathy.nc'
+    bathy = gt.write_bathy(bathy_fname,grid_rot,grid_z2)
+    if write_coord_H + write_coord_Z + bathy == 0:
         print("Success!")
 
     # offset grid
@@ -81,25 +87,22 @@ def _main():
     min_dep = 10
     z_end_dim = 1
     sf = 10
-    h_fname = 'unit_tests/test_data/test_dst_offset_hgr_zps.nc'
-    z_fname = 'unit_tests/test_data/test_dst_offset_zgr_zps.nc'
+    h_fname = 'unit_tests/test_data/test_offset_dst_hgr_zps.nc'
+    z_fname = 'unit_tests/test_data/test_offset_dst_zgr_zps.nc'
     grid_h3 = gt.set_hgrid(dx,dy,jpi,jpj,zoffx,zoffy,sf)
     grid_z3 = gt.set_zgrid(grid_h2,jpk,max_dep,min_dep,z_end_dim)
     write_coord_H = gt.write_coord_H(h_fname,grid_h3)
     write_coord_Z = gt.write_coord_Z(z_fname,grid_h3,grid_z3)
-    if write_coord_H + write_coord_Z == 0:
+    # write bathy files (constant bathy)
+    bathy_fname = 'unit_tests/test_data/test_offset_dst_bathy.nc'
+    bathy = gt.write_bathy(bathy_fname,grid_h3,grid_z3)
+    if write_coord_H + write_coord_Z + bathy == 0:
         print("Success!")
 
     # plot orginal, rotatated and source lat and lon
     gt.plot_grids(grid_h2['latt'],grid_h2['lont'],grid_rot['latt'],grid_rot['lont'],grid_h3['latt'], \
                grid_h3['lont'],grid_h1['latt'],grid_h1['lont'])
 
-    # write bathy files (constant bathy)
-    bathy_fname = 'unit_tests/test_data/dst_bathy.nc'
-    bathy = gt.write_bathy(bathy_fname,grid_h2,grid_z2)
-    if bathy == 0:
-        print('Success!')
-
     # write boundary files (constant parameters)
     out_fname = 'unit_tests/test_data/output_boundary_T.nc'
     params = {'name':'thetao','const_value':15.0,'longname':'temperature','units':'degreesC'}
@@ -113,5 +116,7 @@ def _main():
     if mask == 0:
         print('Success!')
 
+    return 0
+
 if __name__ == '__main__':
     _main()
\ No newline at end of file
diff --git a/unit_tests/unit_test.py b/unit_tests/unit_test.py
index bbfe4a0..c5d6784 100644
--- a/unit_tests/unit_test.py
+++ b/unit_tests/unit_test.py
@@ -9,11 +9,19 @@ import numpy as np
 import glob
 import os
 
-def test_run():
-    stdout, stderr = Popen(['pynemo','-s','unit_tests/namelist_unit_test.bdy'], stdout=PIPE, stderr=PIPE,
-                           universal_newlines=True).communicate()
-    assert 'Execution Time' in stdout
+# generate test data by import test gen script and executing main function
+import unit_tests.test_gen as tg
+gen_data = tg._main()
+if gen_data != 0:
+    raise Exception('DONT PANIC: Input data generation failed')
 
+# run PyNEMO with test data
+stdout, stderr = Popen(['pynemo', '-s', 'unit_tests/namelist_unit_test.bdy'], stdout=PIPE, stderr=PIPE,
+                       universal_newlines=True).communicate()
+if 'Execution Time' not in stdout:
+    raise Exception('DONT PANIC: Test Run Failed')
+
+# perform tests
 def test_temp():
     test_files = glob.glob('unit_tests/test_outputs/unit_test*')
     for t in test_files:
@@ -36,10 +44,18 @@ def test_temp():
 #        assert abs(sal_[sal_ != 0.0].max() - 35) <= 0.001
 #        assert abs(sal_[sal_ != 0.0].min() - 35) <= 0.001
 
-def test_rm_output():
-    files = glob.glob('unit_tests/test_outputs/*')
-    for f in files:
-        os.remove(f)
-    files = glob.glob('unit_tests/test_outputs/*')
-    assert len(files) == 0
+# clean up test I/O
+files = glob.glob('unit_tests/test_outputs/*')
+for f in files:
+    os.remove(f)
+files = glob.glob('unit_tests/test_outputs/*')
+if len(files) != 0:
+    raise Exception('DONT PANIC: output folder not cleaned')
+
+files = glob.glob('unit_tests/test_data/*')
+for f in files:
+    os.remove(f)
+files = glob.glob('unit_tests/test_data/*')
+if len(files) != 0:
+    raise Exception('DONT PANIC: input folder not cleaned')
 
-- 
GitLab