
clear all
close all
%sullivan's paper
tmp = xlsread('Sullivan_etal_2006_instrumentspecific.xls');
figure;
subplot(2,2,1)
plot(tmp(:,1),tmp(:,2));
subplot(2,2,2)
plot(tmp(:,1),tmp(:,4)*250);
subplot(2,2,3)
plot(tmp(:,1),tmp(:,6)*250);

[t,dat,wla,wlc]=rd_wetview_acs('c0_07092013_T23p0.dat');
c_CDOM=median(dat(:,[1:84]));


[t,dat,wla,wlc]=rd_wetview_acs('a1_07092013_T22p8.dat');
a_CDOMg=median(dat(:,[85:168]));


[t,dat,wla,wlc]=rd_wetview_acs('a2_07092013_T23p1.dat');
a_CDOM=median(dat(:,[85:168]));

phi_T=interp1(tmp(:,1),tmp(:,2),wla,'linear');
phi_Sc=interp1(tmp(:,1),tmp(:,4),wla,'linear');
phi_Sa=interp1(tmp(:,1),tmp(:,6),wla,'linear');

    opts = optimset('fminsearch');      
    opts = optimset(opts,'MaxIter',20000000); 
    opts = optimset(opts,'MaxFunEvals',20000);
    opts = optimset(opts,'TolX',1e-8);
    opts = optimset(opts,'TolFun',1e-8);
    
    I = find(wla>=450);  % spectral srange for optimization (710 to 750nm)
   
    delT = 0;
    delS = 200;
    amp = a_CDOM(20);
    offset = 0
    %slope = 0.014;

            % minimization routine
   [x1] = fminsearch(@f_TS, [delT, delS, amp, offset], opts, c_CDOM(I), phi_T(I), phi_Sc(I), wla(I));
   [x2] = fminsearch(@f_TS, [delT, delS, amp, offset], opts, a_CDOM(I), phi_T(I), phi_Sa(I), wla(I));
   [x3] = fminsearch(@f_TS, [delT, delS, amp, offset], opts, a_CDOMg(I), phi_T(I), phi_Sa(I), wla(I));

figure; 
plot(wla,c_CDOM,'r',wla,x1(4)+ x1(3).*exp(-0.015.*(wla-450))+ x1(1).*phi_T+x1(2).*phi_Sc,'b');
hold on
plot(wla,a_CDOM,':r',wla,x2(4)+x2(3).*exp(-0.015.*(wla-450))+ x2(1).*phi_T+x2(2).*phi_Sa,':b');
plot(wla,a_CDOMg,'.-r',wla,x3(4)+x3(3).*exp(-0.015.*(wla-450))+ x3(1).*phi_T+x3(2).*phi_Sa,'.-b');
hold off
x1
x2 
x3



