correctThermalLag

PURPOSE ^

CORRECTTHERMALLAG Correct CTD conductivity and temperature sequence from thermal lag effects.

SYNOPSIS ^

function [temp_inside, cond_outside] = correctThermalLag(varargin)

DESCRIPTION ^

CORRECTTHERMALLAG  Correct CTD conductivity and temperature sequence from thermal lag effects.

  Syntax:
    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, COND_INSIDE, TEMP_OUTSIDE, PARAMS)
    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, COND_INSIDE, TEMP_OUTSIDE, FLOW_SPEED, PARAMS)

  Description:
    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, COND_INSIDE, TEMP_OUTSIDE, PARAMS)
    corrects thermal lag in a CTD profile sequence with constant flow speed
    (pumped CTD) given by vectors TIMESTAMP (sampe timestamp), COND_INSIDE 
    (conductivity inside CTD cell) and TEMP_OUTSIDE (temperature outside CTD 
    cell), returning vectors COND_OUTSIDE (conductivity outside CTD cell) and 
    TEMP_INSIDE (temperature inside CTD cell). TIMESTAMP, COND_INSIDE, 
    TEMP_OUTSIDE, COND_OUTSIDE and TEMP_OUTSIDE all have the same dimensions. 
    The correction parameters are given in a two element vector PARAMS, 
    with the error magnitude (alpha), and the error time constant (tau). 
    A detailed description of these parameters may be found in the references 
    listed below (Lueck 1990).

    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, DEPTH, PITCH, COND_INSIDE, TEMP_OUTSIDE, PARAMS)
    performs the same correction but for a CTD profile with variable flow 
    speed (unpumped CTD), given by FLOW. FLOW should be a vector with the 
    same dimensions as COND_INSIDE and TEMP_OUTSIDE, with the flow speed 
    inside the CTD cell in m/s. The correction parameters are given in a 
    four element vector PARAMS, with the offset and the slope of the error 
    magnitude (alpha_o and alpha_s), and the offset and the slope of the 
    error  time constant (tau_o and tau_s). A detailed description of these 
    parameters may be found in the references listed below (Morison 1994).

  Notes:
    This function is a recoding of the function by Tomeu Garau with the same 
    name. He is the true glider man. Main changes are:
      - Moved flow speed computation to a separate function COMPUTECTDFLOWSPEED.
      - Added support for pumped CTD profiles (constant flow speed).

  References:
    Garau, B.; Ruiz, S.; G. Zhang, W.; Pascual, A.; Heslop, E.;
    Kerfoot, J.; and Tintoré, J.; 2011:
    Thermal Lag Correction on Slocum CTD Glider Data.
    Journal of Atmospheric and Oceanic Technology, vol. 28, pages 1065-1071.

    Morison, J.; Andersen, R.; Larson, N.; D'Asaro, E.; and Boyd, T.; 1994:
    The Correction for Thermal-Lag Effects in Sea-Bird CTD Data.
    Journal of Atmospheric and Oceanic Technology, vol. 11, pages 1151-1164.

    Lueck, R. G.; and Picklo, J. J.; 1990:
    Thermal Inertia of Conductivity Cells: Observations with a Sea-Bird cell.
    Journal of Atmospheric and Oceanic Technology, vol. 7, pages 756–768.

    Lueck, R. G.; 1990:
    Thermal Inertia of Conductivity Cells: Theory.
    Journal of Atmospheric and Oceanic Technology, vol. 7, pages 741–755.

  Examples:
    % Constant flow speed (pumped CTD) profile:
    [temp_inside, cond_outside] = ...
      correctThermalLag(timestamp, cond_inside, temp_outside, params)
    % Variable flow speed (unpumped CTD) profile:
    [temp_inside, cond_outside] = 
      correctThermalLag(timestamp, cond_inside, temp_outside, ...
                        flow_speed, params)

  See also:
    FINDTHERMALLAGPARAMS
    COMPUTECTDFLOWSPEED

  Authors:
    Joan Pau Beltran  <joanpau.beltran@socib.cat>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

correctThermalLag.m

SOURCE CODE ^

