lsst.meas.algorithms  13.0-20-g02a2147
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 import numpy as np
28 
29 from astropy.io import fits
30 
31 import lsst.pex.config as pexConfig
32 import lsst.pipe.base as pipeBase
33 
34 
35 class ReadFitsCatalogConfig(pexConfig.Config):
36  hdu = pexConfig.Field(
37  dtype=int,
38  default=1,
39  doc="HDU containing the desired binary table, 0-based but a binary table never occurs in HDU 0",
40  )
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. ",
44  keytype=str,
45  itemtype=str,
46  default={},
47  )
48 
49 ## \addtogroup LSST_task_documentation
50 ## \{
51 ## \page ReadFitsCatalogTask
52 ## \ref ReadFitsCatalogTask_ "ReadFitsCatalogTask"
53 ## \copybrief ReadFitsCatalogTask
54 ## \}
55 
56 
57 class ReadFitsCatalogTask(pipeBase.Task):
58  """!Read an object catalog from a FITS table
59 
60  @anchor ReadFitsCatalogTask_
61 
62  @section meas_algorithms_readFitsCatalog_Contents Contents
63 
64  - @ref meas_algorithms_readFitsCatalog_Purpose
65  - @ref meas_algorithms_readFitsCatalog_Initialize
66  - @ref meas_algorithms_readFitsCatalog_Config
67  - @ref meas_algorithms_readFitsCatalog_Example
68 
69  @section meas_algorithms_readFitsCatalog_Purpose Description
70 
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.
73 
74  @section meas_algorithms_readFitsCatalog_Initialize Task initialisation
75 
76  @copydoc \_\_init\_\_
77 
78  @section meas_algorithms_readFitsCatalog_Config Configuration parameters
79 
80  See @ref ReadFitsCatalogConfig
81 
82  @section meas_algorithms_readFitsCatalog_Example A complete example of using ReadFitsCatalogTask
83 
84  Run the following code from the main directory of meas_algorithms:
85 
86  from lsst.meas.algorithms.readFitsCatalogTask import ReadFitsCatalogTask
87  filePath = "tests/data/testReadFitsCatalog.fits"
88  task = ReadFitsCatalogTask()
89  catalogArray = task.run(filePath)
90 
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.
94  """
95  _DefaultName = 'readCatalog'
96  ConfigClass = ReadFitsCatalogConfig
97 
98  def run(self, filename):
99  """Read an object catalog from the specified FITS file
100 
101  @param[in] filename path to FITS file
102  @return a numpy structured array containing the specified columns
103  """
104  with fits.open(filename) as f:
105  hdu = f[self.config.hdu]
106  if hdu.data is None:
107  raise RuntimeError("No data found in %s HDU %s" % (filename, self.config.hdu))
108  if hdu.is_image:
109  raise RuntimeError("%s HDU %s is an image" % (filename, self.config.hdu))
110 
111  if not self.config.column_map:
112  # take the data as it is
113  return hdu.data
114 
115  missingnames = set(self.config.column_map.keys()) - set(hdu.columns.names)
116  if missingnames:
117  raise RuntimeError("Columns %s in column_map were not found in %s" % (missingnames, filename))
118 
119  for inname, outname in self.config.column_map.items():
120  hdu.columns[inname].name = outname
121  return hdu.data
122