# Save complex array into h5 file

**URL:** <https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516>\
**Category:** General Usage\
**Created:** [October 30, 2019, 9:37pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516 "2019-10-30T21:37:45Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 30, 2019, 9:37pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/1 "2019-10-30T21:37:46Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 30, 2019, 10:06pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/2 "2019-10-30T22:06:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 30, 2019, 10:13pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/3 "2019-10-30T22:13:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 30, 2019, 10:14pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/4 "2019-10-30T22:14:17Z")

</div>

using HDF5

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 30, 2019, 10:16pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/5 "2019-10-30T22:16:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 30, 2019, 10:16pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/6 "2019-10-30T22:16:53Z")

</div>

does python HDF5 handle complex array?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 30, 2019, 10:22pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/7 "2019-10-30T22:22:05Z")

</div>

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 ([FAQ — h5py 3.7.0 documentation](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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 30, 2019, 10:24pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/8 "2019-10-30T22:24:04Z")

</div>

Alternatively, if you are not bound to HDF5, you could also consider using [NPZ.jl](https://github.com/fhs/NPZ.jl) for exchaning data with python (numpy).

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 30, 2019, 10:26pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/9 "2019-10-30T22:26:35Z")

</div>

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

[https://github.com/JuliaIO/HDF5.jl/pull/558](https://github.com/JuliaIO/HDF5.jl/pull/558)

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 30, 2019, 10:29pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/10 "2019-10-30T22:29:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 30, 2019, 10:30pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/11 "2019-10-30T22:30:07Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 30, 2019, 10:31pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/12 "2019-10-30T22:31:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 30, 2019, 10:37pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/13 "2019-10-30T22:37:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 30, 2019, 10:42pm UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/14 "2019-10-30T22:42:23Z")

</div>

[https://github.com/JuliaIO/JLD.jl/blob/cc660aec56be692632f93274299efc942fa2cd9d/test/jldtests.jl#L399](https://github.com/JuliaIO/JLD.jl/blob/cc660aec56be692632f93274299efc942fa2cd9d/test/jldtests.jl#L399)

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [October 31, 2019, 6:04am UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/15 "2019-10-31T06:04:06Z")

</div>

You can use the following solution from **[MPIFiles.jl](https://github.com/MagneticParticleImaging/MPIFiles.jl)**:  
[https://github.com/MagneticParticleImaging/MPIFiles.jl/blob/1e80890dfb7ee677fda49e083753dd5ef029fcac/src/Utils.jl#L4](https://github.com/MagneticParticleImaging/MPIFiles.jl/blob/1e80890dfb7ee677fda49e083753dd5ef029fcac/src/Utils.jl#L4)

It is compatible with h5py.

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 31, 2019, 7:34am UTC](https://discourse.julialang.org/t/save-complex-array-into-h5-file/30516/16 "2019-10-31T07:34:03Z")

</div>

The latest merge into HDF5#master fixes this problem. Now my example works!  
[https://github.com/JuliaIO/HDF5.jl/pull/591](https://github.com/JuliaIO/HDF5.jl/pull/591)
