addpath('/ligo/data/camera');

%%ETMX locked in IR
%import files
importfile('etmx_IR_e200_H1 ETMX (h1cam25)_2014-12-10-04-58-46.tiff'); %40000
etmxIR=im2double(etmx_IR_e200_H1ETMX0x28h1cam250x29_20140x2D120x2D100x2D040x2D58);
importfile('etmx_BG_e200_H1 ETMX (h1cam25)_2014-12-10-04-53-54.tiff')
etmxBG=im2double(etmx_BG_e200_H1ETMX0x28h1cam250x29_20140x2D120x2D100x2D040x2D53);

%remove background and noise
etmx=etmxIR-etmxBG;
etmx(etmx<0.2)=0;


figure(7);
subplot(2,2,1)
imagesc(etmx)
axis square
title('etmx');colorbar;caxis([0 1])

%look at total number of saturated pixels and total pixel sum.
sum(sum(etmx==1))
etmx_PC=sum(sum(etmx))

%%ETMY locked in IR
importfile('etmy_IR_e200_H1 ETMY (h1cam27)_2014-12-10-04-53-06.tiff'); %40000
etmyIR= im2double(etmy_IR_e200_H1ETMY0x28h1cam270x29_20140x2D120x2D100x2D040x2D53);
importfile('etmy_BG_e200_H1 ETMY (h1cam27)_2014-12-10-04-58-49.tiff')
etmyBG=im2double(etmy_BG_e200_H1ETMY0x28h1cam270x29_20140x2D120x2D100x2D040x2D58);

%remove background and noise
etmy=etmyIR-etmyBG;
etmy(etmy<0.2)=0;

figure(7);
subplot(2,2,2)
imagesc(etmy)
axis square
title('etmy');colorbar;caxis([0 1])
sum(sum(etmy==1))
etmy_PC=sum(sum(etmy))

%%ITMX locked in IR
importfile('itmx_IR_e10000_H1 ITMX (h1cam21)_2014-12-10-04-58-50.tiff'); %40000
itmxIR=im2double(itmx_IR_e10000_H1ITMX0x28h1cam210x29_20140x2D120x2D100x2D040x2D);
importfile('itmx_BG_e10000_H1 ITMX (h1cam21)_2014-12-10-04-53-40.tiff')
itmxBG=im2double(itmx_BG_e10000_H1ITMX0x28h1cam210x29_20140x2D120x2D100x2D040x2D);

itmx=itmxIR-itmxBG;
itmx(itmx<0.2)=0;

figure(7);
subplot(2,2,3)
imagesc(itmx)
axis square
title('itmx');colorbar;caxis([0 1])
sum(sum(itmx==1))
itmx_PC=sum(sum(itmx))

%%itmY locked in IR
importfile('itmy_IR_e10000_H1 ITMY (h1cam23)_2014-12-10-04-52-50.tiff'); %40000
itmyIR= im2double(itmy_IR_e10000_H1ITMY0x28h1cam230x29_20140x2D120x2D100x2D040x2D);
importfile('itmy_BG_e10000_H1 ITMY (h1cam23)_2014-12-10-04-58-51.tiff')
itmyBG=im2double(itmy_BG_e10000_H1ITMY0x28h1cam230x29_20140x2D120x2D100x2D040x2D);

itmy=itmyIR-itmyBG;
itmy(itmy<0.2)=0;

figure(7);
subplot(2,2,4)
imagesc(itmy)
axis square
title('itmy');colorbar;caxis([0 1])
sum(sum(itmy==1))
itmy_PC=sum(sum(itmy))

%% plot all the IR images
figure(8);
title('background + noise removed')
subplot(2,2,1)
imagesc(etmx)
axis square
title('etmx, exposure=200\mus');
colorbar;caxis([0 1])

subplot(2,2,2)
imagesc(etmy)
axis square
title('etmy, exposure=200\mus');
colorbar;caxis([0 1])


subplot(2,2,3)
imagesc(itmx)
axis square
title('itmx, exposure=10000\mus');
colorbar;caxis([0 1])

subplot(2,2,4)
imagesc(itmy)
axis square
title('itmy, exposure=10000\mus');
colorbar;caxis([0 1])

%%higher exposure images
%%need to add line to import these or import manually...
%%the file name of each image is commented below, files are in
%%/ligo/data/camera
% figure(6);
% subplot(2,2,1)
% imagesc(etmx2)
% title('ETMX, IR locked, exposure=60000\mus')  %etmx_IRlocked_H1 ETMX (h1cam25)_2014-12-10-04-07-59
% subplot(2,2,2)
% imagesc(etmy2)
% title('ETMY, IR locked, exposure=10000\mus') %etmy_IR_e10000_H1 ETMY (h1cam27)_2014-12-10-04-54-17
% subplot(2,2,3)
% imagesc(itmx2)
% title('ITMX, IR locked, exposure=60000\mus') %IRlocked_H1 ITMX (h1cam21)_2014-12-10-02-10-24
% subplot(2,2,4)
% imagesc(itmy2)
% title('ITMY, IR locked, exposure=60000\mus') %IRlocked_H1 ITMY (h1cam23)_2014-12-10-00-54-14


%%
 figure(5);
 
 hist(reshape(im2double(etmxIR),480*640,1),300,'r');
subplot(2,1,2)
 hist(reshape(im2double(etmxBG),480*640,1),300,'b');
 %legend('IR','BG')
