function pbar(u,v,ang,code,scale);
%
% m-file to calculate and plot percent frequency occurence as a function of
% angle
%
% function pbar(u,v,ang,scale)
%
%  u is the e/w velocity
%  v is the n/s velocity
%  ang is the incremental angle
%  code =1 for mean in each sector
%       =2 for std  in each sector
%       =3 for #occurences in each sector
%       =4 for %occurences in each sector
%  scale needed sometimes when mean < 1
%
% for each record keep track of the num of occurence in the 360/ang slots
%


% first, calulate the number of bars
nb=360/ang;

% generate angle vector
ta=0:ang:360-ang;

% mark each observation with angle code 1 through nb
for i=1:length(u);
  [s(i),d(i)]=uv2sd(u(i),v(i));
  for k=1:nb;
    if d(i)>=ta(k) & d(i)<ta(k)+ang;
      cs(i)=k;
    end;
  end;
end;


% now find the stats for each sector
for k=1:nb;
  id=find(cs==k);
  os(k)=length(id);
  ms(k)=mean(s(id));
  stds(k)=std(s(id));
end;

% plot mean,std, number of occurences, percent or histo
if nargin<4;
  code=menu('Which do you want?',...
             'mean','std','number of occurences','%occurence');
end;

if code==1;
% plot this "mean" with compass
  if nargin<5;
    scale=1;
  end;
  [ums,vms]=sd2uv(ms,ta+ang/2);
  compass(ums*scale,vms*scale);
  % generate a decent axis
  mx=maxnnan(ms);
%  axis([-1*mx mx -1*mx mx]);

elseif code==2;
% plot this "stddev" with compass
  if nargin<5;
    scale=1;
  end;
[uss,vss]=sd2uv(stds,ta+ang/2);
compass(uss*scale,vss*scale);

elseif code==3;
% plot this "number of occurences" with compass
[uos,vos]=sd2uv(os,ta+ang/2);
compass(uos,vos);

elseif code==4;
% plot this "percent occurences" with compass
[ups,vps]=sd2uv(os/length(u)*100,ta+ang/2);
compass(ups,vps);
end;



  



 

