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 builtins import zip 

2from builtins import str 

3import unittest 

4import os 

5import gzip 

6import re 

7from astropy.io import fits 

8import lsst.utils 

9import lsst.utils.tests 

10from lsst.sims.catUtils.readGalfast import readGalfast 

11from lsst.utils import getPackageDir 

12 

13 

14def setup_module(module): 

15 lsst.utils.tests.init() 

16 

17 

18class TestReadGalfast(unittest.TestCase): 

19 

20 @classmethod 

21 def setUpClass(cls): 

22 

23 # Set up Test Spectra Directory 

24 cls.testSpecDir = os.path.join(getPackageDir('sims_photUtils'), 

25 'tests/cartoonSedTestData/starSed/') 

26 cls.testKDir = str(cls.testSpecDir + '/kurucz/') 

27 cls.testMLTDir = str(cls.testSpecDir + '/mlt/') 

28 cls.testWDDir = str(cls.testSpecDir + '/wDs/') 

29 

30 @classmethod 

31 def tearDownClass(cls): 

32 del cls.testSpecDir 

33 del cls.testKDir 

34 del cls.testMLTDir 

35 del cls.testWDDir 

36 

37 def testParseGalfast(self): 

38 

39 """Test Read-in of Galfast Header""" 

40 

41 testRG = readGalfast() 

42 # First test that exception is raised when an invalid header label is used 

43 testInvalidHeader = 'lb[2] header' 

44 self.assertRaises(RuntimeError, testRG.parseGalfast, testInvalidHeader) 

45 

46 # Next test that '#' is ignored 

47 testSymbolHeader = '# lb[2]' 

48 testSymbolDict = testRG.parseGalfast(testSymbolHeader) 

49 self.assertEqual(testSymbolDict, {'l': 0, 'b': 1}) 

50 

51 # Test that new line marker at end of line is ignored 

52 testNewLineHeader = '# lb[2] \n ' 

53 testNewLineDict = testRG.parseGalfast(testNewLineHeader) 

54 self.assertEqual(testNewLineDict, {'l': 0, 'b': 1}) 

55 

56 # Next test that extra spaces are ignored 

57 testSpaceHeader = 'lb[2] radec[2]' 

58 testSpaceDict = testRG.parseGalfast(testSpaceHeader) 

59 self.assertEqual(testSpaceDict, {'l': 0, 'b': 1, 'ra': 2, 'dec': 3}) 

60 

61 # Test that every header value gets put in the right place using different order than usually come out 

62 testFullHeader = '# lb[2] XYZ[3] radec[2] absSDSSr{alias=M1;alias=absmag;band=SDSSr;} ' +\ 

63 'DM comp FeH vcyl[3] pmlb[3] pmradec[3] Am AmInf SDSSugriz[5]{class=magnitude;' +\ 

64 'fieldNames=0:SDSSu,1:SDSSg,2:SDSSr,3:SDSSi,4:SDSSz;} ' +\ 

65 'SDSSugrizPhotoFlags{class=flags;}' 

66 actualFullHeaderDict = {'l': 0, 'b': 1, 'X': 2, 'Y': 3, 'Z': 4, 

67 'ra': 5, 'dec': 6, 'absSDSSr': 7, 'DM': 8, 

68 'comp': 9, 'FeH': 10, 'Vr': 11, 'Vphi': 12, 

69 'Vz': 13, 'pml': 14, 'pmb': 15, 'vRadlb': 16, 

70 'pmra': 17, 'pmdec': 18, 'vRad': 19, 'Am': 20, 

71 'AmInf': 21, 'SDSSu': 22, 'SDSSg': 23, 

72 'SDSSr': 24, 'SDSSi': 25, 'SDSSz': 26, 

73 'SDSSPhotoFlags': 27} 

74 testFullHeaderDict = testRG.parseGalfast(testFullHeader) 

75 self.assertEqual(testFullHeaderDict, actualFullHeaderDict) 

76 

77 def testConvDMtoKpc(self): 

78 

79 """Make sure Distance Modulus get correctly converted to distance in kpc""" 

80 

81 testRG = readGalfast() 

82 dm = 10. 

83 actualKpcDistance = 1. 

84 testKpcDistance = testRG.convDMtoKpc(dm) 

85 self.assertEqual(testKpcDistance, actualKpcDistance) 

86 

87 def testLoadGalfast(self): 

88 

89 """Make sure all desired input file types are correctly processed and return correct output files""" 

90 

91 testRG = readGalfast() 

92 # First test that it makes sure file exists 

93 self.assertRaises(RuntimeError, testRG.loadGalfast, ['notarealfile.txt'], ['noOutput.txt']) 

