load("22602901_QC.mat");

%

cline_full = 128-20:137+20;
cline_dec = cline_full(1:5:end);

%

clf
plot(s.doxy(cline_dec,200),s.depth(cline_dec,200))
hold on
plot(s.doxy(cline_full,200),s.depth(cline_full,200))

%

clf
plot(s.tc(cline_dec,200),s.depth(cline_dec,200))
hold on
plot(s.tc(cline_full,200),s.depth(cline_full,200))

%

T_obs = s.tc(cline_dec,200);
c_obs = s.doxy(cline_dec,200);
tau = f(T_obs);
time = s.sdn(cline_dec);
time = time';
time_full = time(s.sdn(cline_full));

%

for i = 1:height(time)-1
    b(i) = (1 + 2*(tau(i)/(time(i+1)-time(i))))^-1;
    a(i) = 1 - 2*b(i);
    c_insitu(i) = (1/(2*b(i)))*(c_obs(i+1) - a(i)*c_obs(i));
end

%

b = (1 + 2*(tau./([diff(time); nan]))).^-1;
a = 1 - 2*b;
c_insitu = (1./(2.*b)).*(circshift(c_obs,-1) - a.*c_obs);
c_insitu(end) = NaN;

%

clf
plot(c_insitu,s.depth(cline_dec,200))

%



%



%

T_obs = 16 + (18-16).*rand(10,1); % random values between 16 and 18 
c_obs = 98 + (98-104).*rand(10,1); % random values between 98 and 104
tau = f(T_obs);
time = datenum('04072024','MMDDYYYY'):datenum('04162024','MMDDYYYY');
time = time';

%

b = (1 + 2*(tau./([diff(time); nan]))).^-1;
a = 1 - 2*b;
c_insitu = (1./(2.*b)).*(circshift(c_obs,-1) - a.*c_obs);
c_insitu(end) = NaN;  % or any other value

%

clf
scatter(c_obs,c_insitu)
xlabel('obs')
ylabel('insitu')

%
% - reconstruct the unbiased O2 profile by interpolating the mean of consecutive data points of the unbiased O2 profile to the original measurement times.