Skip to content

Commit 6a253fb

Browse files
committed
tests and dependencies added
1 parent 25fe404 commit 6a253fb

File tree

5 files changed

+152
-25
lines changed

5 files changed

+152
-25
lines changed

REQUIRE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
julia 1.1.0
2+
FITSIO 0.14.0
3+
WCS 0.5.0

src/Reproject.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
__precompile__()
2+
13
module Reproject
24

3-
greet() = print("Hello World!")
5+
using FITSIO, WCS
6+
7+
include("parsers.jl")
8+
9+
export
10+
parse_input_data,
11+
parse_output_projection
412

513
end # module
14+

src/parser.jl renamed to src/parsers.jl

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ function parse_input_data(input_data::ImageHDU)
55
return read(input_data), WCS.from_header(read_header(input_data, String))[1]
66
end
77

8-
function parse_input_data(input_data::String, hdu_in = nothing)
8+
function parse_input_data(input_data::String; hdu_in = nothing)
99
return parse_input_data(FITS(input_data), hdu_in = hdu_in)
1010
end
1111

12-
function parse_input_data(input_data::FITS, hdu_in = nothing)
13-
if hdu_in == nothing
12+
function parse_input_data(input_data::FITS; hdu_in = nothing)
13+
if isnothing(hdu_in)
1414
if length(input_data) > 1
15-
throw(ArgumentError("More than one HDU is present, please specify HDU to use with ``hdu_in=`` option"))
15+
throw(ArgumentError("More than one HDU is present, please specify which HDU to use with 'hdu_in' option"))
1616
else
1717
hdu_in = 1
1818
end
1919
end
2020
return parse_input_data(input_data[hdu_in])
2121
end
2222

23-
function parse_input_data(input_data, hdu_in = nothing)
24-
throw(TypeError(input_data, "should be in ", Union{String, FITS, ImageHDU, Tuple{ImageHDU,FITSHeader}}))
23+
function parse_input_data(input_data; hdu_in = nothing)
24+
throw(ArgumentError("Input should be in Union{String, FITS, ImageHDU}"))
2525
end
2626

2727
# TODO: extend support for passing FITSHeader when FITSHeader to WCSTransform support is possible.
@@ -30,20 +30,19 @@ end
3030
"""
3131
Parse output projection and returns a WCS object and Shape of output
3232
"""
33-
function parse_output_projection(output_projection::WCSTransform, shape_out = nothing)
34-
wcs_out = output_projection
35-
if shape_out isa nothing
33+
function parse_output_projection(output_projection::WCSTransform; shape_out = nothing)
34+
if isnothing(shape_out)
3635
throw(ArgumentError("Need to specify shape when specifying output_projection as WCS object"))
3736
elseif length(shape_out) == 0
3837
throw(DomainError(shape_out, "The shape of the output image should not be an empty tuple"))
3938
end
4039

41-
return wcs_out, shape_out
40+
return output_projection, shape_out
4241
end
4342

