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 

6 

7from lsst.sims.utils import ObservationMetaData 

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

9 GalaxyAgnObj, GalaxyTileCompoundObj, 

10 StarObj) 

11 

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

13 

14_testCompoundCatalogs_is_connected = True 

15try: 

16 _example_db = GalaxyBulgeObj() 

17except: 

18 _testCompoundCatalogs_is_connected = False 

19 

20 

21def setup_module(module): 

22 lsst.utils.tests.init() 

23 

24 

25class BulgeDiskCatalog(InstanceCatalog): 

26 catalog_type = __file__ + 'bulge_disk_catalog' 

27 cannot_be_null = ['sedFilename'] 

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

29 'componentra', 'componentdec', 

30 'magNorm', 'sedFilename', 

31 'majorAxis', 'minorAxis', 

32 'positionAngle', 

33 'halfLightRadius', 

34 'internalExtinctionModel', 

35 'internalAv', 'internalRv'] 

36 

37 

38class AgnCatalog(InstanceCatalog): 

39 catalog_type = __file__ + 'agn_catalog' 

40 cannot_be_null = ['sedFilename'] 

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

42 'componentra', 'componentdec', 

43 'magNorm', 'sedFilename', 

44 'variabilityParameters'] 

45 

46 

47class StarCatalog(InstanceCatalog): 

48 catalog_type = __file__ + 'star_catalog' 

49 cannot_be_null = ['sedFilename'] 

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

51 'glon', 'glat', 'magNorm', 

52 'properMotionRa', 'properMotionDec', 

53 'parallax', 'galacticAv', 'radialVelocity', 

54 'variabilityParameters', 'sedFilename'] 

55 

56 

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

58 

59class CompoundCatalogTest(unittest.TestCase): 

60 

61 def setUp(self): 

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

63 

64 def tearDown(self): 

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

66 shutil.rmtree(self.baseDir, ignore_errors=True) 

67 

68 @unittest.skipIf(not _testCompoundCatalogs_is_connected, 

69 "We are not connected to fatboy") 

70 def testGalaxyCatalog(self): 

71 """ 

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

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

74 using CompoundInstanceCatalog 

75 """ 

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

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

78 

79 if os.path.exists(controlFileName): 

80 os.unlink(controlFileName) 

81 if os.path.exists(testFileName): 

82 os.unlink(testFileName) 

83 

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

85 boundType='circle', boundLength=0.05) 

86 

87 dbBulge = GalaxyBulgeObj() 

88 dbDisk = GalaxyDiskObj() 

89 dbAgn = GalaxyAgnObj() 

90 

91 catBulge = BulgeDiskCatalog(dbBulge, obs_metadata=obs) 

92 catDisk = BulgeDiskCatalog(dbDisk, obs_metadata=obs) 

93 catAgn = AgnCatalog(dbAgn, obs_metadata=obs) 

94 

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

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

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

98 

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

100 [GalaxyDiskObj, GalaxyBulgeObj, GalaxyAgnObj], 

101 obs_metadata=obs, 

102 compoundDBclass=GalaxyTileCompoundObj) 

103 

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

105 

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

107 control = controlFile.readlines() 

108 

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

110 test = testFile.readlines() 

111 

112 for line in control: 

113 self.assertIn(line, test) 

114 

115 for line in test: 

116 self.assertIn(line, control) 

117 

118 @unittest.skipIf(not _testCompoundCatalogs_is_connected, 

119 "We are not connected to fatboy") 

120 def testGalaxyAndStarCatalog(self): 

121 """ 

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

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

124 using CompoundInstanceCatalog 

125 """ 

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

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

128 

129 if os.path.exists(controlFileName): 

130 os.unlink(controlFileName) 

131 if os.path.exists(testFileName): 

132 os.unlink(testFileName) 

133 

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

135 boundType='circle', boundLength=0.05) 

136 

137 dbBulge = GalaxyBulgeObj() 

138 dbDisk = GalaxyDiskObj() 

139 dbAgn = GalaxyAgnObj() 

140 dbStar = StarObj() 

141 

142 catBulge = BulgeDiskCatalog(dbBulge, obs_metadata=obs) 

143 catDisk = BulgeDiskCatalog(dbDisk, obs_metadata=obs) 

144 catAgn = AgnCatalog(dbAgn, obs_metadata=obs) 

145 catStar = StarCatalog(dbStar, obs_metadata=obs) 

146 

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

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

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

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

151 

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

153 [GalaxyBulgeObj, GalaxyDiskObj, StarObj, GalaxyAgnObj], 

154 obs_metadata=obs, 

155 compoundDBclass=GalaxyTileCompoundObj) 

156 

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

158 

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

160 control = controlFile.readlines() 

161 

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

163 test = testFile.readlines() 

164 

165 for line in control: 

166 self.assertIn(line, test) 

167 

168 for line in test: 

169 self.assertIn(line, control) 

170 

171 

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

173 pass 

174 

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

176 lsst.utils.tests.init() 

177 unittest.main()