From 5c376e43eb3d5bf52d9c3ad17c84d7459365feb9 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sun, 22 Aug 2021 14:44:25 +0400 Subject: [PATCH] Add fits_insert_img (#15) * Bugfix in closing empty files * Add fits_insert_img * Add doc entry --- Project.toml | 2 +- docs/src/index.md | 1 + src/CFITSIO.jl | 56 +++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 34 ++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 72c49f1..ea6f588 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CFITSIO" uuid = "3b1b4be9-1499-4b22-8d78-7db3344d1961" authors = ["Miles Lucas and contributors"] -version = "1.3.1" +version = "1.4.0" [deps] CFITSIO_jll = "b3e40c51-02ae-5482-8a39-3ace5868dcf4" diff --git a/docs/src/index.md b/docs/src/index.md index 04066a7..2b738cf 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -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 diff --git a/src/CFITSIO.jl b/src/CFITSIO.jl index 93afd51..dbfb1f7 100644 --- a/src/CFITSIO.jl +++ b/src/CFITSIO.jl @@ -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, @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index d28090d..fafb2a6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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