function [ii]=gxytzoom(arg1,arg2,arg3)
% [ii]=gxytzoom(x,y,t)
% zoom on axes: x,y  
%               t,y  
%               x,y  
% or reset to original axes.
% closes the window and returns the indices chosen when you're done.
%
%this gets called once with x,y,t and you click on things till you're accept.
% BUGS:
% no checking to make the only active part in the current axes
  
  
if (nargin==0)
  disp('using synthetic data')
  arg1=[-2*pi:pi/64:2*pi]';
  arg2=sin(arg1);
  arg3=20*exp(-(arg1/10).^2);
elseif (nargin==1)
  %passing in a handle with a data structure
  if (~isstruct(arg1))
	help gxytzoom
	error('one argument as assumed to be a data structure')
  else
	x=arg1.x;
	y=arg1.y;
	t=arg1.dday;
  end
elseif (nargin ~= 3)
  help gxytzoom
  error('gxytzoom expects 0,1, or 3 arguments')
end

global GHH  %global handle

%start things up  ----------

GHH.fig = figure(999);
set(GHH.fig,'Tag','zoomfig');
call_init;  %make the stuff for the window
GHH.x=arg1(:);
GHH.y=arg2(:);
GHH.t=arg3(:);
GHH.II=[1:length(arg1)];
draw_plots

disp('zoom with the left button')
GHH.newaxisselected=0;
GHH.reallyquit=0;
GHH.resetaxes=0;
keepgoing = 1;
while ( ~GHH.reallyquit ) & ...
	  ( keepgoing  | ...
	  ~strcmp( get(gcf,'selectiontype'), 'alt') | ...
	  ~strcmp( get(gcf,'selectiontype'), 'extend')  )
  
  %left-click
  keepgoing = 0;
  GHH.newaxisselected=0;
  waitforbuttonpress %can select a new axis, can zoom  or can quit
  
  if (strcmp( get(gcf,'selectiontype'), 'normal'))
	disp(' ')
	if (~GHH.newaxisselected)  %i.e. we did not changed axes, so we zoomed
	  GHH.nuax=rbbox;
	  resize_axes
	end
  else
	disp('must use left button only')
  end
  
end

endit;
ii=GHH.II;
close(GHH.fig);
%clear globals

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              functions                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function   call_init      

  global GHH

  
  set(0,'defaultaxesxgrid','on')
  set(0,'defaultaxesygrid','on')
  if strcmp('off',get(0,'defaultaxesbox'))
	set(0,'defaultaxesbox','on')
	GHH.BOX_OFF = 1;
  else
	GHH.BOX_OFF = 0;
  end

  figure(999);clf;
  GHH.fig= figure(999);
  set(GHH.fig,'Color',[0.8 0.8 0.8], ...
	  'Position',[509 334 512 384], ...
	  'Tag','Fig999');

  %make top right x/y  - this one starts as the active one
  GHH.axesXY = axes('Parent',GHH.fig,...
	  'Position',[.4 .4 .5 .5],'color',[.9 1 1],...
	  'xaxisloc','top','yaxisloc','right');
  xlabel('longitude')
  ylabel('latitude')
  GHH.cur_zm=GHH.axesXY;

  %make bottom right axis: xt
  GHH.axesXT = axes('Parent',GHH.fig,...
	  'Position',[.4 .1 .5 .3],'yaxisloc','right');
  xlabel('longitude')
  ylabel('time')
  
  %make the top left axes:
  GHH.axesTY = axes('Parent',GHH.fig,...
	  'Position',[.1 .4 .3 .5],'xaxisloc','top');
  xlabel('time')
  ylabel('latitude')
  

  GHH.axesall = axes('Parent',GHH.fig,...
	  'Position',[.1 .1 .3 .3],'color',[.8 .8 .8]);
  xlabel('longitude')
  ylabel('latitude')

  GHH.acceptbutton = uicontrol('Parent',GHH.fig, ...
	  'Units','normalized',...
	  'BackgroundColor',[1 0 0], ...
	  'Callback','call_accept',...
	  'Position',[.9 .93 .1 .05],...
	  'fontsize',8,...
	  'String','ACCEPT');
  GHH.resetbutton = uicontrol('Parent',GHH.fig, ...
	  'Units','normalized', ...
	  'BackgroundColor',[0 0 1], ...
	  'ForegroundColor',[1 1 1], ...
	  'Callback','call_reset',...
	  'Position',[.7 .93 .15 .05],...
	  'fontsize',8,...
	  'String','RESET AXES');

  pp=get(GHH.axesTY,'position');
  GHH.tybutton = uicontrol('Parent',GHH.fig, ...
          'Units','normalized', ...
          'Callback','call_ty',...
          'Position',[.01+pp(1) .01+pp(2) .05 .05],...
          'String','t/y');

  pp=get(GHH.axesXY,'position');
  GHH.xybutton = uicontrol('Parent',GHH.fig, ...
          'Units','normalized', ...
          'Callback','call_xy',...
          'Position',[.01+pp(1) .01+pp(2) .05 .05],...
          'String','x/y');

  pp=get(GHH.axesXT,'position');
  GHH.txbutton = uicontrol('Parent',GHH.fig, ...
          'Units','normalized', ...
          'Callback','call_xt',...
          'Position',[.01+pp(1) .01+pp(2) .05 .05],...
          'String','x/t');  

