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""" 

2This file defines the model classes that wrap noise models from 

3galsim into the CatSim interface 

4""" 

5 

6from builtins import object 

7import numpy 

8import galsim 

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

10 LSSTdefaults 

11 

12__all__ = ["ExampleCCDNoise"] 

13 

14class NoiseAndBackgroundBase(object): 

15 """ 

16 This is the base class for all wrappers of sky background 

17 and noise in the GalSim interface. Daughters of this class 

18 are meant to be included as the noise_and_background 

19 class member variable of GalSim InstanceCatalog classes. 

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

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

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

23 instantiation of the PhotometricParameters class defined in sims_photUtils 

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

25 for getNoiseModel() for further details. 

26 """ 

27 

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

29 """ 

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

31 to add noise to the image 

32 

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

34 or not to add the skybackground to the image 

35 

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

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

38 which causes GalSim to generate the seed from the system. 

39 """ 

40 

41 self.addNoise = addNoise 

42 self.addBackground = addBackground 

43 

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

45 self.randomNumbers = galsim.UniformDeviate() 

46 else: 

47 self.randomNumbers = galsim.UniformDeviate(seed) 

48 

49 

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

51 """ 

52 This method returns the noise model implemented for this wrapper 

53 class. 

54 

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

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

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

58 purpose of this parameter is to provide an extra background value 

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

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

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

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

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

64 

65 @param [in] photParams is an instantiation of the 

66 PhotometricParameters class that carries details about the 

67 photometric response of the telescope. Defaults to None. 

68 

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

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

71 """ 

72 

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

74 

75 

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

77 FWHMeff=None, 

78 photParams=None, 

79 detector=None): 

80 """ 

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

82 

83 Note: default parameters are defined in 

84 

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

86 

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

88 and noise are being added. 

89 

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

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

92 

93 @param [in] FWHMeff is the FWHMeff in arcseconds 

94 

95 @param [in] photParams is an instantiation of the 

96 PhotometricParameters class that carries details about the 

97 photometric response of the telescope. Defaults to None. 

98 

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

100 Defaults to None. 

101 

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

103 

104 """ 

105 

106 

107 #calculate the sky background to be added to each pixel 

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

109 

110 image = image.copy() 

111 

112 if self.addBackground: 

113 image += skyCounts 

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

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

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

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

118 #will be calculated from the actual image brightness. 

119 else: 

120 skyLevel = skyCounts*photParams.gain 

121 

122 if self.addNoise: 

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

124 image.addNoise(noiseModel) 

125 

126 return image 

127 

128 

129class ExampleCCDNoise(NoiseAndBackgroundBase): 

130 """ 

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

132 the self.noise_and_background member variable in a GalSim InstanceCatalog. 

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

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

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

136 """ 

137 

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

139 

140 """ 

141 This method returns the noise model implemented for this wrapper 

142 class. 

143 

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

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

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

147 purpose of this parameter is to provide an extra background value 

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

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

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

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

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

153 

154 @param [in] photParams is an instantiation of the 

155 PhotometricParameters class that carries details about the 

156 photometric response of the telescope. Defaults to None. 

157 

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

159 """ 

160 

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

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

163 

164