Exemple #1
0
    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)
Exemple #2
0
 def __init__(self, image):
     self.hdulist = fits.open(image)
     self.ampstring = self.hdulist[0].header['CCDAMP']
     self.flatcorr = self.hdulist[0].header['FLATCORR']
     self.flshcorr = self.hdulist[0].header['FLSHCORR']
     self.darkcorr = self.hdulist[0].header['DARKCORR']
     self.configure_arrays()
Exemple #3
0
    def testOpenConvienceFunction(self):
        """Test the open convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        assert_equal(hdul[0].header['NAXIS'], 2)
        assert_equal(hdul1[0].header['NAXIS'], 0)
        assert_equal(hdul[0].header['NAXIS1'], 10)
        assert_equal(hdul[0].header['NAXIS2'], 10)

        assert_raises(KeyError, lambda: hdul1[0].header['NAXIS1'])
        assert_raises(KeyError, lambda: hdul1[0].header['NAXIS2'])
        assert_raises(KeyError, lambda: hdul[0].header['NPIX1'])
        assert_raises(KeyError, lambda: hdul[0].header['NPIX2'])

        assert_equal(hdul1[0].header['NPIX1'], 10)
        assert_equal(hdul1[0].header['NPIX2'], 10)

        assert_true((hdul[0].data == np.ones((10, 10), dtype=np.int32)).all())

        assert_equal(hdul1[0].data, None)

        hdul.close()
        hdul1.close()
    def testOpenConvienceFunction(self):
        """Test the open convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        assert_equal(hdul[0].header['NAXIS'], 2)
        assert_equal(hdul1[0].header['NAXIS'], 0)
        assert_equal(hdul[0].header['NAXIS1'], 10)
        assert_equal(hdul[0].header['NAXIS2'], 10)

        assert_raises(KeyError, lambda: hdul1[0].header['NAXIS1'])
        assert_raises(KeyError, lambda: hdul1[0].header['NAXIS2'])
        assert_raises(KeyError, lambda: hdul[0].header['NPIX1'])
        assert_raises(KeyError, lambda: hdul[0].header['NPIX2'])

        assert_equal(hdul1[0].header['NPIX1'], 10)
        assert_equal(hdul1[0].header['NPIX2'], 10)

        assert_true((hdul[0].data == np.ones((10, 10), dtype=np.int32)).all())

        assert_equal(hdul1[0].data, None)

        hdul.close()
        hdul1.close()
    def testwritetoConvienceFunction(self):
        """Test the writeto convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0

        stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
                         clobber=True)
        fits.writeto(self.temp('new1.fits'), hdul1[0].data,hdul1[0].header,
                     clobber=True)

        hdul.close()
        hdul1.close()

        info1 = fits.info(self.temp('new.fits'), output=False)
        info2 = stpyfits.info(self.temp('new.fits'), output=False)
        info3 = fits.info(self.temp('new1.fits'), output=False)
        info4 = stpyfits.info(self.temp('new1.fits'), output=False)

        assert_equal(info1, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info2,
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'int32', '')])
        assert_equal(info3, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info4,
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'uint8', '')])
Exemple #6
0
    def testwritetoConvienceFunction(self):
        """Test the writeto convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0

        stpyfits.writeto(self.temp('new.fits'),
                         hdul[0].data,
                         header,
                         clobber=True)
        fits.writeto(self.temp('new1.fits'),
                     hdul1[0].data,
                     hdul1[0].header,
                     clobber=True)

        hdul.close()
        hdul1.close()

        info1 = fits.info(self.temp('new.fits'), output=False)
        info2 = stpyfits.info(self.temp('new.fits'), output=False)
        info3 = fits.info(self.temp('new1.fits'), output=False)
        info4 = stpyfits.info(self.temp('new1.fits'), output=False)

        assert_equal(info1, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info2,
                     [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'int32', '')])
        assert_equal(info3, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info4,
                     [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'uint8', '')])
    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 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 __init__(self, image):
     self.hdulist = fits.open(image)
     self.ampstring = self.hdulist[0].header['CCDAMP']
     self.flatcorr = self.hdulist[0].header['FLATCORR']
     self.flshcorr = self.hdulist[0].header['FLSHCORR']
     self.darkcorr = self.hdulist[0].header['DARKCORR']
     self.configure_arrays()
