gdata = readtable('Diel_GPP_log.csv');
mission = 3;
s = LoadData(mission,gdata);

idiel = QC(gdata, mission, 'manualflag');
iupper = 1:5;


% Define Params
pH = mean(s.pH25atm(iupper,idiel),1,"omitmissing");
TempC = mean(s.tc(iupper,idiel),1,"omitmissing");
Sal = mean(s.psal(iupper,idiel),1,"omitmissing");
s.o2satconc = reshape(calcO2sat(s.tc(:), s.psal(:)), size(s.pres)); % Is this the correct method for this calc?
s.o2anom = s.doxy - s.o2satconc;
meano2anom = mean(s.o2anom(iupper,idiel),1,"omitmissing");
o2satper = mean(s.o2satper(iupper,idiel),1,"omitmissing");
lon = s.lon(idiel);
lat = s.lat(idiel);
depth = mean(s.depth(iupper,idiel),1,"omitmissing");
pres = mean(s.pres(iupper,idiel),1,"omitmissing");
Coordinates = [lon; lat; depth]';
DIC = mean(s.dic_canb(iupper,idiel),1,"omitmissing")';
sdn = s.sdn(idiel)-7/24; % GMT to local

%%
dt = datetime(datevec(sdn));
dt.TimeZone = 'America/Los_Angeles';
tt = table2timetable(table(dt,TempC', Sal',pH',meano2anom',DIC,VariableNames={'date','TempC', 'Sal','pH','meano2anom','DIC'}));
BP = diff(tt.date.Day) == 1;
tt = detrend(tt,1,BP);
% tt.date.Format = "uuuu-MM-dd";
% dailymeans = retime(tt,'daily','mean');
% for i = height(dailymeans)
%     ii = day(tt.date) == day(dailymeans.date(i)) & month(tt.date) == month(dailymeans.date(i));
%     ttanom = tt(ii,{'DIC','meano2anom','pH','Sal','TempC'}) - dailymeans(i,{'DIC','meano2anom','pH','Sal','TempC'});
% end
% ttdailymeans = tt;
% ttdailymeans(ii,"DIC") = dailymeans(1,"DIC");
% ttanoms = table;
% for i = 1:height(dailymeans.date)
%     ii = day(tt.date) == day(dailymeans.date(i)) & month(tt.date) == month(dailymeans.date(i)) ;
%     ttanoms.TempC(ii) = TempC(ii) - dailymeans.TempC(i);
%     ttanoms.Sal(ii) = Sal(ii) - dailymeans.Sal(i);
%     ttanoms.pH(ii) = pH(ii) - dailymeans.pH(i);
%     ttanoms.meano2anom(ii) = meano2anom(ii) - dailymeans.meano2anom(i);
%     ttanoms.DIC(ii) = DIC(ii) - dailymeans.DIC(i);
% end

%%

figure
clf
plot(dailyAverages.date.Hour,dailyAverages.DIC,'.')
%%

tt(1,2:6) - dailymeans(1,2:6)