%figure; plot(1/Fs:1/Fs:length(data)/Fs,data);hold on; plot(1/10000:1/10000:length(raster)/10000,raster*10000);
inhib = [18, 20, 23, 24, 25,31,33,37, 38, 39 41,42,43, 44, 47, 58, 60, 61, 62, 63, 77,79, 82, 88, 92, 95, 100, 102, 107, 108, 109];

%loc = [40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 59 60 61];
loc = [93:113];
sz = '462sz11';
load('/gestalt/home/lvf_pac/462_sz11_16_1044.mat')
%'ISOZ' =inhibitory seizure onset zone
%'ESOZ' = excitatory seizurte onset zone
%'INOZ' = inhibitory not seizure onset zone
%'ENOZ' = excitatory not seizure onset zone
%loc = [41 42 43 44 47 58 60 61];
Fs= 40000;
spikephase = {};
freqFilt = [5 15];

for cc = loc
    %patNum = 17;
    disp(cc)
    longest = 41.9;

    %loc = find([DBfinal.patient{:}] == 17);
    lvf = cell2mat(DBfinal.T_LVF(1,cc));
    addon = longest - diff(lvf);
    
    
    chan = DBfinal.chan_num(1,cc);
    raster = cell2mat(DBfinal.Raster(1,cc));
    SOZ = cell2mat(DBfinal.SOZ_code);
    good_unit = cell2mat(DBfinal.long_unit(1,cc));
    

   % load(strcat('TJ049sz1-LVFclips/TJ049_sz1_23:38:3-23:41:3__', num2str(cell2mat(chan)), '.mat'));
   % data =eeg.eeg_data;
   % figure; plot(1/Fs:1/Fs:length(data)/Fs,data);hold on; plot(1/10000:1/10000:length(raster)/10000,raster*10000);
    data = eeg.eeg_data(cell2mat(chan),Fs*lvf(1):Fs*lvf(2));
    
    %A = figure;
   % plot(1/Fs:1/Fs:length(data)/Fs,data); hold on
    raster = raster(1,10000*lvf(1):10000*lvf(2));
    
     %figure; plot(1/Fs:1/Fs:length(data)/Fs,data);hold on; plot(1/10000:1/10000:length(raster)/10000,raster*10000);

    pos = find(raster);
    pos = round(pos*Fs/10000);

    %process data
    freqHFO = [80 600];
    FIRfilt = fir1(500,[freqFilt(1)/(Fs/2) freqFilt(2)/(Fs/2)], 'bandpass');       
    data = filtfilt(FIRfilt, 1, data);
   % plot(1/Fs:1/Fs:length(data)/Fs,data); hold on
    %plot(1/10000:1/10000:length(raster)/10000,raster*10000);
    

    Hildata = hilbert(data);
    sigphase = angle(Hildata);
    if good_unit == 0
        if SOZ(cc) == 1 && length(inhib(inhib ==cc)) > 0
            label = 'ISOZ';
        elseif SOZ(cc) == 1 && length(inhib(inhib ==cc)) == 0
            label =  'ESOZ';
        elseif SOZ(cc) == 0 && length(inhib(inhib ==cc)) > 0
            label = 'INOZ';
        else 
            label = 'ENOZ';
        end
        spikephase(:, end+1) = {sigphase(1,pos); label};
        %saveas(A, strcat(sz,'__',num2str(cell2mat(chan)), '.eps'),'eps')
    else
        spikephase(:, end+1) = {[]; 'long'};
    end
    %else
      %  spikephase{1, end+1} = sigphase(1,pos);
      %  spikephase{2,1} = label;
    %end
    
end
filename = strcat(sz,'_PA.mat');
save(filename, 'spikephase')
clear eeg
clear data
clear Hildata

ISOZ = {};
ESOZ = {};
INOZ = {};
ENOZ = {};

for ss = 1:length(spikephase)
    if spikephase{2,ss} == 'ISOZ'
        disp(1)
         ISOZ{1,end+1} = cell2mat(spikephase(1,ss));
    elseif  spikephase{2,ss} ==  'ESOZ'
        disp(2)
         ESOZ{1,end+1} = cell2mat(spikephase(1,ss));
    elseif spikephase{2,ss} == 'INOZ'
        disp(3)
         INOZ{1,end+1} = cell2mat(spikephase(1,ss));
    else 
        disp(4)
         ENOZ{1,end+1} = cell2mat(spikephase(1,ss));
    end
end

if length(ISOZ) > 0
    A = figure;
    rose(cell2mat(ISOZ))
    title(strcat(sz,' ',' Seizure Onset Zone Inhibitory(20-30Hz)'))
 print(strcat(sz,'__','ISOZ'), '-dpng', '-r300')
end

if  length(ESOZ) > 0
    A = figure;
    rose(cell2mat(ESOZ))
    title(strcat(sz,' ',' Seizure Onset Zone Excitatory(20-30Hz)') )
 print(strcat(sz,'__','ESOZ'), '-dpng', '-r300') 
end

if length(INOZ) > 0
   A = figure;
    rose(cell2mat(INOZ))
   title(strcat(sz,' ',' Outside Seizure Onset Zone Inhibitory(20-30Hz)') )
 print(strcat(sz,'__','INOZ'), '-dpng', '-r300')
end

if length(ENOZ) > 0
    A = figure;
    rose(cell2mat(ENOZ))
   title(strcat(sz,' ',' Outside Seizure Onset Zone Excitatory(20-30Hz)')  )
 print(strcat(sz,'__','ENOZ'), '-dpng', '-r300') 
end