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

# 

# LSST Data Management System 

# Copyright 2008-2017 LSST/AURA. 

# 

# 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__ = ["clipImage", "resetFilters", "defineFilter", 

"projectImage", "getProjectionIndices"] 

 

import numpy as np 

 

import lsst.afw.detection as afwDetect 

from .maskedImage import MaskedImage, makeMaskedImage 

from .image import Mask 

from .filter import Filter, FilterProperty 

 

 

def clipImage(im, minClip, maxClip): 

"""Clip an image to lie between minClip and maxclip (None to ignore)""" 

if isinstance(im, MaskedImage): 

mi = im 

else: 

mi = makeMaskedImage(im, Mask(im.getDimensions())) 

 

if minClip is not None: 

ds = afwDetect.FootprintSet( 

mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False)) 

afwDetect.setImageFromFootprintList( 

mi.getImage(), ds.getFootprints(), minClip) 

 

if maxClip is not None: 

ds = afwDetect.FootprintSet(mi, afwDetect.Threshold(maxClip)) 

afwDetect.setImageFromFootprintList( 

mi.getImage(), ds.getFootprints(), maxClip) 

 

 

def resetFilters(): 

"""Reset registry of filters and filter properties""" 

Filter.reset() 

FilterProperty.reset() 

 

 

def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False): 

"""Define a filter and its properties in the filter registry""" 

prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force) 

Filter.define(prop) 

if isinstance(alias, str): 

Filter.defineAlias(name, alias) 

else: 

for a in alias: 

Filter.defineAlias(name, a) 

 

 

def getProjectionIndices(imageBBox, targetBBox): 

"""Get the indices to project an image 

 

Given an image and target bounding box, 

calculate the indices needed to appropriately 

slice the input image and target image to 

project the image to the target. 

 

Parameters 

---------- 

imageBBox: Box2I 

Bounding box of the input image 

targetBBox: Box2I 

Bounding box of the target image 

 

Returns 

------- 

targetSlices: tuple 

Slices of the target image in the form (by, bx), (iy, ix). 

imageIndices: tuple 

Slices of the input image in the form (by, bx), (iy, ix). 

""" 

def getMin(dXmin): 

"""Get minimum indices""" 

if dXmin < 0: 

bxStart = -dXmin 

ixStart = 0 

else: 

bxStart = 0 

ixStart = dXmin 

return bxStart, ixStart 

 

def getMax(dXmax): 

"""Get maximum indices""" 

if dXmax < 0: 

bxEnd = None 

ixEnd = dXmax 

elif dXmax != 0: 

bxEnd = -dXmax 

ixEnd = None 

else: 

bxEnd = ixEnd = None 

return bxEnd, ixEnd 

 

dXmin = targetBBox.getMinX() - imageBBox.getMinX() 

dXmax = targetBBox.getMaxX() - imageBBox.getMaxX() 

dYmin = targetBBox.getMinY() - imageBBox.getMinY() 

dYmax = targetBBox.getMaxY() - imageBBox.getMaxY() 

 

bxStart, ixStart = getMin(dXmin) 

byStart, iyStart = getMin(dYmin) 

bxEnd, ixEnd = getMax(dXmax) 

byEnd, iyEnd = getMax(dYmax) 

 

bx = slice(bxStart, bxEnd) 

by = slice(byStart, byEnd) 

ix = slice(ixStart, ixEnd) 

iy = slice(iyStart, iyEnd) 

return (by, bx), (iy, ix) 

 

 

def projectImage(image, bbox, fill=0): 

"""Project an image into a bounding box 

 

Return a new image whose pixels are equal to those of 

`image` within `bbox`, and equal to `fill` outside. 

 

Parameters 

---------- 

image: `afw.Image` or `afw.MaskedImage` 

The image to project 

bbox: `Box2I` 

The bounding box to project onto. 

fill: number 

The value to fill the region of the new 

image outside the bounding box of the original. 

 

Returns 

------- 

newImage: `afw.Image` or `afw.MaskedImage` 

The new image with the input image projected 

into its bounding box. 

""" 

if image.getBBox() == bbox: 

return image 

(by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox) 

 

if isinstance(image, MaskedImage): 

newImage = type(image.image)(bbox) 

newImage.array[by, bx] = image.image.array[iy, ix] 

newMask = type(image.mask)(bbox) 

newMask.array[by, bx] = image.mask.array[iy, ix] 

newVariance = type(image.image)(bbox) 

newVariance.array[by, bx] = image.variance.array[iy, ix] 

newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype) 

else: 

newImage = type(image)(bbox) 

if fill != 0: 

newImage.set(fill) 

newImage.array[by, bx] = image.array[iy, ix] 

return newImage