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

1from __future__ import with_statement 

2from __future__ import print_function 

3import os 

4import sys 

5import traceback 

6import unittest 

7import lsst.utils.tests 

8 

9from lsst.sims.catUtils.utils import failedOnFatboy 

10 

11import numpy as np 

12from lsst.sims.utils.CodeUtilities import sims_clean_up 

13# Observation metadata modules 

14from lsst.sims.utils import ObservationMetaData 

15from lsst.sims.catUtils.utils import ObservationMetaDataGenerator 

16# To access opsim sqlite database 

17from lsst.utils import getPackageDir 

18# photometric parameters (exptime lives here for dmag calculation) 

19from lsst.sims.photUtils import PhotometricParameters 

20# SSM catalog modules 

21from lsst.sims.catUtils.baseCatalogModels import SolarSystemObj 

22from lsst.sims.catUtils.mixins import PhotometrySSM, AstrometrySSM, CameraCoords, ObsMetadataBase 

23from lsst.sims.catalogs.definitions import InstanceCatalog 

24# For camera. 

25import lsst.obs.lsst.phosim as obs_lsst_phosim 

26 

27import time 

28 

29 

30def setup_module(module): 

31 lsst.utils.tests.init() 

32 

33 

34def dtime(time_prev): 

35 return (time.time() - time_prev, time.time()) 

36 

37 

38def reassure(): 

39 print('\ntestObsCat failed to connect to fatboy') 

40 print('Sometimes that happens. Do not worry.') 

41 

42# Build sso instance class 

43basic_columns = ['objid', 'expMJD', 'raJ2000', 'decJ2000', 'velRa', 'velDec', 

44 'skyVelocity', 'dist', 'dmagTrailing', 'dmagDetection', 

45 'sedFilename', 'magFilter', 'magSNR', 'visibility', 

46 'seeing', 'bandpass', 'visitExpTime', 'm5'] 

47 

48 

49class ssmCat(InstanceCatalog, PhotometrySSM, AstrometrySSM, ObsMetadataBase, CameraCoords): 

50 catalog_type = __file__ + 'ssm_cat' 

51 

52 column_outputs = basic_columns 

53 cannot_be_null = ['visibility'] 

54 transformations = {'raJ2000': np.degrees, 'decJ2000': np.degrees, 

55 'velRa': np.degrees, 'velDec': np.degrees} 

56 default_formats = {'f': '%.13f'} 

57 

58 

59class ssmCatCamera(ssmCat): 

60 catalog_type = __file__ + 'ssm_cat_camera' 

61 

62 column_outputs = basic_columns + ['chipName'] 

63 camera = obs_lsst_phosim.PhosimMapper().camera 

64 cannot_be_null = ['visibility', 'chipName'] 

65 transformations = {'raJ2000': np.degrees, 'decJ2000': np.degrees, 

66 'velRa': np.degrees, 'velDec': np.degrees} 

67 default_formats = {'f': '%.13f'} 

68 

69###### 

70 

71 

72class createSSMSourceCatalogsTest(unittest.TestCase): 

73 

74 longMessage = True 

75 

76 @classmethod 

77 def tearDownClass(cls): 

78 sims_clean_up() 

79 

80 def test_ssm_catalog_creation(self): 

81 

82 t = time.time() 

83 # Fake opsim data. 

84 database = os.path.join(getPackageDir('SIMS_DATA'), 'OpSimData/opsimblitz1_1133_sqlite.db') 

85 generator = ObservationMetaDataGenerator(database=database, driver='sqlite') 

86 

87 night = 20 

88 query = 'select min(expMJD), max(expMJD) from summary where night=%d' % (night) 

89 res = generator.opsimdb.execute_arbitrary(query) 

90 expMJD_min = res[0][0] 

91 expMJD_max = res[0][1] 

92 

93 obsMetaDataResults = generator.getObservationMetaData(expMJD=(expMJD_min, expMJD_max), 

94 limit=3, boundLength=2.2) 

95 

96 dt, t = dtime(t) 

97 print('To query opsim database: %f seconds' % (dt)) 

98 

99 write_header = True 

100 write_mode = 'w' 

101 

102 try: 

103 ssmObj = SolarSystemObj() 

104 

105 for obsMeta in obsMetaDataResults: 

106 # But moving objects databases are not currently complete for all years. 

107 # Push forward to night=747. 

108 # (note that we need the phosim dictionary as well) 

109 

110 newMJD = 59590.2 # this MJD is artificially chosen to be in the 

111 # time span of the new baseline simulated survey 

112 

113 obs = ObservationMetaData(mjd=newMJD, 

114 pointingRA=obsMeta.pointingRA, 

115 pointingDec=obsMeta.pointingDec, 

116 bandpassName=obsMeta.bandpass, 

117 rotSkyPos=obsMeta.rotSkyPos, 

118 m5=obsMeta.m5[obsMeta.bandpass], 

119 seeing=obsMeta.seeing[obsMeta.bandpass], 

120 boundLength=obsMeta.boundLength, 

121 boundType=obsMeta.boundType) 

122 

123 obs._OpsimMetaData = {'visitExpTime': 30} 

124 

125 mySsmDb = ssmCatCamera(ssmObj, obs_metadata = obs) 

126 photParams = PhotometricParameters(exptime = obs.OpsimMetaData['visitExpTime'], 

127 nexp=1, bandpass=obs.bandpass) 

128 mySsmDb.photParams = photParams 

129 

130 try: 

131 with lsst.utils.tests.getTempFilePath('.txt') as output_cat: 

132 mySsmDb.write_catalog(output_cat, write_header=write_header, write_mode=write_mode) 

133 

134 # verify that we did not write an empty catalog 

135 with open(output_cat, 'r') as input_file: 

136 lines = input_file.readlines() 

137 msg = 'MJD is %.3f' % obs.mjd.TAI 

138 self.assertGreater(len(lines), 1, msg=msg) 

139 except: 

140 # This is because the solar system object 'tables' 

141 # don't actually connect to tables on fatboy; they just 

142 # call methods stored on fatboy. Therefore, the connection 

143 # failure will not be noticed until this part of the test 

144 msg = sys.exc_info()[1].args[0] 

145 if 'DB-Lib error' in msg: 

146 reassure() 

147 continue 

148 else: 

149 raise 

150 

151 write_mode = 'a' 

152 write_header = False 

153 

154 dt, t = dtime(t) 

155 print('To query solar system objects: %f seconds (obs MJD time %f)' % (dt, obs.mjd.TAI)) 

156 

157 except: 

158 trace = traceback.extract_tb(sys.exc_info()[2], limit=20) 

159 msg = sys.exc_info()[1].args[0] 

160 if 'Failed to connect' in msg or failedOnFatboy(trace): 

161 # if the exception was because of a failed connection 

162 # to fatboy, ignore it. 

163 reassure() 

164 

165 pass 

166 else: 

167 raise 

168 

169 

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

171 pass 

172 

173if __name__ == "__main__": 173 ↛ 174line 173 didn't jump to line 174, because the condition on line 173 was never true

174 lsst.utils.tests.init() 

175 unittest.main()