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

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

# This file is part of obs_lsst. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

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

# for details of code ownership. 

# 

# 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 GNU General Public License 

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

 

__all__ = ("LsstCamInstrument", "ImsimInstrument", "PhosimInstrument", "Ts8Instrument", 

"LatissInstrument", "Ts3Instrument", "UcdCamInstrument", "LsstComCamInstrument") 

 

import os.path 

from dateutil import parser 

 

import lsst.obs.base.yamlCamera as yamlCamera 

from lsst.utils import getPackageDir 

from lsst.obs.base.instrument import Instrument, addUnboundedCalibrationLabel 

from lsst.daf.butler import DatasetType 

from lsst.pipe.tasks.read_curated_calibs import read_all 

from ..filters import LSSTCAM_FILTER_DEFINITIONS, LATISS_FILTER_DEFINITIONS 

 

from ..translators import LsstLatissTranslator, LsstCamTranslator, \ 

LsstUCDCamTranslator, LsstTS3Translator, LsstComCamTranslator, \ 

PhosimTranslator, LsstTS8Translator, ImsimTranslator 

 

PACKAGE_DIR = getPackageDir("obs_lsst") 

 

 

class LsstCamInstrument(Instrument): 

"""Gen3 Butler specialization for the LSST Main Camera. 

 

Parameters 

---------- 

camera : `lsst.cameraGeom.Camera` 

Camera object from which to extract detector information. 

filters : `list` of `FilterDefinition` 

An ordered list of filters to define the set of PhysicalFilters 

associated with this instrument in the registry. 

 

While both the camera geometry and the set of filters associated with a 

camera are expected to change with time in general, their Butler Registry 

representations defined by an Instrument do not. Instead: 

 

- We only extract names, IDs, and purposes from the detectors in the 

camera, which should be static information that actually reflects 

detector "slots" rather than the physical sensors themselves. Because 

the distinction between physical sensors and slots is unimportant in 

the vast majority of Butler use cases, we just use "detector" even 

though the concept really maps better to "detector slot". Ideally in 

the future this distinction between static and time-dependent 

information would be encoded in cameraGeom itself (e.g. by making the 

time-dependent Detector class inherit from a related class that only 

carries static content). 

 

- The Butler Registry is expected to contain physical_filter entries for 

all filters an instrument has ever had, because we really only care 

about which filters were used for particular observations, not which 

filters were *available* at some point in the past. And changes in 

individual filters over time will be captured as changes in their 

TransmissionCurve datasets, not changes in the registry content (which 

is really just a label). While at present Instrument and Registry 

do not provide a way to add new physical_filters, they will in the 

future. 

""" 

filterDefinitions = LSSTCAM_FILTER_DEFINITIONS 

instrument = "lsstCam" 

policyName = "lsstCam" 

_camera = None 

_cameraCachedClass = None 

translatorClass = LsstCamTranslator 

 

@property 

def configPaths(self): 

return [os.path.join(PACKAGE_DIR, "config"), 

os.path.join(PACKAGE_DIR, "config", self.policyName)] 

 

@classmethod 

def getName(cls): 

# Docstring inherited from Instrument.getName 

return cls.instrument 

 

@classmethod 

def getCamera(cls): 

# Constructing a YAML camera takes a long time so cache the result 

# We have to be careful to ensure we cache at the subclass level 

# since LsstCam base class will look like a cache to the subclasses 

if cls._camera is None or cls._cameraCachedClass != cls: 

cameraYamlFile = os.path.join(PACKAGE_DIR, "policy", f"{cls.policyName}.yaml") 

cls._camera = yamlCamera.makeCamera(cameraYamlFile) 

cls._cameraCachedClass = cls 

return cls._camera 

 

def getRawFormatter(self, dataId): 

# Docstring inherited from Instrument.getRawFormatter 

# local import to prevent circular dependency 

from .rawFormatter import LsstCamRawFormatter 

return LsstCamRawFormatter 

 

def register(self, registry): 

# Docstring inherited from Instrument.register 

# The maximum values below make Gen3's ObservationDataIdPacker produce 

# outputs that match Gen2's ccdExposureId. 

obsMax = self.translatorClass.max_detector_exposure_id() 

registry.insertDimensionData("instrument", 

{"name": self.getName(), 

"detector_max": self.translatorClass.DETECTOR_MAX, 

"visit_max": obsMax, 

"exposure_max": obsMax}) 

 

records = [self.extractDetectorRecord(detector) for detector in self.getCamera()] 

registry.insertDimensionData("detector", *records) 

 

self._registerFilters(registry) 

 

def extractDetectorRecord(self, camGeomDetector): 

"""Create a Gen3 Detector entry dict from a cameraGeom.Detector. 

""" 

# All of the LSST instruments have detector names like R??_S??; we'll 

# split them up here, and instruments with only one raft can override 

# to change the group to something else if desired. 

# Long-term, we should get these fields into cameraGeom separately 

# so there's no need to specialize at this stage. 

# They are separate in ObservationInfo 

group, name = camGeomDetector.getName().split("_") 

 

# getType() returns a pybind11-wrapped enum, which unfortunately 

