% Jon Newman
% Ga Tech

% 14 Apr 2010

% Takes data that has already been sorted and pulled from TDT tank by
% stimulus and crops it to a subset of the the full recording time. Useful
% for cutting off the tails of the record where the data may be bad for
% some reason. An alternative is to set T1 and T2 while actually creating
% the data files, but this is a failsafe in case you forget to do that.

dirID = 'C:\TempData\20100409\20100409-1\';
dirdat = dir(strcat([dirID '*.mat']));

% Crop parameters
% *************************************************************************
T1 = 137.5; % Start of crop (sec);
T2 = 211; % end of the crop (sec);
% *************************************************************************

for j = 1:size(dirdat,1)
    fid = dirdat(j).name;
    load(strcat([dirID fid]));
    
    % Append crop params to data file
    if isfield(data,'valitime') && ~isempty(data.valitime) && unique(~isnan(data.valitime))
        data.crop = [T1 T2];
        epochStartTimes = data.valitime(1,:);
        croppedInd = logical((epochStartTimes>T1).*(epochStartTimes<T2));
        if ~sum(croppedInd)
            reply = input('Warning: No records in this file pass the crop condition. Do you want to delete this record? Y/N [Y]: ', 's');
            if isempty(reply)
                reply = 'Y';
            end
            if reply == 'Y';
              delete(strcat([dirID fid]));
              break
            end
        end
        data.valitime = data.valitime(:,croppedInd);
    else
        error('No "valitime" field for referencing the crop.');
    end
    
    % Crop intracell data
    if isfield(data,'intrarec') && unique(~isnan(data.intrarec.vmon))
        data.intrarec.vmon = data.intrarec.vmon(:,croppedInd);
        data.intrarec.imon = data.intrarec.imon(:,croppedInd);
        data.intrarec.stim = data.intrarec.stim(:,croppedInd);
    end
    
    % Crop stimulus data
    if isfield(data,'stimulus') && unique(~isnan(data.stimulus.vapp))
        data.stimulus.vapp = data.stimulus.vapp(:,croppedInd);
        data.stimulus.vmon = data.stimulus.vmon(:,croppedInd);
    end
    
    % Crop extracell data
    if isfield(data,'extrarec') && unique(~isnan(data.extrarec.spiketime))
        spInd = find(T1 < data.extrarec.spiketime < T1);
        data.extrarec.spiketime = data.extrarec.spiketime(spInd);
        data.extrarec.waveform = data.extrarec.waveform(:,spInd);
        data.extrarec.channel = data.extrarec.channel(spInd);
        data.extrarec.unitID = data.extrarec.unitID(spInd);
    end
    
    % Crop arasid data
    if isfield(data,'arasid') && ~ischar(data.arasid.estifact)
        data.arasid.iart = data.arasid.iart(:,croppedInd);
        data.arasid.estifact = data.arasid.estifact(:,croppedInd);
        data.arasid.fits = data.arasid.fits(croppedInd);
    else
        data.arasid.iart = data.arasid.iart(:,croppedInd);
    end
    
    % Resave
    disp(strcat(['Cropped data. Re-saving file as: ' fid]));
    save(strcat([dirID fid]),'data');
    
end