Back to home page

MITgcm

 
 

    


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

view on githubraw file Latest commit b75ad9d2 on 2007-02-06 22:07:28 UTC
b75ad9d212 Dimi*0001 function writebin(fnam,fld,typ,prec,skip,mform)
                0002 
                0003 % Function writebin(fnam,fld,typ,prec,skip,mform)
                0004 % write N-D binary field
                0005 %
                0006 % INPUTS
                0007 % fnam  input path and file name
                0008 % fld   input array
                0009 % typ   0: sequential FORTRAN;  1: plain binary (the default)
                0010 % prec  numeric precision (default 'real*4')
                0011 % skip  records to skip before writing (default 0)
                0012 % mform machine format (see fopen; default: 'ieee-be')
                0013 %
                0014 % SEE ALSO
                0015 % readbin
                0016 
                0017 if nargin < 6, mform='ieee-be'; end
                0018 if nargin < 5, skip=0; end
                0019 if nargin < 4, prec='real*4'; end
                0020 if nargin < 3, typ=1; end
                0021 if skip>0 & typ==0, error('feature not implemented yet'); end
                0022 if nargin < 2, error('please specify array and output file name'); end
                0023 reclength=0;
                0024 
                0025 if exist(fnam)==2
                0026   fid=fopen(fnam,'r+',mform);
                0027 else
                0028   fid=fopen(fnam,'w',mform);
                0029 end
                0030 
                0031 switch typ
                0032   
                0033   case 0
                0034 
                0035     write_record(fid,fld,prec);
                0036   
                0037   case 1
                0038 
                0039     if skip==0
                0040       
                0041       fwrite(fid,fld,prec);
                0042     
                0043     else
                0044 
                0045       switch prec
                0046         case {'int8','integer*1'}
                0047           reclength=prod(size(fld));
                0048         case {'int16','integer*2','uint16','integer*2'}
                0049           reclength=2*prod(size(fld));
                0050         case {'int32','integer*4','uint32','single','real*4','float32'}
                0051           reclength=4*prod(size(fld));
                0052         case {'int64','integer*8','uint64','double','real*8','float64'}
                0053           reclength=8*prod(size(fld));
                0054       end
                0055       
                0056       if fseek(fid,skip*reclength,'bof') == 0
                0057         
                0058         fwrite(fid,fld,prec);
                0059         
                0060       else
                0061         
                0062         fseek(fid,0,'eof');
                0063         file_length=ftell(fid);
                0064         fwrite(fid,zeros((skip*reclength-file_length),1),'int8');
                0065         fwrite(fid,fld,prec);
                0066         
                0067       end
                0068       
                0069     end
                0070     
                0071 end
                0072 
                0073 fid=fclose(fid);