# How to create an array attribute?

**URL:** <https://forum.hdfgroup.org/t/how-to-create-an-array-attribute/12385>\
**Category:** h5py\
**Created:** [June 5, 2024, 9:14pm UTC](https://forum.hdfgroup.org/t/how-to-create-an-array-attribute/12385 "2024-06-05T21:14:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobias](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@tobias](https://forum.hdfgroup.org/u/tobias)\
**Post date:** [June 5, 2024, 9:14pm UTC](https://forum.hdfgroup.org/t/how-to-create-an-array-attribute/12385/1 "2024-06-05T21:14:34Z")

</div>

I have some C++ that reads an attribute from a dataset like this:

```auto
  hsize_t szN[1] = {3};
  hid_t long3_id = H5Tarray_create(H5T_NATIVE_LONG, 1, szN);
  long val[3];

  auto const attrH = H5Aopen_by_name(handle_, dset.c_str(), attr.c_str(), H5P_DEFAULT, H5P_DEFAULT);
  H5Aread(attrH, long3_id, val);

```

I want to write this attribute using `h5py`, but I cannot figure out how. If I simply do:

```auto
dset.attrs['attribute'] = [3, 3, 3]

```

for example, then the attribute is created as integers with a dataspace of [3]. I think I need it to be an array datatype with dataspace [1]. I have also tried:

```auto
traj.attrs.create('attribute', [3, 3, 3], dtype=h5py.h5t.array_create(h5py.h5t.NATIVE_INT64, (3,)))

```

but this results in:

```auto
ValueError: Changing the dtype to a subarray type is only supported if the total itemsize is unchanged

```

I can’t find any examples using `h5t.array_create()`. Any pointers much appreciated!

---

<div class="post-metadata">

**Author:** ![jacques-bernard.leki](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jacques-bernard.leki](https://forum.hdfgroup.org/u/jacques-bernard.leki)\
**Post date:** [June 6, 2024, 10:09am UTC](https://forum.hdfgroup.org/t/how-to-create-an-array-attribute/12385/2 "2024-06-06T10:09:41Z")

</div>

Did you try with numpy:

```auto
import h5py
import numpy as np
with h5py.File('test.h5', mode='w') as h5f:
     h5f.attrs.create(name='attribute', data=np.array([3,3,3], dtype='i8'))

```

```auto
>>> h5dump test.h5 
HDF5 "test.h5" {
GROUP "/" {
   ATTRIBUTE "attribute" {
      DATATYPE H5T_STD_I64LE
      DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
      DATA {
      (0): 3, 3, 3
      }
   }
}
}

```

---

<div class="post-metadata">

**Author:** ![tobias](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@tobias](https://forum.hdfgroup.org/u/tobias)\
**Post date:** [June 6, 2024, 11:26am UTC](https://forum.hdfgroup.org/t/how-to-create-an-array-attribute/12385/3 "2024-06-06T11:26:05Z")

</div>

Using a Numpy array is half the solution. A friend worked out that we need to do:

```auto
traj.attrs.create('attribute', np.array([3, 3, 3]), dtype=h5py.h5t.array_create(h5py.h5t.NATIVE_INT64, (3,)))

```

Which results in:

```auto
      ATTRIBUTE "matrix" {
         DATATYPE H5T_ARRAY { [3] H5T_STD_I64LE }
         DATASPACE SCALAR
         DATA {
         (0): [96, 96, 3]
         }
      }

```

which is the right datatype.

---

<div class="post-metadata">

**Author:** ![tobias](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@tobias](https://forum.hdfgroup.org/u/tobias)\
**Post date:** [September 19, 2024, 1:16pm UTC](https://forum.hdfgroup.org/t/how-to-create-an-array-attribute/12385/4 "2024-09-19T13:16:55Z")

</div>

I’m not sure if it’s best to reply here to my own thread or make a new one, but anyway:

Does anyone have a suggestion for how to do the equivalent to the above in Matlab? A collaborator is trying to write a file for me from Matlab, and I don’t use it much anymore. We are struggling to create the array attribute correctly.
