Skip to content

Commit

Permalink
Merge d9da8c0 into 0827e23
Browse files Browse the repository at this point in the history
  • Loading branch information
aquatiko committed Aug 5, 2019
2 parents 0827e23 + d9da8c0 commit b5fe258
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 22 deletions.
66 changes: 44 additions & 22 deletions src/AstroImages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ export load, AstroImage

_load(fits::FITS, ext::Int) = read(fits[ext])
_load(fits::FITS, ext::NTuple{N, Int}) where {N} = ntuple(i-> read(fits[ext[i]]), N)
_load(fits::NTuple{N, FITS}, ext::NTuple{N, Int}) where {N} = ntuple(i -> _load(fits[i], ext[i]), N)

_header(fits::FITS, ext::Int) = WCS.from_header(read_header(fits[ext], String))[1]
_header(fits::FITS, ext::NTuple{N, Int}) where {N} =
ntuple(i -> WCS.from_header(read_header(fits[ext[i]], String))[1], N)
_header(fits::NTuple{N, FITS}, ext::NTuple{N, Int}) where {N} =
ntuple(i -> _header(fits[i], ext[i]), N)
"""
load(fitsfile::String, n=1)
Expand All @@ -20,19 +26,47 @@ tuple with the data of each corresponding extension is returned.
function FileIO.load(f::File{format"FITS"}, ext::Int=1)
fits = FITS(f.filename)
out = _load(fits, ext)
header = WCS.from_header(read_header(fits[ext], String))[1]
header = _header(fits,ext)
close(fits)
return out, header
end

function FileIO.load(f::File{format"FITS"}, ext::NTuple{N,Int}) where {N}
fits = FITS(f.filename)
out = ntuple(i -> read(fits[ext[i]]), N)
header = ntuple(i -> WCS.from_header(read_header(fits[ext[i]], String))[1], N)
out = _load(fits, ext)
header = _header(fits, ext)
close(fits)
return out, header
end

function FileIO.load(f::NTuple{N, String}) where {N}
fits = ntuple(i-> FITS(f[i]), N)
ext = indexer(fits)
out = _load(fits, ext)
header = _header(fits, ext)
for i in 1:N
close(fits[i])
end
return out, header
end

function indexer(fits::FITS)
ext = 0
for (i, hdu) in enumerate(fits)
if hdu isa ImageHDU && length(size(hdu)) >= 2 # check if Image is atleast 2D
ext = i
break
end
end
if ext > 1
@info "Image was loaded from HDU $ext"
elseif ext == 0
error("There are no ImageHDU extensions in '$(fits.filename)'")
end
return ext
end
indexer(fits::NTuple{N, FITS}) where {N} = ntuple(i -> indexer(fits[i]), N)

# Images.jl expects data to be either a float or a fixed-point number. Here we define some
# utilities to convert all data types supported by FITS format to float or fixed-point:
#
Expand Down Expand Up @@ -80,7 +114,7 @@ Create an `AstroImage` object by reading the `n`-th extension from FITS file `fi
Use `color` as color map, this is `Gray` by default.
"""
AstroImage(color::Type{<:Color}, file::String, ext::Int) =
AstroImage(color, load(file, ext)...)
AstroImage(color, file, (ext,))
AstroImage(color::Type{<:Color}, file::String, ext::NTuple{N, Int}) where {N} =
AstroImage(color, load(file, ext)...)

Expand All @@ -91,24 +125,12 @@ AstroImage(color::Type{<:Color}, fits::FITS, ext::Int) =
AstroImage(color, _load(fits, ext), WCS.from_header(read_header(fits[ext],String))[1])
AstroImage(color::Type{<:Color}, fits::FITS, ext::NTuple{N, Int}) where {N} =
AstroImage(color, _load(fits, ext), ntuple(i -> WCS.from_header(read_header(fits[ext[i]], String))[1], N))
function AstroImage(file::String)
fits = FITS(file)
ext = 0
for (i, hdu) in enumerate(fits)
if hdu isa ImageHDU && length(size(hdu)) >= 2 # check if Image is atleast 2D
ext = i
break
end
end
if ext > 1
@info "Image was loaded from HDU $ext"
elseif ext == 0
error("There are no ImageHDU extensions in \"$file\"")
end
out = AstroImage(Gray, fits, ext)
close(fits)
return out
end
AstroImage(color::Type{<:Color}, fits::NTuple{N, FITS}, ext::NTuple{N, Int}) where {N} =
AstroImage(color, ntuple(i -> _load(fits[i], ext[i]), N), ntuple(i -> WCS.from_header(read_header(fits[i][ext[i]], String))[1], N))

