clear all

cd C:\All_Work\Ocean\Bowdoin_Buoy_Facility\Data\TSRB
[status,sheetNames] = xlsfinfo('TSRB_BBF_C.xls');%
[Hdat,Htxt] =xlsread('TSRB_BBF_C.xls','TSRB_BBF_C');%
for i=1:10
    T=char(Htxt(1,i+1));,L(i)=str2num(T(4:8));,end
del=diff(Hdat(:,14))

J=find(del>=3481)
size(J)
%J=[1;J];,
nJ=length(J)
J=[J;34948];
YY(1)=2008;,MM(1)=4;,DD(1)=17;,HH(1)=9;
h=[9 10 11 12 13 14 15 22];
k=1;,d=17;,m=4
for i=1:nJ,
    avg(i,:)=median(Hdat(J(i):J(i+1),:));
%SN ED443 ED490 ED555 LU683 LU665 LU555 LU705 LU490 LU443 LU412 T TIME MS
% 1   2    3      4    5     6     7     8      9    10    11   12 13   14
        if i~=1
            k=k+1;,if k==9;,d=d+1;,k=1;
                if d==31;,m=5;,d=1;,end,end
        YY(i)=2008;,HH(i)=h(k);,DD(i)=d;,MM(i)=m;,end
end
%starting on April 17 deployment 9am

Gtavg=[YY' MM' DD' HH'];,Gtavg(:,5:6)=zeros;
Mtavg=datenum(Gtavg)
data=[Mtavg avg(:,2:4) avg(:,11) avg(:,10) avg(:,9)  avg(:,7)  avg(:,6)  avg(:,5)  avg(:,8) avg(:,12)];
data(1:5,:)=[];,Gtavg(1:5,:)=[];
data(:,10)=NaN;    
w3=[L(1:3)];
w7=[L(10) L(9) L(8) L(6) L(5) L(4) L(7)];
%I=find(Gtavg(:,4)==12);
%subplot(3,1,1),plot(w3,data(I,2:4))
%subplot(3,1,2),plot(w7,data(I,5:11))
%for i=1:7,R(:,i)=data(:,4+i)./data(:,3);,end
%subplot(3,1,3),plot(w7,R(I,:))

%figure(2)
%plot(data(:,1),data(:,3),'b',data(:,1),data(:,7)*100,'g')
%datetick('x',6)

%%%add in next data sets
[status,sheetNames] = xlsfinfo('BD02_TSRB.xls');%
[Hdat,Htxt] =xlsread('BD02_TSRB.xls','BD02_TSRB_E');%
for i=1:10
    T=char(Htxt(1,i+1));,L(i)=str2num(T(4:8));,end
Hdat(1,:)=[];
%SN ED443 ED490 ED555 LU683 LU665 LU555 LU705 LU490 LU443 LU412.0 Tw Y  M  D  H  M  S  NaN MILLISEC
% 1  2     3     4      5    6     7     8       9   10   11      12 13 14 15 16 17 18  19   20
del=diff(Hdat(:,16));
Hdat(:,19)=[];

J=find(abs(del)>=1);
J=[1;J;length(Hdat)];
nJ=length(J);

for i=1:nJ-1
    av(i,:)=nanmedian(Hdat(J(i)+1:J(i+1),:));,end
Gtime=av(:,13:18);,Mtime=datenum(Gtime);
dat=[Mtime av(:,2:4) av(:,11) av(:,10) av(:,9)  av(:,7)  av(:,6)  av(:,5)  av(:,8) av(:,12)];
dat=[data;dat];,Gtavg=[Gtavg;Gtime];
%% hourly bin averaged


I=find(Gtime(:,4)==23)
data(I,:)=[];,Gtime(I,:)=[];,daily=[];
del=diff(Gtime(:,3));J=find(del==1);J=[1;J;length(data)];,nJ=length(J)
for i=1:nJ-1
    d=[data(J(i)+1:J(i+1),1:12)];%one days data
    G=[Gtime(J(i)+1:J(i+1),1:6)];%one days data
    I=find(G(:,4)>=10&G(:,4)<=14);
    
    for j=1:length(I),r(j,:)=d(I(j),5:11)/d(I(j),3);,end
    subplot(2,2,1),plot(w3,d(I,2:4))
    subplot(2,2,2),plot(w7,d(I,5:11))
    subplot(2,2,3),plot(w7,r)
    subplot(2,2,4),plot(G(I,4),d(I,2:4),'.')
    
    [k]=input('enter number or vector to discard')
    d(k,:)=NaN;
    daily=[daily;d];
    clf,clear r d
end
I=find(daily(:,7)>2);,daily(I,:)=NaN;%get rid of anomalously high Lus
subplot(2,2,1),plot(w7,daily(:,5:11))
for i=1:7,r(:,i)=daily(:,i+4)./daily(:,3);,end
subplot(2,2,2),plot(w7,r)
I=find(r(:,4)<0.001);,r(I,:)=NaN;
for i=1:7 rn(:,i)=r(:,i)./r4;,end
subplot(2,2,3);,plot(w7,rn)
I=find(rn(:,7)==max(rn(:,7)));,rn(183,:)=NaN;
subplot(2,2,4),plot(w7,rn)

I=find(Gtavg(:,4)<20&dat(:,8)>0);
data=dat(I,:);,Gtime=Gtavg(I,:);
ndata=length(data);
for i=1:ndata,R(i,:)=data(i,5:11)./data(i,4);,nR(i,:)=R(i,:)/max(R(i,4));,end
figure(1)
xmin=datenum(2008,4,1,0,0,0);,xmax=datenum(2008,8,10,0,0,0);
subplot(5,1,1),plot(data(:,1),data(:,12),'.b'),ylabel('T ^oC'),axis([xmin xmax 0 35]),datetick('x',6,'keeplimits')
subplot(5,1,2),plot(data(:,1),data(:,4),'.c-'),ylabel('E_d(555)'),axis([xmin xmax 0 200]),datetick('x',6,'keeplimits')
subplot(5,1,3),plot(data(:,1),data(:,7),'.g-'),ylabel('L_u(555)'),axis([xmin xmax 0 6]),datetick('x',6,'keeplimits')
subplot(5,1,4),plot(data(:,1),R(:,3)./R(:,4),'.m'),ylabel('L_u(555)/E_d(555)'),axis([xmin xmax 0 5]),datetick('x',6,'keeplimits')
subplot(5,2,9),plot(w3,data(:,2:4),'b-'),ylabel('E_d(\lambda)'),xlabel('Wavelength'),axis([400 710 0 200])
subplot(5,2,10),plot(w7,nR(J,:),'g-'),ylabel('R(\lambda)'),xlabel('Wavelength'),axis([400 710 0 1.1])

%% now get one good daily average
del=diff(data(:,1));
J=find(del==1);
J=[1;J;length(data)];
subplot(3,1,1),plot(data(:,1),data(:,7),'.g')
for i=1:nJ-1
    subplot(3,1,2),plot(w3,data(J(i)+1:J(i+1),2:4))
    subplot(3,1,3),plot(w7,R(J(i)+1:J(i+1),1:7))
    subplot(3,1,1),plot(data(:,1),R(:,4),'.g',data(J(i)+1:J(i+1),1),R(J(i)+1:J(i+1),4),'.m')
    pause
    %davg(i,:)=nanmedian(data(J(i)+1:J(i+1),:));
end
