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

# This file is part of obs_base. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://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 <https://www.gnu.org/licenses/>. 

 

import math 

import unittest 

 

import astropy.coordinates 

import astropy.units 

 

import lsst.utils.tests 

import lsst.pex.exceptions 

from lsst.daf.base import DateTime, PropertySet 

from lsst.obs.base import MakeRawVisitInfo 

from lsst.geom import degrees 

 

 

class SimpleMakeRawVisitInfo(MakeRawVisitInfo): 

 

def getDateAvg(self, md, exposureTime): 

"""Return date at the middle of the exposure""" 

dateObs = self.popIsoDate(md, "DATE-OBS") 

return self.offsetDate(dateObs, 0.5*exposureTime) 

 

 

def getMetadata(keyValDict): 

md = PropertySet() 

for key, val in keyValDict.items(): 

md.set(key, val) 

return md 

 

 

class VisitInfoTestCase(lsst.utils.tests.TestCase): 

"""Test lsst.afw.image.VisitInfo, a simple struct-like class""" 

 

def setUp(self): 

self.makeRawVisitInfo = SimpleMakeRawVisitInfo() 

 

def testMakeRawVisitInfo(self): 

"""Test base class functor MakeRawVisitInfo 

 

The base class only sets date and exposureTime fields, 

reads DATE-OBS, TIMESYS and EXPTIME, 

and deletes DATE-OBS and EXPTIME, but not TIMESYS. 

""" 

exposureTime = 6.2 # arbitrary value in seconds 

date = DateTime("2001-01-02T03:04:05.123456789", DateTime.TAI) 

dateNsec = date.nsecs(DateTime.TAI) 

startDate = DateTime(dateNsec - int(exposureTime*1e9/2), DateTime.TAI) 

 

exposureId = 54321 # an arbitrary value 

md = getMetadata({ 

"DATE-OBS": startDate.toString(DateTime.UTC), 

"TIMESYS": "UTC", 

"EXPTIME": exposureTime, 

"EXTRA1": "an abitrary key and value", 

"EXTRA2": 5, 

}) 

length = len(md) 

visitInfo = self.makeRawVisitInfo(md=md, exposureId=exposureId) 

self.assertEqual(visitInfo.getExposureId(), exposureId) 

self.assertEqual(md.nameCount(), length) # No stripping 

self.assertEqual(visitInfo.getExposureTime(), exposureTime) 

self.assertEqual(visitInfo.getDate(), date) 

 

# Test stripping keywords 

visitInfo = SimpleMakeRawVisitInfo(doStripHeader=True)(md=md, exposureId=exposureId) 

self.assertEqual(md.nameCount(), 3) # TIMESYS and two EXTRAn keywords 

 

# try TIMESYS=TAI 

md = getMetadata({ 

"DATE-OBS": startDate.toString(DateTime.TAI), 

"TIMESYS": "TAI", 

"EXPTIME": exposureTime, 

}) 

length = len(md) 

visitInfo = self.makeRawVisitInfo(md=md, exposureId=exposureId) 

self.assertEqual(md.nameCount(), length) 

self.assertEqual(visitInfo.getExposureTime(), exposureTime) 

self.assertEqual(visitInfo.getDate(), date) 

 

# try omitting TIMESYS, which defaults to UTC 

md = getMetadata({ 

"DATE-OBS": startDate.toString(DateTime.UTC), 

"EXPTIME": exposureTime, 

}) 

length = len(md) 

visitInfo = self.makeRawVisitInfo(md=md, exposureId=exposureId) 

self.assertEqual(md.nameCount(), length) 

self.assertEqual(visitInfo.getExposureTime(), exposureTime) 

self.assertEqual(visitInfo.getDate(), date) 

 

# omit DATE-OBS; date should be default-constructed 

md = getMetadata({ 

"EXPTIME": exposureTime, 

}) 

length = len(md) 

visitInfo = self.makeRawVisitInfo(md=md, exposureId=exposureId) 

self.assertEqual(md.nameCount(), length) 

self.assertEqual(visitInfo.getExposureTime(), exposureTime) 

self.assertEqual(visitInfo.getDate(), DateTime()) 

 

# omit EXPTIME; date should be start date, not avg date, and exposureTime should be nan 

