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

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 AURA/LSST. 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ["GaussianPsfFactory", "SigmaPerFwhm"] 

 

import math 

 

from lsst.pex.config import Config, Field, ConfigurableField 

from .singleGaussianPsf import SingleGaussianPsf 

from .doubleGaussianPsf import DoubleGaussianPsf 

 

SigmaPerFwhm = 1.0 / (2.0 * math.sqrt(2.0 * math.log(2.0))) 

 

 

def isPositive(x): 

return x > 0 

 

 

class GaussianPsfFactory(Config): 

"""Factory for simple Gaussian PSF models 

 

Provides a high-level interface to DoubleGaussianPsf and SingleGaussianPsf 

by specifying Gaussian PSF model width in FWHM instead of sigma, 

and supporting computing kernel size as a multiple of PSF width. 

This makes it suitable for tasks where PSF width is not known in advance. 

""" 

size = Field( 

doc="Kernel size (width and height) (pixels); if None then sizeFactor is used", 

dtype=int, 

optional=True, 

default=None, 

check=isPositive, 

) 

sizeFactor = Field( 

doc="Kernel size as a factor of fwhm (dimensionless); " 

"size = sizeFactor * fwhm; ignored if size is not None", 

dtype=float, 

optional=False, 

default=3.0, 

check=isPositive, 

) 

minSize = Field( 

doc="Minimum kernel size if using sizeFactor (pixels); ignored if size is not None", 

dtype=int, 

optional=True, 

default=5, 

check=isPositive, 

) 

maxSize = Field( 

doc="Maximum kernel size if using sizeFactor (pixels); ignored if size is not None", 

dtype=int, 

optional=True, 

default=None, 

check=isPositive, 

) 

defaultFwhm = Field( 

doc="Default FWHM of Gaussian model of core of star (pixels)", 

dtype=float, 

default=3.0, 

check=isPositive, 

) 

addWing = Field( 

doc="Add a Gaussian to represent wings?", 

dtype=bool, 

optional=False, 

default=True, 

) 

wingFwhmFactor = Field( 

doc="wing width, as a multiple of core width (dimensionless); ignored if addWing false", 

dtype=float, 

optional=False, 

default=2.5, 

check=isPositive, 

) 

wingAmplitude = Field( 

doc="wing amplitude, as a multiple of core amplitude (dimensionless); ignored if addWing false", 

dtype=float, 

optional=False, 

default=0.1, 

check=isPositive, 

) 

 

def computeSizeAndSigma(self, fwhm=None): 

"""Compute kernel size and star width as sigma 

 

kernel size will be odd unless minSize or maxSize is used and that value is even. 

 

@param[in] fwhm: FWHM of core star (pixels); if None then defaultFwhm is used 

@return two values: 

- kernel size (width == height) in pixels 

- sigma equivalent to supplied fwhm, assuming a Gaussian (pixels) 

 

@warning assumes a valid config 

""" 

if fwhm is None: 

fwhm = self.defaultFwhm 

 

if self.size is not None: 

size = self.size 

else: 

desSize = (int(self.sizeFactor * fwhm) // 2) * 2 + 1 # make result odd 

if self.minSize and self.minSize > desSize: 

size = self.minSize 

elif self.maxSize and self.maxSize < desSize: 

size = self.maxSize 

else: 

size = desSize 

 

return size, fwhm * SigmaPerFwhm 

 

def validate(self): 

Config.validate(self) 

if self.minSize and self.maxSize and self.minSize > self.maxSize: 

raise RuntimeError("minSize=%s > maxSize=%s" % (self.minSize, self.maxSize)) 

 

def apply(self, fwhm=None): 

"""Construct a GaussianPsf 

 

@param[in] self: an instance of ConfigClass 

@param[in] fwhm: FWHM of core of star (pixels); if None then self.defaultFwhm is used 

@return a DoubleGaussianPsf if self.addWing is True, else a SingleGaussianPsf 

""" 

kernelSize, sigma = self.computeSizeAndSigma(fwhm) 

if self.addWing: 

wingsSigma = sigma * self.wingFwhmFactor 

return DoubleGaussianPsf(kernelSize, kernelSize, sigma, wingsSigma, self.wingAmplitude) 

else: 

return SingleGaussianPsf(kernelSize, kernelSize, sigma) 

 

@classmethod 

def makeField(cls, doc): 

"""Make an lsst.pex.config.ConfigurableField 

""" 

def applyWrapper(config, **kwargs): 

"""Construct a Gaussian PSF 

 

@param[in] config: an instance of GaussianPsfFactory 

""" 

return config.apply(**kwargs) 

return ConfigurableField( 

doc=doc, 

target=applyWrapper, 

ConfigClass=cls 

)