24 __all__ = [
"ReadFitsCatalogConfig",
"ReadFitsCatalogTask"]
26 from astropy.table
import Table
33 hdu = pexConfig.Field(
36 doc=
"HDU containing the desired binary table, 0-based but a binary table never occurs in HDU 0",
38 column_map = pexConfig.DictField(
39 doc=
"Mapping of input column name: output column name; each specified column must exist, "
40 "but additional columns in the input data are written using their original name. ",
55 r"""!Read an object catalog from a FITS table
57 @anchor ReadFitsCatalogTask_
59 @section meas_algorithms_readFitsCatalog_Contents Contents
61 - @ref meas_algorithms_readFitsCatalog_Purpose
62 - @ref meas_algorithms_readFitsCatalog_Initialize
63 - @ref meas_algorithms_readFitsCatalog_Config
64 - @ref meas_algorithms_readFitsCatalog_Example
66 @section meas_algorithms_readFitsCatalog_Purpose Description
68 Read an object catalog from a FITS table. Designed to read foreign catalogs
69 so they can be written out in a form suitable for IngestIndexedReferenceTask.
71 @section meas_algorithms_readFitsCatalog_Initialize Task initialisation
75 @section meas_algorithms_readFitsCatalog_Config Configuration parameters
77 See @ref ReadFitsCatalogConfig
79 @section meas_algorithms_readFitsCatalog_Example A complete example of using ReadFitsCatalogTask
81 Run the following code from the main directory of meas_algorithms:
83 from lsst.meas.algorithms.readFitsCatalogTask import ReadFitsCatalogTask
84 filePath = "tests/data/testReadFitsCatalog.fits"
85 task = ReadFitsCatalogTask()
86 catalogArray = task.run(filePath)
88 The resulting `catalogArray` is a numpy structured array containing fields such as "name", "ra" and "dec"
89 and a few rows of data. For more complicated cases config parameters allow you to rename columns
90 and choose which HDU to read.
92 _DefaultName =
'readCatalog'
93 ConfigClass = ReadFitsCatalogConfig
95 def run(self, filename):
96 """Read an object catalog from the specified FITS file
98 @param[in] filename path to FITS file
99 @return a numpy structured array containing the specified columns
102 table = Table.read(filename, hdu=self.config.hdu)
104 raise RuntimeError(
"No data found in %s HDU %s" % (filename, self.config.hdu))
106 if not self.config.column_map:
108 return table.as_array()
110 missingnames = set(self.config.column_map.keys()) - set(table.columns.keys())
112 raise RuntimeError(
"Columns %s in column_map were not found in %s" % (missingnames, filename))
114 for inname, outname
in self.config.column_map.items():
115 table.columns[inname].name = outname
116 return table.as_array()