%   Script: getMagData2
%   Author: Daniel O'Hara
%   Date: 12/6/2015
%   Description: Script to collect any new raw magnetic data from the data
%   server and save the formatted version onto local.

load('codePaths.mat')
folderPath = path_mag;  %local directory.
dataPath = '\\192.168.3.19\mgl1521\raw\serial'; %server directory. 

magDataFileHeader = 'MGL-mag01.'; %File name header.

magDataFiles = ls([dataPath,'\',magDataFileHeader,'*']); %Collect all data 
                                                         %  file titles.

magDataTitles = {'Year','Day','Hour','Minute','Second','Field Strength (nT)'};
    % Formatted data fields.

mGS = size(magDataFiles);

for i = 1:mGS(1,1)-1 %Loop through files, collecting and saving new data. 
                    %   (Currently, ignore newest file since it is still 
                    %   being updated).
    ext = magDataFiles(i,11:end);
    if ~exist([folderPath,'\','Mag_',ext,'_Data.mat'],'file')
        disp(sprintf('Processing %s',magDataFiles(i,:)))
        magData = [];
        fid = fopen([dataPath,'\',magDataFiles(i,:)]);
        c = textscan(fid,'%s %f %f %f %f %f %s %f %s %s','Delimiter',{'\t',':','$',',','\n'});
        magData = [c{2},c{3},c{4},c{5},c{6},c{8}];
        save([folderPath,'\','Mag_',ext,'_Data.mat'],'magData','magDataTitles');
    end
end