close all
clear all
clc

%%
printFigs = true;
figTag = '2017-12-13_ScraperBaffle_BnKResultComparison';


dataDir = '/Users/kissel/Desktop/scratch/2017LHOVent/BnK/ScraperBaffles/';

baffle = {'MC2';'PR2';'SR2'};

dof = {'X'; 'Y'; 'Z'};
ifoName   = {'L1'; 'H1'};
sensType  = {'Vib';'Acc'};
sensUnits = {'m.s^{-1}';'m.s^{-2}'};
      
ifo(2).baffle(1).dof(1).fileName = '2017-11-30_H1SUSMC2_ScraperBaffle_AccZ_IFOX_0-400Hz_Exc_Zresp.txt';
ifo(2).baffle(1).dof(2).fileName = '2017-11-30_H1SUSMC2_ScraperBaffle_AccY_IFOY_0-400Hz_Exc_Yresp.txt';

ifo(2).baffle(2).dof(1).fileName = '2017-11-30_H1SUSPR2_ScraperBaffle_AccZ_IFOX_0-400Hz_Exc_Zresp.txt';
ifo(2).baffle(2).dof(2).fileName = '2017-11-30_H1SUSPR2_ScraperBaffle_AccY_IFOY_0-400Hz_Exc_Yresp.txt';

ifo(2).baffle(3).dof(1).fileName = '2017-11-03_H1SUSSR2_ScraperBaffle_AccZ_IFOX_0-400Hz_Exc_Zresp.txt';
ifo(2).baffle(3).dof(2).fileName = '2017-11-03_H1SUSSR2_ScraperBaffle_AccY_IFOY_0-400Hz_Exc_Yresp.txt';

ifo(1).baffle(1).dof(1).fileName = '2017-12-01_L1SUSMC2_ScraperBaffle_Vib_IFOX_0-400Hz_Exc_IFOYresp.txt';
ifo(1).baffle(1).dof(2).fileName = '2017-12-01_L1SUSMC2_ScraperBaffle_Vib_IFOY_0-400Hz_Exc_IFOYresp.txt';

ifo(1).baffle(2).dof(1).fileName = '2017-12-01_L1SUSPR2_ScraperBaffle_Vib_IFOX_0-400Hz_Exc_IFOYresp.txt';
ifo(1).baffle(2).dof(2).fileName = '2017-12-01_L1SUSPR2_ScraperBaffle_Vib_IFOY_0-400Hz_Exc_IFOYresp.txt';

ifo(1).baffle(3).dof(1).fileName = '2017-12-07_L1SUSSR2_ScraperBaffle_Vib_IFOX_0-400Hz_Exc_IFOXresp.txt';
ifo(1).baffle(3).dof(2).fileName = '2017-12-07_L1SUSSR2_ScraperBaffle_Vib_IFOY_0-400Hz_Exc_IFOXresp.txt';

%%
for iIFO = 1:2
    for iBaffle = 1:3
        for iDOF = 1:2
            ifo(iIFO).baffle(iBaffle).dof(iDOF).rawData = load([dataDir ifo(iIFO).baffle(iBaffle).dof(iDOF).fileName]);
            
            ifo(iIFO).baffle(iBaffle).dof(iDOF).freq = ifo(iIFO).baffle(iBaffle).dof(iDOF).rawData(:,2);
            
            switch ifoName{iIFO}
                case 'H1'
                    ifo(iIFO).baffle(iBaffle).dof(iDOF).tf   = abs(ifo(iIFO).baffle(iBaffle).dof(iDOF).rawData(:,3) ...
                                                                   + 1i* ifo(iIFO).baffle(iBaffle).dof(iDOF).rawData(:,4));
                case 'L1'
                    ifo(iIFO).baffle(iBaffle).dof(iDOF).tf   = 2*pi*ifo(iIFO).baffle(iBaffle).dof(iDOF).freq ...
                                                                   .* ifo(iIFO).baffle(iBaffle).dof(iDOF).rawData(:,3);
            end
        end
    end
end

