%
% VECPLOT2    routine to plot vectors.  
%
% hv=vecplot2(x,y,u,v,sc,sclab,scale_xor,scale_yor,col,colsc,lw)
%
%            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')
%            scale_xor and scale_yor - origin of vector scale
%            col    - string variable of color

%            colsc  - string variable of color for legend
%            lw     - linewidth
%
%            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.
%
% 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,col,colsc,lw)

% 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'];


% Argument check
if nargin < 4
   error(err1);
elseif nargin > 11
   error(err2);
end
  
% process argument list
if nargin==4
   sc=1.;
   sclab='cm/s';      
elseif nargin==5 
   sclab='cm/s';      
end
sclab=[' ' sclab];
if nargin<9;
 col='g';
end;
if nargin<10;
 colsc='k';
end;
if nargin<11;
 lw=1;
end;

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,'RenderLimits');
xr=RLs(2)-RLs(1);
yr=RLs(4)-RLs(3);
Y1=RLs(3);
Y2=RLs(4);

% 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>=RLs(1)&xin<=RLs(2)&yin>=RLs(3)&yin<=RLs(4));
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,lw);
set(hp,'UserData',[xin yin uin vin]);
set(hp,'Tag','vectors');

% COLOR LARGEST VECTOR RED
%
%hvmax=drawvec(xin(imax),yin(imax),us(imax),vs(imax),25,'r');
%set(hvmax,'Tag','scalearrow');

% PLACE SCALE WITH MOUSE ON SCREEN
%
if ~strcmp(blankbb(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 
%   disp(['WARNING: scale vector color harded to ',col])
   ht1=drawvec(scale_xor,scale_yor,pct10,0.,25,colsc);
   set(ht1,'Tag','scalearrow');
   if strcmp(blankbb(sclab),'m/s')
      sctext=[num2str(sc) sclab];
   else
%      sctext=[num2str(100*sc) sclab];
      sctext=[num2str(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(scaletext,'color',colsc)
   set(gcf,'Pointer',ptr);

end

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

%
%        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
%
