function varargout = plotp(varargin)
% ------------------------------------------------------------------
% Plotting of log data. 
%
% SYNOPSIS:
%
%   plotp(move)
%
%      Catalog content of 'DATA' acccording to 'move' string. Possible
%      moves are:
%
%         '-fwd'     : forward 0.1 * NPLOTP pings
%         '-ffwd'    : forward 0.5 * NPLOTP pings
%         '-fffwd'   : forward 0.9 * NPLOTP pings
%         '-rew'     : rewind  0.1 * NPLOTP pings
%         '-rrew'    : rewind  0.5 * NPLOTP pings
%         '-rrrew'   : rewind  0.9 * NPLOTP pings
%         '-pause'   : toggle scrolling through the pings
%         '-refresh' : redisplay current pings
%
%   plotp(start, vars)
%
%      Plot log pings, starting from ping number 'start', and
%      displaying only pings specified in 'vars'. These must have
%      the following synthaxe:
%
%         [x, y, y, ...], [x, y, y, ...], ...
%
%      which will make time serie plots for variable numbers 'y' vs.
%      variable numbers 'x'. If 'vars' is absent, variables are taken
%      from the global variable PLOTP_VARS (which must be set before).
%
%   plotp(0)
%
%      Reset global variables from `plotp', and exit `plotp'.
%
%   plotp
%
%      As plotp('-refresh'), but the associated uicontrol to 'plotp' is
%      not displayed. This is usefull when calling `plotp' from another
%      uicontrol. The global variable DATA must be set within the calling
%      function. 
%
%   [pos, np, m] = plotp('-sizes')
%   [pos, np, m] = plotp('-sizes', n)
%
%      To get information about plot sizes and positions, or the same
%      information about subplot number 'n'. On output, 'pos' is a 
%      column vector containing the 'left', 'bottom', 'width' and
%      'height' positions and dimensions in normalized units of the
%      specified plot (or the top one - if none is specified), 'np' is
%      is the number of subplots and 'm' is the margin used.
%
% Normally, an uicontrol will be initialized to control cataloging of
% of log pings. All cataloging commands should be send to 'plotp' through
% the button window, except for starting 'plotp'.
%
% ------------------------------------------------------------------
%Time:<Last updated on 98/10/29 at 17:22:39 by jaccard@pc34.gfi.uib.no>
%File:</home1/jaccard/data1/codas3/gfi/matlab/matlab50/scan_bin/plotp.m>

scanglob;

%--- Plot reset:
if (nargin == 1) & ~isstr(varargin{1}) & (varargin{1} == 0),
  plotp_quit;
  return;
end

%--- Only get plot sizes:
if (nargin > 0) & isstr(varargin{1}) & strcmp(varargin{1}, '-sizes'),
  if nargin > 1,
	 n = varargin{2};
	 [pos, np, m] = plotp_sizes(n);
  else
	 [pos, np, m] = plotp_sizes;
  end
  if nargout > 0,
	 varargout{1} = pos;
  end
  if nargout > 1,
	 varargout{2} = np;
  end
  if nargout > 2,
	 varargout{3} = m;
  end
  return;
end
 
%--- Default:
move = '-refresh';

%--- Move argument:
if (nargin == 1) & isstr(varargin{1}),
  move = varargin{1};
end
if nargin == 0,
  move = '-refresh';
  if isempty(DATA),
	 fprintf('\n\n\tGlobal variable DATA is empty.\n\n\n');
	 return;
  end
end

%--- Initial arguments:
if (nargin > 0) & ~isstr(varargin{1}) & (varargin{1} > 0),
  PLOTP_STARTP = varargin{1};
end

%--- Get all variables to plot:
if nargin > 1,
  PLOTP_VARS = {};
  for i=2:nargin
	 PLOTP_VARS{i-1} = varargin{i};
  end
end

%--- Control that plot variables are defined:
if isempty(PLOTP_VARS),
  error('No plot variables defined');
end

%--- Initialize plot:
if isempty(PLOTP_UIC_),
  plotp_init;
end

%--- Loop through the data:
done = 0;
while ~done,

  %--- Get the start ping number:
  switch move
	 case '-start',
		PLOTP_STARTP = 1;
		PLOTP_SCROLL = 0;
	 case '-refresh',
		PLOTP_STARTP;
	 case '-fwd',
		PLOTP_STARTP = PLOTP_STARTP + floor(NPLOTP/10);
	 case '-ffwd',
		PLOTP_STARTP = PLOTP_STARTP + floor(NPLOTP/2);
	 case '-fffwd',
		PLOTP_STARTP = PLOTP_STARTP + floor(9*NPLOTP/10);
	 case '-rew',
		PLOTP_STARTP = PLOTP_STARTP - floor(NPLOTP/10);
	 case '-rrew',
		PLOTP_STARTP = PLOTP_STARTP - floor(NPLOTP/2);
	 case '-rrrew',
		PLOTP_STARTP = PLOTP_STARTP - floor(9*NPLOTP/10);
	 otherwise,
		error([ 'Unknown option ' move]);
  end
  PLOTP_STARTP = max([1, PLOTP_STARTP]);
  PLOTP_STARTP = min([PLOTP_STARTP, NTOT]);
  
  %--- Get the data:
  [err, DATA] = getp(PLOTP_STARTP, NPLOTP);
  if err > 0,
	 error([ 'Error while reading pings' ]);
  elseif err == -1,
	 PLOTP_SCROLL = 0;
	 fprintf('\n\n\t AT EOF ... \n\n');
  end
  %--- Flag data:
  [DATA, PLOTP_FLAGS] = flagdata(DATA, LIM, '-hdg', HDG);
  
  %--- Update plot:
  plotp_upd_plot;

  %--- Check for interrupts:
  plotpuic('plotp_interrupt');
  
  %--- Loop exit flag:
  if PLOTP_SCROLL <= 0,
	 done = 1;
  end
  
end

return;

% ------------------------------------------------------------------
% Initialize the UIC for plotp, and the figure for the plots themselves. 

function plotp_init

scanglob;

%--- Set global variables:
PLOTP_SCROLL = 0;
%--- Install button window:
plotpuic;
%--- Create plot:
if isempty(PLOTP_FIG),
  plotp_create_plot;
end

return;

% ------------------------------------------------------------------
% Remove the plot figure and reset global variables

function plotp_quit()

scanglob;

DATA = [];
PLOTP_SCROLL = 0;
if ~isempty(PLOTP_FIG),
  delete(PLOTP_FIG);
  PLOTP_FIG = [];
  PLOTP_SUB = [];
end

return;

% ------------------------------------------------------------------
% Create the figure for plots

function plotp_create_plot

scanglob;

if ~isempty(PLOTP_FIG),
  return;
end

% Initialize figure:
PLOTP_FIG = figure('NumberTitle', 'off', 'Name', 'Plotp', ...
	 'PaperType', 'a4letter');
PLOTP_SUB = [];

% Calculate size of plots:
[pos, np, m] = plotp_sizes;

% Create plots:
for n = 1:np,
  pos = plotp_sizes(n);
  l = pos(1); b = pos(2); w = pos(3); h = pos(4);
  PLOTP_SUB(n) = subplot('position', pos);
end

return;

% ------------------------------------------------------------------
function varargout = plotp_sizes(varargin)

scanglob;

no_plot = 0;
if nargin > 0,
  no_plot = varargin{1};
end

% Calculate size of plots:
np = length(PLOTP_VARS);
m = 0.08;
h = (1-2*m)/np - 1.5*m;
b = 1 - 2*m - h;

% Get max number of y-variables:
nvmax = 0;
for n=1:np,
  nvmax = max(nvmax, length(PLOTP_VARS{n}));
end
nvmax = nvmax - 1;

% Calculate plot indentation:
l = (nvmax+1)*m*0.75;
w = 1 - l - 1.5*m;

% Get sizes for current subplot:
if no_plot > 0,
  b = b - (no_plot-1)*(1.5*m+h);
end

% Output arguments:
if nargout > 0,
  varargout{1} = [l, b, w, h];
end
if nargout > 1,
  varargout{2} = np;
end
if nargout > 2,
  varargout{3} = m;
end

return;
  
% ------------------------------------------------------------------
function plotp_labels

scanglob;

set(0, 'CurrentFigure', PLOTP_FIG);

old_axes = gca;
aa = axes('position', [0 0 1 1]);
axis('off');

% Get plot sizes:
[pos, np, m] = plotp_sizes;

for n=1:np,
  % Get plot sizes:
  pos = plotp_sizes(n);
  l = pos(1); b = pos(2); w = pos(3); h = pos(4);  
  % Put labels:
  pv = PLOTP_VARS{n};
  x = pv(1);
  text(l+m, b-m, HDR{x}, 'FontSize', 8, 'Units', 'normalized', ...
		'Interpreter', 'none');
  nv= length(pv);
  mp = 0;
  for py=2:nv,
	 clr = plotp_colour(py-1);
	 y = pv(py);
	 text((py-1)*m/2, b, HDR{y}, 'FontSize', 8, ...
		  'Units', 'normalized', 'Rotation', 90, 'Interpreter', 'none', ...
		  'Color', clr);
  end
end

axes(old_axes);
return;
  
% ------------------------------------------------------------------
function plotp_upd_plot

scanglob;

np = length(PLOTP_SUB);

for n=1:np,
  % Activate subplot:
  set(0, 'CurrentFigure', PLOTP_FIG);
  set(PLOTP_FIG, 'CurrentAxes', PLOTP_SUB(n));
  % Get variables:
  pv = PLOTP_VARS{n};
  x  = pv(1);
  nv = length(pv);
  % Get axis lengths:
  xmin = min(DATA(x,:));
  xmax = max(DATA(x,:));
  ymin = Inf;
  ymax = -Inf;
  for py = 2:nv,
	 y = pv(py);
	 ymin = min( [ ymin, STAT(y,1) ]);
	 ymax = max( [ ymax, STAT(y,2) ]);
  end
  % Plot good points:
  hold off;
  for py = 2:nv,
	 % Get the colour:
	 clr = plotp_colour(py-1);
	 y = pv(py);
	 good = (find(PLOTP_FLAGS(y,:) == 1));
	 plot(DATA(x,good), DATA(y,good), [ '.' clr ]);
	 hold on;
  end
  % Plot bad points:
  for py = 2:nv,
	 % Get the colour:
	 clr = plotp_colour(py-1);
	 y = pv(py);
	 % Points above interval:
	 bad = (find(PLOTP_FLAGS(y,:) > 1));
	 tmp = ones(size(bad)) * ymax;
	 plot(DATA(x,bad), tmp, [ '+' clr]);
	 % Points below interval:
	 bad = (find(PLOTP_FLAGS(y,:) < 0));
	 tmp = ones(size(bad)) * ymin;
	 plot(DATA(x,bad), tmp, [ '+' clr]);
	 % Points inside interval:
	 bad = (find(PLOTP_FLAGS(y,:) == 0));
	 tmp = ones(size(bad)) * (ymax + ymin)/2.0;
	 plot(DATA(x,bad), tmp, [ 'o' clr]);
  end
  xeps = (xmax - xmin)/100;
  yeps = (ymax - ymin)/100;
  axis([ xmin-xeps, xmax+xeps, ymin-yeps, ymax+yeps]);
  grid on;
  set(gca, 'FontSize', 8, 'FontAngle', 'italic');
end
plotp_labels;

return;

% ------------------------------------------------------------------
function s = plotp_colour(n)

% Colours:
c = 'rgbkmc';
cmax = length(c);
n = mod(n, cmax);

s = c(n);

return;




