function V=read_binary(file,dims,prec,machineformat,iRec,recSize);
% Generic function for reading binary files
% usage: V=read_binary(file,dims,prec,machineformat,iRec,recSize);
% Defaults: 
%          prec = 'float64'
%          machineformat='ieee-be'; 
%          if number of arguments < 5, entire file is read
%          otherwise, the function reads record iRec of length 
%          recSize (of data type given by prec).
if nargin<3
   prec='float64';
end
if nargin<4
   machineformat='ieee-be';
end
if isempty(prec)
  prec='float64';
end
if isempty(machineformat)
   machineformat='ieee-be';
end 

if nargin<5
  readSize=Inf;
  skipBytes=0;
else
  if ~isempty(findstr('64',prec))
    skipBytes=8*(iRec-1)*recSize;
  elseif ~isempty(findstr('32',prec)) 
    skipBytes=4*(iRec-1)*recSize;
  else    
    error('Unknown precision')
  end
  readSize=recSize
end
skipBytes
fid=fopen(file,'r',machineformat);
status=fseek(fid,skipBytes,0);
V=fread(fid,readSize,prec); 
fclose(fid);

if ~isempty(dims)
  V=reshape(V,dims);
end

