% % Jon Newman
% % Examine CL time series quickly
% 
% fid = '2012_07_02_Xeko-20709_one-min-clamp_07';
% sim = loadsimoc([fid '.simoc']);
% spk = loadspk([fid '.spk']);
% 
% %% For getting average perfomance
% t_ignore = 15; %sec
% t_pre = 60;
% t_end = spk.time(end);
% 
% % Defining plot limits
% frmax = 12;
% fr_min = -0.5;
% 
% % Plot downsample factor
% dsfac = 50;
% 
% % CTL sig
% ctlmax = 5;
% 
% % step overlay plots
% pre_step = 10; % sec
% post_step = 10; % sec
% 
% % UNFR dt
% dtUNFR = 10; % sec
% 
% % first step ratio in the logarithm progression 
% first_step_ratio = 1.0;
% 
% % Smooth width
% sm_w = 1;
% 
% %% Process data
% % Smoothed the output
% % sf = smooth(sim.filt,100);
% 
% % ss obj
% ss = SqueakySpk(fid,spk.meta.fs_Hz,1,spk);
% ss.ASDR(dtUNFR,[],[],[],[],0);
% close all;
% 
% % Parse out the step section of the protocol
% b = sim.output(6,:) ~= 0 | sim.output(5,:) ~= 0;
% stepobs = sim.obs(b) ;
% stepfilt = sim.filt(b);
% steptime = sim.time(b);
% stepctl1 = sim.output(6,b);
% stepctl2 = sim.output(5,b);
% steps = sim.target(b);
% usteps = unique(steps);
% 
% % Get avg firing rates during steps
% ufr = [];
% sfr = [];
% uctl1 = [];
% sctl1 = [];
% uctl2 = [];
% sctl2 = [];
% 
% % Save the step sequences for overlay plot
% stime = {};
% ref = {};
% ufr_ts = {};
% ctl_ts = {};
% 
% spk_time = {};
% spk_unit = {};
% 
% for i = 1:length(usteps)
%     
%     % Find the mean UNFR for the current step
%     thistime = steptime(steps == usteps(i));
%     thismean = mean(stepfilt(steps == usteps(i) & steptime > thistime(1) + t_ignore));
%     thiss = std(stepfilt(steps == usteps(i) & steptime > thistime(1) + t_ignore));
%     ufr = [ufr thismean];
%     sfr = [sfr thiss];
%     
% %     thismean = mean(stepctl(steps == usteps(i) & steptime > thistime(1) + t_ignore));
% %     thiss = std(stepctl(steps == usteps(i) & steptime > thistime(1) + t_ignore));
% %     uctl = [uctl thismean];
% %     sctl = [sctl thiss];
%     
%     % Put the step snippet into the time series matrix
%     stime{i} = sim.time(sim.time > (thistime(1) - pre_step) & sim.time < (thistime(end) + post_step)) - thistime(1);
%     ref{i} = sim.target(sim.time > (thistime(1) - pre_step) & sim.time < (thistime(end) + post_step));
%     ufr_ts{i} = sim.filt(sim.time > (thistime(1) - pre_step) & sim.time < (thistime(end) + post_step));
%     ctl1_ts{i} = sim.output(6, sim.time > (thistime(1) - pre_step) & sim.time < (thistime(end) + post_step));
%     ctl2_ts{i} = -sim.output(5, sim.time > (thistime(1) - pre_step) & sim.time < (thistime(end) + post_step));
%     
%     % Get spike data
%     
%     spk_time{i} = spk.time(spk.time > (thistime(1) - pre_step) & spk.time < (thistime(end) + post_step)& spk.unit ~= 0) - thistime(1)
%     spk_unit{i} = spk.unit(spk.time > (thistime(1) - pre_step) & spk.time < (thistime(end) + post_step) & spk.unit ~= 0) ;
%     
% end

%% Just the Steps
close all
wid = 5;
figure('units','centimeters','position',[10 10 20 20])

% Step overlay
ats = axes('units','centimeters','position',[3 5 wid 2.5])
hold on
% col = hsv(length(stime));
% col = gray(length(stime)+6);

% Color map
nc = 10;
r = [23 40 11]/255;
b = [141 211 95]/255;
ff = (1:nc)'/nc;
rc = ff*r;
bc = ff*b;
rc = rc(end:-1:1,:);
col = bc + rc;
col = [col; [0 0 0]];

 
    
for i = 1:length(stime)
    
    % Plot raster
    as = axes('units','centimeters','position',[3 7.5+(i-1)*0.1 wid 0.1]);
    plot(spk_time{i},spk_unit{i},'k.','MarkerSize',0.1)
    set(as,'Xlim',[-pre_step ceil(max(stime{1}))],'Ylim',[1 max(spk.unit)],'XTick',[],'YTick',[])
    
    rt = ref{i};
    tt = stime{i};
    plot(ats, tt,rt,'k')
    plot(ats, tt,ufr_ts{i},'Color',col(i,:))
end
st = stime{1};
r = ref{1};
xlim(ats, [-pre_step ceil(max(stime{1}))])
ylim(ats, [fr_min frmax])
ylabel(ats, 'UNFR (Hz/Unit)')
set(ats, 'XTick',[],'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.01 0.01])

% Control Signal
axes('units','centimeters','position',[3 3.5 wid 1.25])
hold on
for i = 1:length(stime)
    s_u = smooth(ctl1_ts{i},sm_w);
    tt = stime{i};
    plot(tt,s_u,'Color',col(i,:))
end
st = stime{1};
r = ref{1};
xlim([-pre_step ceil(max(stime{1}))])
ylim([-0.1 1.1])
ylabel('UNFR (Hz/Unit)')
set(gca,'XTick',[],'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.01 0.01])

% Control Signal
axes('units','centimeters','position',[3 2 wid 1.25])
hold on
for i = 1:length(stime)
    s_u = smooth(ctl2_ts{i},sm_w);
    tt = stime{i};
    plot(tt,s_u,'Color',col(i,:))
end
st = stime{1};
r = ref{1};
xlim([-pre_step ceil(max(stime{1}))])
ylim([-0.1 1.1])
ylabel('UNFR (Hz/Unit)')
xlabel('Time (sec)')
set(gca,'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.01 0.01])

%%
export_fig([fid '_time-series-summary_01'],'-pdf','-transparent',gcf)
