Coverage for tests/testAllowedChips.py : 24%

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 numpy as np
3import os
4import tempfile
5import shutil
6import lsst.utils.tests
8from lsst.utils import getPackageDir
10import lsst.afw.cameraGeom.testUtils as camTestUtils
12import lsst.afw.image as afwImage
13from lsst.afw.cameraGeom import DetectorType
14from lsst.sims.utils.CodeUtilities import sims_clean_up
15from lsst.sims.utils import ObservationMetaData
16from lsst.sims.catalogs.db import fileDBObject
17from lsst.sims.coordUtils import raDecFromPixelCoords
18from lsst.sims.photUtils import Sed, Bandpass, BandpassDict, PhotometricParameters
19from lsst.sims.GalSimInterface import GalSimStars, SNRdocumentPSF
20from lsst.sims.GalSimInterface import GalSimCameraWrapper
21from testUtils import create_text_catalog
23ROOT = os.path.abspath(os.path.dirname(__file__))
26def setup_module(module):
27 lsst.utils.tests.init()
30class allowedChipsFileDBObj(fileDBObject):
31 idColKey = 'test_id'
32 objectTypeId = 88
33 tableid = 'test'
34 raColName = 'ra'
35 decColName = 'dec'
37 columns = [('raJ2000', 'ra*PI()/180.0', np.float),
38 ('decJ2000', 'dec*PI()/180.0', np.float),
39 ('magNorm', 'mag_norm', np.float)]
42class allowedChipsCatalog(GalSimStars):
44 bandpassNames = ['u']
46 def get_galacticAv(self):
47 ra = self.column_by_name('raJ2000')
48 return np.array([0.1]*len(ra))
50 default_columns = GalSimStars.default_columns
52 default_columns += [('sedFilename', 'sed_flat.txt', (str, 12)),
53 ('properMotionRa', 0.0, np.float),
54 ('properMotionDec', 0.0, np.float),
55 ('radialVelocity', 0.0, np.float),
56 ('parallax', 0.0, np.float)
57 ]
60class allowedChipsTest(unittest.TestCase):
62 longMessage = True
64 @classmethod
65 def setUpClass(cls):
66 cls.scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='allowedChipsTest-')
67 cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1,
68 mjd=57381.2, rotSkyPos=43.2,
69 bandpassName='r')
71 cls.camera = camTestUtils.CameraWrapper().camera
73 cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt')
74 if os.path.exists(cls.dbFileName):
75 os.unlink(cls.dbFileName)
77 cls.controlSed = Sed()
78 cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'),
79 'flatSED', 'sed_flat.txt.gz'))
80 cls.magNorm = 18.1
81 imsim = Bandpass()
82 imsim.imsimBandpass()
83 ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim)
84 cls.controlSed.multiplyFluxNorm(ff)
85 a_x, b_x = cls.controlSed.setupCCM_ab()
86 cls.controlSed.addDust(a_x, b_x, A_v=0.1, R_v=3.1)
87 bpd = BandpassDict.loadTotalBandpassesFromFiles()
88 pp = PhotometricParameters()
89 cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp)
90 cls.countSigma = np.sqrt(cls.controlADU/pp.gain)
92 cls.x_pix = 50
93 cls.y_pix = 50
95 x_list = []
96 y_list = []
97 name_list = []
98 for dd in cls.camera:
99 x_list.append(cls.x_pix)
100 y_list.append(cls.y_pix)
101 name_list.append(dd.getName())
103 x_list = np.array(x_list)
104 y_list = np.array(y_list)
106 ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list,
107 camera=cls.camera, obs_metadata=cls.obs,
108 epoch=2000.0)
110 dra_list = 3600.0*(ra_list-cls.obs.pointingRA)
111 ddec_list = 3600.0*(dec_list-cls.obs.pointingDec)
113 create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list,
114 mag_norm=[cls.magNorm]*len(dra_list))
116 cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
118 @classmethod
119 def tearDownClass(cls):
120 sims_clean_up()
121 del cls.camera
122 if os.path.exists(cls.dbFileName):
123 os.unlink(cls.dbFileName)
124 if os.path.exists(cls.scratchDir):
125 shutil.rmtree(cls.scratchDir)
127 def testCamera(self):
128 """
129 Test that GalSimCatalogs respect the allowed_chips variable by
130 generating a catalog with one object on each chip.
132 Generate images from a control catalog that allows all chips.
133 Verify that each image contains the expected flux.
135 Generate images from a test catalog that only allows two chips.
136 Verify that only the two expected images exist and that each
137 contains only the expected flux.
138 """
140 controlCatalog = allowedChipsCatalog(self.db, obs_metadata=self.obs)
141 testCatalog = allowedChipsCatalog(self.db, obs_metadata=self.obs)
142 psf = SNRdocumentPSF()
143 controlCatalog.setPSF(psf)
144 testCatalog.setPSF(psf)
145 controlCatalog.camera_wrapper = GalSimCameraWrapper(self.camera)
146 testCatalog.camera_wrapper = GalSimCameraWrapper(self.camera)
148 test_root = os.path.join(self.scratchDir, 'allowed_chip_test_image')
149 control_root = os.path.join(self.scratchDir, 'allowed_chip_control_image')
151 name_list = []
152 for dd in self.camera:
153 if dd.getType() == DetectorType.WAVEFRONT or dd.getType() == DetectorType.GUIDER:
154 continue
155 name = dd.getName()
156 name_list.append(name)
157 stripped_name = name.replace(':', '')
158 stripped_name = stripped_name.replace(',', '')
159 stripped_name = stripped_name.replace(' ', '_')
161 test_image_name = os.path.join(self.scratchDir, test_root+'_'+stripped_name+'_u.fits')
162 control_image_name = os.path.join(self.scratchDir, control_root+'_'+stripped_name+'_u.fits')
164 # remove any images that were generated the last time this test
165 # was run
166 if os.path.exists(test_image_name):
167 os.unlink(test_image_name)
168 if os.path.exists(control_image_name):
169 os.unlink(control_image_name)
171 # only allow two chips in the test catalog
172 allowed_chips = [name_list[3], name_list[4]]
174 testCatalog.allowed_chips = allowed_chips
176 test_cat_name = os.path.join(self.scratchDir, 'allowed_chips_test_cat.txt')
177 control_cat_name = os.path.join(self.scratchDir, 'allowed_chips_control_cat.txt')
179 testCatalog.write_catalog(test_cat_name)
180 controlCatalog.write_catalog(control_cat_name)
182 testCatalog.write_images(nameRoot=test_root)
183 controlCatalog.write_images(nameRoot=control_root)
185 test_image_ct = 0
187 for name in name_list:
188 # Loop through each chip on the camera.
189 # Verify that the control catalog generated an image for each chip.
190 # Verify that the test catalog only generated images for the two
191 # specified chips.
192 # Verify that each image contains the expected amount of flux.
194 stripped_name = name.replace(':', '')
195 stripped_name = stripped_name.replace(',', '')
196 stripped_name = stripped_name.replace(' ', '_')
198 test_image_name = os.path.join(self.scratchDir, test_root+'_'+stripped_name+'_u.fits')
199 control_image_name = os.path.join(self.scratchDir, control_root+'_'+stripped_name+'_u.fits')
201 msg = '%s does not exist; it should' % control_image_name
202 self.assertTrue(os.path.exists(control_image_name), msg=msg)
203 im = afwImage.ImageF(control_image_name).getArray()
204 msg="\nimage contains %e counts\nshould contain %e\n\n" % (im.sum(), self.controlADU)
205 self.assertLess(np.abs(im.sum()-self.controlADU), 3.0*self.countSigma,
206 msg=msg)
207 os.unlink(control_image_name)
209 if name in allowed_chips:
210 msg = '%s does not exist; it should' % test_image_name
211 self.assertTrue(os.path.exists(test_image_name), msg=msg)
212 im = afwImage.ImageF(test_image_name).getArray()
213 self.assertLess(np.abs(im.sum()-self.controlADU), 3.0*self.countSigma)
214 os.unlink(test_image_name)
215 test_image_ct += 1
216 else:
217 msg = '%s exists; it should not' % test_image_name
218 self.assertFalse(os.path.exists(test_image_name), msg=msg)
220 self.assertEqual(test_image_ct, len(allowed_chips))
222 if os.path.exists(test_cat_name):
223 os.unlink(test_cat_name)
224 if os.path.exists(control_cat_name):
225 os.unlink(control_cat_name)
228class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
229 pass
231if __name__ == "__main__": 231 ↛ 232line 231 didn't jump to line 232, because the condition on line 231 was never true
232 lsst.utils.tests.init()
233 unittest.main()