
% get data from ITM RHs for acceptance testing

prefix = 'H1:TCS-ITM';
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'};
           
for ii = 1:7
    channelList{ii} = [prefix, 'X_', channelList{ii}];
    channelList{ii+7} = [prefix, 'Y_', channelList{ii+7}];
end

t0 = 1107375897 - 45*60;

% get the data
data1 = get_data(channelList, 'raw', t0, 45*60);

% 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
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(1)+2*16;
    deltat = 23*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 = 2:6
        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
    aFit = polyfit(RList(:, 2), RList(:, 1), 1);
    R1 = aFit(1);
    RListOut = [RListOut, round(R1*10)/10];
    
end
title('Relative increase in RH resistance vs time with 1W applied per segment')
legend(['ITMX\_UPPER, R = ' num2str(RListOut(1)), 'Ohms'], ...
       ['ITMX\_LOWER, R = ' num2str(RListOut(2)), 'Ohms'], ...
       ['ITMY\_UPPER, R = ' num2str(RListOut(3)), 'Ohms'], ...
       ['ITMY\_LOWER, R = ' num2str(RListOut(4)), 'Ohms'], ...
       'Location', 'NorthWest')
orient landscape
text(0, -0.1, [mfilename('fullpath'), '.m'], 'Units', 'normalized', 'FontSize', 6, 'Interpreter', 'none')
print('-dpdf', [mfilename(), '.pdf'])




    
    