DataFits

class sofia_redux.instruments.hawc.datafits.DataFits(filename='', config=None)[source]

Bases: DataParent

Pipeline data FITS object.

The data is stored as a list of (multi-dimensional) images and a similar list of tables. FITS file name and all headers are stored as well.

Initialize data object and variables.

The parent constructor is not called by this function, since this class requires different attribute handling than its parent.

Parameters:
filenamestr, optional

Path to a text file. If specified, data will be loaded immediately from the file.

configconfigobj.ConfigObj, dict, str, or list of str, optional

If specified, the configuration will be loaded.

Attributes Summary

filenamefit

str : Regular expression that matches valid FITS file names.

Methods Summary

copy()

Return a copy of the current object.

copydata(other, dataname)

Copy data into the current object.

copyhead(other[, name, overwrite])

Copy a header into the current object.

delheadval(key[, dataname])

Delete one or more FITS keywords.

getheader([dataname])

Return a stored header.

getheadval(key[, dataname, errmsg])

Get a header value.

imagedel([imagename])

Remove an image.

imageget([imagename])

Return an image.

imageindex([imagename])

Return the index of an image in the current object.

imageset(imagedata[, imagename, ...])

Set an image.

load([filename, hdul])

Load a FITS file into the data object.

loadhead([filename, dataname, hdul])

Load and return the primary header of the FITS file given.

mergehead(other)

Merge the header of another data object to the existing header.

save([filename])

Save the data in the object to the specified file.

setheader(header[, dataname])

Overwrite a stored header.

setheadval(key, value[, comment, dataname])

Set a FITS header keyword.

tableaddcol(colname, array[, tablename, dtype])

Add a column to the table.

tableaddrow(tablerow[, tablename])

Add a row to a data table.

tabledel([tablename])

Remove a table.

tabledelcol(colname[, tablename])

Delete a column of the data table.

tabledelrow(index[, tablename])

Delete a row of the data table.

tableget([tablename])

Return a table.

tableindex([tablename])

Return the index of an table in the current object.

tablemergerows(rows)

Merge several table rows into a single row.

tablemergetables(tables)

Return a new table containing data merged from the input table(s).

tableset(tabledata[, tablename, ...])

Set a table.

to_hdulist([filename, save_tables])

Return an astropy HDUList equivalent to the current data object.

to_header_list([filename])

Return a list of all headers in the current data object.

Attributes Documentation

filenamefit = '\\.(fits|fts)\\Z'

str : Regular expression that matches valid FITS file names.

Methods Documentation

copy()[source]

Return a copy of the current object.

Returns:
DataFits
copydata(other, dataname)[source]

Copy data into the current object.

Copies data (image or table) from another DataFits object to the current object. If an object of that name already exists it is overwritten. Both the data and the header are copied.

Parameters:
otherDataFits

The object to copy from

datanamestr

The name of the extension header to copy.

copyhead(other, name=None, overwrite=True)[source]

Copy a header into the current object.

This function copies all header keywords, comments, and history from other. Will overwrite existing cards, unless overwrite flag is set to False. Exceptions: HISTORY and COMMENT cards are always appended to the end of the list of such keywords present in self.

If name is None, the header from the first HDU (self.header, other.header) will be used.

Parameters:
otherDataFits

The object to copy from.

namestr, optional

The name of the extension header to copy.

overwritebool, optional

If not set, keywords already present in the current header will not be overwritten.

delheadval(key, dataname='')[source]

Delete one or more FITS keywords.

Parameters:
keystr or list of str

The header key to delete.

datanamestr, optional

The header extension to retrieve from. First, if not specified.

Raises:
ValueError

If the key is badly specified.

getheader(dataname='')[source]

Return a stored header.

Parameters:
datanamestr, optional

The name of image/table to return a header from; otherwise the primary header.

Returns:
fits.Header

The requested header.

Raises:
ValueError

If the requested header could not be found.

getheadval(key, dataname='', errmsg=True)[source]

Get a header value.

Returns the value of the requested key from the header. If the keyword is present in the [Header] section of the configuration that value is returned instead. In case that value from the configuration file is itself a header key, the value stored under that key is returned. If the key can not be found an KeyError is produced and a warning is issued.

Parameters:
keystr

The header key to retrieve.

datanamestr, optional

The header extension to retrieve from. First, if not specified.

errmsgbool, optional

If set, an error message will be raised if the keyword could not be found.

Returns:
str, int, float, or bool

The header value.

Raises:
KeyError

If the key is not found, and errmsg = True.

imagedel(imagename=None)[source]

Remove an image.

Parameters:
imagenamestr, optional

The name of the image to delete. The first image will be deleted if not specified.

imageget(imagename=None)[source]

Return an image.

Parameters:
imagenamestr, optional

The name of the requested image. The first image is returned if not provided.

Returns:
array-like

The requested image.

imageindex(imagename=None)[source]

Return the index of an image in the current object.

Given this index, the associated image can be accessed via self.imgdata[index].

Parameters:
imagename: str, optional

The name of the requested image. If not provided, the first image will be returned.

Returns:
int

The image index.

Raises:
ValueError

If the image is not found.

imageset(imagedata, imagename=None, imageheader=None, index=-1)[source]

Set an image.

This should be used to add or replace an image in the current object. The index flag determines the position of the image in the image list.

Parameters:
imagedataarray-like or None

A multi dimensional array containing the image data

imagenamestr or None, optional

The name of the image to set (None for first image)

imageheaderfits.Header

FITS header for the image

indexint, optional

Indicates the position of the image in the image list Ignored if == -1.

load(filename='', hdul=None)[source]

Load a FITS file into the data object.

Loads headers, images, and tables from the file on disk. After loading, self.loaded is set to True.

