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

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

# 

# 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 os 

import lsst.daf.base as dafBase 

import lsst.geom 

import lsst.afw.image as afwImage 

from lsst.afw.fits import FitsError, MemFileManager, reduceToFits, Fits, DEFAULT_HDU 

from . import mathLib as afwMath 

 

 

class BackgroundList: 

"""A list-like class to contain a list of (afwMath.Background, interpStyle, undersampleStyle) tuples 

 

In deference to the deprecated-but-not-yet-removed Background.getImage() API, we also accept a single 

afwMath.Background and extract the interpStyle and undersampleStyle from the as-used values 

""" 

 

def __init__(self, *args): 

self._backgrounds = [] 

39 ↛ 40line 39 didn't jump to line 40, because the loop on line 39 never started for a in args: 

self.append(a) 

 

def __getitem__(self, *args): 

"""Return an item""" 

# 

# Set any previously-unknown Styles (they are set by bkgd.getImage()) 

# 

for i, val in enumerate(self._backgrounds): 

bkgd, interpStyle, undersampleStyle, approxStyle, \ 

approxOrderX, approxOrderY, approxWeighting = val 

if interpStyle is None or undersampleStyle is None: 

interpStyle = bkgd.getAsUsedInterpStyle() 

undersampleStyle = bkgd.getAsUsedUndersampleStyle() 

actrl = bkgd.getBackgroundControl().getApproximateControl() 

approxStyle = actrl.getStyle() 

approxOrderX = actrl.getOrderX() 

approxOrderY = actrl.getOrderY() 

approxWeighting = actrl.getWeighting() 

self._backgrounds[i] = (bkgd, interpStyle, undersampleStyle, 

approxStyle, approxOrderX, approxOrderY, approxWeighting) 

# 

# And return what they wanted 

# 

return self._backgrounds.__getitem__(*args) 

 

def __len__(self, *args): 

return self._backgrounds.__len__(*args) 

 

def append(self, val): 

try: 

bkgd, interpStyle, undersampleStyle, approxStyle, \ 

approxOrderX, approxOrderY, approxWeighting = val 

except TypeError: 

bkgd = val 

interpStyle = None 

undersampleStyle = None 

approxStyle = None 

approxOrderX = None 

approxOrderY = None 

approxWeighting = None 

 

bgInfo = (bkgd, interpStyle, undersampleStyle, approxStyle, 

approxOrderX, approxOrderY, approxWeighting) 

self._backgrounds.append(bgInfo) 

 

def clone(self): 

"""Return a shallow copy 

 

Shallow copies do not share backgrounds that are appended after copying, 

but do share changes to contained background objects. 

""" 

return BackgroundList(*self) 

 

def writeFits(self, fileName, flags=0): 

"""Save our list of Backgrounds to a file 

@param fileName FITS file to write 

@param flags Flags to control details of writing; currently unused, 

but present for consistency with 

afw.table.BaseCatalog.writeFits. 

""" 

 

for i, bkgd in enumerate(self): 

(bkgd, interpStyle, undersampleStyle, approxStyle, approxOrderX, approxOrderY, 

approxWeighting) = bkgd 

 

statsImage = bkgd.getStatsImage() 

 

md = dafBase.PropertyList() 

md.set("INTERPSTYLE", int(interpStyle)) 

md.set("UNDERSAMPLESTYLE", int(undersampleStyle)) 

md.set("APPROXSTYLE", int(approxStyle)) 

md.set("APPROXORDERX", approxOrderX) 

md.set("APPROXORDERY", approxOrderY) 

md.set("APPROXWEIGHTING", approxWeighting) 

bbox = bkgd.getImageBBox() 

md.set("BKGD_X0", bbox.getMinX()) 

md.set("BKGD_Y0", bbox.getMinY()) 

md.set("BKGD_WIDTH", bbox.getWidth()) 

md.set("BKGD_HEIGHT", bbox.getHeight()) 

 

statsImage.getImage().writeFits(fileName, md, "w" if i == 0 else "a") 

statsImage.getMask().writeFits(fileName, md, "a") 

statsImage.getVariance().writeFits(fileName, md, "a") 

 

@staticmethod 

def readFits(fileName, hdu=0, flags=0): 

