clear
outdir   = '~/research/cruise/2019_Brown_0512-0525/data/gps/';
tsgflag  = 1;
flrflag  = 1;
maydate  = 22; % 12:20; % The day in May 2019 to read
inidate  = 12; % the first day of the cruise in May 2019

gpsdir   = '/Volumes/survey/RB1904/NAV/';
tsgdir   = '/Volumes/survey/RB1904/TSG/';
flrdir   = '/Volumes/survey/RB1904/FLUOR/';

% GPS file names (only 20190512 onwards)
gpsfiles = dir([gpsdir,'Primary-GPS-GGA*.Raw']);
gpsfiles = vertcat(gpsfiles.name);

% TSG file names
tsgfiles = dir([tsgdir,'SBE45-TSG-MSG_*.Raw']);
tsgfiles = vertcat(tsgfiles.name);

% FLUOR file names
flrfiles = dir([flrdir,'Fluorometer-DRV_*.Raw']);
flrfiles = vertcat(flrfiles.name);

% initialize the variables with [] for the later concatenation
congpslat   = [];
congpslon   = [];
congpsdate  = [];
if tsgflag
    % initialize the variables with [] for the later concatenation
    cont1       = [];
    conc1       = [];
    cons        = [];
    consv       = [];
    cont2       = [];
    contsgdate  = [];
    contsglat   = [];
    contsglon   = [];
end
if flrflag
    % initialize the variables with [] for the later concatenation
    conflr      = [];
    conflrdate  = [];
    conflrlat   = [];
    conflrlon   = [];
end

