def orig_vs_recon(nshell=3,irfirst=2,view=True):

    #morig=np.ndarray((1000,1000))
    #mb2b=np.ndarray((1000,1000))
    morig=0.0
    mb2b=0.0
    for ir in range(irfirst,nshell):
        mr=band_limit(hp.read_map(orig_file(ir)))
        
        #mr=unf.runf(a2b_file(ir),verbose=1)
        #r dip
        mr= hp.remove_dipole(mr,fitval=False,verbose=True)        

        mr_b2b=unf.runf(a2b_file(ir),verbose=1)
        #mr_b2b=unf.runf(b2b_file(ir))
        #r dip
        mr_b2b= hp.remove_dipole(mr_b2b,fitval=False,verbose=True)        

        morig = morig + mr[grid_pix] #- mr.mean()
        mb2b =  mb2b  + mr_b2b[grid_pix]

        if ir%10 ==0: print('ir={}'.format(ir))
    #- end ir loop
    if view: 
        view_balls(morig, mb2b,nshell=nshell)

    return morig, mb2b, (mr, mr_b2b)
Esempio n. 2
0
def get_dipole(map1):
    uw = np.array([1.] * len(map1))
    try:
        ddir = hp.remove_dipole(map1, fitval=True)[2]  #direction vector array)
    except:
        ddir = hp.remove_dipole(map1, uw, fitval=True)[2]

    return ddir
Esempio n. 3
0
def get_dipole(map1):
    uw=np.array([1.]*len(map1))
    try:
        ddir=hp.remove_dipole(map1, fitval=True)[2] #direction vector array)
    except:
        ddir=hp.remove_dipole(map1, uw, fitval=True)[2]

    return ddir
Esempio n. 4
0
def remove_dipole_and_write(file_name):

    print '-> REMOVING DIPOLE'
    ILC_SZ_map = hp.read_map(setup.out_files_path + file_name + '.fits')
    hp.mollview(ILC_SZ_map, title='BEFORE REMOVING DIPOLE', norm='hist')
    hp.remove_dipole(ILC_SZ_map)
    hp.mollview(ILC_SZ_map, title='AFTER REMOVING DIPOLE', norm='hist')
    hp.write_map(setup.out_files_path + file_name + '_dipole_rem', ILC_SZ_map)
    plt.show()

    return 0
Esempio n. 5
0
def create_dipole_map(nside, dipole, lonlat=None):
    '''
    Create an anisotropy map with given nside and
        dipole amplitude
    If the direction (lonlat) is not specified,
        the dipole is created on random
    '''
    if lonlat is None:
        cl = [0., 1.]
        sky = hp.synfast(cl, nside, verbose=False)
    else:
        if len(lonlat) != 2:
            raise ValueError('Direction should' +
                             ' be (lon, lat)')
        sky = np.zeros(hp.nside2npix(nside))
        ipix = hp.ang2pix(nside, lonlat[0],
                          lonlat[1], lonlat=True)
        sky[ipix] = 1.
        new_lon = lonlat[0] + 180.
        ipix = hp.ang2pix(nside, new_lon,
                          -lonlat[1], lonlat=True)
        sky[ipix] = -1.
        sky -= hp.remove_dipole(sky, verbose=False)
    sky -= sky.mean()
    sky /= sky.max()
    sky *= dipole
    return sky
Esempio n. 6
0
 def run(self, dataSlice, slicePoint=None):
     # Calculate the power spectrum.
     if self.removeDipole:
         cl = hp.anafast(hp.remove_dipole(dataSlice[self.colname]))
     else:
         cl = hp.anafast(dataSlice[self.colname])
     l = np.arange(np.size(cl))
     condition = np.where((l <= self.lmax) & (l >= self.lmin))[0]
     totalpower = np.sum(cl[condition]*l[condition]*(l[condition]+1)/2.0/np.pi)
     return totalpower
Esempio n. 7
0
 def run(self, dataSlice, slicePoint=None):
     # Calculate the power spectrum.
     if self.removeDipole:
         cl = hp.anafast(hp.remove_dipole(dataSlice[self.colname], verbose=False))
     else:
         cl = hp.anafast(dataSlice[self.colname])
     ell = np.arange(np.size(cl))
     condition = np.where((ell <= self.lmax) & (ell >= self.lmin))[0]
     totalpower = np.sum(cl[condition]*(2*ell[condition]+1))
     return totalpower
def remove_dipole(data):
    """Healpy description suggests that this function removes both, the monopole and dipole

    Args:
        data ([type]): [description]

    Returns:
        [type]: [description]
    """
    ret = np.zeros_like(data)
    for n in range(data.shape[0]):
        ret[n,:] = hp.remove_dipole(data[n,:], fitval=False)
    return ret
Esempio n. 9
0
def get_tf(fname_tf, fname_cmb_unlensed, fname_cmb_lensing, fname_output, fname_hits):
    if os.path.isfile(fname_tf):
        tf = hp.read_cl(fname_tf)
    else:
        inmap = hp.read_map(fname_cmb_unlensed, None) + hp.read_map(fname_cmb_lensing, None)
        inmap *= 1e-6  # into K_CMB
        inmap[0] = hp.remove_dipole(inmap[0])
        outmap = hp.read_map(fname_output, None)
        hits = None # hp.read_map(fname_hits)
        cl_in = map2cl(inmap, hits)
        cl_out = map2cl(outmap, hits)
        tf = cl_out / cl_in
        hp.write_cl(fname_tf, tf)
    return tf
Esempio n. 10
0
    def __call__(self, metricValue, slicer, userPlotDict, fignum=None):
        """
        Generate and plot the power spectrum of metricValue (calculated on a healpix grid).
        """
        if 'Healpix' not in slicer.slicerName:
            raise ValueError(
                'HealpixPowerSpectrum for use with healpix metricBundles.')
        plotDict = {}
        plotDict.update(self.defaultPlotDict)
        plotDict.update(userPlotDict)

        fig = plt.figure(fignum, figsize=plotDict['figsize'])
        ax = fig.add_subplot(plotDict['subplot'])
        # If the mask is True everywhere (no data), just plot zeros
        if False not in metricValue.mask:
            return None
        if plotDict['removeDipole']:
            cl = hp.anafast(hp.remove_dipole(metricValue.filled(
                slicer.badval)),
                            lmax=plotDict['maxl'])
        else:
            cl = hp.anafast(metricValue.filled(slicer.badval),
                            lmax=plotDict['maxl'])
        ell = np.arange(np.size(cl))
        if plotDict['removeDipole']:
            condition = (ell > 1)
        else:
            condition = (ell > 0)
        ell = ell[condition]
        cl = cl[condition]
        # Plot the results.
        plt.plot(ell, (cl * ell * (ell + 1)) / 2.0 / np.pi,
                 color=plotDict['color'],
                 linestyle=plotDict['linestyle'],
                 label=plotDict['label'])
        if cl.max() > 0 and plotDict['logScale']:
            plt.yscale('log')
        plt.xlabel(r'$l$', fontsize=plotDict['fontsize'])
        plt.ylabel(r'$l(l+1)C_l/(2\pi)$', fontsize=plotDict['fontsize'])
        if plotDict['labelsize'] is not None:
            plt.tick_params(axis='x', labelsize=plotDict['labelsize'])
            plt.tick_params(axis='y', labelsize=plotDict['labelsize'])
        if plotDict['title'] is not None:
            plt.title(plotDict['title'])
        # Return figure number (so we can reuse/add onto/save this figure if desired).
        return fig.number
Esempio n. 11
0
def test_k2g2k_rand():
    """Test that transformation of k->g->k recovers the input convergence map."""

    nside = 16
    npix = hp.nside2npix(nside)
    lmax = 32

    k = np.random.standard_normal(npix)
    k = hp.smoothing(k, lmax=lmax, verbose=False)
    k = hp.remove_monopole(k)
    k = hp.remove_dipole(k)

    g1, g2 = transformations.conv2shear(k, lmax)

    k_recov = transformations.shear2conv(g1, g2, lmax)

    np.testing.assert_almost_equal(k, k_recov, decimal=3)
Esempio n. 12
0
def get_tf(fname_tf, fname_cmb_unlensed, fname_cmb_lensing, fname_output,
           fname_hits):
    if os.path.isfile(fname_tf):
        tf = hp.read_cl(fname_tf)
    else:
        inmap = hp.read_map(fname_cmb_unlensed, None) + hp.read_map(
            fname_cmb_lensing, None)
        inmap *= 1e-6  # into K_CMB
        inmap[0] = hp.remove_dipole(inmap[0])
        outmap = hp.read_map(fname_output, None)
        mask = get_mask(fname_hits)
        cl_in = map2cl(inmap, mask)
        cl_out = map2cl(outmap, mask)
        tf = cl_out / cl_in
        hp.write_cl(fname_tf, tf)
    tf[:, lmax_tf:] = 1
    tf[tf > 1] = 1
    return tf
Esempio n. 13
0
    def plotPowerSpectrum(self, metricValue, title=None, fignum=None, maxl=500.,
                          label=None, addLegend=False, legendloc='upper right',
                          removeDipole=True,
                          logPlot=True, verbose=False, **kwargs):
        """Generate and plot the power spectrum of metricValue.

        maxl = maximum ell value to plot (default 500 .. to plot all l, set to value > 3500)
        title = plot Title (default None)
        fignum = figure number (default None and create new plot)
        label = label to add in figure legend (default None)
        addLegend = flag to add legend (default False).
        removeDipole = remove dipole when calculating power spectrum (default True)
        (monopole removed automatically.)
        """
        if fignum:
            fig = plt.figure(fignum)
        else:
            fig = plt.figure()
        # If the mask is True everywhere (no data), just plot zeros
        if False not in metricValue.mask:
            return None
        else:
            if removeDipole:
                cl = hp.anafast(hp.remove_dipole(metricValue.filled(self.badval), verbose=verbose))
            else:
                cl = hp.anafast(metricValue.filled(self.badval))
        l = np.arange(np.size(cl))
        # Plot the results.
        if removeDipole:
            condition = ((l < maxl) & (l > 1))
        else:
            condition = (l < maxl)
        plt.plot(l[condition], (cl[condition]*l[condition]*(l[condition]+1))/2.0/np.pi, label=label)
        if cl[condition].max() > 0 and logPlot:
            plt.yscale('log')
        plt.xlabel(r'$l$')
        plt.ylabel(r'$l(l+1)C_l/(2\pi)$')
        if addLegend:
            plt.legend(loc=legendloc, fancybox=True, prop={'size':'smaller'})
        if title!=None:
            plt.title(title)
        # Return figure number (so we can reuse/add onto/save this figure if desired).
        return fig.number
Esempio n. 14
0
    def __call__(self, metricValue, slicer, userPlotDict, fignum=None):
        """
        Generate and plot the power spectrum of metricValue (calculated on a healpix grid).
        """
        if 'Healpix' not in slicer.slicerName:
            raise ValueError('HealpixPowerSpectrum for use with healpix metricBundles.')
        plotDict = {}
        plotDict.update(self.defaultPlotDict)
        plotDict.update(userPlotDict)

        fig = plt.figure(fignum, figsize=plotDict['figsize'])
        ax = fig.add_subplot(plotDict['subplot'])
        # If the mask is True everywhere (no data), just plot zeros
        if False not in metricValue.mask:
            return None
        if plotDict['removeDipole']:
            cl = hp.anafast(hp.remove_dipole(metricValue.filled(slicer.badval)), lmax=plotDict['maxl'])
        else:
            cl = hp.anafast(metricValue.filled(slicer.badval), lmax=plotDict['maxl'])
        ell = np.arange(np.size(cl))
        if plotDict['removeDipole']:
            condition = (ell > 1)
        else:
            condition = (ell > 0)
        ell = ell[condition]
        cl = cl[condition]
        # Plot the results.
        plt.plot(ell, (cl * ell * (ell + 1)) / 2.0 / np.pi,
                 color=plotDict['color'], linestyle=plotDict['linestyle'], label=plotDict['label'])
        if cl.max() > 0 and plotDict['logScale']:
            plt.yscale('log')
        plt.xlabel(r'$l$', fontsize=plotDict['fontsize'])
        plt.ylabel(r'$l(l+1)C_l/(2\pi)$', fontsize=plotDict['fontsize'])
        if plotDict['labelsize'] is not None:
            plt.tick_params(axis='x', labelsize=plotDict['labelsize'])
            plt.tick_params(axis='y', labelsize=plotDict['labelsize'])
        if plotDict['title'] is not None:
            plt.title(plotDict['title'])
        # Return figure number (so we can reuse/add onto/save this figure if desired).
        return fig.number
