% ion2 is an example of an ion distribution function.
% This is meant to reproduce the distribution in the top right panel of
% Fig. 6 in the paper by Gunell and Skiff, Phys. Plasmas, vol. 8,
% 3550-3557 (2001).

ALLpole =[];
ALLresid=[];


n_vec    =[5 3];			% # of terms for Maxwell expansion
nb_vec   =[0 0];			% # of terms for Butterworth 
vtbut_vec=[0 0];			% Cut velocity in cs units
vdbut_vec=[0 0];			% Butterworth center velocity
op2      =[0.4 0.6];			% densities
vt       =[0.1 0.4];			% thermal velocity cs==1
vd       =[0 0];			% drift velocity cs==1

op2=op2/sum(op2);			%use total plasma frequency
%%%% Normalize  
vtbut_vec=vtbut_vec./vt;
vdbut_vec=vdbut_vec./vt;
for jj=1:2
  n= n_vec(jj);  
  nb=nb_vec(jj);
  vtbut=vtbut_vec(jj);
  vdbut=vdbut_vec(jj);

  pol=0*(1:2*n+1);
  pol(2*n+1)=1;
  for i=1:n				% Poles for Maxwellian
    pol(2*(n-i)+1)=1.0/(2^i*prod(1:i));
  end
  sol=roots(pol);
  res=pole2residue(sol);		% Find residues
 
  z0=butterworthpoles(nb,vtbut); 	% Butterworth poles
  z0=z0+vdbut;

  allpole =[sol;z0];			%poles from expansion and mask
  allresid=pole2residue(allpole);	% Find residues
  [uhppole,uhpresid]=uhp(allpole,allresid); % Keep poles in upper half plane

  allresid=allresid/(2i*pi*sum(uhpresid)); % normalize zeroth moment
  for j=1:length(allpole)                  % return to variables with dimension
    ALLpole =[ALLpole allpole(j)*vt(jj)+vd(jj)];
    ALLresid=[ALLresid op2(jj)*allresid(j)];
  end
  
end

[UHPpole,UHPresid]=uhp(ALLpole,ALLresid); % Keep poles in upper half plane