94 

95 # Next test that if an unknown file format is entered it exits 

96 self.assertRaises(RuntimeError, testRG.loadGalfast, ['notarealfile.dat'], ['noOutput.txt']) 

97 

98 # Write example files and then load in and make sure example output files are created 

99 with lsst.utils.tests.getTempFilePath('.in.txt') as inTxtName: 

100 with lsst.utils.tests.getTempFilePath('.in.txt.gz') as inGzipName: 

101 with lsst.utils.tests.getTempFilePath('.in.fits') as inFitsName: 

102 

103 # First write .txt 

104 with open(inTxtName, 'w') as exampleIn: 

105 inHeader = '# lb[2] radec[2] XYZ[3] absSDSSr{alias=M1;alias=absmag;band=SDSSr;} DM comp FeH ' +\ 

106 'vcyl[3] pmlb[3] pmradec[3] Am AmInf SDSSugriz[5]{class=magnitude;fieldNames=0:SDSSu,' +\ 

107 '1:SDSSg,2:SDSSr,3:SDSSi,4:SDSSz;} SDSSugrizPhotoFlags{class=flags;} \n' 

108 testComment = '# Comment\n' 

109 inData = ' 1.79371816 -89.02816704 11.92064832 -27.62775082 7.15 0.22 ' +\ 

110 '-421.87 8.126 4.366 0 -0.095 13.7 -183.4 -6.2 -20.58 -12.60 ' +\ 

111 '13.02 21.34 -11.26 13.02 0.037 0.037 14.350 12.949 12.529 12.381 12.358 0\n' 

112 exampleIn.write(inHeader) 

113 exampleIn.write(testComment) 

114 exampleIn.write(inData) 

115 

116 # Then gzipped. Also testing multiple lines in catalog. 

117 with gzip.open(inGzipName, 'wt') as exampleGzipIn: 

118 exampleGzipIn.write(inHeader) 

119 exampleGzipIn.write(testComment) 

120 exampleGzipIn.write(inData) 

121 exampleGzipIn.write(inData) 

122 

123 # Finally a fits file, but first make sure to remove pre-existing file 

124 columnNames = ['lb', 'XYZ', 'radec', 'absSDSSr', 'DM', 'comp', 'FeH', 'vcyl', 'pmlb', 'pmradec', 

125 'Am', 'AmInf', 'SDSSugriz', 'SDSSugrizPhotoFlags'] 

126 columnArrays = [[[1.79371816, -89.02816704]], [[7.15, 0.22, -421.87]], 

127 [[11.92064832, -27.62775082]], [[8.126]], [[4.366]], [[0]], [[-0.095]], 

128 [[13.7, -183.4, -6.2]], [[-20.58, -12.60, 13.02]], [[21.34, -11.26, 13.02]], 

129 [[0.037]], [[0.037]], [[14.350, 12.949, 12.529, 12.381, 12.358]], [[0]]] 

130 columnFormats = ['2E', '3E', '2E', 'E', 'E', 'E', 'E', '3E', '3E', '3E', 'E', 'E', '5E', 'E'] 

131 cols = fits.ColDefs([fits.Column(name = columnNames[0], format = columnFormats[0], 

132 array = columnArrays[0])]) 

133 for colName, colArray, colFormat in zip(columnNames[1:], columnArrays[1:], columnFormats[1:]): 

134 cols.add_col(fits.Column(name = colName, format = colFormat, array = colArray)) 

135 exampleTable = fits.BinTableHDU.from_columns(cols) 

136 

137 exampleTable.writeto(inFitsName) 

138 

139 with lsst.utils.tests.getTempFilePath('.fits.txt') as outFitsName: 

140 with lsst.utils.tests.getTempFilePath('.txt') as outTxtName: 

141 with lsst.utils.tests.getTempFilePath('.gz') as outGzipName: 

142 testRG.loadGalfast([inTxtName, inGzipName, inFitsName], 

143 [outTxtName, outGzipName, outFitsName], 

144 kuruczPath = self.testKDir, 

145 mltPath = self.testMLTDir, 

146 wdPath = self.testWDDir) 

147 self.assertTrue(os.path.isfile(outTxtName), 

148 msg='file .txt output file was not created') 

149 self.assertTrue(os.path.isfile(outGzipName), 

150 msg='file gzip output file was not created') 

151 self.assertTrue(os.path.isfile(outFitsName), 

152 msg='file fit.txt output file was not created') 

153 

154 

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

156 pass 

157 

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

159 lsst.utils.tests.init() 

160 unittest.main()