%
% VECPLOT2    routine to plot vectors.  
%
% hv=vecplot2(x,y,u,v,sc,sclab)
%
%            vecplot2(x,y,u,v,sc,sclab)
%            routine to plot vectors.  VECPLOT2 scales the magnitude of 
%            (u,v) by the magnitude of max(abs(u,v)) and then 
%            forces a vector of magnitude sc to be 10% of the x data
%            range.  By default, sc = 1., so that a 1 m/s vector will
%            be scaled to 10% of the x data range.  If sc=.5, then
%            a vector magnitude of 50 cm/s  will be scaled to 10% of the 
%            x data range.  Decreasing sc serves to make the vectors
%            appear larger.
%
%	     VECPLOT draws on top of existing axes, if they exist,
%            regardless of the state of 'hold'.  If there is no current
%            axes, VECPLOT draws a new axes. 
%      
%            The first 4 input arguments are required.; 'sc' and 'col'
%            are optional.
%          
%            x,y    - vector origins
%            u,v    - vector amplitudes
%            sc     - vector scaler; (optional; def = 1.)
%            sclab  - label for vector scale; (optional; def = 'cm/s')
%
%            Example call:  vecplot2(x,y,u,v,.2,'m/s')
%            This will draw vectors at (x,y) with appropriately scaled
%            magnitudes (u,v), with a vector of 20 cm/s being scaled
%            to 10% of the x data ragne, and with the string 'm/s' 
%            used as the scale label.
%
%  	     NOTES:
%            VECPLOT2 requires atleast 2 coordinates and vectors.
%
% Call as:   hv=vecplot2(x,y,u,v,sc,sclab)
%
% Written by : Brian O. Blanton
%
function  retval=vecplot2(xin,yin,uin,vin,sc,sclab,scale_xor,scale_yor)

% DEFINE ERROR STRINGS
err1=['Not enough input arguments; type "help vecplot2"'];
err2=['Too many input arguments; type "help vecplot2"'];
err3=['Length of x,y,u,v must be the same'];
err4=['Length of x,y,u,v must be greater than 1'];

%
% save the current value of the current figure's WindowButtonDownFcn,
% WindowButtonMotionFcn, and WindowButtonUpFcn
%
WindowButtonDownFcn=get(gcf,'WindowButtonDownFcn');
WindowButtonMotionFcn=get(gcf,'WindowButtonMotionFcn');
WindowButtonUpFcn=get(gcf,'WindowButtonUpFcn');
set(gcf,'WindowButtonDownFcn','');
set(gcf,'WindowButtonMotionFcn','');
set(gcf,'WindowButtonUpFcn','');


% Argument check
if nargin < 4
   error(err1);
elseif nargin > 8
   error(err2);
end
  
% process argument list
if nargin==4
   sc=1.;
   sclab='cm/s';      
elseif nargin==5 
   sclab='cm/s';      
end
sclab=[' ' sclab];
col='b';

if length(xin)~=length(yin) | ...
   length(xin)~=length(uin) | ...
   length(xin)~=length(vin)
   error(err3);
end
if length(xin)==1 
   error(err4);
end

xin=xin(:);
yin=yin(:);
uin=uin(:);
vin=vin(:);

% SCALE VELOCITY DATA TO RENDERED WINDOW SCALE 
%
RLs= get(gca,'XLim');
xr=RLs(2)-RLs(1);
X1=RLs(1);
X2=RLs(2);
RLs= get(gca,'YLim');
yr=RLs(2)-RLs(1);
Y1=RLs(1);
Y2=RLs(2);

% IF RenderLimits NOT SET, USE RANGE OF DATA
%
if(xr==0|yr==0)
   error('Axes must have been previously set for VECPLOT2 to work');
end
pct10=xr/10;

%FILTER DATA THROUGH VIEWING WINDOW
%
filt=find(xin>=X1&xin<=X2&yin>=Y1&yin<=Y2);
x=xin(filt);
y=yin(filt);
u=uin(filt);
v=vin(filt);

% SCALE BY MAX VECTOR SIZE IN U AND V
%
us=u/sc;
vs=v/sc;

% SCALE TO 10 PERCENT OF X RANGE
%
us=us*pct10;
vs=vs*pct10;
 
% SEND VECTORS TO DRAWVEC ROUTINE
%
hp=drawvec(x,y,us,vs,25,col);
set(hp,'UserData',[xin yin uin vin]);
set(hp,'Tag','vectors');

% COLOR LARGEST VECTOR RED
%[trash,imax]=max(sqrt(us.*us+vs.*vs));
%hvmax=drawvec(x(imax),y(imax),us(imax),vs(imax),25,'r');
%set(hvmax,'Tag','scalearrow');

% PLACE SCALE WITH MOUSE ON SCREEN
%
if 0
if ~strcmp(blank(sclab),'no scale')
   ptr=get(gcf,'Pointer');
   if ~exist('scale_xor')| ~exist('scale_yor')
      disp('place scale on plot with a mouse button');
      [scale_xor,scale_yor]=ginput(1);
   end 
   
   ht1=drawvec(scale_xor,scale_yor,pct10,0.,25,'r');
   set(ht1,'Tag','scalearrow');
   if strcmp(blank(sclab),'m/s')
      sctext=[num2str(sc) sclab];
   else
      sctext=[num2str(100*sc) sclab];
   end
   scaletext=text((scale_xor+scale_xor+pct10)/2,scale_yor-(Y2-Y1)*(.05),sctext);
   set(scaletext,'HorizontalAlignment','center');
   set(scaletext,'VerticalAlignment','middle');
   set(scaletext,'Tag','scaletext');
   set(gcf,'Pointer',ptr);

end


% OUTPUT IF DESIRED
%
if nargout==1,retval=[hp ht1 scaletext];,end

else
retval = 0;  
end

%
% return the saved values of the current figure's WindowButtonDownFcn,
% WindowButtonMotionFcn, and WindowButtonUpFcn to the current figure
%
set(gcf,'WindowButtonDownFcn',WindowButtonDownFcn);
set(gcf,'WindowButtonMotionFcn',WindowButtonMotionFcn);
set(gcf,'WindowButtonUpFcn',WindowButtonUpFcn);

%
%        Brian O. Blanton
%        Curr. in Marine Science
%        15-1A Venable Hall
%        CB# 3300
%        Uni. of North Carolina
%        Chapel Hill, NC
%                 27599-3300
%
%        919-962-4466
%        blanton@marine.unc.edu
%