"""Read a our list of Backgrounds from a file 

@param fileName FITS file to read 

@param hdu First Header/Data Unit to attempt to read from 

@param flags Flags to control details of reading; currently unused, 

but present for consistency with 

afw.table.BaseCatalog.readFits. 

 

See also getImage() 

""" 

135 ↛ 136line 135 didn't jump to line 136, because the condition on line 135 was never true if not isinstance(fileName, MemFileManager) and not os.path.exists(fileName): 

raise RuntimeError("File not found: %s" % fileName) 

 

self = BackgroundList() 

 

140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true if hdu == DEFAULT_HDU: 

hdu = -1 

else: 

# we want to start at 0 (post RFC-304), but are about to increment 

hdu -= 1 

 

fits = Fits(fileName, "r") 

fits.setHdu(hdu + 1) 

148 ↛ 149line 148 didn't jump to line 149, because the condition on line 148 was never true if fits.checkCompressedImagePhu(): 

hdu += 1 

 

while True: 

hdu += 1 

 

md = dafBase.PropertyList() 

try: 

img = afwImage.ImageF(fileName, hdu, md) 

hdu += 1 

except FitsError: 

break 

 

msk = afwImage.Mask(fileName, hdu) 

hdu += 1 

var = afwImage.ImageF(fileName, hdu) 

 

statsImage = afwImage.makeMaskedImage(img, msk, var) 

 

x0 = md.getScalar("BKGD_X0") 

y0 = md.getScalar("BKGD_Y0") 

width = md.getScalar("BKGD_WIDTH") 

height = md.getScalar("BKGD_HEIGHT") 

imageBBox = lsst.geom.BoxI(lsst.geom.PointI( 

x0, y0), lsst.geom.ExtentI(width, height)) 

 

interpStyle = afwMath.Interpolate.Style(md.getScalar("INTERPSTYLE")) 

undersampleStyle = afwMath.UndersampleStyle( 

md.getScalar("UNDERSAMPLESTYLE")) 

 

# Older outputs won't have APPROX* settings. Provide alternative defaults. 

# Note: Currently X- and Y-orders must be equal due to a limitation in 

# math::Chebyshev1Function2. Setting approxOrderY = -1 is equivalent 

# to saying approxOrderY = approxOrderX. 

approxStyle = md.getScalar("APPROXSTYLE") if "APPROXSTYLE" in md.names() \ 

else afwMath.ApproximateControl.UNKNOWN 

approxStyle = afwMath.ApproximateControl.Style(approxStyle) 

approxOrderX = md.getScalar( 

"APPROXORDERX") if "APPROXORDERX" in md.names() else 1 

approxOrderY = md.getScalar( 

"APPROXORDERY") if "APPROXORDERY" in md.names() else -1 

approxWeighting = md.getScalar( 

"APPROXWEIGHTING") if "APPROXWEIGHTING" in md.names() else True 

 

bkgd = afwMath.BackgroundMI(imageBBox, statsImage) 

bctrl = bkgd.getBackgroundControl() 

bctrl.setInterpStyle(interpStyle) 

bctrl.setUndersampleStyle(undersampleStyle) 

actrl = afwMath.ApproximateControl( 

approxStyle, approxOrderX, approxOrderY, approxWeighting) 

bctrl.setApproximateControl(actrl) 

bgInfo = (bkgd, interpStyle, undersampleStyle, approxStyle, 

approxOrderX, approxOrderY, approxWeighting) 

self.append(bgInfo) 

 

return self 

 

def getImage(self): 

""" 

Compute and return a full-resolution image from our list of 

(Background, interpStyle, undersampleStyle) 

""" 

 

bkgdImage = None 

for (bkgd, interpStyle, undersampleStyle, approxStyle, 

approxOrderX, approxOrderY, approxWeighting) in self: 

if not bkgdImage: 

if approxStyle != afwMath.ApproximateControl.UNKNOWN: 

bkgdImage = bkgd.getImageF() 

else: 

bkgdImage = bkgd.getImageF(interpStyle, undersampleStyle) 

else: 

if approxStyle != afwMath.ApproximateControl.UNKNOWN: 

bkgdImage += bkgd.getImageF() 

else: 

bkgdImage += bkgd.getImageF(interpStyle, undersampleStyle) 

 

return bkgdImage 

 

def __reduce__(self): 

return reduceToFits(self)