Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaAstro / FITSIO.jl / 166

26 Jul 2017 - 19:32 coverage: 72.418%. Remained the same
166

Pull #77

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Minor change
Pull Request #77: [WIP] Add Documenter to build documentation

83 of 102 new or added lines in 5 files covered. (81.37%)

68 existing lines in 5 files now uncovered.

554 of 765 relevant lines covered (72.42%)

149.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.75
/src/image.jl
1
# ImageHDU methods
2

3
# Display the image datatype and dimensions
4
function show(io::IO, hdu::ImageHDU)
5
    fits_assert_open(hdu.fitsfile)
!
6
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
!
7
    bitpix = fits_get_img_type(hdu.fitsfile)
!
8
    equivbitpix = fits_get_img_equivtype(hdu.fitsfile)
!
9
    sz = fits_get_img_size(hdu.fitsfile)
!
10

11
    if bitpix == equivbitpix
!
12
        datainfo = string(TYPE_FROM_BITPIX[equivbitpix])
!
13
    else
14
        datainfo = @sprintf("%s (physical: %s)",
!
15
                            TYPE_FROM_BITPIX[equivbitpix],
16
                            TYPE_FROM_BITPIX[bitpix])
17
    end
18

19
    print(io, """
!
20
    File: $(fits_file_name(hdu.fitsfile))
21
    HDU: $(hdu.ext)$(fits_get_ext_info_string(hdu.fitsfile))
22
    Type: Image
23
    Datatype: $datainfo
24
    Datasize: $(tuple(sz...))
25
    """)
26
end
27

28
# Get image dimensions
29
function ndims(hdu::ImageHDU)
30
    fits_assert_open(hdu.fitsfile)
!
31
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
!
32
    fits_get_img_dim(hdu.fitsfile)
!
33
end
34

35
# Get image size
36
function size(hdu::ImageHDU)
37
    fits_assert_open(hdu.fitsfile)
45×
38
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
63×
39
    sz = fits_get_img_size(hdu.fitsfile)
63×
40
    tuple(sz...)
63×
41
end
42

43
function size(hdu::ImageHDU, i::Integer)
44
    fits_assert_open(hdu.fitsfile)
!
45
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
!
46
    sz = fits_get_img_size(hdu.fitsfile)
!
47
    sz[i]
!
48
end
49

50
# `endof` is needed so that hdu[:] can throw DimensionMismatch
51
# when ndim != 1, rather than no method.
52
length(hdu::ImageHDU) = prod(size(hdu))
!
53
endof(hdu::ImageHDU) = length(hdu::ImageHDU)
!
54

55
# Read a full image from an HDU
56
function read(hdu::ImageHDU)
57
    fits_assert_open(hdu.fitsfile)
65×
58
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
91×
59
    sz = fits_get_img_size(hdu.fitsfile)
91×
60
    bitpix = fits_get_img_equivtype(hdu.fitsfile)
91×
61
    data = Array{TYPE_FROM_BITPIX[bitpix]}(sz...)
91×
62
    fits_read_pix(hdu.fitsfile, data)
91×
63
    data
91×
64
end
65

66
# _checkbounds methods copied from Julia v0.4 Base.
67
_checkbounds(sz, i::Integer) = 1 <= i <= sz
270×
68
_checkbounds(sz, i::Colon) = true
all except 166.1 and 166.4 - 135×
69
_checkbounds(sz, r::Range{Int}) =
180×
70
    (isempty(r) || (minimum(r) >= 1 && maximum(r) <= sz))
71

72
# helper functions for constructing cfitsio indexing vectors in read(hdu, ...)
73
_first(i::Union{Integer, Range}) = first(i)
315×
74
_first(::Colon) = 1
all except 166.1 and 166.4 - 135×
75
_last(sz, i::Union{Integer, Range}) = last(i)
315×
76
_last(sz, ::Colon) = sz
225×
77
_step(r::Range) = step(r)
135×
78
_step(::Union{Integer, Colon}) = 1
all except 166.1 and 166.4 - 243×
79

80
# Shape of array to create for read(hdu, ...), dropping trailing
81
# scalars. This is simpler than in Base because we are guaranteed that
82
# length(I) == length(sz).
83
@inline _index_shape(sz, I...) = _index_shape_dim(sz, 1, I...)
270×
84
_index_shape_dim(sz, dim, I::Integer...) = ()
all except 166.1 and 166.4 - 54×
85
_index_shape_dim(sz, dim, ::Colon) = (sz[dim],)
90×
86
_index_shape_dim(sz, dim, r::Range) = (length(r),)
90×
87
@inline _index_shape_dim(sz, dim, ::Colon, I...) =
135×
88
    tuple(sz[dim], _index_shape_dim(sz, dim+1, I...)...)
