Exemple #1
0
def HiResHDU(model):
    '''
    Construct the HDU containing the hi res image of the target.

    '''

    # Get mission cards
    cards = model._mission.HDUCards(model.meta, hdu=5)

    # Add EVEREST info
    cards.append(('COMMENT', '************************'))
    cards.append(('COMMENT', '*     EVEREST INFO     *'))
    cards.append(('COMMENT', '************************'))
    cards.append(('MISSION', model.mission, 'Mission name'))
    cards.append(('VERSION', EVEREST_MAJOR_MINOR, 'EVEREST pipeline version'))
    cards.append(('SUBVER', EVEREST_VERSION, 'EVEREST pipeline subversion'))
    cards.append(('DATE', strftime('%Y-%m-%d'),
                  'EVEREST file creation date (YYYY-MM-DD)'))

    # Create the HDU
    header = pyfits.Header(cards=cards)
    if model.hires is not None:
        hdu = pyfits.ImageHDU(
            data=model.hires, header=header, name='HI RES IMAGE')
    else:
        hdu = pyfits.ImageHDU(data=np.empty(
            (0, 0), dtype=float), header=header, name='HI RES IMAGE')
    return hdu
Exemple #2
0
def reset_cube(name, namer="-1", namev="-1"):
    import pyfits as pyf
    namef = name
    if pt.exists(name + ".fits.gz") == True:
        name = name + ".fits.gz"
    if pt.exists(name + ".fits") == True:
        name = name + ".fits"
    if pt.exists(name + ".fits.fz") == True:
        name = name + ".fits.fz"
    hdulist = pyfits.open(name)
    img = hdulist[0].data
    hd_d = hdulist[0].header
    [nz, nx, ny] = img.shape
    if namer != "-1":
        [img_e, hd_e] = gdata(namer, 0, header=True)
    else:
        img_e = np.ones([nz, nx, ny])
    if namev != "-1":
        [img_v, hd_v] = gdata(namev, 0, header=True)
    else:
        img_v = np.ones([nz, nx, ny])
    if not 'CDELT3' in hd_d:
        hd_d['CDELT3'] = hd_d['CD3_3']
    ivar_list = pyf.ImageHDU(img_e)
    mask_list = pyf.ImageHDU(img_v)
    hlist = pyf.HDUList([hdulist[0], ivar_list, mask_list, mask_list])
    hlist.update_extend()
    wfits_ext(namef + '.cube.fits', hlist)
    call = "gzip " + namef + '.cube.fits'
    sycall(call)
Exemple #3
0
    def test_extension_name_case_sensitive(self):
        """
        Tests that setting fits.EXTENSION_NAME_CASE_SENSITIVE at runtime
        works.
        """

        if 'PYFITS_EXTENSION_NAME_CASE_SENSITIVE' in os.environ:
            del os.environ['PYFITS_EXTENSION_NAME_CASE_SENSITIVE']

        hdu = fits.ImageHDU()
        hdu.name = 'sCi'
        assert hdu.name == 'SCI'
        assert hdu.header['EXTNAME'] == 'SCI'

        try:
            fits.EXTENSION_NAME_CASE_SENSITIVE = True
            hdu = fits.ImageHDU()
            hdu.name = 'sCi'
            assert hdu.name == 'sCi'
            assert hdu.header['EXTNAME'] == 'sCi'
        finally:
            fits.EXTENSION_NAME_CASE_SENSITIVE = False

        hdu.name = 'sCi'
        assert hdu.name == 'SCI'
        assert hdu.header['EXTNAME'] == 'SCI'
Exemple #4
0
	def _Hdf52Fits( dataset, hduname='' ) : 
		if (hduname in ['', None]) : 
			name = str(dataset.name)
			name = name[name.rfind('/')+1:]
		else : name = str(hduname)
		attrs = dataset.attrs.items()
		data  = dataset.value
		hdulist = []
		try : 
			if (data.dtype.name[:7] == 'complex') : 
				hdu = pyfits.ImageHDU( data.real )
				hdu.name = name+'REAL'
				hdr = hdu.header
				for i in range(len(attrs)) : 
					hdr[str(attrs[i][0])] = attrs[i][1]
				hdulist.append(hdu)  #@#@
				hdu = pyfits.ImageHDU( data.imag )
				hdu.name = name+'IMAG'
				hdr = hdu.header
				for i in range(len(attrs)) : 
					hdr[str(attrs[i][0])] = attrs[i][1]
				hdulist.append(hdu)  #@#@
			else : raise
		except : 
			if (IsType.isndarray(data) or IsType.islist(data) or IsType.istuple(data)) : 
				try : hdu = pyfits.ImageHDU( data )
				except : hdu = pyfits.ImageHDU( np.array(data, np.float32) )
				hdu.name = name
				hdr = hdu.header
				for i in range(len(attrs)) : 
					hdr[str(attrs[i][0])[:8]] = attrs[i][1]
				hdulist.append(hdu)
			else : other.header[name[:8]] = data
		return hdulist
