function [data,hInfo]=nic_rsdd_loadData(nameList,maskInd)
%  load data from files
%  Inputs:
%	nameList - the names of files
%	maskInd - the mask
%  Outputs:
%	data - the data
%	hInfo - the info of head
%-----------------------------------------------------------
%   Copyright (C) 2009 Neuroimage Computing Group, State Key Laboratory of
%   Cognitive Neuroscience and Learning
%	Written by Gwen Chen
%	Version=1.0;
%	Release=2009718;
%  Last Revised by Guiwen Chen
%-----------------------------------------------------------
%

if ~exist('maskInd', 'var')
    maskInd = [];
end

fileNumber=size(nameList,1);

data=[];

[data, hInfo, XYZ] = nic_read_data(nameList, [], maskInd);



function [data, HInfo, XYZ] = nic_read_data(P, file_numbers, mask_ind)
%% Function to read the data using spm_vol and spm_read_vols functions.
%
% Inputs:
% 1. P - Character array of file names (Use full path)
% 2. file_numbers - File number/numbers to include
% 3. mask_ind - Mask indices
%
% Outputs:
% 1. data - 4D double array (x, y, z, t) and if mask is specified for fMRI 2D double array (Voxels, t) is
% returned.
% 2. HInfo - Header information
% 3. XYZ - Real world coords
%

%% Use defaults
if (~exist('file_numbers', 'var'))
    file_numbers = [];
end

if (~exist('mask_ind', 'var'))
    mask_ind = [];
end

%% Parse extension
first_file = deblank(P(1, :));
first_file = icatb_parseExtn(first_file);
[pp, fileN, extn] = fileparts(first_file);

%% USE SPM5 functions
spmPath = nic_getSPMPath('.nii');

oldDir = pwd;

addpath(spmPath);

try
    
    cd(spmPath);
    
    pathstr = fileparts(deblank(P(1, :)));
    
    if isempty(pathstr)
        P = strcat(oldDir, filesep, P);
    end
    
    if ~isempty(file_numbers)
        try
            %% Try getting the volume of the given file
            V = nic_spm_vol(deblank(P(file_numbers, :)));
        catch
            %% Get full volume (Handle this for the files that were selected
            % using the old version)
            V = nic_spm_vol(P);
            if (max(file_numbers) > length(V))
                error('Error:Timepoints', 'Maximum of file numbers (%d) specified exceeds the no. of time points (%d)\n', ...
                    max(file_numbers), length(V));
            end
            % Truncate the volume
            V = V(file_numbers);
        end
    else
        V = nic_spm_vol(P);
    end
    % end for checking the existence of file number variable
    
    if (~isempty(mask_ind))
        %% Use less memory if mask is specified
        data = zeros(length(mask_ind), length(V));
    else
        %% 4D double array
        data = zeros([V(1).dim(1:3), length(V)]);
    end
    
    %% Loop over time points
    for nVol = 1:length(V)
        
        %% Check dimensions
        if (nVol == 1)
            oldDim = V(nVol).dim(1:3);
        else
            newDim = V(nVol).dim(1:3);
            if (length(find((newDim == oldDim) == 1)) ~= 3)
                error('Error:DIM', 'Please check file %s as this doesn''t equal the dimension [%d, %d, %d]\n', V(nVol).fname, ...
                    newDim(1), newDim(2), newDim(3));
            end
        end
        
        if ((~isfield(V(1).private.hdr, 'pixdim')) || isempty(V(nVol).private.hdr.pixdim))
            V(nVol).private.hdr.pixdim = V(nVol).private.hdr.dime.pixdim;
        end
        
        %% Read data time point by time point and apply mask
        [tempData, XYZ] = nic_spm_read_vols(V(nVol));
        
        tempData(isnan(tempData)) = 0;
        
        if (~isempty(mask_ind))
            tempData = tempData(:);
            data(:, nVol) = tempData(mask_ind);
            XYZ = XYZ(:, mask_ind);
        else
            data(:, :, :, nVol) = tempData;
        end
        
        clear tempData;
        
    end
    %% End of loop over time points
    
    %% Get the voxel size
    VOX = double(V(1).private.hdr.pixdim(2:4)); VOX = abs(VOX);
    
    %% Save Header info in structure
    HInfo = struct('dim', (V(1).dim(1:3)), 'V', V, 'VOX', VOX);
    
    clear V;
    
    cd(oldDir);
    
catch
    
    cd(oldDir);
    error('Read image list meet errors!');
    
end
