Tables¶
BinTableHDU¶
-
class
pyfits.BinTableHDU(data=None, header=None, name=None, uint=False)¶ Bases:
pyfits.hdu.table._TableBaseHDUBinary table HDU class.
Parameters: header : Header instance
header to be used
data : array
data to be used
name : str
name to be populated in
EXTNAMEkeyworduint : bool, optional
set to
Trueif the table contains unsigned integer columns.-
add_checksum(when=None, override_datasum=False, blocking='standard', checksum_keyword='CHECKSUM', datasum_keyword='DATASUM')¶ Add the
CHECKSUMandDATASUMcards to this HDU with the values set to the checksum calculated for the HDU and the data respectively. The addition of theDATASUMcard may be overridden.Parameters: when : str, optional
comment string for the cards; by default the comments will represent the time when the checksum was calculated
override_datasum : bool, optional
add the
CHECKSUMcard onlyblocking: str, optional
“standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
checksum_keyword: str, optional
The name of the header keyword to store the checksum value in; this is typically ‘CHECKSUM’ per convention, but there exist use cases in which a different keyword should be used
datasum_keyword: str, optional
See
checksum_keywordNotes
For testing purposes, first call
add_datasumwith awhenargument, then calladd_checksumwith awhenargument andoverride_datasumset toTrue. This will provide consistent comments for both cards and enable the generation of aCHECKSUMcard with a consistent value.
-
add_datasum(when=None, blocking='standard', datasum_keyword='DATASUM')¶ Add the
DATASUMcard to this HDU with the value set to the checksum calculated for the data.Parameters: when : str, optional
Comment string for the card that by default represents the time when the checksum was calculated
blocking: str, optional
“standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
datasum_keyword: str, optional
The name of the header keyword to store the datasum value in; this is typically ‘DATASUM’ per convention, but there exist use cases in which a different keyword should be used
Returns: checksum : int
The calculated datasum
Notes
For testing purposes, provide a
whenargument to enable the comment value in the card to remain consistent. This will enable the generation of aCHECKSUMcard with a consistent value.
-
copy()¶ Make a copy of the table HDU, both header and data are copied.
-
dump(datafile=None, cdfile=None, hfile=None, clobber=False)¶ Dump the table HDU to a file in ASCII format. The table may be dumped in three separate files, one containing column definitions, one containing header parameters, and one for table data.
Parameters: datafile : file path, file object or file-like object, optional
Output data file. The default is the root name of the fits file associated with this HDU appended with the extension
.txt.cdfile : file path, file object or file-like object, optional
Output column definitions file. The default is
None, no column definitions output is produced.hfile : file path, file object or file-like object, optional
Output header parameters file. The default is
None, no header parameters output is produced.clobber : bool
Overwrite the output files if they exist.
Notes
The primary use for the
dumpmethod is to allow viewing and editing the table data and parameters in a standard text editor. Theloadmethod can be used to create a new table from the three plain text (ASCII) files.datafile: Each line of the data file represents one row of table data. The data is output one column at a time in column order. If a column contains an array, each element of the column array in the current row is output before moving on to the next column. Each row ends with a new line.
Integer data is output right-justified in a 21-character field followed by a blank. Floating point data is output right justified using ‘g’ format in a 21-character field with 15 digits of precision, followed by a blank. String data that does not contain whitespace is output left-justified in a field whose width matches the width specified in the
TFORMheader parameter for the column, followed by a blank. When the string data contains whitespace characters, the string is enclosed in quotation marks (""). For the last data element in a row, the trailing blank in the field is replaced by a new line character.For column data containing variable length arrays (‘P’ format), the array data is preceded by the string
'VLA_Length= 'and the integer length of the array for that row, left-justified in a 21-character field, followed by a blank.Note
This format does not support variable length arrays using the (‘Q’ format) due to difficult to overcome ambiguities. What this means is that this file format cannot support VLA columns in tables stored in files that are over 2 GB in size.
For column data representing a bit field (‘X’ format), each bit value in the field is output right-justified in a 21-character field as 1 (for true) or 0 (for false).
cdfile: Each line of the column definitions file provides the definitions for one column in the table. The line is broken up into 8, sixteen-character fields. The first field provides the column name (
TTYPEn). The second field provides the column format (TFORMn). The third field provides the display format (TDISPn). The fourth field provides the physical units (TUNITn). The fifth field provides the dimensions for a multidimensional array (TDIMn). The sixth field provides the value that signifies an undefined value (TNULLn). The seventh field provides the scale factor (TSCALn). The eighth field provides the offset value (TZEROn). A field value of""is used to represent the case where no value is provided.hfile: Each line of the header parameters file provides the definition of a single HDU header card as represented by the card image.
-
filebytes()¶ Calculates and returns the number of bytes that this HDU will write to a file.
-
fileinfo()¶ Returns a dictionary detailing information about the locations of this HDU within any associated file. The values are only valid after a read or write of the associated file with no intervening changes to the
HDUList.Returns: dict or None
The dictionary details information about the locations of this HDU within an associated file. Returns
Nonewhen the HDU is not associated with a file.Dictionary contents:
Key Value file File object associated with the HDU filemode Mode in which the file was opened (readonly, copyonwrite, update, append, ostream) hdrLoc Starting byte location of header in file datLoc Starting byte location of data block in file datSpan Data size including padding
-
fromstring(data, fileobj=None, offset=0, checksum=False, ignore_missing_end=False, **kwargs)¶ Creates a new HDU object of the appropriate type from a string containing the HDU’s entire header and, optionally, its data.
Note: When creating a new HDU from a string without a backing file object, the data of that HDU may be read-only. It depends on whether the underlying string was an immutable Python str/bytes object, or some kind of read-write memory buffer such as a
memoryview.Parameters: data : str, bytearray, memoryview, ndarray
A byte string contining the HDU’s header and, optionally, its data. If
fileobjis not specified, and the length ofdataextends beyond the header, then the trailing data is taken to be the HDU’s data. Iffileobjis specified then the trailing data is ignored.fileobj : file, optional
The file-like object that this HDU was read from.
offset : int, optional
If
fileobjis specified, the offset into the file-like object at which this HDU begins.checksum : bool, optional
Check the HDU’s checksum and/or datasum.
ignore_missing_end : bool, optional
Ignore a missing end card in the header data. Note that without the end card the end of the header can’t be found, so the entire data is just assumed to be the header.
kwargs : optional
May contain additional keyword arguments specific to an HDU type. Any unrecognized kwargs are simply ignored.
-
classmethod
load(datafile, cdfile=None, hfile=None, replace=False, header=None)¶ Create a table from the input ASCII files. The input is from up to three separate files, one containing column definitions, one containing header parameters, and one containing column data.
The column definition and header parameters files are not required. When absent the column definitions and/or header parameters are taken from the header object given in the header argument; otherwise sensible defaults are inferred (though this mode is not recommended).
Parameters: datafile : file path, file object or file-like object
Input data file containing the table data in ASCII format.
cdfile : file path, file object, file-like object, optional
Input column definition file containing the names, formats, display formats, physical units, multidimensional array dimensions, undefined values, scale factors, and offsets associated with the columns in the table. If
None, the column definitions are taken from the current values in this object.hfile : file path, file object, file-like object, optional
Input parameter definition file containing the header parameter definitions to be associated with the table. If
None, the header parameter definitions are taken from the current values in this objects header.replace : bool
When
True, indicates that the entire header should be replaced with the contents of the ASCII file instead of just updating the current header.header : Header object
When the cdfile and hfile are missing, use this Header object in the creation of the new table and HDU. Otherwise this Header supercedes the keywords from hfile, which is only used to update values not present in this Header, unless
replace=Truein which this Header’s values are completely replaced with the values from hfile.Notes
The primary use for the
loadmethod is to allow the input of ASCII data that was edited in a standard text editor of the table data and parameters. Thedumpmethod can be used to create the initial ASCII files.datafile: Each line of the data file represents one row of table data. The data is output one column at a time in column order. If a column contains an array, each element of the column array in the current row is output before moving on to the next column. Each row ends with a new line.
Integer data is output right-justified in a 21-character field followed by a blank. Floating point data is output right justified using ‘g’ format in a 21-character field with 15 digits of precision, followed by a blank. String data that does not contain whitespace is output left-justified in a field whose width matches the width specified in the
TFORMheader parameter for the column, followed by a blank. When the string data contains whitespace characters, the string is enclosed in quotation marks (""). For the last data element in a row, the trailing blank in the field is replaced by a new line character.For column data containing variable length arrays (‘P’ format), the array data is preceded by the string
'VLA_Length= 'and the integer length of the array for that row, left-justified in a 21-character field, followed by a blank.Note
This format does not support variable length arrays using the (‘Q’ format) due to difficult to overcome ambiguities. What this means is that this file format cannot support VLA columns in tables stored in files that are over 2 GB in size.
For column data representing a bit field (‘X’ format), each bit value in the field is output right-justified in a 21-character field as 1 (for true) or 0 (for false).
cdfile: Each line of the column definitions file provides the definitions for one column in the table. The line is broken up into 8, sixteen-character fields. The first field provides the column name (
TTYPEn). The second field provides the column format (TFORMn). The third field provides the display format (TDISPn). The fourth field provides the physical units (TUNITn). The fifth field provides the dimensions for a multidimensional array (TDIMn). The sixth field provides the value that signifies an undefined value (TNULLn). The seventh field provides the scale factor (TSCALn). The eighth field provides the offset value (TZEROn). A field value of""is used to represent the case where no value is provided.hfile: Each line of the header parameters file provides the definition of a single HDU header card as represented by the card image.
-
readfrom(fileobj, checksum=False, ignore_missing_end=False, **kwargs)¶ Read the HDU from a file. Normally an HDU should be opened with
pyfits.openwhich reads the entire HDU list in a FITS file. But this method is still provided for symmetry withwriteto.Parameters: fileobj : file object or file-like object
Input FITS file. The file’s seek pointer is assumed to be at the beginning of the HDU.
checksum : bool
If
True, verifies that bothDATASUMandCHECKSUMcard values (when present in the HDU header) match the header and data of all HDU’s in the file.ignore_missing_end : bool
Do not issue an exception when opening a file that is missing an
ENDcard in the last header.
-
req_cards(keyword, pos, test, fix_value, option, errlist)¶ Check the existence, location, and value of a required
Card.Parameters: keyword : str
The keyword to validate
pos : int, callable
If an
int, this specifies the exact location this card should have in the header. Remember that Python is zero-indexed, so this meanspos=0requires the card to be the first card in the header. If given a callable, it should take one argument–the actual position of the keyword–and returnTrueorFalse. This can be used for custom evaluation. For example ifpos=lambda idx: idx > 10this will check that the keyword’s index is greater than 10.test : callable
fix_value : str, int, float, complex, bool, None
A valid value for a FITS keyword to to use if the given
testfails to replace an invalid value. In other words, this provides a default value to use as a replacement if the keyword’s current value is invalid. IfNone, there is no replacement value and the keyword is unfixable.option : str
Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". See Verification options for more info.errlist : list
A list of validation errors already found in the FITS file; this is used primarily for the validation system to collect errors across multiple HDUs and multiple calls to
req_cards.Notes
If
pos=None, the card can be anywhere in the header. If the card does not exist, the new card will have thefix_valueas its value when created. Also check the card’s value by using thetestargument.
-
run_option(option='warn', err_text='', fix_text='Fixed.', fix=None, fixable=True)¶ Execute the verification with selected option.
-
update()¶ Update header keywords to reflect recent changes of columns.
-
update_ext_name(*args, **kwargs)¶ Deprecated since version 3.2: Use the
.nameattribute orHeader.setinstead.Update the extension name associated with the HDU.
If the keyword already exists in the Header, 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
beforeorafteris specified, it will be appended at the end.Parameters: value : str
Value to be used for the new extension name
comment : str, optional
To be used for updating, default=None.
before : str or int, optional
Name of the keyword, or index of the
Cardbefore which the new card will be placed in the Header. The argumentbeforetakes precedence overafterif both are specified.after : str or int, optional
Name of the keyword, or index of the
Cardafter which the new card will be placed in the Headersavecomment : bool, optional
When
True, preserve the current comment for an existing keyword. The argumentsavecommenttakes precedence overcommentif both specified. Ifcommentis not specified then the current comment will automatically be preserved.
-
update_ext_version(*args, **kwargs)¶ Deprecated since version 3.2: Use the
.verattribute orHeader.setinstead.Update the extension version associated with the HDU.
If the keyword already exists in the Header, 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
beforeorafteris specified, it will be appended at the end.Parameters: value : str
Value to be used for the new extension version
comment : str, optional
To be used for updating; default=None.
before : str or int, optional
Name of the keyword, or index of the
Cardbefore which the new card will be placed in the Header. The argumentbeforetakes precedence overafterif both are specified.after : str or int, optional
Name of the keyword, or index of the
Cardafter which the new card will be placed in the Header.savecomment : bool, optional
When
True, preserve the current comment for an existing keyword. The argumentsavecommenttakes precedence overcommentif both specified. Ifcommentis not specified then the current comment will automatically be preserved.
-
verify(option='warn')¶ Verify all values in the instance.
Parameters: option : str
Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". See Verification options for more info.
-
verify_checksum(blocking='standard')¶ Verify that the value in the
CHECKSUMkeyword matches the value calculated for the current HDU CHECKSUM.- blocking: str, optional
- “standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
Returns: valid : int
- 0 - failure
- 1 - success
- 2 - no
CHECKSUMkeyword present
-
verify_datasum(blocking='standard')¶ Verify that the value in the
DATASUMkeyword matches the value calculated for theDATASUMof the current HDU data.- blocking: str, optional
- “standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
Returns: valid : int
- 0 - failure
- 1 - success
- 2 - no
DATASUMkeyword present
-
writeto(name, output_verify='exception', clobber=False, checksum=False)¶ Works similarly to the normal writeto(), but prepends a default
PrimaryHDUare required by extension HDUs (which cannot stand on their own).
-
size¶ Size (in bytes) of the data portion of the HDU.
-
TableHDU¶
-
class
pyfits.TableHDU(data=None, header=None, name=None)¶ Bases:
pyfits.hdu.table._TableBaseHDUFITS ASCII table extension HDU class.
-
add_checksum(when=None, override_datasum=False, blocking='standard', checksum_keyword='CHECKSUM', datasum_keyword='DATASUM')¶ Add the
CHECKSUMandDATASUMcards to this HDU with the values set to the checksum calculated for the HDU and the data respectively. The addition of theDATASUMcard may be overridden.Parameters: when : str, optional
comment string for the cards; by default the comments will represent the time when the checksum was calculated
override_datasum : bool, optional
add the
CHECKSUMcard onlyblocking: str, optional
“standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
checksum_keyword: str, optional
The name of the header keyword to store the checksum value in; this is typically ‘CHECKSUM’ per convention, but there exist use cases in which a different keyword should be used
datasum_keyword: str, optional
See
checksum_keywordNotes
For testing purposes, first call
add_datasumwith awhenargument, then calladd_checksumwith awhenargument andoverride_datasumset toTrue. This will provide consistent comments for both cards and enable the generation of aCHECKSUMcard with a consistent value.
-
add_datasum(when=None, blocking='standard', datasum_keyword='DATASUM')¶ Add the
DATASUMcard to this HDU with the value set to the checksum calculated for the data.Parameters: when : str, optional
Comment string for the card that by default represents the time when the checksum was calculated
blocking: str, optional
“standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
datasum_keyword: str, optional
The name of the header keyword to store the datasum value in; this is typically ‘DATASUM’ per convention, but there exist use cases in which a different keyword should be used
Returns: checksum : int
The calculated datasum
Notes
For testing purposes, provide a
whenargument to enable the comment value in the card to remain consistent. This will enable the generation of aCHECKSUMcard with a consistent value.
-
copy()¶ Make a copy of the table HDU, both header and data are copied.
-
filebytes()¶ Calculates and returns the number of bytes that this HDU will write to a file.
-
fileinfo()¶ Returns a dictionary detailing information about the locations of this HDU within any associated file. The values are only valid after a read or write of the associated file with no intervening changes to the
HDUList.Returns: dict or None
The dictionary details information about the locations of this HDU within an associated file. Returns
Nonewhen the HDU is not associated with a file.Dictionary contents:
Key Value file File object associated with the HDU filemode Mode in which the file was opened (readonly, copyonwrite, update, append, ostream) hdrLoc Starting byte location of header in file datLoc Starting byte location of data block in file datSpan Data size including padding
-
fromstring(data, fileobj=None, offset=0, checksum=False, ignore_missing_end=False, **kwargs)¶ Creates a new HDU object of the appropriate type from a string containing the HDU’s entire header and, optionally, its data.
Note: When creating a new HDU from a string without a backing file object, the data of that HDU may be read-only. It depends on whether the underlying string was an immutable Python str/bytes object, or some kind of read-write memory buffer such as a
memoryview.Parameters: data : str, bytearray, memoryview, ndarray
A byte string contining the HDU’s header and, optionally, its data. If
fileobjis not specified, and the length ofdataextends beyond the header, then the trailing data is taken to be the HDU’s data. Iffileobjis specified then the trailing data is ignored.fileobj : file, optional
The file-like object that this HDU was read from.
offset : int, optional
If
fileobjis specified, the offset into the file-like object at which this HDU begins.checksum : bool, optional
Check the HDU’s checksum and/or datasum.
ignore_missing_end : bool, optional
Ignore a missing end card in the header data. Note that without the end card the end of the header can’t be found, so the entire data is just assumed to be the header.
kwargs : optional
May contain additional keyword arguments specific to an HDU type. Any unrecognized kwargs are simply ignored.
-
readfrom(fileobj, checksum=False, ignore_missing_end=False, **kwargs)¶ Read the HDU from a file. Normally an HDU should be opened with
pyfits.openwhich reads the entire HDU list in a FITS file. But this method is still provided for symmetry withwriteto.Parameters: fileobj : file object or file-like object
Input FITS file. The file’s seek pointer is assumed to be at the beginning of the HDU.
checksum : bool
If
True, verifies that bothDATASUMandCHECKSUMcard values (when present in the HDU header) match the header and data of all HDU’s in the file.ignore_missing_end : bool
Do not issue an exception when opening a file that is missing an
ENDcard in the last header.
-
req_cards(keyword, pos, test, fix_value, option, errlist)¶ Check the existence, location, and value of a required
Card.Parameters: keyword : str
The keyword to validate
pos : int, callable
If an
int, this specifies the exact location this card should have in the header. Remember that Python is zero-indexed, so this meanspos=0requires the card to be the first card in the header. If given a callable, it should take one argument–the actual position of the keyword–and returnTrueorFalse. This can be used for custom evaluation. For example ifpos=lambda idx: idx > 10this will check that the keyword’s index is greater than 10.test : callable
fix_value : str, int, float, complex, bool, None
A valid value for a FITS keyword to to use if the given
testfails to replace an invalid value. In other words, this provides a default value to use as a replacement if the keyword’s current value is invalid. IfNone, there is no replacement value and the keyword is unfixable.option : str
Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". See Verification options for more info.errlist : list
A list of validation errors already found in the FITS file; this is used primarily for the validation system to collect errors across multiple HDUs and multiple calls to
req_cards.Notes
If
pos=None, the card can be anywhere in the header. If the card does not exist, the new card will have thefix_valueas its value when created. Also check the card’s value by using thetestargument.
-
run_option(option='warn', err_text='', fix_text='Fixed.', fix=None, fixable=True)¶ Execute the verification with selected option.
-
update()¶ Update header keywords to reflect recent changes of columns.
-
update_ext_name(*args, **kwargs)¶ Deprecated since version 3.2: Use the
.nameattribute orHeader.setinstead.Update the extension name associated with the HDU.
If the keyword already exists in the Header, 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
beforeorafteris specified, it will be appended at the end.Parameters: value : str
Value to be used for the new extension name
comment : str, optional
To be used for updating, default=None.
before : str or int, optional
Name of the keyword, or index of the
Cardbefore which the new card will be placed in the Header. The argumentbeforetakes precedence overafterif both are specified.after : str or int, optional
Name of the keyword, or index of the
Cardafter which the new card will be placed in the Headersavecomment : bool, optional
When
True, preserve the current comment for an existing keyword. The argumentsavecommenttakes precedence overcommentif both specified. Ifcommentis not specified then the current comment will automatically be preserved.
-
update_ext_version(*args, **kwargs)¶ Deprecated since version 3.2: Use the
.verattribute orHeader.setinstead.Update the extension version associated with the HDU.
If the keyword already exists in the Header, 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
beforeorafteris specified, it will be appended at the end.Parameters: value : str
Value to be used for the new extension version
comment : str, optional
To be used for updating; default=None.
before : str or int, optional
Name of the keyword, or index of the
Cardbefore which the new card will be placed in the Header. The argumentbeforetakes precedence overafterif both are specified.after : str or int, optional
Name of the keyword, or index of the
Cardafter which the new card will be placed in the Header.savecomment : bool, optional
When
True, preserve the current comment for an existing keyword. The argumentsavecommenttakes precedence overcommentif both specified. Ifcommentis not specified then the current comment will automatically be preserved.
-
verify(option='warn')¶ Verify all values in the instance.
Parameters: option : str
Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". See Verification options for more info.
-
verify_checksum(blocking='standard')¶ Verify that the value in the
CHECKSUMkeyword matches the value calculated for the current HDU CHECKSUM.- blocking: str, optional
- “standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
Returns: valid : int
- 0 - failure
- 1 - success
- 2 - no
CHECKSUMkeyword present
-
verify_datasum(blocking='standard')¶ Verify that the value in the
DATASUMkeyword matches the value calculated for theDATASUMof the current HDU data.- blocking: str, optional
- “standard” or “nonstandard”, compute sum 2880 bytes at a time, or not
Returns: valid : int
- 0 - failure
- 1 - success
- 2 - no
DATASUMkeyword present
-
writeto(name, output_verify='exception', clobber=False, checksum=False)¶ Works similarly to the normal writeto(), but prepends a default
PrimaryHDUare required by extension HDUs (which cannot stand on their own).
-
size¶ Size (in bytes) of the data portion of the HDU.
-
Column¶
-
class
pyfits.Column(name=None, format=None, unit=None, null=None, bscale=None, bzero=None, disp=None, start=None, dim=None, array=None, ascii=None)¶ Bases:
objectClass which contains the definition of one column, e.g.
ttype,tform, etc. and the array containing values for the column.Construct a
Columnby specifying attributes. All attributes exceptformatcan be optional.Parameters: name : str, optional
column name, corresponding to
TTYPEkeywordformat : str, optional
column format, corresponding to
TFORMkeywordunit : str, optional
column unit, corresponding to
TUNITkeywordnull : str, optional
null value, corresponding to
TNULLkeywordbscale : int-like, optional
bscale value, corresponding to
TSCALkeywordbzero : int-like, optional
bzero value, corresponding to
TZEROkeyworddisp : str, optional
display format, corresponding to
TDISPkeywordstart : int, optional
column starting position (ASCII table only), corresponding to
TBCOLkeyworddim : str, optional
column dimension corresponding to
TDIMkeywordarray : iterable, optional
a
list,numpy.ndarray(or other iterable that can be used to initialize an ndarray) providing intial data for this column. The array will be automatically converted, if possible, to the data format of the column. In the case were non-trivialbscaleand/orbzeroarguments are given, the values in the array must be the physical values–that is, the values of column as if the scaling has already been applied (the array stored on the column object will then be converted back to its storage values).ascii : bool, optional
set
Trueif this describes a column for an ASCII table; this may be required to disambiguate the column format
ColDefs¶
-
class
pyfits.ColDefs(input, tbtype='BinTableHDU')¶ Bases:
objectColumn definitions class.
It has attributes corresponding to the
Columnattributes (e.g.ColDefshas the attributenameswhileColumnhasname). Each attribute inColDefsis a list of corresponding attribute values from allColumnobjects.Parameters: input :
An existing table HDU, an existing ColDefs, or recarray
**(Deprecated) tbtype** : str, optional
which table HDU,
"BinTableHDU"(default) or"TableHDU"(text table). Now ColDefs for a normal (binary) table by default, but converted automatically to ASCII table ColDefs in the appropriate contexts (namely, when creating an ASCII table).-
change_attrib(col_name, attrib, new_value)¶ Change an attribute (in the
KEYWORD_ATTRIBUTESlist) of aColumn.Parameters: col_name : str or int
The column name or index to change
attrib : str
The attribute name
value : object
The new value for the attribute
-
change_name(col_name, new_name)¶ Change a
Column‘s name.Parameters: col_name : str
The current name of the column
new_name : str
The new name of the column
-
change_unit(col_name, new_unit)¶ Change a
Column‘s unit.Parameters: col_name : str or int
The column name or index
new_unit : str
The new unit for the column
-
del_col(col_name)¶ Delete (the definition of) one
Column.- col_name : str or int
- The column’s name or index
-
info(attrib='all', output=None)¶ Get attribute(s) information of the column definition.
Parameters: attrib : str
Can be one or more of the attributes listed in
pyfits.column.KEYWORD_ATTRIBUTES. The default is"all"which will print out all attributes. It forgives plurals and blanks. If there are two or more attribute names, they must be separated by comma(s).output : file, optional
Notes
This function doesn’t return anything by default; it just prints to stdout.
-
FITS_rec¶
-
class
pyfits.FITS_rec¶ Bases:
numpy.recarrayFITS record array class.
FITS_recis the data part of a table HDU’s data part. This is a layer over therecarray, so we can deal with scaled columns.It inherits all of the standard methods from
numpy.ndarray.-
copy(order='C')¶ The Numpy documentation lies;
numpy.ndarray.copyis not equivalent tonumpy.copy. Differences include that it re-views the copied array as self’s ndarray subclass, as though it were taking a slice; this means__array_finalize__is called and the copy shares all the array attributes (including._convert!). So we need to make a deep copy of all those attributes so that the two arrays truly do not share any data.
-
classmethod
from_columns(columns, nrows=0, fill=False)¶ Given a
ColDefsobject of unknown origin, initialize a newFITS_recobject.Parameters: columns : sequence of
Columnor aColDefsThe columns from which to create the table data. If these columns have data arrays attached that data may be used in initializing the new table. Otherwise the input columns will be used as a template for a new table with the requested number of rows.
nrows : int
Number of rows in the new table. If the input columns have data associated with them, the size of the largest input column is used. Otherwise the default is 0.
fill : bool
-
columns¶ A user-visible accessor for the coldefs.
-
FITS_record¶
-
class
pyfits.FITS_record(input, row=0, start=None, end=None, step=None, base=None, **kwargs)¶ Bases:
objectFITS record class.
FITS_recordis used to access records of theFITS_recobject. This will allow us to deal with scaled columns. It also handles conversion/scaling of columns in ASCII tables. TheFITS_recordclass expects aFITS_recobject as input.Parameters: input : array
The array to wrap.
row : int, optional
The starting logical row of the array.
start : int, optional
The starting column in the row associated with this object. Used for subsetting the columns of the
FITS_recobject.end : int, optional
The ending column in the row associated with this object. Used for subsetting the columns of the
FITS_recobject.-
field(field)¶ Get the field data of the record.
-
setfield(field, value)¶ Set the field data of the record.
-
Table Functions¶
new_table()¶
-
pyfits.new_table(*args, **kwargs)¶ Deprecated since version 3.2: Use
FITS_rec.from_columns()to create a newFITS_recdata object from the input columns to pass into the constructor forBinTableHDUorTableHDUinstead.Create a new table from the input column definitions.
Warning: Creating a new table using this method creates an in-memory copy of all the column arrays in the input. This is because if they are separate arrays they must be combined into a single contiguous array.
If the column data is already in a single contiguous array (such as an existing record array) it may be better to create a
BinTableHDUinstance directly. See the PyFITS documentation for more details.Parameters: input : sequence of
Columnor aColDefsThe data to create a table from
header :
HeaderinstanceHeader to be used to populate the non-required keywords
nrows : int
Number of rows in the new table
fill : bool
tbtype : str or type
Table type to be created (
BinTableHDUorTableHDU) or the class name as a string. Currently onlyBinTableHDUandTableHDU(ASCII tables) are supported.
tabledump()¶
-
pyfits.tabledump(filename, datafile=None, cdfile=None, hfile=None, ext=1, clobber=False)¶ Dump a table HDU to a file in ASCII format. The table may be dumped in three separate files, one containing column definitions, one containing header parameters, and one for table data.
Parameters: filename : file path, file object or file-like object
Input fits file.
datafile : file path, file object or file-like object, optional
Output data file. The default is the root name of the input fits file appended with an underscore, followed by the extension number (ext), followed by the extension
.txt.cdfile : file path, file object or file-like object, optional
Output column definitions file. The default is
None, no column definitions output is produced.hfile : file path, file object or file-like object, optional
Output header parameters file. The default is
None, no header parameters output is produced.ext : int
The number of the extension containing the table HDU to be dumped.
clobber : bool
Overwrite the output files if they exist.
Notes
The primary use for the
tabledumpfunction is to allow editing in a standard text editor of the table data and parameters. Thetcreatefunction can be used to reassemble the table from the three ASCII files.datafile: Each line of the data file represents one row of table data. The data is output one column at a time in column order. If a column contains an array, each element of the column array in the current row is output before moving on to the next column. Each row ends with a new line.
Integer data is output right-justified in a 21-character field followed by a blank. Floating point data is output right justified using ‘g’ format in a 21-character field with 15 digits of precision, followed by a blank. String data that does not contain whitespace is output left-justified in a field whose width matches the width specified in the
TFORMheader parameter for the column, followed by a blank. When the string data contains whitespace characters, the string is enclosed in quotation marks (""). For the last data element in a row, the trailing blank in the field is replaced by a new line character.For column data containing variable length arrays (‘P’ format), the array data is preceded by the string
'VLA_Length= 'and the integer length of the array for that row, left-justified in a 21-character field, followed by a blank.Note
This format does not support variable length arrays using the (‘Q’ format) due to difficult to overcome ambiguities. What this means is that this file format cannot support VLA columns in tables stored in files that are over 2 GB in size.
For column data representing a bit field (‘X’ format), each bit value in the field is output right-justified in a 21-character field as 1 (for true) or 0 (for false).
cdfile: Each line of the column definitions file provides the definitions for one column in the table. The line is broken up into 8, sixteen-character fields. The first field provides the column name (
TTYPEn). The second field provides the column format (TFORMn). The third field provides the display format (TDISPn). The fourth field provides the physical units (TUNITn). The fifth field provides the dimensions for a multidimensional array (TDIMn). The sixth field provides the value that signifies an undefined value (TNULLn). The seventh field provides the scale factor (TSCALn). The eighth field provides the offset value (TZEROn). A field value of""is used to represent the case where no value is provided.hfile: Each line of the header parameters file provides the definition of a single HDU header card as represented by the card image.
tableload()¶
-
pyfits.tableload(datafile, cdfile, hfile=None)¶ Create a table from the input ASCII files. The input is from up to three separate files, one containing column definitions, one containing header parameters, and one containing column data. The header parameters file is not required. When the header parameters file is absent a minimal header is constructed.
Parameters: datafile : file path, file object or file-like object
Input data file containing the table data in ASCII format.
cdfile : file path, file object or file-like object
Input column definition file containing the names, formats, display formats, physical units, multidimensional array dimensions, undefined values, scale factors, and offsets associated with the columns in the table.
hfile : file path, file object or file-like object, optional
Input parameter definition file containing the header parameter definitions to be associated with the table. If
None, a minimal header is constructed.Notes
The primary use for the
tableloadfunction is to allow the input of ASCII data that was edited in a standard text editor of the table data and parameters. The tabledump function can be used to create the initial ASCII files.datafile: Each line of the data file represents one row of table data. The data is output one column at a time in column order. If a column contains an array, each element of the column array in the current row is output before moving on to the next column. Each row ends with a new line.
Integer data is output right-justified in a 21-character field followed by a blank. Floating point data is output right justified using ‘g’ format in a 21-character field with 15 digits of precision, followed by a blank. String data that does not contain whitespace is output left-justified in a field whose width matches the width specified in the
TFORMheader parameter for the column, followed by a blank. When the string data contains whitespace characters, the string is enclosed in quotation marks (""). For the last data element in a row, the trailing blank in the field is replaced by a new line character.For column data containing variable length arrays (‘P’ format), the array data is preceded by the string
'VLA_Length= 'and the integer length of the array for that row, left-justified in a 21-character field, followed by a blank.Note
This format does not support variable length arrays using the (‘Q’ format) due to difficult to overcome ambiguities. What this means is that this file format cannot support VLA columns in tables stored in files that are over 2 GB in size.
For column data representing a bit field (‘X’ format), each bit value in the field is output right-justified in a 21-character field as 1 (for true) or 0 (for false).
cdfile: Each line of the column definitions file provides the definitions for one column in the table. The line is broken up into 8, sixteen-character fields. The first field provides the column name (
TTYPEn). The second field provides the column format (TFORMn). The third field provides the display format (TDISPn). The fourth field provides the physical units (TUNITn). The fifth field provides the dimensions for a multidimensional array (TDIMn). The sixth field provides the value that signifies an undefined value (TNULLn). The seventh field provides the scale factor (TSCALn). The eighth field provides the offset value (TZEROn). A field value of""is used to represent the case where no value is provided.hfile: Each line of the header parameters file provides the definition of a single HDU header card as represented by the card image.