Exemple #5
0
    def save(self, fname, overwrite=False):
        try:
            import pyfits
        except ImportError:
            import astropy.io.fits as pyfits
        import os

        if os.path.exists(fname):
            if overwrite:
                os.unlink(fname)
            else:
                raise IOError("File '%s' exists!" % fname)

        data = pyfits.PrimaryHDU(self._values)
        data.header.set('TYPE', 'Photon detection probability table')

        for k, v in self.header.items():
            # work around 8-char limit in FITS keywords
            tag = 'hierarch _i3_' + k
            data.header.set(tag, v)

        hdulist = pyfits.HDUList([data])

        if self._weights is not None:
            errors = pyfits.ImageHDU(self._weights, name='ERRORS')
            hdulist.append(errors)

        for i in range(self._values.ndim):
            edgehdu = pyfits.ImageHDU(self.bin_edges[i], name='EDGES%d' % i)
            hdulist.append(edgehdu)

        hdulist.writeto(fname)
Exemple #6
0
def compare_matrices_fits(matrix1,
                          matrix2,
                          filename,
                          fractional=False,
                          replacenan=0.):
    """write out a fits file that makes it easy to compare matrices in ds9:
    ds9 -multiframe filename.fits
    """

    if fractional:
        err = (matrix1 - matrix2) / matrix1
    else:
        err = matrix1 - matrix2

    err[np.isnan(err)] = replacenan
    #np.set_printoptions(threshold=800 * 800 * 2, precision=2)
    #print err
    #pl.imshow(np.log10(np.abs(err)))
    #pl.show()

    # write out the comparison fits
    hduf = pyfits.PrimaryHDU(err)
    hdua = pyfits.ImageHDU(matrix1)
    hdub = pyfits.ImageHDU(matrix2)
    thdulist = pyfits.HDUList([hduf, hdua, hdub])
    try:
        os.remove(filename)
        print "overwriting existing fits file: " + filename
    except OSError:
        print "writing new fits file: " + filename

    thdulist.writeto(filename)

    return err
Exemple #7
0
def complete_xtalk_matrix(data):

    # format is:
    # row, src_column, target_column
    matrix = numpy.empty((8, 8, 8))
    matrix[:, :, :] = numpy.NaN

    for src_x, row_y in itertools.product(range(8), repeat=2):
        src_cell = src_x * 10 + row_y

        # select all measurements for this source cell
        src = data[data[:, 0] == src_cell]
        print(src)

        # Now fill in the details
        matrix[row_y, src_x,
               src_x] = 1.0  # every cell contributes 100% flux to itself

        for target_x in range(8):
            _xy = target_x * 10 + row_y
            target_cell = src[:, 1] == _xy
            if (numpy.sum(target_cell) > 0):
                # computed weighted average in case there's more than one
                # measurements for this cell combination
                print(numpy.sum(src[target_cell, 2]),
                      numpy.sum(src[target_cell, 3]))

                avg_xtalk_signal = \
                    numpy.sum(src[target_cell,2] * src[target_cell,3]) \
                    / numpy.sum(src[target_cell,3])
                matrix[row_y, src_x, target_x] = avg_xtalk_signal / 65535.

    #
    # Now we are likely left with a lot of holes
    #
    for src_x, target_x in itertools.product(range(8), repeat=2):
        # find the ones we are missing
        no_data = numpy.isnan(matrix[:, src_x, target_x])
        fill_in = numpy.nanmean(matrix[~no_data, src_x, target_x])
        matrix[no_data, src_x, target_x] = fill_in

    #
    # Now that it's complete, invert it so we can easily use it later on
    #
    matrix_inverted = numpy.empty((8, 8, 8))
    for row_y in range(8):
        inverted = numpy.linalg.inv(matrix[row_y, :, :])
        #print inverted
        matrix_inverted[row_y, :, :] = inverted

    imghdu = pyfits.ImageHDU(data=matrix)
    imghdu.name = "XTALK.FWD"
    imghdu2 = pyfits.ImageHDU(data=matrix_inverted)
    imghdu2.name = "XTALK.INV"
    p_hdu = pyfits.PrimaryHDU()
    hdulist = pyfits.HDUList([p_hdu, imghdu, imghdu2])
    hdulist.writeto("matrix_inv.fits", clobber=True)

    return matrix
