1 |
cnh |
1.1 |
% rdslice(filename,[nx ny ...],k) returns an array of shape [nx,ny,...] |
2 |
|
|
% read from a direct access binary file (float or double precisision) named |
3 |
|
|
% by the string 'filename'. The file may contain multi-dimensional |
4 |
|
|
% data. Note that cumsum([nx ny ...]*k) <= length of file. |
5 |
|
|
% |
6 |
|
|
% eg. temp=rdslice('t.xyt',[160 120],4); |
7 |
|
|
% |
8 |
|
|
% rdsclice(filename,[nx ny ...],k,type) returns an array of type 'type'. |
9 |
|
|
% where type can be one of 'real*4' or 'real*8'. The default is 'real*8'. |
10 |
|
|
function [arr] = rdslice(file,N,k,varargin) |
11 |
|
|
|
12 |
|
|
% Default word-length |
13 |
|
|
WORDLENGTH=8; |
14 |
|
|
rtype='real*8'; |
15 |
|
|
ieee='b'; |
16 |
|
|
|
17 |
|
|
% Check optional arguments |
18 |
|
|
args=char(varargin); |
19 |
|
|
while (size(args,1) > 0) |
20 |
|
|
if deblank(args(1,:)) == 'real*4' |
21 |
|
|
WORDLENGTH=4; |
22 |
|
|
rtype='real*4'; |
23 |
|
|
elseif deblank(args(1,:)) == 'real*8' |
24 |
|
|
WORDLENGTH=8; |
25 |
|
|
rtype='real*8'; |
26 |
|
|
elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
27 |
|
|
ieee='n'; |
28 |
|
|
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
29 |
|
|
ieee='l'; |
30 |
|
|
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
31 |
|
|
ieee='b'; |
32 |
|
|
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
33 |
|
|
ieee='c'; |
34 |
|
|
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
35 |
|
|
ieee='a'; |
36 |
|
|
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
37 |
|
|
ieee='s'; |
38 |
|
|
else |
39 |
|
|
sprintf(['Optional argument ' args(1,:) ' is unknown']) |
40 |
|
|
return |
41 |
|
|
end |
42 |
|
|
args=args(2:end,:); |
43 |
|
|
end |
44 |
|
|
|
45 |
|
|
nnn=prod(N); |
46 |
|
|
|
47 |
|
|
[fid mess]=fopen(file,'r',ieee); |
48 |
|
|
if fid == -1 |
49 |
|
|
sprintf('Error while opening file:\n%s',mess) |
50 |
|
|
arr=0; |
51 |
|
|
return |
52 |
|
|
end |
53 |
|
|
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); |
54 |
|
|
if st ~= 0 |
55 |
|
|
mess=ferror(fid); |
56 |
|
|
sprintf('There was an error while positioning the file pointer:\n%s',mess) |
57 |
|
|
arr=0; |
58 |
|
|
return |
59 |
|
|
end |
60 |
|
|
[arr count]=fread(fid,nnn,rtype); |
61 |
|
|
if count ~= nnn |
62 |
|
|
sprintf('Not enough data was available to be read: off EOF?') |
63 |
|
|
arr=0; |
64 |
|
|
return |
65 |
|
|
end |
66 |
|
|
st=fclose(fid); |
67 |
|
|
arr=reshape(arr,N); |