% This routine uses the results of "LagEst" routine to compute optimum lags for the water
% parameter sensor in question.
%
% J. G. Bellingham

% Compute errors between up yoyo and interpolated down yoyos

bnd_deriv = (zlag(1:2:(n-3), :) - zlag(3:2:(n-1), :)) ./ (tlag(1:2:(n-3), :) - tlag(3:2:(n-1), :));
z_up_interp = (tlag(2:2:(n-2),:) - tlag(1:2:(n-3), :)) .* bnd_deriv + zlag(1:2:(n-3), :);

z_err = zlag(2:2:(n-2),:) - z_up_interp;

temp = z_err;
temp(find(isnan(z_err))) = 0;
temp(find(~isnan(z_err))) = 1;
z_err(find(isnan(z_err))) = 0;

err = sum(z_err) ./ sum(temp)
% 