Coverage for python/lsst/jointcal/cameraGeometry.py: 17%

Shortcuts 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

82 statements  

1# This file is part of jointcal. 

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"""Code to convert jointcal's output WCS models to distortion maps that can be 

23used by afw CameraGeom. 

24""" 

25import numpy as np 

26 

27from lsst.afw import cameraGeom 

28import lsst.afw.geom 

29import astshim as ast 

30import lsst.log 

31from lsst.geom import SpherePoint, Point2D, radians 

32 

33_LOG = lsst.log.Log.getLogger(__name__) 

34 

35 

36class CameraModel: 

37 """Convert a jointcal `~lsst.afw.geom.SkyWcs` into a distortion model and 

38 detector positions (TODO) that can be used by `~lsst.afw.cameraGeom`. 

39 

40 Because this code only operates on the WCS, it is independent of the 

41 format of the persisted output (e.g. gen2 separate files vs. gen3 bundled 

42 visits). 

43 

44 Parameters 

45 ---------- 

46 wcsList : `list` [`lsst.afw.geom.SkyWcs`] 

47 The WCS to use to compute the distortion model from, preferably from 

48 multiple visits on the same tract. 

49 detectors : `list` [`int`] 

50 Detector ids that correspond one-to-one with ``wcsList``. 

51 camera : `lsst.afw.cameraGeom.Camera` 

52 The camera these WCS were fit for. 

53 n : `int` 

54 Number of points to compute the pixel scale at, along the +y axis. 

55 """ 

56 def __init__(self, wcsList, detectors, camera, n=100): 

57 self.wcsList = wcsList 

58 self.camera = camera 

59 self.detectors = detectors 

60 self.maxFocalRadius = self.camera.computeMaxFocalPlaneRadius() 

61 self.n = n 

62 # the computed radius and pixel scales 

63 self.fieldAngle = None # degrees 

64 self.radialScale = None # arcsec 

65 self.tangentialScale = None # arcsec 

66 # the computed values for every input wcs 

67 self.fieldAngles = None 

68 self.radialScales = None 

69 self.tangentialScales = None 

70 self.fieldAngleStd = None 

71 self.radialScaleStd = None 

72 self.tangentialScaleStd = None 

73 

74 self.log = _LOG.getChild("CameraModel") 

75 

76 def computeDistortionModel(self): 

77 """Calculate the afw cameraGeom distortion model to be included in an 

78 on-disk camera model. 

79 

80 PLACEHOLDER: This may be as simple as running `computePixelScale` and 

81 then doing a numpy polynomial fit to it for the cameraGeom input. 

82 However, we need to check details of how that distortion model is 

83 stored in a Camera. e.g.: 

84 np.polyfit(self.fieldAngle, self.radialScale, poly_degree) 

85 """ 

86 raise NotImplementedError("not yet!") 

87 

88 def computePixelScale(self): 

89 """Compute the radial and tangential pixel scale by averaging over 

90 multiple jointcal WCS models. 

91 

92 Also computes the standard deviation and logs any WCS that are 

93 significant outliers. 

94 The calculations are stored in the ``fieldAngle[s]``, 

95 ``radialScale[s]``, and ``tangentialScale[s]`` member variables. 

96 """ 

97 self.fieldAngles = [] 

98 self.radialScales = [] 

99 self.tangentialScales = [] 

100 for id, wcs in zip(self.detectors, self.wcsList): 

101 fieldAngle, radial, tangential = self._computeDetectorPixelScale(id, wcs) 

102 self.fieldAngles.append(fieldAngle) 

103 self.radialScales.append(radial) 

104 self.tangentialScales.append(tangential) 

105 # TODO: For now, don't worry about small differences in the computed 

106 # field angles vs. their respective radial/tangential scales, just 

107 # assume all fieldAngle positions are "close enough" and warn if not. 

108 self.fieldAngle = np.mean(self.fieldAngles, axis=0) 

109 self.fieldAngleStd = np.std(self.fieldAngles, axis=0) 

110 if self.fieldAngleStd.max() > 1e-4: 

111 self.log.warning("Large stddev in computed field angles between visits (max: %s degree).", 

112 self.fieldAngleStd.max()) 

113 # import os; print(os.getpid()); import ipdb; ipdb.set_trace(); 

114 self.radialScale = np.mean(self.radialScales, axis=0) 

115 self.radialScaleStd = np.std(self.radialScales, axis=0) 

116 if self.radialScaleStd.max() > 1e-4: 

117 self.log.warning("Large stddev in computed radial scales between visits (max: %s arcsec).", 

118 self.radialScaleStd.max()) 

119 self.tangentialScale = np.mean(self.tangentialScales, axis=0) 

120 self.tangentialScaleStd = np.std(self.tangentialScales, axis=0) 

121 if self.tangentialScaleStd.max() > 1e-4: 

122 self.log.warning("Large stddev in computed tangential scales between visits (max: %s arcsec).", 

123 self.tangentialScaleStd.max()) 

124 

125 def computeCameraPixelScale(self, detector_id=30): 

