def create_gaussian_alm():
    ng_data.load_cl()
    map_gaussian,alm_g = hp.synfast(ng_data.cl,nside=ng_config.nside,lmax=ng_config.lmax,alm=True)
    #print alm_g
    hp.fitsfunc.write_alm(ng_config.get_filename_alm_g(),alm_g) #save it
    print 'Map written: ' + ng_config.get_filename_alm_g()
    return alm_g
def create_ng_alm():
    #cl
    ng_data.load_cl()
    print len(ng_data.cl)
    #print cl
    
    #alpha, beta, r
    ng_data.load_factorfunctions()
    print len(ng_data.alpha[0])

    print 'Creating Gaussian map.'
    alm_g = create_gaussian_alm()

    pixarea = hp.nside2pixarea(ng_config.nside)
    ng_estimate.get_quadrature()
    alm_ng = np.zeros(hp.sphtfunc.Alm.getsize(ng_config.lmax), dtype=np.complex)
    print "\n\nCreating NG map: "
    for quad_id in range(len(ng_estimate.quadrature_rid)):
        r_id = ng_estimate.quadrature_rid[quad_id]
        weight = ng_estimate.quadrature_weight[quad_id]
        print "quad_id " + str(quad_id) + "  r = " + str(r_id) + "  weight = " + str(weight)

        if (ng_config.model==1):
            #calculate the map B(r,omega)
            #B= sum_lm C_l^-1 beta_l(r) a_lm^G Y_lm(omega)
            fb = ng_data.beta[r_id][2:]/ng_data.cl[2:]  #both are arrays with l elements. but first two elements are discarded, monopole and dipole
            fb = np.insert(fb, 0, [0.,0.])
            scaleblm = hp.sphtfunc.almxfl(alm_g, fb, inplace=False)
            map_b = hp.sphtfunc.alm2map(scaleblm,ng_config.nside,lmax=ng_config.lmax)
            map_bb = map_b*map_b
            #integrate over omega. for local model: we want to get int(Y_lm B*2), i.e. the alm of the b^2 map
            alm_ng_r = hp.sphtfunc.map2alm(map_bb,lmax=ng_config.lmax)
            #scale the alm_ng_r by alpha_l(r)
            alm_ng_r = hp.sphtfunc.almxfl(alm_ng_r, ng_data.alpha[r_id], inplace=True)

        if (ng_config.model==2):
            fa = ng_data.alpha[r_id][2:]/ng_data.cl[2:]
            fa = np.insert(fa, 0, [0.,0.])            
            fb = ng_data.beta[r_id][2:]/ng_data.cl[2:]
            fb = np.insert(fb, 0, [0.,0.]) 
            scalealm = hp.sphtfunc.almxfl(alm_g, fa, inplace=False)
            scaleblm = hp.sphtfunc.almxfl(alm_g, fb, inplace=False)
            map_a = hp.sphtfunc.alm2map(scalealm,ng_config.nside,lmax=ng_config.lmax)
            map_b = hp.sphtfunc.alm2map(scaleblm,ng_config.nside,lmax=ng_config.lmax)
            map_aa = map_a*map_a
            map_ab = map_a*map_b
            map_bb = map_b*map_b
            alm_ng_r_aa = hp.sphtfunc.map2alm(map_aa,lmax=ng_config.lmax)
            alm_ng_r_ab = hp.sphtfunc.map2alm(map_ab,lmax=ng_config.lmax)
            alm_ng_r_bb = hp.sphtfunc.map2alm(map_bb,lmax=ng_config.lmax)
            #term1: X_l M_x^2 
            term1_alm = hp.sphtfunc.almxfl(alm_ng_r_aa, ng_data.alpha[r_id], inplace=False)     #TODO here should be fa fb       
            #term2: Y_l M_y^2
            term2_alm = hp.sphtfunc.almxfl(alm_ng_r_bb, ng_data.beta[r_id], inplace=False)
            #term3: (Y_l M_x M_y + 2perm)
            term3_alm = hp.sphtfunc.almxfl(alm_ng_r_bb, ng_data.alpha[r_id], inplace=False) + 2*hp.sphtfunc.almxfl(alm_ng_r_ab, ng_data.beta[r_id], inplace=False)
            #term4: (X_l M_x M_y + 2perm) 
            term4_alm = hp.sphtfunc.almxfl(alm_ng_r_aa, ng_data.beta[r_id], inplace=False) + 2*hp.sphtfunc.almxfl(alm_ng_r_ab, ng_data.alpha[r_id], inplace=False)
            f1 = math.cos(ng_config.phi) #*ng_config.map_fnl the fnl scaling is done when loading the map
            f2 = math.sin(ng_config.phi) #*ng_config.map_fnl
            alm_ng_r = f1*(-term1_alm + term3_alm) + f2*(term2_alm - term4_alm)

        alm_ng += weight*(ng_data.r[r_id]**2)*alm_ng_r


    hp.fitsfunc.write_alm(ng_config.get_filename_alm_ng(),alm_ng)
    print 'NG_local map simulated. Saved as ' + ng_config.get_filename_alm_ng()