Esempio n. 15
0
def plotBundleMaps(path,
                   outDir,
                   bundle,
                   dataLabel,
                   filterBand,
                   dataName=None,
                   skymap=True,
                   powerSpectrum=True,
                   cartview=False,
                   raRange=[-180, 180],
                   decRange=[-70, 10],
                   showPlots=True,
                   saveFigs=False,
                   outDirNameForSavedFigs='',
                   lmax=500,
                   nTicks=5,
                   numFormat='%.2f',
                   colorMin=None,
                   colorMax=None):
    """

    Plot maps for the data in a metricBundle object without using MAF routines.

    Required Parameters
    -------------------
      * path: str: path to the main directory where output directory is saved
      * outDir: str: name of the main output directory
      * bundle: metricBundle object.
      * dataLabel: str: data type, e.g. 'counts', 'NumGal'. Will be the label for the colorbar in
                        in skymaps/cartview plots.
      * filterBand: str: filter to consider, e.g. 'r'
    
    Optional Parameters
    -------------------
      * dataName: str: dataLabel analog for filename. e.g. say for datalabel='c$_l$', a good dataName is 'cl'
                       Default: None 
      * skymap: boolean: set to True if want to plot skymaps. Default: True
      * powerSpectrum: boolean: set to True if want to plot powerspectra; dipole is removed. Default: True

      * cartview: boolean: set to True if want to plot cartview plots. Default: Fase
      * raRange: float array: range of right ascention (in degrees) to consider in cartview plot; only useful when 
                              cartview=True. Default: [-180,180]
      * decRange: float array: range of declination (in degrees) to consider in cartview plot; only useful when 
                               cartview=True. Default: [-70,10]

      * showPlots: boolean: set to True if want to show figures. Default: True
      * saveFigs: boolean: set to True if want to save figures. Default: False
      * outDirNameForSavedFigs: str: name for the output directory if saveFigs=True. Default: ''
      * lmax: int: upper limit on the multipole. Default: 500
      * nTicks: int: (number of ticks - 1) on the skymap colorbar. Default: 5
      * numFormat: str: number format for the labels on the colorbar. Default: '%.2f'
      * colorMin: float: lower limit on the colorscale for skymaps. Default: None
      * colorMax: float: upper limit on the colorscale for skymaps. Default: None

    """
    if dataName is None:
        dataName = dataLabel

    if saveFigs:
        # set up the subdirectory
        outDirNew = outDirNameForSavedFigs
        if not os.path.exists('%s%s/%s' % (path, outDir, outDirNew)):
            os.makedirs('%s%s/%s' % (path, outDir, outDirNew))

    if powerSpectrum:
        # plot out the power spectrum
        for dither in bundle:
            plt.clf()
            cl = hp.anafast(hp.remove_dipole(
                bundle[dither].metricValues.filled(
                    bundle[dither].slicer.badval)),
                            lmax=lmax)
            ell = np.arange(len(cl))
            plt.plot(ell, (cl * ell * (ell + 1)) / 2.0 / np.pi)
            plt.title('%s: %s' % (dataLabel, dither))
            plt.xlabel(r'$\ell$')
            plt.ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
            plt.xlim(0, lmax)

            if saveFigs:
                # save power spectrum
                filename = '%s_powerSpectrum_%s.png' % (dataName, dither)
                plt.savefig('%s%s/%s/%s' % (path, outDir, outDirNew, filename),
                            bbox_inches='tight',
                            format='png')
            if showPlots:
                plt.show()
            else:
                plt.close('all')
    if skymap:
        # plot out the skymaps
        for dither in bundle:
            inSurveyIndex = np.where(
                bundle[dither].metricValues.mask == False)[0]
            median = np.median(bundle[dither].metricValues.data[inSurveyIndex])
            stddev = np.std(bundle[dither].metricValues.data[inSurveyIndex])

            if (colorMin == None):
                colorMin = median - 1.5 * stddev
            if (colorMax == None):
                colorMax = median + 1.5 * stddev

            increment = (colorMax - colorMin) / float(nTicks)
            ticks = np.arange(colorMin + increment, colorMax, increment)

            hp.mollview(bundle[dither].metricValues.filled(
                bundle[dither].slicer.badval),
                        flip='astro',
                        rot=(0, 0, 0),
                        min=colorMin,
                        max=colorMax,
                        title='',
                        cbar=False)
            hp.graticule(dpar=20, dmer=20, verbose=False)
            plt.title(dither)
            ax = plt.gca()
            im = ax.get_images()[0]
            fig = plt.gcf()
            cbaxes = fig.add_axes([0.1, 0.03, 0.8,
                                   0.04])  # [left, bottom, width, height]
            cb = plt.colorbar(im,
                              orientation='horizontal',
                              ticks=ticks,
                              format=numFormat,
                              cax=cbaxes)
            cb.set_label(dataLabel)

            if saveFigs:
                # save skymap
                filename = '%s_skymap_%s.png' % (dataName, dither)
                plt.savefig('%s%s/%s/%s' % (path, outDir, outDirNew, filename),
                            bbox_inches='tight',
                            format='png')
            if showPlots:
                plt.show()
            else:
                plt.close('all')

    if cartview:
        # plot out the cartview plots
        for dither in bundle:
            inSurveyIndex = np.where(
                bundle[dither].metricValues.mask == False)[0]
            median = np.median(bundle[dither].metricValues.data[inSurveyIndex])
            stddev = np.std(bundle[dither].metricValues.data[inSurveyIndex])

            if (colorMin == None):
                colorMin = median - 1.5 * stddev
            if (colorMax == None):
                colorMax = median + 1.5 * stddev

            increment = (colorMax - colorMin) / float(nTicks)
            ticks = np.arange(colorMin + increment, colorMax, increment)

            hp.cartview(bundle[dither].metricValues.filled(
                bundle[dither].slicer.badval),
                        flip='astro',
                        rot=(0, 0, 0),
                        lonra=raRange,
                        latra=decRange,
                        min=colorMin,
                        max=colorMax,
                        title='',
                        cbar=False)
            hp.graticule(dpar=20, dmer=20, verbose=False)
            plt.title(dither)
            ax = plt.gca()
            im = ax.get_images()[0]
            fig = plt.gcf()
            cbaxes = fig.add_axes([0.1, 0.25, 0.8,
                                   0.04])  # [left, bottom, width, height]
            cb = plt.colorbar(im,
                              orientation='horizontal',
                              ticks=ticks,
                              format=numFormat,
                              cax=cbaxes)
            cb.set_label(dataLabel)

            if saveFigs:
                # save cartview plot
                filename = '%s_cartview_%s.png' % (dataName, dither)
                plt.savefig('%s%s/%s/%s' % (path, outDir, outDirNew, filename),
                            bbox_inches='tight',
                            format='png')
            if showPlots:
                plt.show()
            else:
                plt.close('all')
Esempio n. 16
0
def main():

    '''
    Loading and calculating power spectrum components
    '''

    # Get run parameters
    
    s_fn_params = 'data/params.pkl'
    (i_lmax, i_nside, s_fn_map, s_map_name, 
        s_fn_mask, s_fn_mll) = get_params(s_fn_params)

    print ""
    print "Run parameters:"
    print "lmax: %i, nside: %i, map name: %s" % (i_lmax, i_nside, s_map_name)

    # Load Planck map and mask

    print ""
    print "Loading map and mask..."

    na_map = hp.read_map(s_fn_map) # for Planck SMICA, units of K
    na_map = na_map / 1e6 / 2.7 # convert units to mK -> unitless
    na_map = hp.remove_dipole(na_map) # removes the dipole and monopole -- turn off other lines doing this...
    na_mask = hp.read_map(s_fn_mask)
    na_map_masked = na_map * na_mask
    na_alm = hp.map2alm(na_map_masked, lmax=i_lmax-1)
    s_fn_alm = 'output/na_alm_data.fits'
    hp.write_alm(s_fn_alm, na_alm)

    # Spherical harmonic transform (map -> power spectrum)

    print ""
    print "Calculating power spectra..."

    na_cltt = hp.anafast(na_map_masked, lmax=i_lmax-1)
    na_wll = hp.anafast(na_mask, lmax=i_lmax-1)
    na_wll = na_wll + 2.0 # remove monopole and dipole
    na_ell = np.arange(len(na_cltt))
    na_ell = na_ell + 2.0 # remove monopole and dipole

    # Load mode coupling matrix and invert it

    print ""
    print "Loading and inverting mode coupling matrix..."

    na_mll = np.load(s_fn_mll)
    na_mll_inv = np.linalg.inv(na_mll)

    # Calculate Mll corrected power spectrum
    
    na_clttp = np.dot(na_mll_inv, na_cltt)

    # Save Mll corrected power spectrum
    s_fn_clttp = 'output/na_cltt.npy'
    np.save(s_fn_clttp, na_clttp)

    s_fn_cltt = 'output/na_cltt_not_corrected.npy'
    np.save(s_fn_cltt, na_cltt)

    print ""
    print "Saving power spectrum to %s" % s_fn_clttp

    '''
    Associated plots: map; mask; masked map; power spectrum of mask, power 
        spectrum of map, masked map, and mode coupling corrected map
    '''

    # NOTE: Mollview doesn't seem to work on cirrus -- probably an error with 
    #       this version of Healpy
    # plot_map(na_map, s_title='Raw Planck')
    # plot_map(na_mask, s_title='Mask')
    # plot_map(na_map_masked, s_title='Masked Map')

    plot_ps([na_ell], [na_wll], [''], s_ylabel='$W_\ell$', s_title='', 
        s_fn_plot='plots/fig_mask_ps.png')
    plot_ps([na_ell, na_ell], [na_cltt, na_clttp], 
        ['Masked', 'Masked, Corrected'], s_ylabel='$C_\ell$', s_title='', 
        s_fn_plot='plots/fig_masked_masked_corrected_ps2.png')

    return
Esempio n. 17
0
def spice(map1,
          map2=None,
          window=None,
          mask=None,
          window2=None,
          mask2=None,
          apodizesigma=0.0,
          apodizetype=0,
          thetamax=180.0,
          decouple=False,
          returnxi=False,
          remove_monopole=False,
          remove_dipole=False):
    '''This is an implementation of the PolSpice algorithm in Python.

    Parameters
    ----------
    map1 : str or list
        Input filename to load or list of 3 Healpix maps (I, Q, U)

    map2 : str or list, optional
        Input filename to load or list of 3 Healpix maps (I, Q, U)

    window : str or numpy.ndarray, optional
        Filename or array giving the weighted window to use.

    mask : str or numpy.ndarray, optional
        Filename or array giving the mask to use

    window2 : str or numpy.ndarray, optional
        Filename or array giving the weighted window to use for
        the second map. If not input, the same window is used as
        for the first map.

    mask2 : str or numpy.ndarray, optional
        Filename or array giving the mask to use for the second
        map. If not input, the same window is used as for the
        first map.

    apodizesigma : float
        Scale factor of the correlation function apodization (degrees)

    apodizetype : int
        Type of apodization. 0 is a Gaussian window. 1 is a cosine window

    thetamax : float
        maximum value of theta used in the integrals to calculate the
        power spectra from the correlation functions.

    decouple : bool
        whether to compute the decoupled correlation functions

    returnxi : bool
        whether or not to return the correlation functions as an additional
        output of the function

    remove_monopole : bool
        whether or not to remove the monopole from the maps before analyzing
        them

    remove_dipole : bool
        whether or not to remove the dipole from the maps before analyzing
        them

    Returns
    -------
    cls : numpy.ndarray
        An array containing the power spectra of the maps. TT, EE, BB, TE, EB, TB

    xi : numpy.ndarray, optional
        An array containing the real space correlation functions.

    Notes
    -----
    If neither a window nor a mask are input, the functionality will
    be similar to anafast.
    '''

    #If input is a string assume it is a filename
    if isinstance(map1, str):
        try:
            map1 = H.read_map(map1, field=(0, 1, 2))
        except:
            raise ValueError('Input map should have I, Q, and U')

    have_map2 = False
    if isinstance(map2, str):
        try:
            map2 = H.read_map(map2, field=(0, 1, 2))
        except:
            raise ValueError('Input map should have I, Q, and U')
        have_map2 = True

    if isinstance(window, str):
        window = H.read_map(window)
    elif window is None:
        window = np.ones_like(map1[0])

    if mask is None:
        mask = np.ones_like(map1[0])

    #Merge masks/windows
    window = window * mask

    map1 = (map1[0] * window, map1[1] * window, map1[2] * window)

    if have_map2:
        if isinstance(window2, str):
            window2 = H.read_map(window2)
        elif window2 is None:
            window2 = window

        if isinstance(mask2, str):
            mask2 = H.read_map(mask2)
        elif mask2 is None:
            mask2 = mask

        window2 = window2 * mask2

        map2 = (map2[0] * window2, map2[1] * window2, map2[2] * window2)

    #Remove the monopole and dipole. Points outside the mask/window are set
    #to the UNSEEN value so they are ignored when calculating the monopole/
    #dipole
    if remove_monopole:
        idx = window == 0
        map1[0][idx] = H.UNSEEN
        map1[1][idx] = H.UNSEEN
        map1[2][idx] = H.UNSEEN
        map1[0][:] = H.remove_monopole(map1[0], verbose=False)
        map1[1][:] = H.remove_monopole(map1[1], verbose=False)
        map1[2][:] = H.remove_monopole(map1[2], verbose=False)
        map1[0][idx] = 0.0
        map1[1][idx] = 0.0
        map1[2][idx] = 0.0
        if have_map2:
            idx = window2 == 0
            map2[0][idx] = H.UNSEEN
            map2[1][idx] = H.UNSEEN
            map2[2][idx] = H.UNSEEN
            map2[0][:] = H.remove_monopole(map2[0], verbose=False)
            map2[1][:] = H.remove_monopole(map2[1], verbose=False)
            map2[2][:] = H.remove_monopole(map2[2], verbose=False)
            map2[0][idx] = 0.0
            map2[1][idx] = 0.0
            map2[2][idx] = 0.0

    if remove_dipole:
        idx = window == 0
        map1[0][idx] = H.UNSEEN
        map1[1][idx] = H.UNSEEN
        map1[2][idx] = H.UNSEEN
        map1[0][:] = H.remove_dipole(map1[0], verbose=False)
        map1[1][:] = H.remove_dipole(map1[1], verbose=False)
        map1[2][:] = H.remove_dipole(map1[2], verbose=False)
        map1[0][idx] = 0.0
        map1[1][idx] = 0.0
        map1[2][idx] = 0.0
        if have_map2:
            idx = window2 == 0
            map2[0][idx] = H.UNSEEN
            map2[1][idx] = H.UNSEEN
            map2[2][idx] = H.UNSEEN
            map2[0][:] = H.remove_dipole(map2[0], verbose=False)
            map2[1][:] = H.remove_dipole(map2[1], verbose=False)
            map2[2][:] = H.remove_dipole(map2[2], verbose=False)
            map2[0][idx] = 0.0
            map2[1][idx] = 0.0
            map2[2][idx] = 0.0

    #Construction of the cls of the maps and masks/weights
    cls = H.anafast(map1, map2=map2)
    thetas, xi, w, x = get_xi_from_cl(cls,
                                      return_leggauss=True,
                                      thetamax=thetamax)

    if window is None:
        wls = None
    else:
        wls = H.anafast(window, map2=window2)
        #Compute xi from the cls of the map and masks
        thetas, xi_mask = get_xi_from_cl(wls, thetas=thetas)

        xi_final = _correct_xi_from_mask(xi, xi_mask)

    if decouple:
        xi_final = _update_xi(xi_final, x, thetamax, cls, cl_mask=wls)

    ncor = len(xi_final)

    #apodize the correlation function
    if apodizesigma > 0.0:
        apfunction = _apodizefunction(x, apodizesigma, thetamax, apodizetype)
        for i in range(ncor):
            xi_final[i, :] *= apfunction

    #compute the cls from xi
    cls_out = do_cl_from_xi(xi_final,
                            w,
                            x,
                            decouple=decouple,
                            apodizesigma=apodizesigma,
                            apodizetype=apodizetype,
                            thetamax=thetamax)

    #Correct cl for beam, pixel, transfer function???

    if returnxi:
        return cls_out, xi_final
    else:
        return cls_out
