% 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
%
%
% Modification: Jim's script plots file by file. This is too complex. I put 
% files in one big array sequentially, and then plot once. It's much 
% simpler.
%

% NOTE: these series have been carefully checked. GOOD!!

close all

fprintf('NOTE: use right time resolution dt!! Specify in the code.')
dt = 1;
spauv = 1.2;

% Always run this
x98_colnum

% NOTE: these series have been carefully checked. GOOD!!

if series == 1, % Ptown run
   names = {'x9826737_newtime', 'x9826801_newtime', 'x9826802_newtime', 'x9826804_newtime', 'x9826805_newtime', 'x9826806_newtime', 'x9826807_newtime'};
%   names = {'x9826737_newtime' , 'x9826801_newtime'};
   tempo = [6 18];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [500 1800];
   obso = [0 4000]; 
   deptho = [0 35];
   plot_obs = 'N';
elseif series == 2, % Plymouth run #2?
   names = {'x9827133_newtime', 'x9827134_newtime', 'x9827135_newtime'};
   tempo = [6 18];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [0 2000];
   obso = [0 1300]; 
   deptho = [0 35];
   plot_obs = 'Y';
elseif series == 3, % Stellwagen run...
   names = {'x9827204_newtime', 'x9827206_newtime', 'x9827207_newtime', 'x9827208_newtime'};
%   names = {'x9827204', 'x9827206'};
   tempo = [5 16];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [0 1500];
   obso = [0 600]; 
   deptho = [0 55];
   plot_obs = 'Y';
elseif series == 4, % Barnstable run
   names = {'x9827214_newtime', 'x9827215_newtime'};
   tempo = [6 18];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [0 1500];
   obso = [0 1600]; 
   deptho = [0 25];
   plot_obs = 'Y';
elseif series == 5, % Plymouth run #1?
   names = {'x9826814_newtime', 'x9826815_newtime', 'x9826816_newtime', 'x9826817_newtime', 'x9826818_newtime'};
   tempo = [6 18];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [0 3200];
   obso = [0 2000]; 
   deptho = [0 40];
   plot_obs = 'Y';
elseif series == 6, % Fisherman's Ledge
   names = {'x9827017_newtime', 'x9827026_newtime'};
   tempo = [6 18];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [0 2000];
   obso = [0 950]; 
   deptho = [0 35];
   plot_obs = 'Y';
elseif series == 7, % Dock fine-scale survey?
   names = {'x9827017_newtime', 'x9827101_newtime', 'x9827102_newtime'};
   tempo = [6 18];
   salo = [30.5 32];
   denso = [22 26];
   flouro = [0 1500];
   obso = [0 1000]; 
   deptho = [0 30];
   plot_obs = 'Y';
end

% Xuri: initialize total time series
temp_tot = [];
fl_tot = [];
bac_tot = [];
sal_tot = [];
dens_tot = [];
depth_tot = [];
dist_tot = [];  
time_tot = [];

for indx = 1:length(names),
   % Load run file...
   opts = 0;
   indx, names(indx),

%%%%%%%%%%%%%%%%%John: change to your own directory!!!!!!!!!!!!!

   fn9 = ['/home/xuri/auv/data-original/tmp1-dir/data/' char(names(indx)) '_red.mat']
   eval(['load ' fn9])
%   eval(char(names(indx)), 'err = 1;');
   
%    data = average(data,5);

   % do not use first and last ~45 seconds
   offset = floor(45/dt);
   len = length(data(:,1));
   data = data(offset:len-offset,:);

   time = data(:, m_present_time);
   temp = data(:,water_temp);
   cond = data(:,water_cond);
   fl = data(:,m_fl_voltage);
   bac = data(:,m_bs_voltage);
   press = data(:, m_pressure);
   pitch = data(:, m_pitch);

% calculate distance from the initial location for the run
% NOTE: x and y are in meters already in the original datafiles. Convert to km
% This is right way to calculate distances
   dx = diff(data(:,m_pos_e))*0.001*sin(mean(data(:,m_pos_n))*pi/180);
   dy = diff(data(:,m_pos_n))*0.001;
   dd = (dx.^2+dy.^2).^0.5;

%  add distance of the last data file to get total distance
   if (indx == 1)
     dist = [0; cumsum(dd)];
   else
     dist = [0; cumsum(dd)] + dist_tot(length(dist_tot));
   end

   % Correct depth for pitch angle.  Assume sensors 40 cm forward of depth sensor.
%Xuri: should be +0.4*sin..., ont -0.4*sin

   depth_wc = data(:, m_depth) + 0.40 * sin(pitch);
   
   fname = char(names(indx));
   fn_num = str2num(fname(2:8));
   k = find_para(fn_num,'sal')
   sal = cal_sal(time*24*3600,temp,cond,press,pitch,k);
   
   k = find_para(fn_num,'flu')
   ok = find(~isnan(fl));
   fl_in = interp1q(time(ok)*24*3600,fl(ok),time*24*3600-k(1));

   k = find_para(fn_num,'bac')
   ok = find(~isnan(bac));
   bac_in = interp1q(time(ok)*24*3600,bac(ok),time*24*3600-k(1));

