function [result,errorStr] = checkEx();
%% ===========================================================================
%checkEx - Check experiment description in the global structure ex
%
% Returns:      result		1=OK 0=Error
% Returns:      errorStr	error string
%
% Example(s):
%   ok=checkEx;
%   if ~ok,
%       error('Ex struct not correct');
%   end;
%
% Reference:
%    FSAN xDSL simulation tool manual
%% ===========================================================================

%% ===========================================================================
% Copyright (C):                                        
%       1999 by Telia Research AB, Lulea, Sweden;                
%       2000 by Forschungszentrum Telekommunikation Wien, Austria;
%                                                         All rights reserved.
% Project       : FSAN duplex model
% Author(s)     : Tomas Nordstrom (Tomas.Nordstrom@FTW.at)
%               : Daniel Bengtsson (Daniel.J.Bengtsson@Telia.se)
%
% CVS:       $Id: checkEx.m,v 1.16 2000/11/10 10:18:24 tono Exp $
%% ===========================================================================
% Change History
%      1999-01-14 (ToNo) Created
%      1999-01-18 (ToNo) Fixed "No Disturbed modem" check
%      1999-01-18 (ToNo) Added many new checks and now it returns the
%                        error string
%      1999-02-25 (ToNo) Generlized list (tfplist) handling
%      1999-02-26 (ToNo) Added checks for vdslDuplex and removed for noiseMargin.
%      1999-08-12 (DaB)  Updated for new efficiencyLoss struct
%      1999-08-19 (DaB)  Updated for new tfplan struct
%      2000-10-25 (GS)	 Update for accepting different string length in modemlist
%% ===========================================================================
global ex;

result=1;
errorStr='';

%% ===========================================================================
% Check param
notypes=length(ex.param.modemlist(:,1));
if notypes<1
    result=0;
    errorStr=[errorStr sprintf('Error: No Disturbed modem!\n')];
end;



%% ===========================================================================
% Check all disturbed modems

for distmodem = 1:notypes,
   
   % Remove blank characters from modem names 
   % (produced by 'str2mat' in SDSLmargin.m)
   modem=ex.param.modemlist(distmodem,:);
   blankindex=find(modem==' ');
   if~isempty(blankindex)
     len=blankindex(1)-1;
     modemname=modem(1:len);
   else
     modemname=modem;
   end

   xDSL=getList(ex.param.xDSLlist,(modemname));

    if isempty(xDSL),
        result=0;
        errorStr=[errorStr ...
                sprintf('Error: disturbed modem "%s" not in xDSLlist\n', ...
                modemname)];
        break;
    end;
    
    %% =======================================================================
    % Check tf plan/list

    tfplan=getList(ex.tfplist,xDSL.used);
    if isempty(tfplan),
        result=0;
        errorStr=[errorStr ...
                sprintf('Error: "%s" from ex.param.xDSLlist (element %s) not found in ex.tfplist\n', ...
                xDSL.used,modemname)];
        break;
    end;

    PSD_up=eval(tfplan.PSD.upstream);
    PSD_down=eval(tfplan.PSD.downstream);

    if (tfplan.timeDivision.down+tfplan.timeDivision.up > 2) | ...
            (tfplan.timeDivision.down < 0) | ...
            (tfplan.timeDivision.up < 0),
        result=0;
        errorStr=[errorStr ...
                sprintf('Error: timeDivision should not be %.2f, %.2f (down, up)\n', ...
                tfplan.timeDivision.down,tfplan.timeDivision.up)];
    end;

    %% =======================================================================
    % Check lc structure

    lc = getList(ex.lclist,tfplan.lcname);

    if (lc.param.efficiencyLoss<0 | lc.param.efficiencyLoss>1),
        result=0;
        errorStr=[errorStr sprintf('Error: efficiencyLoss should not be %.2f\n',lc.param.efficiencyLoss)];
    end;

    if lc.param.SNRMax<0 | lc.param.SNRMax>160,
        result=0;
        errorStr=[errorStr sprintf('Error: SNRMax should not be %.2f\n',lc.param.SNRMax)];
    end;

    if ex.param.backgroundNoise>-60,
        result=0;
        errorStr=[errorStr ...
                sprintf('Error: backgroundNoise should not be %.2f\n', ex.param.backgroundNoise)];
    end;

    %if lc.param.Gamma<0 | lc.param.Gamma>50,
    %    result=0;
    %    errorStr=[errorStr sprintf('Error: shannonGap should not be %.2f\n',lc.param.Gamma)];
    %end;

    %% ======================================================================
    % Check tt

    % TBD



    % TBD

    %% =======================================================================
    % Check PBO

    PBOmethod=tfplan.PSD.PBO.method;
    PBOparam=tfplan.PSD.PBO.param;

    switch PBOmethod
        
% $$$         case 'RefLen',	% Reference length method
% $$$             if PBOparam.len>10000,
% $$$                 errorStr=[errorStr sprintf('Error: Too long >10km RefLen')];
% $$$                 result=0;
% $$$             end;
% $$$         case 'RefFEXT',	% Reference FEXT method
% $$$             if PBOparam.len>10000,
% $$$                 errorStr=[errorStr sprintf('Error: Too long >10km RefLen')];
% $$$                 result=0;
% $$$             end;
% $$$         case 'RefFreq'	% Reference frequency method	
% $$$             if PBOparam.len>PBOparam.maxlen,
% $$$                 errorStr=[errorStr sprintf('Warning: PBO.param.len longer than PBO.param.maxlen')];
% $$$                 result=1;
% $$$             end;
% $$$             if PBOparam.freq>30e6,
% $$$                 errorStr=[errorStr sprintf('Error: RefFreq too high >30MHz')];
% $$$                 result=0;
% $$$             end;
% $$$             if PBOparam.freq<0.5e6,
% $$$                 errorStr=[errorStr sprintf('Warning: RefFreq (PBOparam) lower than 0.5 MHz')];
% $$$                 result=1;
% $$$             end;

        otherwise
            
    end; %switch

end;