## Author: Jan
## Created: 2016-11-09

function [retval] = trainStorms ()
  #storm search - Dst drops more than 40 points
  trainData = load("trainingYear2005.txt");
  records = size(trainData)(1, 1);
  Dst = trainData(:, 9);
  plot(1:size(Dst, 1), Dst, 'r-');
  hold on;

  trainingStorms = zeros(0, 9);
  storms = 0;

  i=37;
  #i=37:records-110
  while i < records-110
    if Dst(i)-Dst(i+2) >= 40
      trainingStorms = [trainingStorms;trainData((i-36):(i+2+108),:)];
      plot(i-36:i+110, Dst(i-36:i+110), '-', 'Color', 'blue');
      hold on;
      storms = storms+1;
      i=i+110;
    else 
      i=i+1;
    endif  
  endwhile

  normalize = [1, 1, 1, -Inf, -Inf,-Inf, -Inf, -Inf, -Inf];
  for j=1:size(trainingStorms)(1, 1)
    for k=4:9
      if abs(trainingStorms(j, k)) > normalize(1, k)
        normalize(1, k) = abs(trainingStorms(j, k));
      endif
    endfor
  endfor

  trainingStorms = trainingStorms./normalize;

  trainStormsNorm = "trainStormsNorm.txt";
  dlmwrite(trainStormsNorm, trainingStorms);

  normValues = "normValues.txt";
  dlmwrite(normValues, normalize);
endfunction
