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

import unittest 

import os 

import copy 

import numpy as np 

import lsst.utils.tests 

from lsst.utils import getPackageDir 

from lsst.sims.alertsim.opsim_utils import _convert_obs_to_history 

from lsst.sims.catUtils.utils import ObservationMetaDataGenerator 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class ObsHistoryTestCase(unittest.TestCase): 

 

longMessage = True 

 

def test_history(self): 

""" 

Test that _convert_obs_to_history really does sort the 

ObservationMetaData appropriately. 

""" 

 

opsimdb = os.path.join(getPackageDir("sims_data"), "OpSimData", 

"opsimblitz1_1133_sqlite.db") 

 

gen = ObservationMetaDataGenerator(opsimdb) 

obs_list = gen.getObservationMetaData(night=(0,10)) 

obs_control = copy.deepcopy(obs_list) 

control_fieldid = np.array([obs.OpsimMetaData['fieldID'] for obs in obs_control]) 

control_mjd = np.array([obs.mjd.TAI for obs in obs_control]) 

self.assertGreater(len(obs_list), 1) 

 

history = _convert_obs_to_history(obs_list, gen) 

 

for ix, entry in enumerate(history): 

 

# make sure that the leading ObservationMetaData in each 

# row of history is at a later date than the leading 

# ObservationMetaData of the preceding entry 

if ix > 0: 

msg = 'offending index is %d' % ix 

self.assertGreater(entry[0].mjd.TAI, history[ix-1][0].mjd.TAI, msg=msg) 

 

# Make sure that the ObservationMetaData in each entry are in 

# reverse chronological order and unique 

obshistid_list = [] 

for iy, past_obs in enumerate(entry): 

self.assertNotIn(past_obs.OpsimMetaData['obsHistID'], obshistid_list) 

obshistid_list.append(past_obs.OpsimMetaData['obsHistID']) 

 

self.assertEqual(past_obs.OpsimMetaData['fieldID'], 

entry[0].OpsimMetaData['fieldID']) 

if iy > 0: 

msg = 'offending index is %d' % iy 

self.assertLess(past_obs.mjd.TAI, entry[iy-1].mjd.TAI, msg=msg) 

 

# Make sure that every ObservationMetaData that should be in this entry 

# in the history is in this entry in the history 

obshistid_list = [obs.OpsimMetaData['obsHistID'] for obs in entry] 

should_be_in = np.where(np.logical_and(control_mjd <= entry[0].mjd.TAI, 

control_fieldid == entry[0].OpsimMetaData['fieldID']))[0] 

 

for shld_dex in should_be_in: 

self.assertIn(obs_control[shld_dex].OpsimMetaData['obsHistID'], obshistid_list) 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()