Esempio n. 18
0
def map2cl(m, mask):
    m[0] = hp.remove_dipole(m[0])
    m[m == hp.UNSEEN] = 0
    fsky = np.sum(mask) / mask.size
    cl = hp.anafast(m * mask, lmax=lmax, iter=0) / fsky
    return cl
Esempio n. 19
0
def main(run_type="data", nsim=0, fnl=0):

    """
    MPI Setup
    """
    o_comm = MPI.COMM_WORLD
    i_rank = o_comm.Get_rank()  # current core number -- e.g., i in arange(i_size)
    i_size = o_comm.Get_size()  # number of cores assigned to run this program
    o_status = MPI.Status()

    i_work_tag = 0
    i_die_tag = 1

    """
    Loading and calculating power spectrum components
    """

    if run_type == "fnl":
        nl = 1024
    else:
        nl = 1499

    if run_type == "data":
        fn_map = h._fn_map
    elif run_type == "sim":
        fn_map = "output/map_sim_%i.fits" % nsim
    elif run_type == "fnl":
        # print "fnl value: %i" % fnl
        fn_map = "data/fnl_sims/map_fnl_%i_sim_%i.fits" % (int(fnl), nsim)

    # (1) read map (either map_data or map_sim), mask, mll, and create mll_inv;
    map_in = hp.read_map(fn_map)
    mask = hp.read_map(h._fn_mask)
    if run_type == "fnl":
        mask = 1.0
    fn_mll = "output/na_mll_%i_lmax.npy" % nl
    mll = np.load(fn_mll)
    if run_type == "fnl":
        mll = np.identity(nl)
    mll_inv = np.linalg.inv(mll)

    nside = hp.get_nside(map_in)

    if i_rank == 0:

        if run_type == "data":
            fn_cl21 = "output/cl21_data.dat"
            fn_cl21_no_mll = "output/cl21_data_no_mll.dat"
        elif run_type == "sim":
            fn_cl21 = "output/cl21_sim_%i.dat" % nsim
            fn_cl21_no_mll = "output/cl21_no_mll_%i.dat" % nsim
        elif run_type == "fnl":
            fn_cl21 = "output/cl21_fnl_%i_sim_%i.dat" % (int(fnl), nsim)
            fn_cl21_no_mll = "output/cl21_fnl_%i_sim_%i_no_mll.dat" % (int(fnl), nsim)

        f_t1 = time.time()

        print ""
        print "Run parameters:"
        print "(Using %i cores)" % i_size
        print "nl: %i, nside: %i, map: %s" % (nl, nside, fn_map)
        print "beam: %s, alpha_beta: %s, cltt: %s" % (h._fn_beam, h._fn_alphabeta, h._fn_cltt)

        print ""
        print "Loading ell, r, dr, alpha, beta, cltt, and beam..."

    # (2) normalize, remove mono-/dipole, and mask map to create map_masked;
    map_in /= 1e6 * 2.7
    map_in = hp.remove_dipole(map_in)
    map_masked = map_in * mask

    # (3) create alm_masked (map2alm on map_masked), cltt_masked (anafast on
    #     map_masked), and cltt_corrected (dot cltt_masked with mll_inv)

    alm_masked = hp.map2alm(map_masked)
    alm_masked = alm_masked[: hp.Alm.getsize(nl)]

    cltt_masked = hp.anafast(map_masked)
    cltt_masked = cltt_masked[:nl]
    cltt_corrected = np.dot(mll_inv, cltt_masked)

    # stuff with alpha, beta, r, dr, and beam

    l, r, dr, alpha, beta = np.loadtxt(h._fn_alphabeta, usecols=(0, 1, 2, 3, 4), unpack=True, skiprows=3)

    l = np.unique(l)
    r = np.unique(r)[::-1]

    nr = len(r)

    cltt_denom = np.load("output/cltt_theory.npy")  # replace with 'output/na_cltt.npy'
    cltt_denom = cltt_denom[:nl]

    alpha = alpha.reshape(len(l), nr)
    beta = beta.reshape(len(l), nr)
    dr = dr.reshape(len(l), nr)
    dr = dr[0]

    if run_type != "fnl":
        beam = np.load(h._fn_beam)
    else:
        beam = np.ones(len(cltt_denom))

    l = l[:nl]
    beam = beam[:nl]
    alpha = alpha[:nl, :]
    beta = beta[:nl, :]

    if i_rank == 0:
        print "nr: %i, nl: %i" % (nr, nl)

    f_t2 = time.time()

    if i_rank == 0:
        print ""
        print "Time to create alms from maps: %.2f s" % (f_t2 - f_t1)
        print "Calculating full skewness power spectrum..."

    cl21 = np.zeros(nl)
    work = np.zeros(1, dtype="i")
    result = np.zeros(nl, dtype="d")

    # master loop
    if i_rank == 0:

        # send initial jobs

        for i_rank_out in range(1, i_size):

            work = np.array([i_rank_out - 1], dtype="i")
            o_comm.Send([work, MPI.INT], dest=i_rank_out, tag=i_work_tag)

        for i_r in range(i_size - 1, nr):

            if i_r % (nr / 10) == 0:
                print "Finished %i%% of jobs... (%.2f s)" % (i_r * 100 / nr, time.time() - f_t2)

            work = np.array([i_r], dtype="i")

            o_comm.Recv([result, MPI.DOUBLE], source=MPI.ANY_SOURCE, status=o_status, tag=MPI.ANY_TAG)

            # print "received results from core %i" % o_status.Get_source()

            o_comm.Send([work, MPI.INT], dest=o_status.Get_source(), tag=i_work_tag)

            cl21 += result

        for i_rank_out in range(1, i_size):

            o_comm.Recv([result, MPI.DOUBLE], source=MPI.ANY_SOURCE, status=o_status, tag=MPI.ANY_TAG)

            cl21 += result
            print "cl21 = %.6f, result = %.6f" % (np.average(cl21), np.average(result))

            o_comm.Send([np.array([9999], dtype="i"), MPI.INT], dest=o_status.Get_source(), tag=i_die_tag)

    # slave loop:
    else:

        while 1:

            o_comm.Recv([work, MPI.INT], source=0, status=o_status, tag=MPI.ANY_TAG)

            if o_status.Get_tag() == i_die_tag:

                break

            i_r = work[0]

            # create Alm, Blm (almxfl with alm_masked and beta / cltt_denom
            # * beam, etc.)

            Alm = hp.almxfl(alm_masked, alpha[:, i_r] / cltt_denom * beam)
            Blm = hp.almxfl(alm_masked, beta[:, i_r] / cltt_denom * beam)

            ############################# DEBUG ################################
            # cltt_Alm = hp.alm2cl(Alm)
            # cltt_Blm = hp.alm2cl(Blm)
            # np.savetxt('debug2/cltt_%s_Alm.dat' % run_type, cltt_Alm)
            # np.savetxt('debug2/cltt_%s_Blm.dat' % run_type, cltt_Blm)
            ####################################################################

            An = hp.alm2map(Alm, nside=nside, fwhm=0.00145444104333, verbose=False)
            Bn = hp.alm2map(Blm, nside=nside, fwhm=0.00145444104333, verbose=False)

            ############################# DEBUG ################################
            # cltt_An = hp.anafast(An)
            # cltt_Bn = hp.anafast(Bn)
            # np.savetxt('debug2/cltt_%s_An.dat' % run_type, cltt_An)
            # np.savetxt('debug2/cltt_%s_Bn.dat' % run_type, cltt_Bn)
            ####################################################################

            An = An * mask
            Bn = Bn * mask

            ############################# DEBUG ################################
            if i_r == 100:
                print "saving alpha, beta for %i" % i_r
                np.savetxt("debug2/alpha_ir_%i" % i_r, alpha[:, i_r])
                np.savetxt("debug2/beta_ir_%i" % i_r, beta[:, i_r])
                print "(An * Bn)[:10] == An[:10] * Bn[:10]:", (An * Bn)[:10] == An[:10] * Bn[:10]

            ####################################################################

            B2lm = hp.map2alm(Bn * Bn, lmax=nl)
            ABlm = hp.map2alm(An * Bn, lmax=nl)

            ############################# DEBUG ################################
            # cltt_B2lm = hp.alm2cl(B2lm)
            # cltt_ABlm = hp.alm2cl(ABlm)
            # np.savetxt('debug2/cltt_%s_B2lm.dat' % run_type, cltt_B2lm)
            # np.savetxt('debug2/cltt_%s_ABlm.dat' % run_type, cltt_ABlm)
            ####################################################################

            clAB2 = hp.alm2cl(Alm, B2lm, lmax=nl)
            clABB = hp.alm2cl(ABlm, Blm, lmax=nl)

            ############################# DEBUG ################################
            # np.savetxt('debug2/clAB2_%s.dat' % run_type, clAB2)
            # np.savetxt('debug2/clABB_%s.dat' % run_type, clABB)
            ####################################################################

            clAB2 = clAB2[1:]
            clABB = clABB[1:]

            result = np.zeros(nl, dtype="d")
            result += (clAB2 + 2 * clABB) * r[i_r] ** 2.0 * dr[i_r]

            ############################# DEBUG ################################
            # np.savetxt('debug2/cl21_%s.dat' % run_type, result)
            ####################################################################

            print (
                "finished work for r=%i, dr=%.2f, avg(alpha)=%.2f, avg(beta)=%.2f, avg(result)=%.4g"
                % (int(r[i_r]), dr[i_r], np.average(alpha[:, i_r]), np.average(beta[:, i_r]), np.average(result))
            )

            ############################# DEBUG ################################
            # print "finished debug -- goodbye!"
            # exit()
            ####################################################################

            o_comm.Send([result, MPI.DOUBLE], dest=0, tag=1)

    f_t8 = time.time()

    if i_rank == 0:
        print ""
        print ("Saving power spectrum to %s (not mll corrected)" % fn_cl21_no_mll)

        np.savetxt(fn_cl21_no_mll, cl21)

        print ""
        print "Saving power spectrum to %s (mll corrected)" % fn_cl21

        cl21 = np.dot(mll_inv, cl21)
        np.savetxt(fn_cl21, cl21)

    return
Esempio n. 20
0
    pix = np.arange(npix)
    theta, phi = hp.pix2ang(nside, pix)

    thetaquat = qa.rotation(YAXIS, theta)
    phiquat = qa.rotation(ZAXIS, phi)
    quat = qa.mult(phiquat, thetaquat)

    dipoler = Dipoler(freq=0)
    dipo = dipoler.dipole(quat) * 1e3
    hp.mollview(dipo, title='Solar system dipole, freq=0', unit='mK')
    plt.gca().graticule(30)

    plt.figure(figsize=[20, 6])
    plt.suptitle('Doppler quadrupole')
    for ifreq, freq in enumerate([0, 30, 44, 70, 100, 143, 217, 353, 545,
                                  857]):
        dipoler = Dipoler(freq=freq)
        dipo = dipoler.dipole(quat) * 1e6
        quad = hp.remove_dipole(dipo)
        hp.mollview(quad,
                    title='{}GHz, q={:.3f}, P-to-P={:.1f}uK'.format(
                        freq, dipoler.q, np.ptp(quad)),
                    sub=[2, 5, 1 + ifreq],
                    unit=r'$\mu$K',
                    min=-2,
                    max=4)
        plt.gca().graticule(30)

    plt.show()
