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