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