NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/opnml/FEM/newvecplot.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function  retval=vecplot(xin,yin,uin,vin,varargin)
2 %VECPLOT draw vectors on the current axes 
3 % VECPLOT draws vectors on the current axes, given
4 % vector origins (x,y) and vector magnitudes (u,v).
5 %
6 % VECPLOT scales the magnitude of
7 % (u,v) by the magnitude of max(abs(u,v)) and then
8 % forces a vector of magnitude sc to be 10% of the x data
9 % range.  By default, sc = 1., so that a 1 m/s vector will
10 % be scaled to 10% of the x data range.  If sc=.5, then
11 % a vector magnitude of 50 cm/s  will be scaled to 10% of the
12 % x data range.  Decreasing sc serves to make the vectors
13 % appear larger.  VECPLOT draws a vector scale according to
14 % the user input (see below).
15 %     
16 %   INPUT:   x,y    - vector origins
17 %            u,v    - vector amplitudes
18 %            'help' - if the only argument to VECPLOT is 'help'
19 %                     additional information on its usage is returned
20 %                     to the screen.
21 %
22 %  OUTPUT:   h - vector of handles to the vector lines drawn, the
23 %                the scale vector text, and the scale vector handles.
24 %                Type >> vecplot('help') for details.
25 %                If the 'stick' method is used, then h is ordered like:
26 %                h(1) -> vector shaft object (Tag=vectors')
27 %                h(2) -> stick vector origin object (Tag=vecrots')
28 %                h(3) -> scale vector text object (Tag=scaletext')
29 %                h(4) -> scale vector shaft object (Tag=scalearrow')
30 %                h(5) -> scale vector origin object (Tag=scalearrow')
31 %                If the 'arrow' method is used, then h is ordered like:
32 %                h(1) -> vector object (Tag=vectors')
33 %                h(2) -> scale vector text object (Tag=scaletext')
34 %                h(3) -> scale vector object (Tag=scalearrow')
35 %
36 % PN/PV pairs accepted by VECPLOT:
37 %    ArrowAngle - angle (in degrees) that the arrow head wings
38 %                 make with the shaft. Default=25
39 %    DotColor   - origin symbol color, for VecType=='stick'. Default='k'.
40 %    DotSize    - origin symbol size, for VecType=='stick'. Default=10.
41 %    DotStyle   - origin symbol, or VecType=='stick'. Default='.'.
42 %    MaxThresh  - Maximum vector magnitude to plot. Default=Inf.
43 %    MinThresh  - Minimum vector magnitude to plot. Default=0.
44 %    PctAxis    - Percent of axis for scale length. Default=10.
45 %    ScaleFac   - vector scaling factor. Default=1.
46 %    ScaleLabel - label for vector scale; 'no scale' prevents scale
47 %                 from being drawn; Default='m/s'.
48 %    ScaleType  - how to draw the vector scale, either 'fixed' or
49 %                 'floating'; Default='fixed'.
50 %    ScaleXor   - scale x-origin; Default=[].
51 %    ScaleYor   - scale y-origin; Default=[].
52 %    Stride     - amount to stride over in drawing vectors. Default=1,
53 %                 meaning no stride.
54 %    VecType    - vector drawing method, either 'arrow' or 'stick';
55 %                 Default='arrow'.
56 %         
57 %   NOTES:   VECPLOT requires atleast 2 coordinates and vectors.
58 %
59 %    CALL:   hv=vecplot(x,y,u,v,pn1,pv1,...);
60 %
61 % Written by : Brian O. Blanton
62 %
63
64 % DEFINE ERROR STRINGS
65 err1=['Invalid number of input arguments to VECPLOT'];
66 err2=['VECPLOT no longer can accept a fem_grid_struct.'];
67 err3=['Lengths of x,y,u,v must be the same'];
68 err4=['Length of (x,y) must equal length of (u,v).'];
69 err5=['Alteast 3 arguments are required if first is a fem_grid_struct.'];
70 err6=['Alteast 4 arguments are required if first two are x,y.'];
71 err7=['Second optional argument (sclab) must be a string.'];
72 err8=['Both x- and y-coords of vector scale must be specified.'];
73
74 if nargin==0,disp('Call as: hv=vecplot(x,y,u,v,pn1,pv1,...)');return;end
75 if nargin==1,vecplothelp(xin);return;end
76
77 if nargin<4
78    error(err1)
79 end
80
81 if isstruct(xin)
82    error(err2)
83 end
84
85 % Check lengths of x,y,u,v
86 if length(uin)~=length(vin) | length(xin)~=length(yin) | length(xin)~=length(uin)
87   error(err3)
88 end
89
90 % Default propertyname values
91 MinThresh=0.;
92 MaxThresh=Inf;
93 ScaleLabel='m/s';
94 ScaleType='fixed';
95 Stride=1;
96 VecType='arrow';
97 ScaleFac=1.;
98 ScaleXor=[];
99 ScaleYor=[];
100 PctAxis=10;
101
102 % Strip off propertyname/value pairs in varargin not related to
103 % "line" object properties.
104 k=1;
105 while k<length(varargin),
106   switch lower(varargin{k}),
107     case 'maxthresh',
108       MaxThresh=varargin{k+1};
109       varargin([k k+1])=[];
110     case 'minthresh',
111       MinThresh=varargin{k+1};
112       varargin([k k+1])=[];
113     case 'stride',
114       Stride=varargin{k+1};
115       varargin([k k+1])=[];
116     case 'scaletype',
117       ScaleType=varargin{k+1};
118       varargin([k k+1])=[];
119     case 'scalexor',
120       ScaleXor=varargin{k+1};
121       varargin([k k+1])=[];
122     case 'scaleyor',
123       ScaleYor=varargin{k+1};
124       varargin([k k+1])=[];
125     case 'scalelabel',
126       ScaleLabel=varargin{k+1};
127       varargin([k k+1])=[];
128     case 'scalefac',
129       ScaleFac=varargin{k+1};
130       varargin([k k+1])=[];
131     case 'pctaxis',
132       PctAxis=varargin{k+1};
133       varargin([k k+1])=[];
134     case 'vectype',
135       VecType=lower(varargin{k+1});
136       if strcmp(VecType,{'arrow','stick'})
137          error('Invalid VecType to VECPLOT.')
138       end
139       varargin([k k+1])=[];
140     otherwise
141       k=k+2;
142   end;
143 end;
144
145 if length(varargin)<2
146    varargin={};
147 end
148
149 if xor(isempty(ScaleXor),isempty(ScaleYor))
150    error(err8)
151 end
152
153 %
154 % save the current value of the current figure's WindowButtonDownFcn,
155 % WindowButtonMotionFcn, and WindowButtonUpFcn
156 %
157 WindowButtonDownFcn=get(gcf,'WindowButtonDownFcn');
158 WindowButtonMotionFcn=get(gcf,'WindowButtonMotionFcn');
159 WindowButtonUpFcn=get(gcf,'WindowButtonUpFcn');
160 set(gcf,'WindowButtonDownFcn','');
161 set(gcf,'WindowButtonMotionFcn','');
162 set(gcf,'WindowButtonUpFcn','');
163
164
165 % SCALE VELOCITY DATA TO RENDERED WINDOW SCALE
166 %
167 RLs= get(gca,'XLim');
168 xr=RLs(2)-RLs(1);
169 X1=RLs(1);
170 X2=RLs(2);
171 RLs= get(gca,'YLim');
172 yr=RLs(2)-RLs(1);
173 Y1=RLs(1);
174 Y2=RLs(2);
175
176 % IF RenderLimits NOT SET, USE RANGE OF DATA
177 %
178 if(xr==0|yr==0)
179    error('Axes must have been previously set for VECPLOT to work');
180 end
181 pct10=(PctAxis/100)*xr;   
182
183
184 % determine striding, if needed
185 [m,n]=size(xin);
186 if Stride >1
187    if any([m n]==1)
188       i=1:Stride:length(xin);
189    else
190       i=(1:Stride:m)';
191 %      j=cumsum(Stride*m*ones(size(0:Stride:n-(Stride+1))))';
192       j=repmat(i,[1 length(1:Stride:n)]);
193       ij=j+repmat((0:Stride:n-1)*n,[length(1:Stride:n) 1]);
194       i=ij(:);
195    end
196 else
197    i=1:Stride:length(xin);
198 end
199
200 x=xin(i);
201 y=yin(i);
202 u=uin(i);
203 v=vin(i);
204
205 %keyboard
206
207 %FILTER DATA THROUGH VIEWING WINDOW
208 %
209 filt=find(x>=X1&x<=X2&y>=Y1&y<=Y2);
210 x=x(filt);
211 y=y(filt);
212 u=u(filt);
213 v=v(filt);
214
215 % Delete any NaN's
216 mag=sqrt(u.*u+v.*v);   % Unit mag
217 %mag=mag/max(mag);
218 iding=find(isnan(mag));
219
220 % Further eliminate vectors whose magnitude is at or below MinThresh.
221 %iding=[iding;find(mag<=MinThresh/100)];
222 iding=[iding;find(mag<=MinThresh)];
223
224 % Further eliminate vectors whose magnitude is at or above MaxThresh.
225 %iding=[iding;find(mag>=MaxThresh)];
226 iding=[iding;find(mag>=MaxThresh)];
227
228 x(iding)=[];
229 y(iding)=[];
230 u(iding)=[];
231 v(iding)=[];
232
233 % SCALE BY ScaleFac IN U AND V
234 %
235 us=u/ScaleFac;
236 vs=v/ScaleFac;
237
238 % SCALE TO 10 PERCENT OF X RANGE
239 %
240 us=us*pct10;
241 vs=vs*pct10;
242
243 % SEND VECTORS TO DRAWVEC ROUTINE
244 %
245 switch VecType
246    case 'arrow'
247       %Strip out attributes not used in arrow mode.
248       k=1;
249       while k<length(varargin),
250          switch lower(varargin{k}),
251             case 'dotcolor',
252               varargin([k k+1])=[];
253             case 'dotsize',
254                varargin([k k+1])=[];
255             case 'dotstyle',
256                varargin([k k+1])=[];
257             otherwise
258                k=k+2;
259          end
260       end
261       if length(varargin)<2
262          varargin={};
263       end
264      
265       hp=newdrawvec(x,y,us,vs,varargin{:});
266       set(hp,'UserData',[xin yin uin vin]);
267    case 'stick'
268       hp=newdrawstick(x,y,us,vs,varargin{:});
269       set(hp(1),'UserData',[xin yin uin vin]);
270    otherwise
271       error('Invalid VecType Property Value to VECPLOT.')
272 end     
273 set(hp,'Tag','vectors');
274
275
276 % COLOR LARGEST VECTOR RED
277 %[trash,imax]=max(sqrt(us.*us+vs.*vs));
278 %hvmax=newdrawvec(x(imax),y(imax),us(imax),vs(imax),25,'r');
279 %set(hvmax,'Tag','scalearrow');
280
281
282 if ~strcmp(blank(ScaleLabel),'no scale')
283    [ht1,scaletext]=drawvecscale(ScaleLabel,ScaleXor,ScaleYor,...
284           VecType,ScaleType,ScaleFac,pct10,Y1,Y2,varargin{:});
285 else
286    ht1=[];scaletext=[];
287 end
288
289 % OUTPUT IF DESIRED
290 %
291
292 if nargout==1,retval=[hp; scaletext; ht1(:)];,end
293
294 %
295 % return the saved values of the current figure's WindowButtonDownFcn,
296 % WindowButtonMotionFcn, and WindowButtonUpFcn to the current figure
297 %
298 set(gcf,'WindowButtonDownFcn',WindowButtonDownFcn);
299 set(gcf,'WindowButtonMotionFcn',WindowButtonMotionFcn);
300 set(gcf,'WindowButtonUpFcn',WindowButtonUpFcn);
301
302
303
304
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %%%%%%%%%%%%  PRIVATE FUNCTION TO DRAW VECTOR SCALE   %%%%%%%%%%%
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 function [ht1,scaletext]=drawvecscale(ScaleLabel,ScaleXor,ScaleYor,...
309           VecType,ScaleType,ScaleFac,pct10,Y1,Y2,varargin)
310
311 % PLACE SCALE WITH MOUSE ON SCREEN
312 %
313 ptr=get(gcf,'Pointer');
314 if (isempty(ScaleXor)| isempty(ScaleYor))&strcmp(ScaleType,'fixed')
315    disp('place scale on plot with a mouse button');
316    [ScaleXor,ScaleYor]=ginput(1);
317 end
318
319
320 switch lower(ScaleType)
321    case 'fixed'
322       switch VecType
323          case 'arrow'
324             ht1=newdrawvec(ScaleXor,ScaleYor,pct10,0.,varargin{:});
325          case 'stick'
326             ht1=newdrawstick(ScaleXor,ScaleYor,pct10,0.,varargin{:});
327       end
328
329       set(ht1,'Tag','scalearrow');
330       sctext=[num2str(ScaleFac) ScaleLabel];
331       scaletext=text((ScaleXor+ScaleXor+pct10)/2,ScaleYor-(Y2-Y1)*(.05),sctext);
332       set(scaletext,'HorizontalAlignment','center');
333       set(scaletext,'VerticalAlignment','middle');
334       set(scaletext,'Tag','scaletext');
335       set(gcf,'Pointer',ptr);
336    case 'floating'
337       mainax=gca;
338       % Draw vector scale
339       data_axis=axis;
340       xdif=data_axis(2)-data_axis(1);
341       ydif=data_axis(4)-data_axis(3);
342       dx1=data_axis(1)+xdif*.8;
343       dx2=data_axis(2);
344       dy1=data_axis(3);
345       dy2=data_axis(3)+ydif*.1;
346      
347       cur_units=get(gca,'Units');
348       set(gca,'Units','normalized');
349       axnorm=get(gca,'Position');
350
351       if isempty(ScaleXor)
352          xstart=0;
353          ystart=0;
354       else
355          xtemp=(ScaleXor-data_axis(1))/xdif;
356          ytemp=(ScaleYor-data_axis(3))/ydif;
357          oldfigunits=get(gcf,'Units');
358          set(gcf,'Units','pixels');
359          figpixunits=get(gcf,'Position');
360          set(gcf,'Units',oldfigunits);
361          oldaxesunits=get(gca,'Units');
362          set(gca,'Units','pixels');
363          axespixunits=get(gca,'Position');
364          set(gca,'Units',oldaxesunits);
365          
366          xstart=(xtemp*axespixunits(3)+axespixunits(1))/figpixunits(3);
367          ystart=(ytemp*axespixunits(4)+axespixunits(2))/figpixunits(4);
368          
369       end
370      
371       dx=axnorm(3)*.2;
372       dy=axnorm(4)*.1;
373       scale_axes=axes('Units','normalized','Position',[xstart ystart dx dy]);
374       %scale_axes=axes('Units','normalized','Position',[0 0 dx dy]);
375       axis([dx1 dx2 dy1 dy2])
376       sc_or_x=dx1+(dx2-dx1)/10;
377       switch VecType
378          case 'arrow'
379             ht1=newdrawvec(sc_or_x,(dy1+dy2)/2.,pct10,0.,varargin{:});
380          case 'stick'
381             ht1=newdrawstick(sc_or_x,(dy1+dy2)/2.,pct10,0.,varargin{:});
382       end
383
384       set(ht1,'Tag','scalearrow');
385       sctext=[num2str(ScaleFac) ScaleLabel];
386       scaletext=text((dx1+dx2)/2,dy1+(dy2-dy1)/8,sctext);
387       set(scaletext,'HorizontalAlignment','center');
388       set(scaletext,'VerticalAlignment','middle');
389       set(scaletext,'Tag','scaletext');
390       drawnow
391       set(scale_axes,'Visible','on')
392       set(scale_axes,'XTick',[],'YTick',[])
393       set(scale_axes,'Tag','vecscaleaxes');
394       set(scale_axes,'ButtonDownFcn','movescaleaxes(1)');
395       set(scale_axes,'Color',(get(mainax,'Color')))
396       axes(mainax)
397    otherwise
398       error('Invalid ScaleType Property Value in VECPLOT.')
399 end
400
401
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 %%%%%%%%%%%%  PRIVATE FUNCTION FOR VECPLOT HELP   %%%%%%%%%%%%%%%
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 function vecplothelp(arg)
406
407 if ~isstr(arg)
408    error('invalid help string to VECPLOT')
409 end
410
411 switch arg
412    case 'help'     
413       disp('VECPLOT additional help section')
414       str=[];
415       str=[str sprintf('\n')];
416       str=[str sprintf('VECPLOT returns a vector of handles to objects\n')];
417       str=[str sprintf('drawn in the current axes.  The vector contains: \n')];
418       str=[str sprintf('   If the ''stick'' method is used, then h is ordered like:\n')];
419       str=[str sprintf('      h(1) -> vector shaft object (Tag=vectors)\n')];
420       str=[str sprintf('      h(2) -> stick vector origin object (Tag=vecrots)\n')];
421       str=[str sprintf('      h(3) -> scale vector text object (Tag=scaletext)\n')];
422       str=[str sprintf('      h(4) -> scale vector shaft object (Tag=scalearrow)\n')];
423       str=[str sprintf('      h(5) -> scale vector origin object (Tag=scalearrow)\n')];
424       str=[str sprintf('\n   If the ''arrow'' method is used, then h is ordered like:\n')];
425       str=[str sprintf('      h(1) -> vector object (Tag=vectors)\n')];
426       str=[str sprintf('      h(2) -> scale vector text object (Tag=scaletext)\n')];
427       str=[str sprintf('      h(3) -> scale vector object (Tag=scalearrow)\n')];
428                
429 str=[str sprintf('\nPN/PV pairs accepted by VECPLOT:\n')];
430 str=[str sprintf('    ArrowAngle - angle (in degrees) that the arrow head wings\n')];
431 str=[str sprintf('                             make with the shaft. Default=25.\n')];
432 str=[str sprintf('    DotColor   - origin symbol color, for VecType==''stick''. Default=''k''\n')];
433 str=[str sprintf('    DotSize    - origin symbol size, for VecType==''stick''. Default=10\n')];
434 str=[str sprintf('    DotStyle   - origin symbol, or VecType==''stick''. Default=''.''\n')];
435 str=[str sprintf('    MaxThresh  - Maximum vector magnitude to plot. Default=Inf.\n')];
436 str=[str sprintf('    MinThresh  - Minimum vector magnitude to plot. Default=0.\n')];
437 str=[str sprintf('    ScaleFac   - vector scaling factor. Default=1.\n')];
438 str=[str sprintf('    ScaleLabel - label for vector scale; ''no scale'' prevents scale\n')];
439 str=[str sprintf('                             from being drawn; Default=''m/s''.\n')];
440 str=[str sprintf('    ScaleType  - how to draw the vector scale, either ''fixed'' or\n')];
441 str=[str sprintf('                             ''floating''; Default=''fixed''.\n')];
442 str=[str sprintf('    ScaleXor  - scale x-origin; Default=[].\n')];
443 str=[str sprintf('    ScaleYor  - scale y-origin; Default=[].\n')];
444 str=[str sprintf('    Stride     - amount to stride over in drawing vectors. Default=1,\n')];
445 str=[str sprintf('                             meaning no stride. Stride=2 skips every other point.\n')];
446 str=[str sprintf('    VecType    - vector drawing method, either ''arrow'' or ''stick'';\n')];
447 str=[str sprintf('                             Default=''arrow''.\n')];
448      title1='VECPLOT Additional Help';
449
450    otherwise
451       error('invalid help string to VECPLOT')
452 end
453
454 if ~isempty(str)
455    helpwin(str,title1);
456 end
457
458 %
459 %LabSig  Brian O. Blanton
460 %        Department of Marine Sciences
461 %        12-7 Venable Hall
462 %        CB# 3300
463 %        University of North Carolina
464 %        Chapel Hill, NC
465 %                 27599-3300
466 %
467 %        brian_blanton@unc.edu
468 %     
469 %        SUMMER 1998
470 %
Note: See TracBrowser for help on using the browser.