md = getMetadata({ 

"DATE-OBS": startDate.toString(DateTime.UTC), 

}) 

length = len(md) 

visitInfo = self.makeRawVisitInfo(md=md, exposureId=exposureId) 

self.assertEqual(md.nameCount(), length) 

self.assertTrue(math.isnan(visitInfo.getExposureTime())) 

self.assertEqual(visitInfo.getDate(), startDate) 

 

def testPopItem(self): 

md = getMetadata({ 

"TIMESYS": "UTC", 

"OTHER": 5, 

}) 

names = set(md.names()) 

 

timesys = self.makeRawVisitInfo.popItem(md, "TIMESYS") 

self.assertEqual(timesys, "UTC") 

self.assertEqual(set(md.names()), names) 

 

defVal = self.makeRawVisitInfo.popItem(md, "BADKEY", default=7) 

self.assertEqual(set(md.names()), names) 

self.assertEqual(defVal, 7) 

missingItem = self.makeRawVisitInfo.popItem(md, "BADKEY") 

self.assertIsNone(missingItem) 

 

# Repeat, with stripping 

timesys = SimpleMakeRawVisitInfo(doStripHeader=True).popItem(md, "TIMESYS") 

self.assertEqual(timesys, "UTC") 

self.assertEqual(set(md.names()), set(["OTHER"])) 

 

defVal = SimpleMakeRawVisitInfo(doStripHeader=True).popItem(md, "BADKEY", default=7) 

self.assertEqual(set(md.names()), set(["OTHER"])) 

self.assertEqual(defVal, 7) 

missingItem = SimpleMakeRawVisitInfo(doStripHeader=True).popItem(md, "BADKEY") 

self.assertIsNone(missingItem) 

 

def testPopFloat(self): 

dataDict = { 

"FLOAT": 5.5, 

"INT": 5, 

"FLOATSTR": "6.1", 

"INTSTR": "6", 

"STR": "FOO", 

} 

length = len(dataDict) 

 

for key, desValue in dataDict.items(): 

if key == "STR": 

continue 

md = getMetadata(dataDict) 

value = self.makeRawVisitInfo.popFloat(md, key) 

self.assertAlmostEqual(value, float(desValue)) 

self.assertEqual(len(md.names()), length) 

 

badFloat = self.makeRawVisitInfo.popFloat(md, "STR") 

self.assertTrue(math.isnan(badFloat)) 

 

missingValue = self.makeRawVisitInfo.popFloat(md, "BADKEY") 

self.assertTrue(math.isnan(missingValue)) 

 

def testPopAngle(self): 

dataDict = { 

"DEG1": 270.5, 

"DEG2": "45:30", 

"DEG3": "-310:12:32", 

"HR_1": -23.5, 

"HR_2": "23:30", 

"HR_3": "-13:15:16.7", 

"STR": "FOO", 

} 

 

for key, desValue in dataDict.items(): 

if key == "STR": 

continue 

elif key.startswith("DEG"): 

units = astropy.units.deg 

else: 

units = astropy.units.h 

 

desAngleDeg = astropy.coordinates.Angle(desValue, unit=units).deg 

md = getMetadata(dataDict) 

angle = self.makeRawVisitInfo.popAngle(md, key, units=units) 

self.assertAnglesAlmostEqual(angle, desAngleDeg*degrees) 

 

badAngle = self.makeRawVisitInfo.popAngle(md, "STR") 

self.assertTrue(math.isnan(badAngle.asDegrees())) 

 

missingAngle = self.makeRawVisitInfo.popAngle(md, "BADKEY") 

self.assertTrue(math.isnan(missingAngle.asDegrees())) 

 

def testPopIsoDate(self): 

for timesys in (None, "UTC", "TAI"): 

dataDict = { 

"DATE1": "2001-02-03T04:05:06.123456789", 

"DATE2": "2001-02-03T04:05:06.123456789Z", # Z should be ignored 

"DATE3": "1980-03-04T01:02:03.999999999", 

"BADISODATE": "51234.354", 

} 

if timesys is not None: 

dataDict["TIMESYS"] = timesys 

length = len(dataDict) 

 

for key, dateStr in dataDict.items(): 

if not key.startswith("DATE"): 

continue 

 

