% Jon Newman
% Ga Tech

% 15 Apr 2010

% Plots intracellular current and extracellular stimulus data that has been
% processed by my batch processing routines.

% Specify tank and block and plot parameters
% *************************************************************************
tankID = 'C:\TempData\20100420\20100420-20\';
dirdat = dir(strcat([tankID '*.mat']));
stimRange = [-1.5 1.5]; % Volts
recRange = [-100 50]; %pA
% *************************************************************************

% Display available files
disp('Available data files are:');
for i = 1:size(dirdat,1)
    disp(strcat(['[' num2str(i) ']: ' dirdat(i).name]));
end
reply = input('Which Record do you want? [Enter a number]: ');
if isempty(reply)
    reply = 1;
end

% Load a file
fid = dirdat(reply).name;
load(strcat([tankID fid]));

% Plot the data

% Time data
time = (1/data.sampfreq)*(1:length(data.stimulus.vapp));


h = figure();

% Stimulus plot
subplot(311)
plot(time,data.stimulus.vapp)
xlabel('Time (sec)')
ylabel('Stim (V)')
title(strcat(['V_stim vs I_mem: ' fid]),'Interpreter','none')
ylim(stimRange);

% Get PSC coordinates
coords = [];
if isfield(data,'psc')
    for i = 1:size(data.psc.coords,2)
        coords = [coords;data.psc.coords{i}]; 
    end
end

% Raw Current plot
subplot(312)
plot(time,data.intrarec.imon);
if isfield(data,'psc') && ischar(data.arasid.iart)
    hold on
    plot((1/data.sampfreq)*coords(:,1),coords(:,2),'k*')
end
xlabel('Time (sec)')
ylabel('I_{pip} (pA)')
ylim(recRange);


% Arasid plot

if data.arasid.percentcut ~= 0
    pad1 = floor((length(time)-length(data.arasid.iart))/2);
    pad2 = ceil((length(time)-length(data.arasid.iart))/2);
    plotiart = [zeros(pad1,size(data.arasid.iart,2)); data.arasid.iart; zeros(pad2,size(data.arasid.iart,2))];
end

subplot(313)
if ~ischar(data.arasid.iart)
    plot(time,plotiart)
    if isfield(data,'psc')
        hold on
        plot(pad1/data.sampfreq+(1/data.sampfreq)*coords(:,1),coords(:,2),'k*')
    end
end
xlabel('Time (sec)')
ylabel('I_{pip,corrected} (pA)')
ylim(recRange);

findfigs

reply = input('Save Figure? [y/n]: ','s');
if isempty(reply)
    reply = 'y';
end

if reply == 'y'
    figdir = strcat([tankID fid(1:end-4) '_FIG']);
    figfid = strcat([fid(1:end-4) '_Vstim_Imem']);
    if ~exist(figdir,'dir')
        mkdir(figdir);
    end
    saveas(h, strcat([figdir '\' figfid '.fig']))
    saveas(h, strcat([figdir '\' figfid '.jpg']))
end

close all
