function [WCG,WCU,Info] = wcgarraymax(D,sOpt)
% Calculates worst-case gain info for an array of lftdataM objects.
%
% Input Arguments
%    D:   N-by-1 lftdataM array
%    sOpt: wcgainOptions object
% Output Argument
%    WCG   struct with fields LowerBound, UpperBound, and CriticalFrequency
%          (all scalars)
%    WCU   worst-case uncertainty value
%    INFO  struct with additional information.

%   Copyright 2010-2011 The MathWorks, Inc.
nmat = numel(D);

% Generate data for WCGBNB
S = struct('AData',cell(nmat,1),'B',[],'muB',[]);
SkipComputationFlag = false;
InitialLowerBounds = zeros(nmat,1);
for ct=1:nmat
   [M,B,~,muB] = ulftdata(D(ct));
   H = M.IC;
   blk = muB.muBlkNby3;
   bidx = blk2idx(blk);
   AD = createSP(H,blk,bidx,sOpt,SkipComputationFlag,InitialLowerBounds);
   InitialLowerBounds(ct) = AD.ScaledLb/AD.IOScalings;
   if AD.SkipComputationFlag
      % In this case, Lower bound was extremely large (bad performance), so
      % based on sOpt, no more bounds are computed (performance is already
      % bad enough).  However, the loop is run to completion to fill in the
      % values of the structure appropriately.  This is VERY fast, and
      % should not be eliminated.
      SkipComputationFlag = true;
   end
   S(ct).AData = AD;
   S(ct).B = B;
   S(ct).muB = muB;
end

% Find worst case over all models
Result = wcgbnb(cat(2,S.AData),sOpt);
imat = Result.CriticalIdx;
WCG = struct(...
   'LowerBound',Result.LowerBound,...
   'UpperBound',Result.UpperBound,...
   'CriticalFrequency',[]);
WCU = getWorstCasePerturbation(S(imat).B,S(imat).muB,Result.CriticalPert,0);
Info = struct('Sensitivity',[],'Frequency',[],...
   'BadUncertainValues',WCU,'ArrayIndex',imat);