0001 function [temp_inside, cond_outside] = correctThermalLag(varargin)
0002 %CORRECTTHERMALLAG  Correct CTD conductivity and temperature sequence from thermal lag effects.
0003 %
0004 %  Syntax:
0005 %    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, COND_INSIDE, TEMP_OUTSIDE, PARAMS)
0006 %    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, COND_INSIDE, TEMP_OUTSIDE, FLOW_SPEED, PARAMS)
0007 %
0008 %  Description:
0009 %    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, COND_INSIDE, TEMP_OUTSIDE, PARAMS)
0010 %    corrects thermal lag in a CTD profile sequence with constant flow speed
0011 %    (pumped CTD) given by vectors TIMESTAMP (sampe timestamp), COND_INSIDE
0012 %    (conductivity inside CTD cell) and TEMP_OUTSIDE (temperature outside CTD
0013 %    cell), returning vectors COND_OUTSIDE (conductivity outside CTD cell) and
0014 %    TEMP_INSIDE (temperature inside CTD cell). TIMESTAMP, COND_INSIDE,
0015 %    TEMP_OUTSIDE, COND_OUTSIDE and TEMP_OUTSIDE all have the same dimensions.
0016 %    The correction parameters are given in a two element vector PARAMS,
0017 %    with the error magnitude (alpha), and the error time constant (tau).
0018 %    A detailed description of these parameters may be found in the references
0019 %    listed below (Lueck 1990).
0020 %
0021 %    [TEMP_INSIDE, COND_OUTSIDE] = CORRECTTHERMALLAG(TIMESTAMP, DEPTH, PITCH, COND_INSIDE, TEMP_OUTSIDE, PARAMS)
0022 %    performs the same correction but for a CTD profile with variable flow
0023 %    speed (unpumped CTD), given by FLOW. FLOW should be a vector with the
0024 %    same dimensions as COND_INSIDE and TEMP_OUTSIDE, with the flow speed
0025 %    inside the CTD cell in m/s. The correction parameters are given in a
0026 %    four element vector PARAMS, with the offset and the slope of the error
0027 %    magnitude (alpha_o and alpha_s), and the offset and the slope of the
0028 %    error  time constant (tau_o and tau_s). A detailed description of these
0029 %    parameters may be found in the references listed below (Morison 1994).
0030 %
0031 %  Notes:
0032 %    This function is a recoding of the function by Tomeu Garau with the same
0033 %    name. He is the true glider man. Main changes are:
0034 %      - Moved flow speed computation to a separate function COMPUTECTDFLOWSPEED.
0035 %      - Added support for pumped CTD profiles (constant flow speed).
0036 %
0037 %  References:
0038 %    Garau, B.; Ruiz, S.; G. Zhang, W.; Pascual, A.; Heslop, E.;
0039 %    Kerfoot, J.; and Tintoré, J.; 2011:
0040 %    Thermal Lag Correction on Slocum CTD Glider Data.
0041 %    Journal of Atmospheric and Oceanic Technology, vol. 28, pages 1065-1071.
0042 %
0043 %    Morison, J.; Andersen, R.; Larson, N.; D'Asaro, E.; and Boyd, T.; 1994:
0044 %    The Correction for Thermal-Lag Effects in Sea-Bird CTD Data.
0045 %    Journal of Atmospheric and Oceanic Technology, vol. 11, pages 1151-1164.
0046 %
0047 %    Lueck, R. G.; and Picklo, J. J.; 1990:
0048 %    Thermal Inertia of Conductivity Cells: Observations with a Sea-Bird cell.
0049 %    Journal of Atmospheric and Oceanic Technology, vol. 7, pages 756–768.
0050 %
0051 %    Lueck, R. G.; 1990:
0052 %    Thermal Inertia of Conductivity Cells: Theory.
0053 %    Journal of Atmospheric and Oceanic Technology, vol. 7, pages 741–755.
0054 %
0055 %  Examples:
0056 %    % Constant flow speed (pumped CTD) profile:
0057 %    [temp_inside, cond_outside] = ...
0058 %      correctThermalLag(timestamp, cond_inside, temp_outside, params)
0059 %    % Variable flow speed (unpumped CTD) profile:
0060 %    [temp_inside, cond_outside] =
0061 %      correctThermalLag(timestamp, cond_inside, temp_outside, ...
0062 %                        flow_speed, params)
0063 %
0064 %  See also:
0065 %    FINDTHERMALLAGPARAMS
0066 %    COMPUTECTDFLOWSPEED
0067 %
0068 %  Authors:
0069 %    Joan Pau Beltran  <joanpau.beltran@socib.cat>
0070 
0071 %  Copyright (C) 2013-2016
0072 %  ICTS SOCIB - Servei d'observacio i prediccio costaner de les Illes Balears
0073 %  <http://www.socib.es>
0074 %
0075 %  This program is free software: you can redistribute it and/or modify
0076 %  it under the terms of the GNU General Public License as published by
0077 %  the Free Software Foundation, either version 3 of the License, or
0078 %  (at your option) any later version.
0079 %
0080 %  This program is distributed in the hope that it will be useful,
0081 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0082 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0083 %  GNU General Public License for more details.
0084 %
0085 %  You should have received a copy of the GNU General Public License
0086 %  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0087 
0088   error(nargchk(4, 5, nargin, 'struct'));
0089   
0090   % Parse input arguments.
0091   switch(nargin)
0092     case 4
0093       % Constant flow speed (pumped CTD).
0094       [timestamp, cond_inside, temp_outside, params] = ...
0095         varargin{1:4};
0096       constant_flow = true;
0097     case 5
0098       % Variable flow speed (unpumped CTD).
0099       [timestamp, cond_inside, temp_outside, flow_speed, params] = ...
0100         varargin{1:5};
0101       constant_flow = false;
0102   end
0103   
0104   if constant_flow
0105     % Select full CTD rows
0106     % The positive time test is needed to deal with odd data from initial
0107     % lines in Slocum segment files.
0108     valid = (timestamp(:) > 0) ...
0109           & ~any(isnan([cond_inside(:) temp_outside(:)]), 2);
0110     time_val = timestamp(valid);
0111     temp_val = temp_outside(valid);
0112     cond_val = cond_inside(valid);
0113     % Extract parameter values:
0114     alpha = params(1);
0115     tau = params(2);
0116   else
0117     % Select full CTD rows
0118     % The positive time test is needed to deal with odd data from initial
0119     % lines in Slocum segment files.
0120     valid = (timestamp(:) > 0) ...
0121           & ~any(isnan([cond_inside(:) temp_outside(:) flow_speed(:)]), 2);
0122     time_val = timestamp(valid);
0123     temp_val = temp_outside(valid);
0124     cond_val = cond_inside(valid);
0125     flow_val = flow_speed(valid);
0126     % Extract parameter values:
0127     alpha_offset = params(1);
0128     alpha_slope = params(2);
0129     tau_offset = params(3);
0130     tau_slope = params(4);
0131     % Compute dynamic thermal error and error time parameters for variable flow
0132     % speed. The formula is given in the references above (Morison 1994).
0133     alpha = alpha_offset + alpha_slope ./ flow_val(1:end-1);
0134     tau = tau_offset + tau_slope ./ sqrt(flow_val(1:end-1));
0135   end
0136 
0137   
0138   % Compute the coefficients of the correction formula.
0139   % Definitions in references use the Nyquist frequency (half the sampling
0140   % frequency). This might be wrong in the original implementation by Tomeu
0141   % Garau, where the sampling frequency was used.
0142   % These are three equivalent formulas for coefficients.
0143   dtime = diff(time_val);
0144   % sampling_freq = 1 ./ dtime;
0145   % nyquist_freq = 0.5 * sampling_freq;
0146   % coefa = alpha .* (4 * nyquist_freq .* tau) ./ (1 + 4 * nyquist_freq .* tau);
0147   % coefb = 1 - 2  * (4 * nyquist_freq .* tau) ./ (1 + 4 * nyquist_freq .* tau);
0148   % coefa = 2 * alpha ./ (2 + dtime .* beta); % from SBE Data Processing.
0149   % coefb = 1 - 2 .* coefa ./ alpha;
0150   coefa = 2 * alpha ./ (2 + dtime ./ tau);  % same using tau instead of beta.
0151   coefb = 1 - 4 ./ (2 + dtime ./ tau);
0152   
0153   
0154   % Compute the sensitivity of conductivity with respect to temperature.
0155   % Approximation suggested by SeaBird at section 6 of SBE Data Processing
0156   % User's Manual: Data Processing Modules, Cell Thermal Mass.
0157   % Software Release 7.16a and later. Date: 01/18/08
0158   % dc_dt = 0.1 .* (1 + 0.006 .* (temp - 20));
0159   dcond_dtemp = (0.088 + 0.0006 * temp_val);
0160   
0161   % Compute auxiliary vector of consecutive temperature differences.
0162   dtemp = diff(temp_val);
0163   
0164   % Compute conductivity and temperature correction using the recursive formula
0165   % proposed in references. Loop unfolding seems impractical.
0166   cond_correction = zeros(size(time_val));
0167   temp_correction = zeros(size(time_val));
0168   for n = 1:numel(time_val)-1,
0169     % Compute corrections for next depth level.
0170     cond_correction(n+1) = ...
0171       - coefb(n) * cond_correction(n) + coefa(n) * dcond_dtemp(n) * dtemp(n);
0172     temp_correction(n+1) = ...
0173       - coefb(n) * temp_correction(n) + coefa(n) * dtemp(n);
0174   end
0175 
0176   % Apply corrections to valid values in original sequences,
0177   % preserving invalid values in the output.
0178   temp_inside = nan(size(timestamp));
0179   cond_outside = nan(size(timestamp));
0180   temp_inside(valid) = temp_val - temp_correction;
0181   cond_outside(valid) = cond_val + cond_correction;
0182 
0183 end

Generated on Fri 06-Oct-2017 10:47:42 by m2html © 2005