% load lobo data
load lobo_data_no_headder.ascii
data = lobo_data_no_headder;

% file contains:
% oxygen [ml/l]	fluorescence [micro g/l]	nitrate [micro M]	salinity
% days since 2001-1-1 [UTC]

t = data(:,5);
s = data(:,4);

% QC
bad = find(isnan(s));
t(bad) = [];
s(bad) = [];

% salinity plot
figure
plot(t-t(1),s,'-')
set(gca,'FontSize',14)
xlabel('time [d]')
ylabel('salinity')
title('Lobo Northwest Arm')

% Sampling interval is about 900 s
% Convert time from days to seconds
t = (t-t(1))*86400;
% Interpolate salinity onto regular 900 s grid
t_reg = [0:900:60000*900];
s_reg = interp1(t,s,t_reg);

% Calculate autocovariance
C = xcov(s_reg,'unbiased');

N = length(s_reg);
% Plot autocorrlation (scale C by dividing by variance)
figure
plot(t_reg/86400,C(N:end)/var(s_reg),'-')
set(gca,'FontSize',14)
xlabel('lag [d]')
ylabel('Autocorrelation')
title('Autocorrelogramm of Lobo salinity')
hold on
plot([0 200],[0 0],'k--')
axis([0 200 -0.5 1])