%Xuri: add density
   dens = sw_dens(sal, temp, 10*press)-1000;

   depth_tot = [depth_tot; depth_wc];
   temp_tot = [temp_tot; temp];
   fl_tot = [fl_tot; fl_in];
   bac_tot = [bac_tot; bac_in];
   sal_tot = [sal_tot; sal];
   dens_tot = [dens_tot; dens];
   dist_tot = [dist_tot; dist];

% add time of the last data file to get total time as distance
   if (indx == 1)
     time_tot = (time-time(1))*24;
   else
     time_tot = [time_tot; (time-time(1))*24+time_tot(length(time_tot))];
   end

   clear data time temp depth_wc fl_in bac_in sal dens dist
end


%close all;

f=figure('units','inches','position',[.3,.3,7.9,10.4],'paperposition',[.3,.3,7.9,10.4],'paperorientation','portrait')

% --------
% Plot temperature...
subplot(5,1,1)
potgrid(dist_tot, depth_tot, temp_tot, [100 50], [1 100]);
caxis(tempo)
colorbar
limx = xlim;
limy = ylim;
axis([limx(1) limx(2) deptho(1) deptho(2)])

file_first = char(names(1));
file_last = char(names(length(names)));

% Put labels on figure...
text(limx(1), limy(1)-(limy(2)-limy(1))*1.0, [file_first ' - ' file_last]);
text(limx(2)+(limx(2)-limx(1))*0.3, limy(1)-(limy(2)-limy(1))*0.2, ['MassBay 98: Contours of AUV Data, Section ' num2str(series)],'horizontalal', 'center','fontsize',13)
title('Temperature')

% --------
% Plot salinity: correct time lag with Jim's correction paras
subplot(5,1,2)
potgrid(dist_tot, depth_tot, sal_tot, [100 50], [1 100]);
caxis(salo)
colorbar
limx = xlim;
limy = ylim;
axis([limx(1) limx(2) deptho(1) deptho(2)])

title('Salinity')


% --------
% Plot density: correct time lag with Jim's correction paras
subplot(5,1,3)
potgrid(dist_tot, depth_tot, dens_tot, [100 50], [1 100]);
caxis(denso)
colorbar
limx = xlim;
limy = ylim;
axis([limx(1) limx(2) deptho(1) deptho(2)])

ylabel ('Depth (m)')
title('Density')


% --------
% Plot fluorometery: correct time lag for fluorescence. find the lag 
% by calling find_para.m
subplot(5,1,4)
potgrid(dist_tot, depth_tot, fl_tot, [100 50], [1 100]);
caxis(flouro)
colorbar
limx = xlim;
limy = ylim;
axis([limx(1) limx(2) deptho(1) deptho(2)])

% Put labels on figure...
%text(limx(2), limy(2)+(limy(2)-limy(1))*0.25, 'J. G. Bellingham, MIT/AUV Lab','horizontalal','right');
%text(limx(2), limy(2)+(limy(2)-limy(1))*0.32, 'X. Yu, UCSB/Ocean Physics Lab','horizontalal','right');
title('Fluoremeter')


% --------
% Plot backscattering: correct time lag for fluorescence. find the lag 
% by calling find_para.m
%
%Xuri: OBS data are not good for some sections. So we use plot_obs to make and 
%	not make this panel

if (plot_obs == 'Y' | plot_obs == 'y')
  subplot(5,1,5)
  potgrid(dist_tot, depth_tot, bac_tot, [100 50], [1 100]);
  caxis(obso)
  colorbar
  limx = xlim;
  limy = ylim;
  axis([limx(1) limx(2) deptho(1) deptho(2)])

% Put labels on figure...
%text(limx(2), limy(2)+(limy(2)-limy(1))*0.25, 'J. G. Bellingham, MIT/AUV Lab','horizontalal','right');
%text(limx(2), limy(2)+(limy(2)-limy(1))*0.32, 'X. Yu, UCSB/Ocean Physics Lab','horizontalal','right');
  title('Backscattering')

end

xlabel('Distance from initial Position (km)')
xax = get(gca, 'xticklabel');
lxax = length(xax);

for i = 1:lxax
  xax_num = str2double(xax(i,:))
  r = find(abs(dist_tot-xax_num)<dt*spauv*0.001)
  t_hr = time_tot(r(1))*1;  %it's in hours already
% keep two digit after decimal point
  t_hr = floor(t_hr*100)/100;
  tick_str = num2str(t_hr);
  text(xax_num,deptho(2)+(deptho(2)-deptho(1))*0.35, tick_str,'horizontalal','center')
end
text(limx(1)+(limx(2)-limx(1))*0.5,deptho(2)+(deptho(2)-deptho(1))*0.45,'Time from initial point (hr)','horizontalal','center')

clear depth_tot temp_tot fl_tot bac_tot sal_tot dens_tot dist_tot time_tot
