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

# This file is part of astro_metadata_translator. 

# 

# 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__ = ("read_test_file", "MetadataAssertHelper") 

 

import os 

import yaml 

import pickle 

from collections import OrderedDict 

 

import astropy.units as u 

 

from astro_metadata_translator import ObservationInfo 

 

# PropertyList is optional 

try: 

import lsst.daf.base as dafBase 

except ImportError: 

dafBase = None 

 

 

TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

 

 

# Define a YAML loader for lsst.daf.base.PropertySet serializations that 

# we can use if daf_base is not available. 

def pl_constructor(loader, node): 

"""Construct an OrderedDict from a YAML file containing a PropertyList.""" 

pl = OrderedDict() 

yield pl 

state = loader.construct_sequence(node, deep=True) 

for key, dtype, value, comment in state: 

if dtype == "Double": 

pl[key] = float(value) 

elif dtype == "Int": 

pl[key] = int(value) 

elif dtype == "Bool": 

pl[key] = True if value == "true" else False 

else: 

pl[key] = value 

 

 

61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never trueif dafBase is None: 

yaml.add_constructor("lsst.daf.base.PropertyList", pl_constructor) 

 

 

def read_test_file(filename): 

"""Read the named test file relative to the location of this helper 

 

Parameters 

---------- 

filename : `str` 

Name of file in the data directory. 

 

Returns 

------- 

header : `dict`-like 

Header read from file. 

""" 

with open(os.path.join(TESTDIR, "data", filename)) as fd: 

header = yaml.load(fd) 

return header 

 

 

class MetadataAssertHelper: 

"""Class with helpful asserts that can be used for testing metadata 

translations. 

""" 

 

def assertCoordinatesConsistent(self, obsinfo, max_sep=1.0, amdelta=0.01): # noqa: N802 

"""Check that SkyCoord, AltAz, and airmass are self consistent. 

 

Parameters 

---------- 

obsinfo : `ObservationInfo` 

Object to check. 

max_sep : `float`, optional 

Maximum separation between AltAz derived from RA/Dec headers 

and that found in the AltAz headers. 

amdelta : `float`, optional 

Max difference between header airmass and derived airmass. 

 

Raises 

------ 

AssertionError 

Inconsistencies found. 

""" 

self.assertIsNotNone(obsinfo.tracking_radec) 

self.assertIsNotNone(obsinfo.altaz_begin) 

 

# Is airmass from header close to airmass from AltAz headers? 

# In most cases there is uncertainty over whether the elevation 

# and airmass in the header are for the start, end, or middle 

# of the observation. Sometimes the AltAz is from the start 

# but the airmass is from the middle so accuracy is not certain. 

self.assertAlmostEqual(obsinfo.altaz_begin.secz.to_value(), obsinfo.boresight_airmass, delta=amdelta) 

 

# Is AltAz from headers close to AltAz from RA/Dec headers? 

sep = obsinfo.altaz_begin.separation(obsinfo.tracking_radec.altaz) 

self.assertLess(sep.to_value(unit="arcmin"), max_sep) 

 

def assertObservationInfoFromYaml(self, file, check_wcs=True, wcs_params=None, **kwargs): # noqa: N802 

"""Check contents of an ObservationInfo. 

 

Parameters 

---------- 

file : `str` 

Path to YAML file representing the header. 

check_wcs : `bool`, optional 

Check the consistency of the RA/Dec and AltAz values. 

wcs_params : `dict`, optional 

Parameters to pass to `assertCoordinatesConsistent`. 

kwargs : `dict` 

Keys matching `ObservationInfo` properties with values 

to be tested. 

 

Raises 

------ 

AssertionError 

A value in the ObservationInfo derived from the file is 

inconsistent. 

""" 

header = read_test_file(file) 

self.assertObservationInfo(header, check_wcs=check_wcs, wcs_params=wcs_params, **kwargs) 

 

def assertObservationInfo(self, header, check_wcs=True, wcs_params=None, **kwargs): # noqa: N802 

"""Check contents of an ObservationInfo. 

 

Parameters 

---------- 

header : `dict`-like 

Header to be checked. 

check_wcs : `bool`, optional 

Check the consistency of the RA/Dec and AltAz values. 

wcs_params : `dict`, optional 

Parameters to pass to `assertCoordinatesConsistent`. 

kwargs : `dict` 

Keys matching `ObservationInfo` properties with values 

to be tested. 

 

Raises 

------ 

AssertionError 

A value in the ObservationInfo derived from the file is 

inconsistent. 

""" 

obsinfo = ObservationInfo(header) 

 

# Check that we can pickle and get back the same properties 

newinfo = pickle.loads(pickle.dumps(obsinfo)) 

self.assertEqual(obsinfo, newinfo) 

 

# Check the properties 

for property, expected in kwargs.items(): 

calculated = getattr(obsinfo, property) 

msg = f"Comparing property {property}" 

print(f"{property}: {calculated} vs {expected}") 

if isinstance(expected, u.Quantity): 

calculated = calculated.to_value(unit=expected.unit) 

expected = expected.to_value() 

self.assertAlmostEqual(calculated, expected, msg=msg) 

elif isinstance(calculated, u.Quantity): 

# Only happens if the test is not a quantity when it should be 

self.fail(f"Expected {expected!r} for property {property} but got Quantity '{calculated}'") 

elif isinstance(expected, float): 

self.assertAlmostEqual(calculated, expected, msg=msg) 

else: 

self.assertEqual(calculated, expected, msg=msg) 

 

# Check the WCS consistency 

if check_wcs: 

if wcs_params is None: 

wcs_params = {} 

self.assertCoordinatesConsistent(obsinfo, **wcs_params)