% ecopuck_regress_fluorintercal.m
% script to regress data from the LRAUV ecopuck BB2FLMBA SN 634 from the intercalibration experiment, Sept 29, 2010
% Luke Beatman; September 30, 2010
%
% 
% Data from the ecopuck was acquired by use of text capture in Hyperterminal.  Because the ecopuck does not transmit a time, I used the time of the first sample from the SCUFA
% as a proxy.  The data from the SCUFA was also acquired using text capture in Hyperterminal.  The text capture for both instruments was started simultaneously + or - a couple seconds.
% To set an end time for the data from the ecopuck, I recorded the time in the SCUFA Hyperterminal window at the moment when I stopped the text capture for the Ecopuck.  The time vector
% for the ecopuck was then created below using Matlab's function linspace.  This assumes an even sampling interval for the ecopuck.

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\ecopuck_gold.txt');
count = 1;
while 1
  tline = fgetl(fid);
  if ~ischar(tline), break, end
  tline = strrep(tline,':',' ');
  tline = strrep(tline,'/',' ');
  tdat = str2num(tline);
  instdat = tdat(7:end);
  ch1_nu(count)= instdat(1);
  sig_470(count) = instdat(2);
  ch2_nu(count) = instdat(3);
  sig_650(count) = instdat(4);
  ch2_nu(count) = instdat(5);
  sig_695(count) = instdat(6);
  thermistor(count) = instdat(7);
  count = count + 1;
end
fclose(fid);
[r,c] = size(sig_470);
eco_start = datenum(2010,9,29,15,39,1);                 
eco_stop = datenum(2010,9,29,19,20,3);
eco_time = linspace(eco_start,eco_stop,c);

[y,m,d,h,mi,s] = datevec(eco_time);

sig_470 = sig_470 - 54;                     % Subtract dark counts that were determined by WET Labs calibration on 8/27/2010
sig_650 = sig_650 - 53;                     % pretty sure I want to do this??
sig_695 = sig_695 - 56;

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((y == ref_year(j)) & (m == ref_month(j)) & (d == ref_day(j)) & (h == ref_hour(j)) & (mi == ref_minute(j)));
  sig470_m(j) = mean(sig_470(i));
  sig650_m(j) = mean(sig_650(i));
  sig695_m(j) = mean(sig_695(i));
end
sig695_m = sig695_m'

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

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


plot(chla_m(1:11),sig695_m,'r.')
hold on
plot(chla_m(1:11),P3,'b-')
%plot(chla_m(1:11),ycalc,'k*')
title('Calibration for LRAUV ECOpuck')
xlabel('Extracted Chlorophyll (micrograms ChlA/L)')
ylabel('Instrument Response (digital counts)')
text(2,1200,'Slope = 64.7')
text(2,1150,'Offset = 7')
text(2,1100,'R2 = .984')

return

figure(1)
xlabel('Dilution Number')
ylabel('Mean Digital Count')
plot(sig470_m,'b.-')
hold on
plot(sig650_m,'g.-')
plot(sig695_m,'r.-')
title('Mean digital count at Concentrations')

figure(2)
plot(eco_time,sig_470,'b.-')
hold on
plot(eco_time,sig_650,'g.-')
plot(eco_time,sig_695,'r.-')
title('Raw output of Instrument vs Time')
legend('Backscatter at 470','Backscatter at 650','Fluorescence at 695')

figure(3)
plot(eco_time,thermistor)
title('Thermistor Output')
