Skip to content

Commit 24d8f5e

Browse files
committed
refactoring
1 parent 6dbd7f3 commit 24d8f5e

File tree

3 files changed

+25
-66
lines changed

3 files changed

+25
-66
lines changed

src/Reproject.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
__precompile__()
2-
31
module Reproject
42

53
using FITSIO, WCS
64

75
include("parsers.jl")
86

9-
export
10-
parse_input_data,
11-
parse_output_projection
12-
137
end # module
14-

src/parsers.jl

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,14 @@ function parse_input_data(input_data::ImageHDU)
1414
return read(input_data), WCS.from_header(read_header(input_data, String))[1]
1515
end
1616

17-
function parse_input_data(input_data::String; hdu_in = nothing)
18-
return parse_input_data(FITS(input_data), hdu_in = hdu_in)
17+
function parse_input_data(input_data::String, hdu_in)
18+
return parse_input_data(FITS(input_data), hdu_in)
1919
end
2020

21-
function parse_input_data(input_data::FITS; hdu_in = nothing)
22-
if hdu_in === nothing
23-
if length(input_data) > 1
24-
throw(ArgumentError("More than one HDU is present, please specify which HDU to use with 'hdu_in' option"))
25-
else
26-
hdu_in = 1
27-
end
28-
end
21+
function parse_input_data(input_data::FITS, hdu_in)
2922
return parse_input_data(input_data[hdu_in])
3023
end
3124

32-
function parse_input_data(input_data; hdu_in = nothing)
33-
throw(ArgumentError("Input should be in Union{String, FITS, ImageHDU}"))
34-
end
3525

3626
# TODO: extend support for passing FITSHeader when FITSHeader to WCSTransform support is possible.
3727

@@ -49,17 +39,15 @@ Parse output projection and returns a WCS object and shape of output.
4939
- `shape_out`: shape of the output image.
5040
- `hdu_number`: specifies HDU number when file name is given as input.
5141
"""
52-
function parse_output_projection(output_projection::WCSTransform; shape_out = nothing)
53-
if shape_out === nothing
54-
throw(ArgumentError("Need to specify shape when specifying output_projection as WCS object"))
55-
elseif length(shape_out) == 0
42+
function parse_output_projection(output_projection::WCSTransform, shape_out)
43+
if length(shape_out) == 0
5644
throw(DomainError(shape_out, "The shape of the output image should not be an empty tuple"))
5745
end
5846

5947
return output_projection, shape_out
6048
end
6149

62-
function parse_output_projection(output_projection::ImageHDU; shape_out = nothing)
50+
function parse_output_projection(output_projection::ImageHDU, shape_out)
6351
wcs_out = WCS.from_header(read_header(output_projection, String))[1]
6452
if shape_out === nothing
6553
shape_out = size(output_projection)
@@ -70,25 +58,19 @@ function parse_output_projection(output_projection::ImageHDU; shape_out = nothin
7058
return wcs_out, shape_out
7159
end
7260

73-
function parse_output_projection(output_projection::String; hdu_number = nothing)
74-
hdu_list = FITS(output_projection)
75-
if hdu_number === nothing
76-
wcs_out = WCS.from_header(read_header(hdu_list[1], String))[1]
77-
hdu_number = 1
78-
else
79-
wcs_out = WCS.from_header(read_header(hdu_list[hdu_number], String))[1]
80-
end
61+
function parse_output_projection(output_projection::String, hdu_number)
62+
parse_output_projection(FITS(output_projection), hdu_number)
63+
end
64+
65+
function parse_output_projection(output_projection::FITS, hdu_number)
66+
wcs_out = WCS.from_header(read_header(output_projection[hdu_number], String))[1]
8167

82-
if hdu_list[hdu_number] isa ImageHDU
83-
shape_out = size(hdu_list[hdu_number])
68+
if output_projection[hdu_number] isa ImageHDU
69+
shape_out = size(output_projection[hdu_number])
8470
else
8571
throw(ArgumentError("Given FITS file doesn't have ImageHDU"))
8672
end
8773

8874
return wcs_out, shape_out
8975
end
9076

91-
function parse_output_projection(output_projection; shape_out = nothing)
92-
throw(ArgumentError("output_projection should be in Union{String, WCSTransform, FITSHeader}"))
93-
end
94-

test/parsers.jl

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Reproject: parse_input_data, parse_output_projection
12
@testset "input parser" begin
23
fname = tempname() * ".fits"
34
f = FITS(fname, "w")
@@ -21,14 +22,14 @@
2122
end
2223

2324
@testset "Single HDU FITS file" begin
24-
result = parse_input_data(f)
25+
result = parse_input_data(f, 1)
2526
@test result[1] isa Array
2627
@test result[2] isa WCSTransform
2728
end
2829
close(f)
2930

3031
@testset "String filename input" begin
31-
result = parse_input_data(fname)
32+
result = parse_input_data(fname, 1)
3233
@test result[1] isa Array
3334
@test result[2] isa WCSTransform
3435
end
@@ -38,24 +39,15 @@
3839
write(f, indata; header=inhdr)
3940

4041
@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)
42+
result = parse_input_data(f, 2)
4543
@test result[1] isa Array
4644
@test result[2] isa WCSTransform
4745

4846
close(f)
49-
result = parse_input_data(fname, hdu_in = 1)
47+
result = parse_input_data(fname, 1)
5048
@test result[1] isa Array
5149
@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-
50+
end
5951
end
6052

6153
@testset "output parser" begin
@@ -75,22 +67,17 @@ end
7567
write(f, indata; header=inhdr)
7668

7769
@testset "ImageHDU type" begin
78-
result = parse_output_projection(f[1])
70+
result = parse_output_projection(f[1], (12,12))
7971
@test result[1] isa WCSTransform
8072
@test result[2] isa Tuple
81-
@test parse_output_projection(f[1], shape_out = (12,12))[1] isa WCSTransform
73+
@test_throws DomainError parse_output_projection(f[1], ())
8274
end
8375
close(f)
8476

8577
@testset "String filename" begin
86-
result = parse_output_projection(fname)
78+
result = parse_output_projection(fname, 1)
8779
@test result[1] isa WCSTransform
8880
@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))
9481
end
9582

9683
wcs = WCSTransform(2;
@@ -101,13 +88,10 @@ end
10188
pv = [(2, 1, 45.0)])
10289

10390
@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))
91+
result = parse_output_projection(wcs, (12,12))
10892
@test result[1] isa WCSTransform
10993
@test result[2] isa Tuple
94+
@test_throws DomainError parse_output_projection(wcs, ())
11095
end
111-
11296
end
11397

0 commit comments

Comments
 (0)