1 |
|
function fld=readbin(fnam,siz,typ,prec,skip) |
2 |
|
|
3 |
|
% Function fld=readbin(fnam,siz,typ,prec,skip) |
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 (default); 1: plain binary |
10 |
|
% prec numeric precision (default 'real*4') |
11 |
|
% skip records to skip before reading (default 0) |
12 |
|
% |
13 |
|
% OUTPUTS |
14 |
|
% fld output array of dimension siz |
15 |
|
% |
16 |
|
% SEE ALSO |
17 |
|
% writebin |
18 |
|
|
19 |
|
if nargin < 5, skip=0; end |
20 |
|
if nargin < 4, prec='real*4'; end |
21 |
|
if nargin < 3, typ=0; end |
22 |
|
if nargin < 2, siz=[360 224 46]; end |
23 |
|
if nargin < 1, t=1; end |
24 |
|
if nargin < 0, error('please specify input file name'); end |
25 |
|
|
26 |
|
fid=fopen(fnam,'r','ieee-be'); |
27 |
|
|
28 |
|
if skip>0 |
29 |
|
if typ==0 |
30 |
|
error('feature not implemented yet'); |
31 |
|
else |
32 |
|
switch prec |
33 |
|
case {'int8','integer*1'} |
34 |
|
reclength=prod(siz); |
35 |
|
case {'int16','integer*2','uint16','integer*2'} |
36 |
|
reclength=2*prod(siz); |
37 |
|
case {'int32','integer*4','uint32','single','real*4','float32'} |
38 |
|
reclength=4*prod(siz); |
39 |
|
case {'int64','integer*8','uint64','double','real*8','float64'} |
40 |
|
reclength=8*prod(siz); |
41 |
|
end |
42 |
|
end |
43 |
|
fseek(fid,skip*reclength,'bof'); |
44 |
|
end |
45 |
|
|
46 |
|
switch typ |
47 |
|
case 0 |
48 |
|
tmp=read_record(fid,prec); |
49 |
|
case 1 |
50 |
|
tmp=fread(fid,[siz(1),prod(siz(2:length(siz)))],prec); |
51 |
|
end |
52 |
|
fid=fclose(fid); |
53 |
|
|
54 |
|
switch length(siz) |
55 |
|
case 2 |
56 |
|
fld=reshape(tmp,siz(1),siz(2)); |
57 |
|
case 3 |
58 |
|
fld=reshape(tmp,siz(1),siz(2),siz(3)); |
59 |
|
case 4 |
60 |
|
fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4)); |
61 |
|
case 5 |
62 |
|
fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4),siz(5)); |
63 |
|
otherwise |
64 |
|
fld=tmp; |
65 |
|
end |