lsst.obs.base  19.0.0-7-g8a434f2
utils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2017 AURA/LSST
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 __all__ = ('InitialSkyWcsError', 'createInitialSkyWcs', 'bboxFromIraf')
24 
25 import re
26 import lsst.geom as geom
27 
28 from lsst.afw.cameraGeom import PIXELS, FIELD_ANGLE
29 from lsst.afw.image import RotType
30 from lsst.afw.geom.skyWcs import makeSkyWcs
32 
33 
34 class InitialSkyWcsError(Exception):
35  """For handling failures when creating a SkyWcs from a camera geometry and
36  boresight.
37 
38  Typically used as a chained exception from a lower level exception.
39  """
40  pass
41 
42 
43 def createInitialSkyWcs(visitInfo, detector, flipX=False):
44  """Create a SkyWcs from the telescope boresight and detector geometry.
45 
46  A typical usecase for this is to create the initial WCS for a newly-read
47  raw exposure.
48 
49 
50  Parameters
51  ----------
52  visitInfo : `lsst.afw.image.VisitInfo`
53  Where to get the telescope boresight and rotator angle from.
54  detector : `lsst.afw.cameraGeom.Detector`
55  Where to get the camera geomtry from.
56  flipX : `bool`, optional
57  If False, +X is along W, if True +X is along E.
58 
59  Returns
60  -------
61  skyWcs : `lsst.afw.geom.SkyWcs`
62  The new composed WCS.
63 
64  Raises
65  ------
66  InitialSkyWcsError
67  Raised if there is an error generating the SkyWcs, chained from the
68  lower-level exception if available.
69  """
70  if visitInfo.getRotType() != RotType.SKY:
71  msg = (f"Cannot create SkyWcs from camera geometry: rotator angle defined using "
72  f"RotType={visitInfo.getRotType()} instead of SKY.")
73  raise InitialSkyWcsError(msg)
74  orientation = visitInfo.getBoresightRotAngle()
75  boresight = visitInfo.getBoresightRaDec()
76  try:
77  pixelsToFieldAngle = detector.getTransform(detector.makeCameraSys(PIXELS),
78  detector.makeCameraSys(FIELD_ANGLE))
80  raise InitialSkyWcsError("Cannot compute PIXELS to FIELD_ANGLE Transform.") from e
81  return makeSkyWcs(pixelsToFieldAngle, orientation, flipX, boresight)
82 
83 
84 def bboxFromIraf(irafBBoxStr):
85  """Return a Box2I corresponding to an IRAF-style BBOX
86 
87  [x0:x1,y0:y1] where x0 and x1 are the one-indexed start and end columns, and correspondingly
88  y0 and y1 are the start and end rows.
89  """
90 
91  mat = re.search(r"^\[([-\d]+):([-\d]+),([-\d]+):([-\d]+)\]$", irafBBoxStr)
92  if not mat:
93  raise RuntimeError("Unable to parse IRAF-style bbox \"%s\"" % irafBBoxStr)
94  x0, x1, y0, y1 = [int(_) for _ in mat.groups()]
95 
96  return geom.BoxI(geom.PointI(x0 - 1, y0 - 1), geom.PointI(x1 - 1, y1 - 1))
def createInitialSkyWcs(visitInfo, detector, flipX=False)
Definition: utils.py:43
def bboxFromIraf(irafBBoxStr)
Definition: utils.py:84