function cleanMag()
%   Function: cleanMag
%   Author: Daniel O'Hara
%   Date: 12/6/2015
%   Description: Function to clean combined magnetic and navigation data to
%   remove erronous and missing values, and to remove high-frequency
%   features using a 10m filter.

    load('codePaths.mat')
    load([path_data,'\Mag_Nav_Data.mat'])

    % Remove data with heading values <0 || >360
    t1 = magLLData(:,13)<0;
    t2 = magLLData(:,13)>360;
    t3 = t1+t2;
    magLLData(t3>=1,:) = [];

    % Remove data with values outside of .4 std from the mean
    t1 = magLLData(:,18) > mean(magLLData(:,18))+.4*std(magLLData(:,18));
    t2 = magLLData(:,18) < mean(magLLData(:,18))-.4*std(magLLData(:,18));
    t3 = t1+t2;
    magLLData(t3>=1,:) = [];

    % Remove any NaN-valued Lat,Lon, or Raw Field records.
    magLLData(isnan(magLLData(:,8)),:) = [];
    magLLData(isnan(magLLData(:,9)),:) = [];
    magLLData(isnan(magLLData(:,18)),:) = [];
    
    % Remove first 150 points of data (which are bad).
    magLLData(1:150,:) = [];

    % Recalculate time and distance differences.
    dif = diff(magLLData(:,6),1,1);
    dif = [0;dif];
    magLLData(:,7) = dif;

    xDif = diff(magLLData(:,10),1,1);
    yDif = diff(magLLData(:,11),1,1);
    sq = [0;sqrt(xDif.^2+yDif.^2)];
    magLLData(:,12) = cumsum(sq);

    % Filter data.
    filterWin = 10;
    magLLData = filterMag(magLLData,filterWin,18,10,11);
    magLLData(1:filterWin,:) = [];
    
    % Save results.
    save([path_data,'\Mag_Nav_Data_Cleaned.mat'],'magLLData','magLLDataTitles')
end