23 from __future__
import absolute_import, division, print_function
25 __all__ = [
"ReadTextCatalogConfig",
"ReadTextCatalogTask"]
29 import lsst.pex.config
as pexConfig
30 import lsst.pipe.base
as pipeBase
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.' 61 """!Read an object catalog from a text file 63 @anchor ReadTextCatalogTask_ 65 @section meas_algorithms_readTextCatalog_Contents Contents 67 - @ref meas_algorithms_readTextCatalog_Purpose 68 - @ref meas_algorithms_readTextCatalog_Initialize 69 - @ref meas_algorithms_readTextCatalog_Config 70 - @ref meas_algorithms_readTextCatalog_Example 72 @section meas_algorithms_readTextCatalog_Purpose Description 74 Read an object catalog from a text file. Designed to read foreign catalogs 75 so they can be written out in a form suitable for IngestIndexedReferenceTask. 77 @section meas_algorithms_readTextCatalog_Initialize Task initialisation 81 @section meas_algorithms_readTextCatalog_Config Configuration parameters 83 See @ref ReadTextCatalogConfig 85 @section meas_algorithms_readTextCatalog_Example A complete example of using ReadTextCatalogTask 87 Given a file named `table.csv` containing the following: 93 you can read this file with the following code: 95 from lsst.meas.algorithms.readTextCatalogTask import ReadTextCatalogTask 96 task = ReadTextCatalogTask() 97 catalogArray = task.run("table.csv") 99 The resulting `catalogArray` is a numpy structured array containing three fields 100 ("ra", "dec" and "flux") and two rows of data. For more complex cases, 101 config parameters allow you to specify the names of the columns (instead of using automatic discovery) 102 and set the number of rows to skip. 104 _DefaultName =
'readCatalog' 105 ConfigClass = ReadTextCatalogConfig
108 """Read an object catalog from the specified text file 110 @param[in] filename path to text file 111 @return a numpy structured array containing the specified columns 114 if self.config.colnames:
115 names = self.config.colnames
116 arr = np.genfromtxt(filename, dtype=
None, skip_header=self.config.header_lines,
117 delimiter=self.config.delimiter,
122 for name
in arr.dtype.names:
123 value = arr.dtype[name]
124 if value.kind ==
'S':
125 value = np.dtype(
'|U{}'.format(value.itemsize))
126 newDtype.append((name, value))
127 arr = arr.astype(newDtype)
130 return np.atleast_1d(arr)
Read an object catalog from a text file.