% tc3_regress_fluorintercal.m
% script to regress data from the Liquid Robotics Turner C3's 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);


tc3dat = load('C:\a_data\fluor_intercal_data\turnerC3_gold.txt');
absjday = tc3dat(:,3);
rfu_244 = tc3dat(:,4);
temp_244 = tc3dat(:,5);
pres_244 = tc3dat(:,6);
rfu_241 = tc3dat(:,7);
temp_241 = tc3dat(:,8);
pres_241 = tc3dat(:,9);

ref_year = ones(1,11) * 2008;
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)));
  rfu_244_m(j) = mean(rfu_244(i));
  rfu_241_m(j) = mean(rfu_241(i));
end
rfu_244_m = rfu_244_m';
[B_244,r_244,n,s,C,S,DF,P1,P2,P3_244,yresid] = regress_(chla_m(1:11),rfu_244_m,1,1);

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

figure(1)
plot(chla_m(1:11),rfu_244_m,'r.')
hold on
plot(chla_m(1:11),P3_244,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Turner C3 SN 244')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (Digital Counts)')
text(2,3500,['Slope = ' num2str(B_244(2))])
text(2,3250,['Offset = ' num2str(B_244(1))])
text(2,3000,['R2 = ' num2str(r_244)])
print -dpng 'C:\a_data\fluor_intercal_data\results\tc3_244.png'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rfu_241_m = rfu_241_m';
[B_241,r_241,n,s,C,S,DF,P1,P2,P3_241,yresid] = regress_(chla_m(1:11),rfu_241_m,1,1);

ycalc = (B_241(2)*chla_m) + B_241(1);
ycalc = ycalc(1:11)';
%return
figure(2)
plot(chla_m(1:11),rfu_241_m,'r.')
hold on
plot(chla_m(1:11),P3_241,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Turner C3 SN 241')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (Digital Counts)')
text(2,3500,['Slope = ' num2str(B_241(2))])
text(2,3250,['Offset = ' num2str(B_241(1))])
text(2,3000,['R2 = ' num2str(r_241)])
print -dpng 'C:\a_data\fluor_intercal_data\results\tc3_241.png'
