% flag for GPU (must have CUDA installed)
tic
useGPU = {};

% prepare for kilosort execution
addpath(genpath('{}'));
addpath(genpath('{}'));

% set file path
fpath = '{}';

% create channel map file
run(fullfile('{}'));

% Run the configuration file, it builds the structure of options (ops)
run(fullfile('{}'))

% This part runs the normal Kilosort processing on the simulated data
[rez, DATA, uproj] = preprocessData(ops); % preprocess data and extract spikes for initialization
rez                = fitTemplates(rez, DATA, uproj);  % fit templates iteratively
rez                = fullMPMU(rez, DATA);% extract final spike times (overlapping extraction)

try
    rez = merge_posthoc2(rez);
catch
    fprintf(2, 'merge_posthoc2 error. Reporting pre-merge result\n');
end

% save python results file for Phy
rezToPhy(rez, fullfile(fpath));

elapsed_time = toc;
fid = fopen(fullfile(fpath, 'time.txt'), 'w');
fprintf(fid, '%f', elapsed_time);
fclose(fid);


