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

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

# 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 LATISS headers""" 

 

__all__ = ("LsstLatissTranslator", ) 

 

import logging 

import re 

 

import astropy.units as u 

from astropy.time import Time 

from astropy.coordinates import EarthLocation 

 

from astro_metadata_translator import cache_translation 

from astro_metadata_translator.translators.helpers import is_non_science 

from .lsst import LsstBaseTranslator 

 

log = logging.getLogger(__name__) 

 

 

# AuxTel is not the same place as LSST 

# These coordinates read from Apple Maps 

AUXTEL_LOCATION = EarthLocation.from_geodetic(-70.747698, -30.244728, 2663.0) 

 

# Date instrument is taking data at telescope 

# Prior to this date many parameters are automatically nulled out 

# since the headers have not historically been reliable 

TSTART = Time("2020-01-01T00:00", format="isot", scale="utc") 

 

# Define the sensor and group name for AuxTel globally so that it can be used 

# in multiple places. There is no raft but for consistency with other LSST 

# cameras we define one. 

_DETECTOR_GROUP_NAME = "RXX" 

_DETECTOR_NAME = "S00" 

 

# Date 068 detector was put in LATISS 

DETECTOR_068_DATE = Time("2019-06-24T00:00", format="isot", scale="utc") 

 

# IMGTYPE header is filled in after this date 

IMGTYPE_OKAY_DATE = Time("2019-11-07T00:00", format="isot", scale="utc") 

 

# OBJECT IMGTYPE really means ENGTEST until this date 

OBJECT_IS_ENGTEST = Time("2020-02-01T00:00", format="isot", scale="utc") 

 

 

def is_non_science_or_lab(self): 

"""Pseudo method to determine whether this is a lab or non-science 

header. 

 

Raises 

------ 

KeyError 

If this is a science observation and on the mountain. 

""" 

# Return without raising if this is not a science observation 

# since the defaults are fine. 

try: 

# This will raise if it is a science observation 

is_non_science(self) 

return 

except KeyError: 

pass 

 

# We are still in the lab, return and use the default 

if not self._is_on_mountain(): 

return 

 

# This is a science observation on the mountain so we should not 

# use defaults 

raise KeyError("Required key is missing and this is a mountain science observation") 

 

 

class LsstLatissTranslator(LsstBaseTranslator): 

"""Metadata translator for LSST LATISS data from AuxTel. 

 

For lab measurements many values are masked out. 

""" 

 

name = "LSST_LATISS" 

"""Name of this translation class""" 

 

supported_instrument = "LATISS" 

"""Supports the LATISS instrument.""" 

 

_const_map = { 

# LATISS is not yet attached to a telescope so many translations 

# are null. 

"instrument": "LATISS", 

"telescope": "LSSTAuxTel", 

"detector_group": _DETECTOR_GROUP_NAME, 

"detector_num": 0, 

"detector_name": _DETECTOR_NAME, # Single sensor 

"science_program": "unknown", 

"relative_humidity": None, 

"pressure": None, 

"temperature": None, 

} 

 

_trivial_map = { 

"observation_id": ("OBSID", dict(default=None, checker=is_non_science)), 

"detector_serial": ["LSST_NUM", "DETSER"], 

"object": ("OBJECT", dict(checker=is_non_science_or_lab, default="UNKNOWN")), 

"boresight_rotation_angle": (["ROTPA", "ROTANGLE"], dict(checker=is_non_science_or_lab, 

default=float("nan"), unit=u.deg)), 

} 

 

DETECTOR_GROUP_NAME = _DETECTOR_GROUP_NAME 

"""Fixed name of detector group.""" 

 

DETECTOR_NAME = _DETECTOR_NAME 

"""Fixed name of single sensor.""" 

 

DETECTOR_MAX = 0 

"""Maximum number of detectors to use when calculating the 

detector_exposure_id.""" 

 

_DEFAULT_LOCATION = AUXTEL_LOCATION 

"""Default telescope location in absence of relevant FITS headers.""" 

 

@classmethod 

def can_translate(cls, header, filename=None): 

"""Indicate whether this translation class can translate the 

supplied header. 

 

Parameters 

---------- 

header : `dict`-like 

Header to convert to standardized form. 

filename : `str`, optional 

Name of file being translated. 

 

Returns 

------- 

can : `bool` 

`True` if the header is recognized by this class. `False` 

otherwise. 

""" 

