LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
deblendAndMeasure.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008-2013 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 
24 import lsst.pex.config as pexConfig
25 import lsst.pex.exceptions as pexExceptions
26 import lsst.pipe.base as pipeBase
27 import lsst.daf.base as dafBase
28 import lsst.afw.geom as afwGeom
29 import lsst.afw.math as afwMath
30 import lsst.afw.table as afwTable
31 from lsst.meas.algorithms import SourceMeasurementTask
32 from lsst.meas.deblender import SourceDeblendTask
33 from lsst.pipe.tasks.calibrate import CalibrateTask
34 
35 
36 class DeblendAndMeasureConfig(pexConfig.Config):
37  doDeblend = pexConfig.Field(dtype=bool, default=True, doc = "Deblend sources?")
38  doMeasurement = pexConfig.Field(dtype=bool, default=True, doc = "Measure sources?")
39  doWriteSources = pexConfig.Field(dtype=bool, default=True, doc = "Write sources?")
40  doWriteHeavyFootprintsInSources = pexConfig.Field(dtype=bool, default=False,
41  doc = "Include HeavyFootprint data in source table?")
42 
43  sourceOutputFile = pexConfig.Field(dtype=str, default=None, doc="Write sources to given filename (default: use butler)", optional=True)
44 
45  deblend = pexConfig.ConfigurableField(
46  target = SourceDeblendTask,
47  doc = "Split blended sources into their components",
48  )
49  measurement = pexConfig.ConfigurableField(
50  target = SourceMeasurementTask,
51  doc = "Final source measurement on low-threshold detections",
52  )
53 
54 
55 class DeblendAndMeasureTask(pipeBase.CmdLineTask):
56  ConfigClass = DeblendAndMeasureConfig
57  _DefaultName = "deblendAndMeasure"
58 
59  def writeConfig(self, *args, **kwargs):
60  pass
61 
62  def __init__(self, **kwargs):
63  pipeBase.CmdLineTask.__init__(self, **kwargs)
64 
65  @pipeBase.timeMethod
66  def run(self, dataRef):
67  self.log.info("Processing %s" % (dataRef.dataId))
68  calexp = dataRef.get('calexp')
69  srcs = dataRef.get('src')
70  print 'Calexp:', calexp
71  print 'srcs:', srcs
72 
73  ## FIXME -- this whole mapping business is very fragile -- it
74  ## seems to fail, eg, if you don't set "-c
75  ## doMeasurement=False" when creating the input 'srcs' list.
76 
77  mapper = afwTable.SchemaMapper(srcs.getSchema())
78  # map all the existing fields
79  mapper.addMinimalSchema(srcs.getSchema(), True)
80  schema = mapper.getOutputSchema()
82  if self.config.doDeblend:
83  self.makeSubtask("deblend", schema=schema)
84  if self.config.doMeasurement:
85  self.makeSubtask("measurement", schema=schema, algMetadata=self.algMetadata)
86  self.schema = schema
87 
88  parents = []
89  for src in srcs:
90  if src.getParent() == 0:
91  parents.append(src)
92 
93  outsources = afwTable.SourceCatalog(schema)
94  outsources.reserve(len(parents))
95  outsources.extend(parents, mapper=mapper)
96  srcs = outsources
97  print len(srcs), 'sources before deblending'
98 
99  if self.config.doDeblend:
100  self.deblend.run(calexp, srcs, calexp.getPsf())
101 
102  if self.config.doMeasurement:
103  self.measurement.run(calexp, srcs)
104 
105  if srcs is not None and self.config.doWriteSources:
106  sourceWriteFlags = (0 if self.config.doWriteHeavyFootprintsInSources
107  else afwTable.SOURCE_IO_NO_HEAVY_FOOTPRINTS)
108  print 'Writing "src" outputs'
109  if self.config.sourceOutputFile:
110  srcs.writeFits(self.config.sourceOutputFile, flags=sourceWriteFlags)
111  else:
112  dataRef.put(srcs, 'src', flags=sourceWriteFlags)
113 
114 if __name__ == '__main__':
115  DeblendAndMeasureTask.parseAndRun()
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:55
algMetadata
FIXME – this whole mapping business is very fragile – it seems to fail, eg, if you don&#39;t set &quot;-c do...