Save complex array into h5 file

Why this does not work? what is the appropriate syntax ?

f = h5open("file.h5", "w")
dset_f = d_create(f, "psi", datatype(Complex{Float64}), dataspace(5, 5, 10))
for i in 1:10
    dset_f[:, :, i] = zeros(Complex{Float64}, 5,5)
end
flush(f)
close(f)

Which package is this using? Please make sure this can be copied and pasted.

HDF5 doesn’t contain a standard for saving complex numbers. Some packages save the real and imaginary part separately, others use a compound type.

The simplest way to achieve what you want is to use JLD.jl (or JLD2.jl) instead of HDF5.jl.

2 Likes

using HDF5

yes, JLD solved the problem. But, now only julia can read these files. With HDF5 I can go back and forward, with python for example.

does python HDF5 handle complex array?

Again, their is no standard in HDF5 for complex numbers. Your python package just does “something” to handle complex numbers (similar to JLD(2)). In case you’re using h5py, it is storing real and imaginary part in a HDF5 struct (https://docs.h5py.org/en/stable/faq.html#what-datatypes-are-supported).

In any case, you can save real(x) and imag(x) separately. This is the simplest solution and will readily be readable in python and Julia.

2 Likes

Alternatively, if you are not bound to HDF5, you could also consider using NPZ.jl for exchaning data with python (numpy).

1 Like

yes. That’s another alternative. I was under the impression that this will work.

besides, with JLD or NPZ I need to calculate the whole array in advance, in order to save it, whereas with HDF5 I could do it sequentially.

Oh, in that case you are lucky in the sense that HDF5.jl tries to synchronize its behavior with h5py nowadays. However, this is a new feature with some rough edges still. See the answer you got over there. When https://github.com/JuliaIO/HDF5.jl/pull/591 lands (and a new release is tagged) your code should work.

BTW, I think that’s a great development.

I believe JLD(2).jl also supports hyperslabbing, IIRC. I can’t check it right now, though.

no. It’s not supported. Unless I missed something in the documentation.

You can use the following solution from MPIFiles.jl:

It is compatible with h5py.

The latest merge into HDF5#master fixes this problem. Now my example works!

1 Like