
% get data from all TM RHs for acceptance testing
close all
clear all

filename1 = 'RH_acceptance_data.mat';

prefix = 'H1:TCS-ITM';
prefixE = 'H1:TCS-ETM';
channelList = {'RH_SETUPPERPOWER', 'RH_UPPERCURRENT', 'RH_UPPERVOLTAGE', ...
               'RH_SETLOWERPOWER', 'RH_LOWERCURRENT', 'RH_LOWERVOLTAGE', ...
               'RH_LOWERRTD', ...
               'RH_SETUPPERPOWER', 'RH_UPPERCURRENT', 'RH_UPPERVOLTAGE', ...
               'RH_SETLOWERPOWER', 'RH_LOWERCURRENT', 'RH_LOWERVOLTAGE', ...
               'RH_LOWERRTD', ...
               'RH_SETUPPERPOWER', 'RH_UPPERCURRENT', 'RH_UPPERVOLTAGE', ...
               'RH_SETLOWERPOWER', 'RH_LOWERCURRENT', 'RH_LOWERVOLTAGE', ...
               'RH_LOWERRTD', ...
               'RH_SETUPPERPOWER', 'RH_UPPERCURRENT', 'RH_UPPERVOLTAGE', ...
               'RH_SETLOWERPOWER', 'RH_LOWERCURRENT', 'RH_LOWERVOLTAGE', ...
               'RH_LOWERRTD'};
           
for ii = 1:7
    channelList{ii} = [prefix, 'X_', channelList{ii}];
    channelList{ii+7} = [prefix, 'Y_', channelList{ii+7}];
    channelList{ii+14} = [prefixE, 'X_', channelList{ii+14}];
    channelList{ii+21} = [prefixE, 'Y_', channelList{ii+21}];   
end

t0 = 1107704200;

if exist(filename1, 'file') == 0
    % get the data
    data1 = get_data(channelList, 'raw', t0, 45*60);
    save(filename1, 'data1')
else
    load(filename1)
end

% plot the data
dt = 1/data1(1).rate;
timeL = dt:dt:data1(1).duration;
plot(data1(1).data)

% determine the resistance vs time
close all
figure
h = subplot(2,1,1)
set(h, 'FontSize', 14)
cList = [1, 4, 8, 11];
RListOut = [];
for ii = 1:4
    dataS = (data1(cList(ii)).data);
    diff1 = dataS(2:end) - dataS(1:end-1);
    jj = find(diff1 > 0.05);
    ii0 = jj(2)+2*16;
    deltat = 30*60*data1(1).rate;
    
    dataI = smooth(data1(cList(ii)+1).data(ii0:ii0+deltat), 16*4);
    dataV = smooth(data1(cList(ii)+2).data(ii0:ii0+deltat), 16*4);
    dataR = dataV./dataI;
    R0 = mean(dataR(1:16*10));
    
    plot(timeL(ii0:ii0+deltat)-ii0/16, dataR/R0);
    grid on
    xlabel('Time (s)')
    ylabel('RH relative resistance')
    hold all
    
    % determine the resistance
    RList = [];
    for  kk = 3:9
        ii0 = jj(kk)+2*16;
        ii1 = ii0+7*16;
        V0 = mean(data1(cList(ii)+2).data(ii0:ii1));
        I0 = mean(data1(cList(ii)+1).data(ii0:ii1));
        RList = [RList; V0 I0];
    end
    RList
    aFit = polyfit(RList(:, 2), RList(:, 1), 1);
    R1 = aFit(1);
    RListOut = [RListOut, round(R1*10)/10];
    RListI(ii).data = RList;
end

title('Relative increase in RH resistance vs time with 1W applied per segment')
legend(['H1:ITMX\_UPPER, R = ' num2str(RListOut(1), '%3.1f'), ' Ohms'], ...
       ['H1:ITMX\_LOWER, R = ' num2str(RListOut(2), '%3.1f'), ' Ohms'], ...
       ['H1:ITMY\_UPPER, R = ' num2str(RListOut(3), '%3.1f'), ' Ohms'], ...
       ['H1:ITMY\_LOWER, R = ' num2str(RListOut(4), '%3.1f'), ' Ohms'], ...
       'Location', 'NorthWest')
ylim([0.999, 1.005])
% determine the resistance vs time
h = subplot(2,1,2)
set(h, 'FontSize', 14)
cList = [1, 4, 8, 11] +14;
RListOut = [];
for ii = 1:4
    dataS = (data1(cList(ii)).data);
    diff1 = dataS(2:end) - dataS(1:end-1);
    jj = find(diff1 > 0.05);
    ii0 = jj(2)+2*16;
    deltat = 30*60*data1(1).rate;
    
    dataI = smooth(data1(cList(ii)+1).data(ii0:ii0+deltat), 16*4);
    dataV = smooth(data1(cList(ii)+2).data(ii0:ii0+deltat), 16*4);
    dataR = dataV./dataI;
    R0 = mean(dataR(1:16*10));
    
    plot(timeL(ii0:ii0+deltat)-ii0/16, dataR/R0);
    grid on
    xlabel('Time (s)')
    ylabel('RH relative resistance')
    hold all
    
    % determine the resistance
    RList = [];
    for  kk = 3:9
        ii0 = jj(kk)+2*16;
        ii1 = ii0+7*16;
        V0 = mean(data1(cList(ii)+2).data(ii0:ii1));
        I0 = mean(data1(cList(ii)+1).data(ii0:ii1));
        RList = [RList; V0 I0];
    end
    RList
    aFit = polyfit(RList(:, 2), RList(:, 1), 1);
    R1 = aFit(1);
    RListOut = [RListOut, round(R1*10)/10];
    RListE(ii).data = RList;
    
end
title('Relative increase in RH resistance vs time with 1W applied per segment')
legend(['H1:ETMX\_UPPER, R = ' num2str(RListOut(1), '%3.1f'), ' Ohms'], ...
       ['H1:ETMX\_LOWER, R = ' num2str(RListOut(2), '%3.1f'), ' Ohms'], ...
       ['H1:ETMY\_UPPER, R = ' num2str(RListOut(3), '%3.1f'), ' Ohms'], ...
       ['H1:ETMY\_LOWER, R = ' num2str(RListOut(4), '%3.1f'), ' Ohms'], ...
       'Location', 'NorthWest')
ylim([0.999, 1.005])
orient tall
text(0, -0.11, [mfilename('fullpath'), '.m'], 'Units', 'normalized', 'FontSize', 6, 'Interpreter', 'none')
print('-dpdf', [mfilename(), '.pdf'])

figure
h = subplot(1,1,1)
set(h, 'FontSize', 14)
for ii = 1:4
    plot(RListI(ii).data(:, 2), RListI(ii).data(:, 1), '-o')
    hold all
    plot(RListE(ii).data(:, 2), RListE(ii).data(:, 1), '-x')
end
grid on
legend('H1 ITMX UPPER', 'H1 ETMX UPPER', ...
    'H1 ITMX LOWER', 'H1 ETMX LOWER', ...
    'H1 ITMY UPPER', 'H1 ETMY UPPER', ...
    'H1 ITMY LOWER', 'H1 ETMY LOWER', ...
    'Location', 'NorthWest')
title('Current vs voltage for all RHs - slope = resistance')
xlabel('Current (A)')
ylabel('Voltage (V)')
orient landscape
text(0, -0.1, [mfilename('fullpath'), '.m'], 'Units', 'normalized', 'FontSize', 6, 'Interpreter', 'none')
print('-dpdf', [mfilename(), '_resistance.pdf'])





    
    