Exemple #1
0
def write_alm(filename, alms, out_dtype=None, lmax=-1, mmax=-1, mmax_in=-1):
    """
    Write alms to a fits file. In the fits file the alms are written 
    with explicit index scheme, index = l*l + l + m +1, possibly out of order.
    By default write_alm makes a table with the same precision as the alms.
    If specified, the lmax and mmax parameters truncate the input data to
    include only alms for which l <= lmax and m <= mmax.

    Input:
      - filename: the filename of the output fits file
      - alms: a complex array holding the alms.

    Parameters:
     - lmax: maximum l in the output file
     - mmax: maximum m in the output file
     - out_dtype: data type in the output file (must be a numpy dtype)
     - mmax_in: maximum m in the input array
    """

    l2max = Alm.getlmax(len(alms), mmax=mmax_in)
    if lmax != -1 and lmax > l2max:
        raise ValueError("Too big lmax in parameter")
    elif lmax == -1:
        lmax = l2max

    if mmax_in == -1:
        mmax_in = l2max

    if mmax == -1:
        mmax = lmax
    if mmax > mmax_in:
        mmax = mmax_in

    if out_dtype == None:
        out_dtype = alms.real.dtype

    l, m = Alm.getlm(lmax)
    idx = npy.where((l <= lmax) * (m <= mmax))
    l = l[idx]
    m = m[idx]

    idx_in_original = Alm.getidx(l2max, l=l, m=m)

    index = l ** 2 + l + m + 1

    out_data = npy.empty(len(index), dtype=[("index", "i"), ("real", out_dtype), ("imag", out_dtype)])
    out_data["index"] = index
    out_data["real"] = alms.real[idx_in_original]
    out_data["imag"] = alms.imag[idx_in_original]

    cindex = pyf.Column(name="index", format=getformat(npy.int32), unit="l*l+l+m+1", array=out_data["index"])
    creal = pyf.Column(name="real", format=getformat(out_dtype), unit="unknown", array=out_data["real"])
    cimag = pyf.Column(name="imag", format=getformat(out_dtype), unit="unknown", array=out_data["imag"])

    coldefs = pyf.ColDefs([cindex, creal, cimag])
    tbhdu = pyf.new_table(coldefs)
    tbhdu.writeto(filename, clobber=True)
Exemple #2
0
def read_alm(filename, hdu=1, return_mmax=False):
    """Read alm from a fits file. In the fits file, the alm are written
    with explicit index scheme, index = l**2+l+m+1, while healpix cxx
    uses index = m*(2*lmax+1-m)/2+l. The conversion is done in this 
    function.

    Input:
      - filename: the name of the fits file to read

    Parameters:
      - hdu: the header to read. Start at 0. Default: hdu=1
      - return_mmax: If true, both the alms and mmax is returned in a tuple. Default: return_mmax=False

    Return:
      - alms: if return_mmax=False
      - alms,mmax: if return_mmax=True
    """
    idx, almr, almi = mrdfits(filename, hdu=hdu)
    l = npy.floor(npy.sqrt(idx - 1)).astype(long)
    m = idx - l ** 2 - l - 1
    if (m < 0).any():
        raise ValueError("Negative m value encountered !")
    lmax = l.max()
    mmax = m.max()
    alm = almr * (0 + 0j)
    i = Alm.getidx(lmax, l, m)
    alm.real[i] = almr
    alm.imag[i] = almi
    if return_mmax:
        return alm, mmax
    else:
        return alm
Exemple #3
0
def read_alm(filename, hdu=1, return_mmax=False):
    """Read alm from a fits file. In the fits file, the alm are written
    with explicit index scheme, index = l**2+l+m+1, while healpix cxx
    uses index = m*(2*lmax+1-m)/2+l. The conversion is done in this 
    function.

    Input:
      - filename: the name of the fits file to read

    Parameters:
      - hdu: the header to read. Start at 0. Default: hdu=1
      - return_mmax: If true, both the alms and mmax is returned in a tuple. Default: return_mmax=False

    Return:
      - alms: if return_mmax=False
      - alms,mmax: if return_mmax=True
    """
    idx, almr, almi = mrdfits(filename, hdu=hdu)
    l = npy.floor(npy.sqrt(idx - 1)).astype(long)
    m = idx - l**2 - l - 1
    if (m < 0).any():
        raise ValueError('Negative m value encountered !')
    lmax = l.max()
    mmax = m.max()
    alm = almr * (0 + 0j)
    i = Alm.getidx(lmax, l, m)
    alm.real[i] = almr
    alm.imag[i] = almi
    if return_mmax:
        return alm, mmax
    else:
        return alm