for iday = 1:length(maydate)
    gpsinds  = find(contains(gpsfiles,['201905' num2str(maydate(iday))]) == 1);
    gpsfile  = [gpsdir gpsfiles(gpsinds,:)];
    
    if tsgflag
        tsginds  = find(contains(tsgfiles,['201905' num2str(maydate(iday))]) == 1);
        tsgfile  = [tsgdir tsgfiles(tsginds,:)];
    end
    
    if flrflag
        flrinds  = find(contains(flrfiles,['201905' num2str(maydate(iday))]) == 1);
        flrfile  = [flrdir flrfiles(flrinds,:)];
    end
    
    clear gdate glon glat;
    date = maydate(iday);
    disp(['Processing data for May ' num2str(date)]);
    
    % load GPS data
    fid        = fopen(gpsfile);
    wpind      = 0;
    clear llstr
    while ~feof(fid) %Check every line in the file
        wpind  = wpind + 1;
        llstr(wpind,:) = fscanf(fid,'%d/%d/%d,%d:%d:%f,$GPGGA,%d,%f,N,%f,W,%d,%d,%f,%d,%c,,%c,,*%d%c\n',[1,17]);
    end
    fclose(fid);
    for iw = 1:wpind
        gdate(iw) =  datetime(llstr(iw,3),llstr(iw,1),llstr(iw,2),llstr(iw,4),llstr(iw,5),llstr(iw,6));
        glat(iw)  =  floor(llstr(iw,8)/100) -  (floor(llstr(iw,8)/100)*100 - llstr(iw,8))/60;
        glon(iw)  = -floor(llstr(iw,9)/100) + (floor(llstr(iw,9)/100)*100 - llstr(iw,9))/60;
    end
    % remove the repetitive gps time
    [gdate,unqind,repind] = unique(gdate);
    glat = glat(unqind);
    glon = glon(unqind);
    
    if tsgflag
        % load TSG data
        fid          = fopen(tsgfile);
        wpind        = 0;
        badinds      = [];
        endcommainds = [];
        while ~feof(fid) %Check every line in the file
            wpind = wpind + 1;
            line  = fgetl(fid);
            if (length(line) < 80 || contains(line, 'lat') || contains(line, 'A'))
                badinds = [badinds wpind];
            elseif strcmp(line(end-1:end),', ') == 1
                endcommainds = [endcommainds wpind];
            end
        end
        fclose(fid);
        linenumber = wpind;
        
        fid        = fopen(tsgfile);
        wpind      = 0;
        clear llstrtsg
        for i=1:linenumber
            if ismember(i,badinds)
                fgetl(fid);
            elseif ismember(i,endcommainds)
                wpind = wpind + 1;
                llstrtsg(wpind,:) = fscanf(fid,'%d/%d/%d,%d:%d:%f,t1= %f, c1= %f, s= %f, sv=%f, t2=  %f, ',[1,11]);
            else
                wpind = wpind + 1;
                llstrtsg(wpind,:) = fscanf(fid,'%d/%d/%d,%d:%d:%f,t1= %f, c1= %f, s= %f, sv=%f, t2=  %f',[1,11]);
            end
        end
        fclose(fid);
        
        clear tsd_day t1_day c1_day s_day sv_day t2_day;
        tsd_day = datetime([llstrtsg(:,3) llstrtsg(:,1) llstrtsg(:,2) llstrtsg(:,4) llstrtsg(:,5) llstrtsg(:,6)]);
        t1_day  = llstrtsg(:,7);
        c1_day  = llstrtsg(:,8);
        s_day   = llstrtsg(:,9);
        sv_day  = llstrtsg(:,10);
        t2_day  = llstrtsg(:,11);
        
        % obtain the lat-lon of the tsg data using interpolation
        tsginterp_lat        = interp1(gdate, glat, tsd_day,'linear');
        tsginterp_lon        = interp1(gdate, glon, tsd_day,'linear');
        nnind                = find(isnan(tsginterp_lat) | isnan(tsginterp_lon));
        tsginterp_lat(nnind) = [];
        tsginterp_lon(nnind) = [];
        tsd_day(nnind)       = [];
        t1_day(nnind)        = [];
        c1_day(nnind)        = [];
        s_day(nnind)         = [];
        sv_day(nnind)        = [];
        t2_day(nnind)        = [];
    end
    
    if flrflag
        clear flr_day ftime_day;
        
        % load Flr data
        fid        = fopen(flrfile);
        wpind      = 0;
        clear llstrflr;
        while ~feof(fid) %Check every line in the file
            wpind  = wpind + 1;
            llstrflr(wpind,:) = fscanf(fid,'%d/%d/%d,%d:%d:%f,$FLUORO,%f,%f,\n',[1,8]);
        end
        fclose(fid);
        ftime_day =  datetime([llstrflr(:,3) llstrflr(:,1) llstrflr(:,2) llstrflr(:,4) llstrflr(:,5) llstrflr(:,6)]);
        flr_day   =  llstrflr(:,7);
        
        % obtain the lat-lon of the tsg data using interpolation
        flrinterp_lat        = interp1(gdate, glat, ftime_day,'linear');
        flrinterp_lon        = interp1(gdate, glon, ftime_day,'linear');
        nnind                = find(isnan(flrinterp_lat) | isnan(flrinterp_lon));
        flrinterp_lat(nnind) = [];
        flrinterp_lon(nnind) = [];
        ftime_day(nnind)     = [];
        flr_day(nnind)       = [];
    end
    
    lat     = glat(:);
    lon     = glon(:);
    gpsdate = gdate(:);
    gpsoutfile = [outdir 'gps_' num2str(maydate(iday)) '.mat'];
    save(gpsoutfile,'gpsdate', 'lat','lon')
    disp(['Saved file: ' gpsoutfile]);
    gpsmatrix = [year(gpsdate) month(gpsdate) day(gpsdate) hour(gpsdate) minute(gpsdate) second(gpsdate) lat lon];
    xlswrite([outdir 'gps_' num2str(maydate(iday)) '.xls'],gpsmatrix);

    if tsgflag
        t1         = t1_day(:);
        c1         = c1_day(:);
        s          = s_day(:);
        sv         = sv_day(:);
        t2         = t2_day(:);
        date_tsg   = tsd_day(:);
        lat_tsg    = tsginterp_lat(:);
        lon_tsg    = tsginterp_lon(:);
        tsgoutfile = [outdir 'tsg_with_gps_' num2str(maydate(iday)) '.mat'];
        save(tsgoutfile,'date_tsg', 'lat_tsg','lon_tsg', 't1','c1','s','sv','t2')
        disp(['Saved file: ' tsgoutfile]);
        tsgmatrix = [year(date_tsg) month(date_tsg) day(date_tsg) hour(date_tsg) ...
                     minute(date_tsg) second(date_tsg) lat_tsg lon_tsg t1 c1 s sv t2];
        xlswrite([outdir 'tsg_with_gps_' num2str(maydate(iday)) '.xls'],tsgmatrix);
    end
    
    if flrflag
        flr        = flr_day(:);
        date_flr   = ftime_day(:);
        lat_flr    = flrinterp_lat(:);
        lon_flr    = flrinterp_lon(:);
        flroutfile = [outdir 'flr_with_gps_' num2str(maydate(iday)) '.mat'];
        save(flroutfile,'date_flr', 'lat_flr','lon_flr', 'flr')
        disp(['Saved file: ' flroutfile]);
        flrmatrix = [year(date_flr) month(date_flr) day(date_flr) hour(date_flr) ...
                     minute(date_flr) second(date_flr) lat_flr lon_flr flr];
        xlswrite([outdir 'flr_with_gps_' num2str(maydate(iday)) '.xls'],flrmatrix);
    end
    
    % concatenate the variable from each day
    congpslat   = [congpslat;  lat(:)    ];
    congpslon   = [congpslon;  lon(:)    ];
    congpsdate  = [congpsdate; gpsdate(:)];
    
    if tsgflag
        cont1       = [cont1;       t1(:)      ];
        conc1       = [conc1;       c1(:)      ];
        cons        = [cons;        s(:)       ];
        consv       = [consv;       sv(:)      ];
        cont2       = [cont2;       t2(:)      ];
        contsgdate  = [contsgdate;  date_tsg(:)];
        contsglat   = [contsglat;   lat_tsg(:) ];
        contsglon   = [contsglon;   lon_tsg(:) ];
    end
    if flrflag
        conflr      = [conflr;      flr(:)     ];
        conflrdate  = [conflrdate;  date_flr(:)];
        conflrlat   = [conflrlat;   lat_flr(:) ];
        conflrlon   = [conflrlon;   lon_flr(:) ];
    end