Esempio n. 21
0
print(mapa_mono)

hp.mollview(mapa_mono, norm='hist', title='l = 0 (monopole)')
pyplot.savefig('foreg_l1.png')

# with and without gal_cut ... 
# Compare:::

mapa2 = hp.remove_monopole(mapa, gal_cut=20)
# legal fazer isso em um mapa de foreground -> o monopolo varia bastante

####################
# Removing DIPOLE:::

mapa = hp.read_map('LFI_CompMap_Foregrounds-smica_1024_R2.00.fits')
mapa3 = hp.remove_dipole(mapa)
hp.mollview(mapa3, norm='hist', title='l > 1')
pyplot.savefig('foreg_lgt1.png')

mapa = hp.read_map('LFI_CompMap_Foregrounds-smica_1024_R2.00.fits')
mapa_mono_dip = mapa - mapa3
hp.mollview(mapa_mono_dip, norm='hist', title='0 <= l <= 1 (mono+dip)')
pyplot.savefig('foreg_0gelle1.png')

###
mapa3 = hp.remove_dipole(mapa, gal_cut= 20 )
hp.mollview(mapa3, norm='hist', title='l > 1 (gal_cut=20)')
pyplot.savefig('foreg_cut_lgt1.png')

mapa_mono_dip = mapa - mapa3
hp.mollview(mapa_mono_dip, norm='hist', title='0 <= l <= 1 (mono+dip, gal_cut=20)')
Map = create_namedtuple_for_map(OPTIONS)

########################################
# Read the input maps in memory

INPUT_MAPS = []
for cur_title, cur_map_file in zip(*[iter(ARGS)] * 2):
    try:
        log.info('reading map `%s\'...', cur_map_file)
        cur_map = healpy.read_map(cur_map_file, field=OPTIONS.stokes_component)

        if OPTIONS.no_monopole:
            result = healpy.remove_monopole(cur_map, fitval=True, copy=False)
            log.info('monopole has been removed (%s)', str(result))
        if OPTIONS.no_dipole:
            result = healpy.remove_dipole(cur_map, fitval=True, copy=False)
            log.info('dipole has been removed (amplitude: %s)', str(result))

        cur_map[cur_map < -1.6e+30] = np.NaN

        if type(OPTIONS.mask) is not None:
            cur_map[OPTIONS.mask == 0] = np.NaN

        cur_entry = dict(title=cur_title,
                         pixels=cur_map,
                         input_file=cur_map_file)
        if OPTIONS.plot_distributions:
            cur_entry['histogram'] = np.histogram(cur_map[~np.isnan(cur_map)],
                                                  bins=50)
        INPUT_MAPS.append(Map(**cur_entry))
        log.info('...ok')
