Coverage for python/lsst/obs/base/utils.py : 34%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of obs_base.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22__all__ = ('InitialSkyWcsError', 'createInitialSkyWcs', 'bboxFromIraf')
24import re
25import lsst.geom as geom
27from lsst.afw.cameraGeom import PIXELS, FIELD_ANGLE
28from lsst.afw.image import RotType
29from lsst.afw.geom.skyWcs import makeSkyWcs
30import lsst.pex.exceptions
33class InitialSkyWcsError(Exception):
34 """For handling failures when creating a SkyWcs from a camera geometry and
35 boresight.
37 Typically used as a chained exception from a lower level exception.
38 """
39 pass
42def createInitialSkyWcs(visitInfo, detector, flipX=False):
43 """Create a SkyWcs from the telescope boresight and detector geometry.
45 A typical usecase for this is to create the initial WCS for a newly-read
46 raw exposure.
49 Parameters
50 ----------
51 visitInfo : `lsst.afw.image.VisitInfo`
52 Where to get the telescope boresight and rotator angle from.
53 detector : `lsst.afw.cameraGeom.Detector`
54 Where to get the camera geomtry from.
55 flipX : `bool`, optional
56 If False, +X is along W, if True +X is along E.
58 Returns
59 -------
60 skyWcs : `lsst.afw.geom.SkyWcs`
61 The new composed WCS.
63 Raises
64 ------
65 InitialSkyWcsError
66 Raised if there is an error generating the SkyWcs, chained from the
67 lower-level exception if available.
68 """
69 if visitInfo.getRotType() != RotType.SKY:
70 msg = (f"Cannot create SkyWcs from camera geometry: rotator angle defined using "
71 f"RotType={visitInfo.getRotType()} instead of SKY.")
72 raise InitialSkyWcsError(msg)
73 orientation = visitInfo.getBoresightRotAngle()
74 boresight = visitInfo.getBoresightRaDec()
75 try:
76 pixelsToFieldAngle = detector.getTransform(detector.makeCameraSys(PIXELS),
77 detector.makeCameraSys(FIELD_ANGLE))
78 except lsst.pex.exceptions.InvalidParameterError as e:
79 raise InitialSkyWcsError("Cannot compute PIXELS to FIELD_ANGLE Transform.") from e
80 return makeSkyWcs(pixelsToFieldAngle, orientation, flipX, boresight)
83def bboxFromIraf(irafBBoxStr):
84 """Return a Box2I corresponding to an IRAF-style BBOX
86 [x0:x1,y0:y1] where x0 and x1 are the one-indexed start and end columns, and correspondingly
87 y0 and y1 are the start and end rows.
88 """
90 mat = re.search(r"^\[([-\d]+):([-\d]+),([-\d]+):([-\d]+)\]$", irafBBoxStr)
91 if not mat:
92 raise RuntimeError("Unable to parse IRAF-style bbox \"%s\"" % irafBBoxStr)
93 x0, x1, y0, y1 = [int(_) for _ in mat.groups()]
95 return geom.BoxI(geom.PointI(x0 - 1, y0 - 1), geom.PointI(x1 - 1, y1 - 1))