1 |
function fld=readbin(fnam,siz,typ,prec,skip,mform) |
2 |
|
3 |
% Function fld=readbin(fnam,siz,typ,prec,skip,mform) |
4 |
% read in N-D binary field |
5 |
% |
6 |
% INPUTS |
7 |
% fnam input path and file name |
8 |
% siz grid dimension (default [360 224 46]) |
9 |
% typ 0: sequential FORTRAN; 1: plain binary (the default) |
10 |
% prec numeric precision (see fread; default: 'real*4') |
11 |
% skip records to skip before reading (default 0) |
12 |
% mform machine format (see fopen; default: 'ieee-be') |
13 |
% |
14 |
% OUTPUTS |
15 |
% fld output array of dimension siz |
16 |
% |
17 |
% SEE ALSO |
18 |
% read_ijk read_ijkt writebin |
19 |
|
20 |
if nargin < 6, mform='ieee-be'; end |
21 |
if nargin < 5, skip=0; end |
22 |
if nargin < 4, prec='real*4'; end |
23 |
if nargin < 3, typ=1; end |
24 |
if nargin < 2, siz=[360 224 46]; end |
25 |
if nargin < 1, error('please specify input file name'); end |
26 |
|
27 |
if ~exist(fnam) |
28 |
error(['File ' fnam ' does not exist.']) |
29 |
end |
30 |
|
31 |
fid=fopen(fnam,'r',mform); |
32 |
|
33 |
if skip>0 |
34 |
if typ==0 |
35 |
for n=1:skip |
36 |
tmp=read_record(fid,prec); |
37 |
end |
38 |
else |
39 |
switch prec |
40 |
case {'int8','integer*1'} |
41 |
reclength=prod(siz); |
42 |
case {'int16','integer*2','uint16','integer*2'} |
43 |
reclength=2*prod(siz); |
44 |
case {'int32','integer*4','uint32','single','real*4','float32'} |
45 |
reclength=4*prod(siz); |
46 |
case {'int64','integer*8','uint64','double','real*8','float64'} |
47 |
reclength=8*prod(siz); |
48 |
end |
49 |
if(fseek(fid,skip*reclength,'bof')<0), error('past end of file'); end |
50 |
end |
51 |
end |
52 |
|
53 |
switch typ |
54 |
case 0 |
55 |
tmp=read_record(fid,prec); |
56 |
case 1 |
57 |
tmp=fread(fid,[siz(1),prod(siz(2:length(siz)))],prec); |
58 |
end |
59 |
fid=fclose(fid); |
60 |
|
61 |
switch length(siz) |
62 |
case 2 |
63 |
fld=reshape(tmp,siz(1),siz(2)); |
64 |
case 3 |
65 |
fld=reshape(tmp,siz(1),siz(2),siz(3)); |
66 |
case 4 |
67 |
fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4)); |
68 |
case 5 |
69 |
fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4),siz(5)); |
70 |
otherwise |
71 |
fld=tmp; |
72 |
end |