% SeriesGrid.m
%
% SCRIPT:  Creates three interpolated grids of:  temperature, flourometer, and obs data for
%   a series of runs in one plot.  Runs are appended to each other within the same grid, without
%   making any allowance for time between runs.
%
%   Interpolation is in the vertical plane.  A forward speed of 1.3 m/s is assumed.  Grids
%   are displayed one above the other.
%
%	series = 1		Stellwagen run
%	series = 2		Ptown run
%	series = 3		Plymouth run #1
%	series = 4		Fisherman's Ledge
%	series = 5		Dock fine-scale
%	series = 6		Plymouth #2
%   
% Related functions/scripts:  see MultiGrid.m to created one grid set from a mission.
%
%      J. G. Bellingham
%      December 29, 1998

if series == 1, % Stellwagen run...
   names = {'x9827202', 'x9827204', 'x9827206', 'x9827207', 'x9827208'};
   tempo = [6 16.5];
   flouro = [0 1800];
   obso = [0 750]; 
   endurance = [32 45 32 38 181];
   deptho = [0 55];
elseif series == 2, % Ptown run
   names = {'x9826736', 'x9826737', 'x9826801', 'x9826802', 'x9826804', 'x9826805', 'x9826806', 'x9826807'};
   tempo = [6 16.5];
   flouro = [0 2100];
   obso = [0 2000]; 
   endurance = [32 32 32 32 32 32 22 61];
   deptho = [0 35];
elseif series == 3, % Plymouth run #1?
   names = {'x9826812', 'x9826813', 'x9826814', 'x9826815', 'x9826816', 'x9826817', 'x9826818'};
   tempo = [6 16.5];
   flouro = [0 2800];
   obso = [0 2000]; 
   endurance = [32 32 32 32 32 62 32];
   deptho = [0 40];
elseif series == 4, % Fisherman's Ledge
   names = {'x9827017', 'x9827026'};
   tempo = [6 16.5];
   flouro = [0 1900];
   obso = [0 950]; 
   endurance = [65 75];
   deptho = [0 35];
elseif series == 5, % Dock fine-scale survey?
   names = {'X9827017', 'x9827101', 'x9827102'};
   tempo = [6 16.5];
   flouro = [0 1400];
   obso = [0 1000]; 
   endurance = [66 27 70];
   deptho = [0 30];
elseif series == 6, % Plymouth run #2?
   names = {'x9827133', 'x9827134', 'x9827135'};
   tempo = [6 16.5];
   flouro = [0 1800];
   obso = [0 1300]; 
   endurance = [32 32 92];
   deptho = [0 35];
end

cum = cumsum(endurance);
cum(2:length(endurance)+1) = cum/cum(length(endurance));
cum(1) = 0;

close all;
figure;

for indx = 1:length(names),
   % Load run file...
   opts = 0;
   indx, names(indx),
   eval(char(names(indx)), 'err = 1;');
   save(char(names(indx)));
   % load(char(names(indx)));
   
   % 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 indx == 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(indx) 0.701222 0.792625*(cum(indx+1)-cum(indx)) 0.223778]
   axes('position', pos, 'Visible', 'off');
   % if indx > 1, axes(gca, 'YTickLabelMode', 'manual'); end;
   potgrid(1.3* data(:, m_present_time), depth_wc, data(:,water_temp), [round(endurance(indx)*100/150) 50], [1 100]);
   caxis(tempo)
   axis([0 1.3*60*endurance(indx) 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 indx == 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(indx) 0.405611 0.792625*(cum(indx+1)-cum(indx)) 0.223778]
   
   axes('position', pos, 'Visible', 'off');
   % if indx > 1, set(gca, 'YTickLabelMode', 'manual'); end;
   potgrid(1.3*data(:, m_present_time), depth_wc, fl, [round(endurance(indx)*100/150) 50], [1 100]);
   caxis(flouro)
   axis([0 1.3*60*endurance(indx) 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 indx ==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(indx) 0.11 0.792625*(cum(indx+1)-cum(indx)) 0.223778]
      
      axes('position', pos, 'Visible', 'off');
      % if indx > 1, axes('YTickLabelMode', 'manual'); end;
      potgrid(1.3*data(:, m_present_time), depth_wc, data(:,m_bs_voltage), [round(endurance(indx)*100/150) 50], [1 100]);
      caxis(obso)
      axis([0 1.3*60*endurance(indx) 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');