%parseGPS.m

clear;

raw = '2016Jul12';

path =    sprintf('../%s/raw',raw);
outfile = sprintf('../%s/parsed/CPF%s_GPS.mat',raw,raw);

fileList = dir(path);
fileList = fileList(~[fileList.isdir]); %remove directories
[junk, sortorder] = sort([fileList.datenum]);
fileList = fileList(sortorder); %list is now in ascending date order
numfiles = numel(fileList);
i=1;


for ii = 1:numfiles
   fp = sprintf('%s/%s',path,fileList(ii).name);
   fprintf('Processing %s\n',fp);

   inFID = fopen(fp);
   
    while ~feof(inFID)
        inString = fgets(inFID);
        if (strfind(inString, 'SBDI Msg Num') > 0);
            disp(inString);
            inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
            inFields2 = strsplit(inFields{39},',','CollapseDelimiters', false);
            gps_t{i}=(inFields2{18});
            latmin(i)=str2double(inFields2{2})-3600;
            lonmin(i)=str2double(inFields2{4})-12100;
            prof(i) = sscanf(inFields2{9},'%d');
            i=i+1;
        end
    end

    fclose(inFID);
end

    for j=1:(i-1)
        lat(j) = 36 + latmin(j)/60;
        lon(j) = -1*(121 + lonmin(j)/60);
    end

    save(outfile,'lat','lon','gps_t','prof');

%kmlwrite('23mar.kml',lat,lon,'Name',t);