clear
outdir = '~/research/cruise/2019_Brown_0512-0525/data/gps/';
tsgflag = 1;
flrflag = 1;
daylim = 3; % use only data from last n days

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);
gpsinds = ~contains(gpsfiles,'20190506') & ~contains(gpsfiles,'20190507') & ...
~contains(gpsfiles,'20190508') & ~contains(gpsfiles,'20190509') & ...
~contains(gpsfiles,'20190510') & ~contains(gpsfiles,'20190511');
gpsfiles = gpsfiles(gpsinds,:);

% 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);
flrinds = ~contains(flrfiles,'20190511');
flrfiles = flrfiles(flrinds,:);

% cruise dates
cruisedays = str2num(gpsfiles(:,17:24));

% initialize the variables with [] for the later concatenation
gpslat = [];
gpslon = [];
gpsdate = [];
if tsgflag
t1 = [];
c1 = [];
s = [];
sv = [];
t2 = [];
tsgdate = [];
tsglat = [];
tsglon = [];
end
if flrflag
flr = [];
flrdate = [];
flrlat = [];
flrlon = [];
end

for cruiseday=(length(cruisedays)-daylim+1):length(cruisedays)
clear gdate glon glat;
date = cruisedays(cruiseday);
disp(date)

% load GPS data
gpsdayfile = [gpsdir gpsfiles(cruiseday,:)];
fid = fopen(gpsdayfile);
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
tsgdayfile = [tsgdir tsgfiles(cruiseday,:)];
fid = fopen(tsgdayfile);
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(tsgdayfile);
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;
% for iw = 1:wpind
% tsd_day(iw) = datetime(llstr(iw,3),llstr(iw,1),llstr(iw,2),llstr(iw,4),llstr(iw,5),llstr(iw,6));
% t1_day(iw) = llstr(iw,7);
% c1_day(iw) = llstr(iw,8);
% s_day(iw) = llstr(iw,9);
% sv_day(iw) = llstr(iw,10);
% t2_day(iw) = llstr(iw,11);
% end
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
flrdayfile = [flrdir flrfiles(cruiseday,:)];
fid = fopen(flrdayfile);
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);
%for iw = 1:wpind
% ftime_day(iw) = datetime(llstr(iw,3),llstr(iw,1),llstr(iw,2),llstr(iw,4),llstr(iw,5),llstr(iw,6));
% flr_day(iw) = llstr(iw,7);
%end
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

% concatenate the variable from each day
gpslat = [gpslat; glat(:)];
gpslon = [gpslon; glon(:)];
gpsdate = [gpsdate; gdate(:)];
if tsgflag
t1 = [t1; t1_day(:)];
c1 = [c1; c1_day(:)];
s = [s; s_day(:)];
sv = [sv; sv_day(:)];
t2 = [t2; t2_day(:)];
tsgdate = [tsgdate; tsd_day(:)];
tsglat = [tsglat; tsginterp_lat(:)];
tsglon = [tsglon; tsginterp_lon(:)];
end
if flrflag
flr = [flr; flr_day(:)];
flrdate = [flrdate; ftime_day(:)];
flrlat = [flrlat; flrinterp_lat(:)];
flrlon = [flrlon; flrinterp_lon(:)];
end
end
lat = gpslat;
lon = gpslon;

gpsoutfile = [outdir 'gps_17to19.mat'];
save(gpsoutfile,'gpsdate', 'lat','lon')
disp(['Saved file: ' gpsoutfile]);

if tsgflag
date_tsg = tsgdate;
lat_tsg = tsglat;
lon_tsg = tsglon;
tsgoutfile = [outdir 'tsg_with_gps_17to19.mat'];
save(tsgoutfile,'date_tsg', 'lat_tsg','lon_tsg', 't1','c1','s','sv','t2')
disp(['Saved file: ' tsgoutfile]);
end

if flrflag
date_flr = flrdate;
lat_flr = flrlat;
lon_flr = flrlon;
flroutfile = [outdir 'flr_with_gps_17to19.mat'];
save(flroutfile,'date_flr', 'lat_flr','lon_flr', 'flr')
disp(['Saved file: ' flroutfile]);
end

gpsmatrix = [year(gpsdate) month(gpsdate) day(gpsdate) hour(gpsdate) minute(gpsdate) second(gpsdate) lat lon];
xlswrite([outdir 'gps_17to19.xls'],gpsmatrix);

%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('tsg_with_gps.xls',tsgmatrix);