24__all__ = [
"ReadTextCatalogConfig",
"ReadTextCatalogTask"]
27from astropy.table
import Table
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")
57 fill_values = pexConfig.ListField(
61 doc=(
"A list giving [<match_string>, <fill_value>], which is used to mask"
62 " the given values in the input file. '0' is suggested for the fill value in order to prevent"
63 " changing the column datatype. The default behavior is to fill empty data with zeros. See "
64 "https://docs.astropy.org/en/stable/io/ascii/read.html#bad-or-missing-values for more details."
65 "Use `replace_missing_floats_with_nan` to change floats to NaN instead of <fill_value>.")
67 replace_missing_floats_with_nan = pexConfig.Field(
70 doc=
"If True, replace missing data in float columns with NaN instead of zero. If `fill_values` is "
71 "set, this parameter with replace the floats identified as missing by `fill_values`, and the fill"
72 " value from `fill_values` will be overridden with NaN for floats."
77 """Read an object catalog from a text file
79 _DefaultName = 'readCatalog'
80 ConfigClass = ReadTextCatalogConfig
82 def run(self, filename):
83 """Read an object catalog from the specified text file
88 Path to specified text file
92 A numpy structured array containing the specified columns
95 if self.config.colnames:
97 kwargs[
'names'] = list(self.config.colnames)
99 kwargs[
'data_start'] = self.config.header_lines
102 kwargs[
'header_start'] = self.config.header_lines
104 if self.config.fill_values:
105 kwargs[
'fill_values'] = [list(self.config.fill_values)]
107 table = Table.read(filename, format=self.config.format,
108 delimiter=self.config.delimiter,
112 arr = np.array(table.as_array())
114 if self.config.replace_missing_floats_with_nan:
115 for column
in table.columns:
116 if (table.dtype[column] == np.float32)
or (table.dtype[column] == np.float64):
117 arr[column][table.mask[column]] = np.nan