#!/usr/bin/env python
import sys
import numpy as np
from process_fullsky import simulate_alms, get_theory_cmb, PLANCK_DATA_PATH, FGS_SIM_PATH
from foregrounds import get_theory_fg

lmax = 2000
nside = 2048

filebase = sys.argv[1]
filename_cmb = FGS_SIM_PATH + 'alms/' + '{}_cmb_alm_lmax{}'.format(
    filebase, lmax)
filename_fg = FGS_SIM_PATH + 'alms/' + '{}_fg_alm_lmax{}'.format(
    filebase, lmax)

ls, cls_theory_cmb = get_theory_cmb(lmax=lmax)
ls, cls_theory_fg = get_theory_fg(lmax=lmax)

Tlm_cmb, Elm_cmb, Blm_cmb = simulate_alms(cls_theory_cmb,
                                          nside=nside,
                                          lmax=lmax)
Tlm_fg, Elm_fg, Blm_fg = simulate_alms(cls_theory_fg, nside=nside, lmax=lmax)

np.savez(filename_cmb, Tlm=Tlm_cmb, Elm=Elm_cmb, Blm=Blm_cmb)
np.savez(filename_fg, Tlm=Tlm_fg, Elm=Elm_fg, Blm=Blm_fg)
def calibrate_fsky(mode, nsim=1,
                   smear=True,apo=2,
                    psky=70, f=353, 
                    nside=2048,lmax=1000,
                    visual_check=False,
                    beam_file=pf.PLANCK_DATA_PATH+'HFI_RIMO_Beams-100pc_R2.00.fits',
                    mask_sources=True, put_mask=True):

    """No noise treatment here"""

    fsky_correction = []
    TTm = np.zeros(lmax + 1)
    EEm = np.zeros(lmax + 1)
    BBm = np.zeros(lmax + 1)
    
    if put_mask:
        print 'reading mask...'
        mask = pf.get_planck_mask(psky=psky,
                                mask_sources=mask_sources,
                                apodization=apo)
    else:
        mask = None
    print 'reading beams...'
    hdulist = pf.fits.open(beam_file)
    beam = hdulist[pf.BEAM_INDEX['{}'.format(f)]].data.NOMINAL[0][:lmax+1]
    beamP = hdulist[pf.BEAM_INDEX['{}P'.format(f)]].data.NOMINAL[0][:lmax+1]
    beam = beam[:lmax+1]
    beamP = beamP[:lmax+1]

    if mode == 'fg':
        ls, cls_theory = get_theory_fg(f=f, lmax=lmax, psky=psky, apo=apo)
    if mode == 'cmb':
        ls, cls_theor = pf.get_theory_cmb(lmax=lmax, mode='cl')
        factor = ls*(1+ls)
        
    for i in np.arange(nsim):
        print 'sim #{}...'.format(i+1)
        I, Q, U = pf.simulate_cmb_map(nside=nside, lmax=lmax,
                                    frequency=f,smear=smear,
                                    cls_theory=cls_theory,
                                    beam=beam, beamP=beamP,
                                    save=False,
                                    beam_file=None)
        print 'Cl #{}...'.format(i+1)
        Tlm, Elm, Blm = pf.calc_alm(I, Q, U, mask=mask,
                                    lmax=lmax,
                                    add_beam=None,add_beamP=None,
                                    div_beam=beam,div_beamP=beamP,
                                    healpy_format=True)
        TT = hp.alm2cl(Tlm)
        EE = hp.alm2cl(Elm)
        BB = hp.alm2cl(Blm)
 
        #ls, TT, EE, BB, TE, TB, EB = measure_dlcl(mask=mask, Imap=I, Qmap=Q, Umap=U,
        #                                          beam=beam, beamP=beamP,
        #                                            mode='cl',frequency=f,
        #                                            lmax=lmax,lmin=0,
        #                                            put_mask=put_mask,
        #                                            psky=psky,
        #                                            mask_sources=mask_sources,
        #                                            apodization=apo)
        TTm += TT/nsim; EEm += EE/nsim; BBm += BB/nsim
        
        if visual_check:
            hp.mollview(I)
            hp.mollview(Q)
            hp.mollview(U)

    fsky_correction.append(cls_theory[0] / TTm)
    fsky_correction.append(cls_theory[1] / EEm)
    fsky_correction.append(cls_theory[2] / BBm)

    fsky_correction = np.array(fsky_correction)
    fsky_correction[np.isnan(fsky_correction)] = 0.
    fsky_correction[fsky_correction==np.inf] = 0.

    return ls, fsky_correction, TTm, EEm, BBm, cls_theory
