function [deltas,occ,groups,pfcUnit,ripples,pfc,hpc,varargin,session] = BatchGetDeltasPostSleep(session,varargin)

[~,sessionName] = fileparts(session);

global BASEPATH
deltas = dlmread(fullfile(BASEPATH, 'data', [sessionName '-deltas-lfp-deltagroups']));
groups = deltas(1,4:end);
deltas(1,:) = [];
occ = deltas(:,4:end);
deltas = deltas(:,1:3);
peaks = dlmread(fullfile(BASEPATH, 'data', [sessionName '-deltas-lfp-deltapeaks']));
peaks(1,:) = []; peaks(:,1) = [];
starts = dlmread(fullfile(BASEPATH, 'data', [sessionName '-deltas-lfp-deltastarts']));
starts(1,:) = []; starts(:,1) = [];
stops = dlmread(fullfile(BASEPATH, 'data', [sessionName '-deltas-lfp-deltastops']));
stops(1,:) = []; stops(:,1) = [];

%% Load session

SetCurrentSession(session,'verbose','off');

try
	load([session(1:max(strfind(session,'/'))) 'UnitSeparation/cells']);
	hpc = GetSpikeTimes(HPCpyr,'output','numbered');
catch 
	hpc = dlmread([session(1:max(strfind(session,'/'))) 'HPC_units_Pyr']);
	HPCpyr = 'not empty'; 
end

PFCgroups = varargin{1};
units = GetUnits;
pfcUnit = units(ismember(units(:,1),PFCgroups(:)),:);

if isempty(HPCpyr) || isempty(pfcUnit),return;end

pfc = GetSpikeTimes(pfcUnit(:,1:2),'output','full');
[okunits,~,pfcID] = unique(pfc(:,2:3),'rows');

pfcUnit(~ismember(pfcUnit(:,1:2),okunits,'rows'),:) = [];

%% Restrict to post-behavioural sleep

try positions = dlmread([session(1:max(strfind(session,'/'))) 'positions']);
    beh = minmax(positions(:,1));
catch
    keyboard
end

ok = InIntervals(deltas,[beh(end) Inf]);
peaks = peaks(ok,:);
deltas = deltas(ok,:);
occ = occ(ok,:);
starts = starts(ok,:);
stops = stops(ok,:);

pfc = [pfc(:,1) pfcID];
ripples = dlmread(fullfile(fileparts(session),'ripples'));








