Skip to content

Commit

Permalink
Merge fae7edb into 22fc0bf
Browse files Browse the repository at this point in the history
  • Loading branch information
mweastwood committed Jul 5, 2018
2 parents 22fc0bf + fae7edb commit 6725ef2
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 213 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,7 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- nightly
notifications:
email: false
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
@@ -1,3 +1,3 @@
julia 0.6
julia 0.7.0-beta
BinaryProvider 0.3.0
Compat 0.41.0
1 change: 1 addition & 0 deletions deps/.gitignore
@@ -0,0 +1 @@
build.log
6 changes: 3 additions & 3 deletions src/FITSIO.jl
@@ -1,8 +1,9 @@
isdefined(Base, :__precompile__) && __precompile__()
__precompile__()


module FITSIO

using Compat.Printf
using Printf

export FITS,
HDU,
Expand All @@ -26,7 +27,6 @@ import Base: getindex,
close,
ndims,
size,
endof,
haskey,
keys,
values,
Expand Down
12 changes: 6 additions & 6 deletions src/fits.jl
Expand Up @@ -39,7 +39,7 @@ function length(f::FITS)
Int(fits_get_num_hdus(f.fitsfile))
end

endof(f::FITS) = length(f)
Base.lastindex(f::FITS) = length(f)

# Iteration
start(f::FITS) = 1
Expand All @@ -60,19 +60,19 @@ function show(io::IO, f::FITS)
else
print(io, "HDUs: ")

names = Vector{String}(nhdu)
vers = Vector{String}(nhdu)
types = Vector{String}(nhdu)
names = Vector{String}(undef, nhdu)
vers = Vector{String}(undef, nhdu)
types = Vector{String}(undef, nhdu)
for i = 1:nhdu
t = fits_movabs_hdu(f.fitsfile, i)
types[i] = (t == :image_hdu ? "Image" :
t == :binary_table ? "Table" :
t == :ascii_table ? "ASCIITable" :
error("unknown HDU type"))
nname = fits_try_read_extname(f.fitsfile)
names[i] = get(nname, "")
names[i] = something(nname, "")
nver = fits_try_read_extver(f.fitsfile)
vers[i] = isnull(nver) ? "" : string(get(nver))
vers[i] = nver === nothing ? "" : string(nver)
end

nums = [string(i) for i=1:nhdu]
Expand Down
74 changes: 37 additions & 37 deletions src/header.jl
Expand Up @@ -9,12 +9,12 @@
function try_parse_hdrval(::Type{Bool}, s::String)
if length(s) == 1
if s[1] == 'T'
return Nullable(true)
return true
elseif s[1] == 'F'
return Nullable(false)
return false
end
end
return Nullable{Bool}()
return nothing
end

# Note that trailing whitespace is not significant in FITS header
Expand All @@ -24,39 +24,39 @@ end
# TODO: parse '' within the string as a single '.
function try_parse_hdrval(::Type{String}, s::String)
if length(s) < 2 || s[1] != '\'' || s[end] != '\''
return Nullable{String}()
return nothing
end

i = endof(s) - 1
i = lastindex(s) - 1
while i > 2
if s[i] != ' '
return Nullable(s[2:i])
return s[2:i]
end
i -= 1
end
return Nullable(s[2:i])
return s[2:i]
end

try_parse_hdrval(::Type{Float64}, s::String) = tryparse(Float64, s)
try_parse_hdrval(::Type{Int}, s::String) = tryparse(Int, s)

# Try to parse the header value as any type
function try_parse_hdrval(s::String)
length(s) == 0 && return Nullable(nothing)
length(s) == 0 && return Some(nothing)

nb = try_parse_hdrval(Bool, s)
isnull(nb) || return nb
nb === nothing || return nb

ns = try_parse_hdrval(String, s)
isnull(ns) || return ns
ns === nothing || return ns

ni = try_parse_hdrval(Int, s)
isnull(ni) || return ni
ni === nothing || return ni

