%% 
subi = 0;

fnames = get_crowding_fnames('V1');
load('cmap_wave.mat','cmap_wave')
params.OI_thresh = 0;
params.crowder_ori = 'cross';
params.collapse_phases = true;
bw_type = 'cubic';
iplot = 1;
plot_SU_tuning_curves = true;
for fi = 1:length(fnames)
    
    load(fnames(fi).name,'tuning');
    fnames_tuning = fnames(fi);
    tuning_all{1} = tuning;
    oris = unique(tuning.RF_ori);
    oris = oris(~isnan(oris));
    oris_rad = deg2rad(oris);
    cluster_layer_all = cell(length(fnames),1);
    narrowbroad_all = cell(length(fnames),1);
    bw_cell = cell(length(fnames),1);
    params.OI_thresh = 0;
    params.npasses = length(fnames);
    params.reset_passes = 1;
    
    params.u = 'SU';
    params.align_to_peak = 0;
    params.normalize = false;
    params.use_flanker_ori = false;
    params.flanker_type = 'RF_only';
    [~,fr_raw,R_pref_all] = extract_tuning_traces(fnames_tuning,params,tuning_all);
    textprogressbar('done')
    
        load(fnames(fi).name,'cluster_layer','waveform_metrics','cluster_channel','input_layer');
        [narrow,broad] = classify_neurons(waveform_metrics);
        cluster_layer_all{fi} = cluster_layer(:);
        narrowbroad_all{fi} = logical([narrow,broad]);
    %R_pref = R_pref_all{1}(narrow|broad);
    cluster_channel_SU = cluster_channel(narrow|broad)';
    cluster_channel_SU(cluster_channel_SU>32) = cluster_channel_SU(cluster_channel_SU>32)-32;
    fr_mat = cell2mat(fr_raw);
    fr_mat_collapse = (fr_mat(:,1:6) + fr_mat(:,7:end))/2;
    bandwidth_all = nan(size(fr_mat_collapse,1),1);
    bw_calculable = bandwidth_all;
    R_pref = bandwidth_all;
    
    for ii = 1:size(fr_mat_collapse,1)

        if plot_SU_tuning_curves
        figure
        hold on
        [bandwidth_all(ii),bw_calculable(ii),R_pref(ii)] = calc_bandwidth(fr_mat_collapse(ii,:),oris(1:size(fr_mat_collapse,2))',bw_type,plot_SU_tuning_curves);
        iplot = iplot+1;
        fn_save_fig = sprintf('~/BIGFS/Laminar-Crowding/Data/Figures/SU_Bandwidth_Fits/%s_Unit_%d_%s.png',fnames(fi).name,ii,bw_type);
        
        saveas(gcf,fn_save_fig)
        close
        else
                    [bandwidth_all(ii),bw_calculable(ii),R_pref(ii)] = calc_bandwidth(fr_mat_collapse(ii,:),oris(1:size(fr_mat_collapse,2))',bw_type,plot_SU_tuning_curves);
        end
    end
    %%
    figure(1)
    subi = subi+1;subplot(4,7,subi);hold on
    for ut = 1:2
        if ut == 1
            ut_ind = narrow(narrow|broad);
        else
            ut_ind = broad(narrow|broad);
        end
    plot(bandwidth_all(ut_ind),cluster_channel_SU(ut_ind),'o','color',cmap_wave(ut,:));
    plot(-R_pref(ut_ind),cluster_channel_SU(ut_ind),'x','color',cmap_wave(ut,:));
    if ut == 1
        errorbar(mean(-R_pref),-3,std(-R_pref),'ok','horizontal')
        errorbar(mean(bandwidth_all),-3,std(bandwidth_all),'ok','horizontal')
    end
        errorbar(mean(-R_pref(ut_ind)),-ut,std(-R_pref(ut_ind)),'o','horizontal','color',cmap_wave(ut,:))
        errorbar(mean(bandwidth_all(ut_ind)),-ut,std(bandwidth_all(ut_ind)),'o','horizontal','color',cmap_wave(ut,:))
    end
    IL_min = mean([min(input_layer{1}),min(input_layer{2})]);
    IL_max = mean([max(input_layer{1}),max(input_layer{2})]);
    plot([-200 200],[IL_min,IL_min],'k');
    plot([-200 200],[IL_max,IL_max],'k');
    axis([-190 190,-5 35])
    title(fnames(fi).name, 'interpreter', 'none')
    %%
end