# INSTRUME keyword might be of two types 

if "INSTRUME" in header: 

instrume = header["INSTRUME"] 

for v in ("LSST_ATISS", "LATISS"): 

if instrume == v: 

return True 

# Calibration files strip important headers at the moment so guess 

if "DETNAME" in header and header["DETNAME"] == "RXX_S00": 

return True 

return False 

 

@classmethod 

def fix_header(cls, header): 

"""Fix an incorrect LATISS header. 

 

Parameters 

---------- 

header : `dict` 

The header to update. Updates are in place. 

 

Returns 

------- 

modified = `bool` 

Returns `True` if the header was updated. 

 

Notes 

----- 

This method does not apply per-obsid corrections. The following 

corrections are applied: 

 

* On June 24th 2019 the detector was changed from ITL-3800C-098 

to ITL-3800C-068. The header is intended to be correct in the 

future. 

* In late 2019 the DATE-OBS and MJD-OBS headers were reporting 

1970 dates. To correct, the DATE/MJD headers are copied in to 

replace them and the -END headers are cleared. 

* Until November 2019 the IMGTYPE was set in the GROUPID header. 

The value is moved to IMGTYPE. 

* SHUTTIME is always forced to be `None`. 

 

Corrections are reported as debug level log messages. 

""" 

modified = False 

 

obsid = header.get("OBSID", "unknown") 

 

# The DATE-OBS / MJD-OBS keys can be 1970 

if header["DATE-OBS"].startswith("1970"): 

# Copy the headers from the DATE and MJD since we have no other 

# choice. 

header["DATE-OBS"] = header["DATE"] 

header["DATE-BEG"] = header["DATE-OBS"] 

header["MJD-OBS"] = header["MJD"] 

header["MJD-BEG"] = header["MJD-OBS"] 

 

# And clear the DATE-END and MJD-END -- the translator will use 

# EXPTIME instead. 

header["DATE-END"] = None 

header["MJD-END"] = None 

 

log.debug("%s: Forcing 1970 dates to '%s'", obsid, header["DATE"]) 

modified = True 

 

# Create a translator since we need the date 

translator = cls(header) 

date = translator.to_datetime_begin() 

if date > DETECTOR_068_DATE: 

header["LSST_NUM"] = "ITL-3800C-068" 

log.debug("%s: Forcing detector serial to %s", obsid, header["LSST_NUM"]) 

modified = True 

 

# Up until a certain date GROUPID was the IMGTYPE 

if date < IMGTYPE_OKAY_DATE: 

groupId = header.get("GROUPID") 

if groupId and not groupId.startswith("test"): 

imgType = header.get("IMGTYPE") 

if not imgType: 

header["IMGTYPE"] = groupId 

header["GROUPID"] = None 

log.debug("%s: Setting IMGTYPE to '%s' from GROUPID", obsid, header["IMGTYPE"]) 

modified = True 

else: 

# Someone could be fixing headers in old data 

# and we do not want GROUPID == IMGTYPE 

if imgType == groupId: 

# Clear the group so we default to original 

header["GROUPID"] = None 

 

# We were using OBJECT for engineering observations early on 

if date < OBJECT_IS_ENGTEST: 

imgType = header.get("IMGTYPE") 

if imgType == "OBJECT": 

header["IMGTYPE"] = "ENGTEST" 

log.debug("%s: Changing OBJECT observation type to %s", 

obsid, header["IMGTYPE"]) 

modified = True 

 

if header.get("SHUTTIME"): 

log.debug("%s: Forcing SHUTTIME header to be None", obsid) 

header["SHUTTIME"] = None 

modified = True 

 

if "OBJECT" not in header: 

# Only patch OBJECT IMGTYPE 

if "IMGTYPE" in header and header["IMGTYPE"] == "OBJECT": 

log.debug("%s: Forcing OBJECT header to exist", obsid) 

header["OBJECT"] = "NOTSET" 

modified = True 

 

return modified 

 

def _is_on_mountain(self): 

date = self.to_datetime_begin() 

if date > TSTART: 

return True 

return False 

 

@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. 

