%**************************************************************************
% Copyright 1998 MBARI
%**************************************************************************
% Summary  : MotionPak data plotting routine
% Filename : mpData.m
% Author   : Michael B. Matthews
% Project  : New ROV
% Version  : 2.6
% Created  : 12.8.98
% Modified : 27.10.98	V2.4 added variable number of signals
%          : 23.7.99    V2.5 added command-line options 
%          : 28.7.99    V2.6 added -f, -t command-line options 
% Notes    : Options:
%          : -fill	fill in nan data for plotting
%          : -deg	convert data to degrees
%          : -menubar	plot with menubar for printing
%          : -save 	save plots to files
%          : -plot	plots figures to tahoe
%          : -plus 	plot with '+' trace option
%          : -fN        plot from N seconds 
%          : -tN        plot to N seconds 
%          : -uB	plot with upper limit B
%          : -lB	plot with lower limit B
%          : -help	lists options
%**************************************************************************

function x = mpData(varargin)

disp(['mpData - MotionPak Data Plotting Routine, Version 2.5']);

R2D = 180/pi;
N = length(varargin);

file = [];
menubar = 0; 
save = 0; 
plt = 0; 
fill = 0;
deg = 0;
plus = 0;
ts = 1;
tf = 0;
help = 0;
uBound = nan;
lBound = nan;

for i=1:N
  s = varargin{i};
  if s(1) == '-'
    s = s(2:end);
    switch s
      case 'menubar'
        menubar = 1;
      case 'save'
        save = 1;
      case 'plot'
        plt = 1;
      case 'fill'
        fill = 1;
      case 'deg'
        deg = 1;
      case 'plus'
        plus = 1;
      case 'help'
        help = 1;
      case 'h'
        help = 1;
      otherwise
        if s(1) == 'f', ts = str2num(s(2:end)); end;
        if s(1) == 't', tf = str2num(s(2:end)); end;
        if s(1) == 'u', uBound = str2num(s(2:end)); end;
        if s(1) == 'l', lBound = str2num(s(2:end)); end;
    end
  else
    file = s;
  end;
end

if help
  disp('Usage:');
  disp('mpData [-option1 -option2 ... -optionN [filename (mpData)]]');
  disp('Options:');
  disp('        -fill        fill in nan gaps with lines');
  disp('        -save        save plots in files');
  disp('        -plot        plots figures to tahoe');
  disp('        -plus        plot with + symbols');
  disp('        -deg         convert to degrees');
  disp('        -menubar     plot with menubar for printing');
  disp('        -fN          plot from N seconds');
  disp('        -tN          plot to N seconds');
  disp('        -lB          plot with lower limit B');
  disp('        -uB          plot with upper limit B');
else

if ~isstr(file)
  file = input('input file: ','s');
end
if isempty(file) file = 'mpData'; end

[x,p,n,s] = mpRead([file]);
disp(['numbr of signals ' int2str(n)]);
disp(['file length ' int2str(p) '  minutes']);
disp(['plotting data, press return to exit']);
disp(['x dimension (' int2str(size(x,1))  ','int2str(size(x,2)) ')']);
t = x(:,1) - x(1,1); 
dt = t(2) - t(1); 
if dt == 0, dt = 1.0; t = 0:length(t)-1; end;
ts = floor(ts/dt);
if tf == 0, tf = length(t); else tf = floor(tf/dt); end;
x = x(ts:tf,:);
t = t(ts:tf);

position = [0.0075,0.5675,0.4,0.4];

for i=2:n
  k = find(x(:,i) == -99999);
  x(k,i) = nan;
  k = find(~isnan(x(:,i)));
  if length(k) == 0
    disp(['no ' s(i,:) ' data']);
  else
    if menubar
      figure(					  ...
	     'Units'	, 'normal'		, ...
             'Name' 	,  s(i,:)		, ...
	     'Position'	,  position		  ...
	    );
    else
      figure(					  ...
	     'Units'	, 'normal'		, ...
             'Name' 	,  s(i,:)		, ...
	     'Position'	,  position		, ...
             'MenuBar'	, 'none'		  ...
	    );
    end
    if fill
      if deg
        if plus 
          plot(t(k),R2D*x(k,i),'+'); grid; 
        else
          plot(t(k),R2D*x(k,i)); grid; 
        end
      else
        if plus 
          plot(t(k),x(k,i),'+'); grid; 
        else
          plot(t(k),x(k,i)); grid; 
        end
      end
    else
      if deg
        if plus 
   	  plot(t,R2D*x(:,i),'+'); grid; 
        else
   	  plot(t,R2D*x(:,i)); grid; 
        end
      else
        if plus 
   	  plot(t,x(:,i),'+'); grid; 
        else
   	  plot(t,x(:,i)); grid; 
        end
      end
    end
    title([file ', ' s(i,:)]);
    xlabel('time (sec)'); ylabel(s(i,:));
    disp(['mean ' s(i,:) ' ' num2str(mean(x(k,i)))]);
    a = axis;
    if ~isnan(uBound), a = [a(1:3) uBound]; end;
    if ~isnan(lBound), a = [a(1:2) lBound a(4)]; end;
    axis(a);
    position = position + [0.025 -0.028 0 0];
    if save
      filename = [file 'Plot' int2str(i-1) '.ps'];
      disp(['saving ' filename]);
      print(gcf, '-dpsc', filename);
    end
    if plt
      disp(['plotting Figure 'int2str(i-1)]);
      print(gcf, '-dpsc', 'tmpFig.ps');
      eval('!lp -opostscript -onb -dtahoe tmpFig.ps');
    end
  end
end

pause;

for i=2:n, close; end

end