Package noaa.coastwatch.io
Class BinaryWriter
java.lang.Object
noaa.coastwatch.io.EarthDataWriter
noaa.coastwatch.io.BinaryWriter
- Direct Known Subclasses:
ByteWriter
,FloatWriter
,ShortWriter
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 Summary
Modifier and TypeFieldDescriptionstatic final int
Output buffer size in kilobytes.static final int
Host byte order.static final int
Least significant byte first.static final int
Most significant byte first.Fields inherited from class noaa.coastwatch.io.EarthDataWriter
info, isCanceled, variables, writeProgress, writeVariableName, writeVariables
-
Constructor Summary
ConstructorDescriptionBinaryWriter
(EarthDataInfo info, String file) Creates a new binary file from the specified earth data info and file name. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
byteSwap
(byte[] array) Performs a byte swap on a byte array.void
close()
Closes the writer and frees any resources.abstract byte[]
convertValue
(Number value) Converts a data value to a byte array.void
flush()
Flushes all unwritten data to the destination.static byte[]
getBytes
(int value) Gets the bytes representing an integer in MSB order.static byte[]
getBytes
(short value) Gets the bytes representing a short integer in MSB order.abstract Number
Gets the default missing value.abstract double
Gets the type maximum as a double.abstract double
Gets the type minimum as a double.abstract double
Gets the type range as a double.void
setHeader
(boolean header) Sets the header flag.void
setMissing
(Number missing) Sets the missing value.void
setOrder
(int order) Sets the byte order.void
setRange
(double min, double max) Sets the scaling range.void
setScaling
(double[] scaling) Sets the scaling factor and offset.protected void
writeHeader
(DataVariable var) Writes a dimension header.protected void
Writes the variable data as a stream of binary data values.Methods inherited from class noaa.coastwatch.io.EarthDataWriter
addVariable, cancel, finalize, getDestination, getProgress, getProgressLength, getProgressVariable
-
Field Details
-
HOST
public static final int HOSTHost byte order.- See Also:
-
MSB
public static final int MSBMost significant byte first.- See Also:
-
LSB
public static final int LSBLeast significant byte first.- See Also:
-
DEFAULT_CHUNK_SIZE
public static final int DEFAULT_CHUNK_SIZEOutput buffer size in kilobytes.- See Also:
-
-
Constructor Details
-
BinaryWriter
Creates a new binary file from the specified earth data info and file name. By default the byte order isHOST
, 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 areHOST
for host byte order,MSB
for most significant byte first andLSB
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))
wheretype_min
andtype_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
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
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
Converts a data value to a byte array. The byte array has a length appropriate for the subclass. The value must be in the rangegetTypeMin()
togetTypeMax()
.- 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
Gets the default missing value. -
writeVariable
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
Description copied from class:EarthDataWriter
Flushes all unwritten data to the destination.- Specified by:
flush
in classEarthDataWriter
- Throws:
IOException
- if the data destination had I/O errors.
-
close
Description copied from class:EarthDataWriter
Closes the writer and frees any resources. Theflush
method is called prior to closing.- Specified by:
close
in classEarthDataWriter
- Throws:
IOException
- if the data destination had I/O errors.
-