function make_func_dice_compatible(func_file, rearrange)
% Replaces cluster numbers/labels in the given func file with that of the
% numbers/labels given in "rearrange"

%==========================================================================
% Load data
%==========================================================================
% data = readimgfile(func_file); % does not always work

mask = gifti(func_file);
data = mask.cdata; clear temp

%==========================================================================
% Check if input arguments are valid (issue error msg otherwise)
%==========================================================================

if length(unique(nonzeros(data))) ~= length(rearrange)
    error('Error: Number of clusters in the file and number of rearrange labels do not match.')
else
   % disp 'Valid data. Proceeding to relabel file.'
end

%==========================================================================
% Replace 1:k cluster numbers with "rearrange" in the gievn order. 
%==========================================================================
% Get mask file and replace data with cluster solutions

% original_ind = find(data);

%fprintf('Compiling results to make a func.gii file...\n');
 
for i_ind = 1:length(rearrange)
    replace_ind = find(data==i_ind);
    % replace data of indentified indices with their respective cluster number    
    mask.private.data{1}.data(replace_ind) = rearrange(i_ind); 
end

% Backproject solution to a func file
%fprintf('Backprojecting...\n');
saveimgfile(mask.cdata, ['rearranged_' func_file], 'L'); % for macaque

end