Skip to content

Commit

Permalink
Add config options to read PhotoCalib or Wcs from calexps
Browse files Browse the repository at this point in the history
by WriteSourceTable to retrieve local PhotoCalib/Wcs columns.
This feature is limited and only for creating transformed
source tables from older afw src tables that do not contain
local calib columns.
  • Loading branch information
yalsayyad committed Apr 28, 2020
1 parent d80f8f7 commit 62a7c3d
Showing 1 changed file with 76 additions and 7 deletions.
83 changes: 76 additions & 7 deletions python/lsst/pipe/tasks/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
import lsst.afw.table as afwTable
from lsst.meas.base import SingleFrameMeasurementTask
from lsst.pipe.base import CmdLineTask, ArgumentParser, DataIdContainer
from lsst.coadd.utils.coaddDataIdContainer import CoaddDataIdContainer

Expand Down Expand Up @@ -202,7 +204,18 @@ def writeMetadata(self, dataRefList):


class WriteSourceTableConfig(pexConfig.Config):
pass
doApplyExternalPhotoCalib = pexConfig.Field(
dtype=bool,
default=False,
doc=("Add local photoCalib columns from the calexp.photoCalib? Should only set True if "
"generating Source Tables from older src tables which do not already have local calib columns")
)
doApplyExternalSkyWcs = pexConfig.Field(
dtype=bool,
default=False,
doc=("Add local WCS columns from the calexp.wcs? Should only set True if "
"generating Source Tables from older src tables which do not already have local calib columns")
)


class WriteSourceTableTask(CmdLineTask):
Expand All @@ -213,6 +226,9 @@ class WriteSourceTableTask(CmdLineTask):

def runDataRef(self, dataRef):
src = dataRef.get('src')
if self.config.doApplyExternalPhotoCalib or self.config.doApplyExternalSkyWcs:
src = self.addCalibColumns(src, dataRef)

ccdVisitId = dataRef.get('ccdExposureId')
result = self.run(src, ccdVisitId=ccdVisitId)
dataRef.put(result.table, 'source')
Expand All @@ -222,7 +238,7 @@ def run(self, catalog, ccdVisitId=None):
Parameters
----------
catalog: `lsst.afw.table.SourceCatalog`
catalog: `afwTable.SourceCatalog`
catalog to be converted
ccdVisitId: `int`
ccdVisitId to be added as a column
Expand All @@ -238,13 +254,66 @@ def run(self, catalog, ccdVisitId=None):
df['ccdVisitId'] = ccdVisitId
return pipeBase.Struct(table=ParquetTable(dataFrame=df))

def writeMetadata(self, dataRef):
"""No metadata to write.
def addCalibColumns(self, catalog, dataRef):
"""Add columns with local calibration evaluated at each centroid
This exists for the purpose of converting old src catalogs
(which don't have the expected local calib columns) to Source Tables.
It is unnecessarily I/O intensive to always do it here on purpose
because we don't really need the pixels.
Parameters
----------
catalog: `afwTable.SourceCatalog`
catalog to which calib columns will be added
dataRef: `lsst.daf.persistence.ButlerDataRef
for fetching the calibs from disk.
Returns
-------
newCat: `afwTable.SourceCatalog`
Source Catalog with requested local calib columns
"""
pass
mapper = afwTable.SchemaMapper(catalog.schema)
measureConfig = SingleFrameMeasurementTask.ConfigClass()
exposure = dataRef.get('calexp')

mapper = afwTable.SchemaMapper(catalog.schema)
mapper.addMinimalSchema(catalog.schema, True)
schema = mapper.getOutputSchema()

exposureIdInfo = dataRef.get("expIdInfo")
measureConfig.plugins.names = []
if self.config.doApplyExternalSkyWcs:
source = "calexp_wcs"
self.log.debug("Applying external skyWcs to %s from %s", dataRef.dataId, source)
skyWcs = dataRef.get(source)
exposure.setWcs(skyWcs)
plugin = 'base_LocalWcs'
if plugin in schema:
raise RuntimeError(f"{plugin} already in src catalog. Set doApplyExternalSkyWcs=False")
else:
measureConfig.plugins.names.add(plugin)

if self.config.doApplyExternalPhotoCalib:
source = "calexp_photoCalib"
self.log.debug("Applying external photoCalib to %s from %s", dataRef.dataId, source)
photoCalib = dataRef.get(source)
exposure.setPhotoCalib(photoCalib)
plugin = 'base_LocalPhotoCalib'
if plugin in schema:
raise RuntimeError(f"{plugin} already in src catalog. Set doApplyExternalPhotoCalib=False")
else:
measureConfig.plugins.names.add(plugin)

def writeConfig(self, butler, clobber=False, doBackup=True):
"""No config to write.
measurement = SingleFrameMeasurementTask(config=measureConfig, schema=schema)
newCat = afwTable.SourceCatalog(schema)
newCat.extend(catalog, mapper=mapper)
measurement.run(measCat=newCat, exposure=exposure, exposureId=exposureIdInfo.expId)
return newCat

def writeMetadata(self, dataRef):
"""No metadata to write.
"""
pass

Expand Down

0 comments on commit 62a7c3d

Please sign in to comment.