Home > tests > fvcom_prepro > test_write_FVCOM_tsobc.m

test_write_FVCOM_tsobc

PURPOSE ^

Unit test for write_FVCOM_tsobc.

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 Unit test for write_FVCOM_tsobc.

 DESCRIPTION:
   Currently checks against a reference data set for the following:
       - number of nodes in the output
       - number of sigma layers in the output
       - number of time steps in the output
       - range of values in the node arrays

 It uses synthetic data on an automatically generated grid and then reads
 in the results and compares them against the inputs. Warning are issued
 for non-identical results.

 Author(s):
   Pierre Cazenave (Plymouth Marine Laboratory)

 Revision history:
   2013-06-02 First version.

==========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % Unit test for write_FVCOM_tsobc.
0002 %
0003 % DESCRIPTION:
0004 %   Currently checks against a reference data set for the following:
0005 %       - number of nodes in the output
0006 %       - number of sigma layers in the output
0007 %       - number of time steps in the output
0008 %       - range of values in the node arrays
0009 %
0010 % It uses synthetic data on an automatically generated grid and then reads
0011 % in the results and compares them against the inputs. Warning are issued
0012 % for non-identical results.
0013 %
0014 % Author(s):
0015 %   Pierre Cazenave (Plymouth Marine Laboratory)
0016 %
0017 % Revision history:
0018 %   2013-06-02 First version.
0019 %
0020 %==========================================================================
0021 
0022 matlabrc
0023 close all
0024 clc
0025 
0026 % Set up our test environment.
0027 [base, subname] = fileparts(mfilename('fullpath'));
0028 cd(base)
0029 
0030 addpath(fullfile(base, '../../fvcom_prepro'))
0031 addpath(fullfile(base, '../../utilities'))
0032 
0033 %% Make some synthetic data for the test to compare the results.
0034 
0035 % A boring grid.
0036 nt = 30; % about a month of daily data
0037 nx = 1024; % initial guess for the number of nodes
0038 nz = 21; % vertical levels (= layers + 1)
0039 [Mobj.lon, Mobj.lat] = meshgrid(1:ceil(sqrt(nx)), 1:ceil(sqrt(nx)));
0040 % Offset each row by half a grid width.
0041 Mobj.lon(1:2:end) = Mobj.lon(1:2:end) - max(max(diff(Mobj.lon, [], 2) / 2));
0042 % Add the missing corners.
0043 Mobj.lon = [Mobj.lon; min(Mobj.lon); max(Mobj.lon)];
0044 Mobj.lat = [Mobj.lat; max(Mobj.lat); min(Mobj.lat)];
0045 Mobj.lon = Mobj.lon(:) + 10;
0046 Mobj.lat = Mobj.lat(:) + 50;
0047 dt = delaunayTriangulation(Mobj.lon, Mobj.lat);
0048 Mobj.tri = dt.ConnectivityList;
0049 % Update the node count.
0050 nx = length(Mobj.lon);
0051 Mobj.h = randn(nx, 1) * 50;
0052 % Set the "west" and "east" nodes as the open boundaries.
0053 nodes = 1:nx;
0054 Mobj.nObs = 2;
0055 Mobj.read_obc_nodes{1} = nodes(Mobj.lon == min(Mobj.lon));
0056 Mobj.read_obc_nodes{2} = nodes(Mobj.lon == max(Mobj.lon));
0057 
0058 % Number of open boundary nodes.
0059 nobn = length([Mobj.read_obc_nodes{:}]);
0060 
0061 % Have a look see.
0062 % clf
0063 % trimesh(Mobj.tri, Mobj.lon, Mobj.lat, 'color', 'k')
0064 % hold on
0065 % plot(Mobj.lon([Mobj.read_obc_nodes{:}]), Mobj.lat([Mobj.read_obc_nodes{:}]), 'r.')
0066 
0067 % Make the vertical grid (nice and simple at first).
0068 Mobj.siglev = repmat(sigma_geo(nz, 1), [nx, 1]);
0069 Mobj.siglay = nan(nx, nz - 1);
0070 for i = 1:nz - 1
0071     Mobj.siglay(:, i) = mean(Mobj.siglev(:, i:i+1), 2);
0072 end
0073 
0074 % Some times.
0075 Mobj.ts_times = linspace(51400, 51400 + nt - 1, nt);
0076 
0077 % Data to write to file. Warm and fresh on top, cold and salty below.
0078 in_temp = repmat(15, nobn, nz - 1, nt);
0079 in_temp(:, 1, :) = 18;
0080 in_salt = repmat(35, nobn, nz - 1, nt);
0081 in_salt(:, 1, :) = 33;
0082 
0083 write_FVCOM_tsobc(subname, Mobj.ts_times, nz - 1, in_temp, in_salt, Mobj)
0084 
0085 %% Read in the written and compare against the inputs.
0086 
0087 outvars = {'obc_h', 'obc_temp', 'obc_salinity', 'siglev', 'siglay', 'obc_nodes'};
0088 obc_nodes = [Mobj.read_obc_nodes{:}];
0089 inpvars = {Mobj.h(obc_nodes), in_temp, in_salt, ...
0090     Mobj.siglev(obc_nodes, :), ...
0091     Mobj.siglay(obc_nodes, :), ...
0092     obc_nodes};
0093 assert(length(outvars) == length(inpvars), 'Inconsistent length of variables to compare.')
0094 for v = 1:length(outvars)
0095     dump = double(ncread(sprintf('%s_tsobc.nc', subname), outvars{v}));
0096     % Try transposing vectors, everything else needs to be the same shape.
0097     if isvector(dump)
0098         try
0099             ncdiffs = dump - inpvars{v};
0100         catch
0101             ncdiffs = dump - inpvars{v}';
0102         end
0103     else
0104         ncdiffs = dump - inpvars{v};
0105     end
0106     if max(ncdiffs(:)) ~= 0
0107         warning('Input and output of ''%s'' differ by at most %g. Test FAILED.', outvars{v}, max(ncdiffs(:)))
0108     end
0109 end

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005