Coverage for python/lsst/atmospec/centroiding.py: 40%

71 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-07 02:31 -0700

1# This file is part of atmospec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import lsst.afw.image as afwImage 

23import lsst.pipe.base as pipeBase 

24 

25from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask, MagnitudeLimit, ReferenceObjectLoader 

26from lsst.meas.astrom import AstrometryTask, FitAffineWcsTask 

27from lsst.pipe.tasks.quickFrameMeasurement import (QuickFrameMeasurementTask) 

28from lsst.pipe.base.task import TaskError 

29import lsst.pipe.base.connectionTypes as cT 

30import lsst.pex.config as pexConfig 

31 

32from .utils import getTargetCentroidFromWcs 

33 

34__all__ = ['SingleStarCentroidTaskConfig', 'SingleStarCentroidTask'] 

35 

36 

37class SingleStarCentroidTaskConnections(pipeBase.PipelineTaskConnections, 

38 dimensions=("instrument", "visit", "detector")): 

39 inputExp = cT.Input( 

40 name="icExp", 

41 doc="Image-characterize output exposure", 

42 storageClass="ExposureF", 

43 dimensions=("instrument", "visit", "detector"), 

44 multiple=False, 

45 ) 

46 inputSources = cT.Input( 

47 name="icSrc", 

48 doc="Image-characterize output sources.", 

49 storageClass="SourceCatalog", 

50 dimensions=("instrument", "visit", "detector"), 

51 multiple=False, 

52 ) 

53 astromRefCat = cT.PrerequisiteInput( 

54 doc="Reference catalog to use for astrometry", 

55 name="gaia_dr2_20200414", 

56 storageClass="SimpleCatalog", 

57 dimensions=("skypix",), 

58 deferLoad=True, 

59 multiple=True, 

60 ) 

61 atmospecCentroid = cT.Output( 

62 name="atmospecCentroid", 

63 doc="The main star centroid in yaml format.", 

64 storageClass="StructuredDataDict", 

65 dimensions=("instrument", "visit", "detector"), 

66 ) 

67 

68 

69class SingleStarCentroidTaskConfig(pipeBase.PipelineTaskConfig, 

70 pipelineConnections=SingleStarCentroidTaskConnections): 

71 """Configuration parameters for ProcessStarTask.""" 

72 astromRefObjLoader = pexConfig.ConfigurableField( 

73 target=LoadIndexedReferenceObjectsTask, 

74 doc="Reference object loader for astrometric calibration", 

75 ) 

76 astrometry = pexConfig.ConfigurableField( 

77 target=AstrometryTask, 

78 doc="Task to perform astrometric calibration to refine the WCS", 

79 ) 

80 qfmTask = pexConfig.ConfigurableField( 

81 target=QuickFrameMeasurementTask, 

82 doc="XXX", 

83 ) 

84 referenceFilterOverride = pexConfig.Field( 

85 dtype=str, 

86 doc="Which filter in the reference catalog to match to?", 

87 default="phot_g_mean" 

88 ) 

89 

90 def setDefaults(self): 

91 super().setDefaults() 

92 # this is a null option now in Gen3 - do not set it here 

93 # self.astromRefObjLoader.ref_dataset_name 

94 

95 self.astromRefObjLoader.pixelMargin = 1000 

96 

97 self.astrometry.wcsFitter.retarget(FitAffineWcsTask) 

98 self.astrometry.referenceSelector.doMagLimit = True 

99 magLimit = MagnitudeLimit() 

100 magLimit.minimum = 1 

101 magLimit.maximum = 15 

102 self.astrometry.referenceSelector.magLimit = magLimit 

103 self.astrometry.referenceSelector.magLimit.fluxField = "phot_g_mean_flux" 

104 self.astrometry.matcher.maxRotationDeg = 5.99 

105 self.astrometry.matcher.maxOffsetPix = 3000 

106 self.astrometry.sourceSelector['matcher'].minSnr = 10 

107 

108 

109class SingleStarCentroidTask(pipeBase.PipelineTask): 

110 """XXX Docs here 

111 """ 

112 

113 ConfigClass = SingleStarCentroidTaskConfig 

114 _DefaultName = 'singleStarCentroid' 

115 

116 def __init__(self, initInputs=None, **kwargs): 

117 super().__init__(**kwargs) 

118 

119 self.makeSubtask("astrometry", refObjLoader=None) 

120 self.makeSubtask('qfmTask') 

121 

122 def runQuantum(self, butlerQC, inputRefs, outputRefs): 

123 inputs = butlerQC.get(inputRefs) 

124 refObjLoader = ReferenceObjectLoader(dataIds=[ref.datasetRef.dataId 

125 for ref in inputRefs.astromRefCat], 

126 refCats=inputs.pop('astromRefCat'), 

127 config=self.config.astromRefObjLoader, log=self.log) 

128 

129 refObjLoader.pixelMargin = 1000 

130 self.astrometry.setRefObjLoader(refObjLoader) 

131 

132 # See L603 (def runQuantum(self, butlerQC, inputRefs, outputRefs):) 

133 # in calibrate.py to put photocal back in 

134 

135 outputs = self.run(**inputs) 

136 butlerQC.put(outputs, outputRefs) 

137 

138 def run(self, inputExp, inputSources): 

139 """XXX Docs 

140 """ 

141 

142 # TODO: Change this to doing this the proper way 

143 referenceFilterName = self.config.referenceFilterOverride 

144 referenceFilterLabel = afwImage.FilterLabel(physical=referenceFilterName, band=referenceFilterName) 

145 # there's a better way of doing this with the task I think 

146 originalFilterLabel = inputExp.getFilterLabel() 

147 inputExp.setFilterLabel(referenceFilterLabel) 

148 

149 successfulFit = False 

150 try: 

151 astromResult = self.astrometry.run(sourceCat=inputSources, exposure=inputExp) 

152 scatter = astromResult.scatterOnSky.asArcseconds() 

153 inputExp.setFilterLabel(originalFilterLabel) 

154 if scatter < 1: 

155 successfulFit = True 

156 except (RuntimeError, TaskError): 

157 self.log.warn("Solver failed to run completely") 

158 inputExp.setFilterLabel(originalFilterLabel) 

159 

160 if successfulFit: 

161 target = inputExp.getMetadata()['OBJECT'] 

162 centroid = getTargetCentroidFromWcs(inputExp, target, logger=self.log) 

163 else: 

164 result = self.qfmTask.run(inputExp) 

165 centroid = result.brightestObjCentroid 

166 

167 centroidTuple = (centroid[0], centroid[1]) # unify Point2D or tuple to tuple 

168 self.log.info(f"Centroid of main star found at {centroidTuple} found" 

169 f" via {'astrometry' if successfulFit else 'QuickFrameMeasurement'}") 

170 result = pipeBase.Struct(atmospecCentroid={'centroid': centroidTuple, 

171 'astrometricMatch': successfulFit}) 

172 return result