% 22 Apr 2010 - RUN ON WINDOWS XP ONLY!

%% 1. Enter the parameters needed to specify tank and block to access
% *************************************************************************
tankID = 'C:\Documents and Settings\Annihilatrix II\My Documents\Temp Data\20100417\DataTanks\20100420'; % tank name
blockID = '20100420-27'; % block name, the '~' automatically calls CreateEpocIndexing() so that an epoch index is created for the filtering methods
Ifactor = 10; % scale factor for Imon (pA/mV)
Fs = 24414.1; % Hz
trange = [495 505]; % time range, in seconds that you are intrested in extracting
MaxReturn=1000000; %max number of events to return.
MaxWave = 10*33554432; % max waveform size, bytes
chan2get = 0; % Channels to extract, 0 is all of them
sort2get = 0; % sort code to extract, 0 is all of them
% *************************************************************************
%% 2. 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');
% Select the block to access and set epoch indexing
TT.SelectBlock(blockID);
% Set gobal extraction parameters
TT.SetGlobalV('Channel', chan2get);
TT.SetGlobalV('SortCode', sort2get);
if ~isempty(trange)
    TT.SetGlobalV('T1', trange(1));
    TT.SetGlobalV('T2', trange(2));
end
TT.SetGlobalV('MaxReturn', MaxReturn);
TT.SetGlobalV('WavesMemLimit', MaxWave);

% Enter memo for data
memo = input('Enter a memo about why you are processing these data if you wish: ','s');
if isempty(memo)
    memo = 'none provided.';
end


%%

% Read all the data within the range specified filtered events
data = {};

% data.memo = memo;

% Record the date when the data was recorded
start = TT.CurBlockStartTime;
timeofrec = TT.FancyTime(start ,'Y/O/D H:M:S');
data.rectime = timeofrec;

% Record the sample freq for these data
data.sampfreq = Fs;

% Record TDT-derived notes on the data
data.notes = TT.CurBlockNotes;

% Time range the data is cropped over, empty means not cropped
data.trange = trange;

% Extract intracellular record
intradat = TT.ReadWavesV('Heka');
data.intrarec.vmon = double(intradat(:,1));
data.intrarec.imon = double(Ifactor*detrend(intradat(:,2),'constant')); % remome holding current and save
data.intrarec.stim = double(intradat(:,3));

% Extract extracellular stimulus record
stimdat = TT.ReadWavesV('Stim');
data.stimulus.vapp = double(stimdat(:,1));
data.stimulus.vmon = double(stimdat(:,2));

% Extract extracellular record
N = TT.ReadEventsSimple('eNeu');
data.extrarec.spiketime = TT.ParseEvInfoV(0, N, 6);
data.extrarec.waveform = double(TT.ParseEvV(0, N));
data.extrarec.channel = TT.ParseEvInfoV(0, N, 4);
data.extrarec.unitID = TT.ParseEvInfoV(0, N, 5);

%  Save this cropped data in its own file.
fid = strcat([blockID '_cont_' num2str(trange(1)) 'to' num2str(trange(2)) '.mat']);

fileloc = strcat([tankID '\' blockID '\' fid]);
save(fileloc,'data');

%% 5. Close the tank and release client access to server
TT.CloseTank
TT.ReleaseServer
TT.ClearEpocIndexing