Headers

FITS files consist of one or more HDUs (header data units), and each HDU can contain an N-dimensional image or table. Before the data is a header. Headers contain (key, value, comment) groups as well as dedicated long-form COMMENT and HISTORY sections used to document, for example, the series of post-processing steps applied to an image.

Accessing Headers

Here are some examples of how to set and read keys, comments, and history.

We'll start by making a blank image:

img = AstroImage(zeros(10, 10))
Example block output

Set keys to values with different data types:

img["KEY1"] = 2   # Integer
img["KEY2"] = 2.0 # Float
2.0
Note

Floating point values are formatted as ASCII strings when written to the FITS files, so the precision may be limited.

img["KEY3"] = "STRING"
img["KEY4"] = true
img["KEY5"] = false
img["KEY6"] = nothing # Undefined value
Note

A keyword may be present in a header with no value at all. Assigning nothing (or missing) creates such a card, and it reads back as missing:

img["KEY6"]
missing

A key that is not present in the header instead reads back as nothing, so the two cases can be told apart:

isnothing(img["KEY7"])
true

We can set comments:

img["KEY1", Comment] = "A key with an integer value";

and view:

header(img)
6-element Vector{FITSFiles.Card}:
 KEY1    =                    2 / A key with an integer value                    
 KEY2    =                  2.0                                                  
 KEY3    = 'STRING'                                                              
 KEY4    =                    T                                                  
 KEY5    =                    F                                                  
 KEY6    =                                                                       

Read keys:

a = img["KEY3"]
"STRING"

Read comment:

com = img["KEY1", Comment]
"A key with an integer value"

Add long-form COMMENT:

push!(img, Comment, """
We now describe how to
add a long form comment
to the end of a header.
""")

header(img)
9-element Vector{FITSFiles.Card}:
 KEY1    =                    2 / A key with an integer value                    
 KEY2    =                  2.0                                                  
 KEY3    = 'STRING'                                                              
 KEY4    =                    T                                                  
 KEY5    =                    F                                                  
 KEY6    =                                                                       
 COMMENT We now describe how to                                                  
 COMMENT add a long form comment                                                 
 COMMENT to the end of a header.                                                 

Add HISTORY entry:

push!(img, History, """
We now describe how to
add a long form history
to the end of a header.
""")

header(img)
12-element Vector{FITSFiles.Card}:
 KEY1    =                    2 / A key with an integer value                    
 KEY2    =                  2.0                                                  
 KEY3    = 'STRING'                                                              
 KEY4    =                    T                                                  
 KEY5    =                    F                                                  
 KEY6    =                                                                       
 COMMENT We now describe how to                                                  
 COMMENT add a long form comment                                                 
 COMMENT to the end of a header.                                                 
 HISTORY We now describe how to                                                  
 HISTORY add a long form history                                                 
 HISTORY to the end of a header.                                                 

A COMMENT or HISTORY card stores its text in columns 9-80, so it can hold at most 72 characters and cannot contain a newline. Multi-line text is therefore split into one card per line, and any line too long to fit on a single card is wrapped across as many cards as it needs:

push!(img, Comment, """
This comment spans two lines, the second of which is long enough that it will not fit on a single card.
""")
header(img)
14-element Vector{FITSFiles.Card}:
 KEY1    =                    2 / A key with an integer value                    
 KEY2    =                  2.0                                                  
 KEY3    = 'STRING'                                                              
 KEY4    =                    T                                                  
 KEY5    =                    F                                                  
 KEY6    =                                                                       
 COMMENT We now describe how to                                                  
 COMMENT add a long form comment                                                 
 COMMENT to the end of a header.                                                 
 HISTORY We now describe how to                                                  
 HISTORY add a long form history                                                 
 HISTORY to the end of a header.                                                 
 COMMENT This comment spans two lines, the second of which is long enough that it
 COMMENT will not fit on a single card.                                          

We can retrieve long form comments/history by indexing them directly:

comment_strings = img[Comment]
5-element Vector{String}:
 "We now describe how to"
 "add a long form comment"
 "to the end of a header."
 "This comment spans two lines, the second of which is long enough that it"
 "will not fit on a single card."
history_strings = img[History]
3-element Vector{String}:
 "We now describe how to"
 "add a long form history"
 "to the end of a header."

API docs: