function hydroplt(argin,second_arg)

% No help is available for this function.  Please use 'help staplot' (qv),
% or view staplot.txt or staplot.html.  Deirdre Byrne, L-DEO, 96/06/17.

global sta old_sta scr_size scr_offset saveopts_file
global sta_menu F_HANDLES B_HANDLES P_CTRL iso_handles 
global TS_HANDLE GEO_HANDLE edit_win figchars prop Dist
global polon polat switchon switchoff VERSION

% Global options that may be user-specified in the OPTS file
global nstn sections typevar file batlist
global station_style proplist proj projlim colornames
global mapcolors plotcolors plotstyle isopycnals 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

begin = 0;
if isstr(argin)
  if length(argin) > 12
    if strcmp(argin(1:13),'options_file:')
      % STARTING UP      
      opts_file = argin(14:length(argin));
      xlim = []; ylim = [];
      eval(opts_file)
      if ~isempty(file)
	disp(['options file loaded. Data file is: ' file])
	q = input('Do you wish to change the path? y/n [n] >> ','s');
	if isempty(q), q = 'n';	end
	if strcmp(lower(deblank(q)),'y')
	  newpath = input('New path [CR to leave unchanged] >> ','s');
	  if ~isempty(newpath)
	    newpath = fliplr(deblank(fliplr(deblank(newpath))));
	    if ~isempty(newpath)
	      ll = size(newpath,2);
	      dirsep = '/';
	      c = computer;
	      if strcmp(c(1:2),'PC')
		dirsep = '\';
	      elseif strcmp(c(1:2),'MA')
		dirsep = ':';
	      else
		if isvms
		  %shite
		  disp('Sorry, I''m foxed.')
		  return
		end
	      end
	      p = findstr(file,dirsep); 
	      % strip off old path
	      if ~isempty(p)
		file = file(max(p)+1:size(file,2));
	      end
	      if ~strcmp(newpath(ll),dirsep);
		newpath = [newpath dirsep];
	      end
	      file = [newpath file];
	    end
	  end
	end
      end
      begin = 1;
      if ~isempty(deblank(file))
	file = deblank(file); file = fliplr(deblank(fliplr(file)));
	l = length(file);
	if l > 4,
	  if strcmp(file(l-3:l),'.mat'), arg = file; else, arg = [file '.mat']; end
	else
	  arg = [file '.mat'];
	end
	if(exist(arg) ~= 2),
	  disp(['Data file name or path specification incorrect.  ', ...
		  'Please edit options file.'])
	  return
	else
	  s = size(proplist); f = 0;
	  for i = 1:s(1)
	    if strcmp(deblank(proplist(i,:)),'Dist'), f = 1; end
	  end
	  if ~f
	    proplist = str2mat('Dist',proplist);
	    typevar = [1 typevar];
	  end
	  s = size(proplist); str = []; for i = 1:s(1); str = [str ' ']; end
	  str = sprintf('%s',[proplist str']'); eval(['global ' str]); 
	  disp(['loading ' file]); 
	  % take care of weird filenames
	  while (~isempty(findstr(file,'''')))
	    file = strrep(file,'''','');
	  end
	  eval(['load ' '''' file '''']);
	  reloadflag = 0;
	  if nargin == 2
	    oldproplist = second_arg;
	    reloadflag = 1;
	    set(B_HANDLES(4),'Style','Popup','String', proplist)
	    drawnow
	    oo = size(oldproplist,1);
	    for i = 1:oo % now get rid of old variables not on new list
	      k = 0;
	      for j = 1:s
		if strcmp(deblank(oldproplist(i,:)),deblank(proplist(j,:)))
		  k = 1;
		end
	      end
	      if ~k
		if ~strcmp(deblank(oldproplist(i,:)),'Dist')
		  eval(['clear global ' oldproplist(i,:)])
		end
	      end
	    end
	  end
	  
	end
	% if we get this far, save tagname as default session for next startup
	if isempty(saveopts_file)
	  disp('Saving session name to current directory')
	  saveopts_file = 'saveopts.m';
	elseif ~isstr(saveopts_file)
	  disp('Saving session name to current directory')
	  saveopts_file = 'saveopts.m';
	end
	fid = fopen(saveopts_file,'w');
	if fid > -1
	  fprintf(fid,'%s\n',opts_file);
	  fclose(fid);
	else
	  disp('Identity of this session unable to be saved.  Please check')
	  disp('the contents of global variable ''saveopts_file'' in')
	  disp('the main workspace. You may not have  write permission')
	  disp('in the target directory, or possibly it is not extant.')
	end
      else % gvars defaults
	% THIS PART
	reloadflag = 0;
	s = size(proplist); str = []; for i = 1:s(1); str = [str ' ']; end
	str = sprintf('%s',[proplist str']'); eval(['global ' str]);
      end
      %set up vars and workspace
      F_HANDLES = []; prop = [0 0 0];
      TS_HANDLE = []; GEO_HANDLE = []; sta = [];
      nstn = []; old_sta = []; Dist = [];
      figchars = [];
      if ~reloadflag
	% ***************
	B_HANDLES = zeros(14,1);
	sta_menu = [];
	sta = nan; old_sta = nan;

	VERSION = version; VERSION = str2num(VERSION(1));
	if VERSION < 5
	  arg1 = '''backgroundcolor'',dods_colors(1,:)';
	  switchon = 'yes';
	  switchoff = 'no';
	else
	  arg1 = '';
	  switchon = 'on';
	  switchoff = 'off';
	end

        stamenu % call the stamenu script and open the stamenu window
	% window the second: station edit/ID window: put in upper right corner
	fig_size = [round(scr_size(1)*0.70) round(scr_size(2)*0.0625)];
	fig_offset =  scr_size + scr_offset - 1 - fig_size;
	edit_win = figure('units','pixels', ...
	    'position', [fig_offset fig_size], ...
	    'NumberTitle','off', ...
	    'Name','Edit Stations', ...
	    'resize','on'); 
	B_HANDLES(12) = uicontrol( ...
	    'Style','text', ...
	    'Units','normalized', ...
	    'Position', [0.01 0.5 0.98 0.49], ...
	    'HorizontalAlignment','left', ...
	    'BackgroundColor',[1 1 1], ...
	    'String','');
	drawnow
	B_HANDLES(13) = uicontrol( ...
	    'Style','edit', ...
	    'Units','normalized', ...
	    'Position', [0.01 0.01 0.98 0.49], ...
	    'HorizontalAlignment','left', ...
	    'String','',...
	    'Callback','hydroplt(''setsta'')');
	drawnow
	% ************
      end
      % window the third: The Geo window
      if ~isempty(eval(lon_var)) & ~isempty(eval(lat_var))
	if isempty(nstn)
	  nstn = length(eval(lon_var));
	end
	fig_size = round(scr_size*0.40);
	fig_offset = scr_offset + [round(scr_size(1)*0.10)+1 round(scr_size(2)*0.48)];
	%
	GEO_HANDLE = figure('units','pixels','position',[fig_offset fig_size], ...
	    'NumberTitle','off','Name','Stations','resize','on', ...
	    'userdata','staplot','visible','off','windowbuttondownfcn','staid2');
	F_HANDLES = [GEO_HANDLE F_HANDLES];
	figchars = [zeros(1,4); figchars];
	s = size(proplist);
	for i = 1:s(1);
	  arg = fliplr(deblank(fliplr(deblank(proplist(i,:)))));
	  if strcmp(arg, lon_var)
	    figchars(1,1) = i;
	  elseif strcmp(arg, lat_var)
	    figchars(1,2) = i;
	  end
	end
	ax = axes('Units', 'Normalized', 'Position', [0.15 0.1 0.8 0.8],...
	    'drawmode','fast');
	if strcmp(proj,'rec')
	  % figure out some axis limits
	  xlim2 = minmax(eval(lon_var));
	  xlim2 = ((diff(xlim2) + 0.1)*0.04) * [-1 1] + xlim2;
	  if isempty(xlim) | (sort(size(xlim)) ~= [1 2])
	    xlim = xlim2;
	  else 
	    if xlim(1) > xlim2(1), xlim(1) = xlim2(1); end
	    if xlim(2) < xlim2(2), xlim(2) = xlim2(2); end
	  end
	  ylim2 = minmax(eval(lat_var));
	  ylim2 = ((diff(ylim2) + 0.1)*0.04) * [-1 1] + ylim2;
	  if isempty(ylim) | (sort(size(ylim)) ~= [1 2])
	    ylim = ylim2;
	  else 
	    if ylim(1) > ylim2(1), ylim(1) = ylim2(1); end
	    if ylim(2) < ylim2(2), ylim(2) = ylim2(2); end
	  end
%	  xlim = minmax(eval(lon_var));
%	  xlim = ((diff(xlim) + 0.1)*0.04) * [-1 1] + xlim;
%	  ylim = minmax(eval(lat_var));
%	  ylim = ((diff(ylim) + 0.1)*0.04) * [-1 1] + ylim;
  
          plot(eval(lon_var), eval(lat_var), '+', 'color', [0.5 0.5 0.5],...
	      'erasemode', 'none', 'markersize',5); hold on;
	  set(ax,'XLimMode','manual', 'XLim', xlim,...
	      'YLimMode','manual','YLim', ylim,...
	      'Userdata',[1:nstn]')
	elseif strcmp(proj,'north')
	  set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); 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')
	  set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); 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')
      end
    end
  end
end
if ~begin
  s = size(proplist);
  if s(1) ~= length(typevar)
    disp('Property list and variable types do not match!')
    disp('Use ''showtype'' to examine current values.')
    disp('Use ''settype'' to change them.')
    return
  end
  str = []; for i = 1:s(1); str = [str ' ']; end
  str = sprintf('%s',[proplist str']');
  eval(['global ' str]);
  if ~isempty(sta)
    sta = sta(:);
  end
  s = [];
  if ~strcmp(argin,'setsta') & ~strcmp(argin,'gsetsta') % update stations from "sta", 
    if length(sta) == length(old_sta) % unless update is specifically being requested
      if ~isempty(sta)
	if ~all(sta == old_sta)
	  for i = 1:length(sta);
	    s = [s, num2str(sta(i)) ' '];
	  end
	  set(B_HANDLES(13),'string',s);
	  old_sta = sta;
	end
      end
    else
      for i = 1:length(sta);
	s = [s, num2str(sta(i)) ' '];
      end
      set(B_HANDLES(13),'string',s);
      old_sta = sta;
    end
  end
end

if isstr(argin)
  if strcmp(argin,'plotcontrol')
    pix = get(0,'children'); 		% check for live plots
    kids = [];
    for i = 1:length(pix)
      if isstrcmp(get(pix(i),'userdata'),'staplot')
	kids = [kids pix(i)];
      end
    end
    for i = 1:length(kids) 		% find most recent plot that's a staplot plot
      if any(kids(i) == F_HANDLES)
	curr_ax = get(kids(i),'currentaxes');
	break
      end
    end
    set(B_HANDLES(1),'visible','off') % turn off plotstyle to get some room
    str = [];
    % stick in existing axis limits
    if second_arg == 1
      str = sprintf('%g ',get(curr_ax,'xlim'));
    elseif second_arg == 2
      str = sprintf('%g ',get(curr_ax,'ylim'));
    elseif second_arg == 3
      str = sprintf('%g ',get(curr_ax,'zlim'));
    end
    set(P_CTRL(second_arg,1),'visible','on','string',str)
    set(P_CTRL(second_arg,2),'visible','on')
  elseif strcmp(argin,'setview')
    pix = get(0,'children'); 		% check for live plots
    kids = [];
    for i = 1:length(pix)
      if isstrcmp(get(pix(i),'userdata'),'staplot')
	kids = [kids pix(i)];
      end
    end
    for i = 1:length(kids) 		% find most recent plot, a staplot plot
      if any(kids(i) == F_HANDLES)
	figure(kids(i));
	view(second_arg)
	break
      end
    end
  elseif strcmp(argin,'flipud')
    pix = get(0,'children'); 		% check for live plots
    kids = [];
    for i = 1:length(pix)
      if isstrcmp(get(pix(i),'userdata'),'staplot')
	kids = [kids pix(i)];
      end
    end
    for i = 1:length(kids) 		% find most recent plot, a staplot plot
      if any(kids(i) == F_HANDLES)
	figure(kids(i)); ax = gca;
	if max(size(axis)) == 4
	  arg = get(ax,'ydir');
	  if strcmp(arg,'normal');
	    set(gca,'ydir','reverse')
	  elseif strcmp(arg,'reverse');
	    set(gca,'ydir','normal')
	  end
	else
	  arg = get(ax,'zdir');
	  if strcmp(arg,'normal');
	    set(gca,'zdir','reverse')
	  elseif strcmp(arg,'reverse');
	    set(gca,'zdir','normal')
	  end
	end
	break
      end
    end
  elseif strcmp(argin,'tsedit'), 	% turn zoom off and stasel off and tsedit on
    if get(B_HANDLES(14),'value')
      zoom off
      set(B_HANDLES(2),'backgroundcolor',[0.702 0.702 0.702],'value',0)
      set(B_HANDLES(14),'backgroundcolor',[1 0 0])
      tsedit(1)
    else
      tsedit(0)
      set(B_HANDLES(14),'backgroundcolor',[0.702 0.702 0.702])
    end
  elseif strcmp(argin,'stasel'), 	% turn zoom off and stasel on
    pix = get(0,'children'); 		% check for live plots
    kids = [];
    for i = 1:length(pix)
      if isstrcmp(get(pix(i),'userdata'),'staplot')
	kids = [kids pix(i)];
      end
    end
    for i = 1:length(kids) 		% find most recent plot that's a staplot plot
      if any(kids(i) == F_HANDLES)
	figure(kids(i));
	break
      end
    end
    if (get(B_HANDLES(2),'value')),
      zoom off
      if B_HANDLES(14) > 0
	if ~isempty(TS_HANDLE)
	  if ~isempty(findobj(TS_HANDLE,'type','uicontrol','style','radiobutton'))
	    set(B_HANDLES(14),'backgroundcolor',[0.702 0.702 0.702],'value',0)
	    tsedit(0)
	  end
	end
      end
      % this must come after turning tsedit off ... 
      stasel(1);
      set(B_HANDLES(2),'backgroundcolor',[1 0 0])
    else
      stasel(0);
      set(B_HANDLES(2),'backgroundcolor',[0.702 0.702 0.702])
      % turn staid2 on again
      if any(findobj(get(0,'children'),'userdata','staplot') == GEO_HANDLE)
	set(GEO_HANDLE,'windowbuttondownfcn','staid2')
      end
    end
%    if (get(B_HANDLES(2),'value')),
%      zoom off
%      stasel(1);
%      set(B_HANDLES(2),'backgroundcolor',[1 0 0])
%    else
%      stasel(0);
%      set(B_HANDLES(2),'backgroundcolor',[0.702 0.702 0.702])
%    end
  elseif strcmp(argin,'selall') % select all stations
    sect = get(B_HANDLES(3),'value')-1;
    if sect == 0
      if ~isempty(nstn)
	sta = 1:nstn;
	hydroplt('gsetsta')
      elseif ~isempty(eval(lon_var))
	if isempty(nstn)
	  nstn = length(eval(lon_var));
	end
	sta = 1:nstn;
	hydroplt('gsetsta')
      end
    else % there are sections
      sta = sections(sect,1):sections(sect,2);
      hydroplt('gsetsta')
    end
  elseif strcmp(argin,'zoom'), 		% turn zoom on or off
    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
    for i = 1:length(kids) 		% find most recent plot that's a staplot plot
      if any(kids(i) == F_HANDLES)
	figure(kids(i));
	break
      end
    end
    zoom
    if gcf == GEO_HANDLE & isempty(get(gcf,'windowbuttondownfcn'))
      set(GEO_HANDLE,'windowbuttondownfcn','staid2')
    end
  elseif strcmp(argin,'change_az_slide')
    kids = get(gcf,'children');
    for i = 1:length(kids)
      if strcmp(get(kids(i),'type'),'uicontrol')
	if strcmp(get(kids(i),'userdata'),'azimuth')
	  if strcmp(get(kids(i),'style'),'slider')
	    h(1) = kids(i);
          elseif strcmp(get(kids(i),'style'),'edit')
	    h(2) = kids(i);
	  end
	end
      end
    end
    new_az = (360*get(h(1),'value'))-180; [az,el] = view;
    view(new_az,el); set(h(2),'string',num2str(round(new_az)))
  elseif strcmp(argin,'change_az_edit')
    kids = get(gcf,'children');
    for i = 1:length(kids)
      if strcmp(get(kids(i),'type'),'uicontrol')
	if strcmp(get(kids(i),'userdata'),'azimuth')
	  if strcmp(get(kids(i),'style'),'edit')
	    h(1) = kids(i);
          elseif strcmp(get(kids(i),'style'),'slider')
	    h(2) = kids(i);
	  end
	end
      end
    end
    new_az = str2num(get(h(1),'string')); [az,el] = view;
    view(new_az,el); set(h(2),'value',(new_az+180)/360)
  elseif strcmp(argin,'change_el_slide')
    kids = get(gcf,'children');
    for i = 1:length(kids)
      if strcmp(get(kids(i),'type'),'uicontrol')
	if strcmp(get(kids(i),'userdata'),'elevation')
	  if strcmp(get(kids(i),'style'),'slider')
	    h(1) = kids(i);
	  elseif strcmp(get(kids(i),'style'),'edit')
	    h(2) = kids(i);
	  end
	end
      end
    end
    new_el = (180*get(h(1),'value'))-90; [az,el] = view;
    view(az,new_el); set(h(2),'string',num2str(round(new_el)))
  elseif strcmp(argin,'change_el_edit')
    kids = get(gcf,'children');
    for i = 1:length(kids)
      if strcmp(get(kids(i),'type'),'uicontrol')
	if strcmp(get(kids(i),'userdata'),'elevation')
	  if strcmp(get(kids(i),'style'),'edit')
	    h(1) = kids(i);
	  elseif strcmp(get(kids(i),'style'),'slider')
	    h(2) = kids(i);
	  end
	end
      end
    end
    new_el = str2num(get(h(1),'string')); [az,el] = view;
    view(az,new_el); set(h(2),'value',(new_el+90)/180)    
  elseif strcmp(argin,'setcolor')
    sta = sta(:);
    station_style(sta) = 10*second_arg*ones(1,length(sta))+ ...
	rem(station_style(sta),10);
  elseif strcmp(argin,'setstyle')
    sta = sta(:);
    S = get(B_HANDLES(1),'value');
    station_style(sta) = (station_style(sta)-rem(station_style(sta),10)) + ...
    S * ones(1,length(sta));
  elseif strcmp(argin,'prop')
    prop(3) = prop(2);
    prop(2) = prop(1);
    prop(1) = get(B_HANDLES(4),'value');
  elseif strcmp(argin,'grid')
    % check for live staplot figs
    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
    for i = 1:length(kids) 		% find most recent plot that's a staplot plot
      if any(kids(i) == F_HANDLES)
	figure(kids(i)); grid
	break
      end
    end
  elseif strcmp(argin,'staid')
    staid(B_HANDLES(12));
  elseif strcmp(argin,'gsetsta')
    for i = 1:length(sta);
      s = [s, num2str(sta(i)) ' '];
    end
    set(B_HANDLES(13),'string',s);
  elseif strcmp(argin,'setsta')
    sta = [];
    ssta = get(B_HANDLES(13),'string');
    if ~isempty(ssta),
      while(1)
	if ~isempty(findstr(ssta,[32 32]))
	  ssta = strrep(ssta,[32 32],[32]);
	else
	  break
	end
      end
      i = findstr(ssta,[32]);
      if ~any(i==1)
	i = [1 i];
      end
      l = size(ssta,2);
      if ~any(i == l);
	i = [i l];
      end
      for j = 2:length(i)
	sta(j-1) = str2num(ssta(i(j-1):i(j)));
      end
      sta = sta(:);
    end
  elseif strcmp(argin,'clear')
    toggle = get(B_HANDLES(6),'value');
    if (toggle == 1) 			% clear figure
      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
      for i = 1:length(kids)
	if any(kids(i) == F_HANDLES)
	  figure(kids(i));
	  break
	end
      end
      cla;
      set(gca,'userdata',[])
      if ~isempty(TS_HANDLE)
	if gcf == TS_HANDLE
	  iso_handles = []; refresh(gcf)
	end
      end
      % find if this is geoplot
      if ~isempty(GEO_HANDLE)
	if(gcf == GEO_HANDLE)
	  ax = gca;
	  if ~isempty(eval(lon_var)) & ~isempty(eval(lat_var))
	    if isempty(nstn)
	      nstn = length(eval(lon_var));
	    end
	    if strcmp(proj,'rec')
	      xlim = minmax(eval(lon_var));
	      xlim = ((diff(xlim) + 0.1)*0.04) * [-1 1] + xlim;
	      ylim = minmax(eval(lat_var));
	      ylim = ((diff(ylim) + 0.1)*0.04) * [-1 1] + ylim;
	      plot(eval(lon_var), eval(lat_var), '+', 'color', [0.5 0.5 0.5],...
		  'erasemode', 'none', 'markersize',5); hold on;
	      set(ax,'XLimMode','manual', 'XLim', xlim,...
		  'YLimMode','manual','YLim', ylim,...
		  'Userdata',[1:nstn]')
	    elseif strcmp(proj,'north')
	      set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); 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')
	      set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); 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
	  end
	end
      end
    elseif (toggle == 2) 		% clear stations
      sta = [];
      set(B_HANDLES(13),'string','');
    elseif (toggle == 3) 		% clear isolines
      k = get(gca,'children');
      for i = 1:length(iso_handles),
	if any(iso_handles(i) == k)
	  delete(iso_handles(i))
	end
      end; 
      iso_handles = [];
      k = get(gcf,'children');
      if ~isempty(TS_HANDLE)
	if any(k == TS_HANDLE)
	  if strcmp(get(TS_HANDLE,'userdata'),'staplot')
	    refresh(TS_HANDLE);
	  end
	end
      end
    elseif (toggle == 4) 		% clear text
      set(B_HANDLES(12),'string','');
    end
  elseif strcmp(argin,'load')
    action = get(B_HANDLES(9),'value');
    if action == 1
      loaddata 				% a long and messy script
    elseif action == 2
      add_data 				% yet another ....
    end
  elseif strcmp(argin,'replot') % re-evaluate proplist after reloading data
    fig_size = round(scr_size*0.40);
    fig_offset = scr_offset + [round(scr_size(1)*0.10)+1 round(scr_size(2)*0.48)];
    %
    GEO_HANDLE = figure('units','pixels','position',[fig_offset fig_size], ...
	'NumberTitle','off','Name','Stations','resize','on', ...
	'userdata','staplot','visible','off','windowbuttondownfcn','staid2');
    set(0,'currentfigure',GEO_HANDLE)
    F_HANDLES = [GEO_HANDLE F_HANDLES];
    figchars = [zeros(1,4)];
    gd = 0;
    s = size(proplist);
    for i = 1:s(1),
      arg = fliplr(deblank(fliplr(deblank(proplist(i,:)))));
      if strcmp(arg, lon_var)
	figchars(1,1) = i;
	gd = gd + 1;
      elseif strcmp(arg, 'Dist')
	figchars(1,2) = i;
	gd = gd + 1;
      elseif strcmp(arg, lat_var)
	figchars(1,2) = i;
	gd = gd + 1;
      end
    end
    if gd ~= 3
      disp('Please use ''showtype'' and ''settype'' to correct your variable')
      disp('list, or save your options, quit STAPLOT, and edit the options file')
      disp('before restarting.  The assigned variable types do not match the')
      disp('defined variables, or else one of the required variables, latitude')
      disp('longitude or station-to-station distance (''Dist''), is missing.')
      return
    end
    ax = axes('Units', 'Normalized', 'Position', [0.15 0.1 0.8 0.8],...
	'drawmode','fast');
    if strcmp(proj,'rec')
      xlim = minmax(eval(lon_var));
      xlim = ((diff(xlim) + 0.1)*0.04) * [-1 1] + xlim;
      ylim = minmax(eval(lat_var));
      ylim = ((diff(ylim) + 0.1)*0.04) * [-1 1] + ylim;
      plot(eval(lon_var), eval(lat_var), '+', 'color', [0.5 0.5 0.5],...
	  'erasemode', 'none', 'markersize',5); hold on;
      set(ax,'XLimMode','manual', 'XLim', xlim,...
	  'YLimMode','manual','YLim', ylim,...
	  'Userdata',[1:nstn]')
    elseif strcmp(proj,'north')
      set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); 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')
      set(ax,'visible','off','aspect',[1 1],'userdata',[1:nstn]'); 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')
  elseif strcmp(argin,'maps')
    map = get(B_HANDLES(7),'value');
    if map == 1
      if (~isempty(eval(lon_var)) & ~isempty(eval(lat_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
	% check for valid staplot figs
	i = findold(F_HANDLES,kids);
	F_HANDLES = F_HANDLES(i);
	figchars = figchars(i,:);
	fig_offset = scr_offset + round(scr_size*0.10) + 2;
	fig_size = round(scr_size*0.30);
	new_fig_handle = figure('NumberTitle','off','Name','Worldmap',...
	    'units','pixels','resize','on','Position',[fig_offset fig_size], ...
	    'userdata','staplot');
	figure(new_fig_handle)
	F_HANDLES = [new_fig_handle F_HANDLES];
	figchars = [zeros(1,4); figchars];
	if strcmp(proj,'rec')
	  if any(eval(lon_var) < -180)
	    coast; hold on; 	% axes will be [-334 25 -90 90]
	  elseif any(eval(lon_var) > 180)
	    coast([0 360 -90 90]); hold on;
	  else
	    coast([-180 180 -90 90]); hold on
	  end
	else
	  if isempty(projlim)
	    projlim = 0;
	  else
	    projlim = abs(projlim);
	  end
	  coast(proj,projlim); hold on
	end
      end
    else
      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
      for i = 1:length(kids) 		% find most recent plot that's a staplot plot
	if any(kids(i) == F_HANDLES)
	  figure(kids(i));
	  break
	end
      end
      if strcmp(proj,'rec')
	lims = [get(gca,'Xlim') get(gca,'Ylim')];
      else
	lims = proj;
      end
      if isempty(projlim)
	projlim = 0;
      else
	projlim = abs(projlim);
      end
      if map > 1
	map = map-1;
	s = size(batlist,1);
	ss = size(mapcolors,1);
	if s <= ss;
	  topomap(batlist(map,:),lims,mapcolors(map,:),projlim)
	else
	  disp('More bathymetry files than mapcolors!  Please fix ')
	  disp('this in your options file.  Plotting in white.')
	  topomap(batlist(map,:),lims,'w',projlim)
	end
      end
    end
  elseif strcmp(argin,'sigmas')
    if ~isempty(TS_HANDLE)
      figure(TS_HANDLE)
      % get pressure level for isopycnal contours
      lvl = get(B_HANDLES(8),'value');
      pr = (lvl-1)*1000;
      % calculate and plot isopycnals
      iso_handles = [iso_handles (isopycs(TS_HANDLE,pr,isopycnals(lvl,:)))'];
    else, disp('Please open a TS window first')
    end
  elseif strcmp(argin,'save')
    s = get(B_HANDLES(10),'value');
    if s == 1
      stasave % a long and messy script
    else
      disp('Note that only the variables on the plot list will be saved.')
      disp('Please specify a fully qualified path with the file if you.')
      disp('want to save it anywhere other than the current directory.')
      disp('To save the session options, use ''saveopts''.')
      file = input('Data file name ([CR] to abort) >> ','s');
      if ~isempty(file)
	file = deblank(file);
	file = fliplr(deblank(fliplr(file)));
	while (~isempty(findstr(file,'''')))
	  file = strrep(file,'''','');
	end
	l = length(file);
	if l > 4,
	  if strcmp(file(l-3:l),'.mat')
	    file = file(1:l-4);
	  end
	end
	sss = []; s = size(proplist); for i = 1:s(1); sss = [sss ' ']; end
	sss = sprintf('%s',[proplist sss']');
	% take care of weird filenames
	while (~isempty(findstr(file,'''')))
	  file = strrep(file,'''','');
	end
	sss = ['save ''' file ''' ' sss];
	disp(sss)
	eval(sss)
	sss = sprintf('\n'); disp(sss)
      end
    end
  elseif strcmp(argin,'addprop')
    action = get(B_HANDLES(11),'value');
    if action == 1
      disp('New variable must be a global var in the main workspace!');
      sss= input('Property to add (CR to abort) >> ','s');
      if ~isempty(sss)
	disp('Variable types:')
	disp('1. one value per station')
	disp('2. one value per depth')
	disp('3. one column per station')
	tvar = input('Variable type (1 2 or 3)? >> ');
	if isempty(tvar), tvar = 0; end
	proplist = str2mat(proplist,sss); 
	typevar = [typevar tvar];
	s = size(proplist); str = []; for i = 1:s(1); str = [str ' ']; end
        str = sprintf('%s',[proplist str']');
	eval(['global ' str]);
	disp('Special variables are the following.  Is the new variable one of these?')
	disp('1. Latitude')
	disp('2. Longitude')
	disp('3. Station number')
	disp('4. Pressure')
	disp('5. Potential Temperature')
	disp('6. Salinity')
	disp('7. Depth')
	disp('8. In situ Temperature')
	q = input('If so, input that number here, (else [CR]) >> ','s');
	if ~isempty(q)
	  q = str2num(deblank(q));
	  if q == 1
	    lat_var = sss;
	  elseif q == 2
	    lon_var = sss;
	  elseif q == 3
	    sta_var = sss;
	  elseif q == 4
	    pr_var = sss;
	    pr_reverse = input(['Pressure positive, increasing downwards? (Boolean:',...
		    ' 1 = yes, 0 = no) >> ']);
	  elseif q == 5
	    pt_var = sss;
	  elseif q == 6
	    sa_var = sss;
	  elseif q == 7
	    de_var = sss;
	    de_reverse = input(['Depth positive, increasing downwards? (Boolean:', ...
		    ' 1 = yes, 0 = no) >> ']);
	  elseif q == 2
	    lon_var = sss;
	  else
	    disp('Not a recognized property number.')
	  end
	end
	set(B_HANDLES(4),'Style','Popup','String',...
	    proplist)
	drawnow
      end
    elseif action == 2
      disp('Note: This will NOT delete the variable from your workspace,')
      disp('but you will lose any plots that contain this variable.')
      rmvar = input('Name of variable to delete [CR to abort] >> ','s');
      if ~isempty(rmvar)
	rmvar = deblank(fliplr(deblank(fliplr(rmvar))));
	if ~isempty(rmvar)
	  if strcmp('Dist',rmvar)
	    disp('Distance variable (Dist) may not be removed')
	    return
	  elseif strcmp(lon_var,rmvar)
	    disp(['Longitude variable ''' lon_var ''' may not be removed'])
	    return
	  elseif strcmp(lat_var,rmvar)
	    disp(['Latitude variable ''' lat_var ''' may not be removed'])
	    return
	  end
	  s = size(proplist); ss = max(size(typevar));
	  if s(1) ~= ss
	    disp('Number of variables and defined variable types does not match!')
	    disp('You will have to edit the options file for the dataset manually')
	    disp('and restart STAPLOT.')
	    return
	  end
	  for i = 1:s(1)
	    if strcmp(deblank(proplist(i,:)),rmvar)
	      delvar = i;
	      proplist = [proplist(1:delvar-1,:); proplist(delvar+1:s(1),:)];
	      typevar = typevar(:)';
	      typevar = [typevar(1:delvar-1) typevar(delvar+1:s(1))];
	      j = find(prop == delvar);
	      if ~isempty(j)
		prop(j) = 0*j;
	      end
	      j = find(prop > delvar);
	      if ~isempty(j)
		prop(j) = prop(j)-1;
	      end
	      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
	      % check for valid staplot fignos
	      ii = findold(F_HANDLES,kids);
	      F_HANDLES = F_HANDLES(ii);
	      figchars = figchars(ii,:);
	      % close plots containing deleted variable
	      if ~isempty(figchars)
		figno = find ((figchars(:,1) == delvar) | ...
		    (figchars(:,2) == delvar) | (figchars(:,3) == delvar));
		if ~isempty(figno),
		  for jj = 1:length(figno)
		    close(F_HANDLES(figno(jj)));
		  end
		  goodnos = find((figchars(:,1) ~= delvar) & ...
		      (figchars(:,2) ~= delvar) & (figchars(:,3) ~= delvar));
		  figchars = figchars(goodnos,:);
		  F_HANDLES = F_HANDLES(goodnos);
		end
	      end
	      set(B_HANDLES(4),'Style','Popup','String',...
		  proplist)
	      drawnow
	      break
	    end
	  end
	else
	  return
	end
	return
      end
    end
  elseif strcmp(argin,'QUIT')
    kids = get(0,'children');
    for i = 1:length(kids)
      if isstrcmp(get(kids(i),'userdata'),'staplot')
	close(kids(i))
      elseif any(kids(i) == edit_win)
	close(edit_win)
      end
    end
    close(sta_menu)
    % *************************************************************** %       
  elseif strcmp(argin,'plot')
    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
    % check for valid staplot fignos
    i = findold(F_HANDLES,kids);
    F_HANDLES = F_HANDLES(i);
    figchars = figchars(i,:);
    plot_choice = get(B_HANDLES(5),'value');
    sta_plot(plot_choice) 		% a long and messy function
  end
end
clear C S action ans arg argin argx argy argz ax axpos az backs begin breaks 
clear buttonstring 
clear c c_label call callbacks callbackstring clrs cyc 
clear delvar dirsep el f fid figno fname 
clear fig_offset fig_size figno gd genlist gvars goodnos h i ii index j jj k kids kk
clear l ll label labels labx laby labz lims lvl map new_fig_handle newlist newpath
clear newstring new_az new_el nl nyl oo opts_file ofile oldproplist
clear p pix plot_choice posit pr q reloadflag rept revax rmvar
clear s scr_offset scr_size sect spcs sqax 
clear ss sss ssta str second_arg sz tab temp toggle tvar ui_colors ui_plot
clear x xarg xlim xmult xx y yarg ylim ymult yy z zarg zlev zlim zlm zmult 
return