""" 

if detector_num != 0: 

log.warning("Unexpected non-zero detector number for LATISS") 

return exposure_id 

 

@cache_translation 

def to_dark_time(self): 

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

 

# Always compare with exposure time 

# We may revisit this later if there is a cutoff date where we 

# can always trust the header. 

exptime = self.to_exposure_time() 

 

if self.is_key_ok("DARKTIME"): 

darktime = self.quantity_from_card("DARKTIME", u.s) 

if darktime >= exptime: 

return darktime 

reason = "Dark time less than exposure time." 

else: 

reason = "Dark time not defined." 

 

log.warning("%s: %s Setting dark time to the exposure time.", 

self.to_observation_id(), reason) 

return exptime 

 

@cache_translation 

def to_exposure_time(self): 

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

# Some data is missing a value for EXPTIME. 

# Have to be careful we do not have circular logic when trying to 

# guess 

if self.is_key_ok("EXPTIME"): 

return self.quantity_from_card("EXPTIME", u.s) 

 

# A missing or undefined EXPTIME is problematic. Set to -1 

# to indicate that none was found. 

log.warning("%s: Insufficient information to derive exposure time. Setting to -1.0s", 

self.to_observation_id()) 

return -1.0 * u.s 

 

@cache_translation 

def to_observation_type(self): 

"""Determine the observation type. 

 

In the absence of an ``IMGTYPE`` header, assumes lab data is always a 

dark if exposure time is non-zero, else bias. 

 

Returns 

------- 

obstype : `str` 

Observation type. 

""" 

 

# LATISS observation type is documented to appear in OBSTYPE 

# but for historical reasons prefers IMGTYPE. 

# Test the keys in order until we find one that contains a 

# defined value. 

obstype_keys = ["OBSTYPE", "IMGTYPE"] 

 

obstype = None 

for k in obstype_keys: 

if self.is_key_ok(k): 

obstype = self._header[k] 

self._used_these_cards(k) 

obstype = obstype.lower() 

break 

 

if obstype is not None: 

if obstype == "object" and not self._is_on_mountain(): 

# Do not map object to science in lab since most 

# code assume science is on sky with RA/Dec. 

obstype = "labobject" 

elif obstype in ("skyexp", "object"): 

obstype = "science" 

 

return obstype 

 

# In the absence of any observation type information, return 

# unknown unless we think it might be a bias. 

exptime = self.to_exposure_time() 

if exptime == 0.0: 

obstype = "bias" 

else: 

obstype = "unknown" 

log.warning("%s: Unable to determine observation type. Guessing '%s'", 

self.to_observation_id(), obstype) 

return obstype 

 

@cache_translation 

def to_physical_filter(self): 

"""Calculate the physical filter name. 

 

Returns 

------- 

filter : `str` 

Name of filter. Can be a combination of FILTER, FILTER1 and FILTER2 

headers joined by a "+". Returns "NONE" if no filter is declared. 

Uses "EMPTY" if any of the filters indicate an "empty_N" name. 

""" 

# The base class definition is fine 

physical_filter = super().to_physical_filter() 

 

# empty_N maps to EMPTY at the start of a filter concatenation 

if physical_filter.startswith("empty"): 

physical_filter = re.sub(r"^empty_\d+", "EMPTY", physical_filter) 

 

return physical_filter 

 

@cache_translation 

def to_boresight_rotation_coord(self): 

"""Boresight rotation angle. 

 

Only relevant for science observations. 

""" 

if not self.is_science_on_sky(): 

return "unknown" 

 

self._used_these_cards("ROTCOORD") 

return self._header["ROTCOORD"] 

 

@cache_translation 

def to_boresight_airmass(self): 

"""Calculate airmass at boresight at start of observation. 

 

Notes 

----- 

Early data are missing AMSTART header so we fall back to calculating 

it from ELSTART. 

""" 

if not self.is_science_on_sky(): 

return None 

 

# This observation should have AMSTART 

amkey = "AMSTART" 

if self.is_key_ok(amkey): 

self._used_these_cards(amkey) 

return self._header[amkey] 

 

# Instead we need to look at azel 

altaz = self.to_altaz_begin() 

if altaz is not None: 

return altaz.secz.to_value() 

 

log.warning("%s: Unable to determine airmass of a science observation, returning 1.", 

self.to_observation_id()) 

return 1.0