%  MeanCrossing.m
% Calculates how many times the piston has crossed the mean position
% for the entire deployment and for each 1 hour data file
clear 
clc

dpath = '134.89.12.189/data/';
dataDirs = dir([dpath 'R*']);

TotalCrossings = 0;
TotalCrossings2 = 0;
MeanPosition=[];
CrossingsPerFile=[]; % Crossing of the average position of each file
CrossingsPerFile2=[]; % Crossing of the average position over the entire deployment
MeanPeriod=[];
n=1;
for k = 2:length(dataDirs) % Skips the first 4 files
  if(dataDirs(k).isdir == 1)
     ppath = [dpath dataDirs(k).name '/'];
     
     matfiles = dir([ppath 'D*.mat']);

     K = length(matfiles);
     for k = 1:K
       load([ppath matfiles(k).name]);
       CorrectedPistonDist=PistonDistance-mean(PistonDistance);
       CorrectedPistonDist2=PistonDistance-38;
       MeanPosition(n)=mean(PistonDistance);
       CrossingsPerFile(n)=length(crossing(CorrectedPistonDist));
       CrossingsPerFile2(n)=length(crossing(CorrectedPistonDist2));
       TotalCrossings=TotalCrossings+CrossingsPerFile(n);
       TotalCrossings2=TotalCrossings2+CrossingsPerFile2(n);
       n=n+1;
     end
  end
end
MeanPeriod=2*3600./CrossingsPerFile;