Skip to content

Commit

Permalink
Merge 88170a8 into d722f9a
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthlal25 committed Aug 5, 2020
2 parents d722f9a + 88170a8 commit e7f5890
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Expand Up @@ -9,8 +9,8 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
CFITSIO = "1"
julia = "1.3"
Reexport = "0.2"
julia = "1.3"

[extras]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
5 changes: 3 additions & 2 deletions src/FITSIO.jl
Expand Up @@ -11,7 +11,8 @@ export FITS,
read_header,
get_comment,
set_comment!,
copy_section
copy_section,
default_header

import Base: getindex,
setindex!,
Expand All @@ -33,7 +34,7 @@ using Printf
import Base: iterate, lastindex
# Libcfitsio submodule

## Have to manually import while deprecating `libcfitsio_version`.
## Have to manually import while deprecating `libcfitsio_version`.
## After removing that, can return to using CFITSIO
import CFITSIO: FITSFile,
FITSMemoryHandle,
Expand Down
30 changes: 30 additions & 0 deletions src/header.jl
Expand Up @@ -422,3 +422,33 @@ function show(io::IO, hdr::FITSHeader)
i != n && println(io)
end
end

"""
default_header(data::AbstractArray)
Creates a default header for the given array with the `SIMPLE`, `BITPIX`, `NAXIS`, `NAXIS*`, and `EXTEND` entries.
"""
function default_header(data::AbstractArray{T}) where T <: Number
# assigning keys
hdu_keys = ["SIMPLE",
"BITPIX",
"NAXIS",
("NAXIS$i" for i in 1:ndims(data))...,
"EXTEND"]

# assiging values
hdu_values = [true, # SIMPLE
CFITSIO.bitpix_from_type(T), # BITPIX
ndims(data), # NAXIS
reverse(size(data))..., # size of each axis
true] # EXTEND

# assigning comments
comments = ["file does conform to FITS standard", # comment for SIMPLE
"number of bits per data pixel", # comment for BITPIX
"number of data axes", # comment for NAXIS
("length of data axis $i" for i in 1:ndims(data))..., # comments for axis length
"FITS dataset may contain extensions"] # comment for EXTEND

return FITSHeader(hdu_keys, hdu_values, comments)
end
20 changes: 16 additions & 4 deletions test/runtests.jl
Expand Up @@ -155,7 +155,7 @@ using Random # for `randstring`
b_view = @view b[:,:]
read!(f[1],b_view)
@test a == b

b_view = @view b3D[:,:,:]
read!(f[2],b_view)
@test a3D == b3D
Expand All @@ -171,7 +171,7 @@ using Random # for `randstring`
# Non-contiguous views can not be read into
b .= zero(eltype(b))
b_view = @view b[1,:]
@test_throws ArgumentError read!(f[1],b_view,1,:)
@test_throws ArgumentError read!(f[1],b_view,1,:)
end

@testset "1D slices of a 3D array" begin
Expand Down Expand Up @@ -316,7 +316,7 @@ using Random # for `randstring`
rm(fname)
end
end

@testset "1D slice of a 3D array" begin
try
FITS(fname,"r+") do f
Expand All @@ -337,7 +337,7 @@ using Random # for `randstring`
FITS(fname,"r+") do f
b_view = @view b[:,1,:]
@test_throws ArgumentError write(f,b_view)

b_view = @view b[1,:,:]
@test_throws ArgumentError write(f,b_view)

Expand Down Expand Up @@ -565,6 +565,18 @@ HISTORY this is a history"""
# Clean up from last test.
close(f)
rm(fname, force=true)

# Test for default_header
data = fill(Int16(2), 5, 6, 2)
hdr = default_header(data)
@test hdr isa FITSHeader
@test hdr["SIMPLE"] == true
@test hdr["BITPIX"] == 16
@test hdr["NAXIS"] == 3
@test hdr["NAXIS1"] == 2
@test hdr["NAXIS2"] == 6
@test hdr["NAXIS3"] == 5
@test hdr["EXTEND"] == true
end

# -----------------------------------------------------------------------------
Expand Down

0 comments on commit e7f5890

Please sign in to comment.