function [g2,x]=g2norm(x,param)
%
% [g2 x]=g2norm(x,{'type',Lx,Ly,'subtype'}); 
% Radial and angular distributions of random particles in a 'type' box
% of size Lx and Ly.  'type' is one of the following 'rect'.  'subtype' for
% 'rect' is 'radial' or 'angular'.  Currently Lx must equal Ly. g2 is
% normalize such that the sum(g2.*r*dr)=1 for 'radial' and sum(g2*dphi)=1
% for angular (see below).  
%
% example:    dr=.001;
%             L=4;
%             r=0:dr:L*sqrt(2);
%             [g2 r]=g2norm(r,{'rect',L,L,'radial'});
%             plot(r,g2,r,g2.*r);
%             sum(g2.*r*dr)
%          ans =
%              1.0000
%
%             dphi=.001;
%             L=4;
%             phi=0:dphi:2*pi-dphi;
%             [g2 phi]=g2norm(phi,{'rect',L,L,'angular'});
%             plot(phi,g2);
%             sum(g2*dphi)
%          ans =
%              1.0000


if(strcmp(param{1},'rect'))
  if(length(param)==4)
    Lx=param{2};
    Ly=param{3};
    if(Lx~=Ly)
      error('g2norm:OnlySquare','Lx must equal Ly!!');
    end;
    if(strcmp(param{4},'angular'))
      ph0=atan(Ly/Lx);
      ph=acos(cos(x*4))/4;
      g2=1/3/pi*Lx/Ly*ph0*(2-Lx/Ly*tan(ph))./cos(ph).^2;
    elseif(strcmp(param{4},'radial'))
      L=Lx;
      mm=(x<=L);
      g2(mm)=8*(pi/4-x(mm)/L+(x(mm)/(2*L)).^2);
      mm=(x>L & x<=sqrt(2)*L);
      g2(mm)=8*(sqrt((x(mm)/L).^2-1)+asin(L./x(mm))-1/2-pi/4-(x(mm)/(2*L)).^2);
      mm=(x>sqrt(2)*L);
      g2(mm)=0;
      g2=g2/(Lx*Ly);
    else
      error('g2norm:BadSubType','subtype=''%s'' is not suported for type=''%s''!!',param{4},param{1});
    end
  else
    error('g2norm:BadParam','{''rect'',Lx,Ly,''angle''} are require for type=''%s''!!',param{1});
  end
else
  error('g2norm:BadType','type=''%s'' is not suported!!',param{1});
end
  
  