yorick banner

Home

Manual

Packages

Global Index

Keywords

Quick Reference



func df3_read (fname,big_endian=)
/* DOCUMENT    df3_read(fname)  return a df3 structure
    q= df3_read("./snapshot_000")
    modified to be df3 1.1 compatible
*/      
{
  /* Open file. */
  if (is_void(fname)) error," df3_read needs an argument";
  file= open(fname, "rb");
 if (!is_void(big_endian)) sun_primitives,file; 
  /* Read the header. */
  address= 0;
  ns=         _df3_read(file,address,short,3);
  nx=ns(1);   ny=ns(2);  nz=ns(3);
  write,format="will read a %d x %d * %d array\n",nx,ny,nz;
  /* Read the grid */
  dat=         _df3_read(file,address,char,[3,nx,ny,nz]);
  return dat;
}





func _df3_read (file, &address, what, ..)
/* DOCUMENT _df3_read(file, address, expr)
       -or- _df3_read, file, address, variable;

     Read unformated, binary data from FILE at offset ADDRESS (must be a
     scalar long integer).  On return, ADDRESS is incremented by the size of
     the data read.  The third argument gives the data type and dimension list
     of the array to read.  If called as a function, the argument EXPR may
     either be an expression or a variable which is filled with the data read
     and is returned as the result of the call.  If called as a subroutine,
     the third argument should be a predefined variable.

  SEE ALSO df3_read,_df3_write, open, _read. */
{
  if (! is_array(what)) {
    if (typeof(what)=="struct_definition") {
      type= what;
      dimslist= [];
      nargs= 0;
      while (more_args()) {
        ++nargs;
        grow, dimslist, next_arg();
      }
      what= [];
      if (nargs==0) {
        what= array(type);
      } else if (is_array(dimslist)
                 && (structof(dimslist)==long || structof(dimslist)==int)
                 && noneof(dimslist <= 0)) {
        if (nargs==1 && dimslist(1)==numberof(dimslist)-1) {
          what= array(type, dimslist);
        } else {
          dims= dimslist;
          ndims= numberof(dims);
          dimslist= array(long, ndims+1);
          dimslist(1)= ndims;
          dimslist(2:)= dims;
          what= array(type, dimslist);
        }
      }
    }
    if (! is_array(what)) error, "bad array specification";
  }
  if (_read(file, address, what) != numberof(what)) error, "short file";
  address+= sizeof(what);
  return what;
}




func df3_write (s,fname)
 /* DOCUMENT
        writes a df3 structure into the file
        er= df3_write(s,"test.dat");
     */      
{
  /* Open file. */
  file= open(fname, "wb");
  /* Writes the header. */
  address= 0;
  ns=dimsof(s);
  nx=short(ns(2));  ny=short(ns(3));  nz=short(ns(4));
  er=_df3_write(file,[nx,ny,nz],&address);
  er=_df3_write(file,char(255*(s-min(s))/(max(s)-min(s))),&address);
  return ns;
}



func _df3_write (file,what,&address)
/* DOCUMENT _df3_write(file, address, expr)

     Write unformated, binary data from FILE.  
     On return, ADDRESS is incremented by the size of
     the data written.  The third argument gives the data to be written.
     
  SEE ALSO df3_write,_df3_read, open, _read. */
{
er=  _write(file,*address,what);
*address+=sizeof(what);
return er;
}