function sta_plot(plot_choice)
% STA_PLOT The plotting routine for the STAPLOT interactive GUI (qv).
% No help is available for this function.

% STAPLOT global vars
global TS_HANDLE GEO_HANDLE figchars sta scr_size scr_offset
global F_HANDLES prop Dist polon polat B_HANDLES

% Global options that may be user-specified in the OPTS file
global nstn sections typevar station_style proplist proj projlim 
global colornames mapcolors plotcolors plotstyle contno
global pr_var de_var pt_var sa_var te_var lon_var lat_var
global marksize pr_reverse de_reverse sta_write sta_var
global geolim
% heart of the beast
% Deirdre Byrne, L-DEO 96/05/20

if isempty(sta) | all(isnan(sta))
  disp('Please pick some stations')
  return
end

VERSION = version; VERSION = str2num(VERSION(1));
if VERSION < 5
  affirm = 'yes';
  deny = 'no';
  markerstring = '''linestyle'',S';
else
  affirm = 'on';
  deny = 'off';
  markerstring = '''marker'',S';
end

% make proplist vars global
s = size(proplist); str = []; for i = 1:s(1); str = [str ' ']; end
str = sprintf('%s',[proplist str']'); eval(['global ' str]); 

if length(station_style) < nstn
  l = length(station_style);
  station_style = [station_style 12*ones(1,nstn-l+1)];
end

preface = [];  
% ************************************************************* %
if plot_choice == 1 			% X-Y PLOT
  if (prop(1) > 0) & (prop(2) > 0)
    if ~isempty(figchars)
      figno = [];
      figno = find((figchars(:,1) == prop(2)) & (figchars(:,2) == prop(1))...
	  & (figchars(:,3) == 0) & ((figchars(:,4) == 0) | ...
	  (figchars(:,4) == 3)));
    else
      figno = [];
    end
    argx = fliplr(deblank(fliplr(deblank(proplist(prop(2),:)))));
    argy = fliplr(deblank(fliplr(deblank(proplist(prop(1),:)))));
    % calculate distance, if necessary
    if (strcmp(argx,'Dist') | strcmp(argy,'Dist'))
      if (~isempty(eval(lon_var)) & ~isempty(eval(lat_var)))
	if ~isempty(sta)
	  if sum(~isnan(sta)) > 0
	    eval(['Dist = cumsum([0 dist(' lat_var '(sta),' lon_var ...
		  '(sta))/1000]);'])
	  end
	end
      end
    end
    
    eval(['x = size(' argx '); y = size(' argy ');'])
    if any(x == 0) | any(y == 0)
      disp('One of these things is not like another ... ')
      disp('One or more of the chosen variables is empty.')
      return
    end
    
  else
    disp('      The Creative works sublime success,')
    disp('         Furthering through perseverance.')
    disp('         Please choose 2 properties from the plot menu')
    return
  end
  
  if isempty(figno), 			% it's a NEW PLOT
    fig_offset = [scr_offset + round(scr_size*0.30)];
    fig_size = round(scr_size*0.50);
    %
    new_fig_handle = figure('NumberTitle','off',...
	'Name',[argy '/' argx], 'units','pixels',...
	'Position',[fig_offset fig_size],'resize','on', ...
	'userdata','staplot','visible','off');
    F_HANDLES = [new_fig_handle F_HANDLES];
    figchars = [zeros(1,4); figchars];
    if (strcmp(argx,'Dist') | strcmp(argy,'Dist'))
      figchars(1,4) = 3;
    end
    figchars(1,1:3) = [prop(2) prop(1) 0];
    %      set(0,'currentfigure',new_fig_handle); set(new_figure_handle,'visible','off')
    ax = axes('Units', 'Normalized', 'Position', [0.15 0.1 0.8 0.8],...
	'drawmode','fast','box','on');
    eval(['xlim = minmax(' argx ');']); 
    eval(['ylim = minmax(' argy ');']);
    xlim = ((diff(xlim) + 0.1)*0.04)*[-1 1] + xlim;
    ylim = ((diff(ylim) + 0.1)*0.04)*[-1 1] + ylim;
    hold on;
    % SPECIAL REQUESTS
    if strcmp(argy,pr_var) 
      if pr_reverse
	set(ax,'ydir','reverse')
      end
    elseif strcmp(argy,de_var)
      if de_reverse
	set(ax,'ydir','reverse')
      end
    end
    if strcmp(argx,'Dist') & strcmp(get(gca,'ydir'),'normal')
      if sta_write
	text(0,1,'Sta','units','normalized', ...
	    'horiz','right','vert','bottom')
	for i = 1:length(sta)
	  if exist(sta_var) == 1
	    num = sprintf('num2str(%s(sta(i)))',sta_var);
	    num = [' ' eval(num)];
	  else
	    num = [' ' num2str(sta(i))];
	  end
	  text(Dist(i),ylim(2),num, ...
	      'horiz','center','vert','bottom')
	end
      end
    elseif strcmp(argx,'Dist') & strcmp(get(gca,'ydir'),'reverse')
      if sta_write
	text(0,1,'Sta','units','normalized', ...
	    'horiz','right','vert','bottom')
	for i = 1:length(sta)
	  if exist(sta_var) == 1
	    num = sprintf('num2str(%s(sta(i)))',sta_var);
	    num = [' ' eval(num)];
	  else
	    num = [' ' num2str(sta(i))];
	  end
	  text(Dist(i),ylim(1), num, ...
	      'horiz','center','vert','bottom')
	end
      end
    end
    
    if strcmp(argy,'Dist')
      if sta_write
	text(1,1,'Sta','units','normalized', ...
	    'horiz','left','vert','bottom')
	for i = 1:length(sta)
	  if exist(sta_var) == 1
	    num = sprintf('num2str(%s(sta(i)))',sta_var);
	    num = [' ' eval(num)];
	  else
	    num = [' ' num2str(sta(i))];
	  end
	  text(xlim(2),Dist(i), num, ...
	      'horiz','center','vert','bottom')
	end
      end
    end
    
    if strcmp(argx,sa_var) & (strcmp(argy,pt_var) | ...
	  strcmp(argy,te_var))
      pix = get(0,'children'); 		% check for live staplot figs
      kids = [];
      for i = 1:length(pix)
	if isstrcmp(get(pix(i),'userdata'),'staplot')
	  kids = [kids pix(i)];
	end
      end
      if ~isempty(TS_HANDLE)
	if any(kids == TS_HANDLE)
	  if any(findobj(TS_HANDLE,'type','uicontrol') == ...
		B_HANDLES(14))
	    % there is already a designated TS plot and
	    % this new plot is not it.
	  else
	    TS_HANDLE = [];
	    axis('square')
	    TS_HANDLE = new_fig_handle;
	    % New: create a TSEdit button and put in on the figure
	    B_HANDLES(14) = uicontrol(TS_HANDLE, 'style','radio', ...
		'units', 'normalized', ...
		'position', [0.86 0.01 0.14 0.06], ...	    
		'string', 'TS/Z', ...
		'callback','hydroplt(''tsedit'')');
	  end
	else
	  TS_HANDLE = [];
	  axis('square')
	  TS_HANDLE = new_fig_handle;
	  % New: create a TSEdit button and put in on the figure
	  B_HANDLES(14) = uicontrol(TS_HANDLE, 'style','radio', ...
	      'units', 'normalized', ...
	      'position', [0.86 0.01 0.14 0.06], ...	    
	      'string', 'TS/Z', ...
	      'callback','hydroplt(''tsedit'')');
	end
      else
	axis('square')
	TS_HANDLE = new_fig_handle;
	% New: create a TSEdit button and put in on the figure
	B_HANDLES(14) = uicontrol(TS_HANDLE, 'style','radio', ...
	    'units', 'normalized', ...
	    'position', [0.86 0.01 0.14 0.06], ...	    
	    'string', 'TS/Z', ...
	    'callback','hydroplt(''tsedit'')');
      end
    end
    if strcmp(argx,lon_var) & strcmp(argy,lat_var)
      if isempty(nstn)
	nstn = length(eval(lon_var));
      end
      GEO_HANDLE = new_fig_handle;
      fig_size = round(scr_size*0.40);
      fig_offset = scr_offset + [round(scr_size(1)*0.10)+1 round(scr_size(2)*0.48)];
      set(GEO_HANDLE,'units','pixels','position',[fig_offset fig_size], ...
	  'NumberTitle','off','Name','Stations','resize','on', ...
	  'userdata','staplot','visible','off','windowbuttondownfcn','staid2');
      if strcmp(proj,'rec')
	% fixed so robust wrt NaNs 98/04/09
	xlim2 = minmax(eval(lon_var));
	xlim2 = ((diff(xlim2) + 0.1)*0.04) * [-1 1] + xlim2;
	ylim2 = minmax(eval(lat_var));
	ylim2 = ((diff(ylim2) + 0.1)*0.04) * [-1 1] + ylim2;
	if ~isempty(geolim)
	  if max(size(geolim)) == 4
	    xlim = sort(geolim(1:2));
	    ylim = sort(geolim(3:4));
	  else
	    xlim = xlim2;
	    ylim = ylim2;
	  end
	else
	  xlim = xlim2;
	  ylim = ylim2;
	end
	set(ax,'XLimMode','manual', 'XLim', xlim,...
	    'YLimMode','manual','YLim', ylim,...
	    'Userdata',[1:nstn]')
%	set(ax,'XLimMode','manual', 'XLim', xlim,...
%	    'YLimMode','manual','YLim', ylim)
	plot(eval(lon_var), eval(lat_var), '+', 'color', [0.5 0.5 0.5],...
	    'erasemode', 'none', 'markersize',5); hold on;
      elseif strcmp(proj,'north')
	if isempty(projlim)
	  projlim = 0;
	else
	  projlim = abs(projlim);
	end
	set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); hold on
%	set(ax,'visible','off','aspect',[1 1]); hold on
	for kk=10:10:(90-projlim),
	  xx=kk*cos([0:10:360]*pi/180);
	  yy=kk*sin([0:10:360]*pi/180);
	  line(xx,yy,'color','w','linestyle',':');
	end;
	xx=(90-projlim)*cos([0:5:360]*pi/180);
	yy=(90-projlim)*sin([0:5:360]*pi/180);
	line(xx,yy,'color','w','linestyle','-');
	for kk=10:10:(90-projlim-2),
	  text(0,kk,sprintf('%2.0fN',90-kk),'horizontal','center');
	end;
	for kk=0:30:359,
	  xx=[10 90-projlim]*cos(kk*pi/180);
	  yy=[10 90-projlim]*sin(kk*pi/180);
	  line(xx,yy,'color','w','linestyle',':');
	  if (kk<=180),nyl=sprintf('%3.0fE',kk);
	  else nyl=sprintf('%3.0fW',abs(360-kk)); 
	  end;
	  text(yy(2),-xx(2),nyl,'rotation',kk,'horizontal','center','vertical','top');
	end
	polon=(90-eval(lat_var)).*cos(eval(lon_var)*pi/180-pi/2);
	polat=(90-eval(lat_var)).*sin(eval(lon_var)*pi/180-pi/2);
	plot(polon,polat, '+', 'color', [0.5 0.5 0.5],...
	    'erasemode', 'none', 'markersize',5);
      elseif strcmp(proj,'south')
	if isempty(projlim)
	  projlim = 0;
	else
	  projlim = abs(projlim);
	end
	set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); hold on
