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

199

200

201

202

203

204

205

206

207

from __future__ import absolute_import, division, print_function 

from builtins import range 

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 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/>. 

# 

import sys 

import os 

from astropy.io import fits 

import numpy as num 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.meas.algorithms as measAlg 

DEBUG = False 

 

filtToHdu = {'u': 1, 'g': 2, 'r': 3, 'i': 4, 'z': 5} 

 

# Mapping from psField coefficient locations to PolynomialFunction2 locations 

skMatrixPos2TriSeqPosT = [ 

0, 2, 5, 9, 14, 

1, 4, 8, 13, 19, 

3, 7, 12, 18, 25, 

6, 11, 17, 24, 32, 

10, 16, 23, 31, 40, 

] 

 

 

def convertpsField(infile, filt, trim=True, rcscale=0.001, MAX_ORDER_B=5, LSST_ORDER=4): 

46 ↛ 47line 46 didn't jump to line 47, because the condition on line 46 was never true if filt not in filtToHdu: 

print("INVALID FILTER", filt) 

sys.exit(1) 

 

buff = open(infile, "rb") 

pstruct = fits.getdata(buff, ext=filtToHdu[filt]) 

 

spaParList = [[]]*len(pstruct) 

kernelList = [] 

for i in range(len(pstruct)): 

nrow_b = pstruct[i][0] # ny 

ncol_b = pstruct[i][1] # nx 

cmat = pstruct[i][2].reshape((MAX_ORDER_B, MAX_ORDER_B)) 

krow = pstruct[i][4] # RNROW 

kcol = pstruct[i][5] # RNCOL 

# This is *not* transposed 

karr = pstruct[i][7].reshape((krow, kcol)).astype(num.float64) 

 

64 ↛ 67line 64 didn't jump to line 67, because the condition on line 64 was never false if trim: 

karr = karr[10:41, 10:41] 

 

kim = afwImage.ImageD(karr) 

kern = afwMath.FixedKernel(kim) 

kernelList.append(kern) 

 

# NOTES: 

 

# Afw has the polynomial terms like: 

# 

# * f(x,y) = c0 (0th order) 

# * + c1 x + c2 y (1st order) 

# * + c3 x^2 + c4 x y + c5 y^2 (2nd order) 

# * + c6 x^3 + c7 x^2 y + c8 x y^2 + c9 y^3 (3rd order) 

# * + c10 x^4 + c11 x^3 y + c12 x^2 y^2 + c13 x y^3 + c14 y^4 (4th order) 

# 

# So, ordered: x^0,y^0 x^1,y^0 x^0,y^1 x^2,y^0 x^1,y^1 x^0,y^2 

 

# SDSS has the terms ordered like, after reshape(): 

# 

# x^0,y^0 x^0,y^1 x^0,y^2 

# x^1,y^0 x^1,y^1 x^1,y^2 

# x^2,y^0 x^2,y^1 x^2,y^2 

# 

# So, it technically goes up to fourth order in LSST-speak. OK, that is the trick. 

# 

# Mapping: 

# cmat[0][0] = c0 x^0y^0 

# cmat[1][0] = c2 x^0y^1 

# cmat[2][0] = c5 x^0y^2 

# cmat[0][1] = c1 x^1y^0 

# cmat[1][1] = c4 x^1y^1 

# cmat[2][1] = c8 x^1y^2 

# cmat[0][2] = c3 x^2y^0 

# cmat[1][2] = c7 x^2y^1 

# cmat[2][2] = c12 x^2y^2 

# 

# This is quantified in skMatrixPos2TriSeqPosT 

 

spaParamsTri = num.zeros(MAX_ORDER_B * MAX_ORDER_B) 

for k in range(nrow_b * ncol_b): 

row = k % nrow_b 

col = k // nrow_b 

coeff = cmat[row, col] 

scale = pow(rcscale, row) * pow(rcscale, col) 

scaledCoeff = coeff * scale 

 

# Was originally written like this, but the SDSS code 

# takes inputs as y,x instead of x,y meaning it was 

# originally transposed 

# 

