Skip to content

Commit

Permalink
Merge fd13f51 into d6e921f
Browse files Browse the repository at this point in the history
  • Loading branch information
aquatiko committed Apr 11, 2019
2 parents d6e921f + fd13f51 commit 16cd9d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
33 changes: 25 additions & 8 deletions src/AstroImages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ using FITSIO, FileIO, Images

export load, AstroImage

_load(fits::FITS, ext) = read(fits[ext])

"""
load(fitsfile::String, n=1)
Read and return the data from `n`-th extension of the FITS file. Second argument can also
be a tuple of integers, in which case a tuple with the data of each corresponding extension
is returned.
"""
FileIO.load(f::File{format"FITS"}, ext::Int=1) = read(FITS(f.filename)[ext])
FileIO.load(f::File{format"FITS"}, ext::Int=1) = _load(FITS(f.filename), ext)

function FileIO.load(f::File{format"FITS"}, ext::NTuple{N,Int}) where {N}
fits = FITS(f.filename)
Expand Down Expand Up @@ -45,7 +45,6 @@ end

"""
AstroImage([color=Gray,] data::Matrix{Real})
Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default.
"""
AstroImage(color::Type{<:Color}, data::Matrix{T}) where {T<:Real} =
Expand All @@ -54,24 +53,42 @@ AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray}(data)

"""
AstroImage([color=Gray,] filename::String, n::Int=1)
Create an `AstroImage` object by reading the `n`-th extension from FITS file `filename`.
Use `color` as color map, this is `Gray` by default.
"""
AstroImage(color::Type{<:Color}, file::String, ext::Int=1) =
AstroImage(color::Type{<:Color}, file::String, ext::Int) =
AstroImage(color, load(file, ext))
AstroImage(file::String, ext::Int=1) = AstroImage(Gray, file, ext)
AstroImage(file::String, ext::Int) = AstroImage(Gray, file, ext)

AstroImage(color::Type{<:Color}, fits::FITS, ext::Int) =
AstroImage(color, _load(fits, ext))
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
AstroImage(Gray, fits, ext)
end

# Lazily render the image as a Matrix{Color}, upon request.
function render(img::AstroImage{T,C}) where {T,C}
imgmin, imgmax = extrema(img.data)
# Add one to maximum to work around this issue:
# https://github.com/JuliaMath/FixedPointNumbers.jl/issues/102
f = scaleminmax(_float(imgmin), _float(max(imgmax, imgmax + one(T))))
return colorview(C, f.(_float.(img.data)))
return C.(f.(_float.(img.data)))
end

Images.colorview(img::AstroImage) = render(img)
Base.convert(::Type{Matrix{C}}, img::AstroImage{T,C}) where {T,C<:Color} = render(img)

include("showmime.jl")
include("plot-recipes.jl")
Expand Down
48 changes: 44 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using AstroImages, FITSIO, Images
using AstroImages, FITSIO, Images, Random
using Test

import AstroImages: _float, render

fname = tempname() * ".fits"

@testset "Conversion to float and fixed-point" begin
@testset "Float" begin
for T in (Float16, Float32, Float64)
Expand All @@ -28,7 +30,6 @@ import AstroImages: _float, render
end

@testset "FITS and images" begin
fname = tempname() * ".fits"
for T in [UInt8, Int8, UInt16, Int16, UInt32, Int32, Int64,
Float32, Float64]
data = reshape(T[1:100;], 5, 20)
Expand All @@ -38,10 +39,49 @@ end
@test load(fname) == data
@test load(fname, (1, 1)) == (data, data)
img = AstroImage(fname)
rendered_img = colorview(img)
rendered_img = render(img)
@test iszero(minimum(rendered_img))
@test convert(Matrix{Gray}, img) == rendered_img
end
rm(fname, force=true)
end


@testset "default handler" begin
@testset "less dimensions than 2" begin
data = rand(2)
FITS(fname, "w") do f
write(f, data)
end
@test_throws ErrorException AstroImage(fname)
end

@testset "no ImageHDU" begin
## Binary table
indata = Dict{String, Array}()
i = length(indata) + 1
indata["col$i"] = [randstring(10) for j=1:20] # ASCIIString column
i += 1
indata["col$i"] = ones(Bool, 20) # Bool column
i += 1
indata["col$i"] = reshape([1:40;], (2, 20)) # vector Int64 column
i += 1
indata["col$i"] = [randstring(5) for j=1:2, k=1:20] # vector ASCIIString col
indata["vcol"] = [randstring(j) for j=1:20] # variable length column
indata["VCOL"] = [collect(1.:j) for j=1.:20.] # variable length

FITS(fname, "w") do f
write(f, indata; varcols=["vcol", "VCOL"])
@test_throws MethodError AstroImage(f)
end
end

@testset "Opening AstroImage in different ways" begin
data = rand(2,2)
FITS(fname, "w") do f
write(f, data)
end
@test AstroImage(fname, 1) isa AstroImage
@test AstroImage(Gray ,fname, 1) isa AstroImage
end
end
include("plots.jl")

0 comments on commit 16cd9d4

Please sign in to comment.