def calibrate_fsky(mode,
                   nsim=1,
                   smear=True,
                   apo=2,
                   psky=70,
                   f=353,
                   nside=2048,
                   lmax=1000,
                   visual_check=False,
                   beam_file=pf.PLANCK_DATA_PATH +
                   'HFI_RIMO_Beams-100pc_R2.00.fits',
                   mask_sources=True,
                   put_mask=True):
    """No noise treatment here"""

    fsky_correction = []
    TTm = np.zeros(lmax + 1)
    EEm = np.zeros(lmax + 1)
    BBm = np.zeros(lmax + 1)

    if put_mask:
        print 'reading mask...'
        mask = pf.get_planck_mask(psky=psky,
                                  mask_sources=mask_sources,
                                  apodization=apo)
    else:
        mask = None
    print 'reading beams...'
    hdulist = pf.fits.open(beam_file)
    beam = hdulist[pf.BEAM_INDEX['{}'.format(f)]].data.NOMINAL[0][:lmax + 1]
    beamP = hdulist[pf.BEAM_INDEX['{}P'.format(f)]].data.NOMINAL[0][:lmax + 1]
    beam = beam[:lmax + 1]
    beamP = beamP[:lmax + 1]

    if mode == 'fg':
        ls, cls_theory = get_theory_fg(f=f, lmax=lmax, psky=psky, apo=apo)
    if mode == 'cmb':
        ls, cls_theor = pf.get_theory_cmb(lmax=lmax, mode='cl')
        factor = ls * (1 + ls)

    for i in np.arange(nsim):
        print 'sim #{}...'.format(i + 1)
        I, Q, U = pf.simulate_cmb_map(nside=nside,
                                      lmax=lmax,
                                      frequency=f,
                                      smear=smear,
                                      cls_theory=cls_theory,
                                      beam=beam,
                                      beamP=beamP,
                                      save=False,
                                      beam_file=None)
        print 'Cl #{}...'.format(i + 1)
        Tlm, Elm, Blm = pf.calc_alm(I,
                                    Q,
                                    U,
                                    mask=mask,
                                    lmax=lmax,
                                    add_beam=None,
                                    add_beamP=None,
                                    div_beam=beam,
                                    div_beamP=beamP,
                                    healpy_format=True)
        TT = hp.alm2cl(Tlm)
        EE = hp.alm2cl(Elm)
        BB = hp.alm2cl(Blm)

        #ls, TT, EE, BB, TE, TB, EB = measure_dlcl(mask=mask, Imap=I, Qmap=Q, Umap=U,
        #                                          beam=beam, beamP=beamP,
        #                                            mode='cl',frequency=f,
        #                                            lmax=lmax,lmin=0,
        #                                            put_mask=put_mask,
        #                                            psky=psky,
        #                                            mask_sources=mask_sources,
        #                                            apodization=apo)
        TTm += TT / nsim
        EEm += EE / nsim
        BBm += BB / nsim

        if visual_check:
            hp.mollview(I)
            hp.mollview(Q)
            hp.mollview(U)

    fsky_correction.append(cls_theory[0] / TTm)
    fsky_correction.append(cls_theory[1] / EEm)
    fsky_correction.append(cls_theory[2] / BBm)

    fsky_correction = np.array(fsky_correction)
    fsky_correction[np.isnan(fsky_correction)] = 0.
    fsky_correction[fsky_correction == np.inf] = 0.

    return ls, fsky_correction, TTm, EEm, BBm, cls_theory
#!/usr/bin/env python
import sys
import numpy as np
from process_fullsky import simulate_alms,get_theory_cmb,PLANCK_DATA_PATH,FGS_SIM_PATH
from foregrounds import get_theory_fg

lmax = 2000
nside = 2048

filebase = sys.argv[1]
filename_cmb = FGS_SIM_PATH + 'alms/' + '{}_cmb_alm_lmax{}'.format(filebase,lmax)
filename_fg = FGS_SIM_PATH + 'alms/' + '{}_fg_alm_lmax{}'.format(filebase,lmax)

ls, cls_theory_cmb = get_theory_cmb(lmax=lmax)
ls, cls_theory_fg = get_theory_fg(lmax=lmax)

Tlm_cmb, Elm_cmb, Blm_cmb = simulate_alms(cls_theory_cmb, nside=nside, lmax=lmax)
Tlm_fg, Elm_fg, Blm_fg = simulate_alms(cls_theory_fg, nside=nside, lmax=lmax)

np.savez(filename_cmb, Tlm=Tlm_cmb, Elm=Elm_cmb, Blm=Blm_cmb)
np.savez(filename_fg, Tlm=Tlm_fg, Elm=Elm_fg, Blm=Blm_fg)