function [am,bm,cm,dm,totbnd,hsv] = schmr(Z1,Z2,Z3,Z4,Z5,Z6)
% [SS_M,TOTBND,HSV] = SCHMR(SS_,TYPE,NO) or
% [AM,BM,CM,DM,TOTBND,HSV]=SCHMR(A,B,C,D,TYPE,NO) performs Schur method
%    model reduction on G(s):=(a,b,c,d) such that the infinity-norm
%    of the error (Ghed(s) - G(s)) <= sum of the 2(n-k) smaller Hankel
%    singular values. For unstable G(s) the algorithm works by first 
%    splitting G(s) into a sum of stable and antistable part. 
% 
%   Based on the "TYPE" selected, you have the following options:
%
%    1). TYPE = 1  --- no: size "k" of the reduced order model.
%
%    2). TYPE = 2  --- find a k-th order model such that the total error 
%                      is less than "no".
%
%    3). TYPE = 3  --- disply all the Hankel SV prompt for "k" (in this
%                      case, no need to specify "no").
%
%    TOTBND = Error bound, HSV = Hankel Singular Values.

% R. Y. Chiang & M. G. Safonov 9/13/87
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.
% ------------------------------------------------------------------------
%
disp('  ');
disp('        - - Working on Schur Balanced Model Reduction - -');

inargs = '(a,b,c,d,Type,no)';
eval(mkargs(inargs,nargin,'ss'))

if Type == 3
   no = 0;
end
[ra,ca] = size(a);
[ee,dd] = eig(a); 
%
% ------ Find the no. of stable roots :
%
m = 0;
for k = 1 : ra
    if real(dd(k,k)) < 0
        m = m + 1;
    end
end
%
% ------ If completely stable :
%
kk = -1;
if m == ra
   [am,bm,cm,dm,augl,hsv,slbig,srbig] = schbal(a,b,c,d,Type,no);
   kk = 1;
   augr = [0 0];
   totbnd = augl(1,2);
end
%
% ------ If completely unstable :
%
if m == 0.
   [am,bm,cm,dm,augr,hsv,slbig,srbig] = schbal(-a,-b,c,d,Type,no);
   am = -am; bm = -bm; 
   kk = 1;
   augl = [0 0];
   totbnd = augr(1,2);
end
%
% ------ If having both stable & unstable parts :
%
if kk < 0
  [al,bl,cl,dl,ar,br,cr,dr,msat] = stabproj(a,b,c,d);
  [hsvl,pl,ql] = hksv(al,bl,cl);
  [hsvr,pr,qr] = hksv(-ar,-br,cr);
  hsv = [hsvl;hsvr];
  [hsv,index] = sort(hsv);
  hsv = hsv(ra:-1:1,:);
  index = index(ra:-1:1,:);
  if Type == 1
      nor = 0;
      for i = 1:no
          if index(i) > msat
             nor = nor + 1;           
          end
      end
     nol = no-nor;
   end
   if Type == 2
      tol = no;
      tails = 0;
      for i = ra:-1:1
          tails = tails + hsv(i);
          if 2*tails > tol
             no = i;
             break    
          end
      end
      nor = 0;
      for i = 1:no
          if index(i) > msat
             nor = nor + 1;           
          end
      end
      nol = no-nor;
      Type = 1;
   end
   if Type == 3
      nol = no; nor = no;
   end
   [alf,blf,clf,dlf,augl,hsvl,tll,tlr] = schbal(al,bl,cl,dl,Type,nol);
   [arf,brf,crf,drf,augr,hsvr,trl,trr] = schbal(-ar,-br,cr,dr,Type,nor);
   arf = -arf;
   brf = -brf;
   totbnd = augl(1,2)+augr(1,2);
   hsv = [hsvl;hsvr];
   [am,bm,cm,dm] = addss(alf,blf,clf,dlf,arf,brf,crf,drf);
   [ral,cal] = size(al); [rar,car] = size(ar);
   if augl(1,1) == ral
      am = arf; bm = brf; cm = crf; dm = d;
   end
   if augr(1,1) == rar
      am = alf; bm = blf; cm = clf; dm = d;
   end
end
%
if xsflag
   am = mksys(am,bm,cm,dm);
   bm = totbnd;
   cm = hsv;
end
%
nn = augl(1,1) + augr(1,1);
disp(' ')
disp(['               ' int2str(nn), '    states removed !!'])
%
% ------- End of SCHMR.M --- RYC/MGS 9/13/87 %