# idx = row * MAX_ORDER_B + col 

 

idx = col * MAX_ORDER_B + row 

spaParamsTri[skMatrixPos2TriSeqPosT[idx]] = scaledCoeff 

 

# print "%d y=%d x=%d %10.3e %2d %2d %10.3e" % \ 

# (i, row, col, cmat[row,col], idx, skMatrixPos2TriSeqPosT[idx], scaledCoeff) 

 

# print spaParamsTri 

nTerms = (LSST_ORDER + 1) * (LSST_ORDER + 2) // 2 

spaParamsTri = spaParamsTri[:nTerms] 

spaParList[i] = spaParamsTri 

 

buff.close() 

spaFun = afwMath.PolynomialFunction2D(LSST_ORDER) 

spatialKernel = afwMath.LinearCombinationKernel(kernelList, spaFun) 

spatialKernel.setSpatialParameters(spaParList) 

spatialPsf = measAlg.PcaPsf(spatialKernel) 

return spatialPsf 

 

 

def directCompare(infile, filt, x, y, soft_bias=1000, amp=30000, outfile="/tmp/sdss_psf.fits"): 

if filt not in filtToHdu.keys(): 

print("INVALID FILTER", filt) 

sys.exit(1) 

 

# Make the kernel image from LSST 

psf = convertpsField(infile, filt, trim=False) 

kernel = psf.getKernel() 

 

# Assumes you have built dervish and have read_PSF in your path 

# 

# read_PSF takes coordinates in the order row,col or y,x 

cmd = "read_PSF %s %s %f %f %s" % (infile, filtToHdu[filt], y, x, outfile) 

os.system(cmd) 

if not os.path.isfile(outfile): 

print("Cannot find SDSS-derived kernel", outfile) 

sys.exit(1) 

 

if False: 

# Default version that integerizes Psf 

kImage1 = afwImage.ImageD(outfile) 

kImage1 -= soft_bias 

kImage1 /= (amp - soft_bias) 

maxVal = afwMath.makeStatistics(kImage1, afwMath.MAX).getValue(afwMath.MAX) 

print("TEST 1", maxVal == 1.0) 

kImage1.writeFits("/tmp/sdss_psf_scaled.fits") 

else: 

# Hacked version of main_PSF.c that writes floats 

kImage1 = afwImage.ImageD(outfile) 

maxVal = afwMath.makeStatistics(kImage1, afwMath.MAX).getValue(afwMath.MAX) 

kImage1 /= maxVal 

kImage1.writeFits("/tmp/sdss_psf_scaled.fits") 

 

# 

kImage2 = afwImage.ImageD(kernel.getDimensions()) 

kernel.computeImage(kImage2, True, x, y) 

maxVal = afwMath.makeStatistics(kImage2, afwMath.MAX).getValue(afwMath.MAX) 

kImage2 /= maxVal 

kImage2.writeFits("/tmp/kernel.fits") 

 

kImage3 = afwImage.ImageD(kImage2, True) 

kImage3 -= kImage1 

kImage3.writeFits("/tmp/diff.fits") 

residSum = afwMath.makeStatistics(kImage3, afwMath.SUM).getValue(afwMath.SUM) 

print("TEST 2", residSum) 

 

kImage4 = afwImage.ImageD(kImage2, True) 

kImage4 /= kImage1 

kImage4.writeFits("/tmp/rat.fits") 

 

 

188 ↛ 189line 188 didn't jump to line 189, because the condition on line 188 was never trueif __name__ == '__main__': 

infile = sys.argv[1] 

filt = sys.argv[2] 

x = float(sys.argv[3]) # col 

y = float(sys.argv[4]) # row 

outfile = sys.argv[5] 

 

if not os.path.isfile(infile): 

sys.exit(1) 

 

if DEBUG: 

directCompare(infile, filt, x, y) 

else: 

psf = convertpsField(infile, filt) 

kernel = psf.getKernel() 

kImage = afwImage.ImageD(kernel.getDimensions()) 

kernel.computeImage(kImage, True, x, y) 

kImage.writeFits(outfile) 

 

# Persist the kernel at your own leisure