% Jon Newman

ah = 5;
aw = 5;
aw2 = 2;
tl_w = 2/(28.3464567*aw);
tl_w2 = 2/(28.3464567*aw2);
tl_h = 2/(28.3464567*ah);

close all
figure('units','centimeter','position',[5 5 20 20]);

% Blue Light
a1 = axes('units','centimeter','position',[5 5 aw ah],'Xlim',[0 10],'XTick',0:10,'TickDir','out','TickLength',[tl_w tl_h]);
a1b = axes('units','centimeter','position',[10 5 aw2 ah],'YTick',[],'Xlim',[10 50],'XTick',10:10:50,'TickDir','out','TickLength',[tl_h tl_h]);
% Yellow Light
a2 = axes('units','centimeter','position',[14 5 aw ah],'TickDir','out','TickLength',[tl_w tl_h]);


%% 1-minute, in-vitro clamps
% Load the previous parsed data
load('./optical_power/in-vitro_pi_optical_power.mat');

for i = 1:size(POW.blue,1)
    
    % Blue signal
    b = POW.blue(i,:) > 0;
    semilogy(a1,POW.targ(b),POW.blue(i,b),'bo-','MarkerSize',3)
    hold(a1,'on')
    
    % Yellow
    b = POW.yellow(i,:) > 0;
    semilogy(a2,POW.targ(b),POW.yellow(i,b),'ro-','MarkerSize',3)
    hold(a2,'on')
    
end

%% On-off controller without drugs
fids = {
    'J:/newmonster/2012_02_09_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-09_chr2_lollipop_10803';      % 0.75 Hz
    'J:/newmonster/2012_02_10_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-10_chr2_lollipop_10803_1Hz';  % 1 Hz
    'J:/newmonster/2012_02_27_Lollipop_Long-Term-optoBB/2012_02_27_Lollipop_Long-Term-optoBB_00';           % 2 Hz
    'J:/newmonster/2012_02_11_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-11_chr2_lollipop_10803';      % 3 Hz
    'J:/newmonster/2012_02_16_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-16_chr2_lollipop_10803';      % 4 Hz
    'J:/newmonster/2012_02_14_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-14_chr2_lollipop_10803';      % 5 Hz
    'J:/newmonster/2012_02_12_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-12_chr2_lollipop_10803';      % 6 Hz
    };

% Load optical conversion table
load('../optical-power-map/combined_blue-yellow/combined_led_conversions');

targ = [];
pow  = [];
for f = 1:length(fids)
    
    fid = fids{f};
    
    % Load simoc file
    sim = loadsimoc([fid '.simoc']);
    
    % target value
    iTargOn = find(sim.target > 0,1,'first');
    iTargOff = find(sim.target > 0,1,'last');
    targetVal = sim.target(iTargOn);
    if targetVal == 1.5 % Account for code screw up early on
        targetVal = 1.0;
    end
    
    targ = [targ targetVal];
    
    % Stim power
    tduty = sim.time(sim.output(1,:) > 0);
    c = histc(tduty, 0:300:sim.time(end));
    stim_freq = 10*c/300/100; % convert to stim freq (update rate is 100 Hz, so need to account for that)
    stim_duty = mean(stim_freq(stim_freq~=0))*0.005;
    stim_pow  = stim_duty.*(polyval(blue.p_i2pow,2.0)); % we were using 2A max at this point
    pow  = [pow stim_pow];
    
end

% Plot 12-hour clamp data
semilogy(a1,targ,pow,'b^-','MarkerSize',3)


%% 3-hr, Arch3.0, on-off control
load('./optical_power/arch_onoff_3hr_optical_power.mat')
semilogy(a2,POW.targ,POW.yellow,'rv','MarkerSize',3)

%% 3-hr, Arch3.0, on-off with BIC
load('./optical_power/2013_12_21_Smiley-24729_POW.mat')
semilogy(a2,POW.targ,POW.yellow,'rs','MarkerSize',3)

