Coverage for python/lsst/obs/lsst/assembly.py: 12%
107 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-20 10:51 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-20 10:51 +0000
1# This file is part of obs_lsst
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
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 GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22__all__ = ("attachRawWcsFromBoresight", "fixAmpGeometry", "assembleUntrimmedCcd",
23 "fixAmpsAndAssemble", "readRawAmps")
25import logging
26from contextlib import contextmanager
27import numpy as np
28import lsst.afw.image as afwImage
29from lsst.obs.base import bboxFromIraf, MakeRawVisitInfoViaObsInfo, createInitialSkyWcs
30from lsst.geom import Box2I, Extent2I
31from lsst.ip.isr import AssembleCcdTask
32from astro_metadata_translator import ObservationInfo
34logger = logging.getLogger(__name__)
37def attachRawWcsFromBoresight(exposure, dataIdForErrMsg=None):
38 """Attach a WCS by extracting boresight, rotation, and camera geometry from
39 an Exposure.
41 Parameters
42 ----------
43 exposure : `lsst.afw.image.Exposure`
44 Image object with attached metadata and detector components.
46 Return
47 ------
48 attached : `bool`
49 If True, a WCS component was successfully created and attached to
50 ``exposure``.
51 """
52 md = exposure.getMetadata()
53 # Use the generic version since we do not have a mapper available to
54 # tell us a specific translator to use.
55 obsInfo = ObservationInfo(md)
56 visitInfo = MakeRawVisitInfoViaObsInfo.observationInfo2visitInfo(obsInfo, log=logger)
57 exposure.getInfo().setVisitInfo(visitInfo)
58 exposure.info.id = obsInfo.detector_exposure_id
60 # LATISS (and likely others) need flipping, DC2 etc do not
61 flipX = False
62 if obsInfo.instrument in ("LATISS",):
63 flipX = True
65 if visitInfo.getBoresightRaDec().isFinite():
66 exposure.setWcs(createInitialSkyWcs(visitInfo, exposure.getDetector(), flipX=flipX))
67 return True
69 if obsInfo.observation_type == "science":
70 logger.warning("Unable to set WCS from header as RA/Dec/Angle are unavailable%s",
71 ("" if dataIdForErrMsg is None else " for dataId %s" % dataIdForErrMsg))
72 return False
75def fixAmpGeometry(inAmp, bbox, metadata, logCmd=None):
76 """Make sure a camera geometry amplifier matches an on-disk bounding box.
78 Bounding box differences that are consistent with differences in overscan
79 regions are assumed to be overscan regions, which gives us enough
80 information to correct the camera geometry.
82 Parameters
83 ----------
84 inAmp : `lsst.afw.cameraGeom.Amplifier`
85 Amplifier description from camera geometry.
86 bbox : `lsst.geom.Box2I`
87 The on-disk bounding box of the amplifer image.
88 metadata : `lsst.daf.base.PropertyList`
89 FITS header metadata from the amplifier HDU.
90 logCmd : `function`, optional
91 Call back to use to issue log messages about patching. Arguments to
92 this function should match arguments to be accepted by normal logging
93 functions. Warnings about bad EXTNAMES are always sent directly to
94 the module-level logger.
96 Return
97 ------
98 outAmp : `~lsst.afw.cameraGeom.Amplifier.Builder`
99 modified : `bool`
100 `True` if ``amp`` was modified; `False` otherwise.
102 Raises
103 ------
104 RuntimeError
105 Raised if the bounding boxes differ in a way that is not consistent
106 with just a change in overscan.
107 """
108 if logCmd is None:
109 # Define a null log command
110 def logCmd(*args):
111 return
113 # check that the book-keeping worked and we got the correct EXTNAME
114 extname = metadata.get("EXTNAME")
115 predictedExtname = f"Segment{inAmp.getName()[1:]}"
116 if extname is not None and predictedExtname != extname:
117 logger.warning('expected to see EXTNAME == "%s", but saw "%s"', predictedExtname, extname)
119 modified = False
121 outAmp = inAmp.rebuild()
122 if outAmp.getRawBBox() != bbox: # Oh dear. cameraGeom is wrong -- probably overscan
123 if outAmp.getRawDataBBox().getDimensions() != outAmp.getBBox().getDimensions():
124 raise RuntimeError("Active area is the wrong size: %s v. %s" %
125 (outAmp.getRawDataBBox().getDimensions(), outAmp.getBBox().getDimensions()))
127 logCmd("outAmp.getRawBBox() != data.getBBox(); patching. (%s v. %s)", outAmp.getRawBBox(), bbox)
129 w, h = bbox.getDimensions()
130 ow, oh = outAmp.getRawBBox().getDimensions() # "old" (cameraGeom) dimensions
131 #
132 # We could trust the BIASSEC keyword, or we can just assume that
133 # they've changed the number of overscan pixels (serial and/or
134 # parallel). As Jim Chiang points out, the latter is safer
135 #
136 fromCamGeom = outAmp.getRawHorizontalOverscanBBox()
137 hOverscanBBox = Box2I(fromCamGeom.getBegin(),
138 Extent2I(w - fromCamGeom.getBeginX(), fromCamGeom.getHeight()))
139 fromCamGeom = outAmp.getRawVerticalOverscanBBox()
140 vOverscanBBox = Box2I(fromCamGeom.getBegin(),
141 Extent2I(fromCamGeom.getWidth(), h - fromCamGeom.getBeginY()))
142 outAmp.setRawBBox(bbox)
143 outAmp.setRawHorizontalOverscanBBox(hOverscanBBox)
144 outAmp.setRawVerticalOverscanBBox(vOverscanBBox)
145 #
146 # This gets all the geometry right for the amplifier, but the size
147 # of the untrimmed image will be wrong and we'll put the amp sections
148 # in the wrong places, i.e.
149 # outAmp.getRawXYOffset()
150 # will be wrong. So we need to recalculate the offsets.
151 #
152 xRawExtent, yRawExtent = outAmp.getRawBBox().getDimensions()
154 x0, y0 = outAmp.getRawXYOffset()
155 ix, iy = x0//ow, y0/oh
156 x0, y0 = ix*xRawExtent, iy*yRawExtent
157 outAmp.setRawXYOffset(Extent2I(ix*xRawExtent, iy*yRawExtent))
159 modified = True
161 #
162 # Check the "IRAF" keywords, but don't abort if they're wrong
163 #
164 # Only warn about the first amp, use debug for the others
165 #
166 d = metadata.toDict()
167 detsec = bboxFromIraf(d["DETSEC"]) if "DETSEC" in d else None
168 datasec = bboxFromIraf(d["DATASEC"]) if "DATASEC" in d else None
169 biassec = bboxFromIraf(d["BIASSEC"]) if "BIASSEC" in d else None
171 if detsec and outAmp.getBBox() != detsec:
172 logCmd("DETSEC doesn't match (%s != %s)", outAmp.getBBox(), detsec)
173 if datasec and outAmp.getRawDataBBox() != datasec:
174 logCmd("DATASEC doesn't match for (%s != %s)", outAmp.getRawDataBBox(), detsec)
175 if biassec and outAmp.getRawHorizontalOverscanBBox() != biassec:
176 logCmd("BIASSEC doesn't match for (%s != %s)", outAmp.getRawHorizontalOverscanBBox(), detsec)
178 return outAmp, modified
181def assembleUntrimmedCcd(ccd, exposures):
182 """Assemble an untrimmmed CCD from per-amp Exposure objects.
184 Parameters
185 ----------
186 ccd : `~lsst.afw.cameraGeom.Detector`
187 The detector geometry for this ccd that will be used as the
188 framework for the assembly of the input amplifier exposures.
189 exposures : sequence of `lsst.afw.image.Exposure`
190 Per-amplifier images, in the same order as ``amps``.
192 Returns
193 -------
194 ccd : `lsst.afw.image.Exposure`
195 Assembled CCD image.
196 """
197 ampDict = {}
198 for amp, exposure in zip(ccd, exposures):
199 ampDict[amp.getName()] = exposure
200 config = AssembleCcdTask.ConfigClass()
201 config.doTrim = False
202 assembleTask = AssembleCcdTask(config=config)
203 return assembleTask.assembleCcd(ampDict)
206@contextmanager
207def warn_once(msg):
208 """Return a context manager around a log-like object that emits a warning
209 the first time it is used and a debug message all subsequent times.
211 Parameters
212 ----------
213 msg : `str`
214 Message to prefix all log messages with.
216 Returns
217 -------
218 logger
219 A log-like object that takes a %-style format string and positional
220 substition args.
221 """
222 warned = False
224 def logCmd(s, *args):
225 nonlocal warned
226 log_msg = f"{msg}: {s}"
227 if warned:
228 logger.debug(log_msg, *args)
229 else:
230 logger.warning(log_msg, *args)
231 warned = True
233 yield logCmd
236def fixAmpsAndAssemble(ampExps, msg):
237 """Fix amp geometry and assemble into exposure.
239 Parameters
240 ----------
241 ampExps : sequence of `lsst.afw.image.Exposure`
242 Per-amplifier images.
243 msg : `str`
244 Message to add to log and exception output.
246 Returns
247 -------
248 exposure : `lsst.afw.image.Exposure`
249 Exposure with the amps combined into a single image.
251 Notes
252 -----
253 The returned exposure does not have any metadata or WCS attached.
255 """
256 if not len(ampExps):
257 raise RuntimeError(f"Unable to read raw_amps for {msg}")
259 ccd = ampExps[0].getDetector() # the same (full, CCD-level) Detector is attached to all ampExps
260 #
261 # Check that the geometry in the metadata matches cameraGeom
262 #
263 with warn_once(msg) as logCmd:
264 # Rebuild the detector and the amplifiers to use their corrected
265 # geometry.
266 tempCcd = ccd.rebuild()
267 tempCcd.clear()
268 for amp, ampExp in zip(ccd, ampExps):
269 outAmp, _ = fixAmpGeometry(amp,
270 bbox=ampExp.getBBox(),
271 metadata=ampExp.getMetadata(),
272 logCmd=logCmd)
273 tempCcd.append(outAmp)
274 ccd = tempCcd.finish()
276 # Update the data to be combined to point to the newly rebuilt detector.
277 for ampExp in ampExps:
278 ampExp.setDetector(ccd)
280 exposure = assembleUntrimmedCcd(ccd, ampExps)
281 return exposure
284def readRawAmps(fileName, detector):
285 """Given a file name read the amps and attach the detector.
287 Parameters
288 ----------
289 fileName : `str`
290 The full path to a file containing data from a single CCD.
291 detector : `lsst.afw.cameraGeom.Detector`
292 Detector to associate with the amps.
294 Returns
295 -------
296 ampExps : `list` of `lsst.afw.image.Exposure`
297 All the individual amps read from the file.
298 """
299 amps = []
300 for hdu in range(1, len(detector)+1):
301 reader = afwImage.ImageFitsReader(fileName, hdu=hdu)
302 exp = afwImage.makeExposure(afwImage.makeMaskedImage(reader.read(dtype=np.dtype(np.int32),
303 allowUnsafe=True)))
304 exp.setDetector(detector)
305 exp.setMetadata(reader.readMetadata())
306 amps.append(exp)
307 return amps