def coaddM5Analysis(path, dbfile, runName, slair=False,
                    WFDandDDFs=False,
                    noDithOnly=False, bestDithOnly=False, someDithOnly=False,
                    specifiedDith=None,
                    nside=128, filterBand='r',
                    includeDustExtinction=False,
                    saveunMaskedCoaddData=False,
                    pixelRadiusForMasking=5, cutOffYear=None,
                    plotSkymap=True,
                    plotCartview=True,
                    unmaskedColorMin=None, unmaskedColorMax=None,
                    maskedColorMin=None, maskedColorMax=None,
                    nTicks=5,
                    plotPowerSpectrum=True,
                    showPlots=True, saveFigs=True,
                    almAnalysis=True,
                    raRange=[-50,50], decRange=[-65,5],
                    saveMaskedCoaddData=True):

    """

    Analyze the artifacts induced in the coadded 5sigma depth due to imperfect observing strategy.
      - Creates an output directory for subdirectories containing the specified things to save.
      - Creates, shows, and saves comparison plots.
      - Returns the metricBundle object containing the calculated coadded depth, and the output directory name.

    Required Parameters
    -------------------
      * path: str: path to the main directory where output directory is to be saved.
      * dbfile: str: path to the OpSim output file, e.g. to a copy of enigma_1189
      * runName: str: run name tag to identify the output of specified OpSim output, e.g. 'enigma1189' 

    Optional Parameters
    -------------------
      * slair: boolean: set to True if analysis on a SLAIR output.
                        Default: False
      * WFDandDDFs: boolean: set to True if want to consider both WFD survet and DDFs. Otherwise will only work
                             with WFD. Default: False
      * noDithOnly: boolean: set to True if only want to consider the undithered survey. Default: False
      * bestDithOnly: boolean: set to True if only want to consider RandomDitherFieldPerVisit.
                               Default: False
      * someDithOnly: boolean: set to True if only want to consider undithered and a few dithered surveys. 
                               Default: False
      * specifiedDith: str: specific dither strategy to run.
                            Default: None
      * nside: int: HEALpix resolution parameter. Default: 128
      * filterBand: str: any one of 'u', 'g', 'r', 'i', 'z', 'y'. Default: 'r'
      * includeDustExtinction: boolean: set to include dust extinction. Default: False
      * saveunMaskedCoaddData: boolean: set to True to save data before border masking. Default: False
      * pixelRadiusForMasking: int: number of pixels to mask along the shallow border. Default: 5

      * cutOffYear: int: year cut to restrict analysis to only a subset of the survey. 
                         Must range from 1 to 9, or None for the full survey analysis (10 yrs).
                         Default: None
      * plotSkymap: boolean: set to True if want to plot skymaps. Default: True
      * plotCartview: boolean: set to True if want to plot cartview plots. Default: False
      * unmaskedColorMin: float: lower limit on the colorscale for unmasked skymaps. Default: None
      * unmaskedColorMax: float: upper limit on the colorscale for unmasked skymaps. Default: None

      * maskedColorMin: float: lower limit on the colorscale for border-masked skymaps. Default: None
      * maskedColorMax: float: upper limit on the colorscale for border-masked skymaps. Default: None
      * nTicks: int: (number of ticks - 1) on the skymap colorbar. Default: 5
      * plotPowerSpectrum: boolean: set to True if want to plot powerspectra. Default: True

      * showPlots: boolean: set to True if want to show figures. Default: True
      * saveFigs: boolean: set to True if want to save figures. Default: True
      
      * almAnalysis: boolean: set to True to perform the alm analysis. Default: True
      * raRange: float array: range of right ascention (in degrees) to consider in alm  cartview plot;
                              applicable when almAnalysis=True. Default: [-50,50]
      * decRange: float array: range of declination (in degrees) to consider in alm cartview plot; 
                               applicable when almAnalysis=True. Default: [-65,5]
      * saveMaskedCoaddData: boolean: set to True to save the coadded depth data after the border
                                      masking. Default: True

    """
    # ------------------------------------------------------------------------
    # read in the database
    if slair:
        # slair database
        opsdb = db.Database(dbfile, defaultTable='observations')
    else:
        # OpSim database
        opsdb = db.OpsimDatabase(dbfile)

    # ------------------------------------------------------------------------
    # set up the outDir
    zeropt_tag = ''
    if cutOffYear is not None: zeropt_tag = '%syearCut'%cutOffYear
    else: zeropt_tag = 'fullSurveyPeriod'

    if includeDustExtinction: dust_tag = 'withDustExtinction'
    else: dust_tag = 'noDustExtinction'

    regionType = ''
    if  WFDandDDFs: regionType = 'WFDandDDFs_'
        
    outDir = 'coaddM5Analysis_%snside%s_%s_%spixelRadiusForMasking_%sBand_%s_%s_directory'%(regionType, nside,dust_tag,
                                                                                            pixelRadiusForMasking,
                                                                                            filterBand, runName, zeropt_tag)
    print('# outDir: %s'%outDir)
    resultsDb = db.ResultsDb(outDir=outDir)

    # ------------------------------------------------------------------------
    # set up the sql constraint
    if  WFDandDDFs:
        if cutOffYear is not None:
            nightCutOff = (cutOffYear)*365.25
            sqlconstraint = 'night<=%s and filter=="%s"'%(nightCutOff, filterBand)
        else:
            sqlconstraint = 'filter=="%s"'%filterBand
    else:
        # set up the propID and units on the ra, dec
        if slair: # no prop ID; only WFD is simulated.
            wfdWhere = ''
            raDecInDeg = True
        else:
            propIds, propTags = opsdb.fetchPropInfo()
            wfdWhere = '%s and '%opsdb.createSQLWhere('WFD', propTags)
            raDecInDeg = opsdb.raDecInDeg
        # set up the year cutoff
        if cutOffYear is not None:
            nightCutOff = (cutOffYear)*365.25
            sqlconstraint = '%snight<=%s and filter=="%s"'%(wfdWhere, nightCutOff, filterBand)
        else:
            sqlconstraint = '%sfilter=="%s"'%(wfdWhere, filterBand)
    print('# sqlconstraint: %s'%sqlconstraint)

    # ------------------------------------------------------------------------
    # setup all the slicers
    slicer = {}
    stackerList = {}
    
    if specifiedDith is not None: # would like to add all the stackers first and then keep only the one that is specified
        bestDithOnly, noDithOnly = False, False

    if bestDithOnly:
        stackerList['RandomDitherFieldPerVisit'] = [mafStackers.RandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
                                                                                                 randomSeed=1000)]
        slicer['RandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='randomDitherFieldPerVisitRa',
                                                                   latCol='randomDitherFieldPerVisitDec',
                                                                   latLonDeg=raDecInDeg,
                                                                   nside=nside, useCache=False)
    else:
        if slair:
            slicer['NoDither'] = slicers.HealpixSlicer(lonCol='RA', latCol='dec', latLonDeg=raDecInDeg,
                                                      nside=nside, useCache=False)
        else:
            slicer['NoDither'] = slicers.HealpixSlicer(lonCol='fieldRA', latCol='fieldDec', latLonDeg=raDecInDeg,
                                                      nside=nside, useCache=False)
        if someDithOnly and not noDithOnly:
            #stackerList['RepulsiveRandomDitherFieldPerVisit'] = [myStackers.RepulsiveRandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            #slicer['RepulsiveRandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerVisitRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerVisitDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            slicer['SequentialHexDitherFieldPerNight'] =  slicers.HealpixSlicer(lonCol='hexDitherFieldPerNightRa',
                                                                               latCol='hexDitherFieldPerNightDec',
                                                                               latLonDeg=raDecInDeg,
                                                                               nside=nside, useCache=False)
            slicer['PentagonDitherPerSeason'] = slicers.HealpixSlicer(lonCol='pentagonDitherPerSeasonRa', latCol='pentagonDitherPerSeasonDec',
                                                                     latLonDeg=raDecInDeg,
                                                                     nside=nside, useCache=False)
        elif not noDithOnly:
            # random dithers on different timescales
            stackerList['RandomDitherPerNight'] = [mafStackers.RandomDitherPerNightStacker(degrees=raDecInDeg, randomSeed=1000)]
            stackerList['RandomDitherFieldPerNight'] = [mafStackers.RandomDitherFieldPerNightStacker(degrees=raDecInDeg, randomSeed=1000)]
            stackerList['RandomDitherFieldPerVisit'] = [mafStackers.RandomDitherFieldPerVisitStacker(degrees=raDecInDeg, randomSeed=1000)]
            
            # rep random dithers on different timescales
            #stackerList['RepulsiveRandomDitherPerNight'] = [myStackers.RepulsiveRandomDitherPerNightStacker(degrees=raDecInDeg,
            #                                                                                                randomSeed=1000)]
            #stackerList['RepulsiveRandomDitherFieldPerNight'] = [myStackers.RepulsiveRandomDitherFieldPerNightStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            #stackerList['RepulsiveRandomDitherFieldPerVisit'] = [myStackers.RepulsiveRandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            # set up slicers for different dithers
            # random dithers on different timescales
            slicer['RandomDitherPerNight'] = slicers.HealpixSlicer(lonCol='randomDitherPerNightRa', latCol='randomDitherPerNightDec',
                                                                  latLonDeg=raDecInDeg, nside=nside, useCache=False)
            slicer['RandomDitherFieldPerNight'] = slicers.HealpixSlicer(lonCol='randomDitherFieldPerNightRa',
                                                                       latCol='randomDitherFieldPerNightDec',
                                                                       latLonDeg=raDecInDeg, nside=nside, useCache=False)
            slicer['RandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='randomDitherFieldPerVisitRa',
                                                                       latCol='randomDitherFieldPerVisitDec',
                                                                       latLonDeg=raDecInDeg, nside=nside, useCache=False)
            # rep random dithers on different timescales
            #slicer['RepulsiveRandomDitherPerNight'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherPerNightRa',
            #                                                               latCol='repulsiveRandomDitherPerNightDec',
            #                                                               latLonDeg=raDecInDeg, nside=nside, useCache=False)
            #slicer['RepulsiveRandomDitherFieldPerNight'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerNightRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerNightDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            #slicer['RepulsiveRandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerVisitRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerVisitDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            # spiral dithers on different timescales
            slicer['FermatSpiralDitherPerNight'] = slicers.HealpixSlicer(lonCol='fermatSpiralDitherPerNightRa',
                                                                         latCol='fermatSpiralDitherPerNightDec',
                                                                         latLonDeg=raDecInDeg, nside=nside, useCache=False)
            slicer['FermatSpiralDitherFieldPerNight'] = slicers.HealpixSlicer(lonCol='fermatSpiralDitherFieldPerNightRa',
                                                                              latCol='fermatSpiralDitherFieldPerNightDec',
                                                                              latLonDeg=raDecInDeg, nside=nside,
                                                                              useCache=False)
            slicer['FermatSpiralDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='fermatSpiralDitherFieldPerVisitRa',
                                                                              latCol='fermatSpiralDitherFieldPerVisitDec',
                                                                              latLonDeg=raDecInDeg, nside=nside,
                                                                              useCache=False)
            # hex dithers on different timescales
            slicer['SequentialHexDitherPerNight'] = slicers.HealpixSlicer(lonCol='hexDitherPerNightRa', latCol='hexDitherPerNightDec',
                                                                          latLonDeg=raDecInDeg, nside=nside, useCache=False)
            slicer['SequentialHexDitherFieldPerNight'] = slicers.HealpixSlicer(lonCol='hexDitherFieldPerNightRa',
                                                                               latCol='hexDitherFieldPerNightDec',
                                                                               latLonDeg=raDecInDeg, nside=nside, useCache=False)
            slicer['SequentialHexDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='hexDitherFieldPerVisitRa',
                                                                               latCol='hexDitherFieldPerVisitDec',
                                                                               latLonDeg=raDecInDeg, nside=nside, useCache=False)
            # per season dithers
            slicer['PentagonDitherPerSeason'] = slicers.HealpixSlicer(lonCol='pentagonDitherPerSeasonRa', latCol='pentagonDitherPerSeasonDec',
                                                                     latLonDeg=raDecInDeg, nside=nside, useCache=False)
            slicer['PentagonDiamondDitherPerSeason'] = slicers.HealpixSlicer(lonCol='pentagonDiamondDitherPerSeasonRa',
                                                                            latCol='pentagonDiamondDitherPerSeasonDec',
                                                                            latLonDeg=raDecInDeg, nside=nside,
                                                                            useCache=False)
            slicer['SpiralDitherPerSeason'] = slicers.HealpixSlicer(lonCol='spiralDitherPerSeasonRa',
                                                                   latCol='spiralDitherPerSeasonDec',
                                                                   latLonDeg=raDecInDeg, nside=nside, useCache=False)
    if specifiedDith is not None:
        stackerList_, slicer_ = {}, {}
        if specifiedDith in slicer.keys():
            if specifiedDith.__contains__('Random'):   # only Random dithers have a stacker object for rand seed specification
                stackerList_[specifiedDith] = stackerList[specifiedDith]
            slicer_[specifiedDith] = slicer[specifiedDith]
        else:
            raise ValueError('Invalid value for specifiedDith: %s. Allowed values include one of the following:\n%s'%(specifiedDith,
                                                                                                                      slicer.keys()))
        stackerList, slicer = stackerList_, slicer_

    # ------------------------------------------------------------------------
    if slair:
        m5Col = 'fivesigmadepth'
    else:
        m5Col = 'fiveSigmaDepth'
    # set up the metric
    if includeDustExtinction:
        # include dust extinction when calculating the co-added depth
        coaddMetric = metrics.ExgalM5(m5Col=m5Col, lsstFilter=filterBand)
    else:
        coaddMetric = metrics.Coaddm5Metric(m5col=m5col)
    dustMap = maps.DustMap(interp=False, nside=nside)   # include dustMap; actual in/exclusion of dust is handled by the galaxyCountMetric

    # ------------------------------------------------------------------------
    # set up the bundle
    coaddBundle = {}
    for dither in slicer:
        if dither in stackerList:
            coaddBundle[dither] = metricBundles.MetricBundle(coaddMetric, slicer[dither], sqlconstraint,
                                                             stackerList=stackerList[dither],
                                                             runName=runName, metadata=dither, mapsList=[dustMap])
        else:
            coaddBundle[dither] = metricBundles.MetricBundle(coaddMetric, slicer[dither], sqlconstraint,
                                                             runName=runName, metadata=dither, mapsList=[dustMap])

    # ------------------------------------------------------------------------
    # run the analysis
    if includeDustExtinction: print('\n# Running coaddBundle with dust extinction ...')
    else: print('\n# Running coaddBundle without dust extinction ...')
    cGroup = metricBundles.MetricBundleGroup(coaddBundle, opsdb, outDir=outDir, resultsDb=resultsDb,saveEarly=False)
    cGroup.runAll()

    # ------------------------------------------------------------------------
    # plot and save the data
    plotBundleMaps(path, outDir, coaddBundle,
                   dataLabel='$%s$-band Coadded Depth'%filterBand, filterBand=filterBand,
                   dataName='%s-band Coadded Depth'%filterBand,
                   skymap=plotSkymap, powerSpectrum=plotPowerSpectrum, cartview=plotCartview,
                   colorMin=unmaskedColorMin, colorMax=unmaskedColorMax,
                   nTicks=nTicks,
                   showPlots=showPlots, saveFigs=saveFigs,
                   outDirNameForSavedFigs='coaddM5Plots_unmaskedBorders')
    print('\n# Done saving plots without border masking.\n')

    # ------------------------------------------------------------------------
    plotHandler = plots.PlotHandler(outDir=outDir, resultsDb=resultsDb, thumbnail=False, savefig=False)

    print('# Number of pixels in the survey region (before masking the border):')
    for dither in coaddBundle:
        print('  %s: %s'%(dither, len(np.where(coaddBundle[dither].metricValues.mask == False)[0])))

    # ------------------------------------------------------------------------
    # save the unmasked data?
    if saveunMaskedCoaddData:
        outDir_new = 'unmaskedCoaddData'
        if not os.path.exists('%s%s/%s'%(path, outDir, outDir_new)):
            os.makedirs('%s%s/%s'%(path, outDir, outDir_new))
        saveBundleData_npzFormat('%s%s/%s'%(path, outDir, outDir_new), coaddBundle, 'coaddM5Data_unmasked', filterBand)
    
    # ------------------------------------------------------------------------
    # mask the edges
    print('\n# Masking the edges for coadd ...')
    coaddBundle = maskingAlgorithmGeneralized(coaddBundle, plotHandler,
                                             dataLabel='$%s$-band Coadded Depth'%filterBand,
                                             nside=nside,
                                             pixelRadius=pixelRadiusForMasking,
                                             plotIntermediatePlots=False,
                                             plotFinalPlots=False, printFinalInfo=True)
    if (pixelRadiusForMasking>0):
        # plot and save the masked data
        plotBundleMaps(path, outDir, coaddBundle,
                       dataLabel='$%s$-band Coadded Depth'%filterBand, filterBand=filterBand,
                       dataName='%s-band Coadded Depth'%filterBand,
                       skymap=plotSkymap, powerSpectrum=plotPowerSpectrum, cartview=plotCartview,
                       colorMin=maskedColorMin, colorMax=maskedColorMax,
                       nTicks=nTicks,
                       showPlots=showPlots, saveFigs=saveFigs,
                       outDirNameForSavedFigs='coaddM5Plots_maskedBorders')
        print('\n# Done saving plots with border masking. \n')
        
    # ------------------------------------------------------------------------
    # Calculate total power
    summarymetric = metrics.TotalPowerMetric()
    for dither in coaddBundle:
        coaddBundle[dither].setSummaryMetrics(summarymetric)
        coaddBundle[dither].computeSummaryStats()
        print('# Total power for %s case is %f.' %(dither, coaddBundle[dither].summaryValues['TotalPower']))
    print('')
    
    # ------------------------------------------------------------------------
    # run the alm analysis
    if almAnalysis: almPlots(path, outDir, copy.deepcopy(coaddBundle),
                             nside=nside, filterband=filterBand,
                             raRange=raRange, decRange=decRange,
                             showPlots=showPlots)
    # ------------------------------------------------------------------------
    # save the masked data?
    if saveMaskedCoaddData and (pixelRadiusForMasking>0):
        outDir_new = 'maskedCoaddData'
        if not os.path.exists('%s%s/%s'%(path, outDir, outDir_new)):
            os.makedirs('%s%s/%s'%(path, outDir, outDir_new))
        saveBundleData_npzFormat('%s%s/%s'%(path, outDir, outDir_new), coaddBundle, 'coaddM5Data_masked', filterBand)

    # ------------------------------------------------------------------------
    # plot comparison plots
    if len(coaddBundle.keys())>1:  # more than one key
        # set up the directory
        outDir_comp = 'coaddM5ComparisonPlots'
        if not os.path.exists('%s%s/%s'%(path, outDir, outDir_comp)):
            os.makedirs('%s%s/%s'%(path, outDir, outDir_comp))
        # ------------------------------------------------------------------------
        # plot for the power spectra
        cl = {}
        for dither in plotColor:
            if dither in coaddBundle:
                cl[dither] = hp.anafast(hp.remove_dipole(coaddBundle[dither].metricValues.filled(coaddBundle[dither].slicer.badval)),
                                        lmax=500)
                ell = np.arange(np.size(cl[dither]))
                plt.plot(ell, (cl[dither]*ell*(ell+1))/2.0/np.pi, color=plotColor[dither], linestyle='-', label=dither)
        plt.xlabel(r'$\ell$')
        plt.ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
        plt.xlim(0,500)
        fig = plt.gcf()
        fig.set_size_inches(12.5, 10.5)
        leg = plt.legend(labelspacing=0.001)
        for legobj in leg.legendHandles:
            legobj.set_linewidth(4.0)
        filename = 'powerspectrum_comparison_all.png'
        plt.savefig('%s%s/%s/%s'%(path, outDir, outDir_comp, filename), bbox_inches='tight', format='png')
        plt.show()

        # create the histogram
        scale = hp.nside2pixarea(nside, degrees=True)
        def tickFormatter(y, pos): return '%d'%(y * scale)    # convert pixel count to area
        binsize = 0.01
        for dither in plotColor:
            if dither in coaddBundle:
                ind = np.where(coaddBundle[dither].metricValues.mask == False)[0]
                binAll = int((max(coaddBundle[dither].metricValues.data[ind])-min(coaddBundle[dither].metricValues.data[ind]))/binsize)
                plt.hist(coaddBundle[dither].metricValues.data[ind],
                         bins=binAll, label=dither, histtype='step', color=plotColor[dither])
        ax = plt.gca()
        ymin, ymax = ax.get_ylim()
        nYticks = 10.
        wantedYMax = ymax*scale
        wantedYMax = 10.*np.ceil(float(wantedYMax)/10.)
        increment = 5.*np.ceil(float(wantedYMax/nYticks)/5.)
        wantedArray= np.arange(0, wantedYMax, increment)
        ax.yaxis.set_ticks(wantedArray/scale)
        ax.yaxis.set_major_formatter(FuncFormatter(tickFormatter))
        plt.xlabel('$%s$-band Coadded Depth'%filterBand)
        plt.ylabel('Area (deg$^2$)')
        fig = plt.gcf()
        fig.set_size_inches(12.5, 10.5)
        leg = plt.legend(labelspacing=0.001, loc=2)
        for legobj in leg.legendHandles:
            legobj.set_linewidth(2.0)
        filename = 'histogram_comparison.png'
        plt.savefig('%s%s/%s/%s'%(path, outDir, outDir_comp, filename), bbox_inches='tight', format='png')
        plt.show()
        # ------------------------------------------------------------------------
        # plot power spectra for the separte panel
        totKeys = len(list(coaddBundle.keys()))
        if (totKeys>1):
            plt.clf()
            nCols = 2
            nRows = int(np.ceil(float(totKeys)/nCols))
            fig, ax = plt.subplots(nRows,nCols)
            plotRow = 0
            plotCol = 0
            for dither in list(plotColor.keys()):
                if dither in list(coaddBundle.keys()):
                    ell = np.arange(np.size(cl[dither]))
                    ax[plotRow, plotCol].plot(ell, (cl[dither]*ell*(ell+1))/2.0/np.pi,
                                              color=plotColor[dither], label=dither)
                    if (plotRow==nRows-1):
                        ax[plotRow, plotCol].set_xlabel(r'$\ell$')
                    ax[plotRow, plotCol].set_ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
                    ax[plotRow, plotCol].yaxis.set_major_locator(MaxNLocator(3))
                    if (dither != 'NoDither'):
                        ax[plotRow, plotCol].set_ylim(0,0.0035)
                    ax[plotRow, plotCol].set_xlim(0,500)
                    plotRow += 1
                    if (plotRow > nRows-1):
                        plotRow = 0
                        plotCol += 1
            fig.set_size_inches(20,int(nRows*30/7.))
            filename = 'powerspectrum_sepPanels.png'
            plt.savefig('%s%s/%s/%s'%(path, outDir, outDir_comp, filename), bbox_inches='tight', format='png')
            plt.show()
    return coaddBundle, outDir
import numpy as np
import healpy as hp

nl = 2000

map_smica = hp.read_map('data/CompMap_CMB-smica_2048_R1.11.fits')
mask = hp.read_map('data/CompMap_Mask_2048_R1.00.fits')
map_processed = hp.remove_dipole(map_smica) / 1e6 / 2.7 * mask
alm_data = hp.map2alm(map_processed)
cltt = hp.anafast(map_processed)

np.save('debug/cltt_not_corrected.npy', cltt)