Exemple #10
0
    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()
Exemple #11
0
def _read_DQ_arrays(flt_name):
    with fits.open(flt_name) as h:
        ampstring = h[0].header['CCDAMP']
        dq1 = h['dq', 1].data
        if (ampstring == 'ABCD'):
            dq2 = h['dq', 2].data
        else:
            dq2 = None
    return dq1, dq2
Exemple #12
0
def _read_DQ_arrays(flt_name):
    with fits.open(flt_name) as h:
        ampstring = h[0].header['CCDAMP']
        dq1 = h['dq', 1].data
        if (ampstring == 'ABCD'):
            dq2 = h['dq', 2].data
        else:
            dq2 = None
    return dq1, dq2
Exemple #13
0
def hdr_vals_for_overscan(root):
    """Retrieve header keyword values from RAW and SPT
    FITS files to pass on to :func:`check_oscntab` and
    :func:`check_overscan`.

    Parameters
    ----------
    root : str
        Rootname of the observation. Can be relative path
        to the file excluding the type of FITS file and
        extension, e.g., '/my/path/jxxxxxxxq'.

    Returns
    -------
    ccdamp : str
        Amplifiers used to read out the CCDs.

    xstart : int
        Starting column of the readout in detector
        coordinates.

    ystart : int
        Starting row of the readout in detector
        coordinates.

    xsize : int
        Number of columns in the readout.

    ysize : int
        Number of rows in the readout.

    """
    with fits.open(root + '_spt.fits') as hdu:
        spthdr = hdu[0].header
    with fits.open(root + '_raw.fits') as hdu:
        prihdr = hdu[0].header
    xstart = spthdr['SS_A1CRN']
    ystart = spthdr['SS_A2CRN']
    xsize = spthdr['SS_A1SZE']
    ysize = spthdr['SS_A2SZE']
    ccdamp = prihdr['CCDAMP']

    return ccdamp, xstart, ystart, xsize, ysize
Exemple #14
0
def hdr_vals_for_overscan(root):
    """Retrieve header keyword values from RAW and SPT
    FITS files to pass on to :func:`check_oscntab` and
    :func:`check_overscan`.

    Parameters
    ----------
    root : str
        Rootname of the observation. Can be relative path
        to the file excluding the type of FITS file and
        extension, e.g., '/my/path/jxxxxxxxq'.

    Returns
    -------
    ccdamp : str
        Amplifiers used to read out the CCDs.

    xstart : int
        Starting column of the readout in detector
        coordinates.

    ystart : int
        Starting row of the readout in detector
        coordinates.

    xsize : int
        Number of columns in the readout.

    ysize : int
        Number of rows in the readout.

    """
    with fits.open(root + '_spt.fits') as hdu:
        spthdr = hdu[0].header
    with fits.open(root + '_raw.fits') as hdu:
        prihdr = hdu[0].header
    xstart = spthdr['SS_A1CRN']
    ystart = spthdr['SS_A2CRN']
    xsize = spthdr['SS_A1SZE']
    ysize = spthdr['SS_A2SZE']
    ccdamp = prihdr['CCDAMP']

    return ccdamp, xstart, ystart, xsize, ysize
