24 __all__ = [
"ReadTextCatalogConfig",
"ReadTextCatalogTask"]
27 from astropy.table
import Table
29 import lsst.pex.config
as pexConfig
34 header_lines = pexConfig.Field(
37 doc=
'Number of lines to skip when reading the text reference file.' 39 colnames = pexConfig.ListField(
42 doc=
"An ordered list of column names to use in ingesting the catalog. " 43 "With an empty list, column names will be discovered from the first line " 44 "after the skipped header lines." 46 delimiter = pexConfig.Field(
49 doc=
'Delimiter to use when reading text reference files. Comma is default.' 51 format = pexConfig.Field(
54 doc=(
"Format of files to read, from the astropy.table I/O list here:" 55 "http://docs.astropy.org/en/stable/io/unified.html#built-in-table-readers-writers")
67 r"""!Read an object catalog from a text file 69 @anchor ReadTextCatalogTask_ 71 @section meas_algorithms_readTextCatalog_Contents Contents 73 - @ref meas_algorithms_readTextCatalog_Purpose 74 - @ref meas_algorithms_readTextCatalog_Initialize 75 - @ref meas_algorithms_readTextCatalog_Config 76 - @ref meas_algorithms_readTextCatalog_Example 78 @section meas_algorithms_readTextCatalog_Purpose Description 80 Read an object catalog from a text file. Designed to read foreign catalogs 81 so they can be written out in a form suitable for IngestIndexedReferenceTask. 83 The file is assumed to be encoded as UTF-8 (which is compatible with ASCII). 85 @section meas_algorithms_readTextCatalog_Initialize Task initialisation 89 @section meas_algorithms_readTextCatalog_Config Configuration parameters 91 See @ref ReadTextCatalogConfig 93 @section meas_algorithms_readTextCatalog_Example A complete example of using ReadTextCatalogTask 95 Given a file named `table.csv` containing the following: 101 you can read this file with the following code: 103 from lsst.meas.algorithms.readTextCatalogTask import ReadTextCatalogTask 104 task = ReadTextCatalogTask() 105 catalogArray = task.run("table.csv") 107 The resulting `catalogArray` is a numpy structured array containing three fields 108 ("ra", "dec" and "flux") and two rows of data. For more complex cases, 109 config parameters allow you to specify the names of the columns (instead of using automatic discovery) 110 and set the number of rows to skip. 112 _DefaultName =
'readCatalog' 113 ConfigClass = ReadTextCatalogConfig
116 """Read an object catalog from the specified text file 118 @param[in] filename path to text file 119 @return a numpy structured array containing the specified columns 122 if self.config.colnames:
123 kwargs[
'names'] = self.config.colnames
125 kwargs[
'data_start'] = self.config.header_lines
128 kwargs[
'header_start'] = self.config.header_lines
131 return np.array(Table.read(filename, format=self.config.format,
132 delimiter=self.config.delimiter,
133 **kwargs).as_array())
Read an object catalog from a text file.