function [Data,Head] = nic_ReadAnalyze(imageIN,orientation,volumeIndex)
% Read MRI image file (imageIN) with format of Analyze 7.5. It will return data
% of 3D matrix (Data) and infomation of the header (Head). Please set the volume
%index (volumeIndex) of data if the image file contains 4D data. The structure 
% of header (Head) is the same with SPM5. The orientation (orientation) should
% be the real orientation as the data saved in the file.
%
% Usage: [Data,Head] = nic_ReadAnaylze(imageIN,orientation,volumeIndex)
% Input:
% 1. imageIN - the path and filename of image file: [path\*.img]
% 2. orientation - the orientation of data, the value can be 'Radiology' or 'Neurology'
%                  please make sure the true orientation of the saved data
% 2. volumeIndex - the volumeIndex of 4D data to read, can be 1,2,..., not larger than
%             the number of total volumes, default: 1
% Output:
% 1. Data - 3D matrix of data
% 2. Head - a structure containing image volume information
% The elements in the structure are:
%       Head.fname - the filename of the image.
%       Head.dim   - the x, y and z dimensions of the volume
%       Head.dt    - A 1x2 array.  First element is datatype (see spm_type).
%                 The second is 1 or 0 depending on the endian-ness.
%       Head.mat   - a 4x4 affine transformation matrix mapping from
%                 voxel coordinates to real world coordinates.
%       Head.pinfo - plane info for each plane of the volume.
%              Head.pinfo(1,:) - scale for each plane
%              Head.pinfo(2,:) - offset for each plane
%                 The true voxel intensities of the jth image are given
%                 by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
%              Head.pinfo(3,:) - offset into image (in bytes).
%                 If the size of pinfo is 3x1, then the volume is assumed
%                 to be contiguous and each plane has the same scalefactor
%                 and offset.
%        Head.private.hdr - a structure containing complete infomation in the
%        header.
%____________________________________________________________________________
% Copyright (C) 2007 Neuroimage Computing Group, State Key Laboratory of
% Cognitive Neuroscience and Learning
%
% Guiwen Chen, gwenchill@gmail.com
% @(#)nic_ReadAnalyze.m  ver 1.0, 07/11/22

if ~exist('volumeIndex', 'var')
    volumeIndex=1; % default
end

flip=0;
if ~exist('orientation', 'var')%check the oritention
    error('Alert: please input the orientation of data!');
else
  if(strcmpi(orientation,'Radiology') || strcmpi(orientation,'Neurology'))
    disp(sprintf('The oritention of data is %s',orientation));
  end
end

% get the SPM path
spmPath=nic_getSPMPath('analyze');
oldDir = pwd;

addpath(spmPath);

try
    cd(spmPath);    
    filename=imageIN;
    
    % construct the file name 
    [pathstr, fName, extn] = fileparts(deblank(filename));
    
    if isempty(pathstr)
       filename = strcat(oldDir, filesep, filename);
    end
 
    % Get info of header
    V = nic_spm_vol(filename);
    
    % Initialise data
    Data = zeros([V(1).dim(1:3), 1]);

    % vol can't be larger than the length of structure
    if(volumeIndex>length(V))
      error('The volume is error, please set the right volume to read');
    end
    
    % decide whether to flip the image using mat and oritention gave
    fmat=V(volumeIndex).mat(1,1);
    flip=0;
    switch (orientation)
        case 'Radiology' % left is right, right is left
             if(fmat>0)
	       flip=1;
	     end
        case 'Neurology' % left is left, right is right
             if(fmat<0)
	       flip=1;
	     end
    end
    if(flip==1)
      V(volumeIndex).mat=diag([-1 1 1 1])*V(volumeIndex).mat;% set the matrix of mat, flip?
    end

    % read volumes
    Data(:, :, :,1) = nic_spm_read_vols(V(volumeIndex));

    % replace NaN with zero
    Data(isnan(Data)) = 0;

    % save Header info in structure    
    Head=V(volumeIndex);

    clear V;
    
    rmpath(spmPath);
    cd(oldDir);
catch    
    rmpath(spmPath);
    cd(oldDir);
    error('Meet error while reading the data');
end