nf = try_parse_hdrval(Float64, s)
isnull(nf) || return nf
nf === nothing || return nf

return Nullable{Any}()
return nothing
end

# functions for displaying header values in show(io, header)
Expand All @@ -68,17 +68,17 @@ hdrval_repr(v::Union{AbstractFloat, Integer}) = string(v)
# (never error)
function parse_header_val(s::String)
nval = try_parse_hdrval(s)
return isnull(nval) ? s : get(nval)
return something(nval, s)
end

# Try to read the raw keys in order given; returns Nullable.
# (null if no key exists or if parsing an existing key is unsuccessful.)
function fits_try_read_keys{T}(f::FITSFile, ::Type{T}, keys)
# Try to read the raw keys in order given
# (`nothing` if no key exists or if parsing an existing key is unsuccessful.)
function fits_try_read_keys(f::FITSFile, ::Type{T}, keys) where T
status = Cint[0]
value = Vector{UInt8}(71)
value = Vector{UInt8}(undef, 71)
for key in keys
ccall((:ffgkey, libcfitsio), Cint,
(Ptr{Void},Ptr{UInt8},Ptr{UInt8},Ptr{UInt8},Ptr{Cint}),
(Ptr{Cvoid},Ptr{UInt8},Ptr{UInt8},Ptr{UInt8},Ptr{Cint}),
f.ptr, key, value, C_NULL, status)

# If the key is found, return it. If there was some other error
Expand All @@ -89,7 +89,7 @@ function fits_try_read_keys{T}(f::FITSFile, ::Type{T}, keys)
error(fits_get_errstatus(status[1]))
end
end
return Nullable{T}()
return nothing
end

# Build a string with extension keywords, if present.
Expand All @@ -103,9 +103,9 @@ fits_try_read_extver(f::FITSFile) = fits_try_read_keys(f, Int, EXTVER_KEYS)
function fits_get_ext_info_string(f::FITSFile)
extname = fits_try_read_extname(f)
extver = fits_try_read_extver(f)
if !isnull(extname) && !isnull(extver)
if extname !== nothing && extver !== nothing
return " (name=$(repr(get(extname))), ver=$(get(extver)))"
elseif !isnull(extname)
elseif extname !== nothing
return " (name=$(repr(get(extname))))"
end
return ""
Expand Down Expand Up @@ -133,18 +133,18 @@ function reserved_key_indices(hdr::FITSHeader)
# Note that this removes anything matching NAXIS\d regardless of # of axes.
if in("NAXIS", hdr.keys)
for i=1:nhdr
if ismatch(r"^NAXIS\d*$", hdr.keys[i])
if occursin(r"^NAXIS\d*$", hdr.keys[i])
push!(indices, i)
end
end
end