% ------------------------------
function   call_xy; 
  global GHH

  %choose a new axis, redraw at current scale
  set(GHH.fig,'currentaxes',GHH.axesXY);
  GHH.newaxisselected=1;
  set(GHH.cur_zm,'color',[1 1 1])
  GHH.cur_zm = gca;
  set(GHH.cur_zm,'color',[.9 1 1])
   
  disp(sprintf('now zoom in on the XY axis using the left button'))
  disp(sprintf('start in the light blue region!!'))
  
% ------------------------------
function   call_xt;
  global GHH

  %choose a new axis, redraw at current scale
  set(GHH.fig,'currentaxes',GHH.axesXT);
  GHH.newaxisselected=1;
  set(GHH.cur_zm,'color',[1 1 1])
  GHH.cur_zm = gca;
  set(GHH.cur_zm,'color',[.9 1 1])
  disp(sprintf('now zoom in on the XT axis using the left button'))
  disp(sprintf('start in the light blue region!!'))
  
% ------------------------------
function   call_ty;
  global GHH

  %choose a new axis, redraw at current scale
  set(GHH.fig,'currentaxes',GHH.axesTY);
  GHH.newaxisselected=1;
  set(GHH.cur_zm,'color',[1 1 1])
  GHH.cur_zm = gca;
  set(GHH.cur_zm,'color',[.9 1 1])
  disp(sprintf('now zoom in on the TY axis using the left button'))
  disp(sprintf('start in the light blue region!!'))
  
%end of call_init
%------------------------------  
function   call_accept;
  global GHH
  
  GHH.reallyquit=1;

% ------------------------------
function   call_reset;
  global GHH

  disp('resetting to original axis limits')
  GHH.II = 1:length(GHH.x);
  draw_plots
  GHH.resetaxes=1;
  
