lsst.meas.algorithms  13.0-24-g22030a45+6
readFitsCatalogTask.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <https://www.lsstcorp.org/LegalNotices/>.
22 #
23 from __future__ import absolute_import, division, print_function
24 
25 __all__ = ["ReadFitsCatalogConfig", "ReadFitsCatalogTask"]
26 
27 from astropy.io import fits
28 
29 import lsst.pex.config as pexConfig
30 import lsst.pipe.base as pipeBase
31 
32 
33 class ReadFitsCatalogConfig(pexConfig.Config):
34  hdu = pexConfig.Field(
35  dtype=int,
36  default=1,
37  doc="HDU containing the desired binary table, 0-based but a binary table never occurs in HDU 0",
38  )
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. ",
42  keytype=str,
43  itemtype=str,
44  default={},
45  )
46 
47 
53 
54 
55 class ReadFitsCatalogTask(pipeBase.Task):
56  """!Read an object catalog from a FITS table
57 
58  @anchor ReadFitsCatalogTask_
59 
60  @section meas_algorithms_readFitsCatalog_Contents Contents
61 
62  - @ref meas_algorithms_readFitsCatalog_Purpose
63  - @ref meas_algorithms_readFitsCatalog_Initialize
64  - @ref meas_algorithms_readFitsCatalog_Config
65  - @ref meas_algorithms_readFitsCatalog_Example
66 
67  @section meas_algorithms_readFitsCatalog_Purpose Description
68 
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.
71 
72  @section meas_algorithms_readFitsCatalog_Initialize Task initialisation
73 
74  @copydoc \_\_init\_\_
75 
76  @section meas_algorithms_readFitsCatalog_Config Configuration parameters
77 
78  See @ref ReadFitsCatalogConfig
79 
80  @section meas_algorithms_readFitsCatalog_Example A complete example of using ReadFitsCatalogTask
81 
82  Run the following code from the main directory of meas_algorithms:
83 
84  from lsst.meas.algorithms.readFitsCatalogTask import ReadFitsCatalogTask
85  filePath = "tests/data/testReadFitsCatalog.fits"
86  task = ReadFitsCatalogTask()
87  catalogArray = task.run(filePath)
88 
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.
92  """
93  _DefaultName = 'readCatalog'
94  ConfigClass = ReadFitsCatalogConfig
95 
96  def run(self, filename):
97  """Read an object catalog from the specified FITS file
98 
99  @param[in] filename path to FITS file
100  @return a numpy structured array containing the specified columns
101  """
102  with fits.open(filename) as f:
103  hdu = f[self.config.hdu]
104  if hdu.data is None:
105  raise RuntimeError("No data found in %s HDU %s" % (filename, self.config.hdu))
106  if hdu.is_image:
107  raise RuntimeError("%s HDU %s is an image" % (filename, self.config.hdu))
108 
109  if not self.config.column_map:
110  # take the data as it is
111  return hdu.data
112 
113  missingnames = set(self.config.column_map.keys()) - set(hdu.columns.names)
114  if missingnames:
115  raise RuntimeError("Columns %s in column_map were not found in %s" % (missingnames, filename))
116 
117  for inname, outname in self.config.column_map.items():
118  hdu.columns[inname].name = outname
119  return hdu.data