-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a method to read an image into a pre-allocated array #122
Changes from all commits
0d688e9
a4ad156
f2fbf1e
fe77cb4
82f7f1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import Base: getindex, | |
length, | ||
show, | ||
read, | ||
read!, | ||
write, | ||
close, | ||
ndims, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,91 @@ using Random # for `randstring` | |
rm(fname1, force=true) | ||
rm(fname2, force=true) | ||
end | ||
|
||
@testset "non-allocating read" begin | ||
fname1 = tempname() * ".fits" | ||
try | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that instead of using FITS(fname, "w") do f
# ...
end which will automatically close it at the end. You can find some examples in this file. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you still need this try...finally block to delete the temp file, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated the tests to use this method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kbarbary there is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
FITS(fname1, "r+") do f | ||
|
||
indata = reshape(Float32[1:400;], 20, 20) | ||
write(f, indata) | ||
|
||
# Read the entire array using read to compare with read! | ||
a = read(f[1]) | ||
|
||
# Read the entire array | ||
b = zeros(eltype(indata),size(indata)) | ||
read!(f[1],b) | ||
@test a == b | ||
read!(f[1],b,:,:) | ||
@test a == b | ||
|
||
# Read a 2D slice | ||
b = zeros(eltype(indata),2,2) | ||
read!(f[1],b,1:2,1:2) | ||
@test a[1:2,1:2] == b | ||
|
||
# Read an entire 1D slice | ||
b = zeros(eltype(indata),size(indata,1)) | ||
read!(f[1],b,:,1) | ||
@test a[:,1] == b | ||
|
||
b = zeros(eltype(indata),size(indata,2)) | ||
read!(f[1],b,1,:) | ||
@test a[1,:] == b | ||
|
||
# Read a part of a 1D slice | ||
b = zeros(eltype(indata),1) | ||
read!(f[1],b,1:1,1) | ||
@test a[1,1] == b[1] | ||
read!(f[1],b,1,1:1) | ||
@test a[1,1] == b[1] | ||
|
||
# Read a single element into a 0-dim array | ||
b = zeros(eltype(indata)) | ||
read!(f[1],b,1,1) | ||
@test a[1,1] == first(b) | ||
|
||
# Test for errors | ||
b = zeros(Float64) | ||
# Type is checked before dimensions | ||
@test_throws TypeError read!(f[1],b,1,1) | ||
@test_throws TypeError read!(f[1],b) | ||
|
||
b = zeros(eltype(indata)) | ||
@test_throws DimensionMismatch read!(f[1],b,1:10,1) | ||
@test_throws DimensionMismatch read!(f[1],b,1:10,1:10) | ||
@test_throws DimensionMismatch read!(f[1],b) | ||
@test_throws DimensionMismatch read!(f[1],b,:,1) | ||
@test_throws DimensionMismatch read!(f[1],b,1,:) | ||
@test_throws DimensionMismatch read!(f[1],b,:,:) | ||
|
||
b = zeros(eltype(indata),1) | ||
@test_throws DimensionMismatch read!(f[1],b,1,1) | ||
|
||
@testset "read into views" begin | ||
b = zeros(eltype(indata),size(indata)) | ||
|
||
# Entire array | ||
b_view = view(b,:,:) | ||
read!(f[1],b_view) | ||
@test a == b | ||
|
||
for ax2 in axes(b,2) | ||
b_view = view(b,:,ax2) | ||
read!(f[1],b_view,:,ax2) | ||
@test a[:,ax2] == b[:,ax2] | ||
end | ||
|
||
# Non-contiguous views can not be read into | ||
b_view = view(b,1,:) | ||
@test_throws MethodError read!(f[1],b_view,1,:) | ||
end | ||
end | ||
finally | ||
rm(fname1, force=true) | ||
end | ||
end | ||
end | ||
|
||
@testset "Write data to an existing image HDU" begin | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this refers to JuliaLang/julia#16717, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's what I had in mind