/
fits.jl
145 lines (121 loc) · 3.96 KB
/
fits.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# FITS methods
const VERBOSE_MODE = Dict("r"=>"read-only",
"w"=>"read-write",
"r+"=>"append")
# helper function for show()
function show_ascii_table(io, names, cols, spaces=2, indent=0)
ncols = length(cols)
ncols >= 1 || error("No columns")
nrows = length(cols[1])
length(names) == ncols || error("length of cols and names must match")
for i=1:ncols
length(cols[i]) == nrows || error("column length mismatch")
end
lengths = [max(maximum(length, cols[i]), length(names[i])) + spaces
for i=1:ncols]
for i = 1:ncols
print(io, rpad(names[i], lengths[i]))
end
println(io)
for j = 1:nrows
print(io, " "^indent)
for i=1:ncols
print(io, rpad(cols[i][j], lengths[i]))
end
j != nrows && println(io)
end
end
"""
length(f::FITS)
Number of HDUs in the file.
"""
function length(f::FITS)
fits_assert_open(f.fitsfile)
Int(fits_get_num_hdus(f.fitsfile))
end
Base.lastindex(f::FITS) = length(f)
# Iteration
start(f::FITS) = 1
next(f::FITS, state) = (f[state], state + 1)
done(f::FITS, state) = state > length(f)
function show(io::IO, f::FITS)
fits_assert_open(f.fitsfile)
print(io, """File: $(f.filename)
Mode: $(repr(f.mode)) ($(VERBOSE_MODE[f.mode]))
""")
nhdu = length(f)
if nhdu == 0
print(io, "No HDUs.")
else
print(io, "HDUs: ")
names = Vector{String}(undef, nhdu)
vers = Vector{String}(undef, nhdu)
types = Vector{String}(undef, nhdu)
for i = 1:nhdu
t = fits_movabs_hdu(f.fitsfile, i)
types[i] = (t == :image_hdu ? "Image" :
t == :binary_table ? "Table" :
t == :ascii_table ? "ASCIITable" :
error("unknown HDU type"))
nname = fits_try_read_extname(f.fitsfile)
names[i] = something(nname, "")
nver = fits_try_read_extver(f.fitsfile)
vers[i] = nver === nothing ? "" : string(nver)
end
nums = [string(i) for i=1:nhdu]
# only display version info if present
if maximum(length, vers) > 0
dispnames = ["Num", "Name", "Ver", "Type"]
dispcols = Vector{String}[nums, names, vers, types]
else
dispnames = ["Num", "Name", "Type"]
dispcols = Vector{String}[nums, names, types]
end
show_ascii_table(io, dispnames, dispcols, 2, 6)
end
end
# Returns HDU object based on extension number
function getindex(f::FITS, i::Integer)
fits_assert_open(f.fitsfile)
if haskey(f.hdus, i)
return f.hdus[i]
end
if i > length(f)
error("index out of bounds")
end
hdutype = fits_movabs_hdu(f.fitsfile, i)
f.hdus[i] = (hdutype == :image_hdu ? ImageHDU(f.fitsfile, i) :
hdutype == :binary_table ? TableHDU(f.fitsfile, i) :
hdutype == :ascii_table ? ASCIITableHDU(f.fitsfile, i) :
error("bad HDU type"))
return f.hdus[i]
end
# Returns HDU based on hduname, version
function getindex(f::FITS, name::AbstractString, ver::Int=0)
fits_assert_open(f.fitsfile)
fits_movnam_hdu(f.fitsfile, name, ver)
i = fits_get_hdu_num(f.fitsfile)
if haskey(f.hdus, i)
return f.hdus[i]
end
hdutype = fits_get_hdu_type(f.fitsfile)
f.hdus[i] = (hdutype == :image_hdu ? ImageHDU(f.fitsfile, i) :
hdutype == :binary_table ? TableHDU(f.fitsfile, i) :
hdutype == :ascii_table ? ASCIITableHDU(f.fitsfile, i) :
error("bad HDU type"))
return f.hdus[i]
end
"""
close(f::FITS)
Close the file.
Subsequent attempts to operate on `f` will result in an error. `FITS` objects are also
automatically closed when they are garbage collected.
"""
function close(f::FITS)
fits_assert_open(f.fitsfile)
fits_close_file(f.fitsfile)
f.filename = ""
f.mode = ""
empty!(f.hdus)
nothing
end