Exemple #8
0
 def writeFiles(self):
     """  Writes the data variable into a fits file """
     if os.path.isfile(self.output):
         os.remove(self.output)
     hdu = pyfits.HDUList()
     hdu.append(pyfits.PrimaryHDU(header=self.headprim))
     hdu.append(pyfits.ImageHDU(data=self.data, header=self.head))
     hdu.append(pyfits.ImageHDU(data=self.erro, header=self.headerro))
     hdu.writeto(self.output)
Exemple #9
0
    def write(self, dirName=".", fileName=None):
        if not pyfits:
            raise RuntimeError("I failed to import pyfits, so cannot read from disk")

        hdus = pyfits.HDUList()

        hdr = pyfits.Header()

        for k in sorted(self._metadata):
            if len(k) <= 8:
                kk = k
            else:
                kk = "HIERARCH " + k    # avoid warning
            hdr[kk] = self._metadata[k]

        hdr.update()
        hdus.append(pyfits.PrimaryHDU(header=hdr))

        hdu = pyfits.ImageHDU(self.flux)
        hdu.name = "FLUX"
        hdus.append(hdu)

        hdu = pyfits.ImageHDU(self.covar)
        hdu.name = "COVAR"
        hdus.append(hdu)

        hdu = pyfits.ImageHDU(self.mask)
        hdu.name = "MASK"
        hdus.append(hdu)

        hdu = pyfits.ImageHDU(self.lam)
        hdu.name = "WAVELENGTH"
        hdus.append(hdu)

        hdu = pyfits.ImageHDU(self.sky)
        hdu.name = "SKY"
        hdus.append(hdu)

        hdu = pyfits.BinTableHDU.from_columns([
            pyfits.Column(name='pfsConfigId', format='K',
                          array=np.array([self.pfsConfigId], dtype=np.int64)),
            pyfits.Column(name='visit', format='J',
                          array=np.array([self.visit], dtype=np.int32))
        ])

        hdu.name = 'CONFIG'
        hdus.append(hdu)

        # clobber=True in writeto prints a message, so use open instead
        if fileName is None:
            fileName = self.fileNameFormat % (self.visit, self.arm, self.spectrograph)
        with open(os.path.join(dirName, fileName), "w") as fd:
            hdus.writeto(fd)
Exemple #10
0
 def save2fits(self, fn):
     gauss = self.gaussprofile(self.r)
     moffat_model = self.moffat(self.r)
     out_list = [
         pyfits.PrimaryHDU(),
         pyfits.ImageHDU(data=self.data, name="DATA"),
         pyfits.ImageHDU(data=gauss, name="GAUSS"),
         pyfits.ImageHDU(data=moffat_model, name="MOFFAT"),
         pyfits.ImageHDU(data=(self.data - gauss), name="GAUSS_RESIDUALS"),
         pyfits.ImageHDU(data=(self.data - moffat_model),
                         name="MOFFAT_RESIDUALS"),
     ]
     hdulist = pyfits.HDUList(out_list)
     hdulist.writeto(fn, clobber=True)
Exemple #11
0
def write(table, path):
    """
	Write a SplineTable to disk as a FITS file.
	
	:param table: the SplineTable to be written
	:param path: where to save the FITS file.
	
	.. warning:: pyfits will fail to write the file if it already exists.
	"""
    data = pyfits.PrimaryHDU(table.coefficients)
    data.header.set('TYPE', 'Spline Coefficient Table')

    if getattr(table.order, '__iter__', False):
        for i in range(0, len(table.order)):
            data.header.set('ORDER%d' % i, table.order[i], 'B-Spline Order')
    else:
        data.header.set('ORDER', table.order, 'B-Spline Order')

    for i in range(0, len(table.periods)):
        data.header.set('PERIOD%d' % i, table.periods[i])

    base_keys = set(['coefficients', 'order', 'knots', 'extents', 'periods'])
    for k in dir(table):
        if k.startswith('_') or k in base_keys:
            continue
        data.header.set(k.upper(), getattr(table, k))

    hdulist = pyfits.HDUList([data])

    for i in range(0, len(table.knots)):
        knothdu = pyfits.ImageHDU(table.knots[i], name='KNOTS%d' % i)
        hdulist.append(knothdu)

    extents = []
    for i in range(0, len(table.knots)):
        if getattr(table.order, '__iter__', False):
            order = table.order[i]
        else:
            order = table.order

        if i < len(table.extents):
            ext = list(table.extents[i])
        else:
            ext = [table.knots[i][order], table.knots[i][-order - 1]]
        extents += ext
    extenthdu = pyfits.ImageHDU(numpy.array(extents, dtype=float),
                                name='EXTENTS')
    hdulist.append(extenthdu)

    hdulist.writeto(path)
