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

Read and write complex arrays + limit compat version bounds #131

Closed jishnub closed 4 years ago

jishnub commented 4 years ago

This PR lets us read and write complex arrays that are reinterpreted as floating point ones (addresses #129). Eg:

julia> FITS("temp.fits","w") do f
       write(f, reinterpret(Float64, ones(ComplexF64,3,3) ) )
       end

julia> FITS("temp.fits","r") do f
       read(f[1])
       end
6×3 Array{Float64,2}:
 1.0  1.0  1.0
 0.0  0.0  0.0
 1.0  1.0  1.0
 0.0  0.0  0.0
 1.0  1.0  1.0
 0.0  0.0  0.0

julia> FITS("temp.fits","r") do f
       reinterpret(ComplexF64,read(f[1]))
       end
3×3 reinterpret(Complex{Float64}, ::Array{Float64,2}):
 1.0+0.0im  1.0+0.0im  1.0+0.0im
 1.0+0.0im  1.0+0.0im  1.0+0.0im
 1.0+0.0im  1.0+0.0im  1.0+0.0im

# In-place read into a pre-allocated Complex Array
julia> a = zeros(ComplexF64,3,3);

julia> FITS("temp.fits","r") do f
       read!(f[1], reinterpret(Float64,a))
       end;

julia> a
3×3 Array{Complex{Float64},2}:
 1.0+0.0im  1.0+0.0im  1.0+0.0im
 1.0+0.0im  1.0+0.0im  1.0+0.0im
 1.0+0.0im  1.0+0.0im  1.0+0.0im

The reinterpret wrapper is cheap and allocates minimal memory, so this avoids the need allocate a separate real array. Contiguous views work as expected as well.

julia> a = rand(ComplexF64,2,2)
2×2 Array{Complex{Float64},2}:
 0.00578056+0.220908im  0.826413+0.10244im
   0.162735+0.179831im  0.453422+0.363282im

julia> FITS("temp.fits","w") do f
       write(f, reinterpret(Float64, @view a[1:2,2] ) )
       end

julia> FITS("temp.fits","r") do f
       reinterpret(ComplexF64,read(f[1]))
       end
2-element reinterpret(Complex{Float64}, ::Array{Float64,1}):
 0.8264131246556028 + 0.10244002302386535im
  0.453422466907486 + 0.3632824510821362im

Additionally, this also changes the compatibility bounds on dependencies from inequalities to caret ones (#130). This is more conservative and keeping in line with the julia semver convention.

jishnub commented 4 years ago

Some of the commits are relics of a previous PR that has already been merged. Only the last 3 are new.

jishnub commented 4 years ago

Tests appear to fail on Mac OS because of BinaryProvider. Maybe need to wait for a version bump from them.

jishnub commented 4 years ago

Not sure why BSD tests are failing

giordano commented 4 years ago

Can you please rebase on master? I made some changes to CI

jishnub commented 4 years ago

Thanks, tests pass now

jishnub commented 4 years ago

Hope I've not screwed up the rebase by squashing the commits

giordano commented 4 years ago

I hope you don't mind I have force-pushed to your branch, but the history was messed up.

Opening a pull request from a branch that has the same name as the target one is considered an antipatter and GitHub shows you a big red warning when you try to do so. The problem is exactly what you faced: when you have to sync with upstream the history can become a mess if you don't know how to do it. Help yourself, create a branch with a different name the next time :wink:

jishnub commented 4 years ago

Thanks for your help, for some reason I seem to have missed the warning. Is this ok to merge now?