function [LFP_library,LFP_library_flanks1,LFP_library_flanks2,LFP_library_unfiltered,File_Starts,downsampled_KHz,SW_definition_thresh_LFP,EEG_library_1,EEG_library_2]=create_LFP_library_w_flanks_rev(ElecNum,Datafiles,unit_starts_ends,Info,bandpass_SW,bandstop_SW,amp_percent_thresh,SW_range_only,load_also_EEG,all_NREM_starts_for_LFP)
calculate_SW_def_thresh=0;
dsmp=32; dsmp_EEG=2;
LFP_library=cell(1,length(ElecNum));
LFP_library_flanks1=cell(1,length(ElecNum));
LFP_library_flanks2=cell(1,length(ElecNum));
LFP_library_unfiltered=cell(1,length(ElecNum));
File_Starts=nan(1,length(ElecNum));
if load_also_EEG
   EEG_library_1=cell(1,length(ElecNum));
   EEG_library_2=cell(1,length(ElecNum));
end
SW_definition_thresh_LFP=nan(1,length(ElecNum));

for unitnum=1:length(ElecNum)
    LFP=[];
    LFP_unfiltered=[];
    LFP_flanks1=[];
    LFP_flanks2=[];
%     LFP_wider=[]; %070417
    EEG_1=[]; EEG_2=[];
    all_file_starts=[];
    all_file_ends=[];
    for ff=1:length(Datafiles{1,unitnum})
        all_file_starts=[all_file_starts,Datafiles{1,unitnum}(ff,1).start_sample];
        all_file_ends=[all_file_ends,Datafiles{1,unitnum}(ff,1).end_sample];
    end
    first_stable_sample=unit_starts_ends(1,unitnum);
    last_stable_sample=unit_starts_ends(2,unitnum);
    beginning_file=find(all_file_starts<=first_stable_sample,1,'last');
    ending_file=find(all_file_ends>=last_stable_sample,1,'first');
    for ff=beginning_file:1:ending_file
        if ff~=beginning_file 
            difference_between_samples=Datafiles{1,unitnum}(ff,1).start_sample-Datafiles{1,unitnum}(ff-1,1).end_sample;
            if difference_between_samples>1
                LFP=[LFP,zeros(1,round((difference_between_samples-1)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)))];
                LFP_unfiltered=[LFP_unfiltered,zeros(1,round((difference_between_samples-1)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)))];
                LFP_flanks1=[LFP_flanks1,zeros(1,round((difference_between_samples-1)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)))];
                LFP_flanks2=[LFP_flanks2,zeros(1,round((difference_between_samples-1)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)))];
            elseif difference_between_samples<1
                LFP=LFP(1:length(LFP)-round((1-difference_between_samples)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)));
                LFP_unfiltered=LFP_unfiltered(1:length(LFP_unfiltered)-round((1-difference_between_samples)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)));
                LFP_flanks1=LFP_flanks1(1:length(LFP_flanks1)-round((1-difference_between_samples)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)));
                LFP_flanks2=LFP_flanks2(1:length(LFP_flanks2)-round((1-difference_between_samples)/Info{1,unitnum}.SAMPLING*(LFP_SR_KHz/dsmp)));
            end
        end
        a=0;
        LFP_Samples=['CRAW_00',num2str(ElecNum(1,unitnum))];
        LFP_TimeBegin=['CRAW_00',num2str(ElecNum(1,unitnum)),'_TimeBegin'];
        LFP_SR_KHz=['CRAW_00',num2str(ElecNum(1,unitnum)),'_KHz'];
        filename=Datafiles{1,unitnum}(ff,1).name;
        if load_also_EEG
            EEG1_Samples='CRAW_018'; EEG2_Samples='CRAW_020';
            EEG_TimeBegin='CRAW_018_TimeBegin';
            EEG_SR_KHz='CRAW_018_KHz';            
        end
        if exist(filename)
            if load_also_EEG
                load(filename,'-mat',LFP_Samples,EEG1_Samples,EEG2_Samples);
            else
                load(filename,'-mat',LFP_Samples);
            end
            eval(['a=~isstruct(',LFP_Samples,');'])
            if a==1
                if load_also_EEG
                    load(filename,'-mat',LFP_TimeBegin,LFP_SR_KHz,EEG_TimeBegin,EEG_SR_KHz);
                    eval(['CRAW_00',num2str(ElecNum(1,unitnum)),'.Samples=double(CRAW_00',num2str(ElecNum(1,unitnum)),');'])
                    eval(['CRAW_018.Samples=double(CRAW_018);'])
                    eval(['CRAW_020.Samples=double(CRAW_020);'])
                    eval(['CRAW_00',num2str(ElecNum(1,unitnum)),'.KHz=double(CRAW_00',num2str(ElecNum(1,unitnum)),'_KHz);'])
                    eval(['CRAW_00',num2str(ElecNum(1,unitnum)),'.TimeBegin=double(CRAW_00',num2str(ElecNum(1,unitnum)),'_TimeBegin);'])                                        
                    eval(['CRAW_018.KHz=double(CRAW_018_KHz);'])
                    eval(['CRAW_018.TimeBegin=double(CRAW_018_TimeBegin);'])
                else
                    load(filename,'-mat',LFP_TimeBegin,LFP_SR_KHz);
                    eval(['CRAW_00',num2str(ElecNum(1,unitnum)),'.Samples=double(CRAW_00',num2str(ElecNum(1,unitnum)),');'])
                    eval(['CRAW_00',num2str(ElecNum(1,unitnum)),'.KHz=double(CRAW_00',num2str(ElecNum(1,unitnum)),'_KHz);'])
                    eval(['CRAW_00',num2str(ElecNum(1,unitnum)),'.TimeBegin=double(CRAW_00',num2str(ElecNum(1,unitnum)),'_TimeBegin);'])                    
                end
            end
        end
        eval(['signal_LFP=CRAW_00',num2str(ElecNum(1,unitnum)),'.Samples;']);
        eval(['LFP_SR_KHz=CRAW_00',num2str(ElecNum(1,unitnum)),'.KHz;']);
        if load_also_EEG
            signal_EEG_1=CRAW_018.Samples; signal_EEG_2=CRAW_020.Samples;
            EEG_SR_KHz=CRAW_018.KHz;
        end
        if ff==beginning_file
            eval(['file_begin_sample_LFP=floor(CRAW_00',num2str(ElecNum(1,unitnum)),'.TimeBegin*LFP_SR_KHz*1000);'])
            File_Starts(1,unitnum)=file_begin_sample_LFP;                
        end
        signal_LFP=signal_LFP-nanmean(signal_LFP);
        if SW_range_only
            signal_LFP1=signal_LFP;
            signal_LFP=BWfilter_designer(double(signal_LFP), 1000*LFP_SR_KHz, bandpass_SW, bandstop_SW, 'BP'); %120818 previously 10-15 and 9.5-16.5, also used to be 12
            
            signal_LFP_flanks1=BWfilter_designer(double(signal_LFP), 1000*LFP_SR_KHz, [7,10], [6,11], 'BP'); %120818 previously 8-18 and 6.5-19.5, also used to be 12
            signal_LFP_flanks2=BWfilter_designer(double(signal_LFP), 1000*LFP_SR_KHz, [17,21], [16,22], 'BP'); %120818 previously 8-18 and 6.5-19.5, also used to be 12
