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

1import unittest 

2import os 

3import shutil 

4import tempfile 

5import lsst.utils.tests 

6from lsst.utils import getPackageDir 

7 

8from lsst.sims.utils import ObservationMetaData 

9from lsst.sims.catUtils.baseCatalogModels import (GalaxyBulgeObj, GalaxyDiskObj, 

10 GalaxyAgnObj, GalaxyTileCompoundObj, 

11 StarObj) 

12 

13from lsst.sims.catalogs.definitions import InstanceCatalog, CompoundInstanceCatalog 

14 

15_testCompoundCatalogs_is_connected = True 

16try: 

17 _example_db = GalaxyBulgeObj() 

18except: 

19 _testCompoundCatalogs_is_connected = False 

20 

21 

22def setup_module(module): 

23 lsst.utils.tests.init() 

24 

25 

26class BulgeDiskCatalog(InstanceCatalog): 

27 catalog_type = __file__ + 'bulge_disk_catalog' 

28 cannot_be_null = ['sedFilename'] 

29 column_outputs = ['galtileid', 'raJ2000', 'decJ2000', 

30 'componentra', 'componentdec', 

31 'magNorm', 'sedFilename', 

32 'majorAxis', 'minorAxis', 

33 'positionAngle', 

34 'halfLightRadius', 

35 'internalExtinctionModel', 

36 'internalAv', 'internalRv'] 

37 

38 

39class AgnCatalog(InstanceCatalog): 

40 catalog_type = __file__ + 'agn_catalog' 

41 cannot_be_null = ['sedFilename'] 

42 column_outputs = ['galtileid', 'raJ2000', 'decJ2000', 

43 'componentra', 'componentdec', 

44 'magNorm', 'sedFilename', 

45 'variabilityParameters'] 

46 

47 

48class StarCatalog(InstanceCatalog): 

49 catalog_type = __file__ + 'star_catalog' 

50 cannot_be_null = ['sedFilename'] 

51 column_outputs = ['id', 'raJ2000', 'decJ2000', 

52 'glon', 'glat', 'magNorm', 

53 'properMotionRa', 'properMotionDec', 

54 'parallax', 'galacticAv', 'radialVelocity', 

55 'variabilityParameters', 'sedFilename'] 

56 

57 

58ROOT = os.path.abspath(os.path.dirname(__file__)) 

59 

60class CompoundCatalogTest(unittest.TestCase): 

61 

62 def setUp(self): 

63 self.baseDir = tempfile.mkdtemp(dir=ROOT, prefix='compoundCatalogTest-') 

64 

65 def tearDown(self): 

66 if os.path.exists(self.baseDir): 

67 shutil.rmtree(self.baseDir) 

68 

69 @unittest.skipIf(not _testCompoundCatalogs_is_connected, 

70 "We are not connected to fatboy") 

71 def testGalaxyCatalog(self): 

72 """ 

73 Test GalaxyTileCompoundObj by creating a catalog of galaxy bulges, disks, 

74 and agns using both the 'old fashioned way' (one catalog at a time), and 

75 using CompoundInstanceCatalog 

76 """ 

77 controlFileName = os.path.join(self.baseDir, 'gal_compound_control.txt') 

78 testFileName = os.path.join(self.baseDir, 'gal_compound_test.txt') 

79 

80 if os.path.exists(controlFileName): 

81 os.unlink(controlFileName) 

82 if os.path.exists(testFileName): 

83 os.unlink(testFileName) 

84 

85 obs = ObservationMetaData(pointingRA=25.0, pointingDec=-45.0, 

86 boundType='circle', boundLength=0.05) 

87 

88 dbBulge = GalaxyBulgeObj() 

89 dbDisk = GalaxyDiskObj() 

90 dbAgn = GalaxyAgnObj() 

91 

92 catBulge = BulgeDiskCatalog(dbBulge, obs_metadata=obs) 

93 catDisk = BulgeDiskCatalog(dbDisk, obs_metadata=obs) 

94 catAgn = AgnCatalog(dbAgn, obs_metadata=obs) 

95 

96 catBulge.write_catalog(controlFileName, write_header=False, chunk_size=10000) 

97 catDisk.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000) 

98 catAgn.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000) 

99 

100 totalCat = CompoundInstanceCatalog([BulgeDiskCatalog, BulgeDiskCatalog, AgnCatalog], 

101 [GalaxyDiskObj, GalaxyBulgeObj, GalaxyAgnObj], 

102 obs_metadata=obs, 

103 compoundDBclass=GalaxyTileCompoundObj) 

104 

105 totalCat.write_catalog(testFileName, write_header=False, chunk_size=10000) 

106 

107 with open(controlFileName, 'r') as controlFile: 

108 control = controlFile.readlines() 

109 

110 with open(testFileName, 'r') as testFile: 

111 test = testFile.readlines() 

112 

113 for line in control: 

114 self.assertIn(line, test) 

115 

116 for line in test: 

117 self.assertIn(line, control) 

118 

119 @unittest.skipIf(not _testCompoundCatalogs_is_connected, 

120 "We are not connected to fatboy") 

121 def testGalaxyAndStarCatalog(self): 

122 """ 

123 Test GalaxyTileCompoundObj by creating a catalog of galaxy bulges, disks, 

124 agns, and stars using both the 'old fashioned way' (one catalog at a time), and 

125 using CompoundInstanceCatalog 

126 """ 

127 controlFileName = os.path.join(self.baseDir, 'galStar_compound_control.txt') 

128 testFileName = os.path.join(self.baseDir, 'galStar_compound_test.txt') 

129 

130 if os.path.exists(controlFileName): 

131 os.unlink(controlFileName) 

132 if os.path.exists(testFileName): 

133 os.unlink(testFileName) 

134 

135 obs = ObservationMetaData(pointingRA=25.0, pointingDec=-45.0, 

136 boundType='circle', boundLength=0.05) 

137 

138 dbBulge = GalaxyBulgeObj() 

139 dbDisk = GalaxyDiskObj() 

140 dbAgn = GalaxyAgnObj() 

141 dbStar = StarObj() 

142 

143 catBulge = BulgeDiskCatalog(dbBulge, obs_metadata=obs) 

144 catDisk = BulgeDiskCatalog(dbDisk, obs_metadata=obs) 

145 catAgn = AgnCatalog(dbAgn, obs_metadata=obs) 

146 catStar = StarCatalog(dbStar, obs_metadata=obs) 

147 

148 catBulge.write_catalog(controlFileName, write_header=False, chunk_size=10000) 

149 catDisk.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000) 

150 catAgn.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000) 

151 catStar.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000) 

152 

153 totalCat = CompoundInstanceCatalog([BulgeDiskCatalog, BulgeDiskCatalog, StarCatalog, AgnCatalog], 

154 [GalaxyBulgeObj, GalaxyDiskObj, StarObj, GalaxyAgnObj], 

155 obs_metadata=obs, 

156 compoundDBclass=GalaxyTileCompoundObj) 

157 

158 totalCat.write_catalog(testFileName, write_header=False, chunk_size=10000) 

159 

160 with open(controlFileName, 'r') as controlFile: 

161 control = controlFile.readlines() 

162 

163 with open(testFileName, 'r') as testFile: 

164 test = testFile.readlines() 

165 

166 for line in control: 

167 self.assertIn(line, test) 

168 

169 for line in test: 

170 self.assertIn(line, control) 

171 

172 

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

174 pass 

175 

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

177 lsst.utils.tests.init() 

178 unittest.main()