%Glider log tbl
gdata = readtable("Diel_GPP_log.csv")

%
%% 21202901
%

filename = '21202901_QC.mat';
load(filename, 's'); % .mat file
g = gdata(1,:);

%

idiel = g.StartProf:g.EndProf;
iupper = 1:3;
pH = mean(s.pH25atm(iupper,idiel),1,"omitmissing");
sdn = s.sdn(idiel);
dt = datetime(datevec(sdn));
dt.TimeZone = 'UTC';
dt.TimeZone = 'America/Los_Angeles';
sdn = datenum(dt)';

%

[diel, SDNs] = parse_to_diel(sdn, pH, 24);

%

% % Assuming 'A' is your array
% threshold = 0.5; % Set the threshold for NaN values
% rowsToRemove = mean(isnan(diel), 2) > threshold; % Find rows with more than 50% NaN
% diel(rowsToRemove, :) = []; % Remove those rows

%

dateStrings = datestr(SDNs, 'mm/dd/yy');
dateCells = cellstr(dateStrings);

%

s.o2satconc = reshape(calcO2sat(s.tc(:), s.psal(:)), size(s.pres));
s.o2anom = s.doxy - s.o2satconc;
s.meano2anom = mean(s.o2satper(iupper,idiel),1);

%

figure
clf
tl = tiledlayout(3,1);
title(tl,['Glider Mission: ', num2str(g.MissionID)])
subtitle(tl,['pH Diel Cycle ', char(g.StartDate), ' to ', char(g.EndDate)])
nexttile
plot(sdn,pH)
datetick
ylabel('pH 25C')
xlabel('date')
title('pH full time series')

nexttile
plot(0:23,diel)
legend(dateCells,Location='eastoutside')
ylabel('pH 25C')
xlabel('hr of day')
% xticklabels = {'12:00 AM', '6:00 AM', '12:00 PM', '6:00 PM', '11:00 PM'};
xlim([0 23])
title('diel cycle by day')

nexttile
plot(0:23,mean(diel,1,"omitmissing"))
ylabel('pH 25C')
xlabel('hr of day')
title('mean diel cycle')
xlim([0 23])

%
%% 21903401

filename = '21903401_QC.mat';
load(filename, 's'); % .mat file

%

idiel = 97:251;
iupper = 1:3;
pH = mean(s.pH25atm(iupper,idiel),1,"omitmissing");
sdn = s.sdn(idiel);
dt = datetime(datevec(sdn));
dt.TimeZone = 'UTC';
dt.TimeZone = 'America/Los_Angeles';
sdn = datenum(dt)';

%

[diel, SDNs] = parse_to_diel(sdn, pH, 24)

%

figure
clf
tl = tiledlayout(3,1);
title(tl,'Glider Mission 21903401: 10/1/21-10/7/21')
subtitle(tl,'pH Diel Cycle')
nexttile
plot(sdn,pH)
datetick
ylabel('pH 25C')
xlabel('date')

nexttile
plot(0:23,diel)
legend('10/1/21', '10/2/21', '10/3/21', '10/4/21', '10/5/21', '10/6/21', '10/7/21',Location='eastoutside')
ylabel('pH 25C')
xlabel('hr of day')
% xticklabels = {'12:00 AM', '6:00 AM', '12:00 PM', '6:00 PM', '11:00 PM'};
nexttile
plot(0:23,mean(diel,1,"omitmissing"))
ylabel('pH 25C')
xlabel('hr of day')

%
%% 22602901
%

clear all; close all;
filename = '22602901_QC.mat';
load(filename, 's'); % .mat file

%

idiel = 127:323;
iupper = 1:3;
pH = mean(s.pH25atm(iupper,idiel),1,"omitmissing");
sdn = s.sdn(idiel);
dt = datetime(datevec(sdn));
dt.TimeZone = 'UTC';
dt.TimeZone = 'America/Los_Angeles';
sdn = datenum(dt)';

%

[diel, SDNs] = parse_to_diel(sdn, pH, 24)

%

figure
clf
tl = tiledlayout(3,1);
title(tl,'Glider Mission 22602901: 6/22/22-6/30/22')
subtitle(tl,'pH Diel Cycle')
nexttile
plot(sdn,pH)
datetick
ylabel('pH 25C')
xlabel('date')

nexttile
plot(0:23,diel)
legend('6/22/22', '6/23/22', '6/24/22', '6/25/22', '6/26/22',...
    '6/27/22', '6/28/22', '6/29/22', '6/30/22',Location='eastoutside')
ylabel('pH 25C')
xlabel('hr of day')
% xticklabels = {'12:00 AM', '6:00 AM', '12:00 PM', '6:00 PM', '11:00 PM'};
nexttile
plot(0:23,mean(diel,1,"omitmissing"))
ylabel('pH 25C')
xlabel('hr of day')

%
%% 22602901
%

clear all; close all;
filename = '22602901_QC.mat';
load(filename, 's'); % .mat file

%

idiel = 127:323;
% idiel = 713:910;
iupper = 1:3;
pH = mean(s.pH25atm(iupper,idiel),1,"omitmissing");
sdn = s.sdn(idiel);
dt = datetime(datevec(sdn));
dt.TimeZone = 'UTC';
dt.TimeZone = 'America/Los_Angeles';
sdn = datenum(dt)';

%

[diel, SDNs] = parse_to_diel(sdn, pH, 24);

%

figure
clf
tl = tiledlayout(3,1);
title(tl,'Glider Mission 22602901: 6/22/22-6/30/22')
subtitle(tl,'pH Diel Cycle')
nexttile
plot(sdn,pH)
datetick
ylabel('pH 25C')
xlabel('date')

nexttile
plot(0:23,diel)
legend('6/22/22', '6/23/22', '6/24/22', '6/25/22', '6/26/22',...
    '6/27/22', '6/28/22', '6/29/22', '6/30/22',Location='eastoutside')
ylabel('pH 25C')
xlabel('hr of day')
% xticklabels = {'12:00 AM', '6:00 AM', '12:00 PM', '6:00 PM', '11:00 PM'};
nexttile
plot(0:23,mean(diel,1,"omitmissing"))
ylabel('pH 25C')
xlabel('hr of day')