end

if maydate(1) > inidate
    % concatenate the variable with data from the previous days
    numpday = maydate-inidate;  % number of previous days to read
    for iday = 1:numpday
        pgpsfile    = [outdir 'gps_' num2str(maydate(1)-iday) '.mat'];
        pgps        = load(pgpsfile);
        congpslat   = [pgps.lat(:);     congpslat ];
        congpslon   = [pgps.lon(:);     congpslon ];
        congpsdate  = [pgps.gpsdate(:); congpsdate];
        
        if tsgflag
            ptsgfile    = [outdir 'tsg_with_gps_' num2str(maydate(1)-iday) '.mat'];
            ptsg        = load(ptsgfile);
            cont1       = [ptsg.t1(:);       cont1;       ];
            conc1       = [ptsg.c1(:);       conc1;       ];
            cons        = [ptsg.s(:);        cons;        ];
            consv       = [ptsg.sv(:);       consv;       ];
            cont2       = [ptsg.t2(:);       cont2;       ];
            contsgdate  = [ptsg.date_tsg(:); contsgdate;  ];
            contsglat   = [ptsg.lat_tsg(:);  contsglat;   ];
            contsglon   = [ptsg.lon_tsg(:);  contsglon;   ];
        end
        if flrflag
            pflrfile    = [outdir 'flr_with_gps_' num2str(maydate(1)-iday) '.mat'];
            pflr        = load(pflrfile);
            conflr      = [pflr.flr(:);      conflr;      ];
            conflrdate  = [pflr.date_flr(:); conflrdate;  ];
            conflrlat   = [pflr.lat_flr(:);  conflrlat;   ];
            conflrlon   = [pflr.lon_flr(:);  conflrlon;   ];
        end
    end
end

% save the concatenated files:

lat     = congpslat(:);
lon     = congpslon(:);
gpsdate = congpsdate(:);
gpsoutfile = [outdir 'gps.mat'];
save(gpsoutfile,'gpsdate', 'lat','lon')
disp(['Saved file: ' gpsoutfile]);
gpsmatrix = [year(gpsdate) month(gpsdate) day(gpsdate) hour(gpsdate) minute(gpsdate) second(gpsdate) lat lon];
xlswrite([outdir 'gps.xls'],gpsmatrix);

if tsgflag
    t1         = cont1(:);
    c1         = conc1(:);
    s          = cons(:);
    sv         = consv(:);
    t2         = cont2(:);
    date_tsg   = contsgdate(:);
    lat_tsg    = contsglat(:);
    lon_tsg    = contsglon(:);
    tsgoutfile = [outdir 'tsg_with_gps.mat'];
    save(tsgoutfile,'date_tsg', 'lat_tsg','lon_tsg', 't1','c1','s','sv','t2')
    disp(['Saved file: ' tsgoutfile]);
    tsgmatrix = [year(date_tsg) month(date_tsg) day(date_tsg) hour(date_tsg) ...
        minute(date_tsg) second(date_tsg) lat_tsg lon_tsg t1 c1 s sv t2];
    xlswrite([outdir 'tsg_with_gps.xls'],tsgmatrix);
end

if flrflag
    flr        = conflr(:);
    date_flr   = conflrdate(:);
    lat_flr    = conflrlat(:);
    lon_flr    = conflrlon(:);
    flroutfile = [outdir 'flr_with_gps.mat'];
    save(flroutfile,'date_flr', 'lat_flr','lon_flr', 'flr')
    disp(['Saved file: ' flroutfile]);
    flrmatrix = [year(date_flr) month(date_flr) day(date_flr) hour(date_flr) ...
        minute(date_flr) second(date_flr) lat_flr lon_flr flr];
    xlswrite([outdir 'flr_with_gps.xls'],flrmatrix);
end

    