89
if VERSION >= v"0.5.0-dev"  # In Julia v0.5+ drop scalar-indexed dimensions.
90
    @inline _index_shape_dim(sz, dim, ::Integer, I...) =
45×
91
        _index_shape_dim(sz, dim+1, I...)
92
else
93
    @inline _index_shape_dim(sz, dim, ::Integer, I...) =
!
94
        tuple(1, _index_shape_dim(sz, dim+1, I...)...)
95
end
96
@inline _index_shape_dim(sz, dim, r::Range, I...) =
45×
97
    tuple(length(r), _index_shape_dim(sz, dim+1, I...)...)
98

99
# Read a subset of an ImageHDU
100
function read_internal(hdu::ImageHDU, I::Union{Range{Int}, Integer, Colon}...)
101
    fits_assert_open(hdu.fitsfile)
450×
102
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
630×
103
    sz = fits_get_img_size(hdu.fitsfile)
630×
104

105
    # check number of indices and bounds. Note that number of indices and
106
    # array dimension must match, unlike in Arrays. Array-like behavior could
107
    # be supported in the future with care taken in constructing first, last,
108
    if length(I) != length(sz)
630×
109
        throw(DimensionMismatch("number of indices must match dimensions"))
126×
110
    end
111
    for i=1:length(sz)
1,980×
112
        _checkbounds(sz[i], I[i]) || throw(BoundsError())
3,177×
113
    end
114

115
    # construct first, last and step vectors
116
    firsts = Clong[_first(idx) for idx in I]
378×
117
    lasts = Clong[_last(sz[i], I[i]) for i=1:length(sz)]
6,264×
118
    steps = Clong[_step(idx) for idx in I]
378×
119

120
    # construct output array
121
    bitpix = fits_get_img_equivtype(hdu.fitsfile)
378×
122
    data = Array{TYPE_FROM_BITPIX[bitpix]}(_index_shape(sz, I...))
378×
123

124
    fits_read_subset(hdu.fitsfile, firsts, lasts, steps, data)
378×
125
    data
378×
126
end
127

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

133
# Add a new ImageHDU to a FITS object
134
# The following Julia data types are supported for writing images by cfitsio:
135
# Uint8, Int8, Uint16, Int16, Uint32, Int32, Int64, Float32, Float64
136
function write{T}(f::FITS, data::Array{T};
137
                  header::Union{Void, FITSHeader}=nothing,
138
                  name::Union{Void, String}=nothing,
139
                  ver::Union{Void, Integer}=nothing)
140
    fits_assert_open(f.fitsfile)
115×
141
    s = size(data)
91×
142
    fits_create_img(f.fitsfile, T, [s...])
91×
143
    if isa(header, FITSHeader)
111×
144
        fits_write_header(f.fitsfile, header, true)
27×
145
    end
only 166.1 and 166.4 - 258×
146
    if isa(name, String)
117×
UNCOV
147
        fits_update_key(f.fitsfile, "EXTNAME", name)
!
148
    end
149
    if isa(ver, Integer)
117×
UNCOV
150
        fits_update_key(f.fitsfile, "EXTVER", ver)
!
151
    end
152
    fits_write_pix(f.fitsfile, ones(Int, length(s)), length(data), data)
91×
153
    nothing
91×
154
end
155

156
# Copy a rectangular section of an image and write it to a new FITS
157
# primary image or image extension. The new image HDU is appended to
158
# the end of the output file; all the keywords in the input image will
159
# be copied to the output image. The common WCS keywords will be
160
# updated if necessary to correspond to the coordinates of the section.
161

162
# convert a range to a string that cfitsio understands
163
cfitsio_range_string(r::UnitRange) = @sprintf "%d:%d" first(r) last(r)
15×
164
cfitsio_range_string(r::StepRange) =
5×
UNCOV
165
    @sprintf "%d:%d:%d" first(r) last(r) step(r)
!
166

167
function copy_section(hdu::ImageHDU, dest::FITS, r::Range{Int}...)
168
    fits_assert_open(hdu.fitsfile)
10×
169
    fits_assert_open(dest.fitsfile)
14×
170
    fits_movabs_hdu(hdu.fitsfile, hdu.ext)
14×
171
    fits_copy_image_section(hdu.fitsfile, dest.fitsfile,
46×
172
                            join([cfitsio_range_string(ri) for ri in r], ','))
173
end
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc