Coverage for tests/testFitsHeaders.py : 33%

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 os
2import numpy as np
3import unittest
4import tempfile
5import shutil
6import lsst.utils.tests
8import astropy.io.fits as fits
10from lsst.utils import getPackageDir
11from lsst.sims.utils.CodeUtilities import sims_clean_up
12from lsst.sims.utils import ObservationMetaData
13from lsst.sims.catalogs.db import fileDBObject
14from lsst.sims.GalSimInterface import GalSimStars, SNRdocumentPSF
15from lsst.sims.GalSimInterface import GalSimCameraWrapper
16from lsst.sims.GalSimInterface import LSSTCameraWrapper
17#from lsst.sims.coordUtils.utils import ReturnCamera
19from testUtils import create_text_catalog
21from lsst.sims.coordUtils import clean_up_lsst_camera
23ROOT = os.path.abspath(os.path.dirname(__file__))
26def setup_module(module):
27 lsst.utils.tests.init()
30class fitsHeaderFileDBObj(fileDBObject):
31 idColKey = 'test_id'
32 objectTypeId = 8123
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 fitsHeaderCatalog(GalSimStars):
44 bandpassNames = ['u']
46 def get_galacticRv(self):
47 ra = self.column_by_name('raJ2000')
48 return np.array([3.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 ]
59@unittest.skip('ReturnCamera deprecated - need replacement')
60class FitsHeaderTest(unittest.TestCase):
62 @classmethod
63 def tearDownClass(cls):
64 sims_clean_up()
65 clean_up_lsst_camera()
67 def testFitsHeader(self):
68 """
69 Create a test image with the LSST camera and with the
70 cartoon camera. Verify that the image created with the LSST
71 camera has the DM-required cards in its FITS header while the
72 image created with the cartoon camera does not
73 """
75 cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
76 cartoonCamera = ReturnCamera(cameraDir)
78 outputDir = tempfile.mkdtemp(dir=ROOT, prefix='testFitsHeader-')
80 lsst_cat_name = os.path.join(outputDir, 'fits_test_lsst_cat.txt')
81 lsst_cat_root = os.path.join(outputDir, 'fits_test_lsst_image')
83 cartoon_cat_name = os.path.join(outputDir, 'fits_test_cartoon_cat.txt')
84 cartoon_cat_root = os.path.join(outputDir, 'fits_test_cartoon_image')
86 obs = ObservationMetaData(pointingRA=32.0, pointingDec=22.0,
87 boundLength=0.1, boundType='circle',
88 mjd=58000.0, rotSkyPos=14.0, bandpassName='u')
90 obs.OpsimMetaData = {'obshistID': 112}
92 dbFileName = os.path.join(outputDir, 'fits_test_db.dat')
93 create_text_catalog(obs, dbFileName, np.array([30.0]), np.array([30.0]), [22.0])
94 db = fitsHeaderFileDBObj(dbFileName, runtable='test')
96 # first test the lsst camera
97 lsstCat = fitsHeaderCatalog(db, obs_metadata=obs)
98 lsstCat.camera_wrapper = LSSTCameraWrapper()
99 lsstCat.PSF = SNRdocumentPSF()
100 lsstCat.write_catalog(lsst_cat_name)
101 lsstCat.write_images(nameRoot=lsst_cat_root)
103 list_of_files = os.listdir(outputDir)
104 ct = 0
105 for file_name in list_of_files:
106 true_name = os.path.join(outputDir, file_name)
107 if lsst_cat_root in true_name:
108 ct += 1
109 with fits.open(true_name) as fitsTest:
110 header = fitsTest[0].header
111 self.assertIn('CHIPID', header)
112 self.assertIn('OBSID', header)
113 self.assertIn('OUTFILE', header)
114 self.assertEqual(header['OBSID'], 112)
115 self.assertEqual(header['CHIPID'], 'R22_S11')
116 self.assertEqual(header['OUTFILE'], 'lsst_e_112_f0_R22_S11_E000')
117 os.unlink(true_name)
119 self.assertGreater(ct, 0)
120 if os.path.exists(lsst_cat_name):
121 os.unlink(lsst_cat_name)
123 # now test with the cartoon camera
124 cartoonCat = fitsHeaderCatalog(db, obs_metadata=obs)
125 cartoonCat.camera_wrapper = GalSimCameraWrapper(cartoonCamera)
126 cartoonCat.PSF = SNRdocumentPSF()
127 cartoonCat.write_catalog(cartoon_cat_name)
128 cartoonCat.write_images(nameRoot=cartoon_cat_root)
129 list_of_files = os.listdir(outputDir)
130 ct = 0
131 for file_name in list_of_files:
132 true_name = os.path.join(outputDir, file_name)
133 if cartoon_cat_root in true_name:
134 ct += 1
135 with fits.open(true_name) as fitsTest:
136 header = fitsTest[0].header
137 self.assertNotIn('CHIPID', header)
138 self.assertNotIn('OBSID', header)
139 self.assertNotIn('OUTFILE', header)
140 os.unlink(true_name)
142 self.assertGreater(ct, 0)
143 if os.path.exists(cartoon_cat_name):
144 os.unlink(cartoon_cat_name)
146 if os.path.exists(dbFileName):
147 os.unlink(dbFileName)
149 if os.path.exists(outputDir):
150 shutil.rmtree(outputDir)
153class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
154 pass
156if __name__ == "__main__": 156 ↛ 157line 156 didn't jump to line 157, because the condition on line 156 was never true
157 lsst.utils.tests.init()
158 unittest.main()