mll = np.load('output/na_mll_2000_lmax.npy')
mll_inv = np.linalg.inv(mll)
cltt_corrected = np.dot(mll_inv, cltt[:nl])
alm_sim = hp.synalm(cltt_corrected[:nl]) #this is probably the issue...

# load and resize things

l, r, dr, alpha, beta = np.loadtxt('data/l_r_alpha_beta.txt', usecols=(0,1,2,3,4), unpack=True, skiprows=3)

lmax = 1499

mll = np.load('output/na_mll_1499_lmax.npy')
mll_inv = np.linalg.inv(mll)

l = np.unique(l)
r = np.unique(r)[::-1]
l = l[:lmax]
Esempio n. 25
0

plt.show()


####################################################################
#check correlation between temperature and Hubble varance maps
#for some reason Peter and Wiltshire et al use  Fixsen et al 1996 CMB monopole temperature
Tnot = 2.728 #2.735
T_map_sun = boost_T(T0=Tnot,v=-v_cmb,ell=ell_hp,bee=bee_hp,l=l_cmb,b=b_cmb)

T_map_lg = boost_T(T0=Tnot,v=-v_lg,ell=ell_hp,bee=bee_hp,l=l_lg,b=b_lg)

T_residual = T_map_sun - T_map_lg

hp.remove_dipole(T_residual,verbose=True)

hp.mollview(T_residual,flip='geo')
plt.show()
print "caution. In mollview the longitude run from -180 to 180 degrees but "
print "In healpix the longitude runs from 0. to 360 degress."
print "Thus add 180 degrees to the longitude here to get the healpix longitude."
print "Longitudes in healpix are the same as in galactic coordinates only "
print "the latitudes change. It is confusing."

#testing the Pearson correlation coefficient function 
print "shape of Hs_out ", np.shape(Hs_out)
rho_HT = Pearson_corr_coeff(H_map = Hs_out[1,:], sigma=sigma_out[1,:], 
                            T_map = T_residual, T_mean = T_residual.mean() )

print "rho_HT (compare to Tabel III, third row first column ", rho_HT
Esempio n. 26
0
def main(run_type='data', nsim=0, fnl=0):

    '''
    MPI Setup
    '''
    o_comm = MPI.COMM_WORLD
    i_rank = o_comm.Get_rank() # current core number -- e.g., i in arange(i_size)
    i_size = o_comm.Get_size() # number of cores assigned to run this program
    o_status = MPI.Status()

    i_work_tag = 0
    i_die_tag = 1

    '''
    Loading and calculating power spectrum components
    '''

    if (run_type == 'fnl'):
        nl = 1024
    else:
        nl = 1499

    if (run_type == 'data'):
        fn_map = h._fn_map
    elif (run_type == 'sim'):
        fn_map = 'output/map_sim_%i.fits' % nsim
    elif (run_type == 'fnl'):
        #print "fnl value: %i" % fnl
        fn_map = 'data/fnl_sims/map_fnl_%i_sim_%i.fits' % (int(fnl), nsim)

    # (1) read map (either map_data or map_sim), mask, mll, and create mll_inv;
    map_in = hp.read_map(fn_map)
    mask = hp.read_map(h._fn_mask)
    if (run_type == 'fnl'):
        mask = 1.
    fn_mll = 'output/na_mll_%i_lmax.npy' % nl
    mll = np.load(fn_mll)
    if (run_type == 'fnl'):
        mll = np.identity(nl)
    mll_inv = np.linalg.inv(mll)

    nside = hp.get_nside(map_in)

    if (i_rank == 0):

        if (run_type == 'data'):
            fn_cl21 = 'output/cl21_data.dat'
            fn_cl21_no_mll = 'output/cl21_data_no_mll.dat'
        elif (run_type == 'sim'):
            fn_cl21 = 'output/cl21_sim_%i.dat' % nsim
            fn_cl21_no_mll = 'output/cl21_no_mll_%i.dat' % nsim
        elif (run_type == 'fnl'):
            fn_cl21 = 'output/cl21_fnl_%i_sim_%i.dat' % (int(fnl), nsim)
            fn_cl21_no_mll = 'output/cl21_fnl_%i_sim_%i_no_mll.dat' % (int(fnl), nsim)

        f_t1 = time.time()

        print ""
        print "Run parameters:"
        print "(Using %i cores)" % i_size
        print "nl: %i, nside: %i, map: %s" % (nl, nside, fn_map)
        print "beam: %s, alpha_beta: %s, cltt: %s" % (h._fn_beam, h._fn_alphabeta, h._fn_cltt)

        print ""
        print "Loading ell, r, dr, alpha, beta, cltt, and beam..."

    # (2) normalize, remove mono-/dipole, and mask map to create map_masked;
    map_in /= (1e6 * 2.7)
    map_in = hp.remove_dipole(map_in)
    map_masked = map_in * mask

    # (3) create alm_masked (map2alm on map_masked), cltt_masked (anafast on 
    #     map_masked), and cltt_corrected (dot cltt_masked with mll_inv)

    if (run_type == 'data' or run_type == 'sim'):
        alm_masked = hp.map2alm(map_masked)
    elif (run_type == 'fnl'):
        fn_almg = ('data/fnl_sims/alm_l_%04d_v3.fits' % (nsim,))
        almg = hp.read_alm(fn_almg)
        fn_almng = ('data/fnl_sims/alm_nl_%04d_v3.fits' % (nsim,))
        almng = hp.read_alm(fn_almng)
        alm = almg + fnl * almng
        alm_masked = alm

    cltt_masked = hp.anafast(map_masked)
    cltt_masked = cltt_masked[:nl]
    cltt_corrected = np.dot(mll_inv, cltt_masked)

    # stuff with alpha, beta, r, dr, and beam

    l, r, dr, alpha, beta = np.loadtxt(h._fn_alphabeta, 
                                usecols=(0,1,2,3,4), unpack=True, skiprows=3)

    l = np.unique(l)
    r = np.unique(r)[::-1]

    nr = len(r)

    if (run_type == 'data' or run_type == 'sim'):
        cltt_denom = np.load('output/cltt_theory.npy') #replace with 'output/na_cltt.npy'
        cltt_denom = cltt_denom[:nl]
    elif (run_type == 'fnl'):
        cltt_denom = np.loadtxt('joe/cl_wmap5_bao_sn.dat', usecols=(1,), unpack=True)

    alpha = alpha.reshape(len(l), nr)
    beta = beta.reshape(len(l), nr)
    dr = dr.reshape(len(l), nr)
    dr = dr[0]
    
    if (run_type != 'fnl'):
        beam = np.load(h._fn_beam)
    else:
        #beam = np.ones(len(cltt_denom))
        beam = np.load(h._fn_beam)
        noise = np.zeros(len(cltt_denom))
        nlm = hp.synalm(noise, lmax=nl)

    ####### TEMPORARY -- change beam #######
    #beam = np.ones(len(cltt_denom))
    #noise = np.zeros(len(cltt_denom))
    #nlm = hp.synalm(noise, lmax=nl)
    ########################################

    l = l[:nl]
    beam = beam[:nl]
    alpha = alpha[:nl,:]
    beta = beta[:nl,:]

    if (i_rank == 0):
        print "nr: %i, nl: %i" % (nr, nl)

    f_t2 = time.time()

    if (i_rank == 0):
        print ""
        print "Time to create alms from maps: %.2f s" % (f_t2 - f_t1)
        print "Calculating full skewness power spectrum..."

    cl21 = np.zeros(nl)
    work = np.zeros(1, dtype='i')
    result = np.zeros(nl, dtype='d')

    # master loop
    if (i_rank == 0):

        # send initial jobs

        for i_rank_out in range(1,i_size):

            work = np.array([i_rank_out-1], dtype='i')
            o_comm.Send([work, MPI.INT], dest=i_rank_out, tag=i_work_tag)

        for i_r in range(i_size-1,nr):

            if (i_r % (nr / 10) == 0):
                print "Finished %i%% of jobs... (%.2f s)" % (i_r * 100 / nr,
                time.time() - f_t2)

            work = np.array([i_r], dtype='i')

            o_comm.Recv([result, MPI.DOUBLE], source=MPI.ANY_SOURCE, 
                status=o_status, tag=MPI.ANY_TAG)

            #print "received results from core %i" % o_status.Get_source()

            o_comm.Send([work,MPI.INT], dest=o_status.Get_source(), 
                tag=i_work_tag)

            cl21 += result

        for i_rank_out in range(1,i_size):

            o_comm.Recv([result, MPI.DOUBLE], source=MPI.ANY_SOURCE,
                status=o_status, tag=MPI.ANY_TAG)

            cl21 += result
            print "cl21 = %.6f, result = %.6f" % (np.average(cl21), np.average(result))

            o_comm.Send([np.array([9999], dtype='i'), MPI.INT], 
                dest=o_status.Get_source(), tag=i_die_tag)

    #slave loop:
    else:

        while(1):

            o_comm.Recv([work, MPI.INT], source=0, status=o_status, 
                tag=MPI.ANY_TAG)

            if (o_status.Get_tag() == i_die_tag):

                break

            i_r = work[0]
            #print ("i_r: %i" % i_r)
            #print ("alm_masked: ", alm_masked)
            #print ("cltt_denom: ", cltt_denom)
            #print ("beam: ", beam)
            #print ("mask: ", mask)
            #print ("fn_map: ", fn_map)

            # create Alm, Blm (almxfl with alm_masked and beta / cltt_denom 
            # * beam, etc.)

            Alm = np.zeros(alm_masked.shape[0],complex)
            Blm = np.zeros(alm_masked.shape[0],complex)
            clAB2 = np.zeros(nl+1)
            clABB = np.zeros(nl+1)

            #Alm = hp.almxfl(alm_masked, alpha[:,i_r] / cltt_denom * beam)
            #Blm = hp.almxfl(alm_masked, beta[:,i_r] / cltt_denom * beam)
            #for li in xrange(2,nl):
            #    I = hp.Alm.getidx(nl,li,np.arange(min(nl,li)+1))
            #    Alm[I]=alpha[li-2][i_r]*(alm_masked[I]*beam[li]+nlm[I])/(cltt_denom[li]*beam[li]**2+noise[li])
            #    Blm[I]=beta[li-2][i_r]*(alm_masked[I]*beam[li]+nlm[I])/(cltt_denom[li]*beam[li]**2+noise[li])
            if (run_type == 'fnl'):
                for li in xrange(2,nl):
                    I = hp.Alm.getidx(nl,li,np.arange(min(nl,li)+1))
                    Alm[I]=alpha[li-2][i_r]*(alm_masked[I]*beam[li]+nlm[I])/(cltt_denom[li]*beam[li]**2+noise[li])
                    Blm[I]=beta[li-2][i_r]*(alm_masked[I]*beam[li]+nlm[I])/(cltt_denom[li]*beam[li]**2+noise[li])
            else:
                for li in xrange(2,nl):
                    I = hp.Alm.getidx(nl,li,np.arange(min(nl,li)+1))
                    Alm[I]=alpha[li-2][i_r]*(alm_masked[I])/cltt_denom[li]*beam[li]
                    Blm[I]=beta[li-2][i_r]*(alm_masked[I])/cltt_denom[li]*beam[li]

            ############################# DEBUG ################################
            if i_r == 0:
                cltt_Alm = hp.alm2cl(Alm)
                cltt_Blm = hp.alm2cl(Blm)
                np.savetxt('debug2/cltt_%s_Alm.dat' % run_type, cltt_Alm)
                np.savetxt('debug2/cltt_%s_Blm.dat' % run_type, cltt_Blm)
            ####################################################################

            #An = hp.alm2map(Alm, nside=nside, fwhm=0.00145444104333,
            #    verbose=False)
            #Bn = hp.alm2map(Blm, nside=nside, fwhm=0.00145444104333,
            #    verbose=False)
            An = hp.alm2map(Alm, nside=nside)
            Bn = hp.alm2map(Blm, nside=nside)

            ############################# DEBUG ################################
            if i_r == 0:
                cltt_An = hp.anafast(An)
                cltt_Bn = hp.anafast(Bn)
                np.savetxt('debug2/cltt_%s_An.dat' % run_type, cltt_An)
                np.savetxt('debug2/cltt_%s_Bn.dat' % run_type, cltt_Bn)
            ####################################################################

            An = An * mask
            Bn = Bn * mask

            ############################# DEBUG ################################
            #if i_r == 0:
            #    print "saving alpha, beta for %i" % i_r
            #    np.savetxt('debug2/alpha_ir_%i' % i_r, alpha[:,i_r])
            #    np.savetxt('debug2/beta_ir_%i' % i_r, beta[:,i_r])
            #    print "(An * Bn)[:10] == An[:10] * Bn[:10]:", (An * Bn)[:10] == An[:10] * Bn[:10]
            
            ####################################################################

            B2lm = hp.map2alm(Bn*Bn, lmax=nl)
            ABlm = hp.map2alm(An*Bn, lmax=nl)

            ############################# DEBUG ################################
            if i_r == 0:
                cltt_B2lm = hp.alm2cl(B2lm)
                cltt_ABlm = hp.alm2cl(ABlm)
                np.savetxt('debug2/cltt_%s_B2lm.dat' % run_type, cltt_B2lm)
                np.savetxt('debug2/cltt_%s_ABlm.dat' % run_type, cltt_ABlm)
            ####################################################################

            #clAB2 = hp.alm2cl(Alm, B2lm, lmax=nl)
            #clABB = hp.alm2cl(ABlm, Blm, lmax=nl)
            for li in xrange(2,nl+1):
                I = hp.Alm.getidx(nl,li,np.arange(min(nl,li)+1))
                clAB2[li] = (Alm[I[0]]*B2lm[I[0]].conj()
                        +2.*sum(Alm[I[1:]]*B2lm[I[1:]].conj()))/(2.0*li+1.0)
                clABB[li] = (Blm[I[0]]*ABlm[I[0]].conj()
                        +2.*sum(Blm[I[1:]]*ABlm[I[1:]].conj()))/(2.0*li+1.0)


            ############################# DEBUG ################################
            if i_r == 0:
                np.savetxt('debug2/clAB2_%s.dat' % run_type, clAB2)
                np.savetxt('debug2/clABB_%s.dat' % run_type, clABB)
            ####################################################################

            clAB2 = clAB2[1:]
            clABB = clABB[1:]

            result = np.zeros(nl, dtype='d')
            result += (clAB2 + 2 * clABB) * r[i_r]**2. * dr[i_r]

            ############################# DEBUG ################################
            np.savetxt('debug2/cl21_%s.dat' % run_type, result)
            ####################################################################

            print ("finished work for r=%i, dr=%.2f, avg(alpha)=%.2f, avg(beta)=%.2f, avg(result)=%.4g" % 
                (int(r[i_r]), dr[i_r], np.average(alpha[:,i_r]), 
                    np.average(beta[:,i_r]), np.average(result)))

            ############################# DEBUG ################################
            #if i_r == 0:
            #    print "finished debug -- goodbye!"
            #    exit()
            ####################################################################
            
            o_comm.Send([result,MPI.DOUBLE], dest=0, tag=1)

    f_t8 = time.time()

    if (i_rank == 0):
        print ""
        print ("Saving power spectrum to %s (not mll corrected)" 
            % fn_cl21_no_mll)

        np.savetxt(fn_cl21_no_mll, cl21)

        print ""
        print "Saving power spectrum to %s (mll corrected)" % fn_cl21
        
        cl21 = np.dot(mll_inv, cl21)
        np.savetxt(fn_cl21, cl21)

    return
