function arWrappedPhase = wrapAngle(arModelPhase)
% wrapAngle
%   arWrappedPhase = wrapAngle(arModelPhase)
%   This function wraps the phase for models created in the script
%   buildSimpleModelSingleBody.m
%
%   Project: IMPACT
%   Title of Code: wrapAngle
%   Relevant additional documentation: 6.18.09091.SCL.R.001 Deliverable 4
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with SgurrControl
%   and acquiring permission. SgurrControl is not liable for any
%   damage caused as a result of unauthorised changes to this
%   document.


% Initialise to current phase
arWrappedPhase = arModelPhase;

% For each value test whether it requires wrapping and wrap appropriately
for iCntPhase=1:numel(arModelPhase)-1
    
    arDiffPhase = diff(arWrappedPhase(iCntPhase:iCntPhase+1));
    
    if diff(arWrappedPhase(iCntPhase:iCntPhase+1))<-350
    
        arWrappedPhase(iCntPhase + 1:end) =...
            arWrappedPhase(iCntPhase + 1:end)...
            + 360*round(abs(arDiffPhase)/360);
    
    else
        
        if diff(arWrappedPhase(iCntPhase:iCntPhase + 1))>350
        
            arWrappedPhase(iCntPhase + 1:end) =...
                arWrappedPhase(iCntPhase + 1:end)...
                - 360*round(abs(arDiffPhase)/360);
        
        end
    end
end
