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

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

__all__ = ['PupilFactory', 'Pupil'] 

 

import numpy as np 

 

 

class Pupil: 

"""Pupil obscuration function. 

 

Parameters 

---------- 

illuminated : `numpy.ndarray`, (Nx, Ny) 

2D numpy array indicating which parts of the pupil plane are 

illuminated. 

size : `float` 

Size of pupil plane array in meters. Note that this may be larger 

than the actual diameter of the illuminated pupil to accommodate 

zero-padding. 

scale : `float` 

Sampling interval of pupil plane array in meters. 

""" 

 

def __init__(self, illuminated, size, scale): 

self.illuminated = illuminated 

self.size = size 

self.scale = scale 

 

 

class PupilFactory: 

"""Pupil obscuration function factory for use with Fourier optics. 

 

Parameters 

---------- 

visitInfo : `lsst.afw.image.VisitInfo` 

Visit information for a particular exposure. 

pupilSize : `float` 

Size in meters of constructed Pupil array. 

Note that this may be larger than the actual diameter of the 

illuminated pupil to accommodate zero-padding. 

npix : `int` 

Constructed Pupils will be npix x npix. 

""" 

 

def __init__(self, visitInfo, pupilSize, npix): 

self.visitInfo = visitInfo 

self.pupilSize = pupilSize 

self.npix = npix 

self.pupilScale = pupilSize/npix 

u = (np.arange(npix, dtype=np.float64) - (npix - 1)/2) * self.pupilScale 

self.u, self.v = np.meshgrid(u, u) 

 

def getPupil(self, point): 

"""Calculate a Pupil at a given point in the focal plane. 

 

Parameters 

---------- 

point : `lsst.geom.Point2D` 

The focal plane coordinates. 

 

Returns 

------- 

pupil : `Pupil` 

The Pupil at ``point``. 

""" 

raise NotImplementedError( 

"PupilFactory not implemented for this camera") 

 

@staticmethod 

def _pointLineDistance(p0, p1, p2): 

"""Compute the right-angle distance between the points given by `p0` 

and the line that passes through `p1` and `p2`. 

 

Parameters 

---------- 

p0 : `tuple` of `numpy.ndarray` 

2-tuple of numpy arrays (x, y focal plane coordinates) 

p1 : ``pair`` of `float` 

x,y focal plane coordinates 

p2 : ``pair`` of `float` 

x,y focal plane coordinates 

 

Returns 

------- 

distances : `numpy.ndarray` 

Numpy array of distances; shape congruent to p0[0]. 

""" 

x0, y0 = p0 

x1, y1 = p1 

x2, y2 = p2 

dy21 = y2 - y1 

dx21 = x2 - x1 

return np.abs(dy21*x0 - dx21*y0 + x2*y1 - y2*x1)/np.hypot(dy21, dx21) 

 

def _fullPupil(self): 

"""Make a fully-illuminated Pupil. 

 

Returns 

------- 

pupil : `Pupil` 

The illuminated pupil. 

""" 

illuminated = np.ones(self.u.shape, dtype=np.bool) 

return Pupil(illuminated, self.pupilSize, self.pupilScale) 

 

def _cutCircleInterior(self, pupil, p0, r): 

"""Cut out the interior of a circular region from a Pupil. 

 

Parameters 

---------- 

pupil : `Pupil` 

Pupil to modify in place. 

p0 : `pair`` of `float` 

2-tuple indicating region center. 

r : `float` 

Circular region radius. 

""" 

r2 = (self.u - p0[0])**2 + (self.v - p0[1])**2 

pupil.illuminated[r2 < r**2] = False 

 

def _cutCircleExterior(self, pupil, p0, r): 

"""Cut out the exterior of a circular region from a Pupil. 

 

Parameters 

---------- 

pupil : `Pupil` 

Pupil to modify in place 

p0 : `pair`` of `float` 

2-tuple indicating region center. 

r : `float` 

Circular region radius. 

""" 

r2 = (self.u - p0[0])**2 + (self.v - p0[1])**2 

pupil.illuminated[r2 > r**2] = False 

 

def _cutRay(self, pupil, p0, angle, thickness): 

"""Cut out a ray from a Pupil. 

 

Parameters 

---------- 

pupil : `Pupil` 

Pupil to modify in place. 

p0 : `pair`` of `float` 

2-tuple indicating ray starting point. 

angle : `pair` of `float` 

Ray angle measured CCW from +x. 

thickness : `float` 

Thickness of cutout. 

""" 

angleRad = angle.asRadians() 

# the 1 is arbitrary, just need something to define another point on 

# the line 

p1 = (p0[0] + 1, p0[1] + np.tan(angleRad)) 

d = PupilFactory._pointLineDistance((self.u, self.v), p0, p1) 

pupil.illuminated[(d < 0.5*thickness) 

& ((self.u - p0[0])*np.cos(angleRad) 

+ (self.v - p0[1])*np.sin(angleRad) >= 0)] = False 

 

def _centerPupil(self, pupil): 

"""Center the illuminated portion of the pupil in array. 

 

Parameters 

---------- 

pupil : `Pupil` 

Pupil to modify in place 

""" 

def center(arr, axis): 

smash = np.sum(arr, axis=axis) 

w = np.where(smash)[0] 

return int(0.5*(np.min(w)+np.max(w))) 

ycenter = center(pupil.illuminated, 0) 

xcenter = center(pupil.illuminated, 1) 

ytarget = pupil.illuminated.shape[0]//2 

xtarget = pupil.illuminated.shape[1]//2 

pupil.illuminated = np.roll(np.roll(pupil.illuminated, 

xtarget-xcenter, 

axis=0), 

ytarget-ycenter, 

axis=1)