% Jon Newman
% 2013 12 11

function [spk, wave, lfp] = tank_extract_09(tank_loc, block_loc, fs_wave, wave_ds, fs_lfp)


%% Channel map for this recording session
% [Wave] (continuous)
wave.meta.fs = fs_wave;
wave.meta.fs_wave_ds = fs_wave/wave_ds;

% 1. target firing rate
wave.channels.targ_chan = 1;
% 2. instanteous firing rate
wave.channels.ifr_chan = 2;
% 3. filtered firing rate
wave.channels.filt_chan = 3;
% 4. control on/off (binary)
wave.channels.ctlon_chan = 4;
% 5. error signal
wave.channels.error_chan = 5;
% 6. output signal
wave.channels.out1_chan = 6;
% 7. whisker stimulus waveform (piezo voltage)
wave.channels.whiskerstim_chan = 7;
% 8. trial marker (provided by stimulator, binary)
wave.channels.trial_chan = 8;
% 9. control lock signal (binary)
wave.channels.ctllock_chan = 9;
% 10. k parameter
wave.channels.k_chan = 10;
% 7. Ti parameter
wave.channels.Ti_chan = 11;
% 8. whisker stim on marker (binary)
wave.channels.whiskeron_chan = 12;


%% Access block of interest

% Open the tank
[TT, r] = open_tank(tank_loc)

% Open the block
TT.SelectBlock(block_loc)

%% Wave Stream
% Set the memory limit to 8 gigs
TT.SetGlobalV('WavesMemLimit', 1024^3);

% Get trial marker
TT.SetGlobalV('Channel', tri_chan);
tri = TT.ReadWavesV('Wave');

% Find trial start indicies
wave.series.ind_trial = round(find(diff(tri) > 0)/wave_ds);

clear tri;

% Get target signal
TT.SetGlobalV('Channel', targ_chan);
wave.series.targ = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get the firing rate
TT.SetGlobalV('Channel', ifr_chan);
wave.series.ifr = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get the firing rate
TT.SetGlobalV('Channel', filt_chan);
wave.series.filt = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get ctl on/off bool
TT.SetGlobalV('Channel', ctlon_chan);
wave.series.ctlon = logical(downsample(TT.ReadWavesV('Wave'),wave_ds));

% Get error signal
TT.SetGlobalV('Channel', error_chan);
wave.series.error = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get Ouput signal
TT.SetGlobalV('Channel', out1_chan);
wave.series.out1 = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get the firing rate
TT.SetGlobalV('Channel', whiskerstim_chan);
wave.series.whiskerstim = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get the trial marker bool
TT.SetGlobalV('Channel', trial_chan);
wave.series.trial = logical(downsample(TT.ReadWavesV('Wave'),wave_ds));

% Get target signal
TT.SetGlobalV('Channel', ctllock_chan);
wave.series.ctllock = logical(downsample(TT.ReadWavesV('Wave'),wave_ds));

% Get the firing rate
TT.SetGlobalV('Channel', k_chan);
wave.series.k = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get Ouput signal
TT.SetGlobalV('Channel', Ti_chan);
wave.series.Ti = downsample(TT.ReadWavesV('Wave'),wave_ds);

% Get target signal
TT.SetGlobalV('Channel', whiskeron_chan);
wave.series.whiskerstimon = logical(downsample(TT.ReadWavesV('Wave'),wave_ds));

% make a downsampled set of sample indexes for the 'Wave' data
wave.series.ind = 1:length(wave.series.targ);

%% Neu Streams
N = TT.ReadEventsSimple('eNeu');

%%
spk.waveform = TT.ParseEvV(0, N);
spk.channel = TT.ParseEvInfoV(0, N, 4);
spk.unit = TT.ParseEvInfoV(0, N, 5);
spk.time = TT.ParseEvInfoV(0, N, 6);

%% LFP stream
if ~isempty(fs_lfp)
    lfp = TT.ReadWavesV('LFP1'); 
end










