23 from __future__
import absolute_import, division, print_function
25 __all__ = [
"ReadFitsCatalogConfig",
"ReadFitsCatalogTask"]
27 from astropy.io
import fits
29 import lsst.pex.config
as pexConfig
30 import lsst.pipe.base
as pipeBase
34 hdu = pexConfig.Field(
37 doc=
"HDU containing the desired binary table, 0-based but a binary table never occurs in HDU 0",
39 column_map = pexConfig.DictField(
40 doc=
"Mapping of input column name: output column name; each specified column must exist, "
41 "but additional columns in the input data are written using their original name. ",
56 """!Read an object catalog from a FITS table
58 @anchor ReadFitsCatalogTask_
60 @section meas_algorithms_readFitsCatalog_Contents Contents
62 - @ref meas_algorithms_readFitsCatalog_Purpose
63 - @ref meas_algorithms_readFitsCatalog_Initialize
64 - @ref meas_algorithms_readFitsCatalog_Config
65 - @ref meas_algorithms_readFitsCatalog_Example
67 @section meas_algorithms_readFitsCatalog_Purpose Description
69 Read an object catalog from a FITS table. Designed to read foreign catalogs
70 so they can be written out in a form suitable for IngestIndexedReferenceTask.
72 @section meas_algorithms_readFitsCatalog_Initialize Task initialisation
76 @section meas_algorithms_readFitsCatalog_Config Configuration parameters
78 See @ref ReadFitsCatalogConfig
80 @section meas_algorithms_readFitsCatalog_Example A complete example of using ReadFitsCatalogTask
82 Run the following code from the main directory of meas_algorithms:
84 from lsst.meas.algorithms.readFitsCatalogTask import ReadFitsCatalogTask
85 filePath = "tests/data/testReadFitsCatalog.fits"
86 task = ReadFitsCatalogTask()
87 catalogArray = task.run(filePath)
89 The resulting `catalogArray` is a numpy structured array containing fields such as "name", "ra" and "dec"
90 and a few rows of data. For more complicated cases config parameters allow you to rename columns
91 and choose which HDU to read.
93 _DefaultName =
'readCatalog'
94 ConfigClass = ReadFitsCatalogConfig
96 def run(self, filename):
97 """Read an object catalog from the specified FITS file
99 @param[in] filename path to FITS file
100 @return a numpy structured array containing the specified columns
102 with fits.open(filename)
as f:
103 hdu = f[self.config.hdu]
105 raise RuntimeError(
"No data found in %s HDU %s" % (filename, self.config.hdu))
107 raise RuntimeError(
"%s HDU %s is an image" % (filename, self.config.hdu))
109 if not self.config.column_map:
113 missingnames = set(self.config.column_map.keys()) - set(hdu.columns.names)
115 raise RuntimeError(
"Columns %s in column_map were not found in %s" % (missingnames, filename))
117 for inname, outname
in self.config.column_map.items():
118 hdu.columns[inname].name = outname
Read an object catalog from a FITS table.