Class HDFWriter

All Implemented Interfaces:
HDFSD
Direct Known Subclasses:
CWHDFWriter

public abstract class HDFWriter extends EarthDataWriter implements HDFSD
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 Details

    • DEFAULT_CHUNK_SIZE

      public static final int DEFAULT_CHUNK_SIZE
      Default HDF chunk size in bytes.
      See Also:
    • sdid

      protected int sdid
      HDF file id.
    • closed

      protected boolean closed
      Flag to signify that the file is closed.
  • Constructor Details

    • HDFWriter

      protected HDFWriter(String file) throws hdf.hdflib.HDFException
      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 no EarthDataInfo instance variable.
      Parameters:
      file - the HDF file name.
      Throws:
      hdf.hdflib.HDFException - if an error occurred in an HDF routine.
    • HDFWriter

      protected HDFWriter(EarthDataInfo info, String file) throws hdf.hdflib.HDFException
      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 the writeVariable(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.
      Specified by:
      getSDID in interface HDFSD
    • getFilename

      public String getFilename()
      Description copied from interface: HDFSD
      Gets the HDF scientific dataset file name.
      Specified by:
      getFilename in interface HDFSD
    • setCompressed

      public void setCompressed(boolean compressed)
      Sets the HDF compression flag. If compression is on, subsequent calls to flush 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 to flush 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

      public static int getType(Class c, boolean isUnsigned) throws ClassNotFoundException
      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 Java String, 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 Java String, 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, UnsupportedEncodingException
      Writes 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

      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.
    • 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

      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.