44-
function parse_output_projection(output_projection::ImageHDU, shape_out = nothing)
45-
wcs_out = WCS.from_header(read_header(output_projection))[1]
46-
if shape_out == nothing
43+
function parse_output_projection(output_projection::ImageHDU; shape_out = nothing)
44+
wcs_out = WCS.from_header(read_header(output_projection, String))[1]
45+
if isnothing(shape_out)
4746
shape_out = size(output_projection)
4847
end
4948
if length(shape_out) == 0
@@ -52,23 +51,25 @@ function parse_output_projection(output_projection::ImageHDU, shape_out = nothin
5251
return wcs_out, shape_out
5352
end
5453

55-
function parse_output_projection(output_projection::String, hdu_number = nothing)
54+
function parse_output_projection(output_projection::String; hdu_number = nothing)
5655
hdu_list = FITS(output_projection)
57-
if hdu_number == nothing
58-
wcs_out = WCS.from_header(read_header(hdu_list[1], String))
56+
if isnothing(hdu_number)
57+
wcs_out = WCS.from_header(read_header(hdu_list[1], String))[1]
58+
hdu_number = 1
5959
else
60-
wcs_out = WCS.from_header(read_header(hdu_list[hdu_number], String))
60+
wcs_out = WCS.from_header(read_header(hdu_list[hdu_number], String))[1]
6161
end
6262

63-
try
64-
shape_out = size(output_projection)
65-
catch
63+
if hdu_list[hdu_number] isa ImageHDU
64+
shape_out = size(hdu_list[hdu_number])
65+
else
6666
throw(ArgumentError("Given FITS file doesn't have ImageHDU"))
6767
end
6868

6969
return wcs_out, shape_out
7070
end
7171

72-
function parse_output_projection(output_projection, shape_out = nothing)
73-
throw(TypeError(output_projection, "should be in ", Union{String, WCSTransform, FITSHeader}))
74-
end
72+
function parse_output_projection(output_projection; shape_out = nothing)
73+
throw(ArgumentError("output_projection should be in Union{String, WCSTransform, FITSHeader}"))
74+
end
75+

test/parsers.jl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
@testset "input parser" begin
2+
fname = tempname() * ".fits"
3+
f = FITS(fname, "w")
4+
inhdr = FITSHeader(["FLTKEY", "INTKEY", "BOOLKEY", "STRKEY", "COMMENT",
5+
"HISTORY"],
6+
[1.0, 1, true, "string value", nothing, nothing],
7+
["floating point keyword",
8+
"",
9+
"boolean keyword",
10+
"string value",
11+
"this is a comment",
12+
"this is a history"])
13+
14+
indata = reshape(Float32[1:100;], 5, 20)
15+
write(f, indata; header=inhdr)
16+
17+
@testset "ImageHDU type" begin
18+
result = parse_input_data(f[1])
19+
@test result[1] isa Array
20+
@test result[2] isa WCSTransform
21+
end
22+
23+
@testset "Single HDU FITS file" begin
24+
result = parse_input_data(f)
25+
@test result[1] isa Array
26+
@test result[2] isa WCSTransform
27+
end
28+
close(f)
29+
30+
@testset "String filename input" begin
31+
result = parse_input_data(fname)
32+
@test result[1] isa Array
33+
@test result[2] isa WCSTransform
34+
end
35+
36+
f = FITS(fname, "w")
37+
write(f, indata; header=inhdr)
38+
write(f, indata; header=inhdr)
39+
40+
@testset "Multiple HDU FITS file" begin
41+
42+
@test_throws ArgumentError parse_input_data(f)
43+
44+
result = parse_input_data(f, hdu_in = 2)
45+
@test result[1] isa Array
46+
@test result[2] isa WCSTransform
47+
48+
close(f)
49+
result = parse_input_data(fname, hdu_in = 1)
50+
@test result[1] isa Array
51+
@test result[2] isa WCSTransform
52+
@test_throws ArgumentError parse_input_data(fname)
53+
end
54+
55+
@testset "invalid input" begin
56+
@test_throws ArgumentError parse_input_data(1:40, hdu_in = 12)
57+
end
58+
59+
end
60+
61+
@testset "output parser" begin
62+
fname = tempname() * ".fits"
63+
f = FITS(fname, "w")
64+
inhdr = FITSHeader(["FLTKEY", "INTKEY", "BOOLKEY", "STRKEY", "COMMENT",
65+
"HISTORY"],
66+
[1.0, 1, true, "string value", nothing, nothing],
67+
["floating point keyword",
68+
"",
69+
"boolean keyword",
70+
"string value",
71+
"this is a comment",
72+
"this is a history"])
73+
74+
indata = reshape(Float32[1:100;], 5, 20)
75+
write(f, indata; header=inhdr)
76+
77+
@testset "ImageHDU type" begin
78+
result = parse_output_projection(f[1])
79+
@test result[1] isa WCSTransform
80+
@test result[2] isa Tuple
81+
@test parse_output_projection(f[1], shape_out = (12,12))[1] isa WCSTransform
82+
end
83+
close(f)
84+
85+
@testset "String filename" begin
86+
result = parse_output_projection(fname)
87+
@test result[1] isa WCSTransform
88+
@test result[2] isa Tuple
89+
@test parse_output_projection(fname, hdu_number = 1)[1] isa WCSTransform
90+
end
91+
92+
@testset "invalid input" begin
93+
@test_throws ArgumentError parse_output_projection(1:40, shape_out = (12,12))
94+
end
95+
96+
wcs = WCSTransform(2;
97+
cdelt = [-0.066667, 0.066667],
98+
ctype = ["RA---AIR", "DEC--AIR"],
99+
crpix = [-234.75, 8.3393],
100+
crval = [0., -90],
101+
pv = [(2, 1, 45.0)])
102+
103+
@testset "WCSTransform input" begin
104+
@test_throws ArgumentError parse_output_projection(wcs)
105+
@test_throws DomainError parse_output_projection(wcs, shape_out = ())
106+
107+
result = parse_output_projection(wcs, shape_out = (12,12))
108+
@test result[1] isa WCSTransform
109+
@test result[2] isa Tuple
110+
end
111+
112+
end
113+

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using Reproject
22
using Test
33
using Conda, PyCall
4+
using FITSIO, WCS
45

56
ENV["PYTHON"]=""
67
Conda.add_channel("astropy")
78
Conda.add("reproject")
89
rp = pyimport("reproject")
910

1011
@testset "Reproject.jl" begin
11-
# Write your own tests here.
12+
include("parsers.jl")
1213
end

0 commit comments

Comments
 (0)