Exemple #12
0
def ApertureHDU(model):
    '''
    Construct the HDU containing the aperture used to de-trend.

    '''

    # Get mission cards
    cards = model._mission.HDUCards(model.meta, hdu=3)

    # Add EVEREST info
    cards.append(('COMMENT', '************************'))
    cards.append(('COMMENT', '*     EVEREST INFO     *'))
    cards.append(('COMMENT', '************************'))
    cards.append(('MISSION', model.mission, 'Mission name'))
    cards.append(('VERSION', EVEREST_MAJOR_MINOR, 'EVEREST pipeline version'))
    cards.append(('SUBVER', EVEREST_VERSION, 'EVEREST pipeline subversion'))
    cards.append(('DATE', strftime('%Y-%m-%d'),
                  'EVEREST file creation date (YYYY-MM-DD)'))

    # Create the HDU
    header = pyfits.Header(cards=cards)
    hdu = pyfits.ImageHDU(data=model.aperture,
                          header=header, name='APERTURE MASK')

    return hdu
Exemple #13
0
def fixpix(fs=None):
    iraf.cd('work')
    if fs is None:
        fs = glob('nrm/sci*nrm*.fits')
    if len(fs) == 0:
        print "WARNING: No rectified images to fix."
        iraf.cd('..')
        return
    if not os.path.exists('fix'):
        os.mkdir('fix')
    for f in fs:
        outname = f.replace('nrm', 'fix')
        # Copy the file to the fix directory
        shutil.copy(f, outname)
        # Set all of the BPM pixels = 0
        h = pyfits.open(outname, mode='update')
        h['SCI'].data[h['BPM'].data == 1] = 0
        # Grab the CRM extension from the lax file
        laxhdu = pyfits.open(f.replace('nrm', 'lax'))
        h.append(pyfits.ImageHDU(data=laxhdu['CRM'].data.copy(),
                                 header=laxhdu['CRM'].header.copy(),
                                 name='CRM'))
        h.flush()
        h.close()
        laxhdu.close()

        # Run iraf's fixpix on the cosmic rays, not ideal,
        # but better than nothing because apall doesn't take a bad pixel mask
        iraf.unlearn(iraf.fixpix)
        iraf.flpr()
        iraf.fixpix(outname + '[SCI]', outname + '[CRM]', mode='hl')
    iraf.cd('..')
Exemple #14
0
def save_cube(var_cube,exposure_hdr,varfilename,clobber=False,verbose=True):
    """
    Saving variance cube to fits file

    --- INPUT ---

    --- EXAMPLE OF USE ---
    import MUSEWideGenerateNoiseCubes as mgnc



    """
    if verbose: print ' - Saving variance cube to \n   '+varfilename
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if verbose: print ' - Using header provided as header template'
    removekeys = ['EXTNAME','CHECKSUM','DATASUM','SCIDATA','HDUCLASS','HDUDOC','HDUVERS','HDUCLAS1']
    for key in removekeys:
        if key in exposure_hdr.keys():
            exposure_hdr.remove(key)

    # writing hdrkeys:   '---KEY--',              '----------------MAX LENGTH COMMENT-------------'
    exposure_hdr.append(('EXTNAME ','VARCUBE'    ,'Varinace cube from MUSEWideGenerateNoiseCubes()'),end=True)
    exposure_hdr['OBJECT'] = exposure_hdr['OBJECT'].replace('EXP','VARCUBE')

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if 'XTENSION' in exposure_hdr.keys():
        hduprim        = pyfits.PrimaryHDU()  # default HDU with default minimal header
        hducube        = pyfits.ImageHDU(var_cube,header=exposure_hdr)
        hdus           = [hduprim,hducube]
    else:
        hducube = pyfits.PrimaryHDU(var_cube,header=exposure_hdr)
        hdus           = [hducube]
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    hdulist = pyfits.HDUList(hdus)       # turn header into to hdulist
    hdulist.writeto(varfilename,clobber=clobber)  # write fits file (clobber=True overwrites excisting file)
