function rs = xyt_zoom(x,y,t);

% rs = xyt_zoom(x,y,t);
%
% xyt_zoom enables the user to select portions of a cruise track in
% the x-y, x-t, and/or the y-t domains.  xyt_zoom returns a vector
% of indices of the selected cruise track portion - i.e., the selected
% track is x(rs), y(rs), and t(rs).  the xyt_zoom interface works as
% follows:  
%
%		active (light blue) window	inactive (white) window
% left mouse     zooms in on selected area	 no effect
% middle mouse   resets axes to original	 selects active window
% right mouse    exit				 exit
%
% note that the selected portion of the entire cruise track is shown in
% red in the lower left hand window, and that this window can not be zoomed. 

% dail, 6/29/98


figure(999)
clf
zoom off

global xt_ax xy_ax yt_ax sm_ax xx yy tt rrs box_off cur_zm

xx = x;
yy = y;
tt = t;

rrs = 1:length(x);
init

start = 1;
while ~strcmp( get(gcf,'selectiontype'), 'alt') | start
 start = 0;
 waitforbuttonpress
 nuax = rbbox;
 if strcmp( get(gcf,'selectiontype'), 'normal')
  if gca==cur_zm, 
   resize_axes(nuax)
  end
 elseif strcmp( get(gcf,'selectiontype'), 'extend')
  if gca==cur_zm
   rrs = 1:length(x);
   draw_plots
  elseif gca~=sm_ax
   set(cur_zm,'color',[1 1 1])
   cur_zm = gca;
   set(cur_zm,'color',[.9 1 1])
  else
   warning('zooming is not permitted on this axis')
  end
 end
end
   
rs = rrs;
endit

function resize_axes(nuax)

global cur_zm xy_ax xt_ax yt_ax rrs xx yy tt 

% return if probably mistake
if nuax(3)<10 | nuax(4)<10
 return
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(cur_zm,'posit');

x1 = x1 - t1(1);
x2 = x2 - t1(1);
y1 = y1 - t1(2);
y2 = y2 - t1(2);

xlm = get(cur_zm,'xlim');
ylm = get(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 cur_zm==xy_ax
 t1 = find(xx(rrs)>t1 & xx(rrs)<t2 & yy(rrs)>t3 & yy(rrs)<t4);
elseif cur_zm==xt_ax
 t1 = find(xx(rrs)>t1 & xx(rrs)<t2 & tt(rrs)>t3 & tt(rrs)<t4);
else
 t1 = find(tt(rrs)>t1 & tt(rrs)<t2 & yy(rrs)>t3 & yy(rrs)<t4);
end
rrs = rrs(t1);

draw_plots


function init

global xt_ax xy_ax yt_ax sm_ax xx yy tt rrs box_off cur_zm 

% reduce typing in function
set(0,'defaultaxesxgrid','on')
set(0,'defaultaxesygrid','on')
if strcmp('off',get(0,'defaultaxesbox'))
 set(0,'defaultaxesbox','on')
 box_off = 1;
else
 box_off = 0;
end

% set up axes
set(999,'position',[500 200 540 540])

xy_ax = axes('position',[.4 .4 .5 .5]);
set(xy_ax,'color',[.9 1 1])
set(xy_ax,'xaxisloc','top')
set(xy_ax,'yaxisloc','right')
xlabel('longitude')
ylabel('latitude')
cur_zm = xy_ax;

xt_ax = axes('position',[.1 .4 .3 .5]);
set(xt_ax,'xaxisloc','top')
xlabel('longitude')
ylabel('time')

yt_ax = axes('position',[.4 .1 .5 .3]);
set(yt_ax,'yaxisloc','right')
xlabel('time')
ylabel('latitude')

sm_ax = axes('position',[.1 .1 .3 .3]);
xlabel('longitude')
ylabel('latitude')

draw_plots

function endit

global box_off

% increase typing in workspace
set(0,'defaultaxesxgrid','off')
set(0,'defaultaxesygrid','off')
if box_off
 set(0,'defaultaxesbox','off')
end

function draw_plots 

global xt_ax xy_ax yt_ax sm_ax xx yy tt rrs box_off

axes(xy_ax)
delete( findobj(gca,'type','line') )
line(xx(rrs),yy(rrs),'marker','.')
set(xy_ax,'xtickmode','auto')
xts = get(xy_ax,'xtick');
set(xy_ax,'xtick',xts(2:end));
set(xy_ax,'ytickmode','auto')
yts = get(xy_ax,'ytick');
set(xy_ax,'ytick',yts(2:end));

axes(sm_ax)
delete( findobj(gca,'type','line') )
line(xx,yy,'marker','.','color','b')
line(xx(rrs),yy(rrs),'marker','.','color','r')
set(sm_ax,'xtickmode','auto')
xts = get(sm_ax,'xtick');
set(sm_ax,'xtick',xts(1:end-1));
set(sm_ax,'ytickmode','auto')
yts = get(sm_ax,'ytick');
set(sm_ax,'ytick',yts(1:end-1));

axes(xt_ax)
delete( findobj(gca,'type','line') )
line(xx(rrs),tt(rrs),'marker','.')
set(xt_ax,'xtickmode','auto')
xts = get(xt_ax,'xtick');
set(xt_ax,'xtick',xts(1:end-1));
set(xt_ax,'ytickmode','auto')
yts = get(xt_ax,'ytick');
set(xt_ax,'ytick',yts(2:end));

axes(yt_ax)
delete( findobj(gca,'type','line') )
line(tt(rrs),yy(rrs),'marker','.')
set(yt_ax,'xtickmode','auto')
xts = get(yt_ax,'xtick');
set(yt_ax,'xtick',xts(2:end));
set(yt_ax,'ytickmode','auto')
yts = get(yt_ax,'ytick');
set(yt_ax,'ytick',yts(1:end-1));

