JuliaAstro / FITSIO.jl

Flexible Image Transport System (FITS) file support for Julia
http://juliaastro.org/FITSIO.jl/
MIT License
55 stars 29 forks source link

Add support for reading and writing complex arrays by reinterpreting the elements as pairs of floating point ones #129

Closed jishnub closed 4 years ago

jishnub commented 4 years ago

This is more of a discussion that I wanted to have, as I often have to work in Fourier space where arrays are complex. Since complex arrays are usually laid out contiguously in memory, it's possible to write them to FITS files by reinterpreting them as floating point arrays where the first dimension is doubled in size. Such arrays may also be read in by reinterpreting them as Complex ones of the correct type. Is this a feature that might be implemented? In that case I might consider submitting a PR.

What I currently use for complex arrays is something like floatarr = collect(reinterpret(Float64,complexarr) before writing to FITS. This involves an extra collection --- and hence allocation --- that is unnecessary. It appears solving the write is straightforward by expanding the type dispatched upon from Array{T} to Union{Array{T},Base.ReinterpretArray{T}}. I haven't looked into the read part yet. I was wondering if this appears sensible, or if there is something that I'm missing.

jishnub commented 4 years ago

The type might be further restricted to something along the lines of

Base.ReinterpretArray{T,N,Complex{T},Array{Complex{T},N}} where {N,T}

which is expected to be contiguous. Just a general Base.ReinterpretArray{T} might not be the best idea if arrays of structs are used because of padding (eg this discourse post).

jishnub commented 4 years ago

Fixed by #131