if in("ZNAXIS", hdr.keys)
for i=1:nhdr
if (ismatch(r"^ZNAXIS\d*$", hdr.keys[i]) ||
ismatch(r"^ZTILE\d*$", hdr.keys[i]) ||
ismatch(r"^ZNAME\d*$", hdr.keys[i]) ||
ismatch(r"^ZVAL\d*$", hdr.keys[i]))
if (occursin(r"^ZNAXIS\d*$", hdr.keys[i]) ||
occursin(r"^ZTILE\d*$", hdr.keys[i]) ||
occursin(r"^ZNAME\d*$", hdr.keys[i]) ||
occursin(r"^ZVAL\d*$", hdr.keys[i]))
push!(indices, i)
end
end
Expand All @@ -158,7 +158,7 @@ function reserved_key_indices(hdr::FITSHeader)
r"^TDMAX\d*$", r"^TDESC\d*$", r"^TROTA\d*$",
r"^TRPIX\d*$", r"^TRVAL\d*$", r"^TDELT\d*$",
r"^TCUNI\d*$", r"^TFIELDS$"]
if ismatch(re, hdr.keys[i])
if occursin(re, hdr.keys[i])
push!(indices, i)
end
end
Expand Down Expand Up @@ -226,8 +226,8 @@ keyword does not already exist, a new record will be appended at the
end of the header.
"""
function write_key(hdu::HDU, key::String,
value::Union{String, Bool, Integer, AbstractFloat, Void},
comment::Union{String, Ptr{Void}}=C_NULL)
value::Union{String, Bool, Integer, AbstractFloat, Nothing},
comment::Union{String, Ptr{Cvoid}}=C_NULL)
fits_assert_open(hdu.fitsfile)
fits_movabs_hdu(hdu.fitsfile, hdu.ext)
fits_update_key(hdu.fitsfile, key, value, comment)
Expand All @@ -250,20 +250,20 @@ function read_header(hdu::HDU)

# Below, we use a direct call to ffgkyn so that we can keep reusing the
# same buffers.
key = Vector{UInt8}(81)
value = Vector{UInt8}(81)
comment = Vector{UInt8}(81)
key = Vector{UInt8}(undef, 81)
value = Vector{UInt8}(undef, 81)
comment = Vector{UInt8}(undef, 81)
status = Cint[0]

nkeys, morekeys = fits_get_hdrspace(hdu.fitsfile)

# Initialize output arrays
keys = Vector{String}(nkeys)
values = Vector{Any}(nkeys)
comments = Vector{String}(nkeys)
keys = Vector{String}(undef, nkeys)
values = Vector{Any}(undef, nkeys)
comments = Vector{String}(undef, nkeys)
for i=1:nkeys
ccall((:ffgkyn,libcfitsio), Cint,
(Ptr{Void},Cint,Ptr{UInt8},Ptr{UInt8},Ptr{UInt8},Ptr{Cint}),
(Ptr{Cvoid},Cint,Ptr{UInt8},Ptr{UInt8},Ptr{UInt8},Ptr{Cint}),
hdu.fitsfile.ptr, i, key, value, comment, status)
keys[i] = unsafe_string(pointer(key))
values[i] = parse_header_val(unsafe_string(pointer(value)))
Expand Down
34 changes: 17 additions & 17 deletions src/image.jl
Expand Up @@ -64,9 +64,9 @@ Get total number of pixels in image (product of ``size(hdu)``).
"""
length(hdu::ImageHDU) = prod(size(hdu))

# `endof` is needed so that hdu[:] can throw DimensionMismatch
# `lastindex` is needed so that hdu[:] can throw DimensionMismatch
# when ndim != 1, rather than no method.
endof(hdu::ImageHDU) = length(hdu::ImageHDU)
Base.lastindex(hdu::ImageHDU) = length(hdu::ImageHDU)