%----------------------------------------------------------------------	
function resize_axes
  
  global GHH
  
  XX=GHH.x;
  YY=GHH.y;
  TT=GHH.t;

  nuax=GHH.nuax;
  if (GHH.resetaxes==1)
	GHH.resetaxes=0;
  else
	% return if it's a mistake (small region and not a button)
	if (nuax(3)<10 | nuax(4)<10 ) & (GHH.newaxisselected | GHH.reallyquit)
	  return %do nothing
	elseif  (nuax(3)<10 | nuax(4)<10 ) %just too small
	  disp('not redrawing: zoom is too small to register')
	  return
	else
	end
	
	% determine the two corners of the selected box in pixels
	x1 = nuax(1);
	y1 = nuax(2);
	x2 = x1 + nuax(3);
	y2 = y1 + nuax(4);
	
	% translate pixels to relative coordinates
	
	t1 = get(gcf,'posit');
	x1 = x1/t1(3);
	x2 = x2/t1(3);
	y1 = y1/t1(4);
	y2 = y2/t1(4);
	
	% translate relative coordinates to axis coordinates
	
	t1 = get(GHH.cur_zm,'posit');
	
	x1 = x1 - t1(1);
	x2 = x2 - t1(1);
	y1 = y1 - t1(2);
	y2 = y2 - t1(2);
	
	xlm = get(GHH.cur_zm,'xlim');
	ylm = get(GHH.cur_zm,'ylim');
	
	x1 = x1/t1(3)*diff(xlm)+xlm(1);
	x2 = x2/t1(3)*diff(xlm)+xlm(1);
	y1 = y1/t1(4)*diff(ylm)+ylm(1);
	y2 = y2/t1(4)*diff(ylm)+ylm(1);
	
	t1 = min([x1 x2]);
	t2 = max([x1 x2]);
	t3 = min([y1 y2]);
	t4 = max([y1 y2]);
	if GHH.cur_zm==GHH.axesXY
	  t1 = find(XX(GHH.II)>t1 & XX(GHH.II)<t2 & YY(GHH.II)>t3 & YY(GHH.II)<t4);
	elseif GHH.cur_zm==GHH.axesXT
	  t1 = find(XX(GHH.II)>t1 & XX(GHH.II)<t2 & TT(GHH.II)>t3 & TT(GHH.II)<t4);
	else
	  t1 = find(TT(GHH.II)>t1 & TT(GHH.II)<t2 & YY(GHH.II)>t3 & YY(GHH.II)<t4);
	end
	GHH.II = GHH.II(t1);
	
	draw_plots
  end
	
%------------------------------
function draw_plots
  global GHH
  
  XX=GHH.x;
  YY=GHH.y;
  TT=GHH.t;

  axes(GHH.axesXY);
  delete( findobj(gca,'type','line') )
  line(XX(GHH.II),YY(GHH.II),'marker','.')
  set(GHH.axesXY,'xtickmode','auto')
  xts = get(GHH.axesXY,'xtick');
  set(GHH.axesXY,'xtick',xts(2:end));
  set(GHH.axesXY,'ytickmode','auto')
  yts = get(GHH.axesXY,'ytick');
  set(GHH.axesXY,'ytick',yts(2:end));

  axes(GHH.axesall)
  delete( findobj(gca,'type','line') )
  line(XX,YY,'marker','.','color','b')
  line(XX(GHH.II),YY(GHH.II),'marker','.','color','r')
  set(GHH.axesall,'xtickmode','auto')
  xts = get(GHH.axesall,'xtick');
  set(GHH.axesall,'xtick',xts(1:end-1));
  set(GHH.axesall,'ytickmode','auto')
  yts = get(GHH.axesall,'ytick');
  set(GHH.axesall,'ytick',yts(1:end-1));

  axes(GHH.axesXT)
  delete( findobj(gca,'type','line') )
  line(XX(GHH.II),TT(GHH.II),'marker','.')
  set(GHH.axesXT,'xtickmode','auto')
  xts = get(GHH.axesXT,'xtick');
  set(GHH.axesXT,'xtick',xts(1:end-1));
  set(GHH.axesXT,'ytickmode','auto')
  yts = get(GHH.axesXT,'ytick');
  set(GHH.axesXT,'ytick',yts(2:end));

  axes(GHH.axesTY)
  delete( findobj(gca,'type','line') )
  line(TT(GHH.II),YY(GHH.II),'marker','.')
  set(GHH.axesTY,'xtickmode','auto')
  xts = get(GHH.axesTY,'xtick');
  set(GHH.axesTY,'xtick',xts(2:end));
  set(GHH.axesTY,'ytickmode','auto')
  yts = get(GHH.axesTY,'ytick');
  set(GHH.axesTY,'ytick',yts(1:end-1));

  %end of function draw_plots

  
%------------------------------

function endit
  
  global GHH
  
  % increase typing in workspace
  set(0,'defaultaxesxgrid','off')
  set(0,'defaultaxesygrid','off')
  if GHH.BOX_OFF
	set(0,'defaultaxesbox','off')
  end

