% Jon Newman

%% file id
fids = { 
    'J:/newmonster/2012_02_09_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-09_chr2_lollipop_10803';
    'J:/newmonster/2012_02_10_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-10_chr2_lollipop_10803_1Hz';
    'J:/newmonster/2012_02_11_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-11_chr2_lollipop_10803';
    'J:/newmonster/2012_02_12_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-12_chr2_lollipop_10803';
    'J:/newmonster/2012_02_14_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-14_chr2_lollipop_10803';
    'J:/newmonster/2012_02_16_Lollipop_Long-Term-optoBB/cl-long-bb_01_2012-02-16_chr2_lollipop_10803';
    };


%% Parameters
% time bounds for the plots
tbound = [0 24*3600]; %3 hrs pre, 12 hrs clamp, 9 hrs post.
sort_bound = [0 3*3600]; % The time to sort unit firing rate over
% Upper bound for UNFR plot
maxFR = 10;
% regions of interests for zoomed rasters, in hours
r1 = 1.5;
r2 = 9.0;
r3 = 16;
r4 = 24;
roiwidth = 60; % region of interest width in seconds
s_T = 10;
tdev = 3600; % time (in seconds) scaling factor

% drug parameters
isdrug = 0;
tdrug = 3600*8;
twash = 3600*10;

dT = 60; % binsize seconds, for all time seris analysis
stimHz = 10;
stimWidthSec = 0.005;
col = hsv(length(fids));

%% Figures
close all
f1 =figure('units','centimeter','position',[5 5 16.5 5]);
f2 = figure('units','centimeter','position',[5 5 16.5 5]);
f3 =figure('units','centimeter','position',[5 5 16.5 5]);
f4 = figure('units','centimeter','position',[5 5 16.5 5]);
f5 = figure('units','centimeter','position',[5 5 16.5 20]);
numsp = length(fids); % number of subplots

for f = 1:length(fids)
    
    % Assing file
    fid = fids{f};

    % Load simoc file
%     sim = loadsimoc([fid '.simoc']);
    
    % Load SS
%     load([fid '.SS'],'-mat');

    % Recalculate ASDR
%     SS.ASDR(dT,[],[],[],[],0);
    
    % target values
    iTargOn = find(sim.target > 0,1,'first');
    iTargOff = find(sim.target > 0,1,'last');
    targetVal = sim.target(iTargOn);
    if targetVal == 1.5
        targetVal = 1.0;
    end
    tTargOn = sim.time(iTargOn);
    tTargOff = sim.time(iTargOff);
    
    
    
    %% bang-bang input signal (duty cycle)
    set(0,'CurrentFigure',f1);
    maxduty = stimWidthSec*stimHz;
%     if f <= 4
        tduty = sim.time(sim.output(1,:) ~= 0);
        c = hist(tduty, SS.asdr.bin);
        duty = maxduty*(c/(100*dT)); % convert to duty cycle
%     else
%         c = hist(SS.st_time, SS.asdr.bin);
%         duty = (c/dT)*stimWidthSec; % convert to duty cycle
%     end
%     
%     semilogy(SS.asdr.bin/tdev,duty,'color',col(f,:))
%     hold on
%     semilogy([0 tbound(2)/tdev],[maxduty maxduty],'r-')
%     ylim([0.001 1.0])
%     xlim(tbound/tdev)
%     ylabel('Duty Cycle')
%     set(gca,'TickDir','out','TickLength',[0.005 0.025],'XAxisLocation','top','Box','off')
%     xlabel('time (hrs)')
    
    set(0,'CurrentFigure',f2);
    hold on
    plot(SS.asdr.bin/tdev,duty,'color',col(f,:))
    plot([0 tbound(2)/tdev],[maxduty maxduty],'r-')
    ylim([0 maxduty+maxduty/2])
    xlim(tbound/tdev)
    ylabel('Duty Cycle')
    set(gca,'TickDir','out','TickLength',[0.005 0.025],'XAxisLocation','top','Box','off')
    xlabel('time (hrs)')
    
    %% UNFR
%     set(0,'CurrentFigure',f3);
%     semilogy(SS.asdr.bin/tdev,SS.asdr.asdr/max(SS.unit),'color',col(f,:))
%     hold on
%     semilogy([tTargOn/tdev tTargOff/tdev],[targetVal targetVal],'b','LineWidth',2)
%     xlim(tbound/tdev)
%     ylim([0.1 10])
%     ylabel('Hz/Unit')
%     set(gca,'TickDir','out','TickLength',[0.005 0.025],'Xtick',[])

    set(0,'CurrentFigure',f4);
    hold on
    plot(SS.asdr.bin/tdev,SS.asdr.asdr/max(SS.unit),'color',col(f,:))
    plot([tTargOn/tdev tTargOff/tdev],[targetVal targetVal],'b','LineWidth',2)
    xlim(tbound/tdev)
    ylim([0 10])
    ylabel('Hz/Unit')
    set(gca,'TickDir','out','TickLength',[0.005 0.025],'Xtick',[])
    
    
    %% USDR
    set(0,'CurrentFigure',f5);
    subplot(numsp,1,f)
    SS.PlotUSDR(maxFR,[],sort_bound)
    set(gca,'XTick',[],'TickDir','out','TickLength',[0.005 0.025],'Box','off')
    xlabel ''
    
end

%% Export figures
% set(0,'CurrentFigure',f1);
% export_fig 'cl-long-bb_lollipop_10803_ctl-sig_log' -pdf -transparent
% export_fig 'cl-long-bb_lollipop_10803_ctl-sig_log' -r400 -transparent
% 
% % Export figures
% set(0,'CurrentFigure',f2);
% export_fig 'cl-long-bb_lollipop_10803_ctl-sig_lin' -pdf -transparent
% export_fig 'cl-long-bb_lollipop_10803_ctl-sig_lin' -r400 -transparent
% 
% % Export figures
% set(0,'CurrentFigure',f3);
% export_fig 'cl-long-bb_lollipop_10803_unfr_log' -pdf -transparent
% export_fig 'cl-long-bb_lollipop_10803_unfr_log' -r400 -transparent
% 
% % Export figures
% set(0,'CurrentFigure',f4);
% export_fig 'cl-long-bb_lollipop_10803_unfr_lin' -pdf -transparent
% export_fig 'cl-long-bb_lollipop_10803_unfr_lin' -r400 -transparent
% 
% % Export figures
% set(0,'CurrentFigure',f5);
% export_fig 'cl-long-bb_lollipop_10803_usdr' -pdf -transparent
% export_fig 'cl-long-bb_lollipop_10803_usdr' -r400 -transparent