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