% Jon Newman
% Examine CL time series quickly

load('2013_12_22_Smiley-24729_01_parse.SS','-mat');
load('2013_12_22_Smiley-24729_01_oc_parse.mat');
t_pre = 1*3600;
oc.time = oc.time - t_pre;

% Plot parameters
fr_max = 5;
fr_min = -1;

% For getting average perfomance
t_ignore = 30; %sec
t_end = 6*3600;

% Plot downsample factor
dsfac = 1;

% CTL sig
ctlmax = 5;

% step overlay plots
pre_step = 10; % sec
post_step = 70; % sec

% UNFR dt
dtASDR = 1;
dtUNFR = 300; % sec


%% Process data

%% Just the Steps
close all
f = figure('Units','centimeters','Position',[2 2 30 20]);
ah = 1;
aw = 10;
as = 1;

% Short-term ASDR
SS.ASDR(dtASDR,[],[],[],[],0);
axes('Units','centimeters','Position',[2 8 aw ah]);
hold on
stairs((SS.asdr.bin - t_pre)/3600, SS.asdr.asdr/max(SS.unit),'b-')
xlim([-t_pre t_end]/3600)
ylim([0 10])
ylabel('UNFR (Hz/Unit)')
set(gca,'XTick',-1:1:20,'TickDir','out','YTick',[0 5 10 15],'XTickLabel',{}, 'box','off')

% USDR
SS.ASDR(dtUNFR,[],[],[],[],0);
axes('Units','centimeters','Position',[2 10 aw ah]);
SS.PlotUSDR(fr_max,1,[0 3600],1)
set(gca,'TickDir','out','box','off','XTickLabel',{}, 'box','off')
ylabel off
xlabel off

% Long term ASDR
ats = axes('Units','centimeters','Position',[2 6 aw ah]);
hold on
stairs((SS.asdr.bin - t_pre)/3600, SS.asdr.asdr/max(SS.unit),'b-')
stairs(oc.time/3600,oc.targ,'r-')
xlim([-t_pre t_end]/3600)
ylim([0 2])
ylabel('UNFR (Hz/Unit)')
set(gca,'XTick',-1:1:20,'TickDir','out','YTick',[0 5 10 15],'XTickLabel',{}, 'box','off')

% Control Signals
axes('Units','centimeters','Position',[2 4 aw ah]);
hold on
plot(oc.time/3600,oc.out1,'b')
plot(oc.time/3600,oc.out2,'r')
xlim([-t_pre t_end]/3600)
ylim([-0.1 1.1])
ylabel('Control')
set(gca,'TickDir','out','XTick',-1:1:20,'XTickLabel',{})

% Control Signals
axes('Units','centimeters','Position',[2 2 aw ah]);
toff = oc.time([diff(oc.out2) 0] < 0);
c = histc(toff+t_pre,SS.asdr.bin);
offHz = c/SS.asdr.dt;
stairs(SS.asdr.bin/3600 - t_pre/3600,offHz,'b-')
xlim([-t_pre t_end]/3600)
ylim([0 2])
ylabel('Off freq.')
set(gca,'TickDir','out','XTick',-1:1:20, 'box','off')

%% Raster plots of various size

totw = 15;
totr = 14.749;
ratio = totr/totw;

mpre = 0.5*3600;
mclamp = 2.3*3600;
mpost = 6.5*3600;
wind = 5;

% Pre drug
loc = [mpre-wind,mpre+wind];
wp1 = wind*ratio;
ra(1) = axes('units','centimeter','position',[2 12 wp1 0.5]);
SS.RasterPlot(loc,'clean')
set(ra(1),'box','on','TickDir','out','XTick',[],'YTick',[])
xlabel ''
ylabel ''
title ''

axes(ats)
plot(loc(1)/3600-t_pre/3600,2,'r+')

% Drug without clamp
loc = [mclamp-wind/2,mclamp+wind/2];
wp2 = wind*ratio;
ra(2) = axes('units','centimeter','position',[2 14 wp2 0.5]);
SS.RasterPlot(loc,'clean')
hold on
b = oc.time >= (loc(1)-t_pre) & oc.time < (loc(2)-t_pre);
t = oc.time(b);
ctl = oc.out2(b);
ctl(ctl == 0) = nan;
plot(t+t_pre,ctl,'r-')
set(ra(2),'box','on','TickDir','out','XTick',[],'YTick',[])
xlabel ''
ylabel ''
title ''

axes(ats)
plot(loc(1)/3600-t_pre/3600,2,'r+')

% Clamp
loc = [mpost-wind/2,mpost+wind/2];
wp3 = wind*ratio;
ra(3) = axes('units','centimeter','position',[2 16 wp3 0.5]);
SS.RasterPlot(loc,'clean')
set(ra(3),'box','on','TickDir','out','XTick',[],'YTick',[])
xlabel ''
ylabel ''
title ''

axes(ats)
plot(loc(1)/3600-t_pre/3600,2,'r+')

%%
% saveas(gcf, '2013_12_22_Smiley-24729_01_ts_summary')
% export_fig('2013_12_22_Smiley-24729_01_ts_summary','-pdf','-transparent',gcf)

%% Get average light intensity during control epoch


% Load optical conversion table
load('../../optical-power-map/combined_blue-yellow/combined_led_conversions');
POW.targ = 0.75;
POW.yellow = polyval(yellow.p_i2pow,1.0*mean(oc.out2(logical(oc.clon))));
save('arch_onoff_3hr_optical_power','POW');