%	set(ax,'visible','off','aspect',[1 1]); hold on
	for kk=10:10:(90-projlim),
	  xx=kk*cos([0:10:360]*pi/180);
	  yy=kk*sin([0:10:360]*pi/180);
	  line(xx,yy,'color','w','linestyle',':');
	end;
	xx=(90-projlim)*cos([0:5:360]*pi/180);
	yy=(90-projlim)*sin([0:5:360]*pi/180);
	line(xx,yy,'color','w','linestyle','-');
	for kk=10:10:(90-projlim-2),
	  text(0,kk,sprintf('%2.0fS',90-kk),'horizontal','center');
	end;
	for kk=0:30:359,
	  xx=[10 90-projlim]*cos(kk*pi/180);
	  yy=[10 90-projlim]*sin(kk*pi/180);
	  line(xx,yy,'color','w','linestyle',':');
	  if (kk<=180),nyl=sprintf('%3.0fE',kk);
	  else nyl=sprintf('%3.0fW',abs(360-kk)); 
	  end;
	  text(yy(2),xx(2),nyl,'rotation',-kk,'horizontal','center','vertical','bottom');
	end
	polon=(90+eval(lat_var)).*cos(-eval(lon_var)*pi/180+pi/2);
	polat=(90+eval(lat_var)).*sin(-eval(lon_var)*pi/180+pi/2);
	plot(polon,polat, '+', 'color', [0.5 0.5 0.5],...
	    'erasemode', 'none', 'markersize',5);
      else
	disp('Unrecognized map projection! Please check session options file.')
	return
      end
      drawnow
      set(GEO_HANDLE,'visible','on')
    else
      set(ax,'XLimMode','manual', 'XLim', xlim,...
	  'YLimMode','manual','YLim', ylim,...
	  'Userdata',sta(findnew(sta)))
    end
    xlabel(argx); ylabel(argy); set(gcf,'visible','on')
  else 				% not a new window
    figure(F_HANDLES(figno))
    ax = gca;
    f = [get(ax,'Userdata'); sta];
    set(ax,'Userdata',f(findnew(f)))
  end
  lims = axis; 			% preserve axis data for later use
  % find out what kind of plot it will be
  if (any(x == 1) & any(y == 1)) 	% VECTOR-VECTOR
    % the special-est case  :-(
    if strcmp(argx,lon_var) & strcmp(argy,lat_var)
      if strcmp(proj,'north') | strcmp(proj,'south')
	argx = 'polon'; argy = 'polat';
      end
    end
    if typevar(prop(1)) == 1 & typevar(prop(2)) == 1
      if (max(x) < nstn) & (max(y) == nstn)
	eval(['x = ' argx '; y = ' argy '(sta);'])
      elseif (max(x) == nstn) & (max(y) < nstn)
	eval(['x = ' argx '(sta); y = ' argy ';'])
      elseif (max(x) == max(y)) & (max(x) < nstn)
	eval(['x = ' argx '; y = ' argy ';'])
      elseif (max(x) == max(y)) & max(x) == max(nstn)
	eval(['x = ' argx '(sta); y = ' argy '(sta);'])
      end
      for i = 1:length(sta)
	C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
	S = plotstyle(rem(station_style(sta(i)),10)+1);
	if VERSION < 5
	  plot(x(i),y(i),'color',C,'linestyle',S, ...
	      'markersize',marksize, 'linewidth',2)
	else
	  if strcmp(S,'-')
	    plot(x(i),y(i),'color',C,'linestyle',S, ...
		'markersize',marksize,'linewidth',2)
	  else
	    plot(x(i),y(i),'color',C,'marker',S, ... 
		'linestyle', 'none', ...
		'markersize',marksize, 'linewidth',2)
	  end
	end
      end
    elseif (typevar(prop(2)) == 1) & (typevar(prop(1)) == 2)
      if max(x) == nstn
	x = eval([argx '(sta)']);
      else
	x = eval(argx);
      end
      y = eval(argy); nrow = length(y); yarg = 'y(:)';
      xarg = ['ones(nrow,1)*' argx '(i)'];
      if VERSION < 5
	arg1 = sprintf('plot(%s,%s,%s)', xarg, yarg, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg2 = sprintf('plot(%s,%s,%s)', xarg, yarg, ...
	    '''color'',C,''linestyle'',S');
	arg1 = sprintf('plot(%s,%s,%s,%s)', xarg, yarg, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
      for i = 1:length(sta)
	C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
	S = plotstyle(rem(station_style(sta(i)),10)+1);
	if VERSION < 5
	  eval(arg1)
	else
	  if strcmp(S,'-')
	    eval(arg2)
	  else
	    eval(arg1)
	  end
	end
      end
    elseif typevar(prop(2)) == 2 & typevar(prop(1)) == 1
      if max(y) == nstn
	y = eval([argy '(sta)']);
      else
	y = eval(argy);
      end
      x = eval(argx); nrow = length(x); xarg = 'x(:)';
      yarg = ['ones(nrow,1)*' argy '(i)'];
      if VERSION < 5
	arg1 = sprintf('plot(%s,%s,%s)', xarg, yarg, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg2 = sprintf('plot(%s,%s,%s)', xarg, yarg, ...
	    '''color'',C,''linestyle'',S');
	arg1 = sprintf('plot(%s,%s,%s,%s)', xarg, yarg, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
      for i = 1:length(sta)
	C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
	S = plotstyle(rem(station_style(sta(i)),10)+1);
	if VERSION < 5
	  eval(arg1)
	else
	  if strcmp(S,'-')
	    eval(arg2)
	  else
	    eval(arg1)
	  end
	end
      end
    else
      if max(x) == max(y)
	plot(eval(argx),eval(argy))
      else
	disp('These quantities not able to be plotted')
	return
      end
    end
  elseif (any(x == 1) & ~any(y == 1)) % VECTOR-MATRIX
    if y(2) == nstn
      argy = [argy '(:,sta(i))'];
    else
      argy = [argy '(:,i)'];
    end
    if typevar(prop(2)) == 1 		% x is one value per station
      if max(x) == nstn,
	eval(['x = ' argx '(sta);'])
      else
	eval(['x = ' argx ';'])
      end
      if VERSION < 5
	arg1=sprintf('plot(ones(y(1),1)*x(i), %s,%s)', argy, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg2=sprintf('plot(ones(y(1),1)*x(i),%s)', argy, ...
	    '''color'',C,''linestyle'',S');
	arg1=sprintf('plot(ones(y(1),1)*x(i), %s,%s)', argy, ...
	    '''color'',C,''marker'',S,''markersize'',marksize');
      end
    elseif typevar(prop(2)) == 2 	% x is one value per row
      % no selection of x values
      eval(['x = ' argx '(:);'])
      if VERSION < 5
	arg1=sprintf('plot(x, %s, %s)',argy, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg2=sprintf('plot(x, %s, %s)',argy, ...
	    '''color'',C,''linestyle'',S');
	arg1=sprintf('plot(x, %s, %s, %s)',argy, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      if VERSION < 5
	eval(arg1)
      else
	if strcmp(S,'-')
	  eval(arg2)
	else
	  eval(arg1)
	end
      end
    end
  elseif any(y == 1) % MATRIX VECTOR
    if x(2) == nstn
      argx = [argx '(:,sta(i))'];
    else
      argx = [argx '(:,i)'];
    end
    if typevar(prop(1)) == 1 		% y is one value per station
      if max(y) == nstn,
	eval(['y = ' argy '(sta);'])
      else
	eval(['y = ' argy ';'])
      end
      if VERSION < 5
	arg1=sprintf('plot(%s, ones(x(1),1)*y(i), %s)', ...
	    argx, '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg2=sprintf('plot(%s, ones(x(1),1)*y(i), %s)', ...
	    argx, '''color'',C,''linestyle'',S');
	arg1=sprintf('plot(%s, ones(x(1),1)*y(i), %s, %s)', ...
	    argx, '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    elseif typevar(prop(1)) == 2 	% y is one value per row (depth)
      % no selection of y values
      eval(['y = ' argy '(:);'])
      if VERSION < 5
	arg1=sprintf('plot(%s, y, %s)',argx, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg2=sprintf('plot(%s, y, %s)',argx, ...
	    '''color'',C,''linestyle'',S');
	arg1=sprintf('plot(%s, y, %s, %s)',argx, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      if VERSION < 5
	eval(arg1)
      else
	if strcmp(S,'-')
	  eval(arg2)
	else
	  eval(arg1)
	end
      end
    end
  else % MATRIX MATRIX PLOTS
    for i = 1:length(sta)
      C = plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      if VERSION < 5
	arg = sprintf('plot(%s(:,sta(i)),%s(:,sta(i)),%s)',argx,argy, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	if strcmp(S,'-')
	  arg = sprintf('plot(%s(:,sta(i)),%s(:,sta(i)),%s)',argx,argy, ...
	      '''color'',C,''linestyle'',S');
	else
	  arg = sprintf('plot(%s(:,sta(i)),%s(:,sta(i)),%s,%s)',argx,argy, ...
	      '''color'',C,''marker'',S,''markersize'',marksize', ...
	      '''linestyle'',''none''');
	end
      end
      eval(arg)
    end
  end
  set(ax,'XLimMode','manual', 'XLim', lims(1:2),...
      'YLimMode','manual','YLim', lims(3:4))
  return
  %% ******************************************************************** %
elseif plot_choice == 2 		% X-Y-Z PLOT
  if (prop(1) > 0) & (prop(2) > 0) & (prop(3) > 0)
    if ~isempty(figchars)
      figno = find((figchars(:,1) == prop(3)) & (figchars(:,2) == prop(2))...
	  & (figchars(:,3) == prop(1)) & ((figchars(:,4) == 1) | ...
	  (figchars(:,4) == 4)));
    else
      figno = [];
    end
  else
    disp('    The movement of heaven is full of power.')
    disp('       Thus the superior person makes himself strong and untiring.')
    disp('       Please choose 3 properties first')
    return
  end
  
  % get names of variables to be plotted
  argx = fliplr(deblank(fliplr(deblank(proplist(prop(3),:)))));
  argy = fliplr(deblank(fliplr(deblank(proplist(prop(2),:)))));
  argz = fliplr(deblank(fliplr(deblank(proplist(prop(1),:)))));
  labx = argx; laby = argy; labz = argz; % save for future use as labels
  
  if (strcmp(argx,'Dist') | strcmp(argy,'Dist') | strcmp(argz,'Dist'))
    if (~isempty(eval(lon_var)) & ~isempty(eval(lat_var)))
      eval(['Dist = cumsum([0 dist(' lat_var '(sta),' lon_var '(sta))/1000]);'])
    end
  end
  
  eval(['x = size(' argx '); y = size(' argy '); z = size(' argz ');'])
  
  % do one last quick check to save 1000's of miserable souls
  if (typevar(prop(3)) == 1 | typevar(prop(3)) == 2) & (min(x) ~= 1)
    disp('X argument typevar is screwed up.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  elseif typevar(prop(3)) == 3 & ~all(x > 1)
    disp('X argument typevar is screwed up. X is a vector.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  end
  if (typevar(prop(2)) == 1 | typevar(prop(2)) == 2) & (min(y) ~= 1)
    disp('Y argument typevar is screwed up.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  elseif typevar(prop(2)) == 3 & ~all(y > 1)
    disp('Y argument typevar is screwed up. Y is a vector.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  end
  if (typevar(prop(1)) == 1 | typevar(prop(1)) == 2) & (min(z) ~= 1)
    disp('Z argument typevar is screwed up.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  elseif typevar(prop(1)) == 3 & ~all(z > 1)
    disp('Z argument typevar is screwed up. Z is a vector.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  end
  
  % Hokay.
  
  if isempty(figno), 			% it's a NEW PLOT
    fig_offset = [scr_offset + round(scr_size*0.30)];
    fig_size = round(scr_size*0.50);
    fig_size = [max([fig_size(1) 384]) max([fig_size(2) 306])];
    %
    new_fig_handle = figure('NumberTitle','off',...
	'Name',[argz ' / (' argx '&' argy ')'], 'units','pixels',...
	'Position',[fig_offset fig_size],'resize','on', ...
	'userdata','staplot','visible','off');
    %      set(0,'currentfigure',new_fig_handle); set(new_fig_handle,'visible','off')
    F_HANDLES = [new_fig_handle F_HANDLES];
    figchars = [zeros(1,4); figchars];
    if (strcmp(argx,'Dist') | strcmp(argy,'Dist'))
      figchars(1,4) = 4;
    else
      figchars(1,4) = 1;
    end
    figchars(1,1:3) = [prop(3) prop(2) prop(1)];
    ax = axes('Units', 'Normalized', 'Position', [0.15 0.1 0.8 0.8],...
	'drawmode','fast','box','on'); view(3)
    eval(['xlim = minmax(' argx ');']); 
    eval(['ylim = minmax(' argy ');']);
    eval(['zlim = minmax(' argz ');']);
    xlim = ((diff(xlim) + 0.1)*0.04)*[-1 1] + xlim;
    ylim = ((diff(ylim) + 0.1)*0.04)*[-1 1] + ylim;
    zlim = ((diff(zlim) + 0.1)*0.04)*[-1 1] + zlim;
    hold on;
    
    % set up the bells and whistles
    uicontrol('style','slider', ...
	'units','normalized', ...
	'position',[0 0.95 0.2 0.05], ...
	'callback', 'hydroplt(''change_az_slide'')', ...
	'interruptible',affirm, ...
	'userdata', 'azimuth', ...
	'value',0.3958);
    uicontrol('style','edit', 'units','normalized', ...
	'position', [0.2 0.95 0.07 0.05], ...
	'string','-37.5','backgroundcolor',[1 1 1], ...
	'userdata', 'azimuth', ...
	'callback', 'hydroplt(''change_az_edit'')', ...
	'horizontalalign','left', ...
	'foregroundcolor',[0 0 0]);
    
    uicontrol('style','text', 'units','normalized', ...
	'position', [0.27 0.95 0.05 0.05], ...
	'string','AZ','backgroundcolor',[0 0.5 0.5], ...
	'foregroundcolor',[1 1 1])
    drawnow
    
    uicontrol('style','slider', ...
	'units','normalized', ...
	'position',[0.8 0.95 0.2 0.05], ...
	'callback', 'hydroplt(''change_el_slide'')', ...
	'interruptible',affirm, ...
	'userdata', 'elevation', ...
	'value',0.6667);
    uicontrol('style','edit', 'units','normalized', ...
	'position', [0.74 0.95 0.06 0.05], ...
	'string','30','backgroundcolor',[1 1 1], ...
	'userdata', 'elevation', ...
	'callback', 'hydroplt(''change_el_edit'')', ...
	'horizontalalign','left', ...
	'foregroundcolor',[0 0 0]);
    uicontrol('style','text', 'units','normalized', ...
	'position', [0.69 0.95 0.05 0.05], ...
	'string','EL','backgroundcolor',[0 0.5 0.5], ...
	'foregroundcolor',[1 1 1])
    drawnow
    
    % SPECIAL REQUESTS
    if strcmp(argz,pr_var) 
      if pr_reverse
	set(ax,'zdir','reverse')
      end
    elseif strcmp(argz,de_var)
      if de_reverse
	set(ax,'zdir','reverse')
      end
    end
    
    % sloppy
    if strcmp(argx,'Dist') & strcmp(get(gca,'zdir'),'normal')
      text(0,1,1,'Sta','units','normalized', ...
	  'horiz','right','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(Dist(i),ylim(2),zlim(2),num, ...
	    'horiz','center','vert','bottom')
      end
    elseif strcmp(argx,'Dist') & strcmp(get(gca,'zdir'),'reverse')
      text(0,1,1,'Sta','units','normalized', ...
	  'horiz','right','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(Dist(i),ylim(2),zlim(1), num, ...
	    'horiz','center','vert','bottom')
      end
    end
    if strcmp(argy,'Dist') & strcmp(get(gca,'zdir'),'normal')
      text(1,1,1,'Sta','units','normalized', ...
	  'horiz','right','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(xlim(2),Dist(i),zlim(2), num, ...
	    'horiz','center','vert','bottom')
      end
    elseif strcmp(argy,'Dist') & strcmp(get(gca,'zdir'),'reverse')
      text(1,1,1,'Sta','units','normalized', ...
	  'horiz','right','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(xlim(2),Dist(i),zlim(1), num, ...
	    'horiz','center','vert','bottom')
      end
    end
    
    set(ax,'XLimMode','manual', 'XLim', xlim,...
	'YLimMode','manual','YLim', ylim,...
	'ZLimMode','manual','ZLim', zlim,...
	'Userdata',sta(findnew(sta)))
    xlabel(argx); ylabel(argy); zlabel(argz); set(gcf,'visible','on')
    
  else 				% not a new window
    figure(F_HANDLES(figno))
    f = [get(gca,'Userdata'); sta];
    set(gca,'Userdata',f(findnew(f)))
  end
  
  % preserve axis data for later use
  lims = axis; 
  if size(lims) == [1 4], 
    view(3)
  end
  lims = axis; 
  % *********************************************************************
  % finally ready to plot3; now fairly well debugged (I think)
  % find out what kind of plot it will be
  % VECTOR VECTOR VECTOR PLOTS: these are the easy ones
  if (any(x == 1) & any(y == 1) & any(z == 1))
    if all(typevar([prop(1) prop(2) prop(3)]) == 1)
      if max(x) == nstn
	eval(['x = ' argx '(sta);'])
      else
	x = eval(argx);
      end
      if max(y) == nstn
	eval(['y = ' argy '(sta);'])
      else
	y = eval(argy);
      end
      if max(z) == nstn
	eval(['z = ' argz '(sta);'])
      else
	z = eval(argz);
      end
      % ensure row vectors
      x = x(:)'; y = y(:)'; z = z(:)';
      for i = 1:length(sta)
	C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
	S = plotstyle(rem(station_style(sta(i)),10)+1);
	if VERSION < 5
	  plot3(x(i),y(i),z(i),'color',C,'linestyle',S,'markersize',marksize)
	else
	  plot3(x(i),y(i),z(i),'color',C,'marker',S, ...
	      'markersize',marksize,'linestyle','none')
	end
      end
    else
      if max(x) == max(z) & max(x) == max(y)
	plot3(eval(argx),eval(argy),eval(argz))
      else
	disp('       It furthers one')
	disp('          To be unremittingly persevering.')
	disp('          Plot3 elements are unequal in length')
	return
      end
    end
    % VECTOR MATRIX MATRIX: Y and Z are matrices
  elseif (any(x == 1) & ~any(y == 1) & ~any(z == 1))
    if y(2) == nstn
      argy = [argy '(:,sta(i))'];
    else
      argy = [argy '(:,i)'];
    end
    if z(2) == nstn
      argz = [argz '(:,sta(i))'];
    else
      argz = [argz '(:,i)'];
    end
    if typevar(prop(3)) == 1
      if max(x) == nstn
	eval(['x = ' argx '(sta);'])
      else
	eval(['x = ' argx ';'])
      end
      if VERSION < 5
	arg=sprintf('plot3(ones(y(1),1)*x(i), %s, %s, %s)', argy, argz, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg=sprintf('plot3(ones(y(1),1)*x(i), %s, %s, %s, %s)', argy, argz, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    elseif typevar(prop(3)) == 2 	% x is one value per row
      % no selection of x values
      eval(['x = ' argx '(:);'])
      if VERSION < 5
	arg=sprintf('plot3(x, %s, %s, %s)',argy, argz, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg=sprintf('plot3(x, %s, %s, %s, %s)',argy, argz, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)), ...
	  10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
    % MATRIX VECTOR MATRIX: X and Z are matrices
  elseif (~any(x == 1) & any(y == 1) & ~any(z == 1))
    if x(2) == nstn
      argx = [argx '(:,sta(i))'];
    else
      argx = [argx '(:,i)'];
    end
    if z(2) == nstn
      argz = [argz '(:,sta(i))'];
    else
      argz = [argz '(:,i)'];
    end
    if typevar(prop(2)) == 1
      if max(y) == nstn
	eval(['y = ' argy '(sta);'])
      else
	eval(['y = ' argy ';'])
      end
      if VERSION < 5
	arg=sprintf('plot3(%s, ones(z(1),1)*y(i), %s, %s)', argx, argz, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg=sprintf('plot3(%s, ones(z(1),1)*y(i), %s, %s, %s)', argx, argz, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    elseif typevar(prop(2)) == 2 	% y is one value per row
      % no selection of y values
      eval(['y = ' argy '(:);'])
      if VERSION < 5
	arg=sprintf('plot3(%s, y, %s, %s)',argx, argz, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg=sprintf('plot3(%s, y, %s, %s, %s)',argx, argz, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)), ...
	  10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
    % MATRIX MATRIX VECTOR: X and Y are matrices; weird
  elseif (~any(x == 1) & ~any(y == 1) & any(z == 1))
    if x(2) == nstn
      argx = [argx '(:,sta(i))'];
    else
      argx = [argx '(:,i)'];
    end
    if y(2) == nstn
      argy = [argy '(:,sta(i))'];
    else
      argy = [argy '(:,i)'];
    end
    if typevar(prop(1)) == 1
      if max(z) == nstn
	eval(['z = ' argz '(sta);'])
      else
	eval(['z = ' argz ';'])
      end
      if VERSION < 5
	arg=sprintf('plot3(%s, %s, ones(y(1),1)*z(i), %s)', argx, argy, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg=sprintf('plot3(%s, %s, ones(y(1),1)*z(i), %s, %s)', argx, argy, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    elseif typevar(prop(1)) == 2 	% z is one value per row
      % no selection of z values
      eval(['z = ' argz '(:);'])
      if VERSION < 5
	arg=sprintf('plot3(%s, %s, z, %s)',argx, argy, ...
	    '''color'',C,''linestyle'',S,''markersize'',marksize');
      else
	arg=sprintf('plot3(%s, %s, z, %s, %s)',argx, argy, ...
	    '''color'',C,''marker'',S,''markersize'',marksize', ...
	    '''linestyle'',''none''');
      end
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)), ...
	  10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
    % VECTOR VECTOR MATRIX PLOTS: only Z is a matrix
  elseif (~any(z == 1) & (any(x == 1) & any(y == 1)))
    if typevar(prop(3)) == 1
      nrow = z(1);
      if max(x) == nstn
	eval(['x = ' argx '(sta);'])
      else
	eval(['z = ' argx ';'])
      end
      x = x(:)';
      argx = 'ones(nrow,1)*x(i)';
    elseif typevar(prop(3)) == 2
      eval(['x = ' argx '(:);'])
      argx = 'x';
    else
      disp('Sorry!  X is an undefined type.')
      return
    end
    if typevar(prop(2)) == 1
      if max(y) == nstn
	eval(['y = ' argy '(sta);'])
      else
	eval(['y = ' argy ';'])
      end
      y = y(:)';
      argy = 'ones(1,nrow)*y(i)';
    elseif typevar(prop(2)) == 2
      eval(['y = ' argy '(:);'])
      argy = 'y';
    else
      disp('Sorry!  Y is an undefined type.')
      return
    end
    if z(2) == nstn
      argz = [argz '(:,sta(i))'];
    else
      argz = [argz '(:,i)'];
    end
    if VERSION < 5
      arg=sprintf('plot3(%s, %s, %s, %s)',argx, argy, argz, ...
	  '''color'',C,''linestyle'',S,''markersize'',marksize');
    else
      arg=sprintf('plot3(%s, %s, %s, %s, %s)',argx, argy, argz, ...
	  '''color'',C,''marker'',S,''markersize'',marksize', ...
	  '''linestyle'',''none''');
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)), ...
	  10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
    return
    % VECTOR MATRIX VECTOR: only Y is a matrix
  elseif (any(x == 1) & ~any(y == 1) & any(z == 1))
    if y(2) == nstn
      argy = [argy '(:,sta(i))'];
    else
      argy = [argy '(:,i)'];
    end
    if typevar(prop(3)) == 1
      if max(x) == nstn
	eval(['x = ' argx '(sta);'])
      else
	eval(['x = ' argx ';'])
      end
      xarg = 'ones(y(1),1)*x(i)';
    elseif typevar(prop(3)) == 2 	% x is one value per row
      % no selection of x values
      eval(['x = ' argx '(:);'])
      xarg = 'x';
    end
    if typevar(prop(1)) == 1
      if max(z) == nstn
	eval(['z = ' argz '(sta);'])
      else
	eval(['z = ' argz ';'])
      end
      zarg = 'ones(y(1),1)*z(i)';
    elseif typevar(prop(1)) == 2 	% z is one value per row
      % no selection of z values
      eval(['z = ' argz '(:);'])
      zarg = 'z';
    end
    if VERSION < 5
      arg=sprintf('plot3(%s, %s, %s, %s)', xarg, argy, zarg, ...
	  '''color'',C,''linestyle'',S,''markersize'',marksize');
    else
      arg=sprintf('plot3(%s, %s, %s, %s, %s)', xarg, argy, zarg, ...
	  '''color'',C,''marker'',S,''markersize'',marksize', ...
	  '''linestyle'',''none''');
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)), ...
	  10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
    % MATRIX VECTOR VECTOR: X is a matrix
  elseif (~any(x == 1) & any(y == 1) & any(z == 1))
    if x(2) == nstn
      argx = [argx '(:,sta(i))'];
    else
      argx = [argx '(:,i)'];
    end
    if typevar(prop(2)) == 1
      if max(y) == nstn
	eval(['y = ' argy '(sta);'])
      else
	eval(['y = ' argy ';'])
      end
      yarg = 'ones(x(1),1)*y(i)';
    elseif typevar(prop(2)) == 2 	% y is one value per row
      % no selection of y values
      eval(['y = ' argy '(:);'])
      yarg = 'y';
    end
    if typevar(prop(1)) == 1
      if max(z) == nstn
	eval(['z = ' argz '(sta);'])
      else
	eval(['z = ' argz ';'])
      end
      zarg = 'ones(x(1),1)*z(i)';
    elseif typevar(prop(1)) == 2 	% z is one value per row
      % no selection of z values
      eval(['z = ' argz '(:);'])
      zarg = 'z';
    end
    if VERSION < 5
      arg=sprintf('plot3(%s, %s, %s, %s)', argx, yarg, zarg, ...
	  '''color'',C,''linestyle'',S,''markersize'',marksize');
    else
      arg=sprintf('plot3(%s, %s, %s, %s)', argx, yarg, zarg, ...
	  '''color'',C,''marker'',S,''markersize'',marksize', ...
	  '''linestyle'',''none''');
    end
    for i = 1:length(sta)
      C=plotcolors((station_style(sta(i))-rem(station_style(sta(i)), ...
	  10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
  else % MATRIX MATRIX MATRIX PLOTS: also easy
    if x(2) == nstn
      argx = [argx '(:,sta(i))'];
    else
      argx = [argx '(:,i)'];
    end
    if y(2) == nstn
      argy = [argy '(:,sta(i))'];
    else
      argy = [argy '(:,i)'];
    end
    if z(2) == nstn
      argz = [argz '(:,sta(i))'];
    else
      argz = [argz '(:,i)'];
    end
    if VERSION < 5
      arg = sprintf('plot3(%s,%s,%s,%s)',argx,argy,argz, ...
	  '''color'',C,''linestyle'',S,''markersize'',marksize');
    else
      arg = sprintf('plot3(%s,%s,%s,%s, %s)',argx,argy,argz, ...
	  '''color'',C,''marker'',S,''markersize'',marksize', ...
	  '''linestyle'',''none''');
    end
    for i = 1:length(sta)
      C = plotcolors((station_style(sta(i))-rem(station_style(sta(i)),10))/10,:);
      S = plotstyle(rem(station_style(sta(i)),10)+1);
      eval(arg)
    end
  end
  set(ax,'XLimMode','manual', 'XLim', lims(1:2), ...
      'YLimMode','manual','YLim', lims(3:4), ...
      'ZLimMode','manual','ZLim', lims(5:6))
  return
  %%%% ************************************************************************* %%%%
elseif plot_choice > 2 		% CONTOUR PLOT
  if (prop(1) > 0) & (prop(2) > 0) & (prop(3) > 0)
    if ~isempty(figchars)
      figno = find((figchars(:,1) == prop(3)) & (figchars(:,2) == prop(2))...
	  & (figchars(:,3) == prop(1)) & ((figchars(:,4) == 2) | ...
	  (figchars(:,4) == 5)));
    else
      figno = [];
    end
    % get names of variables to be contoured
    argx = fliplr(deblank(fliplr(deblank(proplist(prop(3),:)))));
    argy = fliplr(deblank(fliplr(deblank(proplist(prop(2),:)))));
    argz = fliplr(deblank(fliplr(deblank(proplist(prop(1),:)))));
    labx = argx; laby = argy; labz = argz; % save for future use as labels
  else
    disp(' The movement of heaven is full of power.')
    disp('    Thus the superior person makes himself strong and untiring.')
    disp('    Please choose 3 properties first')
    return
  end
  % SPECIAL REQUESTS
  revax = 0;
  if strcmp(argy,pr_var) 
    if pr_reverse
      revax = 1;
    end
  elseif strcmp(argy,de_var)
    if de_reverse
      revax = 1;
    end
  end
  sqax = 0;
  if strcmp(argx,sa_var) & (strcmp(argy,pt_var) | strcmp(argy,te_var))
    sqax = 1;
  end
  
  if (strcmp(argx,'Dist') | strcmp(argy,'Dist') | strcmp(argz,'Dist'))
    if (~isempty(eval(lon_var)) & ~isempty(eval(lat_var)))
      eval(['Dist = cumsum([0 dist(' lat_var '(sta),' lon_var '(sta))/1000]);'])
    end
  end
  % ******************************************************************************
  % Determining contour arguments
  % ******************************************************************************
  % get sizes of the variables
  eval(['x = size(' argx ');'])
  eval(['y = size(' argy ');'])
  eval(['z = size(' argz ');'])
  
  % do one last quick check to save 1000's of miserable souls
  if (typevar(prop(3)) == 1 | typevar(prop(3)) == 2) & (min(x) ~= 1)
    disp('X argument typevar is screwed up. X is a matrix.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  elseif typevar(prop(3)) == 3 & ~all(x > 1)
    disp('X argument typevar is screwed up. X is a vector.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  end
  if (typevar(prop(2)) == 1 | typevar(prop(2)) == 2) & (min(y) ~= 1)
    disp('Y argument typevar is screwed up. Y is a matrix.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  elseif typevar(prop(2)) == 3 & ~all(y > 1)
    disp('Y argument typevar is screwed up. Y is a vector.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  end
  if (typevar(prop(1)) == 1 | typevar(prop(1)) == 2) & (min(z) ~= 1)
    disp('Z argument typevar is screwed up. Z is a matrix.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  elseif typevar(prop(1)) == 3 & ~all(z > 1)
    disp('Z argument typevar is screwed up. Z is a vector.')
    disp('Please use ''showtype'' and ''settype'' and try again.')
    return
  end
  
  % big sigh. ready to take the plunge.
  
  ncol = length(sta);
  if ncol < 2
    disp('       The image of Difficulty at the Beginning.')
    disp('          Thus the superior person')
    disp('          Brings order out of confusion.')
    disp('          A single station alone cannot be contoured.')
    disp('          Please select more stations.')
    return
  end
  if all(x == 1) | all(y == 1) | all(z == 1)
    disp('       The superior person understands the signs of the time')
    disp('          And prefers to desist.')
    disp('          To go on brings humiliation.')
    disp('          A 1x1 variable cannot be contoured.')
    disp('          Please inspect your plot selections.')
    return
  end
  if any(x == 1) & any(y == 1) & any(z == 1)
    % VECTOR VECTOR VECTOR PLOTS
    disp('       Difficulties in blessing.')
    disp('          A little perseverance brings good fortune.')
    disp('          Great perseverance brings misfortune.')
    disp('          Cannot contour this selection of variables 100')
    disp('          Please use ''XYZ plot'' instead')
    return
  elseif any(x == 1) & any (y ==1) & ~any(z ==1)
    % VECTOR VECTOR MATRIX PLOTS: only Z is a matrix
    nrow = z(1); 
    if typevar(prop(2)) == 2 & typevar(prop(3)) == 1
      % NORMAL ORIENTATION
      if max(y) ~= z(1)
	disp('       Horse and wagon part.')
	disp('          Bloody tears flow.')
	disp('          Y variable is the wrong size 300')
	return
      else
	eval(['y = ' argy '(:);'])
      end
      if any(x == nstn)
	eval(['x = ' argx '(sta);'])
      else
	eval(['x = ' argx ';'])
      end
      x = x(:)';
      preface = '[j jj] = sort(x); [k kk] = sort(y);';
      xarg = 'ones(nrow,1)*x(jj)'; 
      yarg = 'y(kk)*ones(1,ncol)';
      if z(2) == nstn
	zarg = [argz  '(kk,sta(jj))'];
      elseif z(2) == ncol
	zarg = [argz '(kk,jj)'];
      else
	disp('       At the first oracle I inform him.')
	disp('          If he asks two or three times, it is importunity.')
	disp('          Z argument is the wrong size 200')
	return
      end
    elseif typevar(prop(2)) == 1 & typevar(prop(3)) == 2
      % SIDEWAYS ORIENTATION
      if max(x) ~= z(1)
	disp('       If he importunes, I give him no information.')
	disp('       X variable  is the wrong size 400')
	return
      else
	eval(['x = ' argx '(:);'])
      end
      if any(y == nstn)
	eval(['y = ' argy '(sta);'])
      else
	eval(['y = ' argy ';'])
      end
      y = y(:)';
      preface = '[k kk] = sort(x); [j jj] = sort(y);';
      xarg = 'x(kk)*ones(1,ncol)';
      yarg = 'ones(nrow,1)*y(jj)';
      if z(2) == nstn
	zarg = [argz '(kk,sta(jj))'];
      elseif z(2) == ncol
	zarg = [argz '(kk,jj)'];
      else
	disp('       Entangled folly brings humiliation.')
	disp('          Z argument is the wrong size 250')
	return
      end
    else
      disp('       The finest clothes turn to rags.')
      disp('          Be careful all day long.')
      disp('          Cannot contour this selection of variables.')
      disp('          Please check variable types with ''showtype''.')
      disp('          Use ''settype'' to reset as necessary.')
      return
    end
  elseif any(x == 1) & ~any(y == 1) & any(z == 1)
    % VECTOR MATRIX VECTOR PLOTS: only Y is a matrix
    nrow = y(1); 
    if typevar(prop(1)) == 2 & typevar(prop(3)) == 1
      % Z IS COLUMN VECTOR
      if max(z) ~= y(1)
	disp('       It does not further one')
	disp('          To commit transgressions.')
	disp('          Z argument is the wrong size 500')
	return
      else
	eval(['z = ' argz '(:);'])
      end
      if any(x == nstn)
	eval(['x = ' argx '(sta);'])
      else
	eval(['x = ' argx ';'])
      end
      x = x(:)';
      preface = '[j jj] = sort(x); [k kk] = sort(z);';
      xarg = 'ones(nrow,1)*x(jj)'; 
      zarg = 'z(kk)*ones(1,ncol)';
      if y(2) == nstn
	yarg = [argy  '(kk,sta(jj))'];
      elseif y(2) == ncol
	yarg = [argy '(kk,jj)'];
      else
	disp('       If you are sincere,')
	disp('          You have light and success.')
	disp('          Perseverance brings good fortune.')
	disp('          It furthers one to cross the great water.')
	disp('          Y argument is the wrong size 550')
	return
      end
    elseif typevar(prop(1)) == 1 & typevar(prop(3)) == 2
      % Z IS ROW VECTOR
      if max(x) ~= y(1)
	disp('       Waiting in the mud')
	disp('          Brings about the arrival of the enemy.')
	disp('X argument is the wrong size 600')
	return
      else
	eval(['x = ' argx '(:);'])
      end
      if any(z == nstn)
	eval(['z = ' argz '(sta);'])
      else
	eval(['z = ' argz ';'])
      end
      z = z(:)';
      preface = '[k kk] = sort(x); [j jj] = sort(z);';
      xarg = 'x(kk)*ones(1,ncol)';
      zarg = 'ones(nrow,1)*z(jj)';
      if y(2) == nstn
	yarg = [argy '(kk,sta(jj))'];
      elseif y(2) == ncol
	yarg = [argy '(kk,jj)'];
      else
	disp('       Conflict. You are sincere')
	disp('          And are being obstructed.')
	disp('          Y argument is the wrong size 650')
	return
      end
    else
      disp('    It is not well to strive upward,')
      disp('       It is well to remain below.')
      disp('       Cannot contour this selection of variables.')
      disp('       Please check variable types with ''showtype''.')
      disp('       Use ''settype'' to reset if necessary.')
      return
    end
  elseif ~any(x == 1) & any(y == 1) & any(z == 1)
    % MATRIX VECTOR VECTOR: only X is a matrix
    nrow = x(1); 
    if typevar(prop(1)) == 2 & typevar(prop(2)) == 1
      % Z IS COLUMN VECTOR
      if max(z) ~= x(1)
	disp('       One turns back and submits to fate,')
	disp('          Z argument is the wrong size 700')
	return
      else
	eval(['z = ' argz '(:);'])
      end
      if any(y == nstn)
	eval(['y = ' argy '(sta);'])
      else
	eval(['y = ' argy ';'])
      end
      y = y(:)';
      preface = '[j jj] = sort(y); [k kk] = sort(z);';
      yarg = 'ones(nrow,1)*y(jj)'; 
      zarg = 'z(kk)*ones(1,ncol)';
      if x(2) == nstn
	xarg = [argx  '(kk,sta(jj))'];
      elseif x(2) == ncol
	xarg = [argx '(kk,jj)'];
      else
	disp('       Perchance the army carries corpses in the wagon.')
	disp('          X argument is the wrong size 750')
	return
      end
    elseif typevar(prop(1)) == 1 & typevar(prop(2)) == 2
      % Z IS ROW VECTOR
      if max(y) ~= x(1)
	disp('       The spokes burst out of the wagon wheels.')
	disp('          Husband and wife roll their eyes.')
	disp('          Y argument is the wrong size 800')
	return
      else
	eval(['y = ' argy '(:);'])
      end
      if any(z == nstn)
	eval(['z = ' argz '(sta);'])
      else
	eval(['z = ' argz ';'])
      end
      z = z(:)';
      preface = '[k kk] = sort(y); [j jj] = sort(z);';
      yarg = 'y(kk)*ones(1,ncol)';
      zarg = 'ones(nrow,1)*z(jj)';
      if x(2) == nstn
	xarg = [argx '(kk,sta(jj))'];
      elseif x(2) == ncol
	xarg = [argx '(kk,jj)'];
      else
	disp('       He treads on the tail of the tiger.')
	disp('          The tiger bites him.')
	disp('          X argument is the wrong size 850')
	return
      end
    else
      disp('       Do not act. Be constantly persevering.')
      disp('          Cannot contour this selection of variables.')
      disp('          Please check variable types with ''showtype''.')
      disp('          Use ''settype'' to reset if necessary.')
      return
    end
  elseif any(x == 1) & ~any(y == 1) & ~any(z == 1)
    % VECTOR MATRIX MATRIX: Y and Z are matrices
    if y(1) ~= z(1)
      disp('       When the eighth month comes,')
      disp('          There will be misfortune.')
      disp('          Y or Z is the wrong size 900')
      return
    end
    nrow = z(1);
    if typevar(prop(3)) == 1
      if any(x == nstn)
	eval(['x = ' argx '(sta);'])
      else
	x = eval(argx);
      end
      x = x(:)';
      preface = '[j jj] = sort(x);';
      xarg = 'ones(nrow,1)*x(jj)'; 
      if y(2) == nstn
	yarg = [argy '(:,sta(jj))'];
      elseif y(2) == ncol
	yarg = [argy '(:,jj)'];
      else
	disp('       Splitting Apart. It does not further one')
	disp('          To go anywhere.')
	disp('          Y argument is the wrong size 950')
	return
      end
      if z(2) == nstn
	zarg = [argz '(:,sta(jj))'];
      elseif z(2) == ncol
	zarg = [argz '(:,jj)'];
      else
	disp('       Danger is at hand. It furthers one to desist.')
	disp('          Z argument is the wrong size 1000')
	return
      end
    elseif typevar(prop(3)) == 2
      if max(x) ~= z(1)
	disp('       The ridgepole sags to the breaking point.')
	disp('          X argument is the wrong size 1050')
	return
      else
	eval(['x = ' argx '(:);']);
	preface = '[k kk] = sort(x);';
	xarg = 'x(kk)*ones(1,ncol)';
	if y(2) == nstn
	  yarg = [argy '(kk,sta)'];
	elseif y(2) == ncol
	  yarg = [argy '(kk,:)'];
	else
	  disp('       The abyss is dangerous.')
	  disp('          One should strive to attain small things only.')
	  disp('          Y argument is the wrong size 1100')
	  return
	end
	if z(2) == nstn
	  zarg = [argz '(kk,sta)'];
	elseif z(2) == ncol
	  zarg = [argz '(kk,:)'];
	else
	  disp('       In danger like this, pause at first and wait,')
	  disp('          Otherwise you will fall into a pit in the abyss.')
	  disp('          Do not act in this way.')
	  disp('          Z argument is the wrong size 1150')
	  return
	end
      end
    end
  elseif ~any(x == 1) & any(y == 1) & ~any(z == 1)
    % MATRIX VECTOR MATRIX: X and Z are matrices
    if x(1) ~= z(1)
      disp('          Shut in between thorn-hedged prison walls:')
      disp('       For three years one does not find the way.')
      disp('       Misfortune.')
      disp('       X or Z is the wrong size 1200')
      return
    end
    nrow = z(1);
    if typevar(prop(2)) == 1
      if any(y == nstn)
	eval(['y = ' argy '(sta);'])
      else
	y = eval(argy);
      end
      y = y(:)';
      preface = '[j jj] = sort(y);';
      yarg = 'ones(nrow,1)*y(jj)'; 
      if x(2) == nstn
	xarg = [argx '(:,sta(jj))'];
      elseif x(2) == ncol
	xarg = [argx '(:,jj)'];
      else
	disp('          Tears in floods, sighing and lamenting.')
	disp('      X argument is the wrong size 1250')
	return
      end
      if z(2) == nstn
	zarg = [argz '(:,sta(jj))'];
      elseif z(2) == ncol
	zarg = [argz '(:,jj)'];
      else
	disp('    At the tail in retreat. This is dangerous.')
	disp('       One must not wish to undertake anything.')
	disp('       Z argument is the wrong size 1300')
	return
      end
    elseif typevar(prop(2)) == 2
      if max(y) ~= z(1)
	disp('     Voluntary retreat brings good fortune to the superior man')
	disp('        Y argument is the wrong size 1350')
	return
      else
	y = eval(argy); y = y(:);
	preface = '[k kk] = sort(y);';
	yarg = 'y(kk)*ones(1,ncol)';
	if x(2) == nstn
	  xarg = [argx '(kk,sta)'];
	elseif x(2) == ncol
	  xarg = [argx '(kk,:)'];
	else
	  disp('       To continue is dangerous.')
	  disp('          A goat butts against a hedge')
	  disp('          And gets its horns entangled.')
	  disp('          X argument is the wrong size 1400')
	  return
	end
	if z(2) == nstn
	  zarg = [argz '(kk,sta)'];
	elseif z(2) == ncol
	  zarg = [argz '(kk,:)'];
	else
	  disp('          Nothing serves to further.')
	  disp('       If one notes the difficulty, this brings good fortune.')
	  disp('       Z argument is the wrong size 1450')
	  return
	end
      end
    end
  elseif ~any(x == 1) & ~any(y == 1) & any(z == 1)
    % MATRIX MATRIX VECTOR: X and Y are matrices; weird
    if x(1) ~= y(1)
      disp('       Progress like a hamster.')
      disp('          Perseverance brings danger.')
      disp('          X or Y is the wrong size 1500')
      return
    end
    nrow = y(1);
    if typevar(prop(1)) == 1
      if any(z == nstn)
	eval(['z = ' argz '(sta);'])
      else
	z = eval(argz);
      end
      z = z(:)';
      preface = '[j jj] = sort(z);';
      zarg = 'ones(nrow,1)*z(jj)'; 
      if x(2) == nstn
	xarg = [argx '(:,sta(jj))'];
      elseif x(2) == ncol
	xarg = [argx '(:,jj)'];
      else
	disp('          Perseverance brings humiliation.')
	disp('      X argument is the wrong size 1550')
	return
      end
      if y(2) == nstn
	yarg = [argy '(:,sta(jj))'];
      elseif y(2) == ncol
	yarg = [argy '(:,jj)'];
      else
	disp('       Darkening of the Light. In adversity')
	disp('          It furthers one to be persevering.')
	disp('          Y argument is the wrong size 1600')
	return
      end
    elseif typevar(prop(1)) == 2
      if max(z) ~= y(1)
	disp('       If you lose your horse, do not run after it.')
	disp('          Z argument is the wrong size 1650')
	return
      else
	z = eval(argz); z = z(:);
	preface = '[k kk] = sort(z);';
	zarg = 'z(kk)*ones(1,ncol)';
	if x(2) == nstn
	  xarg = [argx '(kk,sta)'];
	elseif x(2) == ncol
	  xarg = [argx '(kk,:)'];
	else
	  disp('       One sees one''s companion as a pig covered with dirt,')
	  disp('          As a wagon full of devils.')
	  disp('          X argument is the wrong size 1700')
	  return
	end
	if y(2) == nstn
	  yarg = [argy '(kk,sta)'];
	elseif y(2) == ncol
	  yarg = [argy '(kk,:)'];
	else
	  disp('       Obstruction. The southwest furthers.')
	  disp('          The northeast does not further.')
	  disp('          Y argument is the wrong size 1750')
	  return
	end
      end
    end
  elseif ~any(x == 1) & ~any(y == 1) & ~any(z == 1)
    % MATRIX MATRIX MATRIX PLOTS: also easy
    nrow = z(1);
    if (x(1) == nrow) & (y(1) == nrow)
      if z(2) == nstn
	zarg = [argz '(:,sta)'];
      else
	if z(2) == x(2) & z(2) == y(2)
	  zarg = argz;
	else
	  disp('       One falls into the pit.')
	  disp('          Three uninvited guests arrive.')
	  disp('          Honor them, and in the end there will be good fortune.')
	  disp('          Z argument is not the same size as X and/or Y.')
	  return
	end
      end
      if x(2) == nstn
	xarg = [argx '(:,sta)'];
      else
	xarg = argx;
      end
      if y(2) == nstn
	yarg = [argy '(:,sta)'];
      else
	yarg = argy; 
      end
    else
      disp('       It does not further to resort to arms.')
      disp('          X and/or Y is the wrong length 1850')
      return
    end
  end
  % ***************************************************************************
  % FINALLY, WE HAVE CONTOUR ARGUMENTS!!!
  % FIGURE HANDLING
  if isempty(figno), 			% it's a NEW PLOT
    fig_offset = [scr_offset + round(scr_size*0.30)];
    fig_size = round(scr_size*0.50);
    %
    new_fig_handle = figure('NumberTitle','off',...
	'Name',labz, 'units','pixels',...
	'Position',[fig_offset fig_size],'resize','on', ...
	'userdata','staplot','visible','off');
    %      set(0,'currentfigure',new_fig_handle); set(new_fig_handle,'visible','off')
    F_HANDLES = [new_fig_handle F_HANDLES];
    figchars = [zeros(1,4); figchars];
    if (strcmp(labx,'Dist') | strcmp(laby,'Dist'))
      figchars(1,4) = 5;
    else
      figchars(1,4) = 2;
    end
    figchars(1,1:3) = [prop(3) prop(2) prop(1)];
    set(gcf,'visible','on'); drawnow
  else 				% desired plot window already exists
    figure(F_HANDLES(figno))
    clf
  end
  %      END FIGURE HANDLING FOR CONTOUR PLOT
  %      ************ THE BONES: CONTOURING  *****************************
  eval(['xlim = minmax(' argx ');'])
  eval(['ylim = minmax(' argy ');'])
  eval(['zlim = minmax(' argz ');']);
  xlim = ((diff(xlim) + 0.1)*0.04)*[-1 1] + xlim;
  ylim = ((diff(ylim) + 0.1)*0.04)*[-1 1] + ylim;
  
  % set up axis
  axpos = [0.15 0.3 0.8 0.6];
  ax = axes('Units', 'Normalized', ...
      'drawmode','fast','box','on', ...
      'XLimMode','manual', 'XLim', xlim,...
      'YLimMode','manual','YLim', ylim,...
      'Userdata',sta(findnew(sta)));
  if revax == 1, 			% special conditions
    set(ax,'ydir','reverse')
  elseif sqax ==1 ;
    axis('square')
  end
  if strcmp(labx,'Dist') & strcmp(get(gca,'ydir'),'normal')
    if sta_write
      text(0,1,'Sta','units','normalized', ...
	  'horiz','right','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(Dist(i),ylim(2),num, ...
	    'horiz','center','vert','bottom')
      end
    end
  elseif strcmp(labx,'Dist') & strcmp(get(gca,'ydir'),'reverse')
    if sta_write
      text(0,1,'Sta','units','normalized', ...
	  'horiz','right','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(Dist(i),ylim(1), num, ...
	    'horiz','center','vert','bottom')
      end
    end
  end
  if strcmp(laby,'Dist')
    if sta_write
      text(1,1,'Sta','units','normalized', ...
	  'horiz','left','vert','bottom')
      for i = 1:length(sta)
	if exist(sta_var) == 1
	  num = sprintf('num2str(%s(sta(i)))',sta_var);
	  num = [' ' eval(num)];
	else
	  num = [' ' num2str(sta(i))];
	end
	text(xlim(2),Dist(i), num, ...
	    'horiz','left','vert','middle')
      end
    end
  end
  xlabel(labx); ylabel(laby); hold on
  drawnow
  if plot_choice == 3
    % reset axis to leave room for colorbar
    set(ax,'Position', axpos); hold on
    % check z-lims; set contour levels.  You have to set these or
    % the lowest values of your data will not get colored in.
    zlim = ((diff(zlim) + 0.1)*0.04)*[-1 1] + zlim;
    zlim = zlim(1):diff(zlim)./(contno-1):zlim(2);
    % ---------------------------------------------------------------------
    if VERSION < 5
      contarg = [preface 'xcontour(' xarg ',' yarg ',' zarg, ...
	    ',''fill'',zlim); colormap(jet)'];
    else
      contarg = [preface 'contourf(' xarg ',' yarg ',' zarg, ...
	    ',zlim); colormap(jet)'];
    end
    eval(contarg)
    % ---------------------------------------------------------------------
    % COLORBAR  MUST COME AFTER CONTOUR OR CLIMS AREN'T SET CORRECTLY
    if VERSION < 5
      h = colorbr1('horiz'); set(h,'units','normalized',...
	  'Position', [0.15 0.1 0.8 0.06]); drawnow
    else
      h = colorbar('horiz'); set(h,'units','normalized', ...
	  'Position', [0.15 0.1 0.8 0.06]); drawnow
    end
    subplot(h); text(0.5,1,labz,'units','normalized',...
	'horiz','center','vert','bottom')
    % switch to contour axis so that 'cla' works
    axes(ax)
  elseif plot_choice == 4
    % check z-lims; set contour levels.  Round levels
    % to get pretty contour labels -- nothing messy.
%    zlim = ((diff(zlim) + 0.1)*0.04)*[-1 1] + zlim;
%    zlim = zlim(1):diff(zlim)./(contno-1):zlim(2);
    % ---------------------------------------------------------------------
    if VERSION < 5
      contarg = [preface '[clines ch] = xcontour(' xarg ',' yarg ',' zarg, ...
	    ',''label'',contno);'];
    else
      contarg = [preface '[clines] = contours(' xarg ',' yarg ',' ...
	    zarg,',contno); [clines, ch]=xclabel(clines);'];
    end
    eval(contarg)
    % ---------------------------------------------------------------------
    k=1; i = 1;	% find Z-values of each contour line
    while (k < size(clines,2)),
      zlev(i) = clines(1,k);
      nl=clines(2,k);
      k=k+1+nl;
      i=i+1;
    end
    zlim = zlev(sort(findnew(zlev))); clrs = hsv(length(zlim));
    for i = 1:length(zlim); 		% for every contour level
      j = find(zlev == zlim(i)); 	% find the lines that contour it
      for k = 1:length(j)
	set(ch(j(k)),'color',clrs(i,:)) % change to a distinct color
      end
    end
    text(0,-0.05,labz,'units','normalized','horiz','left','vert','top')
  end
end