Exemple #15
0
    def test_compressed_image_data_float32(self):
        n = np.arange(100, dtype='float32')
        hdu = fits.ImageHDU(n)
        comp_hdu = fits.CompImageHDU(hdu.data, hdu.header)
        comp_hdu.writeto(self.temp('tmp.fits'), checksum=True)
        hdu.writeto(self.temp('uncomp.fits'), checksum=True)
        with fits.open(self.temp('tmp.fits'), checksum=True) as hdul:
            assert np.all(hdul[1].data == comp_hdu.data)
            assert np.all(hdul[1].data == hdu.data)
            assert 'CHECKSUM' in hdul[0].header
            assert hdul[0].header['CHECKSUM'] == 'D8iBD6ZAD6fAD6ZA'
            assert 'DATASUM' in hdul[0].header
            assert hdul[0].header['DATASUM'] == '0'

            assert 'CHECKSUM' in hdul[1].header
            assert 'DATASUM' in hdul[1].header
            assert 'CHECKSUM' in hdul[1].header
            if sys.platform != 'win32':
                # The checksum ends up being different on Windows, possibly due
                # to slight floating point differences
                # TODO: In Astropy mark these properly as known fail
                assert hdul[1]._header['CHECKSUM'] == 'eATIf3SHe9SHe9SH'
                assert hdul[1]._header['DATASUM'] == '1277667818'

            with fits.open(self.temp('uncomp.fits'), checksum=True) as hdul2:
                header_comp = hdul[1]._header
                header_uncomp = hdul2[1].header
                assert 'ZHECKSUM' in header_comp
                assert 'CHECKSUM' in header_uncomp
                assert header_uncomp['CHECKSUM'] == 'Cgr5FZo2Cdo2CZo2'
                assert header_comp['ZHECKSUM'] == header_uncomp['CHECKSUM']
                assert 'ZDATASUM' in header_comp
                assert 'DATASUM' in header_uncomp
                assert header_uncomp['DATASUM'] == '2393636889'
                assert header_comp['ZDATASUM'] == header_uncomp['DATASUM']
def printWCS ():

    otalist = [pyfits.PrimaryHDU()]

    cx, cy = 5,5
    for yy in range(0, 8):
        for xx in range(0, 8):
            imghdu = pyfits.ImageHDU()
            
            imghdu.header['WCSASTRM'] = 'baseline wcs D. Harbeck Jan 2015'
            imghdu.header['CTYPE1'] = 'RA---TAN'
            imghdu.header['CTYPE2'] = 'DEC--TAN'
            imghdu.header['CRVAL1'] = 0.0
            imghdu.header['CRVAL2'] = 0.0
            imghdu.header['CUNIT1']  = ('deg     ', 'Axis unit')
            imghdu.header['CUNIT2']  = ('deg     ', 'Axis unit')
            imghdu.header['CD1_1'] = 3.11588947907302E-5
            imghdu.header['CD2_2'] = 3.11503811254695E-5
            imghdu.header['CD1_2'] = -4.9413159234116E-8
            imghdu.header['CD2_1'] = -3.9298844602068E-8

            imghdu.header['CRPIX1'] = -4222.7484375 * xx + 15973.0199279 - 508.0 * cx
            imghdu.header['CRPIX2'] = -4315.55125 * yy + 12702.5552885 - 505.0 * cy

            imghdu.name = "OTA%d%d.SCI" % (xx,yy)
            
            otalist.append(imghdu)

    return otalist
Exemple #17
0
    def test_compressed_image_data_int16(self):
        n = np.arange(100, dtype='int16')
        hdu = fits.ImageHDU(n)
        comp_hdu = fits.CompImageHDU(hdu.data, hdu.header)
        comp_hdu.writeto(self.temp('tmp.fits'), checksum=True)
        hdu.writeto(self.temp('uncomp.fits'), checksum=True)
        with fits.open(self.temp('tmp.fits'), checksum=True) as hdul:
            assert np.all(hdul[1].data == comp_hdu.data)
            assert np.all(hdul[1].data == hdu.data)
            assert 'CHECKSUM' in hdul[0].header
            assert hdul[0].header['CHECKSUM'] == 'D8iBD6ZAD6fAD6ZA'
            assert 'DATASUM' in hdul[0].header
            assert hdul[0].header['DATASUM'] == '0'

            assert 'CHECKSUM' in hdul[1].header
            assert hdul[1]._header['CHECKSUM'] == 'J5cCJ5c9J5cAJ5c9'
            assert 'DATASUM' in hdul[1].header
            assert hdul[1]._header['DATASUM'] == '2453673070'
            assert 'CHECKSUM' in hdul[1].header

            with fits.open(self.temp('uncomp.fits'), checksum=True) as hdul2:
                header_comp = hdul[1]._header
                header_uncomp = hdul2[1].header
                assert 'ZHECKSUM' in header_comp
                assert 'CHECKSUM' in header_uncomp
                assert header_uncomp['CHECKSUM'] == 'ZE94eE91ZE91bE91'
                assert header_comp['ZHECKSUM'] == header_uncomp['CHECKSUM']
                assert 'ZDATASUM' in header_comp
                assert 'DATASUM' in header_uncomp
                assert header_uncomp['DATASUM'] == '160565700'
                assert header_comp['ZDATASUM'] == header_uncomp['DATASUM']
