def testDeconvertConstantArray(self): """When a constant value array's data is overridden with non- constant data, test that when saving the file it removes all constant value array keywords and is treated as a normal image HDU. """ data = np.ones((100, 100)) hdu = stpyfits.PrimaryHDU(data=data) hdu.header['PIXVALUE'] = 1 hdu.writeto(self.temp('test.fits')) with stpyfits.open(self.temp('test.fits'), mode='update') as h: assert_equal(h[0].header['PIXVALUE'], 1) h[0].data[20:80, 20:80] = 2 with fits.open(self.temp('test.fits')) as h: assert_true('PIXVALUE' not in h[0].header) assert_true('NPIX1' not in h[0].header) assert_true('NPIX2' not in h[0].header) assert_equal(h[0].header.count('NAXIS'), 1) assert_equal(h[0].header['NAXIS'], 2) assert_equal(h[0].header['NAXIS1'], 100) assert_equal(h[0].header['NAXIS2'], 100) assert_equal(h[0].data.max(), 2) assert_equal(h[0].data.min(), 1)
def testImageBaseHDUWriteToMethod(self): """Test the writeto method of _ConstantValueImageBaseHDU in the stpyfits namespace.""" n = np.zeros(10) n = n + 1 hdu = stpyfits.PrimaryHDU(n) hdu.header.set('PIXVALUE', 1., 'constant pixel value', after='EXTEND') hdu.writeto(self.temp('new.fits'), clobber=True) hdul = stpyfits.open(self.temp('new.fits')) hdul1 = fits.open(self.temp('new.fits')) assert_equal(hdul[0].header['NAXIS'], 1) assert_equal(hdul[0].header['NAXIS1'], 10) assert_equal(hdul[0].header['PIXVALUE'], 1.0) assert_raises(KeyError, lambda: hdul[0].header['NPIX1']) assert_true((hdul[0].data == np.ones(10, dtype=np.float32)).all()) assert_equal(hdul1[0].header['NAXIS'], 0) assert_equal(hdul1[0].header['NPIX1'], 10) assert_equal(hdul1[0].header['PIXVALUE'], 1.0) assert_raises(KeyError, lambda: hdul1[0].header['NAXIS1']) assert_equal(hdul1[0].data, None) hdul.close() hdul1.close()
def testGetvalExtensionHDU(self): """Regression test for an issue that came up with the fact that ImageHDU has a different argument signature from PrimaryHDU. """ data = np.ones((100, 100)) hdu = stpyfits.ImageHDU(data=data) hdu.header['PIXVALUE'] = 1 hdu.header['FOO'] = 'test' hdul = stpyfits.HDUList([stpyfits.PrimaryHDU(), hdu]) hdul.writeto(self.temp('test.fits')) assert_equal(stpyfits.getval(self.temp('test.fits'), 'FOO', ext=1), 'test')
def testDimensionlessConstantValueArray(self): """Tests a case that was reported where an HDU can be a constant value HDU (it has a PIXVALUE and NAXIS=0) but NPIX1 = NPIX2 = 0 as well. """ hdu = stpyfits.PrimaryHDU() hdu.header['NAXIS'] = 0 hdu.header['BITPIX'] = 16 hdu.header['NPIX1'] = 0 hdu.header['NPIX2'] = 0 hdu.header['PIXVALUE'] = 0 hdu.writeto(self.temp('test.fits')) with stpyfits.open(self.temp('test.fits')) as h: assert_true(h[0].data is None) h.writeto(self.temp('test2.fits'))
def testPrimaryHDUConstructor(self): """Test the PrimaryHDU constructor in both the fits and stpyfits namespace. Although stpyfits does not reimplement the constructor, it does add _ConstantValueImageBaseHDU to the inheritance hierarchy of fits.PrimaryHDU when accessed through the stpyfits namespace. This method tests that that inheritance is working""" n = np.zeros(10) n = n + 1 hdu = stpyfits.PrimaryHDU(n) hdu.header.set('PIXVALUE', 1.0, 'Constant pixel value', after='EXTEND') hdu.header.set('NAXIS', 0) stpyfits.writeto(self.temp('new.fits'), hdu.data, hdu.header, clobber=True) hdul = stpyfits.open(self.temp('new.fits')) hdul1 = fits.open(self.temp('new.fits')) assert_equal(hdul[0].header['NAXIS'], 1) assert_equal(hdul[0].header['NAXIS1'], 10) assert_equal(hdul[0].header['PIXVALUE'], 1.0) assert_raises(KeyError, lambda: hdul[0].header['NPIX1']) assert_true((hdul[0].data == np.ones(10, dtype=np.float32)).all()) assert_equal(hdul1[0].header['NAXIS'], 0) assert_equal(hdul1[0].header['NPIX1'], 10) assert_equal(hdul1[0].header['PIXVALUE'], 1.0) assert_raises(KeyError, lambda: hdul1[0].header['NAXIS1']) assert_equal(hdul1[0].data, None) hdul.close() hdul1.close()
def testHDUListFlushMethod(self): """Test the flush method of HDUList in both the fits and stpyfits namespace.""" hdu = stpyfits.PrimaryHDU() hdu1 = stpyfits.ImageHDU() hdu.data = np.zeros((10, 10), dtype=np.int32) hdu1.data = hdu.data + 2 hdu.header.set('BITPIX', 32) hdu1.header.set('BITPIX', 32) hdu.header.set('NAXIS', 2) hdu.header.set('NAXIS1', 10, 'length of constant array axis 1', after='NAXIS') hdu.header.set('NAXIS2', 10, 'length of constant array axis 2', after='NAXIS1') hdu.header.set('PIXVALUE', 0, 'Constant pixel value') hdu1.header.set('PIXVALUE', 2, 'Constant pixel value', after='GCOUNT') hdu1.header.set('NAXIS', 2) hdu1.header.set('NAXIS1', 10, 'length of constant array axis 1', after='NAXIS') hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2', after='NAXIS1') hdul = stpyfits.HDUList([hdu, hdu1]) hdul.writeto(self.temp('new.fits'), clobber=True) hdul = stpyfits.open(self.temp('new.fits'), 'update') d = np.arange(10, dtype=np.int32) d = d * 0 d = d + 3 hdul[0].data = d hdul.flush() hdul.close() assert_equal(stpyfits.info(self.temp('new.fits'), output=False), [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, ), 'int32', ''), (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')]) assert_equal(fits.info(self.temp('new.fits'), output=False), [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', ''), (1, '', 'ImageHDU', 8, (), '', '')]) hdul1 = stpyfits.open(self.temp('new.fits')) hdul2 = fits.open(self.temp('new.fits')) assert_equal(hdul1[0].header['NAXIS'], 1) assert_equal(hdul1[0].header['NAXIS1'], 10) assert_equal(hdul1[0].header['PIXVALUE'], 3) assert_raises(KeyError, lambda: hdul1[0].header['NPIX1']) assert_true( (hdul1[0].data == (np.zeros(10, dtype=np.int32) + 3)).all()) assert_equal(hdul2[0].header['NAXIS'], 0) assert_equal(hdul2[0].header['NPIX1'], 10) assert_equal(hdul2[0].header['PIXVALUE'], 3) assert_raises(KeyError, lambda: hdul2[0].header['NAXIS1']) assert_equal(hdul2[0].data, None) hdul1.close() hdul2.close() hdul3 = stpyfits.open(self.temp('new.fits'), 'update') d = np.arange(15, dtype=np.int32) d = d * 0 d = d + 4 hdul3[0].data = d hdul3.close() # Note that close calls flush assert_equal(stpyfits.info(self.temp('new.fits'), output=False), [(0, 'PRIMARY', 'PrimaryHDU', 6, (15, ), 'int32', ''), (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')]) assert_equal(fits.info(self.temp('new.fits'), output=False), [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', ''), (1, '', 'ImageHDU', 8, (), '', '')]) hdul1 = stpyfits.open(self.temp('new.fits')) hdul2 = fits.open(self.temp('new.fits')) assert_equal(hdul1[0].header['NAXIS'], 1) assert_equal(hdul1[0].header['NAXIS1'], 15) assert_equal(hdul1[0].header['PIXVALUE'], 4) assert_raises(KeyError, lambda: hdul1[0].header['NPIX1']) assert_true( (hdul1[0].data == (np.zeros(15, dtype=np.int32) + 4)).all()) assert_equal(hdul2[0].header['NAXIS'], 0) assert_equal(hdul2[0].header['NPIX1'], 15) assert_equal(hdul2[0].header['PIXVALUE'], 4) assert_raises(KeyError, lambda: hdul2[0].header['NAXIS1']) assert_equal(hdul2[0].data, None) hdul1.close() hdul2.close()
def testHDUListWritetoMethod(self): """Test the writeto method of HDUList in both the fits and stpyfits namespace.""" hdu = stpyfits.PrimaryHDU() hdu1 = stpyfits.ImageHDU() hdu.data = np.zeros((10, 10), dtype=np.int32) hdu1.data = hdu.data + 2 hdu.header.set('BITPIX', 32) hdu1.header.set('BITPIX', 32) hdu.header.set('NAXIS', 2) hdu.header.set('NAXIS1', 10, 'length of constant array axis 1', after='NAXIS') hdu.header.set('NAXIS2', 10, 'length of constant array axis 2', after='NAXIS1') hdu.header.set('PIXVALUE', 0, 'Constant pixel value') hdu1.header.set('PIXVALUE', 2, 'Constant pixel value', after='GCOUNT') hdu1.header.set('NAXIS', 2) hdu1.header.set('NAXIS1', 10, 'length of constant array axis 1', after='NAXIS') hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2', after='NAXIS1') hdul = stpyfits.HDUList([hdu, hdu1]) hdul.writeto(self.temp('new.fits'), clobber=True) assert_equal(stpyfits.info(self.temp('new.fits'), output=False), [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', ''), (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')]) assert_equal(fits.info(self.temp('new.fits'), output=False), [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', ''), (1, '', 'ImageHDU', 8, (), '', '')]) hdul1 = stpyfits.open(self.temp('new.fits')) hdul2 = fits.open(self.temp('new.fits')) assert_equal(hdul1[0].header['NAXIS'], 2) assert_equal(hdul1[0].header['NAXIS1'], 10) assert_equal(hdul1[0].header['NAXIS2'], 10) assert_equal(hdul1[0].header['PIXVALUE'], 0) assert_raises(KeyError, lambda: hdul1[0].header['NPIX1']) assert_raises(KeyError, lambda: hdul1[0].header['NPIX2']) assert_true((hdul1[0].data == np.zeros((10, 10), dtype=np.int32)).all()) assert_equal(hdul1[1].header['NAXIS'], 2) assert_equal(hdul1[1].header['NAXIS1'], 10) assert_equal(hdul1[1].header['NAXIS2'], 10) assert_equal(hdul1[1].header['PIXVALUE'], 2) assert_raises(KeyError, lambda: hdul1[1].header['NPIX1']) assert_raises(KeyError, lambda: hdul1[1].header['NPIX2']) assert_true((hdul1[1].data == (np.zeros( (10, 10), dtype=np.int32) + 2)).all()) assert_equal(hdul2[0].header['NAXIS'], 0) assert_equal(hdul2[0].header['NPIX1'], 10) assert_equal(hdul2[0].header['NPIX2'], 10) assert_equal(hdul2[0].header['PIXVALUE'], 0) assert_raises(KeyError, lambda: hdul2[0].header['NAXIS1']) assert_raises(KeyError, lambda: hdul2[0].header['NAXIS2']) assert_equal(hdul2[0].data, None) assert_equal(hdul2[1].header['NAXIS'], 0) assert_equal(hdul2[1].header['NPIX1'], 10) assert_equal(hdul2[1].header['NPIX2'], 10) assert_equal(hdul2[1].header['PIXVALUE'], 2) assert_raises(KeyError, lambda: hdul2[1].header['NAXIS1']) assert_raises(KeyError, lambda: hdul2[1].header['NAXIS2']) hdul1.close() hdul2.close()