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[]isMemberSelectedThe array to store flags to indicate if a member of this compound attribute is selected for read/write.protected booleanisScalarFlag to indicate if the attribute data is a single scalar pointprotected Object[]memberDimsThe dimension sizes of each member.protected String[]memberNamesThe names of the members of the compound attribute.protected int[]memberOrdersArray containing the total number of elements of the members of this compound attribute.protected Datatype[]memberTypesThe datatypes of the compound attribute's members.protected intnumberOfMembersThe number of members of the compound attribute.protected HObjectparentObjectThe HObject to which this Attribute is attachedprotected booleanunsignedConvertedFlag 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 voidclearData()Clears the current data buffer in memory and forces the next read() to load the data from file.voidclose(long aid)Closes access to the object.ObjectconvertFromUnsignedC()Converts the data values of this Attribute to appropriate Java integers if they are unsigned integers.ObjectconvertToUnsignedC()Converts Java integer data values of this Attribute back to unsigned C-type integer data if they are unsigned integers.Datasetcopy(Group pgroup, String name, long[] dims, Object data)Creates a new dataset and writes the data buffer to the new dataset.booleanequals(Object obj)Returns whether this Attribute is equal to the specified HObject by comparing various properties.DatatypegetDatatype()Returns the datatype of the data object.ObjectgetFillValue()Returns the fill values for the data object.intgetMemberCount()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.ListgetMetadata()Retrieves the object's metadata, such as attributes, from the file.HObjectgetParentObject()Returns the HObject to which this Attribute is currently "attached".ObjectgetProperty(String key)get a property for a given key.Collection<String>getPropertyKeys()get all property keys.intgetSelectedMemberCount()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.booleanhasAttribute()Check if the object has any attributes attached.inthashCode()voidinit()booleanisMemberSelected(int idx)Checks if a member of the compound attribute is selected for read/write.booleanisScalar()longopen()Opens an existing object such as a dataset or group for access.Objectread()Reads the data from file.byte[]readBytes()Reads the raw data of the dataset from file to a byte array.voidremoveMetadata(Object metadata)Deletes an existing piece of metadata from this object.voidselectMember(int idx)Selects the i-th member for read/write.voidsetAllMemberSelection(boolean selectAll)Selects/deselects all members.voidsetParentObject(HObject pObj)Sets the HObject to which this Attribute is "attached".voidsetProperty(String key, Object value)set a property for the attribute.StringtoString(String delimiter)Returns a string representation of the data value of the attribute.StringtoString(String delimiter, int maxItems)Returns a string representation of the data value of the attribute.voidupdateMetadata(Object metadata)Updates an existing piece of metadata attached to this object.voidwrite(Object buf)Writes a memory buffer to the object in the file.voidwriteMetadata(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:HObjectOpens 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:
openin 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:HObjectCloses 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:
initin 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:
convertFromUnsignedCin 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:
convertToUnsignedCin 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:DataFormatReturns the fill values for the data object.- Specified by:
getFillValuein interfaceDataFormat- Returns:
- the fill values for the data object.
-
clearData
public void clearData()
Description copied from class:DatasetClears 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:
clearDatain interfaceDataFormat- Overrides:
clearDatain 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:DataFormatReads 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:
readin 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:DataFormatWrites a memory buffer to the object in the file.- Specified by:
writein 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:
getMemberCountin 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:
getSelectedMemberCountin 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:
getMemberNamesin 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:
getSelectedMemberNamesin 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:
isMemberSelectedin 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:
selectMemberin interfaceCompoundDataFormat- Parameters:
idx- the index of compound member.
-
setAllMemberSelection
public void setAllMemberSelection(boolean selectAll)
Selects/deselects all members.- Specified by:
setAllMemberSelectionin 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:
getMemberOrdersin 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:
getSelectedMemberOrdersin 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:
getMemberDimsin 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:
getMemberTypesin 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:
getSelectedMemberTypesin interfaceCompoundDataFormat- Returns:
- an array of datatype objects of selected compound members.
-
getMetadata
public List getMetadata() throws Exception
Description copied from interface:MetaDataContainerRetrieves the object's metadata, such as attributes, from the file.Metadata, such as attributes, is stored in a List.
- Specified by:
getMetadatain 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:MetaDataContainerWrites 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:
writeMetadatain 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:MetaDataContainerDeletes an existing piece of metadata from this object.- Specified by:
removeMetadatain 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:MetaDataContainerUpdates an existing piece of metadata attached to this object.- Specified by:
updateMetadatain interfaceMetaDataContainer- Parameters:
metadata- the metadata to update.- Throws:
Exception- if the metadata can not be updated
-
hasAttribute
public boolean hasAttribute()
Description copied from interface:MetaDataContainerCheck if the object has any attributes attached.- Specified by:
hasAttributein interfaceMetaDataContainer- Returns:
- true if it has any attributes, false otherwise.
-
getDatatype
public final Datatype getDatatype()
Description copied from interface:DataFormatReturns the datatype of the data object.- Specified by:
getDatatypein interfaceDataFormat- Overrides:
getDatatypein classDataset- Returns:
- the datatype of the data object.
-
readBytes
public byte[] readBytes() throws Exception
Description copied from class:DatasetReads 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:DatasetCreates 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:
copyin 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.
-
-