Esempio n. 27
0
def coaddM5Analysis(path,
                    dbfile,
                    runName,
                    slair=False,
                    WFDandDDFs=False,
                    noDithOnly=False,
                    bestDithOnly=False,
                    someDithOnly=False,
                    specifiedDith=None,
                    nside=128,
                    filterBand='r',
                    includeDustExtinction=False,
                    saveunMaskedCoaddData=False,
                    pixelRadiusForMasking=5,
                    cutOffYear=None,
                    plotSkymap=True,
                    plotCartview=True,
                    unmaskedColorMin=None,
                    unmaskedColorMax=None,
                    maskedColorMin=None,
                    maskedColorMax=None,
                    nTicks=5,
                    plotPowerSpectrum=True,
                    showPlots=True,
                    saveFigs=True,
                    almAnalysis=True,
                    raRange=[-50, 50],
                    decRange=[-65, 5],
                    saveMaskedCoaddData=True):
    """

    Analyze the artifacts induced in the coadded 5sigma depth due to imperfect observing strategy.
      - Creates an output directory for subdirectories containing the specified things to save.
      - Creates, shows, and saves comparison plots.
      - Returns the metricBundle object containing the calculated coadded depth, and the output directory name.

    Required Parameters
    -------------------
      * path: str: path to the main directory where output directory is to be saved.
      * dbfile: str: path to the OpSim output file, e.g. to a copy of enigma_1189
      * runName: str: run name tag to identify the output of specified OpSim output, e.g. 'enigma1189' 

    Optional Parameters
    -------------------
      * slair: boolean: set to True if analysis on a SLAIR output.
                        Default: False
      * WFDandDDFs: boolean: set to True if want to consider both WFD survet and DDFs. Otherwise will only work
                             with WFD. Default: False
      * noDithOnly: boolean: set to True if only want to consider the undithered survey. Default: False
      * bestDithOnly: boolean: set to True if only want to consider RandomDitherFieldPerVisit.
                               Default: False
      * someDithOnly: boolean: set to True if only want to consider undithered and a few dithered surveys. 
                               Default: False
      * specifiedDith: str: specific dither strategy to run.
                            Default: None
      * nside: int: HEALpix resolution parameter. Default: 128
      * filterBand: str: any one of 'u', 'g', 'r', 'i', 'z', 'y'. Default: 'r'
      * includeDustExtinction: boolean: set to include dust extinction. Default: False
      * saveunMaskedCoaddData: boolean: set to True to save data before border masking. Default: False
      * pixelRadiusForMasking: int: number of pixels to mask along the shallow border. Default: 5

      * cutOffYear: int: year cut to restrict analysis to only a subset of the survey. 
                         Must range from 1 to 9, or None for the full survey analysis (10 yrs).
                         Default: None
      * plotSkymap: boolean: set to True if want to plot skymaps. Default: True
      * plotCartview: boolean: set to True if want to plot cartview plots. Default: False
      * unmaskedColorMin: float: lower limit on the colorscale for unmasked skymaps. Default: None
      * unmaskedColorMax: float: upper limit on the colorscale for unmasked skymaps. Default: None

      * maskedColorMin: float: lower limit on the colorscale for border-masked skymaps. Default: None
      * maskedColorMax: float: upper limit on the colorscale for border-masked skymaps. Default: None
      * nTicks: int: (number of ticks - 1) on the skymap colorbar. Default: 5
      * plotPowerSpectrum: boolean: set to True if want to plot powerspectra. Default: True

      * showPlots: boolean: set to True if want to show figures. Default: True
      * saveFigs: boolean: set to True if want to save figures. Default: True
      
      * almAnalysis: boolean: set to True to perform the alm analysis. Default: True
      * raRange: float array: range of right ascention (in degrees) to consider in alm  cartview plot;
                              applicable when almAnalysis=True. Default: [-50,50]
      * decRange: float array: range of declination (in degrees) to consider in alm cartview plot; 
                               applicable when almAnalysis=True. Default: [-65,5]
      * saveMaskedCoaddData: boolean: set to True to save the coadded depth data after the border
                                      masking. Default: True

    """
    # ------------------------------------------------------------------------
    # read in the database
    if slair:
        # slair database
        opsdb = db.Database(dbfile, defaultTable='observations')
    else:
        # OpSim database
        opsdb = db.OpsimDatabase(dbfile)

    # ------------------------------------------------------------------------
    # set up the outDir
    zeropt_tag = ''
    if cutOffYear is not None: zeropt_tag = '%syearCut' % cutOffYear
    else: zeropt_tag = 'fullSurveyPeriod'

    if includeDustExtinction: dust_tag = 'withDustExtinction'
    else: dust_tag = 'noDustExtinction'

    regionType = ''
    if WFDandDDFs: regionType = 'WFDandDDFs_'

    outDir = 'coaddM5Analysis_%snside%s_%s_%spixelRadiusForMasking_%sBand_%s_%s_directory' % (
        regionType, nside, dust_tag, pixelRadiusForMasking, filterBand,
        runName, zeropt_tag)
    print('# outDir: %s' % outDir)
    resultsDb = db.ResultsDb(outDir=outDir)

    # ------------------------------------------------------------------------
    # set up the sql constraint
    if WFDandDDFs:
        if cutOffYear is not None:
            nightCutOff = (cutOffYear) * 365.25
            sqlconstraint = 'night<=%s and filter=="%s"' % (nightCutOff,
                                                            filterBand)
        else:
            sqlconstraint = 'filter=="%s"' % filterBand
    else:
        # set up the propID and units on the ra, dec
        if slair:  # no prop ID; only WFD is simulated.
            wfdWhere = ''
            raDecInDeg = True
        else:
            propIds, propTags = opsdb.fetchPropInfo()
            wfdWhere = '%s and ' % opsdb.createSQLWhere('WFD', propTags)
            raDecInDeg = opsdb.raDecInDeg
        # set up the year cutoff
        if cutOffYear is not None:
            nightCutOff = (cutOffYear) * 365.25
            sqlconstraint = '%snight<=%s and filter=="%s"' % (
                wfdWhere, nightCutOff, filterBand)
        else:
            sqlconstraint = '%sfilter=="%s"' % (wfdWhere, filterBand)
    print('# sqlconstraint: %s' % sqlconstraint)

    # ------------------------------------------------------------------------
    # setup all the slicers
    slicer = {}
    stackerList = {}

    if specifiedDith is not None:  # would like to add all the stackers first and then keep only the one that is specified
        bestDithOnly, noDithOnly = False, False

    if bestDithOnly:
        stackerList['RandomDitherFieldPerVisit'] = [
            mafStackers.RandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
                                                         randomSeed=1000)
        ]
        slicer['RandomDitherFieldPerVisit'] = slicers.HealpixSlicer(
            lonCol='randomDitherFieldPerVisitRa',
            latCol='randomDitherFieldPerVisitDec',
            latLonDeg=raDecInDeg,
            nside=nside,
            useCache=False)
    else:
        if slair:
            slicer['NoDither'] = slicers.HealpixSlicer(lonCol='RA',
                                                       latCol='dec',
                                                       latLonDeg=raDecInDeg,
                                                       nside=nside,
                                                       useCache=False)
        else:
            slicer['NoDither'] = slicers.HealpixSlicer(lonCol='fieldRA',
                                                       latCol='fieldDec',
                                                       latLonDeg=raDecInDeg,
                                                       nside=nside,
                                                       useCache=False)
        if someDithOnly and not noDithOnly:
            #stackerList['RepulsiveRandomDitherFieldPerVisit'] = [myStackers.RepulsiveRandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            #slicer['RepulsiveRandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerVisitRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerVisitDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            slicer['SequentialHexDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='hexDitherFieldPerNightRa',
                latCol='hexDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['PentagonDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='pentagonDitherPerSeasonRa',
                latCol='pentagonDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
        elif not noDithOnly:
            # random dithers on different timescales
            stackerList['RandomDitherPerNight'] = [
                mafStackers.RandomDitherPerNightStacker(degrees=raDecInDeg,
                                                        randomSeed=1000)
            ]
            stackerList['RandomDitherFieldPerNight'] = [
                mafStackers.RandomDitherFieldPerNightStacker(
                    degrees=raDecInDeg, randomSeed=1000)
            ]
            stackerList['RandomDitherFieldPerVisit'] = [
                mafStackers.RandomDitherFieldPerVisitStacker(
                    degrees=raDecInDeg, randomSeed=1000)
            ]

            # rep random dithers on different timescales
            #stackerList['RepulsiveRandomDitherPerNight'] = [myStackers.RepulsiveRandomDitherPerNightStacker(degrees=raDecInDeg,
            #                                                                                                randomSeed=1000)]
            #stackerList['RepulsiveRandomDitherFieldPerNight'] = [myStackers.RepulsiveRandomDitherFieldPerNightStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            #stackerList['RepulsiveRandomDitherFieldPerVisit'] = [myStackers.RepulsiveRandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            # set up slicers for different dithers
            # random dithers on different timescales
            slicer['RandomDitherPerNight'] = slicers.HealpixSlicer(
                lonCol='randomDitherPerNightRa',
                latCol='randomDitherPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['RandomDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='randomDitherFieldPerNightRa',
                latCol='randomDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['RandomDitherFieldPerVisit'] = slicers.HealpixSlicer(
                lonCol='randomDitherFieldPerVisitRa',
                latCol='randomDitherFieldPerVisitDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            # rep random dithers on different timescales
            #slicer['RepulsiveRandomDitherPerNight'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherPerNightRa',
            #                                                               latCol='repulsiveRandomDitherPerNightDec',
            #                                                               latLonDeg=raDecInDeg, nside=nside, useCache=False)
            #slicer['RepulsiveRandomDitherFieldPerNight'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerNightRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerNightDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            #slicer['RepulsiveRandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerVisitRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerVisitDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            # spiral dithers on different timescales
            slicer['FermatSpiralDitherPerNight'] = slicers.HealpixSlicer(
                lonCol='fermatSpiralDitherPerNightRa',
                latCol='fermatSpiralDitherPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['FermatSpiralDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='fermatSpiralDitherFieldPerNightRa',
                latCol='fermatSpiralDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['FermatSpiralDitherFieldPerVisit'] = slicers.HealpixSlicer(
                lonCol='fermatSpiralDitherFieldPerVisitRa',
                latCol='fermatSpiralDitherFieldPerVisitDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            # hex dithers on different timescales
            slicer['SequentialHexDitherPerNight'] = slicers.HealpixSlicer(
                lonCol='hexDitherPerNightRa',
                latCol='hexDitherPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['SequentialHexDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='hexDitherFieldPerNightRa',
                latCol='hexDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['SequentialHexDitherFieldPerVisit'] = slicers.HealpixSlicer(
                lonCol='hexDitherFieldPerVisitRa',
                latCol='hexDitherFieldPerVisitDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            # per season dithers
            slicer['PentagonDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='pentagonDitherPerSeasonRa',
                latCol='pentagonDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['PentagonDiamondDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='pentagonDiamondDitherPerSeasonRa',
                latCol='pentagonDiamondDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['SpiralDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='spiralDitherPerSeasonRa',
                latCol='spiralDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
    if specifiedDith is not None:
        stackerList_, slicer_ = {}, {}
        if specifiedDith in slicer.keys():
            if specifiedDith.__contains__(
                    'Random'
            ):  # only Random dithers have a stacker object for rand seed specification
                stackerList_[specifiedDith] = stackerList[specifiedDith]
            slicer_[specifiedDith] = slicer[specifiedDith]
        else:
            raise ValueError(
                'Invalid value for specifiedDith: %s. Allowed values include one of the following:\n%s'
                % (specifiedDith, slicer.keys()))
        stackerList, slicer = stackerList_, slicer_

    # ------------------------------------------------------------------------
    if slair:
        m5Col = 'fivesigmadepth'
    else:
        m5Col = 'fiveSigmaDepth'
    # set up the metric
    if includeDustExtinction:
        # include dust extinction when calculating the co-added depth
        coaddMetric = metrics.ExgalM5(m5Col=m5Col, lsstFilter=filterBand)
    else:
        coaddMetric = metrics.Coaddm5Metric(m5col=m5col)
    dustMap = maps.DustMap(
        interp=False, nside=nside
    )  # include dustMap; actual in/exclusion of dust is handled by the galaxyCountMetric

    # ------------------------------------------------------------------------
    # set up the bundle
    coaddBundle = {}
    for dither in slicer:
        if dither in stackerList:
            coaddBundle[dither] = metricBundles.MetricBundle(
                coaddMetric,
                slicer[dither],
                sqlconstraint,
                stackerList=stackerList[dither],
                runName=runName,
                metadata=dither,
                mapsList=[dustMap])
        else:
            coaddBundle[dither] = metricBundles.MetricBundle(
                coaddMetric,
                slicer[dither],
                sqlconstraint,
                runName=runName,
                metadata=dither,
                mapsList=[dustMap])

    # ------------------------------------------------------------------------
    # run the analysis
    if includeDustExtinction:
        print('\n# Running coaddBundle with dust extinction ...')
    else:
        print('\n# Running coaddBundle without dust extinction ...')
    cGroup = metricBundles.MetricBundleGroup(coaddBundle,
                                             opsdb,
                                             outDir=outDir,
                                             resultsDb=resultsDb,
                                             saveEarly=False)
    cGroup.runAll()

    # ------------------------------------------------------------------------
    # plot and save the data
    plotBundleMaps(path,
                   outDir,
                   coaddBundle,
                   dataLabel='$%s$-band Coadded Depth' % filterBand,
                   filterBand=filterBand,
                   dataName='%s-band Coadded Depth' % filterBand,
                   skymap=plotSkymap,
                   powerSpectrum=plotPowerSpectrum,
                   cartview=plotCartview,
                   colorMin=unmaskedColorMin,
                   colorMax=unmaskedColorMax,
                   nTicks=nTicks,
                   showPlots=showPlots,
                   saveFigs=saveFigs,
                   outDirNameForSavedFigs='coaddM5Plots_unmaskedBorders')
    print('\n# Done saving plots without border masking.\n')

    # ------------------------------------------------------------------------
    plotHandler = plots.PlotHandler(outDir=outDir,
                                    resultsDb=resultsDb,
                                    thumbnail=False,
                                    savefig=False)

    print(
        '# Number of pixels in the survey region (before masking the border):')
    for dither in coaddBundle:
        print(
            '  %s: %s' %
            (dither,
             len(np.where(coaddBundle[dither].metricValues.mask == False)[0])))

    # ------------------------------------------------------------------------
    # save the unmasked data?
    if saveunMaskedCoaddData:
        outDir_new = 'unmaskedCoaddData'
        if not os.path.exists('%s%s/%s' % (path, outDir, outDir_new)):
            os.makedirs('%s%s/%s' % (path, outDir, outDir_new))
        saveBundleData_npzFormat('%s%s/%s' % (path, outDir, outDir_new),
                                 coaddBundle, 'coaddM5Data_unmasked',
                                 filterBand)

    # ------------------------------------------------------------------------
    # mask the edges
    print('\n# Masking the edges for coadd ...')
    coaddBundle = maskingAlgorithmGeneralized(
        coaddBundle,
        plotHandler,
        dataLabel='$%s$-band Coadded Depth' % filterBand,
        nside=nside,
        pixelRadius=pixelRadiusForMasking,
        plotIntermediatePlots=False,
        plotFinalPlots=False,
        printFinalInfo=True)
    if (pixelRadiusForMasking > 0):
        # plot and save the masked data
        plotBundleMaps(path,
                       outDir,
                       coaddBundle,
                       dataLabel='$%s$-band Coadded Depth' % filterBand,
                       filterBand=filterBand,
                       dataName='%s-band Coadded Depth' % filterBand,
                       skymap=plotSkymap,
                       powerSpectrum=plotPowerSpectrum,
                       cartview=plotCartview,
                       colorMin=maskedColorMin,
                       colorMax=maskedColorMax,
                       nTicks=nTicks,
                       showPlots=showPlots,
                       saveFigs=saveFigs,
                       outDirNameForSavedFigs='coaddM5Plots_maskedBorders')
        print('\n# Done saving plots with border masking. \n')

    # ------------------------------------------------------------------------
    # Calculate total power
    summarymetric = metrics.TotalPowerMetric()
    for dither in coaddBundle:
        coaddBundle[dither].setSummaryMetrics(summarymetric)
        coaddBundle[dither].computeSummaryStats()
        print('# Total power for %s case is %f.' %
              (dither, coaddBundle[dither].summaryValues['TotalPower']))
    print('')

    # ------------------------------------------------------------------------
    # run the alm analysis
    if almAnalysis:
        almPlots(path,
                 outDir,
                 copy.deepcopy(coaddBundle),
                 nside=nside,
                 filterband=filterBand,
                 raRange=raRange,
                 decRange=decRange,
                 showPlots=showPlots)
    # ------------------------------------------------------------------------
    # save the masked data?
    if saveMaskedCoaddData and (pixelRadiusForMasking > 0):
        outDir_new = 'maskedCoaddData'
        if not os.path.exists('%s%s/%s' % (path, outDir, outDir_new)):
            os.makedirs('%s%s/%s' % (path, outDir, outDir_new))
        saveBundleData_npzFormat('%s%s/%s' % (path, outDir, outDir_new),
                                 coaddBundle, 'coaddM5Data_masked', filterBand)

    # ------------------------------------------------------------------------
    # plot comparison plots
    if len(coaddBundle.keys()) > 1:  # more than one key
        # set up the directory
        outDir_comp = 'coaddM5ComparisonPlots'
        if not os.path.exists('%s%s/%s' % (path, outDir, outDir_comp)):
            os.makedirs('%s%s/%s' % (path, outDir, outDir_comp))
        # ------------------------------------------------------------------------
        # plot for the power spectra
        cl = {}
        for dither in plotColor:
            if dither in coaddBundle:
                cl[dither] = hp.anafast(hp.remove_dipole(
                    coaddBundle[dither].metricValues.filled(
                        coaddBundle[dither].slicer.badval)),
                                        lmax=500)
                ell = np.arange(np.size(cl[dither]))
                plt.plot(ell, (cl[dither] * ell * (ell + 1)) / 2.0 / np.pi,
                         color=plotColor[dither],
                         linestyle='-',
                         label=dither)
        plt.xlabel(r'$\ell$')
        plt.ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
        plt.xlim(0, 500)
        fig = plt.gcf()
        fig.set_size_inches(12.5, 10.5)
        leg = plt.legend(labelspacing=0.001)
        for legobj in leg.legendHandles:
            legobj.set_linewidth(4.0)
        filename = 'powerspectrum_comparison_all.png'
        plt.savefig('%s%s/%s/%s' % (path, outDir, outDir_comp, filename),
                    bbox_inches='tight',
                    format='png')
        plt.show()

        # create the histogram
        scale = hp.nside2pixarea(nside, degrees=True)

        def tickFormatter(y, pos):
            return '%d' % (y * scale)  # convert pixel count to area

        binsize = 0.01
        for dither in plotColor:
            if dither in coaddBundle:
                ind = np.where(
                    coaddBundle[dither].metricValues.mask == False)[0]
                binAll = int(
                    (max(coaddBundle[dither].metricValues.data[ind]) -
                     min(coaddBundle[dither].metricValues.data[ind])) /
                    binsize)
                plt.hist(coaddBundle[dither].metricValues.data[ind],
                         bins=binAll,
                         label=dither,
                         histtype='step',
                         color=plotColor[dither])
        ax = plt.gca()
        ymin, ymax = ax.get_ylim()
        nYticks = 10.
        wantedYMax = ymax * scale
        wantedYMax = 10. * np.ceil(float(wantedYMax) / 10.)
        increment = 5. * np.ceil(float(wantedYMax / nYticks) / 5.)
        wantedArray = np.arange(0, wantedYMax, increment)
        ax.yaxis.set_ticks(wantedArray / scale)
        ax.yaxis.set_major_formatter(FuncFormatter(tickFormatter))
        plt.xlabel('$%s$-band Coadded Depth' % filterBand)
        plt.ylabel('Area (deg$^2$)')
        fig = plt.gcf()
        fig.set_size_inches(12.5, 10.5)
        leg = plt.legend(labelspacing=0.001, loc=2)
        for legobj in leg.legendHandles:
            legobj.set_linewidth(2.0)
        filename = 'histogram_comparison.png'
        plt.savefig('%s%s/%s/%s' % (path, outDir, outDir_comp, filename),
                    bbox_inches='tight',
                    format='png')
        plt.show()
        # ------------------------------------------------------------------------
        # plot power spectra for the separte panel
        totKeys = len(list(coaddBundle.keys()))
        if (totKeys > 1):
            plt.clf()
            nCols = 2
            nRows = int(np.ceil(float(totKeys) / nCols))
            fig, ax = plt.subplots(nRows, nCols)
            plotRow = 0
            plotCol = 0
            for dither in list(plotColor.keys()):
                if dither in list(coaddBundle.keys()):
                    ell = np.arange(np.size(cl[dither]))
                    ax[plotRow, plotCol].plot(ell, (cl[dither] * ell *
                                                    (ell + 1)) / 2.0 / np.pi,
                                              color=plotColor[dither],
                                              label=dither)
                    if (plotRow == nRows - 1):
                        ax[plotRow, plotCol].set_xlabel(r'$\ell$')
                    ax[plotRow,
                       plotCol].set_ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
                    ax[plotRow,
                       plotCol].yaxis.set_major_locator(MaxNLocator(3))
                    if (dither != 'NoDither'):
                        ax[plotRow, plotCol].set_ylim(0, 0.0035)
                    ax[plotRow, plotCol].set_xlim(0, 500)
                    plotRow += 1
                    if (plotRow > nRows - 1):
                        plotRow = 0
                        plotCol += 1
            fig.set_size_inches(20, int(nRows * 30 / 7.))
            filename = 'powerspectrum_sepPanels.png'
            plt.savefig('%s%s/%s/%s' % (path, outDir, outDir_comp, filename),
                        bbox_inches='tight',
                        format='png')
            plt.show()
    return coaddBundle, outDir
Map = create_namedtuple_for_map(OPTIONS)

########################################
# Read the input maps in memory

INPUT_MAPS = []
for cur_title, cur_map_file in zip(*[iter(ARGS)] * 2):
    try:
        log.info('reading map `%s\'...', cur_map_file)
        cur_map = healpy.read_map(cur_map_file, field=OPTIONS.stokes_component)

        if OPTIONS.no_monopole:
            result = healpy.remove_monopole(cur_map, fitval=True, copy=False)
            log.info('monopole has been removed (%s)', str(result))
        if OPTIONS.no_dipole:
            result = healpy.remove_dipole(cur_map, fitval=True, copy=False)
            log.info('dipole has been removed (amplitude: %s)',
                     str(result))
            
        cur_map[cur_map < -1.6e+30] = np.NaN

        if type(OPTIONS.mask) is not None:
            cur_map[OPTIONS.mask == 0] = np.NaN

        cur_entry = dict(title=cur_title,
                         pixels=cur_map,
                         input_file=cur_map_file)
        if OPTIONS.plot_distributions:
            cur_entry['histogram'] = np.histogram(cur_map[~np.isnan(cur_map)],
                                                  bins=50)
        INPUT_MAPS.append(Map(**cur_entry))
nl = 1024
nside = 512
###################################

# Read in cl, alpha(r) and beta(r)
# l, cl = loadtxt('cl_wmap5_bao_sn.dat',usecols=(0,1),unpack=True)
# R, a, b = loadtxt("total.dat", usecols = (0,3,4), unpack=True)

############## DEBUG ##############
fnl = 30
nsim = 1

fn_map = "../data/fnl_sims/map_fnl_%i_sim_%i.fits" % (int(fnl), nsim)
map_fnl = hp.read_map(fn_map)
map_fnl /= 1e6 * 2.7
map_processed = hp.remove_dipole(map_fnl)
alm_fnl = hp.map2alm(map_processed, lmax=nl)

# cl = hp.anafast(map_processed)
cl = load("../output/cltt_theory.npy")

# load and resize things

l, R, dR, a, b = loadtxt("../data/l_r_alpha_beta.txt", usecols=(0, 1, 2, 3, 4), unpack=True, skiprows=3)

l = unique(l)
R = unique(R)[::-1]
l = l[:nl]

nR = len(R)
nl = len(l)