diff --git a/src/CFITSIO.jl b/src/CFITSIO.jl index dbfb1f7..8a1c47b 100644 --- a/src/CFITSIO.jl +++ b/src/CFITSIO.jl @@ -79,6 +79,7 @@ export FITSFile, @enum FileMode R = 0 RW = 1 +const PREPEND_PRIMARY = -9 # copied from fitsio.h """ cfitsio_typecode(::Type) -> Cint @@ -1111,15 +1112,23 @@ that is capable of storing the entire array `A`. fits_create_img(f::FITSFile, a::AbstractArray) = fits_create_img(f, eltype(a), size(a)) """ - fits_insert_img(f::FITSFile, T::Type, naxes::Union{Vector{<:Integer}, Tuple{Vararg{Integer}}}) + fits_insert_img(f::FITSFile, T::Type, naxes::Union{Vector{<:Integer}, Tuple{Vararg{Integer}}}[, prependprimary::Bool = false]) Insert a new image extension immediately following the CHDU, or insert a new Primary Array -at the beginning of the file. +at the beginning of the file. A new primary image may be inserted by calling `fits_insert_img` with +`prependprimary` set to `true` after moving to the existing primary HDU, in which case the existing +primary HDU is converted to an image extension. +The inserted array has an eltype `T` and size `naxes`. + + fits_insert_img(f::FITSFile, a::AbstractArray{<:Real}[, prependprimary::Bool = false]) + +Insert a new image HDU with an element type of `eltype(a)` and a size of `size(a)` that is capable +of storing the array `a`. The flag `prependprimary` may be specified to insert a new primary image. """ -function fits_insert_img(f::FITSFile, T::Type, naxes::Vector{<:Integer}) +function fits_insert_img(f::FITSFile, T::Type, naxes::Vector{<:Integer}, prependprimary::Bool = false) fits_assert_open(f) - status = Ref{Cint}(0) + status = Ref{Cint}(prependprimary ? PREPEND_PRIMARY : 0) ccall( (:ffiimgll, libcfitsio), Cint, @@ -1139,10 +1148,10 @@ function fits_insert_img(f::FITSFile, T::Type, naxes::Vector{<:Integer}) fits_assert_ok(status[]) end -function fits_insert_img(f::FITSFile, T::Type, naxes::NTuple{N,Integer}) where {N} +function fits_insert_img(f::FITSFile, T::Type, naxes::NTuple{N,Integer}, prependprimary::Bool = false) where {N} fits_assert_open(f) - status = Ref{Cint}(0) + status = Ref{Cint}(prependprimary ? PREPEND_PRIMARY : 0) naxesr = Ref(map(Int64, naxes)) ccall( (:ffiimgll, libcfitsio), @@ -1163,7 +1172,7 @@ function fits_insert_img(f::FITSFile, T::Type, naxes::NTuple{N,Integer}) where { fits_assert_ok(status[]) end -fits_insert_img(f::FITSFile, a::AbstractArray) = fits_insert_img(f, eltype(a), size(a)) +fits_insert_img(f::FITSFile, a::AbstractArray{<:Real}, prependprimary::Bool = false) = fits_insert_img(f, eltype(a), size(a), prependprimary) """ fits_write_pix(f::FITSFile, fpixel::Union{Vector{<:Integer}, Tuple{Vararg{Integer}}}, nelements::Integer, data::StridedArray) diff --git a/test/runtests.jl b/test/runtests.jl index fafb2a6..4a3d52e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -280,6 +280,17 @@ end fits_movabs_hdu(f, 2) fits_read_pix(f, b) @test b == ones(2,2) .* 4 + # Insert new primary image + for sz in Any[[1,2,3], (2,4)] + fits_movabs_hdu(f, 1) + sz_exist = fits_get_img_size(f) + fits_insert_img(f, Int, sz, true) + @test fits_get_hdu_num(f) == 1 + @test fits_get_img_size(f) == [sz...] + # test that the primary HDU is converted to an image + fits_movabs_hdu(f, 2) + @test fits_get_img_size(f) == sz_exist + end end end end