Back to home page

MITgcm

 
 

    


Warning, /utils/matlab/cs_grid/read_cs/readbin.m is written in an unsupported language. File is not indexed.

view on githubraw file Latest commit 17dbea48 on 2014-09-22 01:26:49 UTC
b75ad9d212 Dimi*0001 function fld=readbin(fnam,siz,typ,prec,skip,mform)
                0002 
                0003 % Function fld=readbin(fnam,siz,typ,prec,skip,mform)
                0004 % read in N-D binary field
                0005 %
                0006 % INPUTS
                0007 % fnam  input path and file name
                0008 % siz   grid dimension (default [360 224 46])
                0009 % typ   0: sequential FORTRAN;  1: plain binary (the default)
                0010 % prec  numeric precision (see fread; default: 'real*4')
                0011 % skip  records to skip before reading (default 0)
                0012 % mform machine format (see fopen; default: 'ieee-be')
                0013 %
                0014 % OUTPUTS
                0015 % fld    output array of dimension siz
                0016 %
                0017 % SEE ALSO
602828dbcc Dimi*0018 % read_ijk read_ijkt writebin
b75ad9d212 Dimi*0019 
                0020 if nargin < 6, mform='ieee-be'; end
                0021 if nargin < 5, skip=0; end
                0022 if nargin < 4, prec='real*4'; end
                0023 if nargin < 3, typ=1; end
                0024 if nargin < 2, siz=[360 224 46]; end
                0025 if nargin < 1, error('please specify input file name'); end
                0026 
17dbea48d4 Dimi*0027 if ~exist(fnam)
                0028     error(['File ' fnam ' does not exist.'])
                0029 end
                0030 
b75ad9d212 Dimi*0031 fid=fopen(fnam,'r',mform);
                0032 
                0033 if skip>0
                0034   if typ==0
                0035     for n=1:skip
                0036       tmp=read_record(fid,prec);
                0037     end
                0038   else
                0039     switch prec
                0040       case {'int8','integer*1'}
                0041         reclength=prod(siz);
                0042       case {'int16','integer*2','uint16','integer*2'}
                0043         reclength=2*prod(siz);
                0044       case {'int32','integer*4','uint32','single','real*4','float32'}
                0045         reclength=4*prod(siz);
                0046       case {'int64','integer*8','uint64','double','real*8','float64'}
                0047         reclength=8*prod(siz);
                0048     end
                0049     if(fseek(fid,skip*reclength,'bof')<0), error('past end of file'); end
                0050   end
                0051 end
                0052 
                0053 switch typ
                0054   case 0
                0055     tmp=read_record(fid,prec);
                0056   case 1
                0057     tmp=fread(fid,[siz(1),prod(siz(2:length(siz)))],prec);
                0058 end
                0059 fid=fclose(fid);
                0060 
                0061 switch length(siz)
                0062   case 2
                0063     fld=reshape(tmp,siz(1),siz(2));
                0064   case 3
                0065     fld=reshape(tmp,siz(1),siz(2),siz(3));
                0066   case 4
                0067     fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4));
                0068   case 5
                0069     fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4),siz(5));
                0070   otherwise
                0071     fld=tmp;
                0072 end