%             signal_LFP_flanks=BWfilter_designer(double(signal_LFP_flanks), 1000*LFP_SR_KHz, [9,18], [8,19], 'BSTP'); %120818 previously 10-15 and 9-16, also used to be 12
            
            not_filtered_signal_LFP=BWfilter_designer(double(signal_LFP1), 1000*LFP_SR_KHz, [1,70], [0.5,90], 'BP');
%             signal_LFP_wider=BWfilter_designer(double(signal_LFP), 1000*LFP_SR_KHz, [0.1,35], [0.05,40], 'BP'); %070417
        else
            signal_LFP=BWfilter_designer(double(signal_LFP), 1000*LFP_SR_KHz, [0.1,35], [0.05,40], 'BP'); 
        end
        downsampled_LFP=signal_LFP(mod((1:length(signal_LFP)),dsmp)==1); %070417 _normal_ change back to nothing
        not_filtered_downsampled_LFP=not_filtered_signal_LFP(mod((1:length(not_filtered_signal_LFP)),dsmp)==1); %070417 _normal_ change back to nothing
        downsampled_LFP_flanks1=signal_LFP_flanks1(mod((1:length(signal_LFP_flanks1)),dsmp)==1); %070417 _normal_ change back to nothing
        downsampled_LFP_flanks2=signal_LFP_flanks2(mod((1:length(signal_LFP_flanks2)),dsmp)==1); %070417 _normal_ change back to nothing
