% Define files to load LFP from
lfp_files = { ...
    '/gh/data/hc3/pin01-11-04/11-04_22-31-40/11-04_22-31-40.eeg', ...
    '/gh/data/hc3/pin01-11-04/11-05_0-06-51/11-05_0-06-51.eeg', ...
    '/gh/data/hc3/pin01-11-04/11-05_0-24-7/11-05_0-24-7.eeg', ...
    '/gh/data/hc3/gor01-6-7/2006-6-7_16-40-19/2006-6-7_16-40-19.eeg', ...
    '/gh/data/hc3/gor01-6-12/2006-6-12_19-26-43/2006-6-12_19-26-43.eeg', ...
    '/gh/data/hc3/gor01-6-13/2006-6-13_15-44-7/2006-6-13_15-44-7.eeg', ...
    '/gh/data/hc3/vvp01-4-10/2006-4-10_21-2-40/2006-4-10_21-2-40.eeg', ...
    '/gh/data/hc3/vvp01-4-18/2006-4-18_15-23-32/2006-4-18_15-23-32.eeg', ...
    '/gh/data/hc3/vvp01-4-18/2006-4-18_21-22-11/2006-4-18_21-22-11.eeg', ...
    '/gh/data/hc3/ec013.29/ec013.426/ec013.426.eeg', ...
    '/gh/data/hc3/ec013.31/ec013.482/ec013.482.eeg', ...
    '/gh/data/hc3/ec013.38/ec013.658/ec013.658.eeg', ...
    '/gh/data/hc3/ec014.16/ec014.183/ec014.183.eeg', ...
    '/gh/data/hc3/ec014.21/ec014.277/ec014.277.eeg', ...
    '/gh/data/hc3/ec014.28/ec014.440/ec014.440.eeg', ...
    '/gh/data/hc3/ec016.25/ec016.397/ec016.397.eeg', ...
    '/gh/data/hc3/ec016.31/ec016.499/ec016.499.eeg', ...
    '/gh/data/hc3/ec016.37/ec016.582/ec016.582.eeg', ...
    '/gh/data/hc3/f01_maze08/f01_maze08_MS.002/f01_maze08_MS.002.eeg', ...
    '/gh/data/hc3/f01_maze09/f01_maze09_MS.002/f01_maze09_MS.002.eeg', ...
    '/gh/data/hc3/f01_maze11/f01_maze11_MS.002/f01_maze11_MS.002.eeg', ...
    '/gh/data/hc3/g01_maze04/g01_maze04_MS.002/g01_maze04_MS.002.eeg', ...
    '/gh/data/hc3/g01_maze05/g01_maze05_MS.001/g01_maze05_MS.001.eeg', ...
    '/gh/data/hc3/g01_maze11/g01_maze11_MS.002/g01_maze11_MS.002.eeg', ...
    '/gh/data/hc3/i01_maze04/i01_maze04_MS.003/i01_maze04_MS.003.eeg', ...
    '/gh/data/hc3/i01_maze05/i01_maze05_MS.001/i01_maze05_MS.001.eeg', ...
    '/gh/data/hc3/i01_maze15/i01_maze15_MS.002/i01_maze15_MS.002.eeg', ...
    };
    
% Define how many seconds the recording should last
% according to the 'session' table in hc3-tables
N_sec = [1811.692, 621.3458, 566.4848, ...
    2587.808, 2358.296, 2581.014, ...
    1100.756, 525.7157, 3166.624, ...
    1538.458, 2164.326, 2644.200, ...
    3846.100, 5514.200, 4740.710, ...
    5449.728, 3703.500, 5452.300, ...
    1349.222, 1310.720, 1451.800, ...
    1252.400, 2074.000, 2203.000, ...
    1321.800, 923.8000, 1440.563];

% Define sampling frequency
Fs = [1252, 1252, 1252, ...
      1252, 1252, 1252, ...
      1252, 1252, 1252, ...
      1250, 1250, 1250, ...
      1250, 1250, 1250, ...
      1250, 1250, 1250, ...
      1250, 1250, 1250, ...
      1250, 1250, 1250, ...
      1250, 1250, 1250];

% Load and re-save LFP for all files
N_files = length(lfp_files);
lfp_time = zeros(N_files,1);
filesave_base = '/gh/data2/hc3/mat/';
mkdir(filesave_base)
for i = 1:N_files
    disp(i)
    
    % Determine rat and session for each file
    file_by_slash = strsplit(lfp_files{i},'/');
    rat_str5 = file_by_slash{5}(1:5);
    rat_by_unders = strsplit(rat_str5,'_');
    rat = rat_by_unders{1};
    sess = file_by_slash{6};
    disp(rat)
    disp(sess)
    
    % Make directories for that rat and session
    mkdir(strcat(filesave_base,'/',rat))
    mkdir(strcat(filesave_base,'/',rat,'/',sess))
    
    % For each file, load the xml to know each good channel and N channels
    lastdot =strfind(lfp_files{i},'.');
    FileBase=lfp_files{i}(1:lastdot(end)-1);
    Par = LoadPar([FileBase]);
    nChans = Par.nChannels;
    chans_by_shank = Par.ElecGp;
    N_shanks = length(chans_by_shank);
    
    % Load all eeg data
    eeg = LoadBinary(lfp_files{i},1:Par.nChannels);
    
    % Save eeg data separately for each electrode by shank
    for j = 1:N_shanks
        % Make directory for the shank
        mkdir(strcat(filesave_base,'/',rat,'/',sess,'/',num2str(j-1)))
        % Save LFP for each electrode on each shank
        chans_this_shank = chans_by_shank{j};
        Nchans_this_shank = length(chans_this_shank);
        for k = 1:Nchans_this_shank
            this_eeg = eeg(k,:);
            save_filename = strcat(filesave_base,'/',rat,'/',sess,'/',num2str(j-1),'/lfp',num2str(k-1),'.mat');
            save(save_filename, 'this_eeg','-v7.3')
        end
    end
    
    % Confirm computed time matches defined time
    disp(N_sec(i))
    disp(length(eeg)/Fs(i));
end

