Hide keyboard shortcuts

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/>. 

21 

22__all__ = ('InitialSkyWcsError', 'createInitialSkyWcs', 'bboxFromIraf') 

23 

24import re 

25import lsst.geom as geom 

26 

27from . import Instrument 

28from lsst.afw.cameraGeom import PIXELS, FIELD_ANGLE 

29from lsst.afw.image import RotType 

30from lsst.afw.geom.skyWcs import makeSkyWcs 

31import lsst.pex.exceptions 

32from lsst.utils import doImport 

33 

34 

35class InitialSkyWcsError(Exception): 

36 """For handling failures when creating a SkyWcs from a camera geometry and 

37 boresight. 

38 

39 Typically used as a chained exception from a lower level exception. 

40 """ 

41 pass 

42 

43 

44def createInitialSkyWcs(visitInfo, detector, flipX=False): 

45 """Create a SkyWcs from the telescope boresight and detector geometry. 

46 

47 A typical usecase for this is to create the initial WCS for a newly-read 

48 raw exposure. 

49 

50 

51 Parameters 

52 ---------- 

53 visitInfo : `lsst.afw.image.VisitInfo` 

54 Where to get the telescope boresight and rotator angle from. 

55 detector : `lsst.afw.cameraGeom.Detector` 

56 Where to get the camera geomtry from. 

57 flipX : `bool`, optional 

58 If False, +X is along W, if True +X is along E. 

59 

60 Returns 

61 ------- 

62 skyWcs : `lsst.afw.geom.SkyWcs` 

63 The new composed WCS. 

64 

65 Raises 

66 ------ 

67 InitialSkyWcsError 

68 Raised if there is an error generating the SkyWcs, chained from the 

69 lower-level exception if available. 

70 """ 

71 if visitInfo.getRotType() != RotType.SKY: 

72 msg = (f"Cannot create SkyWcs from camera geometry: rotator angle defined using " 

73 f"RotType={visitInfo.getRotType()} instead of SKY.") 

74 raise InitialSkyWcsError(msg) 

75 orientation = visitInfo.getBoresightRotAngle() 

76 boresight = visitInfo.getBoresightRaDec() 

77 try: 

78 pixelsToFieldAngle = detector.getTransform(detector.makeCameraSys(PIXELS), 

79 detector.makeCameraSys(FIELD_ANGLE)) 

80 except lsst.pex.exceptions.InvalidParameterError as e: 

81 raise InitialSkyWcsError("Cannot compute PIXELS to FIELD_ANGLE Transform.") from e 

82 return makeSkyWcs(pixelsToFieldAngle, orientation, flipX, boresight) 

83 

84 

85def bboxFromIraf(irafBBoxStr): 

86 """Return a Box2I corresponding to an IRAF-style BBOX 

87 

88 [x0:x1,y0:y1] where x0 and x1 are the one-indexed start and end columns, and correspondingly 

89 y0 and y1 are the start and end rows. 

90 """ 

91 

92 mat = re.search(r"^\[([-\d]+):([-\d]+),([-\d]+):([-\d]+)\]$", irafBBoxStr) 

93 if not mat: 

94 raise RuntimeError("Unable to parse IRAF-style bbox \"%s\"" % irafBBoxStr) 

95 x0, x1, y0, y1 = [int(_) for _ in mat.groups()] 

96 

97 return geom.BoxI(geom.PointI(x0 - 1, y0 - 1), geom.PointI(x1 - 1, y1 - 1)) 

98 

99 

100def getInstrument(instrumentName, registry=None): 

101 """Return an instance of a named instrument. 

102 

103 If the instrument name not is qualified (does not contain a '.') and a 

104 butler registry is provided, this will attempt to load the instrument using 

105 Instrument.fromName. Otherwise the instrument will be imported and 

106 instantiated. 

107 

108 Parameters 

109 ---------- 

110 instrumentName : string 

111 The name or fully-qualified class name of an instrument. 

112 registry : `lsst.daf.butler.Registry`, optional 

113 Butler registry to query to find information about the instrument, by 

114 default None 

115 

116 Returns 

117 ------- 

118 Instrument subclass instance 

119 The instantiated instrument. 

120 

121 Raises 

122 ------ 

123 RuntimeError 

124 If the instrument can not be imported, instantiated, or obtained from 

125 the registry. 

126 TypeError 

127 If the instrument is not a subclass of lsst.obs.base.Instrument. 

128 """ 

129 if "." not in instrumentName and registry is not None: 

130 try: 

131 instr = Instrument.fromName(instrumentName, registry) 

132 except Exception as err: 

133 raise RuntimeError( 

134 f"Could not get instrument from name: {instrumentName}. Failed with exception: {err}") 

135 else: 

136 try: 

137 instr = doImport(instrumentName) 

138 except Exception as err: 

139 raise RuntimeError(f"Could not import instrument: {instrumentName}. Failed with exception: {err}") 

140 instr = instr() 

141 if not isinstance(instr, Instrument): 

142 raise TypeError(f"{instrumentName} is not an Instrument subclass.") 

143 return instr