[18535] | 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 | fid=fopen(fnam,'r',mform);
|
---|
| 28 |
|
---|
| 29 | if skip>0
|
---|
| 30 | if typ==0
|
---|
| 31 | for n=1:skip
|
---|
| 32 | tmp=read_record(fid,prec);
|
---|
| 33 | end
|
---|
| 34 | else
|
---|
| 35 | switch prec
|
---|
| 36 | case {'int8','integer*1'}
|
---|
| 37 | reclength=prod(siz);
|
---|
| 38 | case {'int16','integer*2','uint16','integer*2'}
|
---|
| 39 | reclength=2*prod(siz);
|
---|
| 40 | case {'int32','integer*4','uint32','single','real*4','float32'}
|
---|
| 41 | reclength=4*prod(siz);
|
---|
| 42 | case {'int64','integer*8','uint64','double','real*8','float64'}
|
---|
| 43 | reclength=8*prod(siz);
|
---|
| 44 | end
|
---|
| 45 | if(fseek(fid,skip*reclength,'bof')<0), error('past end of file'); end
|
---|
| 46 | end
|
---|
| 47 | end
|
---|
| 48 |
|
---|
| 49 | switch typ
|
---|
| 50 | case 0
|
---|
| 51 | tmp=read_record(fid,prec);
|
---|
| 52 | case 1
|
---|
| 53 | tmp=fread(fid,[siz(1),prod(siz(2:length(siz)))],prec);
|
---|
| 54 | end
|
---|
| 55 | fid=fclose(fid);
|
---|
| 56 |
|
---|
| 57 | switch length(siz)
|
---|
| 58 | case 2
|
---|
| 59 | fld=reshape(tmp,siz(1),siz(2));
|
---|
| 60 | case 3
|
---|
| 61 | fld=reshape(tmp,siz(1),siz(2),siz(3));
|
---|
| 62 | case 4
|
---|
| 63 | fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4));
|
---|
| 64 | case 5
|
---|
| 65 | fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4),siz(5));
|
---|
| 66 | otherwise
|
---|
| 67 | fld=tmp;
|
---|
| 68 | end
|
---|