Commit 6cd89441 authored by Maude Le Jeune's avatar Maude Le Jeune
Browse files

alm convert real complex

more dmc IO
parent 444beeb4
......@@ -102,4 +102,30 @@ def alm2cov (almtab):
return cov, nmode
def complex2real (alm, lmax, ell):
""" Convert complex alm to real for a given ell
Parameters
----------
alm: array-like, full range complex alm
lmax:int
ell:int
Returns
-------
array like, real alm corresponding for a given ell
"""
idx = array(hp.Alm.getidx (lmax, ell, arange(ell+1)), dtype=int)
alm_cplx = alm[idx]
i0 = arange(1, ell+1, dtype=int)
i1 = arange(ell+1, 2*ell+1, dtype=int)
i2 = arange(ell-1, -1, -1, dtype=int)
alm_real = zeros((2*ell+1))
alm_real[i1] = sqrt(2)*real(alm_cplx[i0])
alm_real[ell] = real(alm_cplx[0])
alm_real[i2] = sqrt(2)*imag(alm_cplx[i0])
return alm_real
......@@ -228,6 +228,63 @@ def read_cat (file):
cat = np.loadtxt(file)
return cat
def read_table (file):
""" Read table from file
Parameters
----------
file: string, FITS file name or DMC object name
Returns
-------
array-like.
"""
if __piolib(file):
try:
tab = pio.ReadTAB2DObject(file, 'PIODOUBLE', '')
grpname = pio.GetGrpName(file)
grp = pio.OpenTAB2DGrp(grpname, "r")
ncols = pio.GetTAB2DColumnGrp(grp)
nrows = len(tab)/ncols
cov = zeros((nrows, ncols))
for c in range(ncols):
cov[:,c] = squeeze(tab[c::ncols])
val = pio.CloseTAB2DGrp(grp)
except pio.pioError,val:
return str(val)
else:
cov= sp.read_table(file)
return cov
def read_covmat (file):
""" Read cov mat from file
Parameters
----------
file: string, FITS file name or DMC object name
Returns
-------
(cov, nmodes) list of 2 arrays.
"""
if __piolib(file):
try:
tab = pio.ReadTAB2DObject(file, 'PIODOUBLE', '')
grpname = pio.GetGrpName(file)
grp = pio.OpenTAB2DGrp(grpname, "r")
ncols = pio.GetTAB2DColumnGrp(grp)
nrows = len(tab)/ncols
nmodes = squeeze(tab[0::ncols])
cov = zeros((nrows, ncols-1))
for c in range(ncols-1):
cov[:,c] = squeeze(tab[c+1::ncols])
val = pio.CloseTAB2DGrp(grp)
except pio.pioError,val:
return str(val)
else:
[cov,nmodes] = sp.read_covmat(file)
return [cov,nmodes]
def write_covmat(file, cov, nbmodes=None):
""" Write cov mat to file.
......@@ -276,9 +333,35 @@ def write_table(file, tab):
pio.CloseTAB2DGrp(grp)
except pio.pioError,val:
return str(val)
else:
sp.write_table(file, tab)
else:
sp.write_table(file, tab)
def main():
import optparse
parser = optparse.OptionParser()
parser.add_option('-o', '--object-type', metavar='N',
help='DMC object type name (map, cl, table, covmat).',type='string')
(options, args) = parser.parse_args()
fn1 = args[0]
fn2 = args[1]
if options.object_type=='map':
o = read_map (fn1)
write_map(fn2, o)
elif options.object_type=='cl':
o = read_cl (fn1)
write_cl(fn2, o)
elif options.object_type=='table':
o = read_table (fn1)
write_table(fn2, o)
elif options.object_ty=='covmat':
(o,n) = read_covmat (fn1)
write_covmat(fn2, o, nbmodes=n)
else:
print "error : object type not recognized"
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment