Headers¶
Header
¶
-
class
astropy.io.fits.
Header
(cards=[], copy=False)[source] [edit on github]¶ Bases:
object
FITS header class. This class exposes both a dict-like interface and a list-like interface to FITS headers.
The header may be indexed by keyword and, like a dict, the associated value will be returned. When the header contains cards with duplicate keywords, only the value of the first card with the given keyword will be returned. It is also possible to use a 2-tuple as the index in the form (keyword, n)–this returns the n-th value with that keyword, in the case where there are duplicate keywords.
For example:
>>> header['NAXIS'] 0 >>> header[('FOO', 1)] # Return the value of the second FOO keyword 'foo'
The header may also be indexed by card number:
>>> header[0] # Return the value of the first card in the header 'T'
Commentary keywords such as HISTORY and COMMENT are special cases: When indexing the Header object with either ‘HISTORY’ or ‘COMMENT’ a list of all the HISTORY/COMMENT values is returned:
>>> header['HISTORY'] This is the first history entry in this header. This is the second history entry in this header. ...
See the Astropy documentation for more details on working with headers.
Construct a
Header
from an iterable and/or text file.Parameters: - cards : A list of
Card
objects, optional The cards to initialize the header with. Also allowed are other
Header
(ordict
-like) objects.Changed in version 1.2: Allowed
cards
to be adict
-like object.- copy : bool, optional
If
True
copies thecards
if they were anotherHeader
instance. Default isFalse
.New in version 1.3.
-
add_blank
(value='', before=None, after=None)[source] [edit on github]¶ Add a blank card.
Parameters: - value : str, optional
Text to be added.
- before : str or int, optional
Same as in
Header.update
- after : str or int, optional
Same as in
Header.update
-
add_comment
(value, before=None, after=None)[source] [edit on github]¶ Add a
COMMENT
card.Parameters: - value : str
Text to be added.
- before : str or int, optional
Same as in
Header.update
- after : str or int, optional
Same as in
Header.update
-
add_history
(value, before=None, after=None)[source] [edit on github]¶ Add a
HISTORY
card.Parameters: - value : str
History text to be added.
- before : str or int, optional
Same as in
Header.update
- after : str or int, optional
Same as in
Header.update
-
append
(card=None, useblanks=True, bottom=False, end=False)[source] [edit on github]¶ Appends a new keyword+value card to the end of the Header, similar to
list.append
.By default if the last cards in the Header have commentary keywords, this will append the new keyword before the commentary (unless the new keyword is also commentary).
Also differs from
list.append
in that it can be called with no arguments: In this case a blank card is appended to the end of the Header. In the case all the keyword arguments are ignored.Parameters: - card : str, tuple
A keyword or a (keyword, value, [comment]) tuple representing a single header card; the comment is optional in which case a 2-tuple may be used
- useblanks : bool, optional
If there are blank cards at the end of the Header, replace the first blank card so that the total number of cards in the Header does not increase. Otherwise preserve the number of blank cards.
- bottom : bool, optional
If True, instead of appending after the last non-commentary card, append after the last non-blank card.
- end : bool, optional
If True, ignore the useblanks and bottom options, and append at the very end of the Header.
-
cards
¶ The underlying physical cards that make up this Header; it can be looked at, but it should not be modified directly.
-
clear
()[source] [edit on github]¶ Remove all cards from the header.
-
comments
¶ View the comments associated with each keyword, if any.
For example, to see the comment on the NAXIS keyword:
>>> header.comments['NAXIS'] number of data axes
Comments can also be updated through this interface:
>>> header.comments['NAXIS'] = 'Number of data axes'
-
copy
(strip=False)[source] [edit on github]¶ Make a copy of the
Header
.Changed in version 1.3:
copy.copy
andcopy.deepcopy
on aHeader
will call this method.Parameters: - strip : bool, optional
If
True
, strip any headers that are specific to one of the standard HDU types, so that this header can be used in a different HDU.
Returns: - header
A new
Header
instance.
-
count
(keyword)[source] [edit on github]¶ Returns the count of the given keyword in the header, similar to
list.count
if the Header object is treated as a list of keywords.Parameters: - keyword : str
The keyword to count instances of in the header
-
extend
(cards, strip=True, unique=False, update=False, update_first=False, useblanks=True, bottom=False, end=False)[source] [edit on github]¶ Appends multiple keyword+value cards to the end of the header, similar to
list.extend
.Parameters: - cards : iterable
An iterable of (keyword, value, [comment]) tuples; see
Header.append
.- strip : bool, optional
Remove any keywords that have meaning only to specific types of HDUs, so that only more general keywords are added from extension Header or Card list (default:
True
).- unique : bool, optional
If
True
, ensures that no duplicate keywords are appended; keywords already in this header are simply discarded. The exception is commentary keywords (COMMENT, HISTORY, etc.): they are only treated as duplicates if their values match.- update : bool, optional
If
True
, update the current header with the values and comments from duplicate keywords in the input header. This supersedes theunique
argument. Commentary keywords are treated the same as ifunique=True
.- update_first : bool, optional
If the first keyword in the header is ‘SIMPLE’, and the first keyword in the input header is ‘XTENSION’, the ‘SIMPLE’ keyword is replaced by the ‘XTENSION’ keyword. Likewise if the first keyword in the header is ‘XTENSION’ and the first keyword in the input header is ‘SIMPLE’, the ‘XTENSION’ keyword is replaced by the ‘SIMPLE’ keyword. This behavior is otherwise dumb as to whether or not the resulting header is a valid primary or extension header. This is mostly provided to support backwards compatibility with the old
Header.fromTxtFile
method, and only applies ifupdate=True
.- useblanks, bottom, end : bool, optional
These arguments are passed to
Header.append()
while appending new cards to the header.
-
classmethod
fromfile
(fileobj, sep='', endcard=True, padding=True)[source] [edit on github]¶ Similar to
Header.fromstring()
, but reads the header string from a given file-like object or filename.Parameters: - fileobj : str, file-like
A filename or an open file-like object from which a FITS header is to be read. For open file handles the file pointer must be at the beginning of the header.
- sep : str, optional
The string separating cards from each other, such as a newline. By default there is no card separator (as is the case in a raw FITS file).
- endcard : bool, optional
If True (the default) the header must end with an END card in order to be considered valid. If an END card is not found an
OSError
is raised.- padding : bool, optional
If True (the default) the header will be required to be padded out to a multiple of 2880, the FITS header block size. Otherwise any padding, or lack thereof, is ignored.
Returns: - header
A new
Header
instance.
-
classmethod
fromkeys
(iterable, value=None)[source] [edit on github]¶ Similar to
dict.fromkeys()
–creates a newHeader
from an iterable of keywords and an optional default value.This method is not likely to be particularly useful for creating real world FITS headers, but it is useful for testing.
Parameters: - iterable
Any iterable that returns strings representing FITS keywords.
- value : optional
A default value to assign to each keyword; must be a valid type for FITS keywords.
Returns: - header
A new
Header
instance.
-
classmethod
fromstring
(data, sep='')[source] [edit on github]¶ Creates an HDU header from a byte string containing the entire header data.
Parameters: - data : str
String containing the entire header.
- sep : str, optional
The string separating cards from each other, such as a newline. By default there is no card separator (as is the case in a raw FITS file).
Returns: - header
A new
Header
instance.
-
classmethod
fromtextfile
(fileobj, endcard=False)[source] [edit on github]¶ Read a header from a simple text file or file-like object.
Equivalent to:
>>> Header.fromfile(fileobj, sep='\n', endcard=False, ... padding=False)
See also
-
get
(key, default=None)[source] [edit on github]¶ Similar to
dict.get()
–returns the value associated with keyword in the header, or a default value if the keyword is not found.Parameters: - key : str
A keyword that may or may not be in the header.
- default : optional
A default value to return if the keyword is not found in the header.
Returns: - value
The value associated with the given keyword, or the default value if the keyword is not in the header.
-
index
(keyword, start=None, stop=None)[source] [edit on github]¶ Returns the index if the first instance of the given keyword in the header, similar to
list.index
if the Header object is treated as a list of keywords.Parameters: - keyword : str
The keyword to look up in the list of all keywords in the header
- start : int, optional
The lower bound for the index
- stop : int, optional
The upper bound for the index
-
insert
(key, card, useblanks=True, after=False)[source] [edit on github]¶ Inserts a new keyword+value card into the Header at a given location, similar to
list.insert
.Parameters: - key : int, str, or tuple
The index into the list of header keywords before which the new keyword should be inserted, or the name of a keyword before which the new keyword should be inserted. Can also accept a (keyword, index) tuple for inserting around duplicate keywords.
- card : str, tuple
A keyword or a (keyword, value, [comment]) tuple; see
Header.append
- useblanks : bool, optional
If there are blank cards at the end of the Header, replace the first blank card so that the total number of cards in the Header does not increase. Otherwise preserve the number of blank cards.
- after : bool, optional
If set to
True
, insert after the specified index or keyword, rather than before it. Defaults toFalse
.
-
items
()[source] [edit on github]¶ Like
dict.items()
.
-
keys
()[source] [edit on github]¶ Like
dict.keys()
–iterating directly over theHeader
instance has the same behavior.
-
pop
(*args)[source] [edit on github]¶ Works like
list.pop()
if no arguments or an index argument are supplied; otherwise works likedict.pop()
.
-
popitem
()[source] [edit on github]¶ Similar to
dict.popitem()
.
-
remove
(keyword, ignore_missing=False, remove_all=False)[source] [edit on github]¶ Removes the first instance of the given keyword from the header similar to
list.remove
if the Header object is treated as a list of keywords.Parameters: - keyword : str
The keyword of which to remove the first instance in the header.
- ignore_missing : bool, optional
When True, ignores missing keywords. Otherwise, if the keyword is not present in the header a KeyError is raised.
- remove_all : bool, optional
When True, all instances of keyword will be removed. Otherwise only the first instance of the given keyword is removed.
-
rename_keyword
(oldkeyword, newkeyword, force=False)[source] [edit on github]¶ Rename a card’s keyword in the header.
Parameters: - oldkeyword : str or int
Old keyword or card index
- newkeyword : str
New keyword
- force : bool, optional
When
True
, if the new keyword already exists in the header, force the creation of a duplicate keyword. Otherwise aValueError
is raised.
-
set
(keyword, value=None, comment=None, before=None, after=None)[source] [edit on github]¶ Set the value and/or comment and/or position of a specified keyword.
If the keyword does not already exist in the header, a new keyword is created in the specified position, or appended to the end of the header if no position is specified.
This method is similar to
Header.update()
prior to Astropy v0.1.Note
It should be noted that
header.set(keyword, value)
andheader.set(keyword, value, comment)
are equivalent toheader[keyword] = value
andheader[keyword] = (value, comment)
respectively.New keywords can also be inserted relative to existing keywords using, for example:
>>> header.insert('NAXIS1', ('NAXIS', 2, 'Number of axes'))
to insert before an existing keyword, or:
>>> header.insert('NAXIS', ('NAXIS1', 4096), after=True)
to insert after an existing keyword.
The only advantage of using
Header.set()
is that it easily replaces the old usage ofHeader.update()
both conceptually and in terms of function signature.Parameters: - keyword : str
A header keyword
- value : str, optional
The value to set for the given keyword; if None the existing value is kept, but ‘’ may be used to set a blank value
- comment : str, optional
The comment to set for the given keyword; if None the existing comment is kept, but
''
may be used to set a blank comment- before : str, int, optional
Name of the keyword, or index of the
Card
before which this card should be located in the header. The argumentbefore
takes precedence overafter
if both specified.- after : str, int, optional
Name of the keyword, or index of the
Card
after which this card should be located in the header.
-
setdefault
(key, default=None)[source] [edit on github]¶ Similar to
dict.setdefault()
.
-
tofile
(fileobj, sep='', endcard=True, padding=True, overwrite=False)[source] [edit on github]¶ Writes the header to file or file-like object.
By default this writes the header exactly as it would be written to a FITS file, with the END card included and padding to the next multiple of 2880 bytes. However, aspects of this may be controlled.
Parameters: - fileobj : str, file, optional
Either the pathname of a file, or an open file handle or file-like object
- sep : str, optional
The character or string with which to separate cards. By default there is no separator, but one could use
'\\n'
, for example, to separate each card with a new line- endcard : bool, optional
If
True
(default) adds the END card to the end of the header string- padding : bool, optional
If
True
(default) pads the string with spaces out to the next multiple of 2880 characters- overwrite : bool, optional
If
True
, overwrite the output file if it exists. Raises anOSError
ifFalse
and the output file exists. Default isFalse
.Changed in version 1.3:
overwrite
replaces the deprecatedclobber
argument.
-
tostring
(sep='', endcard=True, padding=True)[source] [edit on github]¶ Returns a string representation of the header.
By default this uses no separator between cards, adds the END card, and pads the string with spaces to the next multiple of 2880 bytes. That is, it returns the header exactly as it would appear in a FITS file.
Parameters: - sep : str, optional
The character or string with which to separate cards. By default there is no separator, but one could use
'\\n'
, for example, to separate each card with a new line- endcard : bool, optional
If True (default) adds the END card to the end of the header string
- padding : bool, optional
If True (default) pads the string with spaces out to the next multiple of 2880 characters
Returns: - s : str
A string representing a FITS header.
-
totextfile
(fileobj, endcard=False, overwrite=False)[source] [edit on github]¶ Write the header as text to a file or a file-like object.
Equivalent to:
>>> Header.tofile(fileobj, sep='\n', endcard=False, ... padding=False, overwrite=overwrite)
Changed in version 1.3:
overwrite
replaces the deprecatedclobber
argument.See also
-
update
(*args, **kwargs)[source] [edit on github]¶ Update the Header with new keyword values, updating the values of existing keywords and appending new keywords otherwise; similar to
dict.update
.update
accepts either a dict-like object or an iterable. In the former case the keys must be header keywords and the values may be either scalar values or (value, comment) tuples. In the case of an iterable the items must be (keyword, value) tuples or (keyword, value, comment) tuples.Arbitrary arguments are also accepted, in which case the update() is called again with the kwargs dict as its only argument. That is,
>>> header.update(NAXIS1=100, NAXIS2=100)
is equivalent to:
header.update({'NAXIS1': 100, 'NAXIS2': 100})
Warning
As this method works similarly to
dict.update
it is very different from theHeader.update()
method in Astropy v0.1. Use of the old API was deprecated for a long time and is now removed. Most uses of the old API can be replaced as follows:Replace
header.update(keyword, value)
with
header[keyword] = value
Replace
header.update(keyword, value, comment=comment)
with
header[keyword] = (value, comment)
Replace
header.update(keyword, value, before=before_keyword)
with
header.insert(before_keyword, (keyword, value))
Replace
header.update(keyword, value, after=after_keyword)
with
header.insert(after_keyword, (keyword, value), after=True)
See also
Header.set()
which is a new method that provides an interface similar to the oldHeader.update()
and may help make transition a little easier.
-
values
()[source] [edit on github]¶ Like
dict.values()
.
- cards : A list of