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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

# 

# LSST Data Management System 

# Copyright 2017 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ["getSipMatrixFromMetadata", "makeDistortedTanWcs", "computePixelToDistortedPixel"] 

 

import lsst.geom 

from ..transformFactory import linearizeTransform, makeTransform 

from ..skyWcs import makeModifiedWcs 

from .wcsUtils import _getSipMatrixFromMetadata 

 

 

def getSipMatrixFromMetadata(metadata, name): 

"""Extract a SIP matrix from FITS TAN-SIP WCS metadata 

 

Omitted coefficients are set to 0 and all coefficients may be omitted. 

 

Parameters 

---------- 

metadata : lsst.daf.base.PropertySet 

FITS metadata 

name : str 

Name of TAN-SIP matrix; one of "A", "B", "Ap", "Bp" 

 

Returns 

------- 

numpy.array 

The SIP matrix 

 

Raises 

------ 

TypeError 

If the order keyword <name>_ORDER (e.g. AP_ORDER) is not found, 

the value of the order keyword cannot be read as an integer, 

the value of the order keyword is negative, 

or if a matrix parameter (e.g. AP_5_0) cannot be read as a float. 

""" 

arr = _getSipMatrixFromMetadata(metadata, name) 

if arr.shape == (): # order=0 

arr.shape = (1, 1) 

return arr 

 

 

def makeDistortedTanWcs(tanWcs, pixelToFocalPlane, focalPlaneToFieldAngle): 

""" 

Compute a WCS that includes a model of optical distortion. 

 

This is useful in the common case that the initial WCS entirely ignores 

the effect of optical distortion. 

 

Parameters 

---------- 

tanWcs : lsst.afw.geom.SkyWcs 

A pure TAN WCS, such as is usually provided in raw data. 

This should have no existing compensation for optical distortion 

(though it may include an ACTUAL_PIXELS frame to model pixel-level 

distortions). 

pixelToFocalPlane : lsst.afw.geom.TransformPoint2ToPoint2 

Transform parent pixel coordinates to focal plane coordinates. 

This models the location of the CCD on the focal plane 

and is almost always an affine transformation. 

This can be obtained from the detector of an exposure. 

focalPlaneToFieldAngle : lsst.afw.geom.TransformPoint2ToPoint2 

Transform focal plane coordinates to field angle coordinates. 

This is a model for optical distortion, and is often a radial 

polynomial. This can be obtained from the camera geometry. 

 

 

Returns 

------- 

lsst.afw.geom.SkyWcs 

A copy of `tanWcs` that includes the effect of optical distortion. 

 

Raises 

------ 

RuntimeError 

If the current frame of `wcs` is not a SkyFrame; 

LookupError 

If 2-dimensional Frames with Domain "PIXELS" and "IWC" 

are not all found. 

""" 

# The math is as follows: 

# 

# Our input TAN WCS is: 

# tanWcs = PIXELS frame -> pixelToIwc -> IWC frame -> iwcToSky -> SkyFrame 

# See lsst.afw.geom.SkyWcs for a description of these frames. 

# tanWcs may also contain an ACTUAL_PIXELS frame before the PIXELS frame; 

# if so it will be preserved, but it is irrelevant to the computation 

# and so not discussed further. 

# 

# Our desired WCS must still contain the PIXELS and IWC frames. 

# The distortion will be inserted just after the PIXELS frame, 

# So the new WCS will be as follows: 

# wcs = PIXELS frame -> pixelToDistortedPixel -> pixelToIwc -> IWC frame -> iwcToSky -> SkyFrame 

# 

# We compute pixelToDistortedPixel as follows... 

# 

# We will omit the frames from now on, for simplicity. Thus: 

# tanWcs = pixelToIwc -> iwcToSksy 

# and: 

# wcs = pixelToDistortedPixel -> pixelToIwc -> iwcToSky 

# 

# We also know pixelToFocalPlane and focalPlaneToFieldAngle, 

# and can use them as follows: 

# 

# The tan WCS can be expressed as: 

# tanWcs = pixelToFocalPlane -> focalPlaneToTanFieldAngle -> fieldAngleToIwc -> iwcToSky 

# where: 

# - focalPlaneToTanFieldAngle is the linear approximation to 

# focalPlaneToFieldAngle at the center of the focal plane 

# 

# The desired WCS can be expressed as: 

# wcs = pixelToFocalPlane -> focalPlaneToFieldAngle -> fieldAngleToIwc -> iwcToSky 

# 

# By equating the two expressions for tanWcs, we get: 

# pixelToIwc = pixelToFocalPlane -> focalPlaneToTanFieldAngle -> fieldAngleToIwc 

# fieldAngleToIwc = tanFieldAngleToFocalPlane -> focalPlaneToPixel -> pixelToIwc 

# 

# By equating the two expressions for desired wcs we get: 

# pixelToDistortedPixel -> pixelToIwc = pixelToFocalPlane -> focalPlaneToFieldAngle -> fieldAngleToIwc 

# 

# Substitute our expression for fieldAngleToIwc from tanWcs into the 

# previous equation, we get: 

# pixelToDistortedPixel -> pixelToIwc 

# = pixelToFocalPlane -> focalPlaneToFieldAngle -> tanFieldAngleToFocalPlane -> focalPlaneToPixel 

# -> pixelToIwc 

# 

# Thus: 

# pixelToDistortedPixel 

# = pixelToFocalPlane -> focalPlaneToFieldAngle -> tanFieldAngleToFocalPlane -> focalPlaneToPixel 

 

pixelToDistortedPixel = computePixelToDistortedPixel(pixelToFocalPlane, focalPlaneToFieldAngle) 

return makeModifiedWcs(pixelTransform=pixelToDistortedPixel, wcs=tanWcs, modifyActualPixels=False) 

 

 

def computePixelToDistortedPixel(pixelToFocalPlane, focalPlaneToFieldAngle): 

""" 

Compute the transform pixelToDistortedPixel, which applies optical 

distortion specified by focalPlaneToFieldAngle 

 

The resulting transform is designed to be used to convert a pure TAN WCS 

to a WCS that includes a model for optical distortion. In detail, 

the initial WCS will contain these frames and transforms: 

PIXELS frame -> pixelToIwc -> IWC frame -> gridToIwc -> SkyFrame 

To produce the WCS with distortion, replace pixelToIwc with: 

pixelToDistortedPixel -> pixelToIwc 

 

Parameters 

---------- 

pixelToFocalPlane : lsst.afw.geom.TransformPoint2ToPoint2 

Transform parent pixel coordinates to focal plane coordinates 

focalPlaneToFieldAngle : lsst.afw.geom.TransformPoint2ToPoint2 

Transform focal plane coordinates to field angle coordinates 

 

Returns 

------- 

pixelToDistortedPixel : lsst.afw.geom.TransformPoint2ToPoint2 

A transform that applies the effect of the optical distortion model. 

""" 

# return pixelToFocalPlane -> focalPlaneToFieldAngle -> tanFieldAngleToocalPlane -> focalPlaneToPixel 

focalPlaneToTanFieldAngle = makeTransform(linearizeTransform(focalPlaneToFieldAngle, 

lsst.geom.Point2D(0, 0))) 

return pixelToFocalPlane.then(focalPlaneToFieldAngle) \ 

.then(focalPlaneToTanFieldAngle.getInverse()) \ 

.then(pixelToFocalPlane.getInverse())