% a script to load and clean data
% need: cube2bio.m, cube2mat.m

% load data

y = load( 'FLR_244_DC_Final_Take2.mat' );
flr244=y.theDC;

bio244=cube2bio(flr244);
spec244=cube2mat(flr244);

y = load( 'FLR_245_DC_Final_Take2.mat' );
flr245=y.theDC;

bio245=cube2bio(flr245);
spec245=cube2mat(flr245);

% ex/em matrix

xm=[sort(repmat(330:10:480,[1 501]));repmat(300:800,[1 16])];

clear flr244 flr245 y

% put the 244/245 data together
% and delete empty columns of and add some more info to bio data
% new bio data is 4846 by 48
% col 1 - 44: same as before
% col 45: QC (see README file by Brian)
% col 46: box (see README file by Brian)
% col 47: indicate 244 or 245
% col 48: obs number

bio=[bio244; bio245];
bio(:,[45:299,302:end])=[];
bio=[bio [repmat(244, [2606 1]) ; repmat(245, [2240 1])] (1:size(bio,1))'];

spec=[spec244; spec245];

% clean up some files

clear spec244 spec245 bio244 bio245


% masks

ind=zeros(1,size(xm,2));
ind(1,xm(2,:)-xm(1,:)<55)=1;
ind(1,xm(2,:)>700)=1;
ind(1,xm(1,:)==330&xm(2,:)>650)=1;
ind(1,(xm(1,:)==340|xm(1,:)==350|xm(1,:)==360)&xm(2,:)>660)=1;
ind(1,xm(1,:)==370&xm(2,:)>667)=1;
ind=logical(ind);

xm(:,ind)=[];

spec(:,ind)=[];

% filter leak (for EEM3, by Brian)

ind=bio(:,1);
ind=((ind>=24450000&ind<24460000)|(ind>=24550000&ind<24550127));
spec(ind,xm(1,:)==380)=NaN;

ind=bio(:,44);
ind=(ind==5);
spec(ind,xm(1,:)==430)=NaN;

% some more filter leaks
spec([1396 1397],xm(1,:)==380)=NaN;

% clean up box id (column 5)

bio(bio(:,5)==714319,5)=2;
bio(bio(:,5)==732717,5)=3;

% fill in missing probe

bio(bio(:,6)==-999,6)=4;

% flag additioal autoFLR (by Brian)

ind=bio(:,1);
ind=((ind>=24420243&ind<=24420258)|(ind>=24410202&ind<=24410223)|(ind>=24420070&ind<=24420072));
bio(ind,45)=-100;

% flag some bad obs

bio([3257 4182 4354 4617],45)=-1;

% delete ind

clear ind