AstroImage(files::NTuple{N,String}) where {N} =
AstroImage(Gray, load(files)...)
AstroImage(file::String) = AstroImage((file,))

# Lazily render the image as a Matrix{Color}, upon request.
function render(img::AstroImage{T,C,N}, header_number = 1) where {T,C,N}
Expand Down
57 changes: 57 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,16 @@ end

@testset "Opening AstroImage in different ways" begin
data = rand(2,2)
wcs = WCSTransform(2;)
FITS(fname, "w") do f
write(f, data)
end
@test AstroImage(fname, 1) isa AstroImage
@test AstroImage(Gray ,fname, 1) isa AstroImage
@test AstroImage(Gray, FITS(fname), 1) isa AstroImage
@test AstroImage(data, wcs) isa AstroImage
@test AstroImage((data,data), (wcs,wcs)) isa AstroImage
@test AstroImage(Gray, data, wcs) isa AstroImage
end

@testset "Image HDU is not at 1st position" begin
Expand Down Expand Up @@ -179,4 +184,56 @@ end
@test WCS.to_header(img.wcs[2]) === WCS.to_header(WCS.from_header(read_header(FITS(fname)[2], String))[1])
end

@testset "multi file AstroImage" begin
fname1 = tempname() * ".fits"
f = FITS(fname1, "w")
inhdr = FITSHeader(["CTYPE1", "CTYPE2", "RADESYS", "FLTKEY", "INTKEY", "BOOLKEY", "STRKEY", "COMMENT",
"HISTORY"],
["RA---TAN", "DEC--TAN", "UNK", 1.0, 1, true, "string value", nothing, nothing],
["",
"",
"",
"floating point keyword",
"",
"boolean keyword",
"string value",
"this is a comment",
"this is a history"])

indata1 = reshape(Int64[1:100;], 5, 20)
write(f, indata1; header=inhdr)
close(f)

fname2 = tempname() * ".fits"
f = FITS(fname2, "w")
indata2 = reshape(Int[1:100;], 5, 20)
write(f, indata2; header=inhdr)
close(f)

fname3 = tempname() * ".fits"
f = FITS(fname3, "w")
indata3 = reshape(Int[1:100;], 5, 20)
write(f, indata3; header=inhdr)
close(f)

img = AstroImage((fname1, fname2, fname3))

@test length(img.data) == length(img.wcs) == 3
@test img.data[1] == indata1
@test img.data[2] == indata2
@test img.data[3] == indata3
@test WCS.to_header(img.wcs[1]) == WCS.to_header(img.wcs[2]) ==
WCS.to_header(img.wcs[3]) == WCS.to_header(WCS.from_header(read_header(FITS(fname1)[1], String))[1])
@test eltype(eltype(img.data)) == Int

img = AstroImage(Gray, (FITS(fname1), FITS(fname2), FITS(fname3)), (1,1,1))
@test length(img.data) == length(img.wcs) == 3
@test img.data[1] == indata1
@test img.data[2] == indata2
@test img.data[3] == indata3
@test WCS.to_header(img.wcs[1]) == WCS.to_header(img.wcs[2]) ==
WCS.to_header(img.wcs[3]) == WCS.to_header(WCS.from_header(read_header(FITS(fname1)[1], String))[1])
@test eltype(eltype(img.data)) == Int
end

include("plots.jl")

0 comments on commit b5fe258

Please sign in to comment.