function [Ts,uTs,Ta,uTa,Vsh,uVsh,Vh,uVh] = getBlockData(fname)
% getBlockData  Read and convert raw data for temperatures etc. from MAT
%               file for the single block experiment
%
% Synopsis: [Ts,uTs,Ta,uTa,Vh,uVh,...] = getBlockData(fname)
%
%   THIS FILE IS NOT FINISHED:  fill in the "..." in the list of output
%   arguments and provide the necessary code to define the variables in
%   output argument list
%
% Input:  fname = (string) name of .MAT file exported from aveAgilentDataGUI
%
% Output: Ts,uTa = surface temperature and uncertainty estimate for surface
%                  temperature of the block
%         Ta,uTa = surface temperature and uncertainty estimate for surface
%                 temperature of the block
%         Vsh,uVsh = shunt resister voltage and uncertainty
%         Vh,uVh = heater voltage and uncertainty

% --- Load data.  Variables like R101ave, R101std, V101ave, etc. are
%     created in the private workspace of this function
load(fname);
verbose = true;    %  set to false to suppress printing of converted data

% --- Constants for uncertainty analysis
uTzcal = 0.2;     %  Thermistors are replaceable to within +/- 0.2C
uTcal = 0.2*5/9;  %  Thermocouple calibration is +/- 0.2 F at 200 F

% --- Convert Zone box thermistor
Rz = abs(R101ave);    %  Fix bug in Benchlink.  Resistance can't be negative
Tz = thermistorT(Rz);
uTzrand = thermistorT(Rz+2*R101std) - Tz;
uTz = sqrt( uTzrand.^2 + uTzcal.^2 );

% --- Convert thermocouples on the surface of the block
T105 = Ttemp(V105ave,Tz);
uT105rand = Ttemp(V105ave+2*V105std,Tz) - T105;
uT105zone = Ttemp(V105ave,Tz+uTz)       - T105;
uT105 = sqrt( uT105rand.^2 + uT105zone.^2 + uTcal.^2 );

T106 = Ttemp(V106ave,Tz);
uT106rand = Ttemp(V106ave+2*V106std,Tz) - T106;
uT106zone = Ttemp(V106ave,Tz+uTz)       - T106;
uT106 = sqrt( uT106rand.^2 + uT106zone.^2 + uTcal.^2 );

% --- Use two thermocouples to compute surface temperature
Ts = 0.5*(T105+T106);
uTs = 0.5*sqrt( uT105.^2 + uT106.^2 );

% --- Repeat calculation for air temperature
Ta = Ttemp(V102ave,Tz);             %   Is channel 102 the TC measuring air temperature?
uTaz = Ttemp(V102ave,Tz+uTz) - Ta;
uTa = sqrt( (2*V102std).^2 + uTcal.^2 + uTaz.^2);

% --- Assign shunt and heater voltages and uncertainties.  No conversion
Vsh = V103ave;   uVsh = 2*V103std;
Vh  = V104ave;   uVh  = 2*V104std;

% --- Print results
if verbose
  fprintf('\nConverted data from %s:\n\n',fname);
  fprintf('  Tz    uTz    Ts    uTs    Ta    uTa      Vsh      uVsh       Vh   uVh\n');
  for i=1:length(Ts)
    fprintf('%6.2f %5.2f %6.2f %5.2f %6.2f %5.2f%11.2e%11.3e %5.2f %5.3f\n',...
            Tz(i),uTz(i),Ts(i),uTs(i),Ta(i),uTa(i),Vsh(i),uVsh(i),...
            Vh(i),uVh(i));
  end
end