function [frparms,detList,sigList,nameList] = checkframeconfig(fname)
% CHECKFRAMECONFIG - read configuration file/struct for information on frames
%   [frparms,detList,sigList,nameList] = checkframeconfig(fname)
%       fname    configuration file name (or structure)
%
%   frparms  frame creation parameters
%   detList - detId for each channel simulated
%   sigList - signal for each channel simulated
%   nameList - output name for each channel simulated
%
%  $Id: checkframeconfig.m,v 1.5 2005/04/21 19:58:49 thorne Exp $

% SET defaults for frame configuration
% IF input is already a structure
%   USE input as reference structure
% ELSE
%   IF frame config file exists
%       READ in frame configuration file
%   ELSE
%       CREATE error and exit
%   ENDIF
%   READ in frame config from file
% ENDIF
% IF frame label defined
%   SET frame label from input
% ENDIF
% IF background defined
%   IF background is valid
%       SET background from input
%   ENDIF
% ENDIF
% IF detector list defined
%   SET detector list from input
% ENDIF
% IF frame channel defined
%   SET frame channel from input
% ENDIF
% IF frame duration defined
%   SET frame duration from input
% ENDIF
DEF_FRAME_TYPE = 'graven';
DEF_CHAN_DESC = '[H1:AS_Q,H2:AS_Q,L1:AS_Q]';
DEF_CHAN_DESC = strassign(DEF_CHAN_DESC);
DEF_DURATION = 16;
frparms = struct('instrument',[],...
              'type',DEF_FRAME_TYPE,...
              'duration',DEF_DURATION,...
              'bkgd','none',...
              'chanDesc',[],...
              'chanName',[],...
              'frameDirFlag',false,...
              'frameDir',[]);
frparms.chanDesc = DEF_CHAN_DESC;
detList = [];
sigList = [];
nameList = [];

tmpStruct = struct('null','');
if(isstruct(fname))
    tmpStruct = fname;
else
    if(exist(fname,'file'))
        d = load(fname);
    else
        matFile = strcat(fname,'.mat');
        if(exist(matFile,'file'))
            d = load(matFile);
        else
            msgId = 'checkframeconfig:noConfigFile';
            error(msgId,'%s: Frame config file %s does not exist',msgId,fname);
        end
    end
    if(isfield(d,'frparms'))
        tmpStruct = d.frparms;
    end
end
if(isfield(tmpStruct,'instrument'))
    frparms.instrument = tmpStruct.instrument;
end
if(isfield(tmpStruct,'type'))
    frparms.type = tmpStruct.type;
end
if(isfield(tmpStruct,'duration'))
    frparms.duration = tmpStruct.duration;
end
if(isfield(tmpStruct,'bkgd'))
    frparms.bkgd = tmpStruct.bkgd;
end
if((isfield(tmpStruct,'chanDesc')) && (~isempty(tmpStruct.chanDesc)))
    frparms.chanDesc = tmpStruct.chanDesc;
else
    frparms.chanDesc = DEF_CHAN_DESC;
end

chanDesc = frparms.chanDesc;
numChan = numel(chanDesc);
detList = cell(1,numChan);
sigList = cell(1,numChan);
DEF_SIG = [];
for k = 1:numChan
    detStr = char(chanDesc{k});
    numChar = numel(detStr);
    strPos = strfind(detStr,':');
    if(strPos > 0)
        detEnd = strPos - 1;
        chanBeg = strPos + 1;
    else
        detEnd = numChar;
        chanBeg = numChar+1;
    end
    detList{1,k} = detStr(1:detEnd);
    if(numChar < chanBeg)
        sigList{1,k} = DEF_SIG;
    else
        sigList{1,k} = detStr(chanBeg:end);
    end
end
if(isfield(tmpStruct,'chanName') && (~isempty(tmpStruct.chanName)))
    frparms.chanName = tmpStruct.chanName;
    nameList = frparms.chanName;
else
    nameList = cell(1,numChan);
    for k = 1:numChan
        sigId = sigList{1,k};
        if(isempty(sigId))
            nameList{1,k} = sprintf('%s:GW',...
                char(detList{1,k}));
        else
            nameList{1,k} = sprintf('%s:GW-%s',...
                char(detList{1,k}),sigId);
        end
    end
    frparms.chanName = nameList;
end
if(isfield(tmpStruct,'frameDir'))
    if(~isempty(tmpStruct.frameDir))
        frparms.frameDirFlag = true;
        frparms.frameDir = tmpStruct.frameDir;
    end    
end
return
