function T = Ttemps(e,Tref)
% Ttemps  Temperature of T-type thermocouple;  scalar version
%
% Synopsis:  T = Ttemps(e)
%            T = Ttemps(e,Tref)
%
% Input:  e = (scalar) emf of thermocouple junction in Volts
%         Tref = (optional) temperature of reference junction
%                Default:  Tref = 0 C (ice point)
%
% Output:  T = temperature, degrees C

if length(e)>1
  warning('e must be a scalar:  only e(1) is converted');
end
if nargin<2
  T = TofTtypes(e(1));  return;
elseif nargin==2
  eref = emfTtypes(Tref);    %  emf of ice-point referenced T-couple at Tref
  T = TofTtypes(e(1)+eref);  %  adjust for nonzero reference temperature
  return;
else
  error('only one or two inputs allowed');
end

% ==============================================================
function T = TofTtypes(e)
% TofTtypes  Temperature of T type thermocouple with ice-point compensation
%            Scalar version:  input e must be a scalar
%
% Synopsis:  T = TofTtypes(e)
%
% Input:   e = (vector or scalar) emf of thermocouple junction in Volts
%              T-couple is assumed to have a reference junction at 0 C
%
% Output:  T = temperature, degrees C

em = e*1e3;   %  emf in mV, must be a scalar
% --- Polynomial constants are determined by value of em
if 0<=em && em<=20.872
  c = [-7.293422e-7  6.048144e-5  -2.165394e-3  4.637791e-2 ...
       -0.7602961 25.92800  0];
elseif -5.603<=em && em<0
  c = [1.2668171e-3   2.0241446e-2  0.13304473  0.42527777 ...
       0.79018692  -0.21316967  25.949192 0];
else
  error(sprintf('e = %g value outside of range -5.603 <= e <= 20.872 mV\n',em));
end
T = polyval(c,em);