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

229

230

231

232

233

234

235

236

237

238

239

240

241

242

# 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__ = ["BackgroundList"] 

 

import os 

import warnings 

import lsst.daf.base as dafBase 

import lsst.geom 

import lsst.afw.image as afwImage 

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

from lsst.utils import suppress_deprecations 

from . import mathLib as afwMath 

 

 

class BackgroundList: 

"""A list-like class to contain a list of (`lsst.afw.math.Background`, 

`lsst.afw.math.Interpolate.Style`, `~lsst.afw.math.UndersampleStyle`) 

tuples. 

 

Parameters 

---------- 

*args : `tuple` or `~lsst.afw.math.Background` 

A sequence of arguments, each of which becomes an element of the list. 

In deference to the deprecated-but-not-yet-removed 

`~lsst.afw.math.Background.getImageF()` API, we also accept a single 

`lsst.afw.math.Background` and extract the ``interpStyle`` and 

``undersampleStyle`` from the as-used values. 

""" 

 

def __init__(self, *args): 

self._backgrounds = [] 

for a in args: 

self.append(a) 

 

def __getitem__(self, *args): 

"""Return an item 

 

Parameters 

---------- 

*args 

Any valid list index. 

""" 

# 

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

warnings.warn("Passing Background objects to BackgroundList is deprecated; " 

"use a (Background, Interpolation.Style, UndersampleStyle, " 

"ApproximateControl.Style, int, int, bool) tuple instead.", 

category=FutureWarning, stacklevel=2) 

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. 

 

Parameters 

----------- 

fileName : `str` 

FITS file to write 

flags : `int` 

Flags to control details of writing; currently unused, but present 

for consistency with `lsst.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 our list of Backgrounds from a file. 

 

Parameters 

---------- 

fileName : `str` 

FITS file to read 

hdu : `int` 

First Header/Data Unit to attempt to read from 

flags : `int` 

Flags to control details of reading; currently unused, but present 

for consistency with `lsst.afw.table.BaseCatalog.readFits`. 

 

See Also 

-------- 

getImage() 

""" 

if not isinstance(fileName, MemFileManager) and not os.path.exists(fileName): 

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

 

self = BackgroundList() 

 

f = Fits(fileName, 'r') 

nHdus = f.countHdus() 

f.closeFile() 

if nHdus % 3 != 0: 

raise RuntimeError(f"BackgroundList FITS file {fileName} has {nHdus} HDUs;" 

f"expected a multiple of 3 (compression is not supported).") 

 

for hdu in range(0, nHdus, 3): 

# It seems like we ought to be able to just use 

# MaskedImageFitsReader here, but it warns about EXTTYPE and still 

# doesn't work quite naturally when starting from a nonzero HDU. 

imageReader = afwImage.ImageFitsReader(fileName, hdu=hdu) 

maskReader = afwImage.MaskFitsReader(fileName, hdu=hdu + 1) 

varianceReader = afwImage.ImageFitsReader(fileName, hdu=hdu + 2) 

statsImage = afwImage.MaskedImageF(imageReader.read(), maskReader.read(), varianceReader.read()) 

md = imageReader.readMetadata() 

 

x0 = md["BKGD_X0"] 

y0 = md["BKGD_Y0"] 

width = md["BKGD_WIDTH"] 

height = md["BKGD_HEIGHT"] 

imageBBox = lsst.geom.BoxI(lsst.geom.PointI(x0, y0), lsst.geom.ExtentI(width, height)) 

 

interpStyle = afwMath.Interpolate.Style(md["INTERPSTYLE"]) 

undersampleStyle = afwMath.UndersampleStyle(md["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.get("APPROXSTYLE", afwMath.ApproximateControl.UNKNOWN) 

approxStyle = afwMath.ApproximateControl.Style(approxStyle) 

approxOrderX = md.get("APPROXORDERX", 1) 

approxOrderY = md.get("APPROXORDERY", -1) 

approxWeighting = md.get("APPROXWEIGHTING", True) 

 

bkgd = afwMath.BackgroundMI(imageBBox, statsImage) 

bctrl = bkgd.getBackgroundControl() 

 

# TODO: DM-22814: remove this after v20. 

# Still needed until then because other code might call the old-style getImageF. 

with suppress_deprecations(): 

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: 

bkgdImage = bkgd.getImageF(interpStyle, undersampleStyle) 

else: 

bkgdImage += bkgd.getImageF(interpStyle, undersampleStyle) 

 

return bkgdImage 

 

def __reduce__(self): 

return reduceToFits(self)