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-45815: Add utility functions to access gain and readnoise #340

Merged
merged 3 commits into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions python/lsst/ip/isr/isrFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"trimToMatchCalibBBox",
"updateVariance",
"widenSaturationTrails",
"getExposureGains",
"getExposureReadNoises",
]

import math
Expand Down Expand Up @@ -1222,3 +1224,67 @@ def countMaskedPixels(maskedIm, maskPlane):
maskBit = maskedIm.mask.getPlaneBitMask(maskPlane)
nPix = numpy.where(numpy.bitwise_and(maskedIm.mask.array, maskBit))[0].flatten().size
return nPix


def getExposureGains(exposure):
"""Get the per-amplifier gains used for this exposure.

Parameters
----------
exposure : `lsst.afw.image.Exposure`
The exposure to find gains for.

Returns
-------
gains : `dict` [`str` `float`]
Dictionary of gain values, keyed by amplifier name.
kadrlica marked this conversation as resolved.
Show resolved Hide resolved
Returns empty dict when detector is None.
"""
det = exposure.getDetector()
if det is None:
return dict()
kadrlica marked this conversation as resolved.
Show resolved Hide resolved

metadata = exposure.getMetadata()
gains = {}
for amp in det:
ampName = amp.getName()
# The key may use the new LSST ISR or the old LSST prefix
if (key1 := f"LSST ISR GAIN {ampName}") in metadata:
gains[ampName] = metadata[key1]
elif (key2 := f"LSST GAIN {ampName}") in metadata:
Alex-Broughton marked this conversation as resolved.
Show resolved Hide resolved
gains[ampName] = metadata[key2]
else:
gains[ampName] = amp.getGain()
return gains


def getExposureReadNoises(exposure):
"""Get the per-amplifier read noise used for this exposure.

Parameters
----------
exposure : `lsst.afw.image.Exposure`
The exposure to find read noise for.

Returns
-------
readnoises : `dict` [`str` `float`]
Dictionary of read noise values, keyed by amplifier name.
Returns empty dict when detector is None.
"""
det = exposure.getDetector()
if det is None:
return dict()
kadrlica marked this conversation as resolved.
Show resolved Hide resolved

metadata = exposure.getMetadata()
readnoises = {}
for amp in det:
ampName = amp.getName()
# The key may use the new LSST ISR or the old LSST prefix
if (key1 := f"LSST ISR READNOISE {ampName}") in metadata:
readnoises[ampName] = metadata[key1]
elif (key2 := f"LSST READNOISE {ampName}") in metadata:
readnoises[ampName] = metadata[key2]
else:
readnoises[ampName] = amp.getReadNoise()
return readnoises
42 changes: 42 additions & 0 deletions tests/test_isrFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,48 @@ def test_countMaskedPixels(self):
mi.mask[noDataBox] ^= NODATABIT # XOR to reset what we did
self.assertEqual(ipIsr.countMaskedPixels(mi, 'NO_DATA'), 0)

def test_getExposureGains(self):
exposure = self.inputExp.clone()
metadata = exposure.metadata
amps = exposure.getDetector().getAmplifiers()

# Check the default values
values = ipIsr.getExposureGains(exposure).values()
self.assertEqual(list(values), len(amps)*[1.0])

# Set values using old ISR keys
for amp in amps:
metadata[f"LSST GAIN {amp.getName()}"] = 2.0
values = ipIsr.getExposureGains(exposure).values()
self.assertEqual(list(values), len(amps)*[2.0])

# Set values using new ISR keys
for amp in amps:
metadata[f"LSST ISR GAIN {amp.getName()}"] = 3.0
values = ipIsr.getExposureGains(exposure).values()
self.assertEqual(list(values), len(amps)*[3.0])

def test_getExposureReadNoises(self):
exposure = self.inputExp.clone()
metadata = exposure.metadata
amps = exposure.getDetector().getAmplifiers()

# Check the default values
values = ipIsr.getExposureReadNoises(exposure).values()
self.assertEqual(list(values), len(amps)*[10.0])

# Set values using old ISR keys
for amp in amps:
metadata[f"LSST READNOISE {amp.getName()}"] = 2.0
values = ipIsr.getExposureReadNoises(exposure).values()
self.assertEqual(list(values), len(amps)*[2.0])

# Set values using new ISR keys
for amp in amps:
metadata[f"LSST ISR READNOISE {amp.getName()}"] = 3.0
values = ipIsr.getExposureReadNoises(exposure).values()
self.assertEqual(list(values), len(amps)*[3.0])


class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass
Expand Down
Loading