Skip to content

Commit

Permalink
Merge 7f8c0ca into b83d8d6
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed May 15, 2017
2 parents b83d8d6 + 7f8c0ca commit 226e37f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
15 changes: 15 additions & 0 deletions src/FITSIO.jl
Expand Up @@ -101,6 +101,21 @@ type FITS
end
end

"""
FITS(f::Function, args...)
Apply the function `f` to the result of `FITS(args...)` and close the resulting file
descriptor upon completion.
"""
function FITS(f::Function, args...)
io = FITS(args...)
try
f(io)
finally
close(io)
end
end

# FITSHeader
#
# An in-memory representation of the header of an HDU. It stores the
Expand Down
62 changes: 31 additions & 31 deletions test/runtests.jl
Expand Up @@ -8,40 +8,40 @@ import Compat.String

# Create a FITS instance and loop over supported types.
fname = tempname() * ".fits"
f = FITS(fname, "w")
for T in [UInt8, Int8, UInt16, Int16, UInt32, Int32, Int64,
Float32, Float64]
indata = reshape(T[1:100;], 5, 20)

# Test writing the data to a new extension
write(f, indata)

# test reading the full array
outdata = read(f[end])
@test indata == outdata
@test eltype(indata) == eltype(outdata)

# test reading subsets of the array
@test read(f[end], :, :) == indata
@test read(f[end], 4, 1:10) == indata[4, 1:10] # 2-d array
@test read(f[end], :, 4) == indata[:, 4] # 1-d array
@test read(f[end], 2, 3) == indata[2, 3] # scalar
@test read(f[end], :, 1:2:10) == indata[:, 1:2:10]
@test read(f[end], 1:3, :) == indata[1:3, :]

# test expected errors
@test_throws DimensionMismatch read(f[end], :)
@test_throws DimensionMismatch read(f[end], :, :, 1)
@test_throws BoundsError read(f[end], 1:6, :)
@test_throws BoundsError read(f[end], 1, 0)
FITS(fname, "w") do f
for T in [UInt8, Int8, UInt16, Int16, UInt32, Int32, Int64,
Float32, Float64]
indata = reshape(T[1:100;], 5, 20)

# Test writing the data to a new extension
write(f, indata)

# test reading the full array
outdata = read(f[end])
@test indata == outdata
@test eltype(indata) == eltype(outdata)

# test reading subsets of the array
@test read(f[end], :, :) == indata
@test read(f[end], 4, 1:10) == indata[4, 1:10] # 2-d array
@test read(f[end], :, 4) == indata[:, 4] # 1-d array
@test read(f[end], 2, 3) == indata[2, 3] # scalar
@test read(f[end], :, 1:2:10) == indata[:, 1:2:10]
@test read(f[end], 1:3, :) == indata[1:3, :]

# test expected errors
@test_throws DimensionMismatch read(f[end], :)
@test_throws DimensionMismatch read(f[end], :, :, 1)
@test_throws BoundsError read(f[end], 1:6, :)
@test_throws BoundsError read(f[end], 1, 0)

end
end

# test iteration
for hdu in f
@test size(hdu) == (5, 20)
# test iteration
for hdu in f
@test size(hdu) == (5, 20)
end
end
close(f)
if isfile(fname)
rm(fname)
end
Expand Down

0 comments on commit 226e37f

Please sign in to comment.