function root = FMA2CMB_SM11_feb24
%v = load('/data/rat-40/xml_chmap');
%v = load('/data/rat-29/xml_chmap');
  import CMBHOME.*
            import CMBHOME.Utils.*
v = load('/data/SUBCA1/SM11/xml_chmap');


global DATA

if ~isstruct(DATA)
    [filename,path] = uigetfile('*.xml','Please select a parameter file for this session');
    SetCurrentSession([path filename])
    
    
end
path = DATA.session.path;
outfile = [DATA.session.basename '_CMB.mat'];



coords = DATA.positions;




% Find the mean position of all the LEDs
coords(coords==-1 | coords==0)=nan;
[t,x,y] = meanCoord(coords);
x = x*.51;
y= y*.51;


if (isnan(t(1)))
    t(1) = mode(diff(t));
end
% Find the head direction based on the LEDs
if(exist('hd','var')~=1 && size(coords,2)>=5)
    hd = headdirectionLED(coords);
    hd(:,2) = hd(:,2).*(180/pi);
    %     [hd, ~, LEDloc] = headdirectionLED(coords);
else
    hd = nan(length(t),2);
end

%vel = cat(1, 0, sqrt(diff(x).^2+diff(y).^2)) / mode(diff(t));
% Unless the LEDs were mounted on the cup, the HD is inaccurate.
% if(strcmp(LEDloc,'headstage')); hd(:,2) = nan(size(hd,1),1); end

% Create an output file name by strapping '.CMB.mat' to the end of the
% existing file name.


t2 = 26776935;
t1 = 23616586;


dt = 1/39.0625;
t3 = t(end)+dt:dt:t(end)+(t2-t1)/1000;
x2 = zeros(size(t3,2),1);
y2 = zeros(size(t3,2),1);

y = [y;y2];
x = [x;x2];
hd = [hd;nan(length(t3),2)];
t = [t;t3(:)];

event = cell(0,2);
% Create CMBHOME object.
%%
root = CMBHOME.Session('name', outfile, 'b_ts', t,...
    'b_x',x, 'b_y', y, 'b_headdir', hd(:,2),...
    'event', event, 'raw_pos', 1, 'raw_headdir', 1, 'date_created', now, ...
    'epoch', t([1 end]), 'fs_video', 39.0625, 'path_raw_data', path);
%%
root = root.FixPos;
[len,pos,xp,yp,del] =linearizePositionTrack(root.b_x,root.b_y);
root.b_linpos = len;
root.spatial_scale = 1;



%%

units = GetUnits;
spikes = GetSpikeTimes(units,'output','numbered');


for ii = 1:size(units,1)
    
    spk_ts = spikes(spikes(:,2)==ii,1);
    spk_i = time2ind(root.b_ts,spk_ts);
    spk_ts = spk_ts(spk_i>0);
    spk_i = spk_i(spk_i>0);
    
    root.spike(units(ii,1),units(ii,2)) = CMBHOME.Spike('i',spk_i,'ts',spk_ts,'tet_name',num2str(units(ii,:)));
end



%%
root.path_lfp = repmat({[path '/' filename]},128,1);

%%
tet = unique(root.cells(:,1));







for i = 1:length(tet)
    
    
    
    [alldat,ch] = PYR_LFP(tet(i),[root.path_lfp{1}(1:end-3) 'lfp']);
    
    
    signal = alldat{1};
    ts = 0:(1/DATA.rates.lfp):((length(signal)-1)/DATA.rates.lfp);
    fs = DATA.rates.lfp;
    channel_name=['tet ' num2str(tet(i)) ' ch ' num2str(ch)];
    
    root.b_lfp(i) = CMBHOME.LFP(signal, ts, fs, channel_name);
    root.b_lfp(i) = root.b_lfp(i).AppendTheta;
    root.b_lfp(i) = root.b_lfp(i).AppendThetaAmplitude;
    root.b_lfp(i) = root.b_lfp(i).AppendThetaPhase;
    
    if ~isfield( root.lfp_manager,'theta_freq')
        root.lfp_manager.theta_freq=0;
    end
    root.b_lfp(i) = root.b_lfp(i).AppendThetaFreq;
    
    
    root.b_lfp(i).channel_name = channel_name;
end


for ii = 1:size(root.cells,1)
    
    spk_lfp = Utils.SpkInds(ts,root.spike(root.cells(ii,1),root.cells(ii,2)).ts);
    root.spike(root.cells(ii,1),root.cells(ii,2)).i_lfp = spk_lfp;
end




%%
root = defineEventsSes(root);


save([path '/' outfile],'root','-v7.3')
end

