function identifyTestData(obj, varargin)
%

%   Copyright 2010-2012 The MathWorks, Inc.

    counter = 1;
    for idx=1:length(varargin)
        data = varargin{idx};
        if ~isempty(data)                       
            if ischar(data) || isstring(data)
                rawdata = load(data);
                dataFields = fields(rawdata);
                if length(dataFields)==1 
                    data = rawdata.(dataFields{1});
                else
                    obj.handleMsg('error', message('Sldv:DataMerger:InvalidDataFormat', obj.UtilityName));
                end               
            end
            dataFields = fields(data);
            if length(dataFields)==1 
                data = data.(dataFields{1});
            end              
            if ~isDataVersionAcceptable(data)
                obj.handleMsg('error', message('Sldv:DataMerger:InvalidDataVersion', obj.UtilityName, idx));
            end
            try
                simData = Sldv.DataUtils.getSimData(data);
            catch Mex %#ok<NASGU>
                simData = [];
            end
            if isempty(simData)      
                obj.handleMsg('error', message('Sldv:DataMerger:NoTestCase', obj.UtilityName, idx));               
            end
            try
                interfaceInfo = Sldv.DataUtils.getInterfaceInfo(data);
            catch Mex %#ok<NASGU>
                interfaceInfo = [];
            end
            if isempty(interfaceInfo)      
                obj.handleMsg('error', message('Sldv:DataMerger:NoInterfaceInfo', obj.UtilityName, idx));               
            end
            dataToMerge.TestCases = filterSimData(simData);            
            dataToMerge.InterfaceInfo = interfaceInfo;
            obj.Data(counter) = dataToMerge;
            counter = counter+1;
        end
    end
end

function out = isDataVersionAcceptable(data)
    out = true;
    if ~isfield(data,'LoggedTestUnitInfo') && ...      
        ~isfield(data,'DerivedDataInfo') && ...      
        (~isfield(data,'Version') || ...
           Sldv.DataUtils.dataVersionLessThan(data,'1.3'))   
        out = false;        
    end
end

function simData = filterSimData(simData)
    fieldNames = fieldnames(simData);
    fieldNamesToRemove = ...
        setdiff(fieldNames,{'timeValues','dataValues','paramValues'});
    for idx=1:length(fieldNamesToRemove)
        simData = rmfield(simData,fieldNamesToRemove{idx});
    end    
end