lsst.obs.base  19.0.0-36-g22095ce
utils.py
Go to the documentation of this file.
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/>.
21 
22 __all__ = ('InitialSkyWcsError', 'createInitialSkyWcs', 'bboxFromIraf')
23 
24 import re
25 import lsst.geom as geom
26 
27 from lsst.afw.cameraGeom import PIXELS, FIELD_ANGLE
28 from lsst.afw.image import RotType
29 from lsst.afw.geom.skyWcs import makeSkyWcs
31 
32 
33 class InitialSkyWcsError(Exception):
34  """For handling failures when creating a SkyWcs from a camera geometry and
35  boresight.
36 
37  Typically used as a chained exception from a lower level exception.
38  """
39  pass
40 
41 
42 def createInitialSkyWcs(visitInfo, detector, flipX=False):
43  """Create a SkyWcs from the telescope boresight and detector geometry.
44 
45  A typical usecase for this is to create the initial WCS for a newly-read
46  raw exposure.
47 
48 
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.
57 
58  Returns
59  -------
60  skyWcs : `lsst.afw.geom.SkyWcs`
61  The new composed WCS.
62 
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))
79  raise InitialSkyWcsError("Cannot compute PIXELS to FIELD_ANGLE Transform.") from e
80  return makeSkyWcs(pixelsToFieldAngle, orientation, flipX, boresight)
81 
82 
83 def bboxFromIraf(irafBBoxStr):
84  """Return a Box2I corresponding to an IRAF-style BBOX
85 
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  """
89 
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()]
94 
95  return geom.BoxI(geom.PointI(x0 - 1, y0 - 1), geom.PointI(x1 - 1, y1 - 1))
def createInitialSkyWcs(visitInfo, detector, flipX=False)
Definition: utils.py:42
def bboxFromIraf(irafBBoxStr)
Definition: utils.py:83