lsstSys = dict( 

UTC=DateTime.UTC, 

TAI=DateTime.TAI, 

).get(timesys, DateTime.UTC) 

 

# lsstDateStr = dateStr with trailing Z if UTC, else no trailing Z, 

# because lsst.daf.base.DateTime is very picky 

lsstDateStr = dateStr 

if lsstSys == DateTime.UTC: 

if not lsstDateStr.endswith("Z"): 

lsstDateStr = lsstDateStr + "Z" 

elif dateStr.endswith("Z"): 

lsstDateStr = lsstDateStr[0:-1] 

desDate = DateTime(lsstDateStr, lsstSys) 

 

md = getMetadata(dataDict) 

date = self.makeRawVisitInfo.popIsoDate(md, key, timesys=timesys) 

self.assertEqual(len(md.names()), length) 

self.assertEqual(date, desDate) 

 

badDate = self.makeRawVisitInfo.popIsoDate(md, "BADISODATE") 

self.assertEqual(badDate, DateTime()) 

 

missingDate = self.makeRawVisitInfo.popIsoDate(md, "BADKEY") 

self.assertEqual(missingDate, DateTime()) 

 

def testPopMjdDate(self): 

for timesys in (None, "UTC", "TAI"): 

 

dataDict = { 

"DATE1": 51943.1705801, 

"DATE2": 44302.0433218, 

"BADMJDDATE": "2001-02-03T04:05:06.123456789", 

} 

if timesys is not None: 

dataDict["TIMESYS"] = timesys 

length = len(dataDict) 

 

for key, mjdDate in dataDict.items(): 

if not key.startswith("DATE"): 

continue 

 

lsstSys = dict( 

UTC=DateTime.UTC, 

TAI=DateTime.TAI, 

).get(timesys, DateTime.UTC) 

 

desDate = DateTime(mjdDate, DateTime.MJD, lsstSys) 

 

md = getMetadata(dataDict) 

date = self.makeRawVisitInfo.popMjdDate(md, key, timesys=timesys) 

self.assertEqual(len(md.names()), length) 

self.assertAlmostEqual(date.get(), desDate.get()) 

 

badDate = self.makeRawVisitInfo.popMjdDate(md, "BADMJDDATE") 

self.assertEqual(badDate, DateTime()) 

 

missingDate = self.makeRawVisitInfo.popMjdDate(md, "BADKEY") 

self.assertEqual(missingDate, DateTime()) 

 

def testEraFromLstAndLongitude(self): 

LST = 90*degrees 

Longitude = 50*degrees 

era = self.makeRawVisitInfo.eraFromLstAndLongitude(LST, Longitude) 

self.assertAnglesAlmostEqual(era, LST-Longitude) 

 

def testEraFromLstAndLongitude_float_vs_Angle_fails(self): 

val1 = 90*degrees 

val2 = 50.0 

with self.assertRaises(TypeError): 

self.makeRawVisitInfo.eraFromLstAndLongitude(val1, val2) 

with self.assertRaises(TypeError): 

self.makeRawVisitInfo.eraFromLstAndLongitude(val2, val1) 

 

def testAltitudeFromZenithDistance(self): 

for zdDeg in (0, 35.6, 89.999, 90.0): 

desAltDeg = 90-zdDeg 

self.assertAnglesAlmostEqual( 

desAltDeg*degrees, 

self.makeRawVisitInfo.altitudeFromZenithDistance(zdDeg*degrees), 

) 

 

def testCentigradeFromKelvin(self): 

for tempK, desTempC in ( # a few values from http://www.convertunits.com/from/kelvin/to/centigrade 

(0, -273.15), 

(301.5, 28.35), 

): 

self.assertAlmostEqual(desTempC, self.makeRawVisitInfo.centigradeFromKelvin(tempK)) 

 

def testPascalFromMmHg(self): 

for mmHg, desPascal in ( # a few values from http://www.convertunits.com/from/mm+Hg/to/pascal 

(1, 133.32239), 

(0.062, 8.26598818), 

): 

self.assertAlmostEqual(desPascal, self.makeRawVisitInfo.pascalFromMmHg(mmHg), places=5) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class MemoryTester(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

333 ↛ 334line 333 didn't jump to line 334, because the condition on line 333 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()