Skip to content

Commit 06254c7

Browse files
authored
Parser added (#2)
Parser added
2 parents 2449a88 + fd44b23 commit 06254c7

File tree

5 files changed

+183
-2
lines changed

5 files changed

+183
-2
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ uuid = "d1dcc2e6-806e-11e9-2897-3f99785db2ae"
33
authors = ["Mosè Giordano"]
44
version = "0.1.0"
55

6+
[deps]
7+
FITSIO = "525bcba6-941b-5504-bd06-fd0dc1a4d2eb"
8+
WCS = "15f3aee2-9e10-537f-b834-a6fb8bdb944d"
9+
610
[compat]
711
julia = "^1.0.0"
812

src/Reproject.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Reproject
22

3-
greet() = print("Hello World!")
3+
using FITSIO, WCS
4+
5+
include("parsers.jl")
46

57
end # module

src/parsers.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
parse_input_data(input_data::ImageHDU)
3+
parse_input_data(input_data::String, hdu_in)
4+
parse_input_data(input_data::FITS, hdu_in)
5+
6+
Parse input data and returns an Array and WCS object.
7+
8+
# Arguments
9+
- `input_data`: image to reproject which can be name of a FITS file,
10+
an ImageHDU or a FITS file.
11+
- `hdu_in`: used to set HDU to use when more than one HDU is present.
12+
"""
13+
function parse_input_data(input_data::ImageHDU)
14+
return read(input_data), WCS.from_header(read_header(input_data, String))[1]
15+
end
16+
17+
function parse_input_data(input_data::String, hdu_in)
18+
return parse_input_data(FITS(input_data), hdu_in)
19+
end
20+
21+
function parse_input_data(input_data::FITS, hdu_in)
22+
return parse_input_data(input_data[hdu_in])
23+
end
24+
25+
26+
# TODO: extend support for passing FITSHeader when FITSHeader to WCSTransform support is possible.
27+
28+
29+
"""
30+
parse_output_projection(output_projection::WCSTransform, shape_out)
31+
parse_output_projection(output_projection::ImageHDU; shape_out)
32+
parse_output_projection(output_projection::String, hdu_number)
33+
parse_output_projection(output_projection::FITS, hdu_number)
34+
35+
Parse output projection and returns a WCS object and shape of output.
36+
37+
# Arguments
38+
- `output_projection`: WCS information about the image to be reprojected which can be
39+
name of a FITS file, an ImageHDU or WCSTransform.
40+
- `shape_out`: shape of the output image.
41+
- `hdu_number`: specifies HDU number when file name is given as input.
42+
"""
43+
function parse_output_projection(output_projection::WCSTransform, shape_out)
44+
if length(shape_out) == 0
45+
throw(DomainError(shape_out, "The shape of the output image should not be an empty tuple"))
46+
end
47+
48+
return output_projection, shape_out
49+
end
50+
51+
function parse_output_projection(output_projection::ImageHDU, shape_out)
52+
wcs_out = WCS.from_header(read_header(output_projection, String))[1]
53+
if shape_out === nothing
54+
shape_out = size(output_projection)
55+
end
56+
if length(shape_out) == 0
57+
throw(DomainError(shape_out, "The shape of the output image should not be an empty tuple"))
58+
end
59+
return wcs_out, shape_out
60+
end
61+
62+
function parse_output_projection(output_projection::String, hdu_number)
63+
parse_output_projection(FITS(output_projection), hdu_number)
64+
end
65+
66+
function parse_output_projection(output_projection::FITS, hdu_number)
67+
wcs_out = WCS.from_header(read_header(output_projection[hdu_number], String))[1]
68+
69+
if output_projection[hdu_number] isa ImageHDU
70+
shape_out = size(output_projection[hdu_number])
71+
else
72+
throw(ArgumentError("Given FITS file doesn't have ImageHDU"))
73+
end
74+
75+
return wcs_out, shape_out
76+
end
77+

test/parsers.jl

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

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)