Class Attribute
- Object
-
- hdf.object.HObject
-
- hdf.object.Dataset
-
- hdf.object.Attribute
-
- All Implemented Interfaces:
CompoundDataFormat
,DataFormat
,MetaDataContainer
,Serializable
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 Summary
Fields Modifier and Type Field Description protected boolean[]
isMemberSelected
The array to store flags to indicate if a member of this compound attribute is selected for read/write.protected boolean
isScalar
Flag to indicate if the attribute data is a single scalar pointprotected Object[]
memberDims
The dimension sizes of each member.protected String[]
memberNames
The names of the members of the compound attribute.protected int[]
memberOrders
Array containing the total number of elements of the members of this compound attribute.protected Datatype[]
memberTypes
The datatypes of the compound attribute's members.protected int
numberOfMembers
The number of members of the compound attribute.protected HObject
parentObject
The HObject to which this Attribute is attachedprotected boolean
unsignedConverted
Flag to indicate is the original unsigned C data is converted.-
Fields inherited from class hdf.object.Dataset
chunkSize, compression, COMPRESSION_GZIP_TXT, convertByteToString, convertedBuf, data, datatype, dimNames, dims, filters, inited, isDataLoaded, maxDims, nPoints, originalBuf, rank, selectedDims, selectedIndex, selectedStride, startDims, storage, storageLayout
-
Fields inherited from class hdf.object.HObject
fileFormat, linkTargetObjName, oid, SEPARATOR
-
-
Constructor Summary
Constructors Constructor Description Attribute(HObject parentObj, String attrName, Datatype attrType, long[] attrDims)
Create an attribute with specified name, data type and dimension sizes.Attribute(HObject parentObj, String attrName, Datatype attrType, long[] attrDims, Object attrValue)
Create an attribute with specific name and value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearData()
Clears the current data buffer in memory and forces the next read() to load the data from file.void
close(long aid)
Closes access to the object.Object
convertFromUnsignedC()
Converts the data values of this Attribute to appropriate Java integers if they are unsigned integers.Object
convertToUnsignedC()
Converts Java integer data values of this Attribute back to unsigned C-type integer data if they are unsigned integers.Dataset
copy(Group pgroup, String name, long[] dims, Object data)
Creates a new dataset and writes the data buffer to the new dataset.boolean
equals(Object obj)
Returns whether this Attribute is equal to the specified HObject by comparing various properties.Datatype
getDatatype()
Returns the datatype of the data object.Object
getFillValue()
Returns the fill values for the data object.int
getMemberCount()
Returns the number of members of the compound attribute.int[]
getMemberDims(int i)
Returns the dimension sizes of the i-th member.String[]
getMemberNames()
Returns the names of the members of the compound attribute.int[]
getMemberOrders()
Returns array containing the total number of elements of the members of the compound attribute.Datatype[]
getMemberTypes()
Returns an array of datatype objects of compound members.List
getMetadata()
Retrieves the object's metadata, such as attributes, from the file.HObject
getParentObject()
Returns the HObject to which this Attribute is currently "attached".Object
getProperty(String key)
get a property for a given key.Collection<String>
getPropertyKeys()
get all property keys.int
getSelectedMemberCount()
Returns the number of selected members of the compound attribute.String[]
getSelectedMemberNames()
Returns an array of the names of the selected members of the compound dataset.int[]
getSelectedMemberOrders()
Returns array containing the total number of elements of the selected members of the compound attribute.Datatype[]
getSelectedMemberTypes()
Returns an array of datatype objects of selected compound members.boolean
hasAttribute()
Check if the object has any attributes attached.int
hashCode()
void
init()
boolean
isMemberSelected(int idx)
Checks if a member of the compound attribute is selected for read/write.boolean
isScalar()
long
open()
Opens an existing object such as a dataset or group for access.Object
read()
Reads the data from file.byte[]
readBytes()
Reads the raw data of the dataset from file to a byte array.void
removeMetadata(Object metadata)
Deletes an existing piece of metadata from this object.void
selectMember(int idx)
Selects the i-th member for read/write.void
setAllMemberSelection(boolean selectAll)
Selects/deselects all members.void
setParentObject(HObject pObj)
Sets the HObject to which this Attribute is "attached".void
setProperty(String key, Object value)
set a property for the attribute.String
toString(String delimiter)
Returns a string representation of the data value of the attribute.String
toString(String delimiter, int maxItems)
Returns a string representation of the data value of the attribute.void
updateMetadata(Object metadata)
Updates an existing piece of metadata attached to this object.void
write(Object buf)
Writes a memory buffer to the object in the file.void
writeMetadata(Object metadata)
Writes a specific piece of metadata (such as an attribute) into the file.-
Methods inherited from class hdf.object.Dataset
byteToString, clear, convertFromUnsignedC, convertFromUnsignedC, convertToUnsignedC, convertToUnsignedC, getChunkSize, getCompression, getConvertByteToString, getData, getDimNames, getDims, getFilters, getHeight, getMaxDims, getOriginalClass, getRank, getSelectedDims, getSelectedIndex, getSize, getStartDims, getStorage, getStorageLayout, getStride, getVirtualFilename, getVirtualMaps, getWidth, isInited, isString, isVirtual, setConvertByteToString, setData, stringToByte, write
-
Methods inherited from class hdf.object.HObject
debug, equals, equalsOID, getFID, getFile, getFileFormat, getFullName, getLinkTargetObjName, getName, getOID, getPath, setLinkTargetObjName, setName, setPath, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface hdf.object.DataFormat
getCompression, getData, getDims, getHeight, getOriginalClass, getRank, getSelectedDims, getSelectedIndex, getStartDims, getStride, getWidth, isInited, setData, write
-
-
-
-
Field Detail
-
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
-
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 Detail
-
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 attributeattrValue
- the value of the attribute, null if no value- See Also:
Datatype
-
-
Method Detail
-
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 classHObject
- 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.
-
init
public void init()
- Specified by:
init
in interfaceDataFormat
-
getParentObject
public HObject 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 interfaceDataFormat
- 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 interfaceDataFormat
- 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 interfaceDataFormat
- 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 interfaceDataFormat
- Overrides:
clearData
in classDataset
- See Also:
Dataset.getData()
,DataFormat.read()
-
setProperty
public void setProperty(String key, Object value)
set a property for the attribute.- Parameters:
key
- the attribute Map keyvalue
- 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 interfaceDataFormat
- Returns:
- the data read from file.
- Throws:
Exception
- if object can not be readOutOfMemoryError
- 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 interfaceDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- Parameters:
idx
- the index of compound member.
-
setAllMemberSelection
public void setAllMemberSelection(boolean selectAll)
Selects/deselects all members.- Specified by:
setAllMemberSelection
in interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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 interfaceCompoundDataFormat
- 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
public Datatype[] 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 interfaceCompoundDataFormat
- Returns:
- the array of datatype objects of the compound members.
-
getSelectedMemberTypes
public Datatype[] getSelectedMemberTypes()
Returns an array of datatype objects of selected compound members.- Specified by:
getSelectedMemberTypes
in interfaceCompoundDataFormat
- 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 interfaceMetaDataContainer
- 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 interfaceMetaDataContainer
- 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 interfaceMetaDataContainer
- 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 interfaceMetaDataContainer
- 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 interfaceMetaDataContainer
- 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 interfaceDataFormat
- Overrides:
getDatatype
in classDataset
- 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.
-
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 classDataset
- 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.
-
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.
-
-