%% Achilles
% correct axis orientations
tmp         = data_frame.joints;
chk         = tmp{1}(:,2);
tmp{1}(:,2) = tmp{1}(:,3);
tmp{1}(:,3) = -chk;
tmp{4}      = -tmp{4};
tmp{5}(:,1) = -tmp{5}(:,1);
tmp{6}      = -tmp{6};
tmp{7}(:,1) = -tmp{7}(:,1);
chk         = tmp{7}(:,2);
tmp{7}(:,2) = -tmp{7}(:,3);
tmp{7}(:,3) = chk;

% correct joint bounds
tmp         = data_frame.bounds;
chk         = tmp{1}(:,2);
tmp{1}(:,2) = tmp{1}(:,3);
tmp{1}(:,3) = -[chk(2); chk(1)];
tmp{4}      = -[tmp{4}(2,:); tmp{4}(1,:)];
tmp{5}(:,1) = -[tmp{5}(2,1); tmp{5}(1,1)];
tmp{6}      = -[tmp{6}(2,:); tmp{6}(1,:)];
tmp{7}(:,1) = -[tmp{7}(2,1); tmp{7}(1,1)];
chk         = tmp{7}(:,2);
tmp{7}(:,2) = -[tmp{7}(2,3); tmp{7}(1,3)];
tmp{7}(:,3) = chk;

%% Gatsby
%correct axis orientations
tmp         = data_frame.joints;
tmp{1}(:,1) = -tmp{1}(:,1);
chk         = tmp{1}(:,2);
tmp{1}(:,2) = -tmp{1}(:,3);
tmp{1}(:,3) = -chk;
tmp{4}      = -tmp{4};
tmp{5}      = -tmp{5};
tmp{6}      = -tmp{6};
tmp{7}(:,1) = -tmp{7}(:,1);
chk         = tmp{7}(:,2);
tmp{7}(:,2) = tmp{7}(:,3);
tmp{7}(:,3) = -chk;

%% Buddy
% correct axis orientations
tmp         = data_frame.joints;
chk         = tmp{1}(:,2);
tmp{1}(:,2) = tmp{1}(:,3);
tmp{1}(:,3) = -chk;
tmp{4}      = -tmp{4};
tmp{5}(:,1) = -tmp{5}(:,1);
tmp{6}      = -tmp{6};
tmp{7}(:,1) = -tmp{7}(:,1);

%% Correct model fits
cd('E:\GigE_mocap_data\buddy140\buddy140_06082013')
for jj = [1]    
    TrialId = jj;
    fprintf(1,'Correcting Trial %d\n',jj);
    
    folder = sprintf('Data%d%sData%d_km%s',TrialId,filesep,TrialId,filesep);
    tmp    = dir([pwd filesep folder 'km*.mat']);

    % check fits
    for ii = 1:length(tmp)
        chk = load([folder tmp(ii).name]);
        if isempty(chk.model_fit)
            continue
        else
            model_fit = chk.model_fit;
            % Correct values
            theta     = model_fit.theta;
            %swap
            chk       = theta(2);
            theta(2)  = theta(3);
            theta(3)  = chk;
            %change sign
            theta([3:7 10:13]) = -theta([3:7 10:13]);
            % save output
            model_fit.theta = theta;
            save([folder tmp(ii).name],'model_fit')
        end
    end
end