
%  SCALE_OPENSIM_WORKFLOW scales the dynamic pipeline model.
%
%     SCALE_OPENSIM_WORKFLOW(FUNCTIONDIRECTORY, SUBJECTID) scales the model 
%     to SUBJECTID using scale.exe within FUNCTIONDIRECTORY and saves 
%     several OpenSim and SIMM model files.

% Change current working directory to function directory
%cd(functionDirectory);

% Copy and rename input files
copyfile([functionDirectory '\HPLScaleSettings.xml'], [dat_path '\HPLScaleSettings.xml']);
copyfile([functionDirectory '\HPLgait.osim'], [dat_path '\HPLgait.osim']);
copyfile([functionDirectory '\HPLMarkerSet.xml'], [dat_path '\HPLMarkerSet.xml']);
copyfile([functionDirectory '\HPL_MeasurementSet.xml'], [dat_path '\HPL_MeasurementSet.xml']);
copyfile([functionDirectory '\HPL_ScaleSet.xml'], [dat_path '\HPL_ScaleSet.xml']);
copyfile([functionDirectory '\HPL_IKTaskSet.xml'], [dat_path '\HPL_IKTaskSet.xml']);



% Load patient's scale setup file
xmlDoc = xmlread('HPLScaleSettings.xml');

% Rename file names
    % ScaleTool root
    xmlDoc.getElementsByTagName('ScaleTool').item(0).getAttributes.item(0).setValue(subject_name)
    % GenericModelMaker children
    xmlDoc.getElementsByTagName('model_file').item(0).getFirstChild.setNodeValue('HPLgait.osim')
    xmlDoc.getElementsByTagName('marker_set_file').item(0).getFirstChild.setNodeValue('HPLMarkerSet.xml')
    % ModelScaler children
    xmlDoc.getElementsByTagName('ScaleSet').item(0).getAttributes.item(0).setValue('HPL_ScaleSet.xml')
    xmlDoc.getElementsByTagName('MeasurementSet').item(0).getAttributes.item(0).setValue('HPL_MeasurementSet.xml')
    xmlDoc.getElementsByTagName('marker_file').item(0).getFirstChild.setNodeValue(static_mkr_file{1}.name)
%     xmlDoc.getElementsByTagName('output_joint_file').item(0).getFirstChild.setNodeValue([subjectID '_scaledOnly.jnt'])
%     xmlDoc.getElementsByTagName('output_muscle_file').item(0).getFirstChild.setNodeValue([subjectID '_scaledOnly.msl'])
    xmlDoc.getElementsByTagName('output_model_file').item(0).getFirstChild.setNodeValue([subject_name '_scaled.osim'])
    xmlDoc.getElementsByTagName('output_scale_file').item(0).getFirstChild.setNodeValue([subject_name '_scaleSet_applied.xml'])
    % MarkerPlacer children
    xmlDoc.getElementsByTagName('marker_file').item(1).getFirstChild.setNodeValue([subjectID '_raw.trc'])
    xmlDoc.getElementsByTagName('IKTaskSet').item(0).getAttributes.item(0).setValue([subjectID '_Scale_Tasks.xml'])
    xmlDoc.getElementsByTagName('coordinate_file').item(0).getFirstChild.setNodeValue([subjectID '_zeros.mot'])
%     xmlDoc.getElementsByTagName('output_joint_file').item(1).getFirstChild.setNodeValue([subjectID '.jnt'])
%     xmlDoc.getElementsByTagName('output_muscle_file').item(1).getFirstChild.setNodeValue([subjectID '.msl'])
    xmlDoc.getElementsByTagName('output_model_file').item(1).getFirstChild.setNodeValue([subjectID '.osim'])
    xmlDoc.getElementsByTagName('output_motion_file').item(0).getFirstChild.setNodeValue([subjectID '_static_output.mot'])
   
% Redefine time range strings
xmlDoc.getElementsByTagName('time_range').item(0).getFirstChild.setNodeValue(num2str(time))
xmlDoc.getElementsByTagName('time_range').item(1).getFirstChild.setNodeValue(num2str(time))

% Redefine mass, height, and age strings
xmlDoc.getElementsByTagName('mass').item(0).getFirstChild.setNodeValue(num2str(mass))
xmlDoc.getElementsByTagName('height').item(0).getFirstChild.setNodeValue(num2str(height))
xmlDoc.getElementsByTagName('age').item(0).getFirstChild.setNodeValue(num2str(age))

% Redefine notes string
xmlDoc.getElementsByTagName('notes').item(0).getFirstChild.setNodeValue('This is a setup file for scale.exe generated during SCALE_OpenSim_Workflow.m')

