#!/usr/bin/env octave

dane = struct(
"auth",
{"mg"; "am"; "mo"; "ms" ; "mr"},
"names",
{ "CEDU"; "CHAT"; "UCSF"; "NKLG" ; "BRAZ" },
"steps",
{ 
  [2005.42, 2016.79 ];
  [2001.9,  2004.3  ];
  [2011.6,  2013.8, 2016.2];
  [2005.8,  2010.34 ]  ;
  [2012.7, 2012.9, 2013.3, 2014.9, 2015.3, 2016.8,  2017.57, 2018.4, 2018.6 ]  ;
  }
);

i = 3;

data = load([dane(i).names ".series"]);

t  = data(:,1);
mt = mean(t);
t -= mt;

A = [
  ones(size(t)) t ...
  cos(2*pi*t) sin(2*pi*t) ...
  cos(2*pi*2*t) sin(2*pi*2*t) ...
  zeros(size(t,1), size(dane(i).steps,2))
  ];

for j = 1:size(dane(i).steps,2)
  A(:,j+6) = (t > (dane(i).steps(j) - mt));
end

for j = 4:4
  X = A\data(:,j)
  hold on
  % plot(t+mt, data(:,j))
  % plot(t+mt, A*X, "linewidth", 3)

  const_trend = X(1) + X(2)*t;
  plot(t+mt, data(:,j) - const_trend)
  plot(t+mt, A*X - const_trend, "linewidth", 3)
  % plot(t+mt, A*X, "linewidth", 3)

  plot(t+mt, data(:,j)-A*X)
  % plot(t, A*X, "linewidth", 3)

  hold off
  pause 
end
