function [sldvData, errorMsg] = convertLoggedSldvDataToHarnessDataFormat(sldvDataLogged, copyModelH, compress)%
%

%   Copyright 2009-2014 The MathWorks, Inc.

    errorMsg = '';
    sldvData = [];
    
    if nargin<3
        compress = true;
    end
    
    if nargin<2
        copyModelH = [];
    end
    
    if ~isfield(sldvDataLogged,'LoggedTestUnitInfo')
        sldvData = sldvDataLogged;
        return;
    end
    
    sldvDataLogged = updateModelIfo(sldvDataLogged, copyModelH);
    
    LoggedTestUnitInfo = sldvDataLogged.LoggedTestUnitInfo;        
    
    if isfield(LoggedTestUnitInfo,'ModelBlock')
        ModelHarnessedInfo = LoggedTestUnitInfo.ModelBlock.ReferencedModel;
    elseif isfield(LoggedTestUnitInfo,'SldvHarnessModel')
        ModelHarnessedInfo = LoggedTestUnitInfo.SldvHarnessModel.TestUnitModel;
    else
        assert(isfield(LoggedTestUnitInfo,'SubsystemLogging'))
        ModelHarnessedInfo = LoggedTestUnitInfo.SubsystemLogging.TestSubsystem;
        ModelInformation.SubsystemPath = LoggedTestUnitInfo.SubsystemLogging.TestSubsystem.SubsystemPath;
	if isfield(LoggedTestUnitInfo.SubsystemLogging.TestSubsystem, 'HarnessOwnerModel')
	    ModelInformation.HarnessOwnerModel = LoggedTestUnitInfo.SubsystemLogging.TestSubsystem.HarnessOwnerModel;
        end
    end
    
    if ~isfield(ModelHarnessedInfo, 'HarnessOwnerModel')
        isLoaded = bdIsLoaded(ModelHarnessedInfo.Name);
        if ~isLoaded
            errorMsg = message('Sldv:MAKEHARNESS:LoadMdl',ModelHarnessedInfo.Name);
            return
        end
    end
    
    %Create Model Information
    ModelInformation.Name = get_param(ModelHarnessedInfo.Name, 'Name');
    ModelInformation.Version = get_param(ModelHarnessedInfo.Name,'ModelVersion');
    ModelInformation.Author = get_param(ModelHarnessedInfo.Name,'Creator'); 
       
    sldvOptions = sldvdefaultoptions(ModelHarnessedInfo.Name);
    
    AnalysisInformation.Status = [];
    AnalysisInformation.AnalysisTime = 0;
    AnalysisInformation.Options = sldvOptions;
    AnalysisInformation.InputPortInfo = ModelHarnessedInfo.InputPortInfo;
    AnalysisInformation.OutputPortInfo = ModelHarnessedInfo.OutputPortInfo;
    AnalysisInformation.SampleTimes = ModelHarnessedInfo.SampleTimes;
    
    sldvData.ModelInformation = ModelInformation;
    sldvData.AnalysisInformation = AnalysisInformation;    
    sldvData.Constraints = [];
    sldvData.ModelObjects = [];
    sldvData.Objectives = []; 
    
    [sldvData, dataUpdated] = ...
        Sldv.DataUtils.addComplexityInformation(sldvData, ModelHarnessedInfo.Name);
    if ~dataUpdated
        %If Used flag is not on the AnalysisInformation, then add it. 
        %If data is already updated then it must be there. 
        sldvData = Sldv.DataUtils.addInportUsage(sldvData);
    end
    
    sldvData = Sldv.DataUtils.setSimData(sldvData,[],sldvDataLogged.TestCases);                
    sldvData = Sldv.DataUtils.correctTestCaseFields(sldvData);
    if compress
        sldvData = Sldv.DataUtils.compressSldvData(sldvData);        
    end
    
    % Version is empty because DV might not be installed
    sldvData.Version = '';
end

function sldvDataLogged = updateModelIfo(sldvDataLogged, copyModelH)
    if ~isempty(copyModelH)
        LoggedTestUnitInfo = sldvDataLogged.LoggedTestUnitInfo;            
        if isfield(LoggedTestUnitInfo,'ModelBlock')
            LoggedTestUnitInfo.ModelBlock.ReferencedModel.Name = ...
                get_param(copyModelH,'Name');            
            if isfield(LoggedTestUnitInfo,'SldvHarnessModel')
                LoggedTestUnitInfo.SldvHarnessModel.TestUnitModel.Name = ...
                    get_param(copyModelH,'Name');
            end
            sldvDataLogged.LoggedTestUnitInfo = LoggedTestUnitInfo;
        end
    end
end