% Jon Newman
% Georgia Tech
% 16 Feb 2010 - Basic data recall
% 17 Feb 2010 - Timing extraction for stimulation events
% 07 Mar 2010 - Extraction of stimulation events with specific parameters
% 08 Mar 2010 - Extraction of records over specific parametric stimulus
% times
%
% This script extracts data from TDT tanks that a filled during concurrent
% patch MEA experiments


%% Parameters needed to specify tank and block to access
tankID = 'C:\Users\Jon\Desktop\20100308\DataTanks\20100308'; % tank name
blockID = 'arasidPOC-4'; % block name, the '~' automatically calls CreateEpocIndexing() so that an epoch index is created for the filtering methods
N = 4; % Number of Tau parameters used
Amp = 0.25;

% Filter parameters

for i = -1:N
    Type = i;
    fid = strcat(['Type_' num2str(Type) '-' 'Amp_' num2str(Amp) '.mat']);
    fileloc = strcat([tankID '\' blockID '\' fid]);
    
    %% Open connection to server
    
    %First instantiate a variable for the ActiveX wrapper interface
    TT = actxcontrol('TTank.X');
    % Then connect to a server.
    TT.ConnectServer('Local', 'Me');
    % Now open a tank for reading.
    TT.OpenTank(tankID, 'R');
    % Set global parameters
    TT.SetGlobals('Channel=0; Options=FILTERED; RespectOffsetEpoc=0')
    % Select the block to access
    TT.SelectBlock(blockID);
    % Create epoch indexing capabilities
    TT.CreateEpocIndexing
    % Set the reference epoch
    TT.SetRefEpocV('StmV');
    %% Get the Epoch Codes
    epochcodes = {};
    i = 0;
    code = 'temp';
    while 1==1
        code = TT.GetEpocCode(i);
        i=i+1;
        if ~strcmp(code,'')
            epochcodes{i} = code;
        else
            clear 'code'
            break
        end
    end
    
    disp('The available epoch string codes for this block: ');
    disp(epochcodes);
    
    %% Filter data so that only epochs for specified events are pulled
    filtstr = strcat(['Type=' num2str(Type) ' and ' 'Amp_=' num2str(Amp)]);
    TT.ResetFilters; % Reset any residual filters that might be hanging around
    TT.SetFilterWithDescEx(filtstr); %Set filter, returns (1) if it succeeds
    disp('The epochs that pass the filter are [[Type;T1;T2],...]:');
    timing = TT.GetEpocsExV('Type',2);
    disp(timing)
    
    %% Read only the filtered events
    data = {};
    
    % Record the stimulus parameters for this data
    data.date = datestr(now,1);
    
    % Record the stimulus parameters for this data
    data.stimpara.Type = Type;
    data.stimpara.Amp_ = Amp;
    
    % Record the valid record time
    if ~isnan(timing)
        data.valitime = timing(2:3,:);
    end
    
    % Extract extracellular record
    % N = TT.ReadEventsSimple('eNeu');
    % data.extrarec.spiketime = TT.ParseEvInfoV(0, N, 6);
    % data.extrarec.waveform = TT.ParseEvV(0, N);
    % data.extrarec.channel = TT.ParseEvInfoV(0, N, 4);
    % data.extrarec.unitID = TT.ParseEvInfoV(0, N, 5);
    
    % Extract intracellular record
    data.intrarec.vmon = TT.ReadWavesOnTimeRangeV('Heka',1);
    data.intrarec.imon = TT.ReadWavesOnTimeRangeV('Heka',2);
    data.intrarec.stim = TT.ReadWavesOnTimeRangeV('Heka',3);
    
    % Extract extracellular stimulus record
    data.stimulus.vapp = TT.ReadWavesOnTimeRangeV('Stim',1);
    data.stimulus.vmon = TT.ReadWavesOnTimeRangeV('Stim',2);
    
    % Close the tank and release client access to server
    TT.CloseTank
    TT.ReleaseServer
    TT.ClearEpocIndexing
    
%     save(fileloc,'data');
end