Exemple #18
0
def dumpSpectrum(outname, y, hdr=None, **kwargs):
    """Dump array y as spectrum."""

    phdu = pyfits.PrimaryHDU(y, header=hdr)
    if hdr.get('EXTNAME') == 'E3D_DATA': # E3d cube header
        del phdu.header['EXTNAME']
        phdu.header.update('CRVAL1', hdr['CRVALS'], after='NAXIS1')
        phdu.header.update('CDELT1', hdr['CDELTS'], after='CRVAL1')
        phdu.header.update('CRPIX1', hdr['CRPIXS'], after='CDELT1')
    elif 'CRVAL3' in hdr:               # Fits 3D cube header
        phdu.header.update('CRVAL1', hdr['CRVAL3'], after='NAXIS1')
        phdu.header.update('CDELT1', hdr['CDELT3'], after='CRVAL1')
        phdu.header.update('CRPIX1', hdr['CRPIX3'], after='CDELT1')
    hdulist = pyfits.HDUList([phdu])
    # Add extension(s) if any
    for key,val in kwargs.iteritems():
        if isinstance(val,N.ndarray):   # Add extension
            assert val.shape == y.shape
            ima = pyfits.ImageHDU(val, name=key.upper())
            ima.header.update('CRVAL1', phdu.header['CRVAL1'])
            ima.header.update('CDELT1', phdu.header['CDELT1'])
            ima.header.update('CRPIX1', phdu.header['CRPIX1'])
            hdulist.append(ima)
        else:                           # Add keyword
            try:
                value,comment = val     # (value,comment)
            except:
                value,comment = val,None
            phdu.header.update(key.upper(), value, comment=comment)
    hdulist.writeto(outname, clobber=True)
Exemple #19
0
    def test_compressed_image_data_float32(self):
        n = np.arange(100, dtype='float32')
        hdu = fits.ImageHDU(n)
        comp_hdu = fits.CompImageHDU(hdu.data, hdu.header)
        comp_hdu.writeto(self.temp('tmp.fits'), checksum=True)
        hdu.writeto(self.temp('uncomp.fits'), checksum=True)
        with fits.open(self.temp('tmp.fits'), checksum=True) as hdul:
            assert np.all(hdul[1].data == comp_hdu.data)
            assert np.all(hdul[1].data == hdu.data)
            assert 'CHECKSUM' in hdul[0].header
            assert hdul[0].header['CHECKSUM'] == 'D8iBD6ZAD6fAD6ZA'
            assert 'DATASUM' in hdul[0].header
            assert hdul[0].header['DATASUM'] == '0'

            assert 'CHECKSUM' in hdul[1].header
            assert 'DATASUM' in hdul[1].header

            if not sys.platform.startswith('win32'):
                assert hdul[1]._header['CHECKSUM'] == 'eATIf3SHe9SHe9SH'
                assert hdul[1]._header['DATASUM'] == '1277667818'

            with fits.open(self.temp('uncomp.fits'), checksum=True) as hdul2:
                header_comp = hdul[1]._header
                header_uncomp = hdul2[1].header
                assert 'ZHECKSUM' in header_comp
                assert 'CHECKSUM' in header_uncomp
                assert header_uncomp['CHECKSUM'] == 'Cgr5FZo2Cdo2CZo2'
                assert header_comp['ZHECKSUM'] == header_uncomp['CHECKSUM']
                assert 'ZDATASUM' in header_comp
                assert 'DATASUM' in header_uncomp
                assert header_uncomp['DATASUM'] == '2393636889'
                assert header_comp['ZDATASUM'] == header_uncomp['DATASUM']
Exemple #20
0
 def test_fix_missing_card_append(self):
     hdu = fits.ImageHDU()
     errs = hdu.req_cards('TESTKW', None, None, 'foo', 'silentfix', [])
     assert len(errs) == 1
     assert 'TESTKW' in hdu.header
     assert hdu.header['TESTKW'] == 'foo'
     assert hdu.header.ascard[-1].key == 'TESTKW'
Exemple #21
0
def mergeQuadrants(input):
    """Merge quadrants into CCD image."""
    import pyfits

    output = input.replace('.fit', '_ccd.fit')

    if os.access(output, os.F_OK):
        return

    logging.info('merging quadrants into CCD image (%s)' % input)

    fh = pyfits.open(input)
    hdulist = [fh[0]]

    for i in range(36):
        logging.debug('CCD #%d' % (i + 1))
        ccd = pyfits.ImageHDU(data=numpy.zeros([4132,
                                                4096]).astype(numpy.float32),
                              header=fh[i * 4 + 1].header)
        ccd.data[0:2066, 0:2048] = fh[4 * i + 1].data.astype(numpy.float32)
        ccd.data[0:2066, 2048:4096] = fh[4 * i + 2].data.astype(numpy.float32)
        ccd.data[2066:4132, 0:2048] = fh[4 * i + 3].data.astype(numpy.float32)
        ccd.data[2066:4132,
                 2048:4096] = fh[4 * i + 4].data.astype(numpy.float32)
        hdulist += [ccd]

    nfh = pyfits.HDUList(hdulist)

    logging.info('writing CCD image to disk (%s)' % output)

    nfh.writeto(output)
