function residues2modes
%
%  Name: residues2modes
% 
%  Usage: residues2modes
% 
%  Description:
%     Compute modal vectors and mode participation factors
%     from complex residues
%

%  Version SWD970818
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This matlab source code was originally     %
% developed as part of "DIAMOND" at          %
% Los Alamos National Laboratory. It may     %
% be copied, modified, and distributed in    %
% any form, provided:                        %
%  a) This notice accompanies the files and  %
%     appears near the top of all source     %
%     code files.                            %
%  b) No payment or commercial services are  %
%     received in exchange for the code.     %
%                                            %
% Original copyright is reserved by the      %
% Regents of the University of California,   %
% in addition to Scott W. Doebling, Phillip  %
% J. Cornwell, Erik G. Straser, and Charles  %
% R. Farrar.                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

global MODES

if isfield(MODES,'nmodes'),
   
   if MODES.nmodes > 0,
   
      disp('Creating Mode Shapes from Residues...')

      for i = 1:MODES.nmodes,
         switch MODES.ic2rmeth,
         case 'Real Part',
            MODES.modalvector{i} = real(MODES.residue{i});
         case 'Imag Part',
            MODES.modalvector{i} = imag(MODES.residue{i});
         case 'Phase Rounding',      
            r = abs(MODES.residue{i});
            a = angle(MODES.residue{i});
            pp = find(a < pi & a > 0);
            pm = find(a > -pi & a < 0);
            amod = a + mean([a(pp) -a(pm)]);
            
            for ir = 1:length(amod),
               
               if cos(amod(ir)) > 0,
                  angsign = 1;
               else
                  angsign = -1;
               end
         
               MODES.modalvector{i}(ir) = abs(MODES.residue{i}(ir)) * angsign;
         
            end
         end
         
         MODES.mpf{i} =  MODES.modalvector{i}.' \ MODES.residue{i}.';
         
      end
   
      disp('Done Creating Mode Shapes')
   
   end

end

return