Skip to content

Commit eed61f1

Browse files
authored
Merge pull request #20 from JuliaAstro/extension-name
Extension name
2 parents 6108b57 + ae53ae1 commit eed61f1

File tree

9 files changed

+269
-193
lines changed

9 files changed

+269
-193
lines changed

data/random_test.fits

Lines changed: 1 addition & 5 deletions
Large diffs are not rendered by default.

src/card.jl

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
#### Card type ####
1+
#= Improvements
2+
3+
* add HIERARCH keyword conventions
4+
* make 32-bit floats the default instead of 64-bit floats
5+
* add parsing of non-stardard formats
6+
* add splitting of long strings at spaces
27
3-
# add HIERARCH keyword conventions
4-
# make 32-bit floats the default instead of 64-bit floats
5-
# add parsing of non-stardard formats
6-
# add splitting of long strings at spaces
8+
=#
9+
10+
#### Card type ####
711

812
const CARDLENGTH::Int = 80
913
# const BLANKCARD::String = repeat(" ", CARDLENGTH)
@@ -185,8 +189,10 @@ mutable struct CardFormat
185189
cbeg::Int8
186190
cend::Int8
187191
# frmt::AbstractString
188-
function CardFormat(fixed = true, vbeg = 0, vend = 0, amper = 0, slash = 0,
189-
ubeg = 0, uend = 0, cbeg = 0, cend = 0)
192+
function CardFormat(fixed::Bool = true, vbeg::I = 0, vend::I = 0,
193+
amper::I = 0, slash::I = 0, ubeg::I = 0, uend::I = 0, cbeg::I = 0,
194+
cend::I = 0) where I<:Integer
195+
190196
begv = typeof(vbeg) <: Tuple ? Int8.(vbeg) : Int8(vbeg)
191197
endv = typeof(vend) <: Tuple ? Int8.(vend) : Int8(vend)
192198
new(Bool(fixed), begv, endv, Int8(amper), Int8(slash),
@@ -282,7 +288,7 @@ function Card(key::S = "", value::V = missing, comment::S = ""; fixed::B = true,
282288
type = Value{typeof(value)}
283289
format = formatcard(type, value, comment; fixed = fixed, append = append,
284290
slash = slash, lpad = lpad, rpad = rpad, upad = upad, truncate = truncate)
285-
elseif is_valid_key(upkey) && is_long_string(value, comment, slash, lpad,
291+
elseif is_valid_key(upkey) && is_long_string(String(value), comment, slash, lpad,
286292
rpad, truncate)
287293
# Create a list of cards from from a long string.
288294
type = Value{typeof(value)}
@@ -366,8 +372,8 @@ function is_fixed_key(key::AbstractString)
366372
key in ["BITPIX", "END", "NAXIS", "SIMPLE"] || !isnothing(match(r"NAXIS\d{1,3}", key))
367373
end
368374

369-
function is_long_string(value::S, comment::S, slash, lpad, rpad, truncate) where
370-
S <: AbstractString
375+
function is_long_string(value::AbstractString, comment::AbstractString,
376+
slash::I, lpad::I, rpad::I, truncate::Bool) where I <: Integer
371377
# Test for long value and comment strings. Return true if found, except in the case where
372378
# the comment can be truncated, i.e., truncate == true.
373379
length(replace(value, "'" => "''")) > value_length(slash, lpad) ||
@@ -526,18 +532,18 @@ function formatcomment!(comment::S, vend::I, format::F; units = missing,
526532
format
527533
end
528534

529-
function value_length(slash, lpad)
535+
function value_length(slash::I, lpad::I)::Integer where I <: Integer
530536
# Calculate length of value based comment separator index and padding.
531537
# First 2 is for value indicator. Second 2 is single quotes.
532538
((slash-lpad) > CARDLENGTH ? CARDLENGTH : slash-lpad-1) - (TOKENLEN+2)
533539
end
534540

535-
function comment_length(slash, rpad)
541+
function comment_length(slash::I, rpad::I)::Integer where I <: Integer
536542
# Calculate length of comment using the comment separator index and padding.
537543
(slash+rpad) >= CARDLENGTH ? 0 : CARDLENGTH - (slash+rpad)
538544
end
539545

540-
rpad_key(key, n = 0) = rpad(key, KEYLENGTH + n)
546+
rpad_key(key::AbstractString, n::Integer = 0) = rpad(key, KEYLENGTH + n)
541547

542548
function slices(value::S, slen::I, ncard::I, single::B = false) where
543549
{S <: AbstractString, I <: Integer, B <: Bool}
@@ -554,7 +560,7 @@ function slices(value::S, slen::I, ncard::I, single::B = false) where
554560
end
555561

556562
# Format special XTENSION values, pad to 8 characters
557-
function format_xtension_value(key::S, value::S) where S <: AbstractString
563+
function format_xtension_value(key::AbstractString, value::AbstractString)
558564
(key == "XTENSION" && any(contains(s, uppercase(value)) for s in XTENSION_VALUES)) ?
559565
rpad(uppercase(value), 8) : value
560566
end
@@ -876,7 +882,7 @@ function named_offsets(match::RegexMatch)
876882
(; (Symbol.(keys(match)) .=> match.offsets[1:length(keys(match))])...)
877883
end
878884

879-
function value_type(valus)
885+
function value_type(valus::RegexMatch)
880886
types = (strg = String, bool = Bool, numr = Real, cplx = Complex, miss = Missing)
881887
keys = (:strg, :bool, :numr, :cplx, :miss)
882888
basetype([types[k] for k in keys if valus[k] !== nothing][1])
@@ -890,7 +896,9 @@ function parse_value_comment(::Type{Value}, image::AbstractString)
890896
(value*units, String(comment), format)
891897
end
892898

893-
function parse_value(::Type{String}, valus, offsets)::Tuple{String, CardFormat}
899+
function parse_value(::Type{String}, valus::RegexMatch,
900+
offsets::NamedTuple)::Tuple{String, CardFormat}
901+
894902
format = CardFormat()
895903
value = string(valus[:strg])
896904
# vbeg and vend include the the single quotes.
@@ -901,15 +909,19 @@ function parse_value(::Type{String}, valus, offsets)::Tuple{String, CardFormat}
901909
(value, format)
902910
end
903911

904-
function parse_value(::Type{Bool}, valus, offsets)::Tuple{Bool, CardFormat}
912+
function parse_value(::Type{Bool}, valus::RegexMatch,
913+
offsets::NamedTuple)::Tuple{Bool, CardFormat}
914+
905915
format = CardFormat()
906916
value = valus[:bool] == "T" ? true : false
907917
format.fixd = offsets[:bool] == FIXEDINDEX ? true : false
908918
format.vbeg, format.vend = Int8(offsets[:bool]), Int8(offsets[:bool])
909919
(value, format)
910920
end
911921

912-
function parse_value(::Type{Real}, valus, offsets)::Tuple{Real, CardFormat}
922+
function parse_value(::Type{Real}, valus::RegexMatch,
923+
offsets::NamedTuple)::Tuple{Real, CardFormat}
924+
913925
format = CardFormat()
914926
value = parse_number(valus[:numr])
915927
numr, nlen = offsets[:numr], length(valus[:numr])
@@ -919,7 +931,9 @@ function parse_value(::Type{Real}, valus, offsets)::Tuple{Real, CardFormat}
919931
(value, format)
920932
end
921933

922-
function parse_value(::Type{Complex}, valus, offsets)::Tuple{Complex, CardFormat}
934+
function parse_value(::Type{Complex}, valus::RegexMatch,
935+
offsets::NamedTuple)::Tuple{Complex, CardFormat}
936+
923937
format = CardFormat()
924938
value = parse_complex(valus[:real], valus[:imag])
925939
real_, imag_ = offsets[:real], offsets[:imag]
@@ -930,7 +944,9 @@ function parse_value(::Type{Complex}, valus, offsets)::Tuple{Complex, CardFormat
930944
(value, format)
931945
end
932946

933-
function parse_value(::Type{Missing}, valus, offsets)::Tuple{Missing, CardFormat}
947+
function parse_value(::Type{Missing}, valus::RegexMatch,
948+
offsets::NamedTuple)::Tuple{Missing, CardFormat}
949+
934950
format = CardFormat()
935951
(missing, format)
936952
end
@@ -1025,7 +1041,7 @@ function parse_number(real::AbstractString)
10251041
value
10261042
end
10271043

1028-
function overflow(real)
1044+
function overflow(real::AbstractString)
10291045
# test for overflow or precision
10301046
n = findlast('E', real)
10311047
abs(Base.parse(Int, real[(n+1):end])) >= 39 || n >= 14

src/fitscore.jl

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,48 @@ end
5555
Briefly describe the list of header-data units (HDUs).
5656
"""
5757
function info(hdus::Vector{HDU})
58-
for hdu in hdus
59-
info(hdu)
58+
typ = rpad("HDU", INFOTYPELEN)
59+
nam = rpad("Name", INFONAMELEN)
60+
ver = lpad("Ver", INFOVERSLEN)
61+
crd = lpad("Cards", INFOCARDLEN)
62+
ltyp = lpad("Type", INFOTYPELEN-1)
63+
siz = "Shape"
64+
print(stdout, " # $typ $nam $ver $crd $ltyp $siz")
65+
print(stdout, "\n")
66+
for (j, hdu) in enumerate(hdus)
67+
info(hdu, j)
68+
end
69+
end
70+
71+
function Base.show(io::IO, ::MIME"text/plain", hdus::AbstractVector{HDU})
72+
typ = rpad("HDU", INFOTYPELEN)
73+
nam = rpad("Name", INFONAMELEN)
74+
ver = lpad("Ver", INFOVERSLEN)
75+
crd = lpad("Cards", INFOCARDLEN)
76+
ltyp = lpad("Type", INFOTYPELEN-1)
77+
siz = "Shape"
78+
print(io, " # $typ $nam $ver $crd $ltyp $siz")
79+
print(io, "\n")
80+
for (j, hdu) in enumerate(hdus)
81+
cards = getfield(hdu, :cards)
82+
N = cards["NAXIS"]
83+
typ = rpad(typeofhdu(hdu), INFOTYPELEN)
84+
nam = rpad(haskey(cards, "EXTNAME") ? cards["EXTNAME"] : "", INFONAMELEN)
85+
ver = lpad(haskey(cards, "EXTVERS") ? cards["EXTVER"] : "1", INFOVERSLEN)
86+
crd = lpad(length(cards), INFOCARDLEN)
87+
siz = join([string(cards["NAXIS$j"])
88+
for j (typeofhdu(hdu)==Random ? 2 : 1):N], " × ")
89+
eltype = lpad(datatype(cards), INFOTYPELEN)
90+
print(io, "$(lpad(j, 3)) $typ $nam $ver $crd $eltype $siz")
91+
if j != length(hdus)
92+
print(io, "\n")
93+
end
6094
end
6195
end
6296

6397
#### Dictionary-like key function for HDUs
6498

65-
function Base.haskey(hdus::Vector{HDU}, key::Type{<:AbstractHDU})
99+
function Base.haskey(hdus::Vector{HDU}, key::Type{<:AbstractHDU})::Bool
66100
for hdu in hdus
67101
if typeofhdu(hdu) == key
68102
return true
@@ -71,7 +105,7 @@ function Base.haskey(hdus::Vector{HDU}, key::Type{<:AbstractHDU})
71105
false
72106
end
73107

74-
function Base.getindex(hdus::Vector{HDU}, key::Type{<:AbstractHDU})
108+
function Base.getindex(hdus::Vector{HDU}, key::Type{<:AbstractHDU})::HDU
75109
for hdu in hdus
76110
if typeofhdu(hdu) == key
77111
return hdu
@@ -80,44 +114,57 @@ function Base.getindex(hdus::Vector{HDU}, key::Type{<:AbstractHDU})
80114
throw(KeyError(key))
81115
end
82116

83-
function Base.getindex(hdus::Vector{HDU}, key::AbstractString)
117+
function Base.getindex(hdus::Vector{HDU}, name::U)::HDU where
118+
U<:Union{AbstractString, Symbol}
119+
84120
for hdu in hdus
85121
if haskey(hdu.cards, "EXTNAME") &&
86-
uppercase(rstrip(hdu.cards["EXTNAME"])) == uppercase(key)
122+
uppercase(rstrip(hdu.cards["EXTNAME"])) ==
123+
uppercase(name isa Symbol ? string(name) : name)
87124
return hdu
88125
end
89126
end
90-
throw(KeyError(key))
127+
throw(KeyError(name))
91128
end
92129

93-
function Base.get(hdus::Vector{HDU}, key::AbstractString, default = missing)
94-
value = default
95-
for hdu in hdus
96-
if haskey(hdu.cards, "EXTNAME") &&
97-
uppercase(rstrip(hdu.cards["EXTNAME"])) == uppercase(key)
98-
value = hdu
99-
break
130+
function Base.get(hdus::Vector{HDU}, name::U, default::V = "")::Union{HDU, Nothing} where
131+
{U<:Union{AbstractString, Symbol}, V<:Union{AbstractString, Symbol}}
132+
133+
value::Union{HDU, Nothing} = nothing
134+
for key in (name, default)
135+
for hdu in hdus
136+
if haskey(hdu.cards, "EXTNAME") &&
137+
uppercase(rstrip(hdu.cards["EXTNAME"])) ==
138+
uppercase(key isa Symbol ? string(key) : key)
139+
value = hdu
140+
break
141+
end
100142
end
101143
end
102144
value
103145
end
104146

105-
function Base.get(hdus::Vector{HDU}, keys::Union{AbstractArray, Tuple},
106-
defaults::Union{AbstractArray, Tuple})
147+
function Base.get(hdus::Vector{HDU}, names::K, defaults::D)::Vector where
148+
{K<:Union{Vector, Tuple}, D<:Union{Vector, Tuple}}
107149

108-
values = Any[defaults...]
109-
for (j, key) in enumerate(keys)
150+
values = Any[fill(nothing, length(names))...]
151+
for (j, (name, default)) in enumerate(zip(names, defaults))
110152
for hdu in hdus
111-
if haskey(hdu.cards, "EXTNAME") &&
112-
uppercase(rstrip(hdu.cards["EXTNAME"])) == uppercase(key)
113-
values[j] = hdu
153+
if haskey(hdu.cards, "EXTNAME")
154+
if uppercase(rstrip(hdu.cards["EXTNAME"])) ==
155+
uppercase(name isa Symbol ? string(name) : name)
156+
values[j] = hdu
157+
elseif uppercase(rstrip(hdu.cards["EXTNAME"])) ==
158+
uppercase(default isa Symbol ? string(default) : default)
159+
values[j] = hdu
160+
end
114161
end
115162
end
116163
end
117164
values
118165
end
119166

120-
function Base.findfirst(key::AbstractString, hdus::Vector{HDU})
167+
function Base.findfirst(key::AbstractString, hdus::Vector{HDU})::Union(Integer, Nothing)
121168
for (j, hdu) in enumerate(hdus)
122169
if uppercase(rstrip(hdu.cards[key])) == uppercase(key)
123170
return j

0 commit comments

Comments
 (0)