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
812const 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))
367373end
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
527533end
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 )
533539end
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)
538544end
539545
540- rpad_key (key, n = 0 ) = rpad (key, KEYLENGTH + n)
546+ rpad_key (key:: AbstractString , n:: Integer = 0 ) = rpad (key, KEYLENGTH + n)
541547
542548function 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
554560end
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
560566end
@@ -876,7 +882,7 @@ function named_offsets(match::RegexMatch)
876882 (; (Symbol .(keys (match)) .=> match. offsets[1 : length (keys (match))]). .. )
877883end
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)
891897end
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)
902910end
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)
910920end
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)
920932end
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)
931945end
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)
936952end
@@ -1025,7 +1041,7 @@ function parse_number(real::AbstractString)
10251041 value
10261042end
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
0 commit comments