Skip to content

Commit

Permalink
Add fits_insert_img (#15)
Browse files Browse the repository at this point in the history
* Bugfix in closing empty files

* Add fits_insert_img

* Add doc entry
  • Loading branch information
jishnub authored Aug 22, 2021
1 parent c69968f commit 5c376e4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CFITSIO"
uuid = "3b1b4be9-1499-4b22-8d78-7db3344d1961"
authors = ["Miles Lucas <mdlucas@hawaii.edu> and contributors"]
version = "1.3.1"
version = "1.4.0"

[deps]
CFITSIO_jll = "b3e40c51-02ae-5482-8a39-3ace5868dcf4"
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ fits_hdr2str
```@docs
fits_get_img_size
fits_create_img
fits_insert_img
fits_write_pix
fits_write_pixnull
fits_write_subset
Expand Down
56 changes: 56 additions & 0 deletions src/CFITSIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export FITSFile,
fits_get_version,
fits_read_tdim,
fits_hdr2str,
fits_insert_img,
fits_insert_rows,
fits_movabs_hdu,
fits_movrel_hdu,
Expand Down Expand Up @@ -1109,6 +1110,61 @@ 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}}})
Insert a new image extension immediately following the CHDU, or insert a new Primary Array
at the beginning of the file.
"""
function fits_insert_img(f::FITSFile, T::Type, naxes::Vector{<:Integer})
fits_assert_open(f)

status = Ref{Cint}(0)
ccall(
(:ffiimgll, libcfitsio),
Cint,
(
Ptr{Cvoid},
Cint,
Cint,
Ptr{Int64},
Ref{Cint},
),
f.ptr,
bitpix_from_type(T),
length(naxes),
convert(Vector{Int64}, naxes),
status,
)
fits_assert_ok(status[])
end

function fits_insert_img(f::FITSFile, T::Type, naxes::NTuple{N,Integer}) where {N}
fits_assert_open(f)

status = Ref{Cint}(0)
naxesr = Ref(map(Int64, naxes))
ccall(
(:ffiimgll, libcfitsio),
Cint,
(
Ptr{Cvoid},
Cint,
Cint,
Ptr{NTuple{N,Int64}},
Ref{Cint},
),
f.ptr,
bitpix_from_type(T),
N,
naxesr,
status,
)
fits_assert_ok(status[])
end

fits_insert_img(f::FITSFile, a::AbstractArray) = fits_insert_img(f, eltype(a), size(a))

"""
fits_write_pix(f::FITSFile, fpixel::Union{Vector{<:Integer}, Tuple{Vararg{Integer}}}, nelements::Integer, data::StridedArray)
Expand Down
34 changes: 34 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,40 @@ end
@test fits_get_hdu_num(f) == 1
@test fits_get_img_dim(f) == 0
end

@testset "insert image" begin
tempfitsfile() do f
a = ones(2,2); b = similar(a)
fits_insert_img(f, a)
fits_write_pix(f, a)
fits_read_pix(f, b)
@test b == a
@test fits_get_num_hdus(f) == 1
a .*= 2
fits_insert_img(f, eltype(a), [size(a)...])
fits_write_pix(f, a)
fits_read_pix(f, b)
@test b == a
@test fits_get_num_hdus(f) == 2
fits_movabs_hdu(f, 1)
a .*= 2
fits_insert_img(f, a)
fits_write_pix(f, a)
fits_read_pix(f, b)
@test b == a
@test fits_get_num_hdus(f) == 3
# test that the HDU is added in the middle
fits_movabs_hdu(f, 1)
fits_read_pix(f, b)
@test b == ones(2,2)
fits_movabs_hdu(f, 3)
fits_read_pix(f, b)
@test b == ones(2,2) .* 2
fits_movabs_hdu(f, 2)
fits_read_pix(f, b)
@test b == ones(2,2) .* 4
end
end
end

@testset "image type/size" begin
Expand Down

2 comments on commit 5c376e4

@giordano
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/43315

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.0 -m "<description of version>" 5c376e43eb3d5bf52d9c3ad17c84d7459365feb9
git push origin v1.4.0

Please sign in to comment.