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

# This file is currently part of obs_lsst but is written to allow it 

# to be migrated to the astro_metadata_translator package at a later date. 

# 

# This product includes software developed by the LSST Project 

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

# See the LICENSE file in this directory for details of code ownership. 

# 

# Use of this source code is governed by a 3-clause BSD-style 

# license that can be found in the LICENSE file. 

 

"""Metadata translation code for LSST simulations""" 

 

__all__ = ("LsstSimTranslator", ) 

 

import warnings 

import logging 

 

import astropy.utils.exceptions 

from astropy.time import Time 

from astropy.coordinates import AltAz 

 

from astro_metadata_translator import cache_translation, StubTranslator 

 

from .lsst import LSST_LOCATION, read_detector_ids, compute_detector_exposure_id_generic 

 

log = logging.getLogger(__name__) 

 

 

class LsstSimTranslator(StubTranslator): 

"""Shared routines for LSST Simulated Data. 

""" 

cameraPolicyFile = None 

"""Path to policy file relative to obs_lsst root.""" 

 

detectorMapping = None 

"""Mapping of detector name to detector number and serial.""" 

 

detectorSerials = None 

"""Mapping of detector serial number to raft, number, and name.""" 

 

@staticmethod 

def compute_detector_exposure_id(exposure_id, detector_num): 

"""Compute the detector exposure ID from detector number and 

exposure ID. 

 

This is a helper method to allow code working outside the translator 

infrastructure to use the same algorithm. 

 

Parameters 

---------- 

exposure_id : `int` 

Unique exposure ID. 

detector_num : `int` 

Detector number. 

 

Returns 

------- 

detector_exposure_id : `int` 

The calculated ID. 

""" 

return compute_detector_exposure_id_generic(exposure_id, detector_num, max_num=1000, 

mode="concat") 

 

@classmethod 

def detector_mapping(cls): 

"""Returns the mapping of full name to detector ID and serial. 

 

Returns 

------- 

mapping : `dict` of `str`:`tuple` 

Returns the mapping of full detector name (group+detector) 

to detector number and serial. 

 

Notes 

----- 

Will construct the mapping if none has previously been constructed. 

""" 

if cls.cameraPolicyFile is not None: 

if cls.detectorMapping is None: 

cls.detectorMapping = read_detector_ids(cls.cameraPolicyFile) 

 

return cls.detectorMapping 

 

@classmethod 

def detector_serials(cls): 

"""Obtain the mapping of detector serial to detector group, name, 

and number. 

 

Returns 

------- 

info : `dict` of `tuple` of (`str`, `str`, `int`) 

A `dict` with the serial numbers as keys and values of detector 

group, name, and number. 

""" 

if cls.detectorSerials is None: 

detector_mapping = cls.detector_mapping() 

 

if detector_mapping is not None: 

# Form mapping to go from serial number to names/numbers 

serials = {} 

for fullname, (id, serial) in cls.detectorMapping.items(): 

raft, detector_name = fullname.split("_") 

if serial in serials: 

raise RuntimeError(f"Serial {serial} is defined in multiple places") 

serials[serial] = (raft, detector_name, id) 

cls.detectorSerials = serials 

else: 

raise RuntimeError("Unable to obtain detector mapping information") 

 

return cls.detectorSerials 

 

@classmethod 

def compute_detector_num_from_name(cls, detector_group, detector_name): 

"""Helper method to return the detector number from the name. 

 

Parameters 

---------- 

detector_group : `str` 

Name of the detector grouping. This is generally the raft name. 

detector_name : `str` 

Detector name. 

 

Returns 

------- 

num : `int` 

Detector number. 

""" 

fullname = f"{detector_group}_{detector_name}" 

 

num = None 

detector_mapping = cls.detector_mapping() 

if detector_mapping is None: 

raise RuntimeError("Unable to obtain detector mapping information") 

 

if fullname in detector_mapping: 

num = detector_mapping[fullname] 

else: 

log.warning(f"Unable to determine detector number from detector name {fullname}") 

return None 

 

return num[0] 

 

@classmethod 

def compute_detector_info_from_serial(cls, detector_serial): 

"""Helper method to return the detector information from the serial. 

 

Parameters 

---------- 

detector_serial : `str` 

Detector serial ID. 

 

Returns 

------- 

info : `tuple` of (`str`, `str`, `int`) 

Detector group, name, and number. 

""" 

serial_mapping = cls.detector_serials() 

if serial_mapping is None: 

raise RuntimeError("Unable to obtain serial mapping information") 

 

if detector_serial in serial_mapping: 

info = serial_mapping[detector_serial] 

else: 

raise RuntimeError("Unable to determine detector information from detector serial" 

f" {detector_serial}") 

 

return info 

 

@cache_translation 

def to_telescope(self): 

# Docstring will be inherited. Property defined in properties.py 

telescope = None 

if "OUTFILE" in self._header and self._header["OUTFILE"].startswith("lsst"): 

telescope = "LSST" 

self._used_these_cards("OUTFILE") 

elif "LSST_NUM" in self._header: 

telescope = "LSST" 

self._used_these_cards("LSST_NUM") 

return telescope 

 

@cache_translation 

def to_location(self): 

# Docstring will be inherited. Property defined in properties.py 

location = None 

tel = self.to_telescope() 

if tel == "LSST": 

location = LSST_LOCATION 

return location 

 

@cache_translation 

def to_datetime_begin(self): 

# Docstring will be inherited. Property defined in properties.py 

self._used_these_cards("MJD-OBS") 

return Time(self._header["MJD-OBS"], scale="tai", format="mjd") 

 

@cache_translation 

def to_datetime_end(self): 

# Docstring will be inherited. Property defined in properties.py 

return self.to_datetime_begin() + self.to_exposure_time() 

 

@cache_translation 

def to_detector_num(self): 

# Docstring will be inherited. Property defined in properties.py 

raft = self.to_detector_group() 

detector = self.to_detector_name() 

return self.compute_detector_num_from_name(raft, detector) 

 

@cache_translation 

def to_detector_exposure_id(self): 

exposure_id = self.to_exposure_id() 

num = self.to_detector_num() 

return self.compute_detector_exposure_id(exposure_id, num) 

 

@cache_translation 

def to_observation_type(self): 

# Docstring will be inherited. Property defined in properties.py 

obstype = self._header["IMGTYPE"] 

self._used_these_cards("IMGTYPE") 

obstype = obstype.lower() 

if obstype == "skyexp": 

obstype = "science" 

return obstype 

 

@cache_translation 

def to_altaz_begin(self): 

# Docstring will be inherited. Property defined in properties.py 

if self.to_observation_type() == "science": 

# Derive from RADec in absence of any other information 

radec = self.to_tracking_radec() 

if radec is not None: 

# This triggers warnings because of the future dates 

with warnings.catch_warnings(): 

warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning) 

altaz = radec.transform_to(AltAz) 

return altaz 

return None