Exemple #15
0
def extract_dark(prihdr, scihdu):
    """Extract superdark data from ``DARKFILE`` or ``DRKCFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    dark : ndarray or `None`
        Superdark, if any. Subtract this to apply ``DARKCORR``.

    """
    if prihdr.get('PCTECORR', 'OMIT') == 'COMPLETE':
        darkfile = prihdr.get('DRKCFILE', 'N/A')
    else:
        darkfile = prihdr.get('DARKFILE', 'N/A')

    if darkfile == 'N/A':
        return None

    darkfile = from_irafpath(darkfile)
    ampstring = prihdr['CCDAMP']

    # Calculate DARKTIME
    exptime = prihdr.get('EXPTIME', 0.0)
    flashdur = prihdr.get('FLASHDUR', 0.0)
    darktime = exptime + flashdur
    if exptime > 0:  # Not BIAS
        darktime += 3.0

    with fits.open(darkfile) as hdudark:
        if ampstring == 'ABCD':
            dark = np.concatenate(
                (hdudark['sci', 1].data, hdudark['sci', 2].data[::-1, :]),
                axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            dark = extract_ref(scihdu, hdudark['sci', 2])
        else:
            dark = extract_ref(scihdu, hdudark['sci', 1])

    dark = dark * darktime

    return dark
Exemple #16
0
def extract_dark(prihdr, scihdu):
    """Extract superdark data from ``DARKFILE`` or ``DRKCFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    dark : ndarray or `None`
        Superdark, if any. Subtract this to apply ``DARKCORR``.

    """
    if prihdr.get('PCTECORR', 'OMIT') == 'COMPLETE':
        darkfile = prihdr.get('DRKCFILE', 'N/A')
    else:
        darkfile = prihdr.get('DARKFILE', 'N/A')

    if darkfile == 'N/A':
        return None

    darkfile = from_irafpath(darkfile)
    ampstring = prihdr['CCDAMP']

    # Calculate DARKTIME
    exptime = prihdr.get('EXPTIME', 0.0)
    flashdur = prihdr.get('FLASHDUR', 0.0)
    darktime = exptime + flashdur
    if exptime > 0:  # Not BIAS
        darktime += 3.0

    with fits.open(darkfile) as hdudark:
        if ampstring == 'ABCD':
            dark = np.concatenate(
                (hdudark['sci', 1].data,
                 hdudark['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            dark = extract_ref(scihdu, hdudark['sci', 2])
        else:
            dark = extract_ref(scihdu, hdudark['sci', 1])

    dark = dark * darktime

    return dark
    def testImageBaseHDU__getattr__Method(self):
        """Test the __getattr__ method of ImageBaseHDU in both the fits
           and stpyfits namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        hdu = hdul[0]
        hdu1 = hdul1[0]

        assert_true((hdu.data == np.ones((10, 10), dtype=np.int32)).all())
        assert_equal(hdu1.data, None)

        hdul.close()
        hdul1.close()
Exemple #18
0
    def testImageBaseHDU__getattr__Method(self):
        """Test the __getattr__ method of ImageBaseHDU in both the fits
           and stpyfits namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        hdu = hdul[0]
        hdu1 = hdul1[0]

        assert_true((hdu.data == np.ones((10, 10), dtype=np.int32)).all())
        assert_equal(hdu1.data, None)

        hdul.close()
        hdul1.close()
Exemple #19
0
def extract_flash(prihdr, scihdu):
    """Extract postflash data from ``FLSHFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    flash : ndarray or `None`
        Postflash, if any. Subtract this to apply ``FLSHCORR``.

    """
    flshfile = prihdr.get('FLSHFILE', 'N/A')
    flashsta = prihdr.get('FLASHSTA', 'N/A')
    flashdur = prihdr.get('FLASHDUR', 0.0)

    if flshfile == 'N/A' or flashdur <= 0:
        return None

    if flashsta != 'SUCCESSFUL':
        warnings.warn('Flash status is {0}'.format(flashsta),
                      AstropyUserWarning)

    flshfile = from_irafpath(flshfile)
    ampstring = prihdr['CCDAMP']

    with fits.open(flshfile) as hduflash:
        if ampstring == 'ABCD':
            flash = np.concatenate(
                (hduflash['sci', 1].data,
                 hduflash['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            flash = extract_ref(scihdu, hduflash['sci', 2])
        else:
            flash = extract_ref(scihdu, hduflash['sci', 1])

    flash = flash * flashdur

    return flash
Exemple #20
0
def extract_flash(prihdr, scihdu):
    """Extract postflash data from ``FLSHFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    flash : ndarray or `None`
        Postflash, if any. Subtract this to apply ``FLSHCORR``.

    """
    flshfile = prihdr.get('FLSHFILE', 'N/A')
    flashsta = prihdr.get('FLASHSTA', 'N/A')
    flashdur = prihdr.get('FLASHDUR', 0.0)

    if flshfile == 'N/A' or flashdur <= 0:
        return None

    if flashsta != 'SUCCESSFUL':
        warnings.warn('Flash status is {0}'.format(flashsta),
                      AstropyUserWarning)

    flshfile = from_irafpath(flshfile)
    ampstring = prihdr['CCDAMP']

    with fits.open(flshfile) as hduflash:
        if ampstring == 'ABCD':
            flash = np.concatenate(
                (hduflash['sci', 1].data,
                 hduflash['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            flash = extract_ref(scihdu, hduflash['sci', 2])
        else:
            flash = extract_ref(scihdu, hduflash['sci', 1])

    flash = flash * flashdur

    return flash
    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'))
Exemple #22
0
def extract_flatfield(prihdr, scihdu):
    """Extract flatfield data from ``PFLTFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    invflat : ndarray or `None`
        Inverse flatfield, if any. Multiply this to apply ``FLATCORR``.

    """
    for ff in ['DFLTFILE', 'LFLTFILE']:
        vv = prihdr.get(ff, 'N/A')
        if vv != 'N/A':
            warnings.warn(f'{ff}={vv} is not accounted for',
                          AstropyUserWarning)

    flatfile = prihdr.get('PFLTFILE', 'N/A')

    if flatfile == 'N/A':
        return None

    flatfile = from_irafpath(flatfile)
    ampstring = prihdr['CCDAMP']

    with fits.open(flatfile) as hduflat:
        if ampstring == 'ABCD':
            invflat = np.concatenate((1 / hduflat['sci', 1].data,
                                      1 / hduflat['sci', 2].data[::-1, :]),
                                     axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            invflat = 1 / extract_ref(scihdu, hduflat['sci', 2])
        else:
            invflat = 1 / extract_ref(scihdu, hduflat['sci', 1])

    return invflat
Exemple #23
0
def extract_flatfield(prihdr, scihdu):
    """Extract flatfield data from ``PFLTFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    invflat : ndarray or `None`
        Inverse flatfield, if any. Multiply this to apply ``FLATCORR``.

    """
    for ff in ['DFLTFILE', 'LFLTFILE']:
        vv = prihdr.get(ff, 'N/A')
        if vv != 'N/A':
            warnings.warn('{0}={1} is not accounted for'.format(ff, vv),
                          AstropyUserWarning)

    flatfile = prihdr.get('PFLTFILE', 'N/A')

    if flatfile == 'N/A':
        return None

    flatfile = from_irafpath(flatfile)
    ampstring = prihdr['CCDAMP']

    with fits.open(flatfile) as hduflat:
        if ampstring == 'ABCD':
            invflat = np.concatenate(
                (1 / hduflat['sci', 1].data,
                 1 / hduflat['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            invflat = 1 / extract_ref(scihdu, hduflat['sci', 2])
        else:
            invflat = 1 / extract_ref(scihdu, hduflat['sci', 1])

    return invflat
Exemple #24
0
    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'))
Exemple #25
0
    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()
Exemple #26
0
    def testStrayPixvalue(self):
        """Regression test for #885
        (https://svn.stsci.edu/trac/ssb/stsci_python/ticket/885)

        Tests that HDUs containing a non-zero NAXIS as well as a PIXVALUE
        keyword in their header are not treated as constant value HDUs.
        """

        data = np.arange(100).reshape((10, 10))
        phdu = fits.PrimaryHDU(data=data)
        hdu = fits.ImageHDU(data=data)

        phdu.header['PIXVALUE'] = 10
        hdu.header['PIXVALUE'] = 10

        hdul = fits.HDUList([phdu, hdu])
        hdul.writeto(self.temp('test.fits'))

        with stpyfits.open(self.temp('test.fits')) as h:
            assert_false(isinstance(h[0], stpyfits.ConstantValuePrimaryHDU))
            assert_false(isinstance(h[1], stpyfits.ConstantValueImageHDU))
            assert_true((h[0].data == data).all())
            assert_true((h[1].data == data).all())
    def testStrayPixvalue(self):
        """Regression test for #885
        (https://svn.stsci.edu/trac/ssb/stsci_python/ticket/885)

        Tests that HDUs containing a non-zero NAXIS as well as a PIXVALUE
        keyword in their header are not treated as constant value HDUs.
        """

        data = np.arange(100).reshape((10, 10))
        phdu = fits.PrimaryHDU(data=data)
        hdu = fits.ImageHDU(data=data)

        phdu.header['PIXVALUE'] = 10
        hdu.header['PIXVALUE'] = 10

        hdul = fits.HDUList([phdu, hdu])
        hdul.writeto(self.temp('test.fits'))

        with stpyfits.open(self.temp('test.fits')) as h:
            assert_false(isinstance(h[0], stpyfits.ConstantValuePrimaryHDU))
            assert_false(isinstance(h[1], stpyfits.ConstantValueImageHDU))
            assert_true((h[0].data == data).all())
            assert_true((h[1].data == data).all())
    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()
Exemple #29
0
    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()
Exemple #30
0
    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()
    def testupdateConvienceFunction(self):
        """Test the update convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0
        stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
                         clobber=True)

        hdu = stpyfits.ImageHDU()
        hdu1 = fits.ImageHDU()

        hdu.data = hdul[0].data
        hdu1.data = hdul1[0].data
        hdu.header.set('BITPIX', 32)
        hdu1.header.set('BITPIX', 32)
        hdu.header.set('NAXIS', 0)
        hdu.header.set('PIXVALUE', 1, 'Constant pixel value', after='GCOUNT')
        hdu1.header.set('PIXVALUE', 1, 'Constant pixel value', after='GCOUNT')
        hdu.header.set('NPIX1', 10, 'length of constant array axis 1',
                       after='GCOUNT')
        hdu.header.set('NPIX2', 10, 'length of constant array axis 2',
                       after='NPIX1')
        stpyfits.append(self.temp('new.fits'), hdu.data, hdu.header)

        d = hdu.data * 0

        stpyfits.update(self.temp('new.fits'), d, hdu.header, 1)

        assert_equal(fits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', ''),
             (1, '', 'ImageHDU', 8, (), '', '')])
        assert_equal(stpyfits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', ''),
             (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')])

        hdul7 = stpyfits.open(self.temp('new.fits'))
        assert_equal(hdul7[1].header['NAXIS'], 2)
        assert_equal(hdul7[1].header['NAXIS1'], 10)
        assert_equal(hdul7[1].header['NAXIS2'], 10)
        assert_equal(hdul7[1].header['PIXVALUE'], 0)

        assert_raises(KeyError, lambda: hdul7[1].header['NPIX1'])
        assert_raises(KeyError, lambda: hdul7[1].header['NPIX2'])

        assert_true((hdul7[1].data ==
                     np.zeros((10, 10), dtype=np.int32)).all())

        hdul8 = fits.open(self.temp('new.fits'))
        assert_equal(hdul8[1].header['NAXIS'], 0)
        assert_equal(hdul8[1].header['NPIX1'], 10)
        assert_equal(hdul8[1].header['NPIX2'], 10)
        assert_equal(hdul8[1].header['PIXVALUE'], 0)

        assert_raises(KeyError, lambda: hdul8[1].header['NAXIS1'])
        assert_raises(KeyError, lambda: hdul8[1].header['NAXIS2'])

        assert_equal(hdul8[1].data, None)

        hdul7.close()
        hdul8.close()
        hdul.close()
        hdul1.close()
def reset_dq_bits(input,bits,extver=None,extname='dq'):
    """ This function resets bits in the integer array(s) of a FITS file.

    Parameters
    ----------
    filename : str
        full filename with path

    bits : str
        sum or list of integers corresponding to all the bits to be reset

    extver : int, optional
        List of version numbers of the DQ arrays
        to be corrected [Default Value: None, will do all]

    extname : str, optional
        EXTNAME of the DQ arrays in the FITS file
        [Default Value: 'dq']

    Notes
    -----
    The default value of None for the 'extver' parameter specifies that all
    extensions with EXTNAME matching 'dq' (as specified by the 'extname'
    parameter) will have their bits reset.

    Examples
    --------
        1. The following command will reset the 4096 bits in all
           the DQ arrays of the file input_file_flt.fits::

                reset_dq_bits("input_file_flt.fits", 4096)

        2. To reset the 2,32,64 and 4096 bits in the second DQ array,
           specified as 'dq,2', in the file input_file_flt.fits::

                reset_dq_bits("input_file_flt.fits", "2,32,64,4096", extver=2)

    """
    # Interpret bits value
    bits = bitmask.interpret_bits_value(bits)

    flist, fcol = parseinput.parseinput(input)
    for filename in flist:
        # open input file in write mode to allow updating the DQ array in-place
        p = fits.open(filename,mode='update')

        # Identify the DQ array to be updated
        # If no extver is specified, build a list of all DQ arrays in the file
        if extver is None:
            extver = []
            for hdu in p:
                # find only those extensions which match the input extname
                # using case-insensitive name comparisons for 'extname'
                if 'extver' in hdu.header and \
                   hdu.header['extname'].lower() == extname.lower():
                    extver.append(int(hdu.header['extver']))
        else:
            # Otherwise, insure that input extver values are a list
            if not isinstance(extver, list): extver = [extver]

        # for each DQ array identified in the file...
        for extn in extver:
            dqarr = p[extname,extn].data
            dqdtype = dqarr.dtype
            # reset the desired bits
            p[extname,extn].data = (dqarr & ~bits).astype(dqdtype) # preserve original dtype
            log.info('Reset bit values of %s to a value of 0 in %s[%s,%s]' %
                     (bits, filename, extname, extn))
        # close the file with the updated DQ array(s)
        p.close()
Exemple #33
0
def reset_dq_bits(input,bits,extver=None,extname='dq'):
    """ This function resets bits in the integer array(s) of a FITS file.

    Parameters
    ----------
    filename : str
        full filename with path

    bits : str
        sum or list of integers corresponding to all the bits to be reset

    extver : int, optional
        List of version numbers of the DQ arrays
        to be corrected [Default Value: None, will do all]

    extname : str, optional
        EXTNAME of the DQ arrays in the FITS file
        [Default Value: 'dq']

    Notes
    -----
    The default value of None for the 'extver' parameter specifies that all
    extensions with EXTNAME matching 'dq' (as specified by the 'extname'
    parameter) will have their bits reset.

    Examples
    --------
        1. The following command will reset the 4096 bits in all
           the DQ arrays of the file input_file_flt.fits::

                reset_dq_bits("input_file_flt.fits", 4096)

        2. To reset the 2,32,64 and 4096 bits in the second DQ array,
           specified as 'dq,2', in the file input_file_flt.fits::

                reset_dq_bits("input_file_flt.fits", "2,32,64,4096", extver=2)

    """
    # Interpret bits value
    bits = interpret_bit_flags(bits)

    flist, fcol = parseinput.parseinput(input)
    for filename in flist:
        # open input file in write mode to allow updating the DQ array in-place
        p = fits.open(filename, mode='update', memmap=False)

        # Identify the DQ array to be updated
        # If no extver is specified, build a list of all DQ arrays in the file
        if extver is None:
            extver = []
            for hdu in p:
                # find only those extensions which match the input extname
                # using case-insensitive name comparisons for 'extname'
                if 'extver' in hdu.header and \
                   hdu.header['extname'].lower() == extname.lower():
                    extver.append(int(hdu.header['extver']))
        else:
            # Otherwise, insure that input extver values are a list
            if not isinstance(extver, list): extver = [extver]

        # for each DQ array identified in the file...
        for extn in extver:
            dqarr = p[extname,extn].data
            dqdtype = dqarr.dtype
            # reset the desired bits
            p[extname,extn].data = (dqarr & ~bits).astype(dqdtype) # preserve original dtype
            log.info('Reset bit values of %s to a value of 0 in %s[%s,%s]' %
                     (bits, filename, extname, extn))
        # close the file with the updated DQ array(s)
        p.close()
def crrej_plus(filelist, outroot, keep_intermediate_files=False, verbose=True):
    r"""Perform CRREJ and ACS2D on given BLV_TMP or BLC_TMP files.
    The purpose of this is primarily for combining destriped
    products from :func:`destripe_plus`.

    .. note::

        If you want to run this step, it is important to keep
        intermediate files when running :func:`destripe_plus`.

    Parameters
    ----------
    filelist : str or list of str
        List of BLV_TMP or BLC_TMP files to be combined and processed.
        Do not mix BLV and BLC in the same list, but rather run this
        once for BLV and once for BLC separately.
        Input filenames in one of these formats:

            * a Python list of filenames
            * a partial filename with wildcards ('\*blx_tmp.fits')
            * filename of an ASN table ('j12345670_asn.fits')
            * an at-file (``@input``)

    outroot : str
        Rootname for combined product, which will be named
        ``<outroot>_crj.fits`` (for BLV inputs) or
        ``<outroot>_crc.fits`` (for BLC inputs).

    keep_intermediate_files : bool
        Keep de-striped CRJ_TMP or CRC_TMP around after CRREJ.

    verbose : bool
        Print informational messages.

    Raises
    ------
    ValueError
        Ambiguous input list.

    Examples
    --------
    >>> from acstools.acs_destripe_plus import destripe_plus, crrej_plus

    First, run :func:`destripe_plus`. Remember to keep its intermediate files:

    >>> destripe_plus(..., keep_intermediate_files=True)

    Now, run this function, once on BLV only, and once again on BLC only:

    >>> rootname = 'j12345678'
    >>> crrej_plus('*_blv_tmp.fits', rootname)
    >>> crrej_plus('*_blc_tmp.fits', rootname)

    """
    # Optional package dependencies
    from stsci.tools import parseinput

    filelist = parseinput.parseinput(filelist)[0]

    is_blv = np.all(['blv_tmp.fits' in f for f in filelist])
    is_blc = np.all(['blc_tmp.fits' in f for f in filelist])

    if is_blv is is_blc:
        raise ValueError('Inputs must be all BLV_TMP or all BLC_TMP files')

    if is_blv:
        sfx = 'crj'
    else:  # is_blc
        sfx = 'crc'

    tmpname = f'{outroot}_{sfx}_tmp.fits'
    acsrej.acsrej(filelist, tmpname, verbose=verbose)
    acs2d.acs2d(tmpname, verbose=verbose)  # crx_tmp -> crx

    # Work around a bug in ACS2D that names crx_tmp -> crx_tmp_flt, see
    # https://github.com/spacetelescope/hstcal/pull/470
    wrongname = f'{outroot}_{sfx}_tmp_flt.fits'
    rightname = f'{outroot}_{sfx}.fits'
    if os.path.isfile(wrongname) and not os.path.exists(rightname):
        os.rename(wrongname, rightname)

        # Brute-force fixing of final output header.
        with fits.open(rightname, 'update') as pf:
            pf[0].header['FILENAME'] = rightname
            pf[0].header['ROOTNAME'] = outroot

            for hdu in pf[1:]:
                hdu.header['EXPNAME'] = outroot
                hdu.header['ROOTNAME'] = outroot

    # Delete intermediate file
    if not keep_intermediate_files and os.path.isfile(tmpname):
        os.remove(tmpname)

    # Delete extraneous trailer file generated by ACSREJ. Its contents are
    # copied to the trailer file generated by ACS2D after anyway.
    final_tra = f'{outroot}_{sfx}_tmp.tra'
    extra_tra = f'{outroot}.tra'
    if os.path.isfile(extra_tra) and os.path.isfile(final_tra):
        os.remove(extra_tra)

        # If fixing the HSTCAL bug changes TRA naming too, this needs to be
        # adjusted accordingly.
        os.rename(final_tra, f'{outroot}_{sfx}.tra')
Exemple #35
0
    def testupdateConvienceFunction(self):
        """Test the update convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0
        stpyfits.writeto(self.temp('new.fits'),
                         hdul[0].data,
                         header,
                         clobber=True)

        hdu = stpyfits.ImageHDU()
        hdu1 = fits.ImageHDU()

        hdu.data = hdul[0].data
        hdu1.data = hdul1[0].data
        hdu.header.set('BITPIX', 32)
        hdu1.header.set('BITPIX', 32)
        hdu.header.set('NAXIS', 0)
        hdu.header.set('PIXVALUE', 1, 'Constant pixel value', after='GCOUNT')
        hdu1.header.set('PIXVALUE', 1, 'Constant pixel value', after='GCOUNT')
        hdu.header.set('NPIX1',
                       10,
                       'length of constant array axis 1',
                       after='GCOUNT')
        hdu.header.set('NPIX2',
                       10,
                       'length of constant array axis 2',
                       after='NPIX1')
        stpyfits.append(self.temp('new.fits'), hdu.data, hdu.header)

        d = hdu.data * 0

        stpyfits.update(self.temp('new.fits'), d, hdu.header, 1)

        assert_equal(fits.info(self.temp('new.fits'), output=False),
                     [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', ''),
                      (1, '', 'ImageHDU', 8, (), '', '')])
        assert_equal(stpyfits.info(self.temp('new.fits'), output=False),
                     [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', ''),
                      (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')])

        hdul7 = stpyfits.open(self.temp('new.fits'))
        assert_equal(hdul7[1].header['NAXIS'], 2)
        assert_equal(hdul7[1].header['NAXIS1'], 10)
        assert_equal(hdul7[1].header['NAXIS2'], 10)
        assert_equal(hdul7[1].header['PIXVALUE'], 0)

        assert_raises(KeyError, lambda: hdul7[1].header['NPIX1'])
        assert_raises(KeyError, lambda: hdul7[1].header['NPIX2'])

        assert_true((hdul7[1].data == np.zeros((10, 10),
                                               dtype=np.int32)).all())

        hdul8 = fits.open(self.temp('new.fits'))
        assert_equal(hdul8[1].header['NAXIS'], 0)
        assert_equal(hdul8[1].header['NPIX1'], 10)
        assert_equal(hdul8[1].header['NPIX2'], 10)
        assert_equal(hdul8[1].header['PIXVALUE'], 0)

        assert_raises(KeyError, lambda: hdul8[1].header['NAXIS1'])
        assert_raises(KeyError, lambda: hdul8[1].header['NAXIS2'])

        assert_equal(hdul8[1].data, None)

        hdul7.close()
        hdul8.close()
        hdul.close()
        hdul1.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()
    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()