126 """Compute the radial and tangential pixel scales using the distortion 

127 model supplied with the camera. 

128 

129 This is designed to be directly comparable with the results of 

130 `~CameraModel.computePixelScale`. 

131 

132 Parameters 

133 ---------- 

134 detector_id: `int` 

135 Detector identifier for the detector_id to use for the calculation. 

136 

137 Returns 

138 ------- 

139 fieldAngle : `numpy.ndarray` 

140 Field angles in degrees. 

141 radialScale : `numpy.ndarray` 

142 Radial direction pixel scales in arcseconds/pixel. 

143 tangentialScale : `numpy.ndarray` 

144 Tangential direction pixel scales in arcseconds/pixel. 

145 """ 

146 # Make a trivial SkyWcs to get a field angle->sky transform from. 

147 iwcToSkyWcs = lsst.afw.geom.makeSkyWcs(Point2D(0, 0), SpherePoint(0, 0, radians), 

148 lsst.afw.geom.makeCdMatrix(1.0 * radians, 0 * radians, True)) 

149 iwcToSkyMap = iwcToSkyWcs.getFrameDict().getMapping("PIXELS", "SKY") 

150 skyFrame = iwcToSkyWcs.getFrameDict().getFrame("SKY") 

151 

152 # Extract the transforms that are defined just on the camera. 

153 pixSys = self.camera[detector_id].makeCameraSys(cameraGeom.PIXELS) 

154 pixelsToFocal = self.camera.getTransform(pixSys, cameraGeom.FOCAL_PLANE) 

155 focalToField = self.camera.getTransform(cameraGeom.FOCAL_PLANE, cameraGeom.FIELD_ANGLE) 

156 

157 # Build a SkyWcs that combines each of the above components. 

158 pixelFrame = ast.Frame(2, "Domain=PIXELS") 

159 focalFrame = ast.Frame(2, "Domain=FOCAL") 

160 iwcFrame = ast.Frame(2, "Domain=IWC") 

161 frameDict = ast.FrameDict(pixelFrame) 

162 frameDict.addFrame("PIXELS", pixelsToFocal.getMapping(), focalFrame) 

163 frameDict.addFrame("FOCAL", focalToField.getMapping(), iwcFrame) 

164 frameDict.addFrame("IWC", iwcToSkyMap, skyFrame) 

165 wcs = lsst.afw.geom.SkyWcs(frameDict) 

166 

167 return self._computeDetectorPixelScale(detector_id, wcs) 

168 

169 def _computeDetectorPixelScale(self, detector_id, wcs): 

170 """Compute pixel scale in radial and tangential directions as a 

171 function of field angle. 

172 

173 Parameters 

174 ---------- 

175 detector_id: `int` 

176 Detector identifier for the detector of this wcs. 

177 wcs : `lsst.afw.geom.SkyWcs` 

178 Full focal-plane model to compute pixel scale on. 

179 

180 Returns 

181 ------- 

182 fieldAngle : `numpy.ndarray` 

183 Field angles in degrees. 

184 radialScale : `numpy.ndarray` 

185 Radial direction pixel scales in arcseconds/pixel. 

186 tangentialScale : `numpy.ndarray` 

187 Tangential direction pixel scales in arcseconds/pixel. 

188 

189 Notes 

190 ----- 

191 Pixel scales are calculated from finite differences only along the +y 

192 focal plane direction. 

193 """ 

194 focalToSky = wcs.getFrameDict().getMapping('FOCAL', 'SKY') 

195 mmPerPixel = self.camera[detector_id].getPixelSize() 

196 

197 focalToPixels = wcs.getFrameDict().getMapping('FOCAL', 'PIXELS') 

198 trans = wcs.getTransform() # Pixels to Sky as Point2d -> SpherePoint 

199 boresight = trans.applyForward(Point2D(focalToPixels.applyForward([0, 0]))) 

200 

201 rs = np.linspace(0, self.maxFocalRadius, self.n) # focal plane units 

202 fieldAngle = np.zeros_like(rs) 

203 radialScale = np.zeros_like(rs) 

204 tangentialScale = np.zeros_like(rs) 

205 for i, r in enumerate(rs): 

206 # point on the sky at position r along the focal plane +y axis 

207 sp1 = SpherePoint(*focalToSky.applyForward(Point2D([0, r])), radians) 

208 # point on the sky one pixel further along the focal plane +y axis 

209 sp2 = SpherePoint(*focalToSky.applyForward(Point2D([0, r + mmPerPixel.getY()])), radians) 

210 # point on the sky one pixel off of the focal plane +y axis at r 

211 sp3 = SpherePoint(*focalToSky.applyForward(Point2D([mmPerPixel.getX(), r])), radians) 

212 fieldAngle[i] = boresight.separation(sp1).asDegrees() 

213 radialScale[i] = sp1.separation(sp2).asArcseconds() 

214 tangentialScale[i] = sp1.separation(sp3).asArcseconds() 

215 return fieldAngle, radialScale, tangentialScale