| 1 |
% result = RDDA( file, dim, irec [options] ) |
| 2 |
% |
| 3 |
% This routine reads the irec'th record of shape 'dim' from the |
| 4 |
% direct-access binary file (float or double precision) 'file'. |
| 5 |
% |
| 6 |
% Required arguments: |
| 7 |
% |
| 8 |
% file - string - name of file to read from |
| 9 |
% dim - vector - dimensions of the file records and the resulting array |
| 10 |
% irec - integer - record number in file in which to write data |
| 11 |
% |
| 12 |
% Optional arguments (must appear after the required arguments): |
| 13 |
% prec - string - precision of storage in file. Default = 'real*8'. |
| 14 |
% ieee - string - IEEE bit-wise representation in file. Default = 'b'. |
| 15 |
% |
| 16 |
% 'prec' may take the values: |
| 17 |
% 'real*4' - floating point, 32 bits. |
| 18 |
% 'real*8' - floating point, 64 bits - the efault. |
| 19 |
% |
| 20 |
% 'ieee' may take values: |
| 21 |
% 'ieee-be' or 'b' - IEEE floating point with big-endian |
| 22 |
% byte ordering - the default |
| 23 |
% 'ieee-le' or 'l' - IEEE floating point with little-endian |
| 24 |
% byte ordering |
| 25 |
% 'native' or 'n' - local machine format |
| 26 |
% 'cray' or 'c' - Cray floating point with big-endian |
| 27 |
% byte ordering |
| 28 |
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian |
| 29 |
% byte ordering and 64 bit long data type |
| 30 |
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte |
| 31 |
% ordering and 64 bit long data type. |
| 32 |
% |
| 33 |
% eg. T=rdda('t.data',[64 64 32],1); |
| 34 |
% T=rdda('t.data',[256],4,'real*4'); |
| 35 |
% T=rdda('t.data',[128 64],2,'real*4','b'); |
| 36 |
function [arr] = rdda(file,N,k,varargin) |
| 37 |
|
| 38 |
% Defaults |
| 39 |
WORDLENGTH=8; |
| 40 |
rtype='real*8'; |
| 41 |
ieee='b'; |
| 42 |
|
| 43 |
% Check optional arguments |
| 44 |
args=char(varargin); |
| 45 |
while (size(args,1) > 0) |
| 46 |
if deblank(args(1,:)) == 'real*4' |
| 47 |
WORDLENGTH=4; |
| 48 |
rtype='real*4'; |
| 49 |
elseif deblank(args(1,:)) == 'real*8' |
| 50 |
WORDLENGTH=8; |
| 51 |
rtype='real*8'; |
| 52 |
elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
| 53 |
ieee='n'; |
| 54 |
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
| 55 |
ieee='l'; |
| 56 |
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
| 57 |
ieee='b'; |
| 58 |
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
| 59 |
ieee='c'; |
| 60 |
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
| 61 |
ieee='a'; |
| 62 |
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
| 63 |
ieee='s'; |
| 64 |
else |
| 65 |
sprintf(['Optional argument ' args(1,:) ' is unknown']) |
| 66 |
return |
| 67 |
end |
| 68 |
args=args(2:end,:); |
| 69 |
end |
| 70 |
|
| 71 |
nnn=prod(N); |
| 72 |
|
| 73 |
[fid mess]=fopen(file,'r',ieee); |
| 74 |
if fid == -1 |
| 75 |
sprintf('Error while opening file:\n%s',mess); % Shouldn't have ; |
| 76 |
arr=0; |
| 77 |
return |
| 78 |
end |
| 79 |
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); |
| 80 |
if st ~= 0 |
| 81 |
mess=ferror(fid); |
| 82 |
sprintf('There was an error while positioning the file pointer:\n%s',mess) |
| 83 |
arr=0; |
| 84 |
return |
| 85 |
end |
| 86 |
[arr count]=fread(fid,nnn,rtype); |
| 87 |
if count ~= nnn |
| 88 |
sprintf('Not enough data was available to be read: off EOF?') |
| 89 |
arr=0; |
| 90 |
return |
| 91 |
end |
| 92 |
st=fclose(fid); |
| 93 |
arr=reshape(arr,N); |