Coverage for tests/testOutputWcs.py : 34%

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
19#from lsst.sims.coordUtils.utils import ReturnCamera
21from testUtils import create_text_catalog
23ROOT = os.path.abspath(os.path.dirname(__file__))
26def setup_module(module):
27 lsst.utils.tests.init()
30class outputWcsFileDBObj(fileDBObject):
31 idColKey = 'test_id'
32 objectTypeId = 88
33 tableid = 'test'
34 raColName = 'ra'
35 decColName = 'dec'
36 # sedFilename
38 columns = [('raJ2000', 'ra*PI()/180.0', np.float),
39 ('decJ2000', 'dec*PI()/180.0', np.float)]
42class outputWcsCat(GalSimStars):
43 bandpassNames = ['u']
45 default_columns = GalSimStars.default_columns
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)]
54@unittest.skip('ReturnCamera deprecated - need replacement')
55class GalSimOutputWcsTest(unittest.TestCase):
57 @classmethod
58 def tearDownClass(cls):
59 sims_clean_up()
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')
75 baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
76 camera = ReturnCamera(baseDir)
78 detector = camera[0]
79 detName = detector.getName()
80 imageName = '%s_%s_u.fits' % (imageRoot, detName)
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
88 for raPointing, decPointing, rotSkyPos in \
89 zip(pointingRaList, pointingDecList, rotSkyPosList):
91 obs = ObservationMetaData(pointingRA = raPointing,
92 pointingDec = decPointing,
93 boundType = 'circle',
94 boundLength = 4.0,
95 rotSkyPos = rotSkyPos,
96 mjd = 49250.0)
98 fwhm = 0.7
99 create_text_catalog(obs, dbFileName, np.array([3.0]),
100 np.array([1.0]))
102 db = outputWcsFileDBObj(dbFileName, runtable='test')
104 cat = outputWcsCat(db, obs_metadata=obs)
105 cat.camera_wrapper = GalSimCameraWrapper(camera)
107 psf = SNRdocumentPSF(fwhm=fwhm)
108 cat.setPSF(psf)
110 cat.write_catalog(catName)
111 cat.write_images(nameRoot=imageRoot)
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)
121 wcs = exposure.getWcs()
123 xxTestList = []
124 yyTestList = []
126 raImage = []
127 decImage = []
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)
134 pt = LsstGeom.Point2D(xx, yy)
135 skyPt = wcs.pixelToSky(pt).getPosition(LsstGeom.degrees)
136 raImage.append(skyPt.getX())
137 decImage.append(skyPt.getY())
139 xxTestList = np.array(xxTestList)
140 yyTestList = np.array(yyTestList)
142 raImage = np.radians(np.array(raImage))
143 decImage = np.radians(np.array(decImage))
145 raControl, \
146 decControl = _raDecFromPixelCoords(xxTestList, yyTestList,
147 [detector.getName()]*len(xxTestList),
148 camera=camera, obs_metadata=obs,
149 epoch=2000.0)
151 errorList = arcsecFromRadians(haversine(raControl, decControl,
152 raImage, decImage))
154 medianError = np.median(errorList)
155 msg = 'medianError was %e' % medianError
156 self.assertLess(medianError, 0.01, msg=msg)
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)
165 if os.path.exists(scratchDir):
166 shutil.rmtree(scratchDir)
169class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
170 pass
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()