mirror of
https://github.com/HDFGroup/hdf5.git
synced 2024-12-21 07:51:46 +08:00
141 lines
3.1 KiB
Plaintext
141 lines
3.1 KiB
Plaintext
Attribute Examples:
|
|
|
|
H5Acreate example: Show how to create an attribute for a dataset and a group
|
|
----------------
|
|
{
|
|
hid_t file;
|
|
hid_t group;
|
|
hid_t dataset;
|
|
hid_t attr;
|
|
hid_t dataspace;
|
|
int32 attr_data;
|
|
int rank;
|
|
size_t dimsf[2];
|
|
|
|
/* Open the file */
|
|
file=H5Fopen("example.h5", H5F_ACC_RDWR, H5P_DEFAULT);
|
|
|
|
/* Describe the size of the array and create the data space */
|
|
rank=2;
|
|
dimsf[0] = H5S_UNLIMITED;
|
|
dimsf[1] = H5S_UNLIMITED;
|
|
dataspace = H5Screate_simple(rank, dimsf, NULL);
|
|
|
|
/* Create a dataset */
|
|
dataset=H5Dcreate(file,"Dataset1",H5T_UINT8,dataspace,H5P_DEFAULT);
|
|
|
|
<Write data to first dataset>
|
|
|
|
/* Create an attribute for the dataset */
|
|
attr=H5Acreate(dataset,"Attr1",H5T_INT32,H5S_SCALAR,H5P_DEFAULT);
|
|
|
|
/* Write attribute information */
|
|
H5Awrite(attr,H5T_INT32,&attr_data);
|
|
|
|
/* Close attribute */
|
|
H5Aclose(attr);
|
|
|
|
/* Close dataset */
|
|
H5Dclose(dataset);
|
|
|
|
/* Create a group */
|
|
group=H5Gcreate(file,"/Group One",0);
|
|
|
|
/* Create an attribute for the dataset */
|
|
attr=H5Acreate(group,"Attr1",H5T_INT32,H5S_SCALAR,H5P_DEFAULT);
|
|
|
|
/* Write attribute information */
|
|
H5Awrite(attr,H5T_INT32,&attr_data);
|
|
|
|
/* Close attribute */
|
|
H5Aclose(attr);
|
|
|
|
/* Close the group */
|
|
H5Gclose(group);
|
|
|
|
/* Close file */
|
|
H5Fclose(file);
|
|
}
|
|
|
|
|
|
H5Aiterate example: Print all the names of attributes of a dataset, without
|
|
any buffers.
|
|
---------------------
|
|
|
|
herr_t print_names (hid_t loc_id, const char *name, void *opdata)
|
|
{
|
|
puts (name);
|
|
return 0;
|
|
}
|
|
|
|
{
|
|
H5Aiterate (dataset_or_group_id, NULL, print_names, NULL);
|
|
}
|
|
|
|
|
|
H5Aread Example: Attach to an attribute of a dataset and read in attr. data
|
|
----------------
|
|
{
|
|
hid_t file;
|
|
hid_t dataset;
|
|
hid_t attr;
|
|
int32 attr_data;
|
|
|
|
/* Open the file */
|
|
file=H5Fopen("example.h5", H5F_ACC_RDWR, H5P_DEFAULT);
|
|
|
|
/* Open the dataset */
|
|
dataset=H5Dopen(file,"Dataset1");
|
|
|
|
/* Get the OID of the attribute */
|
|
attr=H5Aopen_name(dataset,"Attr1");
|
|
|
|
/* Read attribute */
|
|
H5Aread(attr,H5T_INT32,attr_data);
|
|
|
|
/* Close attribute dataset */
|
|
H5Aclose(attr);
|
|
|
|
/* Close first dataset */
|
|
H5Dclose(dataset);
|
|
|
|
/* Close file */
|
|
H5Fclose(file);
|
|
}
|
|
|
|
H5Alink Example: Shows how to share an attribute between two datasets.
|
|
----------------
|
|
{
|
|
hid_t file;
|
|
hid_t dataset1, dataset2;
|
|
hid_t attr;
|
|
|
|
/* Open the file */
|
|
file=H5Fopen("example.h5", H5F_ACC_RDWR, H5P_DEFAULT);
|
|
|
|
/* Open the first dataset */
|
|
dataset1=H5Dopen(file,"Dataset1");
|
|
|
|
/* Open the first dataset */
|
|
dataset2=H5Dopen(file,"Dataset2");
|
|
|
|
/* Get the OID of the attribute */
|
|
attr=H5Aopen_name(dataset1,"Foo");
|
|
|
|
/*
|
|
* Create an attribute in the second dataset to the attribute in dataset1,
|
|
* changing the name of the attribute information in dataset2.
|
|
*/
|
|
H5Alink(dataset2, attr, "Bar");
|
|
|
|
/* Close attribute dataset */
|
|
H5Aclose(attr);
|
|
|
|
/* Close datasets */
|
|
H5Dclose(dataset1);
|
|
H5Dclose(dataset2);
|
|
|
|
/* Close file */
|
|
H5Fclose(file);
|
|
}
|