Exemple #22
0
    def test_extname(self):
        """Test getting/setting the EXTNAME of an HDU."""

        h1 = fits.PrimaryHDU()
        assert h1.name == 'PRIMARY'
        # Normally a PRIMARY HDU should not have an EXTNAME, though it should
        # have a default .name attribute
        assert 'EXTNAME' not in h1.header

        # The current version of the FITS standard does allow PRIMARY HDUs to
        # have an EXTNAME, however.
        h1.name = 'NOTREAL'
        assert h1.name == 'NOTREAL'
        assert h1.header.get('EXTNAME') == 'NOTREAL'

        # Updating the EXTNAME in the header should update the .name
        h1.header['EXTNAME'] = 'TOOREAL'
        assert h1.name == 'TOOREAL'

        # If we delete an EXTNAME keyword from a PRIMARY HDU it should go back
        # to the default
        del h1.header['EXTNAME']
        assert h1.name == 'PRIMARY'

        # For extension HDUs the situation is a bit simpler:
        h2 = fits.ImageHDU()
        assert h2.name == ''
        assert 'EXTNAME' not in h2.header
        h2.name = 'HELLO'
        assert h2.name == 'HELLO'
        assert h2.header.get('EXTNAME') == 'HELLO'
        h2.header['EXTNAME'] = 'GOODBYE'
        assert h2.name == 'GOODBYE'
Exemple #23
0
def writeFITSfile(data, output):
    """
    Writes data to a FITS file.

    :param data: input data to be written
    :type data: ndarray
    :param output: name of the output file
    :type output: str

    :return None
    """
    if os.path.isfile(output):
        os.remove(output)

    #create a new FITS file, using HDUList instance
    ofd = pf.HDUList(pf.PrimaryHDU())

    #new image HDU
    hdu = pf.ImageHDU(data=data)

    #update and verify the header
    hdu.header.add_history(
        'This file was created at %s' %
        datetime.datetime.isoformat(datetime.datetime.now()))
    hdu.verify('fix')

    ofd.append(hdu)

    #write the actual file
    ofd.writeto(output)
Exemple #24
0
    def writeFITS(self, data, output):
        """
        Writes given imaging data to a FITS file.

        :param data: image array
        :type data: ndarray
        :param output: name of the output file
        :type output: str

        :return: None
        """
        self.log.info('Writing data to a FITS file %s' % output)
        if os.path.isfile(output):
            os.remove(output)

        #create a new FITS file, using HDUList instance
        ofd = pf.HDUList(pf.PrimaryHDU())

        #new image HDU
        hdu = pf.ImageHDU(data=data)

        #add info
        for key, value in self.settings.iteritems():
            hdu.header.update(key.upper(), value)

        hdu.header.add_history('If questions, please contact Sami-Matias Niemi (smn2 at mssl.ucl.ac.uk).')
        hdu.header.add_history('This file has been created with the VISsim Python Package at %s' % datetime.datetime.isoformat(datetime.datetime.now()))
        hdu.verify('fix')

        ofd.append(hdu)

        #write the actual file
        ofd.writeto(output)
Exemple #25
0
    def writeFITS(self, data, output):
        """
        Write out a FITS file using PyFITS.

        :param data: data to write to a FITS file
        :type data: ndarray
        :param output: name of the output file
        :type output: string

        :return: None
        """
        if os.path.isfile(output):
            os.remove(output)

        #create a new FITS file, using HDUList instance
        ofd = pf.HDUList(pf.PrimaryHDU())

        #new image HDU
        hdu = pf.ImageHDU(data=data)

        #update and verify the header
        hdu.header.add_history(
            'If questions, please contact Sami-Matias Niemi (s.niemi at ucl.ac.uk).'
        )
        hdu.header.add_history('This file has been created with the VISsim Python Package at %s' \
                               % datetime.datetime.isoformat(datetime.datetime.now()))
        hdu.verify('fix')

        ofd.append(hdu)

        #write the actual file
        ofd.writeto(output)
        self.log.info('Wrote %s' % output)
 def write(self, outfile, clobber=True):
     output = pyfits.HDUList()
     output.append(pyfits.PrimaryHDU())
     for i, screen in enumerate(self.screens):
         output.append(pyfits.ImageHDU(np.array(screen)))
         output[-1].name = "Layer %i" % i
     output.writeto(outfile, clobber=clobber)