%%
figNum = 1;
%%
for iBaffle = 1:3
    figure(figNum)
    figNames{figNum} = [figTag '_H1_' baffle{iBaffle} '.pdf'];
    figNum = figNum+1;
    loglog(ifo(2).baffle(iBaffle).dof(1).freq, ifo(2).baffle(iBaffle).dof(1).tf,...
           ifo(2).baffle(iBaffle).dof(2).freq, ifo(2).baffle(iBaffle).dof(2).tf);
    legend([ifoName{2} ' ' dof{1} ' to ' dof{1} ' (Acc)'],...
           [ifoName{2} ' ' dof{2} ' to ' dof{2} ' (Acc)'],...
           'Location','SouthEast')
    title(['H1' baffle{iBaffle} ' Scraper Baffle Response Comparison';])
    xlim([5 400])
    ylim([1e-4 1e-1])
    set(gca,'XTick',10.^(0:3),'YTick',10.^(-4:-1))
    xlabel('Frequency / Hz')
    ylabel('Magnitude / (m.s^{-2}).N^{-1}')
    
end

%%
    figure(figNum)
    figNames{figNum} = [figTag '_' baffle{2} 'and' baffle{3} '_L1vsH1.pdf'];
    figNum = figNum+1;
    subplot(211)
    loglog(ifo(2).baffle(2).dof(1).freq, ifo(2).baffle(2).dof(1).tf,...
           ifo(2).baffle(3).dof(2).freq, ifo(2).baffle(3).dof(2).tf,...
           ifo(1).baffle(2).dof(2).freq, ifo(1).baffle(2).dof(2).tf);
    legend([ifoName{2} ' ' baffle{2} ' ' dof{1} ' to ' dof{1} ' (Acc)'],...
           [ifoName{2} ' ' baffle{3} ' ' dof{2} ' to ' dof{2} ' (Acc)'],...
           [ifoName{1} ' ' baffle{2} ' ' dof{2} ' to ' dof{2} ' (Vib * 2*pi*f)'],...
           'Location','NorthWest')
    title({'Z Scraper Baffle Response Comparison';'Transverse'})
    xlim([5 400])
    ylim([1e-4 1e1])
    xlabel('Frequency / Hz')
    ylabel('Magnitude / (m.s^{-2}).N^{-1}')
    set(gca,'XTick',10.^(0:3),'YTick',10.^(-4:1))
   
    
    subplot(212)
    loglog(ifo(2).baffle(2).dof(2).freq, ifo(2).baffle(2).dof(2).tf,...
           ifo(2).baffle(3).dof(1).freq, ifo(2).baffle(3).dof(1).tf,...
           ifo(1).baffle(3).dof(1).freq, ifo(1).baffle(3).dof(1).tf);
    legend([ifoName{2} ' ' baffle{2} ' ' dof{2} ' to ' dof{2} ' (Acc)'],...
           [ifoName{2} ' ' baffle{3} ' ' dof{1} ' to ' dof{1} ' (Acc)'],...
           [ifoName{1} ' ' baffle{3} ' ' dof{1} ' to ' dof{1} ' (Vib * 2*pi*f)'],...
           'Location','NorthWest')
    title({'Z Scraper Baffle Response Comparison';'Longitudinal'})
    xlim([5 400])
    ylim([1e-4 1e1])
    xlabel('Frequency / Hz')
    ylabel('Magnitude / (m.s^{-2}).N^{-1}')
    set(gca,'XTick',10.^(0:3),'YTick',10.^(-4:1))
    
%%
    figure(figNum)
    figNames{figNum} = [figTag '_' baffle{1} '_L1vsH1.pdf'];
    figNum = figNum+1;
    loglog(ifo(2).baffle(1).dof(2).freq, ifo(2).baffle(1).dof(2).tf,...
           ifo(1).baffle(1).dof(2).freq, ifo(1).baffle(1).dof(2).tf);
    legend([ifoName{2} ' ' baffle{1} ' ' dof{2} ' to ' dof{2} ' (Acc)'],...
           [ifoName{1} ' ' baffle{1} ' ' dof{2} ' to ' dof{2} ' (Vib * 2*pi*f)'],...
           'Location','NorthWest')
    title([baffle{1} ' Scraper Baffle Response Comparison';])
    xlim([5 400])
    ylim([1e-4 1e0])
    xlabel('Frequency / Hz')
    ylabel('Magnitude / (m.s^{-2}).N^{-1}')
    set(gca,'XTick',10.^(0:3),'YTick',10.^(-4:0))

%%
if printFigs
    mergeCommand = ['pdfmerge ' dataDir figTag '.pdf'];
    
    for iFig = 1:5
        figure(iFig)
        FillPage('w')
        IDfig(', J. Kissel')
        saveas(gcf,[dataDir figNames{iFig}]);
        
        mergeCommand = [mergeCommand ' ' dataDir figNames{iFig}];
    end
    system(mergeCommand);
end