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 PyFITS 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
txtfile : file path, file object or file-like object, optional
|
Add a blank card.
Parameters: | |
---|---|
value : str, optional
before : str or int, optional
after : str or int, optional
|
Add a COMMENT card.
Parameters: | |
---|---|
value : str
before : str or int, optional
after : str or int, optional
|
Add a HISTORY card.
Parameters: | |
---|---|
value : str
before : str or int, optional
after : str or int, optional
|
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
useblanks : bool, optional
bottom : bool, optional
end : bool, optional
|
Remove all cards from the header.
Make a copy of the Header.
Parameters: | |
---|---|
strip : bool, optional
|
|
Returns: | |
header :
|
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
|
Appends multiple keyword+value cards to the end of the header, similar to list.extend.
Parameters: | |
---|---|
cards : iterable
strip : bool, optional
unique : bool, optional
update : bool, optional
update_first : bool, optional
useblanks, bottom, end : bool, optional
|
Deprecated since version 3.1: This is equivalent to self.extend(Header.fromtextfile(fileobj), update=True, update_first=True). Note that there there is no direct equivalent to the replace=True option since Header.fromtextfile() returns a new Header instance.
Input the header parameters from an ASCII file.
The input header cards will be used to update the current header. Therefore, when an input card key matches a card key that already exists in the header, that card will be updated in place. Any input cards that do not already exist in the header will be added. Cards will not be deleted from the header.
Parameters: | |
---|---|
fileobj : file path, file object or file-like object
replace : bool, optional
|
Similar to Header.fromstring(), but reads the header string from a given file-like object or filename.
Parameters: | |
---|---|
fileobj : str, file-like
sep : str, optional
endcard : bool, optional
padding : bool, optional
|
|
Returns: | |
header :
|
Similar to dict.fromkeys()–creates a new Header 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 :
value : optional
|
|
Returns: | |
header :
|
Creates an HDU header from a byte string containing the entire header data.
Parameters: | |
---|---|
data : str
sep : str, optional
|
|
Returns: | |
header :
|
Equivalent to:
>>> Header.fromfile(fileobj, sep='\n', endcard=False,
... padding=False)
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
default : optional
|
|
Returns: | |
value :
|
Deprecated since version 3.1: Use header['COMMENT'] instead.
Get all comment cards as a list of string texts.
Deprecated since version 3.1: Use header['HISTORY'] instead.
Get all history cards as a list of string texts.
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
start : int, optional
stop : int, optional
|
Inserts a new keyword+value card into the Header at a given location, similar to list.insert.
Parameters: | |
---|---|
key : int, str, or tuple
card : str, tuple
useblanks : bool, optional
after : bool, optional |
Like dict.items().
Like dict.iteritems().
Like dict.iterkeys()–iterating directly over the Header instance has the same behavior.
Like dict.itervalues().
Return a list of keywords in the header in the order they appear–like dict.keys() but ordered.
Works like list.pop() if no arguments or an index argument are supplied; otherwise works like dict.pop().
Similar to dict.popitem().
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: | |
---|---|
value : str
|
Deprecated since version 3.1: Use Header.rename_keyword() instead.
Rename a card’s keyword in the header.
Parameters: | |
---|---|
oldkeyword : str or int
newkeyword : str
force : bool, optional
|
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 PyFITS 3.1.
Note
It should be noted that header.set(keyword, value) and header.set(keyword, value, comment) are equivalent to header[keyword] = value and header[keyword] = (value, comment) respectfully.
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 the only advantage of using Header.set() is that it easily replaces the old usage of Header.update() both conceptually and in terms of function signature.
Parameters: | |
---|---|
keyword : str
value : str, optional
comment : str, optional
before : str, int, optional
after : str, int, optional
|
Similar to dict.setitem().
Deprecated since version 3.1: Use Header.totextfile() instead.
Output the header parameters to a file in ASCII format.
Parameters: | |
---|---|
fileobj : file path, file object or file-like object
clobber : bool
|
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
sep : str, optional
endcard : bool, optional
padding : bool, optional
clobber : bool, optional
|
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
endcard : bool, optional
padding : bool, optional
|
|
Returns: | |
s : string
|
Equivalent to:
>>> Header.tofile(fileobj, sep='\n', endcard=False,
... padding=False, clobber=clobber)
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 the Header.update() method in PyFITS versions prior to 3.1.0. However, support for the old API is also maintained for backwards compatibility. If update() is called with at least two positional arguments then it can be assumed that the old API is being used. Use of the old API should be considered deprecated. 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 old Header.update() and may help make transition a little easier.
For reference, the old documentation for the old Header.update() is provided below:
Update one header card.
If the keyword already exists, it’s value and/or comment will be updated. If it does not exist, a new card will be created and it will be placed before or after the specified location. If no before or after is specified, it will be appended at the end.
Parameters: | |
---|---|
key : str
value : str
comment : str, optional
before : str, int, optional
after : str, int, optional
savecomment : bool, optional
|
Returns a list of the values of all cards in the header.
Deprecated since version 3.1: Use the cards attribute instead.
Returns a CardList object wrapping this Header; provided for backwards compatibility for the old API (where Headers had an underlying CardList).
The underlying physical cards that make up this Header; it can be looked at, but it should not be modified directly.
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'