Package noaa.coastwatch.io
Class HDFWriter
java.lang.Object
noaa.coastwatch.io.EarthDataWriter
noaa.coastwatch.io.HDFWriter
- All Implemented Interfaces:
HDFSD
- Direct Known Subclasses:
CWHDFWriter
An HDF writer is an earth data writer that writes HDF format
files using the HDF library class. The HDF writer class is
abstract -- subclasses handle specific metadata variants.
- Since:
- 3.1.0
- Author:
- Peter Hollemans
-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
Flag to signify that the file is closed.static final int
Default HDF chunk size in bytes.protected int
HDF file id.Fields inherited from class noaa.coastwatch.io.EarthDataWriter
info, isCanceled, variables, writeProgress, writeVariableName, writeVariables
-
Constructor Summary
ModifierConstructorDescriptionprotected
Opens an existing HDF file for writing using the specified file name.protected
HDFWriter
(EarthDataInfo info, String file) Creates a new HDF file from the specified earth data info and file name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the writer and frees any resources.void
flush()
Flushes all unwritten data to the destination.boolean
Gets the HDF chunking flag.int
Gets the current chunk size in bytes.boolean
Gets the HDF compression flag.Gets the HDF scientific dataset file name.int
getSDID()
Gets the HDF scientific dataset ID.int[]
Gets the current tile dimensions.static int
Gets the HDF type associated with a Java primitive class.static void
setAttribute
(int sdid, String name, Object value) Sets an HDF attribute value.static void
setAttribute
(int sdid, String name, Object value, boolean isUnsigned) Sets an HDF attribute value.static void
setAttributes
(int sdid, Map map, boolean overwrite) Sets a number of attributes based on a map.static void
setChunkCompress
(int sdsid, boolean compressed, int[] chunk_lengths) Sets the chunking and compression for an SDS HDF variable.void
setChunked
(boolean chunked) Sets the HDF chunking flag.void
setChunkSize
(int size) Sets the chunk size.void
setCompressed
(boolean compressed) Sets the HDF compression flag.protected abstract void
Writes the earth data info metadata.void
setTileDims
(int[] tileDims) Sets the tile dimensions for grid variables passed to thewriteVariable(noaa.coastwatch.util.DataVariable, boolean, boolean)
method.protected abstract void
setVariableInfo
(int sdsid, DataVariable var) Writes the data variable metadata.protected void
writeVariable
(DataVariable var, boolean chunked, boolean compressed) Creates and writes an HDF variable.Methods inherited from class noaa.coastwatch.io.EarthDataWriter
addVariable, cancel, finalize, getDestination, getProgress, getProgressLength, getProgressVariable
-
Field Details
-
DEFAULT_CHUNK_SIZE
public static final int DEFAULT_CHUNK_SIZEDefault HDF chunk size in bytes.- See Also:
-
sdid
protected int sdidHDF file id. -
closed
protected boolean closedFlag to signify that the file is closed.
-
-
Constructor Details
-
HDFWriter
Opens an existing HDF file for writing using the specified file name. Chunking and compression are off by default. Note that a writer created in this way has noEarthDataInfo
instance variable.- Parameters:
file
- the HDF file name.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.
-
HDFWriter
Creates a new HDF file from the specified earth data info and file name. The HDF file is created but initial global metadata not written. Chunking and compression are off by default.- Parameters:
info
- the earth data info object.file
- the new HDF file name.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.- See Also:
-
-
Method Details
-
setTileDims
public void setTileDims(int[] tileDims) Sets the tile dimensions for grid variables passed to thewriteVariable(noaa.coastwatch.util.DataVariable, boolean, boolean)
method. Setting the tile dimensions explicitly overrides any chunk size set.- Parameters:
tileDims
- the tile dimensions to use.- Since:
- 3.5.0
-
getTileDims
public int[] getTileDims()Gets the current tile dimensions.- Returns:
- the tile dimesions or null if the chunk size is being used instead.
- Since:
- 3.5.0
- See Also:
-
getSDID
public int getSDID()Description copied from interface:HDFSD
Gets the HDF scientific dataset ID. -
getFilename
Description copied from interface:HDFSD
Gets the HDF scientific dataset file name.- Specified by:
getFilename
in interfaceHDFSD
-
setCompressed
public void setCompressed(boolean compressed) Sets the HDF compression flag. If compression is on, subsequent calls toflush
will cause variable data will be written compressed.- Parameters:
compressed
- the compression flag.- See Also:
-
setChunked
public void setChunked(boolean chunked) Sets the HDF chunking flag. If chunking is on, subsequent calls toflush
will cause variable data will be written in a chunked form.- Parameters:
chunked
- the chunking flag.- See Also:
-
getChunked
public boolean getChunked()Gets the HDF chunking flag. -
getCompressed
public boolean getCompressed()Gets the HDF compression flag. -
getType
Gets the HDF type associated with a Java primitive class.- Parameters:
c
- the Java primitive class.isUnsigned
- the unsigned flag, true if unsigned type desired.- Returns:
- the HDF data type.
- Throws:
ClassNotFoundException
- if a type cannot be found that matches the Java class.
-
setAttribute
public static void setAttribute(int sdid, String name, Object value) throws hdf.hdflib.HDFException, ClassNotFoundException Sets an HDF attribute value.- Parameters:
sdid
- the HDF scientific dataset ID.name
- the attribute name.value
- the attribute value. If the attribute value is a JavaString
, a character string is written. If the attribute is a Java primitive wrapped in an object, only one value is written. If the attribute value is a Java primitive array, multiple values are written.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.ClassNotFoundException
- if the HDF attribute type is unknown.
-
setAttribute
public static void setAttribute(int sdid, String name, Object value, boolean isUnsigned) throws hdf.hdflib.HDFException, ClassNotFoundException Sets an HDF attribute value.- Parameters:
sdid
- the HDF scientific dataset ID.name
- the attribute name.value
- the attribute value. If the attribute value is a JavaString
, a character string is written. If the attribute is a Java primitive wrapped in an object, only one value is written. If the attribute value is a Java primitive array, multiple values are written.isUnsigned
- the unsigned flag, true if the attribute is numeric and should be written as unsigned, false otherwise.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.ClassNotFoundException
- if the HDF attribute type is unknown.- Since:
- 3.6.1
-
setAttributes
public static void setAttributes(int sdid, Map map, boolean overwrite) throws hdf.hdflib.HDFException, ClassNotFoundException Sets a number of attributes based on a map.- Parameters:
sdid
- the HDF scientific dataset or variable ID for writing.map
- the attribute map.overwrite
- set to true to overwrite an attribute value that already exists in the file, or false to leave existing attribute values the same.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.ClassNotFoundException
- if the HDF attribute type is unknown.
-
setChunkSize
public void setChunkSize(int size) Sets the chunk size. Future data writes will be performed by writing chunks of the specified size.- Parameters:
size
- the chunk size in bytes.
-
getChunkSize
public int getChunkSize()Gets the current chunk size in bytes. -
setGlobalInfo
protected abstract void setGlobalInfo() throws hdf.hdflib.HDFException, IOException, ClassNotFoundException, UnsupportedEncodingExceptionWrites the earth data info metadata.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.IOException
- if an error occurred writing the file metadata.ClassNotFoundException
- if the HDF attribute type is unknown.UnsupportedEncodingException
- if the transform class encoding is not supported.
-
setVariableInfo
protected abstract void setVariableInfo(int sdsid, DataVariable var) throws hdf.hdflib.HDFException, IOException, ClassNotFoundException Writes the data variable metadata.- Parameters:
sdsid
- the variable HDF scientific dataset ID.var
- the variable for which to write info.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.IOException
- if an error occurred writing the file metadata.ClassNotFoundException
- if the HDF attribute type is unknown.
-
writeVariable
protected void writeVariable(DataVariable var, boolean chunked, boolean compressed) throws hdf.hdflib.HDFException, IOException, ClassNotFoundException Creates and writes an HDF variable.- Parameters:
var
- the variable for writing.chunked
- the chunking flag, true is chunking should be used.compressed
- the compression flag, true if compression should be used.- Throws:
hdf.hdflib.HDFException
- if an error occurred in an HDF routine.IOException
- if an error occurred writing the file metadata.ClassNotFoundException
- if the HDF attribute type is unknown.
-
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.
-
setChunkCompress
public static void setChunkCompress(int sdsid, boolean compressed, int[] chunk_lengths) throws hdf.hdflib.HDFException Sets the chunking and compression for an SDS HDF variable.- Parameters:
sdsid
- the HDF SDS variable ID.compressed
- the compression flag, true if compression should be used. Compression is set to GZIP at level 6.chunk_lengths
- the chunk lengths, one for each dimension. If null, no chunking if performed.- Throws:
hdf.hdflib.HDFException
- if an HDF error occurred.
-
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.
-