Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert a new primary HDU #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/CFITSIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export FITSFile,


@enum FileMode R = 0 RW = 1
const PREPEND_PRIMARY = -9 # copied from fitsio.h

"""
cfitsio_typecode(::Type) -> Cint
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down