Class Attribute

java.lang.Object
ncsa.hdf.object.Attribute
All Implemented Interfaces:
Serializable, Metadata

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

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

For more details on attibutes, see {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">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);    // signed or 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,
Version:
1.1 9/4/2007
Author:
Peter X. Cao
See Also:
  • Field Details

    • isScalar

      protected boolean isScalar
      flag to indicate if the dataset is a single scalar point
  • Constructor Details

    • Attribute

      public Attribute(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 = new H5Datatype(Datatype.CLASS_STRING, classValue[0].length() + 1, -1, -1);
       Attribute attr = new Attribute(attrName, attrType, attrDims);
       attr.setValue(classValue);
       
      Parameters:
      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:
    • Attribute

      public Attribute(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 = new H5Datatype(
                                                                                                                        Datatype.CLASS_STRING,
                                                                                                                        classValue[0]
                                                                                                                                .length() + 1,
                                                                                                                        -1, -1);
                                                                                                                                 Attribute attr = new Attribute(
                                                                                                                                                        attrName,
                                                                                                                                                        attrType,
                                                                                                                                                        attrDims,
                                                                                                                                                        classValue);
       
      Parameters:
      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:
  • Method Details

    • getValue

      public Object getValue()
      Returns the value of the attribute. For atomic datatype, this will be an 1D array of integers, floats and strings. For compound datatype, it will be an 1D array of strings with field members separated by comma. For example, "{0, 10.5}, {255, 20.0}, {512, 30.0}" is a cmpound attribute of {int, float} of three data points.
      Specified by:
      getValue in interface Metadata
      Returns:
      the value of the attribute, or null if failed to retrieve data from file.
    • setProperty

      public void setProperty(String key, Object value)
      set a property for the attribute.
    • getProperty

      public Object getProperty(String key)
      get a property for a given key.
    • getPropertyKeys

      public Collection<String> getPropertyKeys()
      get all property keys.
    • setValue

      public void setValue(Object theValue)
      Sets the value of the attribute. It returns null if failed to retrieve the name from file.
      Specified by:
      setValue in interface Metadata
      Parameters:
      theValue - The value of the attribute to set
    • getName

      public String getName()
      Returns the name of the attribute.
      Returns:
      the name of the attribute.
    • getRank

      public int getRank()
      Returns the rank (number of dimensions) of the attribute. It returns a negative number if failed to retrieve the dimension information from file.
      Returns:
      the number of dimensions of the attribute.
    • getDataDims

      public long[] getDataDims()
      Returns the dimension sizes of the data value of the attribute. It returns null if failed to retrieve the dimension information from file.
      Returns:
      the dimension sizes of the attribute.
    • getType

      public Datatype getType()
      Returns the datatype of the attribute. It returns null if failed to retrieve the datatype information from file.
      Returns:
      the datatype of the attribute.
    • isScalar

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

      public boolean isUnsigned()
      Checks if the data type of this attribute is an unsigned integer.
      Returns:
      true if the data type of the attribute is an unsigned integer; otherwise returns false.
    • toString

      public String toString()
      Return the name of the attribute.
      Overrides:
      toString in class Object
      See Also:
    • toString

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

      For compound datatype, it will be an 1D array of strings with field members separated by comma. 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 to separate individual data point. It can be comma, semicolon, tab or space. For example, to String(",") will separate data by comma.
      Returns:
      the string representation of the data values.