% Creates a series of plots inside the subplot limits...
%
% J. G. Bellingham

% Stellwagen run...
names = {'x9827202', 'x9827204', 'x9827206', 'x9827207', 'x9827208'};
YTicko = [0 5 10 15 20 25 30 35 40 45 50 55];
XTicko = [0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000];
tempo = [6 16];
flouro = [0 1800];
obso = [0 750]; 
endurance = [32 45 32 38 181];
deptho = [0 55];
cum = cumsum(endurance);
cum(2:length(endurance)+1) = cum/cum(length(endurance));
cum(1) = 0;

close all;
figure;

for i = 1:length(names),
   % Load run file...
   opts = 0;
   % eval(char(names(i)), 'err = 1;');
   % save(char(names(i)));
   load(char(names(i)));
   
   % Correct depth for pitch angle.  Assume sensors 40 cm forward of depth sensor.
   depth_wc = data(:, m_depth) - 0.40 * sin(data(:, m_pitch));
   
   % -------------------------------------------------------------------
   % Plot temperature...
   if i == 1,
      axes('position', [0.7 0.701222 0.25 0.223778], 'Visible', 'off');
      caxis(tempo);
      colorbar;
      
      axes('position', [0.10 0.701222 0.792625 0.223778]);      
      axis([0 1.3*60*sum(endurance)/1000 deptho(1) deptho(2)])
      title('Temperature ');
      set( gca, 'YDir', 'rev');
      drawnow;
   end;
   
   % 0.13 0.701222 0.662625 0.223778
   % 0.13 0.405611 0.662625 0.223778
   pos = [0.10+0.792625*cum(i) 0.701222 0.792625*(cum(i+1)-cum(i)) 0.223778]
   axes('position', pos, 'Visible', 'off');
   % if i > 1, axes(gca, 'YTickLabelMode', 'manual'); end;
   potgrid(1.3* data(:, m_present_time), depth_wc, data(:,water_temp), [round(endurance(i)*100/150) 50], [1 100]);
   caxis(tempo)
   axis([0 1.3*60*endurance(i) deptho(1) deptho(2)])
   set(gca, 'Visible', 'off');
   
   % -------------------------------------------------------------------
   % Lag fluormetry data.... 15 time cycle lag found to be fairly good estimate...
   dn = 15; n = length(data); fl = data((dn+1):n,m_fl_voltage); fl((n-dn):n) = 0;
   
   if i == 1,
      axes('position', [0.7 0.405611 0.25 0.223778], 'Visible', 'off');
      caxis(flouro);
      caxis
      colorbar;
      
      axes('position', [0.10 0.405611 0.792625 0.223778]);      
      axis([0 1.3*60*sum(endurance)/1000 deptho(1) deptho(2)])
      title('Fluorometer');
      set( gca, 'YDir', 'rev'); 
      drawnow;
   end;
   
   pos = [0.10+0.792625*cum(i) 0.405611 0.792625*(cum(i+1)-cum(i)) 0.223778]
   
   axes('position', pos, 'Visible', 'off');
   % if i > 1, set(gca, 'YTickLabelMode', 'manual'); end;
   potgrid(1.3*data(:, m_present_time), depth_wc, fl, [round(endurance(i)*100/150) 50], [1 100]);
   caxis(flouro)
   axis([0 1.3*60*endurance(i) deptho(1) deptho(2)])
   set(gca, 'Visible', 'off');
   
   % -------------------------------------------------------------------
   % Lag fluormetry data.... 15 time cycle lag found to be fairly good estimate...
   dn = 15; n = length(data); fl = data((dn+1):n,m_fl_voltage); fl((n-dn):n) = 0;
   
   if i == 1,
      axes('position', [0.7 0.405611 0.25 0.223778], 'Visible', 'off');
      caxis(flouro);
      caxis
      colorbar;
      
      axes('position', [0.10 0.405611 0.792625 0.223778]);      
      axis([0 1.3*60*sum(endurance)/1000 deptho(1) deptho(2)])
      title('Fluorometer');
      set( gca, 'YDir', 'rev'); 
      drawnow;
   end;
   
   pos = [0.10+0.792625*cum(i) 0.405611 0.792625*(cum(i+1)-cum(i)) 0.223778]
   
   axes('position', pos, 'Visible', 'off');
   % if i > 1, set(gca, 'YTickLabelMode', 'manual'); end;
   potgrid(1.3*data(:, m_present_time), depth_wc, fl, [round(endurance(i)*100/150) 50], [1 100]);
   caxis(flouro)
   axis([0 1.3*60*endurance(i) deptho(1) deptho(2)])
   set(gca, 'Visible', 'off');
   
   % -------------------------------------------------------------------
   % Plot OBS data if it is not saturated...
   if median(data(find(~isnan(data(:,m_bs_voltage))) ,m_bs_voltage)) < 4000
      if i ==1,
         axes('position', [0.7 0.11 0.25 0.223778], 'Visible', 'off');
         caxis(obso);
         colorbar;
         
         axes('position', [0.10 0.11 0.792625 0.223778]);      
         axis([0 1.3*60*sum(endurance)/1000 deptho(1) deptho(2)])
         title('Optical Backscatter');
         set( gca, 'YDir', 'rev'); 
         drawnow;
      end;
      
      pos = [0.10+0.792625*cum(i) 0.11 0.792625*(cum(i+1)-cum(i)) 0.223778]
      
      axes('position', pos, 'Visible', 'off');
      % if i > 1, axes('YTickLabelMode', 'manual'); end;
      potgrid(1.3*data(:, m_present_time), depth_wc, data(:,m_bs_voltage), [round(endurance(i)*100/150) 50], [1 100]);
      caxis(obso)
      axis([0 1.3*60*endurance(i) deptho(1) deptho(2)])
      set(gca, 'Visible', 'off');
   end;
   
   drawnow;
end;

% Put labels on figure...
h = axes('Position', [0 0 1 1], 'Visible', 'off');
set(gcf, 'currentaxes', h);
text(0.05, 0.97, strcat(run_name, 'OC 330'));
text(0.80, 0.97, 'MIT AUV Lab');
text(0.75, 0.03, 'J. G. Bellingham');