% wetstar_fluorintercal.m
% script to regress data from the analog WET Labs wetstars as part of the fluoromteter intercalibration experiment 
% September 30, 2010


chla = [17.535 20.8082 18.4702;8.4168 6.6633 7.5985;4.0915 3.79925 3.96625;2.51335 3.2231 2.3213;1.6032 1.92885 1.87875;1.09385 .9853 .9853;.7849 .7849 .82665;.384935 .37241 .35404;.19706 .17702 .18704;.116065 .104375 .084335;.069305 .05177 .05177;.019038 .013694 .023965];
% two of these values were missing so I replicated the value from another sample at the same dilution; will not affect the means therefore will not affect the analysis
chla_m = mean(chla,2);


wsdat = load('C:\a_data\fluor_intercal_data\wetstar_gold.txt');
absjday = wsdat(:,3);
rv_362P = wsdat(:,4);
rv_1064P = wsdat(:,5);
rv_221 = wsdat(:,6);
rv_234 = wsdat(:,7);


ref_year = ones(1,11) * 2010;
ref_month = ones(1,11) * 9;
ref_day = ones(1,11) * 29;
ref_hour = [15 16 16 16 17 17 17 18 18 18 19];
ref_minute = [55 13 33 52 10 31 48 9 47 56 13];


[year,month,day,hour,minute,second] = datevec(absjday);


for j = 1:11
  i = find((year == ref_year(j)) & (month == ref_month(j)) & (day == ref_day(j)) & (hour == ref_hour(j)) & (minute == ref_minute(j)));
  rv_362P_m(j) = mean(rv_362P(i));
  rv_1064P_m(j) = mean(rv_1064P(i));
  rv_221_m(j) = mean(rv_221(i));
  rv_234_m(j) = mean(rv_234(i));
end

rv_362P_m = rv_362P_m';
[B_362P,r_362P,n,s,C,S,DF,P1,P2,P3_362P,yresid] = regress_(chla_m(1:11),rv_362P_m,1,1);

ycalc = (B_362P(2)*chla_m) + B_362P(1);
ycalc = ycalc(1:11)';

figure(1)
plot(chla_m(1:11),rv_362P_m,'r.')
hold on
plot(chla_m(1:11),P3_362P,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Wetstar 362P')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (Volts)')
text(2,4,'Slope = 0.1576')
text(2,3.75,'Offset = 1.4889')
text(2,3.5,'R2 = .937')
%print -dpng 'C:\a_data\fluor_intercal_data\results\ws362P.png'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rv_1064P_m = rv_1064P_m';
[B_1064P,r_1064P,n,s,C,S,DF,P1,P2,P3_1064P,yresid] = regress_(chla_m(1:11),rv_1064P_m,1,1);

ycalc = (B_1064P(2)*chla_m) + B_1064P(1);
ycalc = ycalc(1:11)';

figure(2)
plot(chla_m(1:11),rv_1064P_m,'r.')
hold on
plot(chla_m(1:11),P3_1064P,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Wetstar 1064P')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (Volts)')
text(2,4,'Slope = 0.1848')
text(2,3.75,'Offset = 0.8227')
text(2,3.5,'R2 = .974')
print -dpng 'C:\a_data\fluor_intercal_data\results\ws1064P.png'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rv_221_m = rv_221_m';
[B_221,r_221,n,s,C,S,DF,P1,P2,P3_221,yresid] = regress_(chla_m(1:11),rv_221_m,1,1);

ycalc = (B_221(2)*chla_m) + B_221(1);
ycalc = ycalc(1:11)';

figure(3)
plot(chla_m(1:11),rv_221_m,'r.')
hold on
plot(chla_m(1:11),P3_221,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Wetstar 221')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (Volts)')
text(2,3.5,['Slope = ' num2str(B_221(2))])
text(2,3.25,['Offset = ' num2str(B_221(1))])
text(2,3,['R2 = ' num2str(r_221)])
print -dpng 'C:\a_data\fluor_intercal_data\results\ws221.png'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rv_234_m = rv_234_m';
[B_234,r_234,n,s,C,S,DF,P1,P2,P3_234,yresid] = regress_(chla_m(1:11),rv_234_m,1,1);
%pzx = polyfit(chla_m(1:11),rv_234_m,1)

ycalc = (B_234(2)*chla_m) + B_234(1);
ycalc = ycalc(1:11)';

figure(4)
plot(chla_m(1:11),rv_234_m,'r.')
hold on
plot(chla_m(1:11),P3_234,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Wetstar 234')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (Volts)')
text(2,4,['Slope = ' num2str(B_234(2))])
text(2,3.75,['Offset = ' num2str(B_234(1))])
text(2,3.5,['R2 = ' num2str(r_234)])
print -dpng 'C:\a_data\fluor_intercal_data\results\ws234.png'



