close all
clear all
clc

%%
printFigs = false;

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

figTag = '2018-02-22_H1SUSOPO_OPOSBreadboard_BnKResp';

dofs = {'V';'L';'T'};         

ifo(2).name = 'H1';
ifo(2).startInd(1).tf = 84;
ifo(2).startInd(1).coh = 3379;
ifo(2).startInd(2).tf = 6674;
ifo(2).startInd(2).coh = 9969;
ifo(2).startInd(3).tf = 13264;
ifo(2).startInd(3).coh = 16559;
ifo(2).dataFiles = {'2018-02-22_SimpleHammer_H1SUSOPO_SuspendedBench_X_IFOZ_50-450Hz_PlusLongCornerExc_Meas2.txt';...
                    '2018-02-22_SimpleHammer_H1SUSOPO_SuspendedBench_X_IFOZ_50-450Hz_PlusLongCornerExc_WithViton_Meas4.txt';...
                    '2018-02-22_SimpleHammer_H1SUSOPO_SuspendedBench_X_IFOZ_50-450Hz_PlusTransCornerExc_Meas1.txt';...
                    '2018-02-22_SimpleHammer_H1SUSOPO_SuspendedBench_X_IFOZ_50-450Hz_PlusTransCornerExc_WithViton_Meas3.txt'};

%%
for iIFO = 2
    for iFile = 1:length(ifo(iIFO).dataFiles)
        for iDOF = 1:3

                    rawtf = importdata([dataDir ifo(iIFO).dataFiles{iFile}],'\t',ifo(iIFO).startInd(iDOF).tf);
                    rawcoh = importdata([dataDir ifo(iIFO).dataFiles{iFile}],'\t',ifo(iIFO).startInd(iDOF).coh);
                    
                    ifo(iIFO).meas(iFile).dof(iDOF).freq = rawtf.data(:,2);
                    ifo(iIFO).meas(iFile).dof(iDOF).tf   = abs(rawtf.data(:,3) + 1i*rawtf.data(:,4));
                    ifo(iIFO).meas(iFile).dof(iDOF).coh  = rawcoh.data(:,3);

            clear rawtf rawcoh
        end
    end
end
        
%%
figNum = 1;

for iIFO = 2
    for iDOF = 1:3
        figure(figNum)
        figNames{figNum} = [dataDir figTag '_' ifo(iIFO).name 'RawData_' dofs{iDOF} 'Resp_to_PlusLongCorner_V_Exc.pdf'];
        figNum = figNum+1;
        subplot(7,1,1:5)
        ll = loglog(ifo(iIFO).meas(1).dof(iDOF).freq,ifo(iIFO).meas(1).dof(iDOF).tf,...
                    ifo(iIFO).meas(2).dof(iDOF).freq,ifo(iIFO).meas(2).dof(iDOF).tf);
        xlim([150 450])
        ylim([1e-3 1e2])
        legend('No Viton',...
            'With Viton')
        title({[ifo(iIFO).name 'OPOS Breadboard Response'];...
            [dofs{iDOF} ' Resp. to V Exc. on +Long Edge']});
        ylabel('Magnitude / (m.s^{-2}).N^{-1}')
        set(gca,'XTick',100:50:500,'YTick',10.^(-3:2))
        
        subplot(7,1,6:7)
        ll = semilogx(ifo(iIFO).meas(1).dof(iDOF).freq,ifo(iIFO).meas(1).dof(iDOF).coh,...
                      ifo(iIFO).meas(2).dof(iDOF).freq,ifo(iIFO).meas(2).dof(iDOF).coh);
        xlim([150 450])
        ylim([0.5 1.01])
        ylabel('Coherence')
        xlabel('Frequency / Hz')
        set(gca,'XTick',100:50:500,'YTick',0.5:0.1:1)
    end
    
    for iDOF = 1:3
        figure(figNum)
        figNames{figNum} = [dataDir figTag '_' ifo(iIFO).name 'RawData_' dofs{iDOF} 'Resp_to_PlusTransCorner_V_Exc.pdf'];
        figNum = figNum+1;
        subplot(7,1,1:5)
        ll = loglog(ifo(iIFO).meas(3).dof(iDOF).freq,ifo(iIFO).meas(3).dof(iDOF).tf,...
                    ifo(iIFO).meas(4).dof(iDOF).freq,ifo(iIFO).meas(4).dof(iDOF).tf);
        xlim([150 450])
        ylim([1e-3 1e2])
        legend('No Viton',...
            'With Viton')
        title({[ifo(iIFO).name 'OPOS Breadboard Response'];...
            [dofs{iDOF} ' Resp. to V Exc. on +Trans Edge']});
        ylabel('Magnitude / (m.s^{-2}).N^{-1}')
        set(gca,'XTick',100:50:500,'YTick',10.^(-3:2))
        
        subplot(7,1,6:7)
        ll = semilogx(ifo(iIFO).meas(3).dof(iDOF).freq,ifo(iIFO).meas(3).dof(iDOF).coh,...
                      ifo(iIFO).meas(4).dof(iDOF).freq,ifo(iIFO).meas(4).dof(iDOF).coh);
        xlim([150 450])
        ylim([0.5 1.01])
        ylabel('Coherence')
        xlabel('Frequency / Hz')
        set(gca,'XTick',100:50:500,'YTick',0.5:0.1:1)
    end
    
    for iDOF = 1:3
        figure(figNum)
        figNames{figNum} = [dataDir figTag '_' ifo(iIFO).name 'RawData_' dofs{iDOF} 'Resp_to_V_Exc_wViton.pdf'];
        figNum = figNum+1;
        subplot(7,1,1:5)
        ll = loglog(ifo(iIFO).meas(2).dof(iDOF).freq,ifo(iIFO).meas(2).dof(iDOF).tf,...
                    ifo(iIFO).meas(4).dof(iDOF).freq,ifo(iIFO).meas(4).dof(iDOF).tf);
        xlim([150 450])
        ylim([1e-3 1e2])
        legend('+Long Corner',...
               '+Trans Corner')
        title({[ifo(iIFO).name 'OPOS Breadboard Response'];...
            [dofs{iDOF} ' Resp. to V Exc. w/ Viton']});
        ylabel('Magnitude / (m.s^{-2}).N^{-1}')
        set(gca,'XTick',100:50:500,'YTick',10.^(-3:2))
        
        subplot(7,1,6:7)
        ll = semilogx(ifo(iIFO).meas(2).dof(iDOF).freq,ifo(iIFO).meas(2).dof(iDOF).coh,...
                      ifo(iIFO).meas(4).dof(iDOF).freq,ifo(iIFO).meas(4).dof(iDOF).coh);
        xlim([150 450])
        ylim([0.5 1.01])
        ylabel('Coherence')
        xlabel('Frequency / Hz')
        set(gca,'XTick',100:50:500,'YTick',0.5:0.1:1)
    end
end

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