% load the TEM10 data

close all

for mm = 1:2
figure(mm)   
set(gcf, 'defaultAxesFontSize', 16)
    if mm == 1
t0 = 1160956817 +5*3600;
t1 = t0 + 3600;
    else
t0 = 1161043217 +3.4*3600;
t1 = t0 + 3600;
    end
chan = {'H1:TCS-SIM_IFO_XARM_HOM_SPACING_HZ_OUTPUT.mean,s-trend', ...
       'H1:TCS-SIM_IFO_YARM_HOM_SPACING_HZ_OUTPUT.mean,s-trend'};

[dataSIM, tL, outputSIM] = get_data(t0,t1,chan,[]);

chan = 'H1:LSC-DARM_IN1_DQ';
[data1, t, info] = get_data(t0,t1, chan, []);

tmax = 50;
dt = 2;

fListFit = [];
for kk = 1:0.5:floor((t1-t0)/tmax)
    tA = t0 + tmax*(kk-1);
    %[data1, t, output] = get_data(tA ,tA+tmax,chan,[]);



    range1 = (kk-1)*16384*tmax+1:kk*16384*tmax;
    dataSub = data1(range1);
    %tSub = tL(round(tmax*(kk-0.5)));

   % SIMXY = round(data(tSub,:)*10)/10;

    for ii = 1:floor(tmax/dt)-1;
       i0 = (ii-1)*dt*16384 + 1;
       i1 = ii*dt*16384;

       data2 = dataSub(i0:i1);
       data2 = detrend(data2).*hanning(numel(data2));

       f1 = fft(data2);
       f = ((1:numel(data2))-1)/dt;

       %semilogy(f, abs(f1));
       %grid on
       %hold all
       %xlim([5050 5160])
       if ii == 1
           f2 =0*f1;
       end
       f2 = abs(f1)+f2;
       %pause(0.1)
    end
    subplot(2,1,2)
    semilogy(f, abs(f2)/median(abs(f2)));
    M1 = floor(log10(median(abs(f2))));

    finc = find((f>4500).*(f<5500));
    hold all
    xlabel('Frequency (Hz)')
    ylabel('Amplitude (a.u.)')
    

    %title(SIMXY)
    axis([4650 5500, 0.7 5])
    grid on
    
    subplot(2,1,1)
    h = set(gca, 'FontSize', 16);
    plot(dataSIM(:, 2))
    hold all
    tnow = tmax*(kk-0.5);
    hold all
    plot([tnow, tnow], [4700, 5200])
    axis([0 3600 4700 5200])
    grid on
    xlabel('Time (s)')
    ylabel('Frequency (Hz)')
    
    % get the data minuts lines
    fsub = abs(f2)/median(abs(f2));
    indhigh = find(fsub>2.25);
    fsub(indhigh) = 1;
    fmask = find(abs(f-5000)<1.9);
    fsub(fmask) = 0.9;
    
    fNom = dataSIM(round(tnow),2)-146;
    favg = find(abs(f-fNom)<40);
    fcent = sum(f(favg).*fsub(favg)'.^2)/sum(fsub(favg).^2);
    fcent = f(find(fsub == max(fsub(favg))));
    fcent = fcent(1);
    
    if dataSIM(tnow,2) - dataSIM(1,2) < 25
        colscale = 0.5;
    else
        colscale = 1;
    end
        
    fListFit = [fListFit; tnow, fcent, colscale];
    f1 = find(fListFit(:,3) == 1);
    f2 = find(fListFit(:,3) == 0.5);    
%     plot(fListFit(f2, 1), fListFit(f2, 2), 'o', ...
%         'MarkerSize', 10, 'MarkerFaceColor', [0.5,1,1]*0.5, ...
%         'MarkerEdgeColor','k', 'LineWidth', 2)   
    plot(fListFit(f1, 1), fListFit(f1, 2), 'o', ...
        'MarkerSize', 10, 'MarkerFaceColor', [0.5,1,1], ...
        'MarkerEdgeColor','k', 'LineWidth', 2)
 
     subplot(2,1,2)
     hold all
     semilogy([fcent, fcent], [0.5, 5], 'LineWidth', 2)
     hold off
     title('DARM ERR signal around 5kHz')
     subplot(2,1,1)
     title(['TEM10 HOM frequency versus time: t0 =', num2str(t0)])
     legend('H1:YARM - Online SIM HOM spacing estimate', ...
         'Current time', ...
         'Estimate from DARM peak', 'Location', 'NorthWest')
    
    
    %hold all
%     hold on 
%     plot(SIMXY(1), abs(f2(round(SIMXY(1)*10))), 'o', ...
%         'MarkerSize', 10, 'MarkerEdgeColor', 'b', ...
%         'MarkerFaceColor', [0.5,0,0]);
%     plot(SIMXY(2), abs(f2(round(SIMXY(2)*10))), 'o', ...
%         'MarkerSize', 10, 'MarkerEdgeColor', 'b', ...
%         'MarkerFaceColor', [0.0,0.5,0]);
%     hold off
    subplot(2,1,1)
    hold off
    pause(0.1)

end

orient tall
print('-dpdf', ['TEM10_HO_excitation', num2str(mm), '.pdf'])

end