Package hdf.object

Class Attribute

Object
All Implemented Interfaces:
CompoundDataFormat, DataFormat, MetaDataContainer, Serializable
Direct Known Subclasses:
H4Attribute, H5Attribute, NC2Attribute

public class Attribute
extends Dataset
implements DataFormat, CompoundDataFormat
An attribute is a (name, value) pair of metadata attached to a primary data object such as a dataset, group or named datatype.

Like a dataset, an attribute has a name, datatype and dataspace.

For more details on attributes, HDF5 User's Guide

The following code is an example of an attribute with 1D integer array of two elements.

 // Example of creating a new attribute
 // The name of the new attribute
 String name = "Data range";
 // Creating an unsigned 1-byte integer datatype
 Datatype type = new Datatype(Datatype.CLASS_INTEGER, // class
                              1,                      // size in bytes
                              Datatype.ORDER_LE,      // byte order
                              Datatype.SIGN_NONE);    // unsigned
 // 1-D array of size two
 long[] dims = {2};
 // The value of the attribute
 int[] value = {0, 255};
 // Create a new attribute
 Attribute dataRange = new Attribute(name, type, dims);
 // Set the attribute value
 dataRange.setValue(value);
 // See FileFormat.writeAttribute() for how to attach an attribute to an object,
 @see hdf.object.FileFormat#writeAttribute(HObject, Attribute, boolean)
 