Exemple #4
0
def read_alm(filename, hdu=1, return_mmax=False):
    """Read alm from a fits file. In the fits file, the alm are written
    with explicit index scheme, index = l**2+l+m+1, while healpix cxx
    uses index = m*(2*lmax+1-m)/2+l. The conversion is done in this 
    function.

    Parameters
    ----------
    filename : str
      The name of the fits file to read
    hdu : int, optional
      The header to read. Start at 0. Default: hdu=1
    return_mmax : bool, optional
      If true, both the alms and mmax is returned in a tuple. Default: return_mmax=False

    Returns
    -------
    alms[, mmax] : complex array or tuple of a complex array and an int
      The alms read from the file and optionally mmax read from the file
    """
    idx, almr, almi = mrdfits(filename, hdu=hdu)
    l = npy.floor(npy.sqrt(idx - 1)).astype(long)
    m = idx - l**2 - l - 1
    if (m < 0).any():
        raise ValueError('Negative m value encountered !')
    lmax = l.max()
    mmax = m.max()
    alm = almr * (0 + 0j)
    i = Alm.getidx(lmax, l, m)
    alm.real[i] = almr
    alm.imag[i] = almi
    if return_mmax:
        return alm, mmax
    else:
        return alm
Exemple #5
0
def read_alm(filename,hdu=1,return_mmax=False):
    """Read alm from a fits file. In the fits file, the alm are written
    with explicit index scheme, index = l**2+l+m+1, while healpix cxx
    uses index = m*(2*lmax+1-m)/2+l. The conversion is done in this 
    function.

    Parameters
    ----------
    filename : str
      The name of the fits file to read
    hdu : int, optional
      The header to read. Start at 0. Default: hdu=1
    return_mmax : bool, optional
      If true, both the alms and mmax is returned in a tuple. Default: return_mmax=False

    Returns
    -------
    alms[, mmax] : complex array or tuple of a complex array and an int
      The alms read from the file and optionally mmax read from the file
    """
    idx, almr, almi = mrdfits(filename,hdu=hdu)
    l = npy.floor(npy.sqrt(idx-1)).astype(long)
    m = idx - l**2 - l - 1
    if (m<0).any():
        raise ValueError('Negative m value encountered !')
    lmax = l.max()
    mmax = m.max()
    alm = almr*(0+0j)
    i = Alm.getidx(lmax,l,m)
    alm.real[i] = almr
    alm.imag[i] = almi
    if return_mmax:
        return alm, mmax
    else:
        return alm
Exemple #6
0
def read_alm(filename,hdu=1):
    """Read alm from a fits file. In the fits file, the alm are written
    with explicit index scheme, index = l**2+l+m+1, while healpix cxx
    uses index = m*(2*lmax+1-m)/2+l. The conversion is done in this 
    function.
    """
    idx, almr, almi = mrdfits(filename,hdu=hdu)
    l = npy.floor(npy.sqrt(idx-1)).astype(long)
    m = idx - l**2 - l - 1
    if (m<0).any():
        raise ValueError('Negative m value encountered !')
    lmax = l.max()
    mmax = m.max()
    alm = almr*(0+0j)
    i = Alm.getidx(lmax,l,m)
    alm.real[i] = almr
    alm.imag[i] = almi
    return alm
Exemple #7
0
def write_alm(filename,alms,out_dtype=None,lmax=-1,mmax=-1,mmax_in=-1):
    """Write alms to a fits file. 
    
    In the fits file the alms are written 
    with explicit index scheme, index = l*l + l + m +1, possibly out of order.
    By default write_alm makes a table with the same precision as the alms.
    If specified, the lmax and mmax parameters truncate the input data to
    include only alms for which l <= lmax and m <= mmax.

    Parameters
    ----------
    filename : str
      The filename of the output fits file
    alms : array, complex
      A complex ndarray holding the alms.
    lmax : int, optional
      The maximum l in the output file
    mmax : int, optional
      The maximum m in the output file
    out_dtype : data type, optional
      data type in the output file (must be a numpy dtype). Default: *alms*.real.dtype
    mmax_in : int, optional
      maximum m in the input array
    """

    l2max = Alm.getlmax(len(alms),mmax=mmax_in)
    if (lmax != -1 and lmax > l2max):
        raise ValueError("Too big lmax in parameter")
    elif lmax == -1:
        lmax = l2max

    if mmax_in == -1:
	mmax_in = l2max

    if mmax == -1:
        mmax = lmax
    if mmax > mmax_in:
	mmax = mmax_in

    if (out_dtype == None):
        out_dtype = alms.real.dtype

    l,m = Alm.getlm(lmax)
    idx = np.where((l <= lmax)*(m <= mmax))
    l = l[idx]
    m = m[idx]

    idx_in_original = Alm.getidx(l2max, l=l, m=m)
    
    index = l**2 + l + m + 1

    out_data = np.empty(len(index),\
                             dtype=[('index','i'),\
                                        ('real',out_dtype),('imag',out_dtype)])
    out_data['index'] = index
    out_data['real'] = alms.real[idx_in_original]
    out_data['imag'] = alms.imag[idx_in_original]

    cindex = pf.Column(name="index", format=getformat(np.int32), unit="l*l+l+m+1", array=out_data['index'])
    creal = pf.Column(name="real", format=getformat(out_dtype), unit="unknown", array=out_data['real'])
    cimag = pf.Column(name="imag", format=getformat(out_dtype), unit="unknown", array=out_data['imag'])

    tbhdu = pf.new_table([cindex,creal,cimag])
    tbhdu.writeto(filename,clobber=True)