%         downsampled_LFP_wider=signal_LFP_wider(mod((1:length(signal_LFP_wider)),dsmp)==1); %070417
        LFP=[LFP,downsampled_LFP]; % no assumption of no sample loss between files (see before)                            
        LFP_unfiltered=[LFP_unfiltered,not_filtered_downsampled_LFP]; % no assumption of no sample loss between files (see before)                            
        LFP_flanks1=[LFP_flanks1,downsampled_LFP_flanks1]; % no assumption of no sample loss between files (see before)                            
        LFP_flanks2=[LFP_flanks2,downsampled_LFP_flanks2]; % no assumption of no sample loss between files (see before)                            
%         LFP_wider=[LFP_wider,downsampled_LFP_wider]; %070417
        if load_also_EEG
            signal_EEG_1=signal_EEG_1-nanmean(signal_EEG_1);
            signal_EEG_2=signal_EEG_2-nanmean(signal_EEG_2);
            if SW_range_only
                signal_EEG_1=BWfilter_designer(double(signal_EEG_1), 1000*EEG_SR_KHz, bandpass_SW, bandstop_SW, 'BP');
                signal_EEG_2=BWfilter_designer(double(signal_EEG_2), 1000*EEG_SR_KHz, bandpass_SW, bandstop_SW, 'BP');                               
            else
                signal_EEG_1=BWfilter_designer(double(signal_EEG_1), 1000*EEG_SR_KHz, [0.1,35], [0.05,40], 'BP');
                signal_EEG_2=BWfilter_designer(double(signal_EEG_2), 1000*EEG_SR_KHz, [0.1,35], [0.05,40], 'BP');               
            end
            downsampled_EEG_1=signal_EEG_1(mod((1:length(signal_EEG_1)),dsmp_EEG)==1);
            downsampled_EEG_2=signal_EEG_2(mod((1:length(signal_EEG_2)),dsmp_EEG)==1);
            EEG_1=[EEG_1,downsampled_EEG_1]; % no assumption of no sample loss between files (see before)                                        
            EEG_2=[EEG_2,downsampled_EEG_2]; % no assumption of no sample loss between files (see before)                                        
        end
        clear CRAW_0*
    end
    LFP_library{1,unitnum}=LFP;
    LFP_library_unfiltered{1,unitnum}=LFP_unfiltered;
    LFP_library_flanks1{1,unitnum}=LFP_flanks1;
    LFP_library_flanks2{1,unitnum}=LFP_flanks2;
    if load_also_EEG
        EEG_library_1{1,unitnum}=EEG_1;
        EEG_library_2{1,unitnum}=EEG_2;
    else 
        EEG_library_1{1,unitnum}=NaN;
        EEG_library_2{1,unitnum}=NaN;
    end
    if calculate_SW_def_thresh
        h_LFP=hilbert(LFP); ins_LFP_amp=abs(h_LFP);
    end
    if load_also_EEG
        heeg1=hilbert(EEG_1); ins_EEG1_amp=abs(heeg1);
        heeg2=hilbert(EEG_2); ins_EEG2_amp=abs(heeg1);
    end
    if calculate_SW_def_thresh
    % find the threshold for definition of SW as the 70% precentile of the SW amplitude, across the entire duration of the    
    SW_definition_thresh_LFP(1,unitnum)=prctile(sort(ins_LFP_amp),amp_percent_thresh);
    else
        SW_definition_thresh_LFP=[];
    end
%      % changed 061617, used to be calculated on the entire signal. Now - only on NREM, see below.
    % unit recording
    if ~exist('LFP_SR_KHz','var')
        LFP_SR_KHz=44;
    end
    downsampled_KHz=LFP_SR_KHz/dsmp;
    clear a beginning_file ending_file all_file_starts all_file_ends all_nan difference_between_samples ff filename filename_* ii marker signal_EEG_*
end