For an atomic datatype, the value of an Attribute will be a 1D array of integers, floats and strings. For a compound datatype, it will be a 1D array of strings with field members separated by a comma. For example, "{0, 10.5}, {255, 20.0}, {512, 30.0}" is a compound attribute of {int, float} of three data points.
Version:
2.0 4/2/2018
Author:
Peter X. Cao, Jordan T. Henderson
See Also:
Datatype, Serialized Form
  • Field Details

    • parentObject

      protected HObject parentObject
      The HObject to which this Attribute is attached
    • unsignedConverted

      protected boolean unsignedConverted
      Flag to indicate is the original unsigned C data is converted.
    • isScalar

      protected final boolean isScalar
      Flag to indicate if the attribute data is a single scalar point
    • flatNameList

      protected List<String> flatNameList
      A list of names of all compound fields including nested fields.

      The nested names are separated by CompoundDS.SEPARATOR. For example, if compound attribute "A" has the following nested structure,

       A --> m01
       A --> m02
       A --> nest1 --> m11
       A --> nest1 --> m12
       A --> nest1 --> nest2 --> m21
       A --> nest1 --> nest2 --> m22
       i.e.
       A = { m01, m02, nest1{m11, m12, nest2{ m21, m22}}}
       
      The flatNameList of compound attribute "A" will be {m01, m02, nest1[m11, nest1[m12, nest1[nest2[m21, nest1[nest2[m22}
    • flatTypeList

      protected List<Datatype> flatTypeList
      A list of datatypes of all compound fields including nested fields.
    • numberOfMembers

      protected int numberOfMembers
      The number of members of the compound attribute.
    • memberNames

      protected String[] memberNames
      The names of the members of the compound attribute.
    • memberOrders

      protected int[] memberOrders
      Array containing the total number of elements of the members of this compound attribute.

      For example, a compound attribute COMP has members of A, B and C as

           COMP {
               int A;
               float B[5];
               double C[2][3];
           }
       
      memberOrders is an integer array of {1, 5, 6} to indicate that member A has one element, member B has 5 elements, and member C has 6 elements.
    • memberDims

      protected transient Object[] memberDims
      The dimension sizes of each member.

      The i-th element of the Object[] is an integer array (int[]) that contains the dimension sizes of the i-th member.

    • memberTypes

      protected Datatype[] memberTypes
      The datatypes of the compound attribute's members.
    • isMemberSelected

      protected boolean[] isMemberSelected
      The array to store flags to indicate if a member of this compound attribute is selected for read/write.

      If a member is selected, the read/write will perform on the member. Applications such as HDFView will only display the selected members of the compound attribute.

       For example, if a compound attribute has four members
           String[] memberNames = {"X", "Y", "Z", "TIME"};
       and
           boolean[] isMemberSelected = {true, false, false, true};
       members "X" and "TIME" are selected for read and write.
       
  • Constructor Details

    • Attribute

      public Attribute​(HObject parentObj, String attrName, Datatype attrType, long[] attrDims)
      Create an attribute with specified name, data type and dimension sizes. For scalar attribute, the dimension size can be either an array of size one or null, and the rank can be either 1 or zero. Attribute is a general class and is independent of file format, e.g., the implementation of attribute applies to both HDF4 and HDF5.

      The following example creates a string attribute with the name "CLASS" and value "IMAGE".

       long[] attrDims = { 1 };
       String attrName = "CLASS";
       String[] classValue = { "IMAGE" };
       Datatype attrType = null;
       try {
           attrType = new H5Datatype(Datatype.CLASS_STRING, classValue[0].length() + 1, Datatype.NATIVE, Datatype.NATIVE);
       }
       catch (Exception ex) {}
       Attribute attr = new Attribute(attrName, attrType, attrDims);
       attr.setValue(classValue);
       
      Parameters:
      parentObj - the HObject to which this Attribute is attached.
      attrName - the name of the attribute.
      attrType - the datatype of the attribute.
      attrDims - the dimension sizes of the attribute, null for scalar attribute
      See Also:
      Datatype
    • Attribute

      public Attribute​(HObject parentObj, String attrName, Datatype attrType, long[] attrDims, Object attrValue)
      Create an attribute with specific name and value. For scalar attribute, the dimension size can be either an array of size one or null, and the rank can be either 1 or zero. Attribute is a general class and is independent of file format, e.g., the implementation of attribute applies to both HDF4 and HDF5.

      The following example creates a string attribute with the name "CLASS" and value "IMAGE".

       long[] attrDims = { 1 };
       String attrName = "CLASS";
       String[] classValue = { "IMAGE" };
       Datatype attrType = null;
       try {
           attrType = new H5Datatype(Datatype.CLASS_STRING, classValue[0].length() + 1, Datatype.NATIVE, Datatype.NATIVE);
       }
       catch (Exception ex) {}
       Attribute attr = new Attribute(attrName, attrType, attrDims, classValue);
       
      Parameters:
      parentObj - the HObject to which this Attribute is attached.
      attrName - the name of the attribute.
      attrType - the datatype of the attribute.
      attrDims - the dimension sizes of the attribute, null for scalar attribute
      attrValue - the value of the attribute, null if no value
      See Also:
      Datatype
  • Method Details

    • open

      public long open()
      Description copied from class: HObject
      Opens an existing object such as a dataset or group for access. The return value is an object identifier obtained by implementing classes such as H5.H5Dopen(). This function is needed to allow other objects to be able to access the object. For instance, H5File class uses the open() function to obtain object identifier for copyAttributes(long src_id, long dst_id) and other purposes. The open() function should be used in pair with close(long) function.
      Specified by:
      open in class HObject
      Returns:
      the object identifier if successful; otherwise returns a negative value.
      See Also:
      HObject.close(long)
    • close

      public void close​(long aid)
      Description copied from class: HObject
      Closes access to the object.

      Sub-classes must implement this interface because different data objects have their own ways of how the data resources are closed.

      For example, H5Group.close() calls the hdf.hdf5lib.H5.H5Gclose() method and closes the group resource specified by the group id.

      Specified by:
      close in class HObject
      Parameters:
      aid - The object identifier.
    • init

      public void init()
      Specified by:
      init in interface DataFormat
    • getParentObject

      Returns the HObject to which this Attribute is currently "attached".
      Returns:
      the HObject to which this Attribute is currently "attached".
    • setParentObject

      public void setParentObject​(HObject pObj)
      Sets the HObject to which this Attribute is "attached".
      Parameters:
      pObj - the new HObject to which this Attribute is "attached".
    • convertFromUnsignedC

      public Object convertFromUnsignedC()
      Converts the data values of this Attribute to appropriate Java integers if they are unsigned integers.
      Specified by:
      convertFromUnsignedC in interface DataFormat
      Returns:
      the converted data buffer.
      See Also:
      Dataset.convertToUnsignedC(Object), Dataset.convertFromUnsignedC(Object, Object)
    • convertToUnsignedC

      public Object convertToUnsignedC()
      Converts Java integer data values of this Attribute back to unsigned C-type integer data if they are unsigned integers.
      Specified by:
      convertToUnsignedC in interface DataFormat
      Returns:
      the converted data buffer.
      See Also:
      Dataset.convertToUnsignedC(Object), Dataset.convertToUnsignedC(Object, Object), Dataset.convertFromUnsignedC(Object data_in)
    • getFillValue

      public Object getFillValue()
      Description copied from interface: DataFormat
      Returns the fill values for the data object.
      Specified by:
      getFillValue in interface DataFormat
      Returns:
      the fill values for the data object.
    • clearData

      public void clearData()
      Description copied from class: Dataset
      Clears the current data buffer in memory and forces the next read() to load the data from file.

      The function read() loads data from file into memory only if the data is not read. If data is already in memory, read() just returns the memory buffer. Sometimes we want to force read() to re-read data from file. For example, when the selection is changed, we need to re-read the data.

      Specified by:
      clearData in interface DataFormat
      Overrides:
      clearData in class Dataset
      See Also:
      Dataset.getData(), DataFormat.read()
    • resetSelection

      protected void resetSelection()
    • setProperty

      public void setProperty​(String key, Object value)
      set a property for the attribute.
      Parameters:
      key - the attribute Map key
      value - the attribute Map value
    • getProperty

      public Object getProperty​(String key)
      get a property for a given key.
      Parameters:
      key - the attribute Map key
      Returns:
      the property
    • getPropertyKeys

      public Collection<String> getPropertyKeys()
      get all property keys.
      Returns:
      the Collection of property keys
    • isScalar

      public boolean isScalar()
      Returns:
      true if the data is a single scalar point; otherwise, returns false.
    • read

      public Object read() throws Exception, OutOfMemoryError
      Description copied from interface: DataFormat
      Reads the data from file.

      read() reads the data from file to a memory buffer and returns the memory buffer. The dataset object does not hold the memory buffer. To store the memory buffer in the dataset object, one must call getData().

      By default, the whole dataset is read into memory. Users can also select a subset to read. Subsetting is done in an implicit way.

      Specified by:
      read in interface DataFormat
      Returns:
      the data read from file.
      Throws:
      Exception - if object can not be read
      OutOfMemoryError - if memory is exhausted
      See Also:
      DataFormat.getData()
    • write

      public void write​(Object buf) throws Exception
      Description copied from interface: DataFormat
      Writes a memory buffer to the object in the file.
      Specified by:
      write in interface DataFormat
      Parameters:
      buf - the data to write
      Throws:
      Exception - if data can not be written
    • getMemberCount

      public int getMemberCount()
      Returns the number of members of the compound attribute.
      Specified by:
      getMemberCount in interface CompoundDataFormat
      Returns:
      the number of members of the compound attribute.
    • getSelectedMemberCount

      public int getSelectedMemberCount()
      Returns the number of selected members of the compound attribute. Selected members are the compound fields which are selected for read/write.

      For example, in a compound datatype of {int A, float B, char[] C}, users can choose to retrieve only {A, C} from the attribute. In this case, getSelectedMemberCount() returns two.

      Specified by:
      getSelectedMemberCount in interface CompoundDataFormat
      Returns:
      the number of selected members.
    • getMemberNames

      public String[] getMemberNames()
      Returns the names of the members of the compound attribute. The names of compound members are stored in an array of Strings.

      For example, for a compound datatype of {int A, float B, char[] C} getMemberNames() returns ["A", "B", "C"}.

      Specified by:
      getMemberNames in interface CompoundDataFormat
      Returns:
      the names of compound members.
    • getSelectedMemberNames

      public final String[] getSelectedMemberNames()
      Returns an array of the names of the selected members of the compound dataset.
      Specified by:
      getSelectedMemberNames in interface CompoundDataFormat
      Returns:
      an array of the names of the selected members of the compound dataset.
    • isMemberSelected

      public boolean isMemberSelected​(int idx)
      Checks if a member of the compound attribute is selected for read/write.
      Specified by:
      isMemberSelected in interface CompoundDataFormat
      Parameters:
      idx - the index of compound member.
      Returns:
      true if the i-th memeber is selected; otherwise returns false.
    • selectMember

      public void selectMember​(int idx)
      Selects the i-th member for read/write.
      Specified by:
      selectMember in interface CompoundDataFormat
      Parameters:
      idx - the index of compound member.
    • setAllMemberSelection

      public void setAllMemberSelection​(boolean selectAll)
      Selects/deselects all members.
      Specified by:
      setAllMemberSelection in interface CompoundDataFormat
      Parameters:
      selectAll - The indicator to select or deselect all members. If true, all members are selected for read/write. If false, no member is selected for read/write.
    • getMemberOrders

      public int[] getMemberOrders()
      Returns array containing the total number of elements of the members of the compound attribute.

      For example, a compound attribute COMP has members of A, B and C as

           COMP {
               int A;
               float B[5];
               double C[2][3];
           }
       
      getMemberOrders() will return an integer array of {1, 5, 6} to indicate that member A has one element, member B has 5 elements, and member C has 6 elements.
      Specified by:
      getMemberOrders in interface CompoundDataFormat
      Returns:
      the array containing the total number of elements of the members of the compound attribute.
    • getSelectedMemberOrders

      public int[] getSelectedMemberOrders()
      Returns array containing the total number of elements of the selected members of the compound attribute.

      For example, a compound attribute COMP has members of A, B and C as

           COMP {
               int A;
               float B[5];
               double C[2][3];
           }
       
      If A and B are selected, getSelectedMemberOrders() returns an array of {1, 5}
      Specified by:
      getSelectedMemberOrders in interface CompoundDataFormat
      Returns:
      array containing the total number of elements of the selected members of the compound attribute.
    • getMemberDims

      public int[] getMemberDims​(int i)
      Returns the dimension sizes of the i-th member.

      For example, a compound attribute COMP has members of A, B and C as

           COMP {
               int A;
               float B[5];
               double C[2][3];
           }
       
      getMemberDims(2) returns an array of {2, 3}, while getMemberDims(1) returns an array of {5}, and getMemberDims(0) returns null.
      Specified by:
      getMemberDims in interface CompoundDataFormat
      Parameters:
      i - the i-th member
      Returns:
      the dimension sizes of the i-th member, null if the compound member is not an array.
    • getMemberTypes

      Returns an array of datatype objects of compound members.

      Each member of a compound attribute has its own datatype. The datatype of a member can be atomic or other compound datatype (nested compound). The datatype objects are setup at init().

      Specified by:
      getMemberTypes in interface CompoundDataFormat
      Returns:
      the array of datatype objects of the compound members.
    • getSelectedMemberTypes

      Returns an array of datatype objects of selected compound members.
      Specified by:
      getSelectedMemberTypes in interface CompoundDataFormat
      Returns:
      an array of datatype objects of selected compound members.
    • getMetadata

      public List getMetadata() throws Exception
      Description copied from interface: MetaDataContainer
      Retrieves the object's metadata, such as attributes, from the file.

      Metadata, such as attributes, is stored in a List.

      Specified by:
      getMetadata in interface MetaDataContainer
      Returns:
      the list of metadata objects.
      Throws:
      Exception - if the metadata can not be retrieved
    • writeMetadata

      public void writeMetadata​(Object metadata) throws Exception
      Description copied from interface: MetaDataContainer
      Writes a specific piece of metadata (such as an attribute) into the file. If an HDF(4&5) attribute exists in the file, this method updates its value. If the attribute does not exist in the file, it creates the attribute in the file and attaches it to the object. It will fail to write a new attribute to the object where an attribute with the same name already exists. To update the value of an existing attribute in the file, one needs to get the instance of the attribute by getMetadata(), change its values, then use writeMetadata() to write the value.
      Specified by:
      writeMetadata in interface MetaDataContainer
      Parameters:
      metadata - the metadata to write.
      Throws:
      Exception - if the metadata can not be written
    • removeMetadata

      public void removeMetadata​(Object metadata) throws Exception
      Description copied from interface: MetaDataContainer
      Deletes an existing piece of metadata from this object.
      Specified by:
      removeMetadata in interface MetaDataContainer
      Parameters:
      metadata - the metadata to delete.
      Throws:
      Exception - if the metadata can not be removed
    • updateMetadata

      public void updateMetadata​(Object metadata) throws Exception
      Description copied from interface: MetaDataContainer
      Updates an existing piece of metadata attached to this object.
      Specified by:
      updateMetadata in interface MetaDataContainer
      Parameters:
      metadata - the metadata to update.
      Throws:
      Exception - if the metadata can not be updated
    • hasAttribute

      public boolean hasAttribute()
      Description copied from interface: MetaDataContainer
      Check if the object has any attributes attached.
      Specified by:
      hasAttribute in interface MetaDataContainer
      Returns:
      true if it has any attributes, false otherwise.
    • getDatatype

      public final Datatype getDatatype()
      Description copied from interface: DataFormat
      Returns the datatype of the data object.
      Specified by:
      getDatatype in interface DataFormat
      Overrides:
      getDatatype in class Dataset
      Returns:
      the datatype of the data object.
    • readBytes

      public byte[] readBytes() throws Exception
      Description copied from class: Dataset
      Reads the raw data of the dataset from file to a byte array.

      readBytes() reads raw data to an array of bytes instead of array of its datatype. For example, for a one-dimension 32-bit integer dataset of size 5, readBytes() returns a byte array of size 20 instead of an int array of 5.

      readBytes() can be used to copy data from one dataset to another efficiently because the raw data is not converted to its native type, it saves memory space and CPU time.

      Specified by:
      readBytes in class Dataset
      Returns:
      the byte array of the raw data.
      Throws:
      Exception - if data can not be read
    • copy

      public Dataset copy​(Group pgroup, String name, long[] dims, Object data) throws Exception
      Description copied from class: Dataset
      Creates a new dataset and writes the data buffer to the new dataset.

      This function allows applications to create a new dataset for a given data buffer. For example, users can select a specific interesting part from a large image and create a new image with the selection.

      The new dataset retains the datatype and dataset creation properties of this dataset.

      Specified by:
      copy in class Dataset
      Parameters:
      pgroup - the group which the dataset is copied to.
      name - the name of the new dataset.
      dims - the dimension sizes of the the new dataset.
      data - the data values of the subset to be copied.
      Returns:
      the new dataset.
      Throws:
      Exception - if dataset can not be copied
    • equals

      public boolean equals​(Object obj)
      Returns whether this Attribute is equal to the specified HObject by comparing various properties.
      Overrides:
      equals in class HObject
      Parameters:
      obj - The object
      Returns:
      true if the object is equal
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class HObject
    • toString

      public String toString​(String delimiter)
      Returns a string representation of the data value of the attribute. For example, "0, 255".

      For a compound datatype, it will be a 1D array of strings with field members separated by the delimiter. For example, "{0, 10.5}, {255, 20.0}, {512, 30.0}" is a compound attribute of {int, float} of three data points.

      Parameters:
      delimiter - The delimiter used to separate individual data points. It can be a comma, semicolon, tab or space. For example, toString(",") will separate data by commas.
      Returns:
      the string representation of the data values.
    • toString

      public String toString​(String delimiter, int maxItems)
      Returns a string representation of the data value of the attribute. For example, "0, 255".

      For a compound datatype, it will be a 1D array of strings with field members separated by the delimiter. For example, "{0, 10.5}, {255, 20.0}, {512, 30.0}" is a compound attribute of {int, float} of three data points.

      Parameters:
      delimiter - The delimiter used to separate individual data points. It can be a comma, semicolon, tab or space. For example, toString(",") will separate data by commas.
      maxItems - The maximum number of Array values to return
      Returns:
      the string representation of the data values.
    • convertCompoundByteMember

      protected Object convertCompoundByteMember​(byte[] data, long data_type, long start, long len)
      Given an array of bytes representing a compound Datatype and a start index and length, converts len number of bytes into the correct Object type and returns it.
      Parameters:
      data - The byte array representing the data of the compound Datatype
      data_type - The type of data to convert the bytes to
      start - The start index of the bytes to get
      len - The number of bytes to convert
      Returns:
      The converted type of the bytes