% hs2_regress_fluorintercal.m
% script to regress data from the dorado's HS2 from the intercalibration experiment, Sept 2010
% data for the experiment were logged internally and then downloaded for processing
% file H2000325gold.dec was created using hobi labs processing software to convert H2000325gold.raw
% 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);


hs2dat = load('C:\a_data\fluor_intercal_data\H2000325gold.dec');
date4dat = sec_date(hs2dat(:,1));                                           % time format for HS2 dec files is seconds since January 1st 1970
time4hs2 = datenum(date4dat(:,1),date4dat(:,2),date4dat(:,3),date4dat(:,4),date4dat(:,5),date4dat(:,6));
snorm1 = hs2dat(:,3);
snorm2 = hs2dat(:,7);
snorm3 = hs2dat(:,11);
snorm1(1:3604) = snorm1(1:3604) * 10;                     % adjust for change in gain setting; not sure if I did this right
snorm2(1:2564) = snorm2(1:2564) * 10;                     % adjust for change in gain setting; not sure if I did this right    

snorm3(1:2564) = snorm3(1:2564) * 10;                     % adjust for change in gain setting; not sure if I did this right    
%snorm3(2565:end) = snorm3(2565:end)/10; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% depends on what gain setting you want the calibration to be valid for?????????
year = date4dat(:,1);
month = date4dat(:,2);
day = date4dat(:,3);
hour = date4dat(:,4);
minute = date4dat(:,5);


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];


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)));
  hs2_rfu_m(j) = mean(snorm3(i));
end

hs2_rfu_m = hs2_rfu_m';
[B,r,n,s,C,S,DF,P1,P2,P3,yresid] = regress_(chla_m(1:11),hs2_rfu_m,1,1);

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


plot(chla_m(1:11),hs2_rfu_m,'r.')
hold on
plot(chla_m(1:11),P3,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for HOBI Labs HS2')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (digital counts)')
text(2,400,['Slope = ' num2str(B(2))])
text(2,350,['Offset = ' num2str(B(1))])
text(2,300,['R2 = ' num2str(r)])
print -dpng 'C:\a_data\fluor_intercal_data\results\hs2.png'


