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 

2import numpy as np 

3import os 

4import unittest 

5import tempfile 

6import shutil 

7import lsst.utils.tests 

8from lsst.utils import getPackageDir 

9import lsst.afw.image as afwImage 

10import lsst.geom as LsstGeom 

11from lsst.sims.utils.CodeUtilities import sims_clean_up 

12from lsst.sims.utils import ObservationMetaData, arcsecFromRadians 

13from lsst.sims.utils import haversine 

14from lsst.sims.catalogs.db import fileDBObject 

15from lsst.sims.GalSimInterface import GalSimStars, SNRdocumentPSF 

16from lsst.sims.GalSimInterface import GalSimCameraWrapper 

17from lsst.sims.coordUtils import _raDecFromPixelCoords 

18 

19#from lsst.sims.coordUtils.utils import ReturnCamera 

20 

21from testUtils import create_text_catalog 

22 

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

24 

25 

26def setup_module(module): 

27 lsst.utils.tests.init() 

28 

29 

30class outputWcsFileDBObj(fileDBObject): 

31 idColKey = 'test_id' 

32 objectTypeId = 88 

33 tableid = 'test' 

34 raColName = 'ra' 

35 decColName = 'dec' 

36 # sedFilename 

37 

38 columns = [('raJ2000', 'ra*PI()/180.0', np.float), 

39 ('decJ2000', 'dec*PI()/180.0', np.float)] 

40 

41 

42class outputWcsCat(GalSimStars): 

43 bandpassNames = ['u'] 

44 

45 default_columns = GalSimStars.default_columns 

46 

47 default_columns += [('sedFilename', 'sed_flat.txt', (str, 12)), 

48 ('properMotionRa', 0.0, np.float), 

49 ('properMotionDec', 0.0, np.float), 

50 ('radialVelocity', 0.0, np.float), 

51 ('parallax', 0.0, np.float), 

52 ('magNorm', 14.0, np.float)] 

53 

54@unittest.skip('ReturnCamera deprecated - need replacement') 

55class GalSimOutputWcsTest(unittest.TestCase): 

56 

57 @classmethod 

58 def tearDownClass(cls): 

59 sims_clean_up() 

60 

61 def testOutputWcsOfImage(self): 

62 """ 

63 Test that, when GalSim generates an image, in encodes the WCS in a 

64 way afw can read. This is done by creating an image,then reading 

65 it back in, getting its WCS, and comparing the pixel-to-sky conversion 

66 both for the read WCS and the original afw.cameraGeom.detector. 

67 Raise an exception if the median difference between the two is 

68 greater than 0.01 arcseconds. 

69 """ 

70 scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testOutputWcsOfImage-') 

71 catName = os.path.join(scratchDir, 'outputWcs_test_Catalog.dat') 

72 imageRoot = os.path.join(scratchDir, 'outputWcs_test_Image') 

73 dbFileName = os.path.join(scratchDir, 'outputWcs_test_InputCatalog.dat') 

74 

75 baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData') 

76 camera = ReturnCamera(baseDir) 

77 

78 detector = camera[0] 

79 detName = detector.getName() 

80 imageName = '%s_%s_u.fits' % (imageRoot, detName) 

81 

82 nSamples = 3 

83 rng = np.random.RandomState(42) 

84 pointingRaList = rng.random_sample(nSamples)*360.0 

85 pointingDecList = rng.random_sample(nSamples)*180.0 - 90.0 

86 rotSkyPosList = rng.random_sample(nSamples)*360.0 

87 

88 for raPointing, decPointing, rotSkyPos in \ 

89 zip(pointingRaList, pointingDecList, rotSkyPosList): 

90 

91 obs = ObservationMetaData(pointingRA = raPointing, 

92 pointingDec = decPointing, 

93 boundType = 'circle', 

94 boundLength = 4.0, 

95 rotSkyPos = rotSkyPos, 

96 mjd = 49250.0) 

97 

98 fwhm = 0.7 

99 create_text_catalog(obs, dbFileName, np.array([3.0]), 

100 np.array([1.0])) 

101 

102 db = outputWcsFileDBObj(dbFileName, runtable='test') 

103 

104 cat = outputWcsCat(db, obs_metadata=obs) 

105 cat.camera_wrapper = GalSimCameraWrapper(camera) 

106 

107 psf = SNRdocumentPSF(fwhm=fwhm) 

108 cat.setPSF(psf) 

109 

110 cat.write_catalog(catName) 

111 cat.write_images(nameRoot=imageRoot) 

112 

113 # 20 March 2017 

114 # the 'try' block is how it worked in SWIG; 

115 # the 'except' block is how it works in pybind11 

116 try: 

117 exposure = afwImage.ExposureD_readFits(imageName) 

118 except AttributeError: 

119 exposure = afwImage.ExposureD.readFits(imageName) 

120 

121 wcs = exposure.getWcs() 

122 

123 xxTestList = [] 

124 yyTestList = [] 

125 

126 raImage = [] 

127 decImage = [] 

128 

129 for xx in np.arange(0.0, 4001.0, 100.0): 

130 for yy in np.arange(0.0, 4001.0, 100.0): 

131 xxTestList.append(xx) 

132 yyTestList.append(yy) 

133 

134 pt = LsstGeom.Point2D(xx, yy) 

135 skyPt = wcs.pixelToSky(pt).getPosition(LsstGeom.degrees) 

136 raImage.append(skyPt.getX()) 

137 decImage.append(skyPt.getY()) 

138 

139 xxTestList = np.array(xxTestList) 

140 yyTestList = np.array(yyTestList) 

141 

142 raImage = np.radians(np.array(raImage)) 

143 decImage = np.radians(np.array(decImage)) 

144 

145 raControl, \ 

146 decControl = _raDecFromPixelCoords(xxTestList, yyTestList, 

147 [detector.getName()]*len(xxTestList), 

148 camera=camera, obs_metadata=obs, 

149 epoch=2000.0) 

150 

151 errorList = arcsecFromRadians(haversine(raControl, decControl, 

152 raImage, decImage)) 

153 

154 medianError = np.median(errorList) 

155 msg = 'medianError was %e' % medianError 

156 self.assertLess(medianError, 0.01, msg=msg) 

157 

158 if os.path.exists(catName): 

159 os.unlink(catName) 

160 if os.path.exists(dbFileName): 

161 os.unlink(dbFileName) 

162 if os.path.exists(imageName): 

163 os.unlink(imageName) 

164 

165 if os.path.exists(scratchDir): 

166 shutil.rmtree(scratchDir) 

167 

168 

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

170 pass 

171 

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

173 lsst.utils.tests.init() 

174 unittest.main()