| 1 | 
function writebin(fnam,fld,typ,prec,skip,mform) | 
| 2 | 
 | 
| 3 | 
% Function writebin(fnam,fld,typ,prec,skip,mform) | 
| 4 | 
% write N-D binary field | 
| 5 | 
% | 
| 6 | 
% INPUTS | 
| 7 | 
% fnam  input path and file name | 
| 8 | 
% fld   input array | 
| 9 | 
% typ   0: sequential FORTRAN;  1: plain binary (the default) | 
| 10 | 
% prec  numeric precision (default 'real*4') | 
| 11 | 
% skip  records to skip before writing (default 0) | 
| 12 | 
% mform machine format (see fopen; default: 'ieee-be') | 
| 13 | 
% | 
| 14 | 
% SEE ALSO | 
| 15 | 
% readbin | 
| 16 | 
 | 
| 17 | 
if nargin < 6, mform='ieee-be'; end | 
| 18 | 
if nargin < 5, skip=0; end | 
| 19 | 
if nargin < 4, prec='real*4'; end | 
| 20 | 
if nargin < 3, typ=1; end | 
| 21 | 
if skip>0 & typ==0, error('feature not implemented yet'); end | 
| 22 | 
if nargin < 2, error('please specify array and output file name'); end | 
| 23 | 
reclength=0; | 
| 24 | 
 | 
| 25 | 
if exist(fnam)==2 | 
| 26 | 
  fid=fopen(fnam,'r+',mform); | 
| 27 | 
else | 
| 28 | 
  fid=fopen(fnam,'w',mform); | 
| 29 | 
end | 
| 30 | 
 | 
| 31 | 
switch typ | 
| 32 | 
   | 
| 33 | 
  case 0 | 
| 34 | 
 | 
| 35 | 
    write_record(fid,fld,prec); | 
| 36 | 
   | 
| 37 | 
  case 1 | 
| 38 | 
 | 
| 39 | 
    if skip==0 | 
| 40 | 
       | 
| 41 | 
      fwrite(fid,fld,prec); | 
| 42 | 
     | 
| 43 | 
    else | 
| 44 | 
 | 
| 45 | 
      switch prec | 
| 46 | 
        case {'int8','integer*1'} | 
| 47 | 
          reclength=prod(size(fld)); | 
| 48 | 
        case {'int16','integer*2','uint16','integer*2'} | 
| 49 | 
          reclength=2*prod(size(fld)); | 
| 50 | 
        case {'int32','integer*4','uint32','single','real*4','float32'} | 
| 51 | 
          reclength=4*prod(size(fld)); | 
| 52 | 
        case {'int64','integer*8','uint64','double','real*8','float64'} | 
| 53 | 
          reclength=8*prod(size(fld)); | 
| 54 | 
      end | 
| 55 | 
       | 
| 56 | 
      if fseek(fid,skip*reclength,'bof') == 0 | 
| 57 | 
         | 
| 58 | 
        fwrite(fid,fld,prec); | 
| 59 | 
         | 
| 60 | 
      else | 
| 61 | 
         | 
| 62 | 
        fseek(fid,0,'eof'); | 
| 63 | 
        file_length=ftell(fid); | 
| 64 | 
        fwrite(fid,zeros((skip*reclength-file_length),1),'int8'); | 
| 65 | 
        fwrite(fid,fld,prec); | 
| 66 | 
         | 
| 67 | 
      end | 
| 68 | 
       | 
| 69 | 
    end | 
| 70 | 
     | 
| 71 | 
end | 
| 72 | 
 | 
| 73 | 
fid=fclose(fid); |