Exemple #27
0
def make_master_bias(filelist):

    datablocks = []
    for fn in filelist:

        valid_hdu, extras = reduce_sdsspt.reduce_sdss(
            fn,
            overscan=True,
            trim=True,
            subtract_bias=False,
            correct_flat=False,
        )
        if (valid_hdu is None):
            continue

        # valid_hdu.info()
        datablocks.append(valid_hdu['SCI'].data)

    # print datablocks

    masterbias = podi_imcombine.imcombine_data(
        datas=datablocks,
        operation='sigmaclipmean'
    )

    masterbias_hdu = pyfits.HDUList([
        pyfits.PrimaryHDU(),
        pyfits.ImageHDU(
            data=masterbias,
            name='SCI',
    )])

    return masterbias_hdu
Exemple #28
0
    def test_hdulist_file_info(self):
        hdul = fits.open(self.data('checksum.fits'))
        res = hdul.fileinfo(0)

        def test_fileinfo(**kwargs):
            assert res['datSpan'] == kwargs.get('datSpan', 2880)
            assert res['resized'] == kwargs.get('resized', False)
            assert res['filename'] == self.data('checksum.fits')
            assert res['datLoc'] == kwargs.get('datLoc', 8640)
            assert res['hdrLoc'] == kwargs.get('hdrLoc', 0)
            assert res['filemode'] == 'readonly'

        res = hdul.fileinfo(1)
        test_fileinfo(datLoc=17280, hdrLoc=11520)

        hdu = fits.ImageHDU(data=hdul[0].data)
        hdul.insert(1, hdu)

        res = hdul.fileinfo(0)
        test_fileinfo(resized=True)

        res = hdul.fileinfo(1)
        test_fileinfo(datSpan=None, resized=True, datLoc=None, hdrLoc=None)

        res = hdul.fileinfo(2)
        test_fileinfo(resized=1, datLoc=17280, hdrLoc=11520)
Exemple #29
0
    def test_update_resized_header2(self):
        """
        Regression test for https://trac.assembla.com/pyfits/ticket/150

        This is similar to test_update_resized_header, but specifically tests a
        case of multiple consecutive flush() calls on the same HDUList object,
        where each flush() requires a resize.
        """

        data1 = np.arange(100)
        data2 = np.arange(100) + 100
        phdu = fits.PrimaryHDU(data=data1)
        hdu = fits.ImageHDU(data=data2)

        phdu.writeto(self.temp('temp.fits'))

        with fits.open(self.temp('temp.fits'), mode='append') as hdul:
            hdul.append(hdu)

        with fits.open(self.temp('temp.fits'), mode='update') as hdul:
            idx = 1
            while len(str(hdul[0].header)) <= 2880 * 2:
                hdul[0].header['TEST%d' % idx] = idx
                idx += 1
            hdul.flush()
            hdul.append(hdu)

        with fits.open(self.temp('temp.fits')) as hdul:
            assert (hdul[0].data == data1).all()
            assert hdul[1].header == hdu.header
            assert (hdul[1].data == data2).all()
            assert (hdul[2].data == data2).all()
Exemple #30
0
	def write_files(self,pat=None):
		"""Write channels to files. Use pattern to rename file."""
		rex = None
		if pat:
			rex = re.compile(r'(%[f])')
			rex_a = re.compile(r'(@(?:{[^- @}.]+}|[^- @.{}]+))')

		for i in range(0,len(self.filenames)):
			fn = self.filenames[i]
			if self.verbose:
				print fn,
			if rex:
				class match_o:
					def __init__(self,fn):
						self.fn = fn
						self.fp = None
					def p_match(self,m):
						if m.group(0)[1] == 'f':
							return os.path.splitext(os.path.basename(self.fn))[0]
					def a_match(self,m):
						if not(self.fp):
							self.fp = pyfits.open(self.fn)
						return self.fp.header[m.group(0)[1:]]

				ma = match_o(fn)

				fn = rex.sub(ma.p_match,pat)
				fn = rex_a.sub(ma.a_match,fn)
 
			if self.verbose:
				print '->',fn,
				sys.stdout.flush()

			try:
				os.unlink(fn)
			except OSError,ose:
				pass
			f = pyfits.open(fn,'append')

			ph = pyfits.PrimaryHDU()
			for k in self.phu:
				ph.header.update(k,self.phu[k])

			for h in self.history:
				ph.header.add_history(h)

			f.append(ph)

			for c in self.channels:
				h = pyfits.ImageHDU(data=c.data[i])
				h.header.update('EXTNAME',c.name)
				ak = c.headers.keys()
				ak.sort()
				for k in ak:
					h.header.update(k,c.headers[k])
				f.append(h)
			f.close()
			if self.verbose:
				print