# Read a full image from an HDU
"""
Expand All @@ -82,23 +82,23 @@ function read(hdu::ImageHDU)
fits_movabs_hdu(hdu.fitsfile, hdu.ext)
sz = fits_get_img_size(hdu.fitsfile)
bitpix = fits_get_img_equivtype(hdu.fitsfile)
data = Array{TYPE_FROM_BITPIX[bitpix]}(sz...)
data = Array{TYPE_FROM_BITPIX[bitpix]}(undef, sz...)
fits_read_pix(hdu.fitsfile, data)
data
end

# _checkbounds methods copied from Julia v0.4 Base.
_checkbounds(sz, i::Integer) = 1 <= i <= sz
_checkbounds(sz, i::Colon) = true
_checkbounds(sz, r::Range{Int}) =
_checkbounds(sz, r::AbstractRange{Int}) =
(isempty(r) || (minimum(r) >= 1 && maximum(r) <= sz))

# helper functions for constructing cfitsio indexing vectors in read(hdu, ...)
_first(i::Union{Integer, Range}) = first(i)
_first(i::Union{Integer, AbstractRange}) = first(i)
_first(::Colon) = 1
_last(sz, i::Union{Integer, Range}) = last(i)
_last(sz, i::Union{Integer, AbstractRange}) = last(i)
_last(sz, ::Colon) = sz
_step(r::Range) = step(r)
_step(r::AbstractRange) = step(r)
_step(::Union{Integer, Colon}) = 1

# Shape of array to create for read(hdu, ...), dropping trailing
Expand All @@ -107,16 +107,16 @@ _step(::Union{Integer, Colon}) = 1
@inline _index_shape(sz, I...) = _index_shape_dim(sz, 1, I...)
_index_shape_dim(sz, dim, I::Integer...) = ()
_index_shape_dim(sz, dim, ::Colon) = (sz[dim],)
_index_shape_dim(sz, dim, r::Range) = (length(r),)
_index_shape_dim(sz, dim, r::AbstractRange) = (length(r),)
@inline _index_shape_dim(sz, dim, ::Colon, I...) =
tuple(sz[dim], _index_shape_dim(sz, dim+1, I...)...)
@inline _index_shape_dim(sz, dim, ::Integer, I...) =
_index_shape_dim(sz, dim+1, I...)
@inline _index_shape_dim(sz, dim, r::Range, I...) =
@inline _index_shape_dim(sz, dim, r::AbstractRange, I...) =
tuple(length(r), _index_shape_dim(sz, dim+1, I...)...)

# Read a subset of an ImageHDU
function read_internal(hdu::ImageHDU, I::Union{Range{Int}, Integer, Colon}...)
function read_internal(hdu::ImageHDU, I::Union{AbstractRange{Int}, Integer, Colon}...)
fits_assert_open(hdu.fitsfile)
fits_movabs_hdu(hdu.fitsfile, hdu.ext)
sz = fits_get_img_size(hdu.fitsfile)
Expand All @@ -138,14 +138,14 @@ function read_internal(hdu::ImageHDU, I::Union{Range{Int}, Integer, Colon}...)

# construct output array
bitpix = fits_get_img_equivtype(hdu.fitsfile)
data = Array{TYPE_FROM_BITPIX[bitpix]}(_index_shape(sz, I...))
data = Array{TYPE_FROM_BITPIX[bitpix]}(undef, _index_shape(sz, I...))

fits_read_subset(hdu.fitsfile, firsts, lasts, steps, data)
data
end

# general method and version that returns a single value rather than 0-d array
read(hdu::ImageHDU, I::Union{Range{Int}, Int, Colon}...) =
read(hdu::ImageHDU, I::Union{AbstractRange{Int}, Int, Colon}...) =
read_internal(hdu, I...)
read(hdu::ImageHDU, I::Int...) = read_internal(hdu, I...)[1]

Expand All @@ -159,10 +159,10 @@ following array element types are supported: `UInt8`, `Int8`,
`Float64`. If a `FITSHeader` object is passed as the `header` keyword
argument, the header will also be added to the new HDU.
"""
function write{T}(f::FITS, data::Array{T};
header::Union{Void, FITSHeader}=nothing,
name::Union{Void, String}=nothing,
ver::Union{Void, Integer}=nothing)
function write(f::FITS, data::Array{T};
header::Union{Nothing, FITSHeader}=nothing,
name::Union{Nothing, String}=nothing,
ver::Union{Nothing, Integer}=nothing) where T
fits_assert_open(f.fitsfile)
s = size(data)
fits_create_img(f.fitsfile, T, [s...])
Expand Down Expand Up @@ -215,7 +215,7 @@ Same as above but only copy odd columns in y:
copy_section(hdu, f, 1:200, 1:2:200)
```
"""
function copy_section(hdu::ImageHDU, dest::FITS, r::Range{Int}...)
function copy_section(hdu::ImageHDU, dest::FITS, r::AbstractRange{Int}...)
fits_assert_open(hdu.fitsfile)
fits_assert_open(dest.fitsfile)
fits_movabs_hdu(hdu.fitsfile, hdu.ext)
Expand Down

0 comments on commit 6725ef2

Please sign in to comment.