function [] = convert_trc2c3d();

%% start clean
clc;
clear all;

%% load Shriners c3d trial of interest
disp('Pick Shriners c3d trial to put clean data in');
[infile1, inpath1]=uigetfile('*.c3d','Select input file');

%% load UW trc file to be converted
disp('Pick trc data to convert to c3d');
[pos,time,f,n,nmrk,mrk_names,file,inpath] = load_trc();

%% what should the new c3d file be called?
sFile = [file '_clean'];

%% convert
trc2c3d_schmitz(infile1,inpath1,pos,mrk_names,sFile);

%% ------------------------------------------------------------------------
%% ------------------------------------------------------------------------
%% ------------------------------------------------------------------------
%% function trc2c3d_schmitz
function done=trc2c3d_schmitz(infile1,inpath1,pos,mrk_names,sFile)

%% open Shriners c3d trial of interest
% infile1,inpath1 specifies the Shriners c3d trial to put clean data in
itf = c3dserver;
openc3d(itf,0,[inpath1 infile1]);

%% replace Shriners 3d data with uw data

% this keeps all of the original c3d information (including marker
% residuals and masks) but replaces the position coordinates with our data
% from the UW

% Write new position values to c3d
nIndex = itf.GetParameterIndex('POINT','LABELS');
nLength = itf.GetParameterLength(nIndex);
  
for i = 0:(nLength-1);       % go through all the markers in the c3d file
    % get marker name/label
    m_name = itf.GetParameterValue(nIndex,i);
    % find that marker in the trc data
    ind = strmatch(m_name, mrk_names,'exact');
    if length(ind)==0;
        % if this marker is not in the trc data, do nothing and proceed to the
        % next marker
    else
        % if the marker is in the trc data, write this new data to the c3d file
        data = pos(:,((3*ind)-2):3*ind);

        for j = 1:size(data,1);  % go through all the frames
            
            % set X positions
            vData_x = data(j,1)*1000;
            if isnan(vData_x) == 1;
                % if there is no number, no data written
            else
                nRet = itf.SetPointData(i,0,j,vData_x);
            end

            % set Y positions
            vData_y = data(j,2)*1000;
            if isnan(vData_y) == 1;
                % if there is no number, no data written
            else
                nRet = itf.SetPointData(i,1,j,vData_y);
            end

            % set Z positions
            vData_z = data(j,3)*1000;
            if isnan(vData_z) == 1;
                % if there is no number, no data written
            else
                nRet = itf.SetPointData(i,2,j,vData_z);
            end

        end
        disp(['Marker ' num2str(i+1) ' written to c3d'])
    end
end

%% rename and save new c3d

done = itf.SaveFile([sFile '.c3d'], -1);

% This saves the open file to the file 'sFile.c3d' having data in the
% original file format. done = 1, if the file was saved correctly.

if done == 1;
    disp('c3d file saved correctly');
else
    disp('c3d file NOT saved correctly');
end

closec3d(itf);