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

# 

# 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__ = ["MakePsfCandidatesConfig", "MakePsfCandidatesTask"] 

 

import numpy as np 

 

from lsst.afw.table import SourceCatalog 

import lsst.afw.math as afwMath 

import lsst.pex.config as pexConfig 

import lsst.pex.exceptions 

import lsst.pipe.base as pipeBase 

from . import makePsfCandidate 

 

 

class MakePsfCandidatesConfig(pexConfig.Config): 

kernelSize = pexConfig.Field( 

doc="size of the kernel to create", 

dtype=int, 

default=21, 

) 

borderWidth = pexConfig.Field( 

doc="number of pixels to ignore around the edge of PSF candidate postage stamps", 

dtype=int, 

default=0, 

) 

 

 

class MakePsfCandidatesTask(pipeBase.Task): 

"""Create PSF candidates given an input catalog. 

""" 

ConfigClass = MakePsfCandidatesConfig 

_DefaultName = "makePsfCandidates" 

 

def __init__(self, **kwds): 

pipeBase.Task.__init__(self, **kwds) 

 

def run(self, starCat, exposure, psfCandidateField=None): 

"""Make a list of PSF candidates from a star catalog. 

 

Parameters 

---------- 

starCat : `lsst.afw.table.SourceCatalog` 

Catalog of stars, as returned by 

``lsst.meas.algorithms.starSelector.run()``. 

exposure : `lsst.afw.image.Exposure` 

The exposure containing the sources. 

psfCandidateField : `str` or None 

Name of flag field to set True for PSF candidates, or None to not 

set a field; the field is left unchanged for non-candidates. 

 

Returns 

------- 

struct : `lsst.pipe.base.Struct` 

Results struct containing: 

 

- ``psfCandidates`` : List of PSF candidates 

(`list` of `lsst.meas.algorithms.PsfCandidate`). 

- ``goodStarCat`` : Subset of ``starCat`` that was successfully made 

into PSF candidates (`lsst.afw.table.SourceCatalog`). 

 

""" 

psfResult = self.makePsfCandidates(starCat, exposure) 

 

if psfCandidateField is not None: 

isStarKey = starCat.schema[psfCandidateField].asKey() 

for star in psfResult.goodStarCat: 

star.set(isStarKey, True) 

 

return psfResult 

 

def makePsfCandidates(self, starCat, exposure): 

"""Make a list of PSF candidates from a star catalog. 

 

Parameters 

---------- 

starCat : `lsst.afw.table.SourceCatalog` 

Catalog of stars, as returned by 

``lsst.meas.algorithms.starSelector.run()``. 

exposure : `lsst.afw.image.Exposure` 

The exposure containing the sources. 

 

Returns 

------- 

struct : `lsst.pipe.base.Struct` 

Results struct containing: 

 

- ``psfCandidates`` : List of PSF candidates 

(`list` of `lsst.meas.algorithms.PsfCandidate`). 

- ``goodStarCat`` : Subset of ``starCat`` that was successfully made 

into PSF candidates (`lsst.afw.table.SourceCatalog`). 

""" 

goodStarCat = SourceCatalog(starCat.schema) 

 

psfCandidateList = [] 

didSetSize = False 

for star in starCat: 

try: 

psfCandidate = makePsfCandidate(star, exposure) 

 

# The setXXX methods are class static, but it's convenient to call them on 

# an instance as we don't know exposures's pixel type 

# (and hence psfCandidate's exact type) 

if not didSetSize: 

psfCandidate.setBorderWidth(self.config.borderWidth) 

psfCandidate.setWidth(self.config.kernelSize + 2*self.config.borderWidth) 

psfCandidate.setHeight(self.config.kernelSize + 2*self.config.borderWidth) 

didSetSize = True 

 

im = psfCandidate.getMaskedImage().getImage() 

except lsst.pex.exceptions.Exception as err: 

self.log.warn("Failed to make a psfCandidate from star %d: %s", star.getId(), err) 

continue 

 

vmax = afwMath.makeStatistics(im, afwMath.MAX).getValue() 

if not np.isfinite(vmax): 

continue 

psfCandidateList.append(psfCandidate) 

goodStarCat.append(star) 

 

return pipeBase.Struct( 

psfCandidates=psfCandidateList, 

goodStarCat=goodStarCat, 

)