function [phic_resp,phic_ref,freq,damp,emac,mpc_resp,mpc_ref,cmi_resp,cmi_ref,keepm] = modefilt(phic_resp_t,phic_ref_t,freq_t,damp_t,emac_t,mpc_resp_t,mpc_ref_t,cmi_resp_t,cmi_ref_t,f_limits,mqi_limits,table)
%
%  modefilt
%
%  [phic_resp,phic_ref,freq,damp,emac,mpc_resp,mpc_ref,cmi_resp,cmi_ref] 
%	= modefilt(phic_resp_t,phic_ref_t,freq_t,damp_t,emac_t,mpc_resp_t,
%		mpc_ref_t,cmi_resp_t,cmi_ref_t,f_limits,mqi_limits,table)
%
%  Filters the modes according to the following
%   criteria:
%
%  - Modes with negative damping
%  - Modes below f_limits(1) and above f_limits(2)
%  - Modes with emac below mqi_limits(1)
%  -            mpc  below mqi_limits(2)
%  -            cmi  below mqi_limits(3)
%
%  size(f_limits) = [2 1];
%  size(mqi_limits) = [3 1];
%
%  If any limits are negative, that criteria is not applied
%
%  If table = 1, will print modal info. in tabular form

%  Version SWD960711
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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.                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nm = length(freq_t);

keepm = [];

rdamp = find(damp_t < 0);
rfreq = find(freq_t < f_limits(1) | freq_t > f_limits(2));
remac = find(emac_t < mqi_limits(1));

rmpc_resp  = find(mpc_resp_t < mqi_limits(2));
rmpc_ref  = find(mpc_ref_t < mqi_limits(2));

rcmi_resp  = find(cmi_resp_t < mqi_limits(3));
rcmi_ref  = find(cmi_ref_t < mqi_limits(3));

idelm = zeros(5,1);

for i = 1:nm,

   idelm(1) = ~isempty(find(i == rdamp));
   idelm(2) = ~isempty(find(i == rfreq));
   idelm(3) = ~isempty(find(i == remac));
   idelm(4) = ~isempty(find(i == rmpc_resp));
   idelm(5) = ~isempty(find(i == rmpc_ref));
   idelm(6) = ~isempty(find(i == rcmi_resp));
   idelm(7) = ~isempty(find(i == rcmi_ref));

   if sum(idelm) == 0,

     keepm = [keepm i];

   end

end

phic_resp = phic_resp_t(:,keepm);
phic_ref  = phic_ref_t(:,keepm);
freq = freq_t(keepm);
damp = damp_t(keepm);
emac = emac_t(keepm);
mpc_resp = mpc_resp_t(keepm);
mpc_ref  = mpc_ref_t(keepm);
cmi_resp = cmi_resp_t(keepm);
cmi_ref = cmi_ref_t(keepm);

nm = length(freq);

if table,

   printstr = [
   sprintf('%10s','Freq (Hz)','Damp.(%)','EMAC','MPC','CMI');
   sprintf('%10s','---------','--------','----','---','---')];

   nm = length(freq);

   for i = 1:nm,

     printstr = [printstr; sprintf('%10.3f',freq(i),damp(i),emac(i),mpc(i),cmi(i))];

   end

   printstr

end

return