# has no way to extract the name of just the value (it's always 

# prefixed by the enum type name). 

purpose = str(camGeomDetector.getType()).split(".")[-1] 

 

return dict( 

instrument=self.getName(), 

id=camGeomDetector.getId(), 

full_name=camGeomDetector.getName(), 

name_in_raft=name, 

purpose=purpose, 

raft=group, 

) 

 

def writeCuratedCalibrations(self, butler): 

"""Write human-curated calibration Datasets to the given Butler with 

the appropriate validity ranges. 

 

This is a temporary API that should go away once obs_ packages have 

a standardized approach to this problem. 

""" 

 

# Write cameraGeom.Camera, with an infinite validity range. 

datasetType = DatasetType("camera", ("instrument", "calibration_label"), "Camera", 

universe=butler.registry.dimensions) 

butler.registry.registerDatasetType(datasetType) 

unboundedDataId = addUnboundedCalibrationLabel(butler.registry, self.getName()) 

camera = self.getCamera() 

butler.put(camera, datasetType, unboundedDataId) 

 

# Write defects with validity ranges taken from 

# obs_lsst_data/{name}/defects (along with the defects themselves). 

datasetType = DatasetType("defects", ("instrument", "detector", "calibration_label"), "DefectsList", 

universe=butler.registry.dimensions) 

butler.registry.registerDatasetType(datasetType) 

defectPath = os.path.join(getPackageDir("obs_lsst"), self.policyName, "defects") 

 

if os.path.exists(defectPath): 

camera = self.getCamera() 

defectsDict = read_all(defectPath, camera)[0] # second return is calib type 

endOfTime = '20380119T031407' 

with butler.transaction(): 

for det in defectsDict: 

detector = camera[det] 

times = sorted([k for k in defectsDict[det]]) 

defects = [defectsDict[det][time] for time in times] 

times = times + [parser.parse(endOfTime), ] 

for defect, beginTime, endTime in zip(defects, times[:-1], times[1:]): 

md = defect.getMetadata() 

calibrationLabelName = f"defect/{md['CALIBDATE']}/{md['DETECTOR']}" 

butler.registry.insertDimensionData( 

"calibration_label", 

{ 

"instrument": self.getName(), 

"name": calibrationLabelName, 

"datetime_begin": beginTime, 

"datetime_end": endTime, 

} 

) 

butler.put(defect, datasetType, instrument=self.getName(), 

calibration_label=calibrationLabelName, detector=detector.getId()) 

 

 

class LsstComCamInstrument(LsstCamInstrument): 

"""Gen3 Butler specialization for ComCam data. 

""" 

 

instrument = "LSST-ComCam" 

policyName = "comCam" 

translatorClass = LsstComCamTranslator 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import LsstComCamRawFormatter 

return LsstComCamRawFormatter 

 

 

class ImsimInstrument(LsstCamInstrument): 

"""Gen3 Butler specialization for ImSim simulations. 

""" 

 

instrument = "LSST-ImSim" 

policyName = "imsim" 

translatorClass = ImsimTranslator 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import ImsimRawFormatter 

return ImsimRawFormatter 

 

 

class PhosimInstrument(LsstCamInstrument): 

"""Gen3 Butler specialization for Phosim simulations. 

""" 

 

instrument = "LSST-PhoSim" 

policyName = "phosim" 

translatorClass = PhosimTranslator 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import PhosimRawFormatter 

return PhosimRawFormatter 

 

 

class Ts8Instrument(LsstCamInstrument): 

"""Gen3 Butler specialization for raft test stand data. 

""" 

 

instrument = "LSST-TS8" 

policyName = "ts8" 

translatorClass = LsstTS8Translator 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import Ts8RawFormatter 

return Ts8RawFormatter 

 

 

class UcdCamInstrument(LsstCamInstrument): 

"""Gen3 Butler specialization for UCDCam test stand data. 

""" 

 

instrument = "UCDCam" 

policyName = "ucd" 

translatorClass = LsstUCDCamTranslator 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import UcdCamRawFormatter 

return UcdCamRawFormatter 

 

 

class Ts3Instrument(LsstCamInstrument): 

"""Gen3 Butler specialization for TS3 test stand data. 

""" 

 

instrument = "LSST-TS3" 

policyName = "ts3" 

translatorClass = LsstTS3Translator 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import Ts3RawFormatter 

return Ts3RawFormatter 

 

 

class LatissInstrument(LsstCamInstrument): 

"""Gen3 Butler specialization for AuxTel LATISS data. 

""" 

filterDefinitions = LATISS_FILTER_DEFINITIONS 

instrument = "LATISS" 

policyName = "latiss" 

translatorClass = LsstLatissTranslator 

 

def extractDetectorRecord(self, camGeomDetector): 

# Override to remove group (raft) name, because LATISS only has one 

# detector. 

record = super().extractDetectorRecord(camGeomDetector) 

record["raft"] = None 

record["name_in_raft"] = record["full_name"] 

return record 

 

def getRawFormatter(self, dataId): 

# local import to prevent circular dependency 

from .rawFormatter import LatissRawFormatter 

return LatissRawFormatter