Parameters:
filenamestr, optional

The name of the file to load. If omitted, self.filename is used.

hdulfits.HDUList, optional

An in-memory HDUList to load, in place of an on-disk file.

loadhead(filename='', dataname='', hdul=None)[source]

Load and return the primary header of the FITS file given.

This also checks for file existence and type. Only the header is loaded into the DataFits object; the data is not loaded.

Parameters:
filenamestr, optional

The name of the file to load. If omitted, self.filename is used.

datanamestr or int, optional

The EXTNAME value or extension number of the header to be loaded If such a header is not found, or dataname==’’, the first header is loaded. This option should be used if the main file information is not in the primary header.

hdulfits.HDUList, optional

An in-memory HDUList to load, in place of an on-disk file.

mergehead(other)[source]

Merge the header of another data object to the existing header.

The merge is between primary headers only.

Header keywords are merged according to the configuration values specified for the ‘headmerge’ section. Options are: FIRST, LAST, MIN, MAX, SUM, OR, AND, CONCATENATE, DEFAULT.

Parameters:
otherDataFits

The DataFits to merge from.

save(filename=None)[source]

Save the data in the object to the specified file.

Existing files are overwritten.

Parameters:
filenamestr, optional

The file name to store in the FILENAME keyword. If not provided, self.filename will be used.

setheader(header, dataname='')[source]

Overwrite a stored header.

Parameters:
headerfits.Header

The FITS header to set.

datanamestr, optional

The name of image/table to return a header from; otherwise the primary header.

setheadval(key, value, comment=None, dataname='')[source]

Set a FITS header keyword.

If no header exists, it will be created and added as the primary header.

Parameters:
keystr

The header key to set.

valuestr, int, float, or bool

The keyword value to set.

commentstr, optional

The comment to set for the keyword.

datanamestr, optional

The header extension to retrieve from. First, if not specified.

tableaddcol(colname, array, tablename=None, dtype=None)[source]

Add a column to the table.

If the table under tablename doesn’t exist, it is created. This is intended to be used with single-dimension columns only.

Parameters:
colnamestr

The new column name.

arrayarray-like

Values for the new column

tablenamestr

The name of the table to add to (None for first table)

dtypetype, optional

Data type for the new column

tableaddrow(tablerow, tablename=None)[source]

Add a row to a data table.

Parameters:
tablerowarray-like

The elements of the row to be added

tablenamestr

The name of the table to add to (None for first table)

tabledel(tablename=None)[source]

Remove a table.

Parameters:
tablenamestr, optional

The name of the table to delete. The first table will be deleted if not specified.

tabledelcol(colname, tablename=None)[source]

Delete a column of the data table.

Parameters:
colname: str or list of str

The name(s) of the column(s) to delete

tablename: str, optional

The name of the table to delete from (None for first table)

tabledelrow(index, tablename=None)[source]

Delete a row of the data table.

Parameters:
index: int

The index of the row to delete.

tablename: str, optional

The name of the table to delete from (None for first table)

tableget(tablename=None)[source]

Return a table.

Parameters:
tablenamestr, optional

The name of the requested table. The first table is returned if not provided.

Returns:
array-like

The requested table.

tableindex(tablename=None)[source]

Return the index of an table in the current object.

Given this index, the associated table can be accessed via self.tabdata[index].

Parameters:
tablename: str, optional

The name of the requested table. If not provided, the first table will be returned.

Returns:
int

The table index.

Raises:
RuntimeError

If there are no tables present.

ValueError

If the specified table is not found.

tablemergerows(rows)[source]

Merge several table rows into a single row.

Each column is merged according to the rules defined in the [table] section of the configuration file. Options are: FIRST, LAST, MIN, MAX, MED, AVG, SUM, WTAVG.

Parameters:
rowsrecord

NumPy record or FITS table rows to merge.

Returns:
record

The merged row.

Raises:
AttributeError

If input rows have incorrect data type.

tablemergetables(tables)[source]

Return a new table containing data merged from the input table(s).

Columns are merged according to the rules defined in the [table] section of the configuration file. Options are: FIRST, LAST, MIN, MAX, MED, AVG, SUM, WTAVG.

Note that each table is assumed to have a single row of data. If you need to merge rows of data, use tablemergerows().

Parameters:
tableslist of BinTableHDU data

Tables should be NumPy records or FITS tables.

Returns:
BinTableHDU

The merged table.

tableset(tabledata, tablename=None, tableheader=None, index=-1)[source]

Set a table.

This should be used to add or replace a table in the current object. The index flag determines the position of the table in the table list.

Parameters:
tabledataarray-like or None

A multi dimensional array containing the table data

tablenamestr or None, optional

The name of the table to set (None for first table)

tableheaderfits.Header

FITS header for the table

indexint, optional

Indicates the position of the table in the table list Ignored if == -1.

to_hdulist(filename=None, save_tables=True)[source]

Return an astropy HDUList equivalent to the current data object.

Also sets pipeline keywords in headers: PIPEVERS, FILENAME, DATE. The pipeline version for PIPEVERS is read from self.pipever, usually set in the DataParent class.

Parameters:
filenamestr, optional

The file name to store in the FILENAME keyword. If not provided, self.filename will be used.

save_tablesbool, optional

If not set, binary tables stored in the current object will not be passed to the output HDUList.

Returns:
fits.HDUList

The HDUList containing all requested data.

to_header_list(filename=None)[source]

Return a list of all headers in the current data object.

Headers are updated with pipeline keywords before returning (PIPEVERS, FILENAME, DATE).

Parameters:
filenamestr, optional

The file name to store in the FILENAME keyword. If not provided, self.filename will be used.

Returns:
list of fits.Header