lsst.obs.base  18.1.0-12-gf30922b+1
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 import re
24 import lsst.geom as geom
25 
26 from lsst.afw.cameraGeom import PIXELS, FIELD_ANGLE
27 from lsst.afw.image import RotType
28 from lsst.afw.geom.skyWcs import makeSkyWcs
30 
31 
32 class InitialSkyWcsError(Exception):
33  """For handling failures when creating a SkyWcs from a camera geometry and
34  boresight.
35 
36  Typically used as a chained exception from a lower level exception.
37  """
38  pass
39 
40 
41 def createInitialSkyWcs(visitInfo, detector, flipX=False):
42  """Create a SkyWcs from the telescope boresight and detector geometry.
43 
44  A typical usecase for this is to create the initial WCS for a newly-read
45  raw exposure.
46 
47 
48  Parameters
49  ----------
50  visitInfo : `lsst.afw.image.VisitInfo`
51  Where to get the telescope boresight and rotator angle from.
52  detector : `lsst.afw.cameraGeom.Detector`
53  Where to get the camera geomtry from.
54  flipX : `bool`, optional
55  If False, +X is along W, if True +X is along E.
56 
57  Returns
58  -------
59  skyWcs : `lsst.afw.geom.SkyWcs`
60  The new composed WCS.
61 
62  Raises
63  ------
64  InitialSkyWcsError
65  Raised if there is an error generating the SkyWcs, chained from the
66  lower-level exception if available.
67  """
68  if visitInfo.getRotType() != RotType.SKY:
69  msg = (f"Cannot create SkyWcs from camera geometry: rotator angle defined using "
70  f"RotType={visitInfo.getRotType()} instead of SKY.")
71  raise InitialSkyWcsError(msg)
72  orientation = visitInfo.getBoresightRotAngle()
73  boresight = visitInfo.getBoresightRaDec()
74  try:
75  pixelsToFieldAngle = detector.getTransform(detector.makeCameraSys(PIXELS),
76  detector.makeCameraSys(FIELD_ANGLE))
78  raise InitialSkyWcsError("Cannot compute PIXELS to FIELD_ANGLE Transform.") from e
79  return makeSkyWcs(pixelsToFieldAngle, orientation, flipX, boresight)
80 
81 
82 def bboxFromIraf(irafBBoxStr):
83  """Return a Box2I corresponding to an IRAF-style BBOX
84 
85  [x0:x1,y0:y1] where x0 and x1 are the one-indexed start and end columns, and correspondingly
86  y0 and y1 are the start and end rows.
87  """
88 
89  mat = re.search(r"^\[([-\d]+):([-\d]+),([-\d]+):([-\d]+)\]$", irafBBoxStr)
90  if not mat:
91  raise RuntimeError("Unable to parse IRAF-style bbox \"%s\"" % irafBBoxStr)
92  x0, x1, y0, y1 = [int(_) for _ in mat.groups()]
93 
94  return geom.BoxI(geom.PointI(x0 - 1, y0 - 1), geom.PointI(x1 - 1, y1 - 1))
def createInitialSkyWcs(visitInfo, detector, flipX=False)
Definition: utils.py:41
def bboxFromIraf(irafBBoxStr)
Definition: utils.py:82