From 1d094960165eecd86253f70109f2204cdeb7920e Mon Sep 17 00:00:00 2001
From: thopri <thopri@144-148.noc.soton.ac.uk>
Date: Fri, 13 Mar 2020 16:42:43 +0000
Subject: [PATCH] updated to build constant boundary file

---
 unit_tests/gen_tools.py | 46 ++++++++++++++++++++++++++++++++++++++++-
 unit_tests/test_gen.py  |  5 +++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/unit_tests/gen_tools.py b/unit_tests/gen_tools.py
index ea41ff9..80e1abe 100644
--- a/unit_tests/gen_tools.py
+++ b/unit_tests/gen_tools.py
@@ -449,4 +449,48 @@ def plot_grids(lat_in,lon_in,new_lat,new_lon,off_lat,off_lon,src_lat,src_lon):
     # tweak margins of subplots as tight layout doesn't work
     plt.subplots_adjust(left=0.01, right=1, top=0.9, bottom=0.05,wspace=0.01)
 
-    plt.show()
\ No newline at end of file
+    plt.show()
+
+def write_parameter(fileout, grid_h,grid_z,params):
+    '''
+    Writes out a
+
+    Args:
+
+    Returns:
+    '''
+
+    #TODO: implement multiple parameters using dicts.
+
+    # Open pointer to netcdf file
+    dataset = Dataset(fileout, 'w', format='NETCDF4_CLASSIC')
+
+    # Get input size and create appropriate dimensions
+    # TODO: add some sort of error handling
+    nx, ny, nz = np.shape(grid_z['e3t'])
+    dataset.createDimension('x', nx)
+    dataset.createDimension('y', ny)
+    dataset.createDimension('z', nz)
+
+    # Create Variables
+    longitude = dataset.createVariable('longitude', np.float32, ('y', 'x'))
+    latitude = dataset.createVariable('latitude', np.float32, ('y', 'x'))
+    depth = dataset.createVariable('depth', np.float32, 'z')
+    longitude.units, longitude.long_name = 'km', 'X'
+    latitude.units, latitude.long_name = 'km', 'Y'
+    depth.units, depth.long_name = 'm', 'Z'
+    # Populate file with input data
+    longitude[:, :] = grid_h['lont'].T
+    latitude[:, :] = grid_h['latt'].T
+    depth[:] = grid_z['dept_1d']
+
+    parameter = dataset.createVariable(str(params['name']), np.float64, ('z', 'y', 'x'))
+    parameter.units, parameter.long_name = str(params['units']), str(params['longname'])
+    value_fill = np.ones(np.shape(grid_z['e3t']))
+    value_fill = value_fill*params['const_value']
+    parameter[:, :, :] = value_fill.T
+
+    # Close off pointer
+    dataset.close()
+
+    return 0
\ No newline at end of file
diff --git a/unit_tests/test_gen.py b/unit_tests/test_gen.py
index 9582100..95c2984 100644
--- a/unit_tests/test_gen.py
+++ b/unit_tests/test_gen.py
@@ -101,6 +101,11 @@ def _main():
     # close data files
     #ds.close()
     #src.close()
+    out_fname = 'unit_tests/test_data/output_boundary.nc'
+    params = {'name':'thetao','const_value':15.0,'longname':'temperature','units':'degreesC'}
+    boundary = gt.write_parameter(out_fname,grid_h1,grid_z1,params)
+    if boundary == 0:
+        print('Success!')
 
 if __name__ == '__main__':
     _main()
\ No newline at end of file
-- 
GitLab