Class BinaryWriter

java.lang.Object
noaa.coastwatch.io.EarthDataWriter
noaa.coastwatch.io.BinaryWriter
Direct Known Subclasses:
ByteWriter, FloatWriter, ShortWriter

public abstract class BinaryWriter extends EarthDataWriter
A binary writer is an earth data writer that writes variable data as a stream of binary values. The data may be scaled and/or byte swapped prior to writing. An optional dimension header may be prepended to each variable.
Since:
3.1.0
Author:
Mark Robinson
  • Field Details

  • Constructor Details

    • BinaryWriter

      public BinaryWriter(EarthDataInfo info, String file) throws IOException
      Creates a new binary file from the specified earth data info and file name. By default the byte order is HOST, scaling is unity, the missing value is set to the default, and the header flag is false.
      Parameters:
      info - the earth data info object.
      file - the new binary file name.
      Throws:
      IOException - if an error occurred opening the file.
  • Method Details

    • setHeader

      public void setHeader(boolean header)
      Sets the header flag.
      Parameters:
      header - the header flag. If true, a dimension header is written before any data.
    • setOrder

      public void setOrder(int order)
      Sets the byte order.
      Parameters:
      order - the byte order. The options are HOST for host byte order, MSB for most significant byte first and LSB for least significant byte first.
    • setRange

      public void setRange(double min, double max)
      Sets the scaling range. The range is used for data scaling to map data values to integers using the equation:
         integer = type_min + type_range*((value - min) / (max - min))
       
      where type_min and type_range are determined by the implementing class. Any data values outside the specified range are mapped to the missing value.
      Parameters:
      min - the minimum data value.
      max - the maximum data value.
    • setScaling

      public void setScaling(double[] scaling)
      Sets the scaling factor and offset. The scaling is used map data values to integers using the equation:
         integer = value/factor + offset
       
      Parameters:
      scaling - the scaling as [factor, offset]. If null, [1,0] is used.
    • setMissing

      public void setMissing(Number missing)
      Sets the missing value.
      Parameters:
      missing - the missing value. The missing value is used to represent missing or out of range data. If null, the missing value is set to the default.
    • byteSwap

      public static byte[] byteSwap(byte[] array)
      Performs a byte swap on a byte array.
    • getBytes

      public static byte[] getBytes(int value)
      Gets the bytes representing an integer in MSB order.
    • getBytes

      public static byte[] getBytes(short value)
      Gets the bytes representing a short integer in MSB order.
    • writeHeader

      protected void writeHeader(DataVariable var) throws IOException
      Writes a dimension header. The header consists of one byte specifying the number of dimensions followed by a series of 32-bit signed integers specifying the dimension lengths.
      Parameters:
      var - the data variable to write a header for.
      Throws:
      IOException - if an error occurred writing the header data to the file.
    • convertValue

      public abstract byte[] convertValue(Number value)
      Converts a data value to a byte array. The byte array has a length appropriate for the subclass. The value must be in the range getTypeMin() to getTypeMax().
      Parameters:
      value - the value for conversion.
      Returns:
      an array of bytes.
    • getTypeMin

      public abstract double getTypeMin()
      Gets the type minimum as a double.
    • getTypeMax

      public abstract double getTypeMax()
      Gets the type maximum as a double.
    • getTypeRange

      public abstract double getTypeRange()
      Gets the type range as a double.
    • getDefaultMissing

      public abstract Number getDefaultMissing()
      Gets the default missing value.
    • writeVariable

      protected void writeVariable(DataVariable var) throws IOException
      Writes the variable data as a stream of binary data values.
      Parameters:
      var - the data variable to write data for.
      Throws:
      IOException - if an error occurred writing the data to the file.
    • flush

      public void flush() throws IOException
      Description copied from class: EarthDataWriter
      Flushes all unwritten data to the destination.
      Specified by:
      flush in class EarthDataWriter
      Throws:
      IOException - if the data destination had I/O errors.
    • close

      public void close() throws IOException
      Description copied from class: EarthDataWriter
      Closes the writer and frees any resources. The flush method is called prior to closing.
      Specified by:
      close in class EarthDataWriter
      Throws:
      IOException - if the data destination had I/O errors.