%% On-off controller with CNQX 1
sim = loadsimoc('J:\newmonster\2012_03_05_Lollipop-10803_Long-Term-optoBB_CNQX/2012_03_05_Lollipop-10803_Long-Term-optoBB_CNQX_00_20uM-CNQX.simoc');
targetVal = unique(sim.target(sim.target~=0));

% Stim power
tduty = sim.time(sim.output(1,:) > 0);
c = histc(tduty, 0:300:sim.time(end));
stim_freq = 10*c/300/100; % convert to stim freq (update rate is 100 Hz, so need to account for that)
stim_duty = mean(stim_freq(stim_freq~=0))*0.005;
stim_pow  = stim_duty.*(polyval(blue.p_i2pow,2.0));
semilogy(a1,targetVal,stim_pow,'r^','MarkerSize',3)

%% On-off controller with CNQX 2
bang = loadbang('Z:\jon\2013_02_26_Yam-22565_24-hr-cnqx-clamp\2013_02_26_Yam-22565_40-um-cnqx_01.bang');
targetVal = unique(bang.targ(bang.targ~=0));

tduty = bang.time([diff(bang.out1) 0] > 0);
c = histc(tduty, 0:300:bang.time(end));
stim_freq = c/300; % convert to duty cycle
stim_duty = mean(stim_freq(stim_freq~=0))*0.005;
stim_pow  = stim_duty.*(polyval(blue.p_i2pow,2.0));
semilogy(a1,targetVal,stim_pow,'g^','MarkerSize',3)

%% On-off controller with AP5 1
bang = loadbang('Z:\jon\2013_03_11_Yam-22564_24-hr-ap5-clamp\2013_03_11_Yam-22564_50um-ap5_01.bang');
targetVal = unique(bang.targ(bang.targ~=0));

tduty = bang.time([diff(bang.out1) 0] > 0);
c = histc(tduty, 0:300:bang.time(end));
stim_freq = c/300; % convert to duty cycle
stim_duty = mean(stim_freq(stim_freq~=0))*0.005;
stim_pow  = stim_duty.*(polyval(blue.p_i2pow,2.0));
semilogy(a1,targetVal,stim_pow,'yv','MarkerSize',3)

%% On-off controller with AP5 2
bang = loadbang('Z:\jon\2013_03_22_Yam-22565_24-hr-ap5-clamp\2013_03_22_Yam-22565_50um-ap5_01.bang');
targetVal = unique(bang.targ(bang.targ~=0));

tduty = bang.time([diff(bang.out1) 0] > 0);
c = histc(tduty, 0:300:bang.time(end));
stim_freq = c/300; % convert to duty cycle
stim_duty = mean(stim_freq(stim_freq~=0))*0.005;
stim_pow  = stim_duty.*(polyval(blue.p_i2pow,2.0));
semilogy(a1,targetVal,stim_pow,'mv','MarkerSize',3)

%% In-vivo, P01 with optical feedback and LED
load('./optical_power/P01_in-vivo_optical_power.mat')

for i = 1:numel(POW_CELL)
   
    pow = POW_CELL{i};
    b = pow(2,:) > 0;
    semilogy(a1,pow(1,b),pow(2,b),'bx-','MarkerSize',3)
    semilogy(a1b,pow(1,b),pow(2,b),'bx-','MarkerSize',3)
    hold(a1,'on')
    hold(a1b,'on')

end


% Format Axes
set(a1,'Xlim',[-0.5 11],'Ylim',[0.00001 50],'XTick',0:11,'TickDir','out','TickLength',[tl_w tl_h],'YTick',[10^-5,10^-4,10^-3,10^-2,10^-1,1,10,100],'YMinorTick','on','box','off');
set(a1b,'Xlim',[11 45],'Ylim',[0.00001 50],'XTick',10:10:50,'TickDir','out','TickLength',[tl_w tl_h],'YMinorTick','on','YTick',[],'box','off');
set(a2,'Xlim',[-0.5 11],'Ylim',[0.05 15],'XTick',0:11,'TickDir','out','TickLength',[tl_w tl_h],'YMinorTick','on','box','off');

%% Save figure
saveas(gcf,'light_delivery_summary_03')
export_fig -pdf -transparent light_delivery_summary_03.pdf



