Ejemplo n.º 1
0
def harmonic_sphere_map_from_fits(filename, extno=1, fitsformat='healpix', dtype=np.cdouble):
    if fitsformat != 'healpix':
        raise NotImplementedError()
    import pyfits
    hdulist = pyfits.open(filename)
    try:
        ext = hdulist[extno]
        lmin = ext.header['MIN-LPOL']
        lmax = ext.header['MAX-LPOL']

        out = harmonic_sphere_map(0, lmin, lmax, is_complex=True)
        ls = broadcast_l_to_lm(np.arange(lmin, lmax + 1), lmin)
        ms = m_range_lm(lmin, lmax)
        running_index = ls**2 + ls + ms + 1
        index = ext.data.field(0).ravel()
        alm_real = ext.data.field(1).ravel()
        alm_imag = ext.data.field(2).ravel()
        # If Cythonizing this we can make it fast anyway and loose the special case
        if np.all(running_index == index):
            out.real = alm_real
            out.imag = alm_imag
        else:
            raise NotImplementedError('Too lazy to implement non-contiguous indices')
    finally:
        hdulist.close()
    return out
Ejemplo n.º 2
0
    def to_fits(self, filename, fitsformat='healpix', dataformat=None):
        if self.format is not COMPLEX_BRIEF:
            self.to_complex().to_fits(filename, fitsformat=fitsformat, dataformat=dataformat)
            return
        if fitsformat != 'healpix':
            raise NotImplementedError()
        if dataformat is None:
            if self.dtype == np.cdouble:
                dataformat = 'D'
            elif self.dtype == np.csingle:
                dataformat = 'E'
            else:
                raise NotImplementedError()
            
        import pyfits
        import os
        if self.ndim != 1:
            raise NotImplementedError()
        if os.path.exists(filename):
            os.unlink(filename)

        ls = broadcast_l_to_lm(np.arange(self.lmin, self.lmax + 1), self.lmin)
        ms = m_range_lm(self.lmin, self.lmax)
        index = ls**2 + ls + ms + 1

        cols = pyfits.ColDefs([
            pyfits.Column(name='index=l^2+l+m+1', array=index, format='J'), # 4-byte int
            pyfits.Column(name='alm (real)', array=self.real, format=dataformat),
            pyfits.Column(name='alm (imaginary)', array=self.imag, format=dataformat)])

        tab = pyfits.new_table(cols)
        tab.header.update('MIN-LPOL', self.lmin)
        tab.header.update('MAX-LPOL', self.lmax)
        tab.header.update('MAX-MPOL', self.lmax)

        pyfits.HDUList([pyfits.PrimaryHDU(), tab]).writeto(filename)