% scufa_regress_fluorintercal.m
% script to regress data from the Raphe Kudela's SCUFA SN 0239 from the intercalibration experiment, Sept 2010
% data for the experiment were acquired using capture text in hyperterminal
% 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; will not affect the means therefore will not affect the analysis
chla_m = mean(chla,2);


fid = fopen('C:\a_data\fluor_intercal_data\scufa_gold.txt');
count = 1;
while 1
  tline = fgetl(fid);
  if ~ischar(tline), break, end
  i4colon = findstr(':',tline);
  i4slash = findstr('/',tline);
  i4space = findstr(' ',tline);
  i4tunits = findstr('(C)',tline);
  year2d = str2num(tline(i4slash(2)+1:i4slash(2)+2));
  year(count) = yy2yyyy_(year2d);
  month(count) = str2num(tline(1:i4slash(1)-1));
  day(count) = str2num(tline(i4slash(1)+1:i4slash(2)-1));
  hour(count) = str2num(tline(i4colon(1)-2:i4colon(1)-1));
  minute(count) = str2num(tline(i4colon(1)+1:i4colon(2)-1));
  second(count) = str2num(tline(i4colon(2)+1:i4colon(3)-1));
  scufa_rfu(count) = str2num(tline(i4space(2)+1:i4space(3)-1));
  scufa_rfu_tc(count) = str2num(tline(i4space(3)+1:i4space(4)-1));
  scufa_trb(count) = str2num(tline(i4space(4)+1:i4space(5)-1));
  temp(count) = str2num(tline(i4space(5)+1:i4tunits(1)-1));
  count = count + 1;
end
absjday = datenum(year,month,day,hour,minute,second);

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

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

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


plot(chla_m(1:11),scufa_rfu_m,'r.')
hold on
plot(chla_m(1:11),P3,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for Turner Designs SCUFA')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (digital counts)')
text(2,16,'Slope = 0.84')
text(2,15,'Offset = 0.22')
text(2,14,'R2 = .983')

return
figure(1)
plot(absjday,scufa_rfu,'b-')
hold on
plot(absjday,scufa_rfu_tc,'r-')

figure(2)
plot(absjday,scufa_trb,'b-')

figure(3)
plot(absjday,temp,'b-')