function removeShipMag()   
%   Function: removeShipMag
%   Author: Daniel O'Hara
%   Date: 12/6/2015
%   Description: Remove ship magnetic field from processed data with
%   background field already removed. Ship magnetic field calculated using
%   the formula a + bcos(H+theta) + ccos(2(H+theta)); 
%   Where:
%       H = ships heading.
%       theta = Correction for magnetometer angle to heading (magnetometer
%           is not exactly astern the ship).
%       a, b, c = constant coefficients.

%   Current estimates suggest:
%       theta = -8 degrees.
%       a = -55.9669
%       b = -116.7476
%       c = 42.7715

    load('codePaths.mat')
    load([path_data,'\Mag_Nav_Data_NoBackground.mat']) % Load processed 
                                                       %    data.
    load([path_data,'\ShipCorrectionCoeffecients.mat']) % Load correction 
                                                  % coefficients and theta.

    tmp = magLLData_noB;

    % If the file of removed ship field already exists, open it to
    % append more to it. Otherwise, create a new file.
    if exist([path_data,'\Mag_Nav_Data_NB_NoShip.mat'],'file')
        load([path_data,'\Mag_Nav_Data_NB_NoShip.mat'])
        tt = magLLData_noB_noS;
        magLLData_noB_noS = tt;
        clear tt
        tmp(tmp(:,6)<=magLLData_noB_noS(end,6),:) = [];
    else
        magLLData_noB_noS = [];
        magLLData_noB_noS_Titles = magLLData_noB_Titles;
    end

    % Calculate ship magnetic field at each reading and remove it from the 
    %   processed results.
    s = size(tmp);
    newTmp = zeros(s(1,1),20);
    for i = 1:s(1,1)
        disp(sprintf('Processing %d / %d',i,s(1,1)))
        row = tmp(i,:);
        row(1,17) = shipCoeffs(1)+shipCoeffs(2).*cosd(row(1,13)+thet)+shipCoeffs(3).*cosd(2.*(row(1,13)+thet));
        row(1,20) = row(1,19)-row(1,17);
        newTmp(i,:) = row;
    end

    % Save results.
    magLLData_noB_noS = [magLLData_noB_noS;newTmp];
    save([path_data,'\Mag_Nav_Data_NB_NoShip.mat'],'magLLData_noB_noS','magLLData_noB_noS_Titles','-v7')
end