% Redefine scaling_order string
xmlDoc.getElementsByTagName('scaling_order').item(0).getFirstChild.setNodeValue('measurements')

% Save patient's scale setup file
xmlwrite([subjectID '_Setup_Scale.xml'], xmlDoc);

% Invoke operating system command
Command = ['scale -Setup ' subjectID '_Setup_Scale.xml'];
system(Command);

% Copy scaled filed from function directory to subjectID directory
copyfile([functionDirectory '\' subjectID '_Setup_Scale.xml'], [subjectIDDirectory '\' subjectID '_Setup_Scale.xml']);
copyfile([functionDirectory '\' subjectID '_unscaled.osim'], [subjectIDDirectory '\' subjectID '_unscaled.osim']);
copyfile([functionDirectory '\' subjectID '_Scale_MarkerSet.xml'], [subjectIDDirectory '\' subjectID '_Scale_MarkerSet.xml']);
copyfile([functionDirectory '\' subjectID '_Scale_ScaleSet.xml'], [subjectIDDirectory '\' subjectID '_Scale_ScaleSet.xml']);
copyfile([functionDirectory '\' subjectID '_Scale_MeasurementSet.xml'], [subjectIDDirectory '\' subjectID '_Scale_MeasurementSet.xml']);
% copyfile([functionDirectory '\' subjectID '_scaledOnly.jnt'], [subjectIDDirectory '\' subjectID '_scaledOnly.jnt']);
% copyfile([functionDirectory '\' subjectID '_scaledOnly.msl'], [subjectIDDirectory '\' subjectID '_scaledOnly.msl']);
copyfile([functionDirectory '\' subjectID '_scaledOnly.osim'], [subjectIDDirectory '\' subjectID '_scaledOnly.osim']);
copyfile([functionDirectory '\' subjectID '_scaleSet_applied.xml'], [subjectIDDirectory '\' subjectID '_scaleSet_applied.xml']);
% copyfile([functionDirectory '\' subjectID '.jnt'], [subjectIDDirectory '\' subjectID '.jnt']);
% copyfile([functionDirectory '\' subjectID '.msl'], [subjectIDDirectory '\' subjectID '.msl']);
copyfile([functionDirectory '\' subjectID '.osim'], [subjectIDDirectory '\' subjectID '.osim']);
copyfile([functionDirectory '\' subjectID '_static_output.mot'], [subjectIDDirectory '\' subjectID '_static_output.mot']);
copyfile([functionDirectory '\out.log'], [subjectIDDirectory '\' subjectID '_scale.log']);

% Delete patient files from function directory
delete([functionDirectory '\' subjectID '_raw.trc']);
delete([functionDirectory '\' subjectID '_zeros.mot']);
delete([functionDirectory '\' subjectID '_Setup_Scale.xml']);
delete([functionDirectory '\' subjectID '_unscaled.osim']);
delete([functionDirectory '\' subjectID '_Scale_MarkerSet.xml']);
delete([functionDirectory '\' subjectID '_Scale_ScaleSet.xml']);
delete([functionDirectory '\' subjectID '_Scale_MeasurementSet.xml']);
% delete([functionDirectory '\' subjectID '_scaledOnly.jnt']);
% delete([functionDirectory '\' subjectID '_scaledOnly.msl']);
delete([functionDirectory '\' subjectID '_scaledOnly.osim']);
delete([functionDirectory '\' subjectID '_scaleSet_applied.xml']);
% delete([functionDirectory '\' subjectID '.jnt']);
% delete([functionDirectory '\' subjectID '.msl']);
delete([functionDirectory '\' subjectID '.osim']);
delete([functionDirectory '\' subjectID '_static_output.mot']);
delete([functionDirectory '\markers_coords_ik.sto']);
delete([functionDirectory '\out.log']);

% Change current working directory to subjectID directory
cd(subjectIDDirectory);

% Display progress
% OutputFile1 = [subjectID '_scaledOnly.jnt'];
% OutputFile2 = [subjectID '_scaledOnly.msl'];
OutputFile1 = [subjectID '_scaledOnly.osim'];
OutputFile2 = [subjectID '_scaleSet_applied.xml'];
% OutputFile5 = [subjectID '.jnt'];
% OutputFile6 = [subjectID '.msl'];
OutputFile3 = [subjectID '.osim'];
OutputFile4 = [subjectID '_static_output.mot'];
OutputFile5 = [subjectID '_scale.log'];
fprintf('   The following files have been saved in:\n   %s\n   %s\n   %s\n   %s\n   %s\n', cd, OutputFile1, OutputFile2, OutputFile3, OutputFile4, OutputFile5);

