Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-20163: Use PixelAreaBoundedField in fgcmcal calibration outputs #22

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 36 additions & 14 deletions python/lsst/fgcmcal/fgcmBuildStars.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from lsst.meas.algorithms.sourceSelector import sourceSelectorRegistry

from .fgcmLoadReferenceCatalog import FgcmLoadReferenceCatalogTask
from .utilities import computeApproxPixelAreaFields

import fgcm

Expand Down Expand Up @@ -136,13 +137,22 @@ class FgcmBuildStarsConfig(pexConfig.Config):
applyJacobian = pexConfig.Field(
doc="Apply Jacobian correction?",
dtype=bool,
deprecated=("This field is no longer used, and has been deprecated by DM-20163. "
"It will be removed after v20."),
default=False
)
jacobianName = pexConfig.Field(
doc="Name of field with jacobian correction",
dtype=str,
deprecated=("This field is no longer used, and has been deprecated by DM-20163. "
"It will be removed after v20."),
default="base_Jacobian_value"
)
doApplyWcsJacobian = pexConfig.Field(
doc="Apply the jacobian of the WCS to the star observations prior to fit?",
dtype=bool,
default=True
)
psfCandidateName = pexConfig.Field(
doc="Name of field with psf candidate flag for propagation",
dtype=str,
Expand Down Expand Up @@ -340,17 +350,20 @@ def runDataRef(self, butler, dataRefs):

groupedDataRefs = self.findAndGroupDataRefs(butler, dataRefs)

camera = butler.get('camera')

# Make the visit catalog if necessary
if not butler.datasetExists('fgcmVisitCatalog'):
# we need to build visitCat
visitCat = self.fgcmMakeVisitCatalog(butler, groupedDataRefs)
visitCat = self.fgcmMakeVisitCatalog(camera, groupedDataRefs)
else:
self.log.info("Found fgcmVisitCatalog.")
visitCat = butler.get('fgcmVisitCatalog')

# Compile all the stars
if not butler.datasetExists('fgcmStarObservations'):
fgcmStarObservationCat = self.fgcmMakeAllStarObservations(groupedDataRefs, visitCat)
fgcmStarObservationCat = self.fgcmMakeAllStarObservations(groupedDataRefs,
visitCat)
else:
self.log.info("Found fgcmStarObservations")
fgcmStarObservationCat = butler.get('fgcmStarObservations')
Expand All @@ -370,28 +383,29 @@ def runDataRef(self, butler, dataRefs):
if fgcmRefCat is not None:
butler.put(fgcmRefCat, 'fgcmReferenceStars')

def fgcmMakeVisitCatalog(self, butler, groupedDataRefs):
def fgcmMakeVisitCatalog(self, camera, groupedDataRefs):
"""
Make a visit catalog with all the keys from each visit

Parameters
----------
butler: `lsst.daf.persistence.Butler`
camera: `lsst.afw.cameraGeom.Camera`
Camera from the butler
groupedDataRefs: `dict`
Dictionary with visit keys, and `list`s of `lsst.daf.persistence.ButlerDataRef`
Dictionary with visit keys, and `list`s of
`lsst.daf.persistence.ButlerDataRef`

Returns
-------
visitCat: `afw.table.BaseCatalog`
"""

camera = butler.get('camera')
nCcd = len(camera)

schema = self._makeFgcmVisitSchema(nCcd)

visitCat = afwTable.BaseCatalog(schema)
visitCat.table.preallocate(len(groupedDataRefs))
visitCat.reserve(len(groupedDataRefs))

self._fillVisitCatalog(visitCat, groupedDataRefs)

Expand All @@ -404,9 +418,10 @@ def _fillVisitCatalog(self, visitCat, groupedDataRefs):
Parameters
----------
visitCat: `afw.table.BaseCatalog`
Catalog with schema from _createFgcmVisitSchema()
Catalog with schema from _makeFgcmVisitSchema()
groupedDataRefs: `dict`
Dictionary with visit keys, and `list`s of `lsst.daf.persistence.ButlerDataRef`
Dictionary with visit keys, and `list`s of
`lsst.daf.persistence.ButlerDataRef`
"""

bbox = geom.BoxI(geom.PointI(0, 0), geom.PointI(1, 1))
Expand Down Expand Up @@ -573,8 +588,7 @@ def fgcmMakeAllStarObservations(self, groupedDataRefs, visitCat):
Returns
-------
fgcmStarObservations: `afw.table.BaseCatalog`
Full catalog of good observations. Only returned if
returnCatalog is True.
Full catalog of good observations.
"""

startTime = time.time()
Expand All @@ -589,6 +603,8 @@ def fgcmMakeAllStarObservations(self, groupedDataRefs, visitCat):
for ccdIndex, detector in enumerate(camera):
ccdMapping[detector.getId()] = ccdIndex

approxPixelAreaFields = computeApproxPixelAreaFields(camera)

sourceMapper = self._makeSourceMapper(sourceSchema)

# We also have a temporary catalog that will accumulate aperture measurements
Expand Down Expand Up @@ -648,12 +664,18 @@ def fgcmMakeAllStarObservations(self, groupedDataRefs, visitCat):
tempCat[instMagErrKey][:] = k * (sources[instFluxErrKey][goodSrc.selected] /
sources[instFluxKey][goodSrc.selected])

if self.config.applyJacobian:
# Compute the jacobian from an approximate PixelAreaBoundedField
tempCat['jacobian'] = approxPixelAreaFields[ccdId].evaluate(tempCat['x'],
tempCat['y'])

# Apply the jacobian if configured
if self.config.doApplyWcsJacobian:
tempCat[instMagKey][:] -= 2.5 * np.log10(tempCat['jacobian'][:])

fullCatalog.extend(tempCat)

# And the aperture information
# This does not need the jacobian because it is all locally relative
tempAperCat = afwTable.BaseCatalog(aperVisitCatalog.schema)
tempAperCat.reserve(goodSrc.selected.sum())
tempAperCat.extend(sources[goodSrc.selected], mapper=aperMapper)
Expand Down Expand Up @@ -900,8 +922,6 @@ def _makeSourceMapper(self, sourceSchema):
# map to ra/dec
sourceMapper.addMapping(sourceSchema['coord_ra'].asKey(), 'ra')
sourceMapper.addMapping(sourceSchema['coord_dec'].asKey(), 'dec')
sourceMapper.addMapping(sourceSchema[self.config.jacobianName].asKey(),
'jacobian')
sourceMapper.addMapping(sourceSchema['slot_Centroid_x'].asKey(), 'x')
sourceMapper.addMapping(sourceSchema['slot_Centroid_y'].asKey(), 'y')
sourceMapper.addMapping(sourceSchema[self.config.psfCandidateName].asKey(),
Expand All @@ -916,6 +936,8 @@ def _makeSourceMapper(self, sourceSchema):
"instMag", type=np.float32, doc="Instrumental magnitude")
sourceMapper.editOutputSchema().addField(
"instMagErr", type=np.float32, doc="Instrumental magnitude error")
sourceMapper.editOutputSchema().addField(
"jacobian", type=np.float32, doc="Relative pixel scale from wcs jacobian")

return sourceMapper

Expand Down
13 changes: 6 additions & 7 deletions python/lsst/fgcmcal/fgcmCalibrateTract.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ def runDataRef(self, butler, dataRefs):

# Note that we will need visitCat at the end of the procedure for the outputs
groupedDataRefs = self.fgcmBuildStars.findAndGroupDataRefs(butler, dataRefs)
visitCat = self.fgcmBuildStars.fgcmMakeVisitCatalog(butler, groupedDataRefs)
camera = butler.get('camera')
visitCat = self.fgcmBuildStars.fgcmMakeVisitCatalog(camera, groupedDataRefs)
fgcmStarObservationCat = self.fgcmBuildStars.fgcmMakeAllStarObservations(groupedDataRefs,
visitCat)

Expand Down Expand Up @@ -420,14 +421,12 @@ def runDataRef(self, butler, dataRefs):
rep = fgcmFitCycle.fgcmPars.compReservedRawRepeatability[i] * 1000.0
self.log.info(" Band %s, repeatability: %.2f mmag" % (band, rep))

# Do the outputs. Need to keep track of tract, blah.
# Do the outputs. Need to keep track of tract.

if self.config.fgcmFitCycle.superStarSubCcd or self.config.fgcmFitCycle.ccdGraySubCcd:
chebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_CHEB'].shape[1]
else:
chebSize = 0
superStarChebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_SSTAR_CHEB'].shape[1]
zptChebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_CHEB'].shape[1]

zptSchema = makeZptSchema(chebSize)
zptSchema = makeZptSchema(superStarChebSize, zptChebSize)
zptCat = makeZptCat(zptSchema, fgcmFitCycle.fgcmZpts.zpStruct)

atmSchema = makeAtmSchema()
Expand Down
25 changes: 20 additions & 5 deletions python/lsst/fgcmcal/fgcmFitCycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,22 @@ class FgcmFitCycleConfig(pexConfig.Config):
dtype=bool,
default=False,
)
autoPhotometricCutNSig = pexConfig.Field(
doc=("Number of sigma for automatic computation of (low) photometric cut. "
"Cut is based on exposure gray width (per band), unless "
"useRepeatabilityForExpGrayCuts is set, in which case the star "
"repeatability is used (also per band)."),
dtype=float,
default=3.0,
)
autoHighCutNSig = pexConfig.Field(
doc=("Number of sigma for automatic computation of (high) outlier cut. "
"Cut is based on exposure gray width (per band), unless "
"useRepeatabilityForExpGrayCuts is set, in which case the star "
"repeatability is used (also per band)."),
dtype=float,
default=4.0,
)
quietMode = pexConfig.Field(
doc="Be less verbose with logging.",
dtype=bool,
Expand Down Expand Up @@ -1024,11 +1040,10 @@ def _persistFgcmDatasets(self, butler, fgcmFitCycle):

# Save the zeropoint information and atmospheres only if desired
if self.outputZeropoints:
if self.config.superStarSubCcd or self.config.ccdGraySubCcd:
chebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_CHEB'].shape[1]
else:
chebSize = 0
zptSchema = makeZptSchema(chebSize)
superStarChebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_SSTAR_CHEB'].shape[1]
zptChebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_CHEB'].shape[1]

zptSchema = makeZptSchema(superStarChebSize, zptChebSize)
zptCat = makeZptCat(zptSchema, fgcmFitCycle.fgcmZpts.zpStruct)

butler.put(zptCat, 'fgcmZeropoints', fgcmcycle=self.config.cycleNumber)
Expand Down