%  Set up to allow batch processing of files.  Called by 'ProcessAll'
%
% J. G. Bellingham


function mabay_process(name, format);

% Function mabay_process(name, format);
%    loads a vehicle run with 'name'
%    format = 1    consise summary (one line)
%             2    list form of consise
%             3    more comprehensive printout to file 'run_summary'

err = 0;
opts = 0;

% -------------------------------------------------------------------------------------
% Check and see if file exists...if not, then return...
file_check = dir(strcat(name, '.dat'));
if length(file_check) == 0
   % fprintf(1, 'doesnt exist %s\n', name);,
   return
elseif file_check.bytes < 2000000
   % fprintf(1, 'not enough bytes %s\n', name);,
   return
end;

% -------------------------------------------------------------------------------------
% Run review...then check to see if m_present_time exists...
eval(name, 'err = 1;');
if err ==1 | ~exist('m_present_time')
   % fprintf(1, 'no %s\n', name);
   return;
end;

% -------------------------------------------------------------------------------------
% Check and make sure the mission is long enough to be interesting...
total_time = (time(length(time)) - time(1))/60;

if total_time < 20
   return;
end;

%       note:  Matlab time  is seconds since 00:00:00 Jan 1, 0000
%              vehicle time is seconds since 00:00:00 Jan 1, 1970

startstr = datestr((start + 62167305600.00)/(24*60*60));

% -------------------------------------------------------------------------------------
% Print out summary information...
%    format = 1    consise summary (one line)
%             2    list form of consise output 
%             3    compute lags...add to concise summary
%             4    do grid plot of T, F, and OBS

if format == 1
   fprintf(1, '%s  %s %3.0f min %3.0f m %2.0f deg', ...
      name, startstr, round(total_time), ...
      round(max(data(:, m_depth))), round(180/pi*median(abs(data(:, m_pitch)))));
   if exist('yoyo'),
      fprintf(1, ' %4.1f \n', max(max(yoyo)));
   else
      fprintf(1, '\n')
   end;
   % Do conductivity lag optimization...
elseif (format == 2) 
   fprintf(1, '%s \n', name);
   fprintf(1, '%s \n', startstr); 
   fprintf(1, 'Length       = %3.0f min \n', round(total_time)); 
   fprintf(1, 'Max depth    = %3.0f m \n', round(max(data(:, m_depth)))); 
   fprintf(1, 'Median pitch =  %2.0f deg \n\n', round(180/pi*median(abs(data(:, m_pitch)))));
end;

if format == 3
   name,
   fid = fopen('run_summary', 'a');
   fprintf(1, 'Opened run_summary %f \n');
   
   % Basic run statistics...
   fprintf(fid, '%s %s ', name, startstr);
   
   fprintf(fid, '%13.6f %8.2f %6.2f %6.2f ', ((start + 62167305600.00)/(24*60*60)), ...
      round(total_time*60), max(data(find(~isnan(data(:, m_depth))), m_depth)), ...
      median(data(find(~isnan(data(:, m_depth))), m_depth)));
   
   % Pitch & yoyo info...
   fprintf(fid, ' %6.3f %4.1f ', ...
      median(abs(180/3.14159*data(find(~isnan(data(:, m_pitch))), m_pitch))), max(max(yoyo)));
   
   % Do conductivity lag optimization using T-S method...
   lag_method = 1;  
   k = [7.4  0.060];  % Use default CC lag value of -0.05788
   LagModel;
   % fprintf(fid, ' %9.4f %9.4f %9.4f ', ctdlag(1), ctdlag(2), ctdlag(3));
   fprintf(fid, ' %9.4f %9.4f ', ctdlag(1), ctdlag(2));

   % Do conductivity lag optimization using anti-spiking method...
   % lag_method = 2;
   % k = -0.06;
   % LagModel;
   % fprintf(fid, ' %9.4f ', ctdlag(1));
  
   % Determine errors for various conductivity lags...
   lag_err0 = LagModelError(ctdlag , data, m_present_time, water_temp, water_cond, m_pressure, 1);   
   lag_err1 = LagModelError([-0.058 3.7 0.09] , data, m_present_time, water_temp, water_cond, m_pressure, 1);
   lag_err2 = LagModelError([-0.058 3.7 0.06], data, m_present_time, water_temp, water_cond, m_pressure, 1);
   lag_err3 = LagModelError([-0.058 7.4 0.09], data, m_present_time, water_temp, water_cond, m_pressure, 1);
   lag_err4 = LagModelError([-0.058 7.4 0.06], data, m_present_time, water_temp, water_cond, m_pressure, 1);
   lag_err5 = LagModelError([-0.058 7.4 0.04], data, m_present_time, water_temp, water_cond, m_pressure, 1);
   lag_err6 = LagModelError([-0.058 11.1 0.06], data, m_present_time, water_temp, water_cond, m_pressure, 1);
   lag_err7 = LagModelError([-0.058 11.1 0.04], data, m_present_time, water_temp, water_cond, m_pressure, 1);
   
   fprintf(fid, ' %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n', ...
      lag_err0, lag_err1, lag_err2, lag_err3, lag_err4, lag_err5, lag_err6, lag_err7);
   
   fclose(fid);
   
   datestr(now, 0)
end;

if format == 4
   % Only produce grid plots if vehicle yoyos...
   if 180/pi*median(abs(data(:, m_pitch))) > 19
      return;
   end;
   
   % multigrid;
   CTDFOPlot;
   set(gcf, 'PaperOrientation', 'landscape', 'PaperPosition', [0.25 0.25 10.5 8]);
   print;
end;
