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

""" 

This file defines the model classes that wrap noise models from 

galsim into the CatSim interface 

""" 

 

from builtins import object 

import numpy 

import galsim 

from lsst.sims.photUtils import calcSkyCountsPerPixelForM5, PhotometricParameters, \ 

LSSTdefaults 

 

__all__ = ["ExampleCCDNoise"] 

 

class NoiseAndBackgroundBase(object): 

""" 

This is the base class for all wrappers of sky background 

and noise in the GalSim interface. Daughters of this class 

are meant to be included as the noise_and_background 

class member variable of GalSim InstanceCatalog classes. 

To implement a new noise model, users should write a new class 

that inherits from this one. That new class should only define 

a method getNoiseModel() that takes as arguments skyLevel, and an 

instantiation of the PhotometricParameters class defined in sims_photUtils 

(this will carry gain and readnoise information). See the docstring 

for getNoiseModel() for further details. 

""" 

 

def __init__(self, seed=None, addNoise=True, addBackground=True): 

""" 

@param [in] addNoise is a boolean telling the wrapper whether or not 

to add noise to the image 

 

@param [in] addBackground is a boolean telling the wrapper whether 

or not to add the skybackground to the image 

 

@param [in] seed is an (optional) int that will seed the 

random number generator used by the noise model. Defaults to None, 

which causes GalSim to generate the seed from the system. 

""" 

 

self.addNoise = addNoise 

self.addBackground = addBackground 

 

44 ↛ 45line 44 didn't jump to line 45, because the condition on line 44 was never true if seed is None: 

self.randomNumbers = galsim.UniformDeviate() 

else: 

self.randomNumbers = galsim.UniformDeviate(seed) 

 

 

def getNoiseModel(self, skyLevel=0.0, photParams=None): 

""" 

This method returns the noise model implemented for this wrapper 

class. 

 

@param [in] skyLevel is the number of electrons per pixel due 

to the sky background. However, this value should only be non-zero 

if the sky background has been subtracted from the image. The 

purpose of this parameter is to provide an extra background value 

when calculating the level of Poisson noise in each pixel. If the 

sky background is already present in the image, then the noise model 

will just set the noise level based on the intensity in each pixel 

and there is no need to add an additional skyLevel. If the sky 

background is still included in the image, set skyLevel equal to zero. 

 

@param [in] photParams is an instantiation of the 

PhotometricParameters class that carries details about the 

photometric response of the telescope. Defaults to None. 

 

@param [out] returns an instantiation of a GalSim noise class, as 

specified by the particular wrapper class to which this method belongs. 

""" 

 

raise NotImplementedError("There is no noise model for NoiseAndBackgroundBase") 

 

 

def addNoiseAndBackground(self, image, bandpass=None, m5=None, 

FWHMeff=None, 

photParams=None, 

detector=None): 

""" 

This method actually adds the sky background and noise to an image. 

 

Note: default parameters are defined in 

 

sims_photUtils/python/lsst/sims/photUtils/photometricDefaults.py 

 

@param [in] image is the GalSim image object to which the background 

and noise are being added. 

 

@param [in] bandpass is a CatSim bandpass object (not a GalSim bandpass 

object) characterizing the filter through which the image is being taken. 

 

@param [in] FWHMeff is the FWHMeff in arcseconds 

 

@param [in] photParams is an instantiation of the 

PhotometricParameters class that carries details about the 

photometric response of the telescope. Defaults to None. 

 

@param [in] detector is the GalSimDetector corresponding to the image. 

Defaults to None. 

 

@param [out] the input image with the background and noise model added to it. 

 

""" 

 

 

#calculate the sky background to be added to each pixel 

skyCounts = calcSkyCountsPerPixelForM5(m5, bandpass, FWHMeff=FWHMeff, photParams=photParams) 

 

image = image.copy() 

 

112 ↛ 120line 112 didn't jump to line 120, because the condition on line 112 was never false if self.addBackground: 

image += skyCounts 

skyLevel = 0.0 #if we are adding the skyCounts to the image,there is no need 

#to pass a skyLevel parameter to the noise model. skyLevel is 

#just used to calculate the level of Poisson noise. If the 

#sky background is included in the image, the Poisson noise 

#will be calculated from the actual image brightness. 

else: 

skyLevel = skyCounts*photParams.gain 

 

if self.addNoise: 

noiseModel = self.getNoiseModel(skyLevel=skyLevel, photParams=photParams) 

image.addNoise(noiseModel) 

 

return image 

 

 

class ExampleCCDNoise(NoiseAndBackgroundBase): 

""" 

This class wraps the GalSim class CCDNoise. It is meant to be assigned as 

the self.noise_and_background member variable in a GalSim InstanceCatalog. 

To instantiatea different noise model, write a class like this one that 

defines a method getNoiseModel() which accepts as its arguments skyLevel, 

readNoise, and gain and returns an instantiation of a GalSim noise model 

""" 

 

def getNoiseModel(self, skyLevel=0.0, photParams=None): 

 

""" 

This method returns the noise model implemented for this wrapper 

class. 

 

@param [in] skyLevel is the number of electrons per pixel due 

to the sky background. However, this value should only be non-zero 

if the sky background has been subtracted from the image. The 

purpose of this parameter is to provide an extra background value 

when calculating the level of Poisson noise in each pixel. If the 

sky background is already present in the image, then the noise model 

will just set the noise level based on the intensity in each pixel 

and there is no need to add an additional skyLevel. If the sky 

background is still included in the image, set skyLevel equal to zero. 

 

@param [in] photParams is an instantiation of the 

PhotometricParameters class that carries details about the 

photometric response of the telescope. Defaults to None. 

 

@param [out] returns an instantiation of the GalSim CCDNoise class 

""" 

 

return galsim.CCDNoise(self.randomNumbers, sky_level=skyLevel, 

gain=photParams.gain, read_noise=photParams.readnoise)