function [tracking] = catPositionsDay(basepath)
% [tracking] = getPositionsDay(basepath)
% Concatenate postions form optitrack for all sessions in a day 
%
% Antonio FR, 4/2019

   cd(basepath);
   basename = bz_BasenameFromBasepath(basepath); 
   
   load([basename '.MergePoints.events.mat']);
   
            % get tracking from individual sessions
            withTracking = [];
            for i = 1:length(MergePoints.foldernames)
                cd(MergePoints.foldernames{i});
                if ~isempty(dir('*.tracking.behavior.mat'))
                   fileT = dir('*.tracking.behavior.mat');
                   trackingSes{i} = load(fileT.name);
                   withTracking = cat(1,withTracking,i);
                end
                clear tracking
                cd ..
            end   
   
            % cat tracking and correct offset
            if exist('trackingSes','var')
               tracking.timestamps = [];
               tracking.orientation.rx=[];tracking.orientation.ry=[];tracking.orientation.rz=[];tracking.orientation.rw=[]; 
               tracking.position.x=[];tracking.position.y=[];tracking.position.z=[];
               for i = 1:length(withTracking)
                   timestampsOffset = trackingSes{withTracking(i)}.tracking.timestamps + MergePoints.timestamps(withTracking(i),1);
                   tracking.timestamps = cat(1,tracking.timestamps,timestampsOffset); clear timestampsOffset;

                   tracking.orientation.rx = cat(1,tracking.orientation.rx,trackingSes{withTracking(i)}.tracking.orientation.rx);
                   tracking.orientation.ry = cat(1,tracking.orientation.ry,trackingSes{withTracking(i)}.tracking.orientation.ry);
                   tracking.orientation.rz = cat(1,tracking.orientation.rz,trackingSes{withTracking(i)}.tracking.orientation.rz);
                   tracking.position.x = cat(1,tracking.position.x,trackingSes{withTracking(i)}.tracking.position.x);
                   tracking.position.y = cat(1,tracking.position.y,trackingSes{withTracking(i)}.tracking.position.y);
                   tracking.position.z = cat(1,tracking.position.z,trackingSes{withTracking(i)}.tracking.position.z);
               end
               save([basename '.tracking.behavior.mat'],'tracking'); 
               %figure;plot(tracking.position.x(1:end-10000),tracking.position.z(1:end-10000));
     
            else
               warning([basename ': NO tracking found']); 
            end

end

