CFile:WriteBswap


The WriteBswap method writes binary data to a file and reverses the byte order in the process. This method is useful for writing binary data to a file that uses a reversed byte orderi, such as FITS format, or for writing files that are to be read on another computer that uses a reversed byte order architecture. Also see the Write method for writing data with a normal byte order.

Syntax

nBytes = CFile:WriteBswap( value, sPixelType )

nBytes = CFile:WriteBswap( table, sPixelType* )

bullet.gif    value is a value to be written to the file.

bullet.gif    table is a 1-dimensional array. The array cannot contain nested tables and must have sequential indices through the number of elements in the array.

bullet.gif    sPixelType is a string that specifies the data format used to write the value or the table elements to the file. When writing a table, sPixelType is optional and defaults to "double" (i.e., 64-bit real values) if not specified.

bullet.gif    The return value nBytes is the number of bytes written to the file.

Remarks

This method can write single values or an entire data table to the CFile object. The format used in the output file is not necessarily the same as that of the value being written. For example, suppose value = 12. Then you can write this value as an 8-, 16-, or 32-bit integer or a 32- or 64 bit real number by changing the sPixelType string. See Pixel Types for a description of the available pixel types and the string to use for sPixelType. Note that character strings do not have byte ordering issues, so both Write and the current method give the same result when writing string data.

Example

The script below provides a complete function to save a 1-dimensional array to a raw binary file as 64-bit real numbers. The array has indices [1] through [ nCols x nRows ].

The nCols and nRows parameters are written individually as "long" integers (32-bit). Following that is a numeric Pixel Type code to indicate the table contains 64-bit real numbers. Then the entire table is saved. When using WriteBswap for a table, the output pixel type defaults to 64-bit real values if it is not specified. For the file to have the correct length, it is assumed that the number of table elements is nCols x nRows. The values inside the if-end bock are defined as "local" because they are use only inside the block.

 

function Write2dTable( t, nCols, nRows )

 

  if type(t) ~= "table" then return false end

-- failure: a table was not passed

  local sName = GetFileName()

-- Get a file name using the FileOpen dialog

  if sName ~= nil then

-- check that a file name was selected

    local F = FileOpen( sName, "wb" )

-- Open the file for writing binary data

    if F ~= nil then

-- if the file was opened

      F:WriteBswap( nCols, "long" )

-- number of columns as a long integer

      F:WriteBswap( nRows, "long" )

-- number of rows as a long integer

      F:WriteBswap( 6, "long" )

-- 6 = code for 64-bit real number data

      F:WriteBswap( t )

-- write the table as 64-bit real values.

      F:Close()

-- remember to close the file when done

      return true

-- success

    end

 

  end

 

  return false

-- failure

end

 

Related Topics

CFile Class, Write, ReadBswap, File Access Modes, GetFileName


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.