function [data,header]=tfiReadAnalyze(filename,usespm_orig, short_nan);
% function [data,header]=tfiReadAnalyze(filename,usespm_orig, short_nan);

if (~exist('usespm_orig')) usespm_orig=0; end
if (~exist('short_nan')) short_nan=0; end
header=tfiReadAnalyzeHeader(filename,usespm_orig);

[pathstr,name,ext,versn] = fileparts( filename );
filename = fullfile( pathstr, [name '.img']);
fid=fopen(filename,'r',header.byteorder);

[data,c]=fread(fid,inf,header.datatype);
fclose(fid);

switch (header.datatype)
 case {'int16','short'}
  if ( short_nan ) 
    data(data<-32767)=NaN;
  else
    data(data<-32767)=0;
  end
  data=data*header.scale;
 otherwise
  % nothing
end

if (length(header.datasize)==3)
  data=reshape(data,header.datasize(1),header.datasize(2),header.datasize(3));
elseif (length(header.datasize)==4)
  data=reshape(data,header.datasize(1),header.datasize(2),header.datasize(3),header.datasize(4));
end
