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

# 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/>. 

 

import re 

from lsst.pipe.tasks.ingest import ParseTask 

from lsst.pipe.tasks.ingestCalibs import CalibsParseTask 

from astro_metadata_translator import ObservationInfo 

import lsst.log as lsstLog 

from .translators.lsst import ROLLOVERTIME 

 

EXTENSIONS = ["fits", "gz", "fz"] # Filename extensions to strip off 

 

__all__ = ["LsstCamParseTask"] 

 

 

class LsstCamParseTask(ParseTask): 

"""Parser suitable for lsstCam data. 

 

See `LCA-13501 <https://ls.st/LCA-13501>`_ and 

`LSE-400 <https://ls.st/LSE-400>`_. 

""" 

 

_translatorClass = None 

 

def __init__(self, config, *args, **kwargs): 

super().__init__(config, *args, **kwargs) 

 

self.observationInfo = None 

 

def getInfoFromMetadata(self, md, info=None): 

"""Attempt to pull the desired information out of the header. 

 

Parameters 

---------- 

md : `lsst.daf.base.PropertyList` 

FITS header. 

info : `dict`, optional 

File properties, to be updated by this routine. If `None` 

it will be created. 

 

Returns 

------- 

info : `dict` 

Translated information from the metadata. Updated form of the 

input parameter. 

 

Notes 

----- 

 

This is done through two mechanisms: 

 

* translation: a property is set directly from the relevant header 

keyword. 

* translator: a property is set with the result of calling a method. 

 

The translator methods receive the header metadata and should return 

the appropriate value, or None if the value cannot be determined. 

 

This implementation constructs an 

`~astro_metadata_translator.ObservationInfo` object prior to calling 

each translator method, making the translated information available 

through the ``observationInfo`` attribute. 

 

""" 

# Always calculate a new ObservationInfo since getInfo calls 

# this method repeatedly for each header. 

self.observationInfo = ObservationInfo(md, translator_class=self._translatorClass, 

pedantic=False) 

 

info = super().getInfoFromMetadata(md, info) 

 

# Ensure that the translated ObservationInfo is cleared. 

# This avoids possible confusion. 

self.observationInfo = None 

return info 

 

def translate_wavelength(self, md): 

"""Translate wavelength provided by teststand readout. 

 

The teststand driving script asks for a wavelength, and then reads the 

value back to ensure that the correct position was moved to. This 

number is therefore read back with sub-nm precision. Typically the 

position is within 0.005nm of the desired position, so we warn if it's 

not very close to an integer value. 

 

Future users should be aware that the ``HIERARCH MONOCH-WAVELENG`` key 

is NOT the requested value, and therefore cannot be used as a 

cross-check that the wavelength was close to the one requested. 

The only record of the wavelength that was set is in the original 

filename. 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

Image metadata. 

 

Returns 

------- 

wavelength : `int` 

The recorded wavelength in nanometers as an `int`. 

""" 

bad_wl = -666 # Bad value for wavelength 

if "MONOWL" not in md: 

return bad_wl 

 

raw_wl = md.getScalar("MONOWL") 

 

# Negative wavelengths are bad so normalize the bad value 

if raw_wl < 0: 

return bad_wl 

 

wl = int(round(raw_wl)) 

if abs(raw_wl-wl) >= 0.1: 

logger = lsstLog.Log.getLogger('obs.lsst.ingest') 

logger.warn( 

'Translated significantly non-integer wavelength; ' 

'%s is more than 0.1nm from an integer value', raw_wl) 

return wl 

 

def translate_dateObs(self, md): 

"""Retrieve the date of observation as an ISO format string. 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

Image metadata. 

 

Returns 

------- 

dateObs : `str` 

The date that the data was taken in FITS ISO format, 

e.g. ``2018-08-20T21:56:24.608``. 

""" 

dateObs = self.observationInfo.datetime_begin 

dateObs.format = "isot" 

return str(dateObs) 

 

translate_date = translate_dateObs 

 

def translate_dayObs(self, md): 

"""Generate the day that the observation was taken. 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

image metadata 

 

Returns 

------- 

dayObs : `str` 

The day that the data was taken, e.g. ``1958-02-05``. 

""" 

dateObs = self.observationInfo.datetime_begin 

dateObs -= ROLLOVERTIME 

dateObs.format = "iso" 

dateObs.out_subfmt = "date" # YYYY-MM-DD format 

return str(dateObs) 

 

def translate_snap(self, md): 

"""Extract snap number from metadata. 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

Image metadata. 

 

Returns 

------- 

snap : `int` 

Snap number (default: 0). 

""" 

try: 

return int(md.getScalar("SNAP")) 

except KeyError: 

return 0 

 

def translate_detectorName(self, md): 

"""Extract ccd ID from CHIPID. 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

Image metadata. 

 

Returns 

------- 

ccdID : `str` 

Name of ccd, e.g. ``S01``. 

""" 

return self.observationInfo.detector_name 

 

def translate_raftName(self, md): 

"""Extract raft ID from CHIPID. 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

Image metadata. 

 

Returns 

------- 

raftID : `str` 

Name of raft, e.g. ``R21``. 

""" 

return self.observationInfo.detector_group 

 

def translate_detector(self, md): 

"""Extract detector ID from metadata 

 

Parameters 

---------- 

md : `~lsst.daf.base.PropertyList` or `~lsst.daf.base.PropertySet` 

Image metadata. 

 

Returns 

------- 

detID : `int` 

Detector ID, e.g. ``4``. 

""" 

return self.observationInfo.detector_num 

 

def translate_expTime(self, md): 

return self.observationInfo.exposure_time.value 

 

def translate_object(self, md): 

return self.observationInfo.object 

 

def translate_imageType(self, md): 

obstype = self.observationInfo.observation_type.upper() 

# Dictionary for obstype values is not yet clear 

if obstype == "SCIENCE": 

obstype = "SKYEXP" 

return obstype 

 

def translate_filter(self, md): 

return self.observationInfo.physical_filter 

 

def translate_lsstSerial(self, md): 

return self.observationInfo.detector_serial 

 

def translate_run(self, md): 

return self.observationInfo.science_program 

 

def translate_visit(self, md): 

return self.observationInfo.visit_id 

 

 

class LsstCamCalibsParseTask(CalibsParseTask): 

"""Parser for calibs.""" 

 

def _translateFromCalibId(self, field, md): 

"""Get a value from the CALIB_ID written by ``constructCalibs``.""" 

data = md.getScalar("CALIB_ID") 

match = re.search(r".*%s=(\S+)" % field, data) 

return match.groups()[0] 

 

def translate_raftName(self, md): 

return self._translateFromCalibId("raftName", md) 

 

def translate_detectorName(self, md): 

return self._translateFromCalibId("detectorName", md) 

 

def translate_detector(self, md): 

# this is not a _great_ fix, but this obs_package is enforcing that 

# detectors be integers and there's not an elegant way of ensuring 

# this is the right type really 

return int(self._translateFromCalibId("detector", md)) 

 

def translate_filter(self, md): 

return self._translateFromCalibId("filter", md) 

 

def translate_calibDate(self, md): 

return self._translateFromCalibId("calibDate", md)