コード例 #1
0
 def setUp(self):
     self.path = os.path.dirname( os.path.realpath( __file__ ) )
     try:
         self.map1 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_W_v4.fits'), (0,1,2))]
         self.map2 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_V_v4.fits'), (0,1,2))]
         self.mask = hp.read_map(os.path.join(self.path, 'data', 'wmap_temperature_analysis_mask_r9_7yr_v4.fits')).astype(np.bool)
     except exceptions.IOError:
         warnings.warn("""Missing Wmap test maps from the data folder, please download them from Lambda and copy them in the test/data folder:
         http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_iqumap_r9_7yr_W_v4.fits
         http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_iqumap_r9_7yr_V_v4.fits
         http://lambda.gsfc.nasa.gov/data/map/dr4/ancillary/masks/wmap_temperature_analysis_mask_r9_7yr_v4.fits
         on Mac or Linux you can run the bash script get_wmap_maps.sh from the same folder
         """)
     for m in chain(self.map1, self.map2):
         m.mask = np.logical_not(self.mask)
     self.cla = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_fortran.fits'))
     cls = pyfits.open(os.path.join(self.path, 'data',
                                    'cl_iqu_wmap_fortran.fits'))[1].data
     # order of HEALPIX is TB, EB while in healpy is EB, TB
     self.cliqu = [cls.field(i) for i in (0,1,2,3,5,4)]
     nside = 32
     lmax = 64
     fwhm_deg = 7.
     seed = 12345
     np.random.seed(seed)
     self.mapiqu = hp.synfast(self.cliqu, nside, lmax=lmax, pixwin=False,
                              fwhm=np.radians(fwhm_deg), new=False)
コード例 #2
0
def surv_sum_diff(surveydict,ss=1,nside=128,freq=70,mask_ps=True,fwhm=10.0):
    """
    function to genreate smoothed difference between chosen survey and sum of the other 5 surveys from andreas interactive destriper/binner. uses common masks smooth to 10 degrees
    fwhm is in degrees, if zero or negative don't smooth at all, default 10 degrees
    """
    sumlist=surveydict.keys()
    sumlist.remove(ss)
    nsum=len(sumlist)
    m=hp.ma(np.array(surveydict[sumlist[0]]))/nsum
    totalmask=m.mask
    if mask_ps==True:
        psmask = np.logical_not(np.floor(hp.ud_grade(hp.read_map(glob('/project/projectdirs/planck/data/mission/DPC_maps/dx8/lfi/DX8_MASKs/' + 'mask_ps_%dGHz_*.fits' % freq)[0]), nside,order_out='NEST')))
        totalmask=m.mask | psmask
    for ss in sumlist[1:]:
        m1=hp.ma(np.array(surveydict[ss]))
        m=m+m1/nsum
        totalmask=m1.mask | totalmask
    m.mask=totalmask
    m.mask |= np.isnan(m)
    
    m1=hp.ma(np.array(surveydict[ss]))
    totalmask=totalmask | m1.mask
    d=(m1-m)/2.
    d=hp.ud_grade(d,nside,order_in='NEST',order_out='RING')
    if fwhm>0:
        dsm=hp.ma(hp.smoothing(d.filled(),fwhm*np.pi/180.))
        dsm.mask=m.mask
    if fwhm<=0:
        dsm=d
    hp.mollview(dsm,min=-1e-5,max=1e-5,title='SS'+np.str(ss)+ '  - sum of others')
    return dsm
コード例 #3
0
ファイル: test_sphtfunc.py プロジェクト: Rbeaty88/healpy
 def setUp(self):
     self.lmax = 64
     self.path = os.path.dirname( os.path.realpath( __file__ ) )
     self.map1 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_W_v4_udgraded32.fits'), (0,1,2))]
     self.map2 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_V_v4_udgraded32.fits'), (0,1,2))]
     self.mask = hp.read_map(os.path.join(self.path, 'data', 'wmap_temperature_analysis_mask_r9_7yr_v4_udgraded32.fits')).astype(np.bool)
     for m in chain(self.map1, self.map2):
         m.mask = np.logical_not(self.mask)
     self.cla = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_II_lmax64_rmmono_3iter.fits'))
     self.cl_fortran_nomask = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_II_lmax64_rmmono_3iter_nomask.fits'))
     cls_file = pyfits.open(os.path.join(self.path, 'data',
                                    'cl_wmap_band_iqumap_r9_7yr_W_v4_udgraded32_IQU_lmax64_rmmono_3iter.fits'))
     # fix for pyfits to read the file with duplicate column names
     for i in range(2, 6):
         cls_file[1].header['TTYPE%d' % i] += '-%d' % i
     cls = cls_file[1].data
     # order of HEALPIX is TB, EB while in healpy is EB, TB
     self.cliqu = [np.array(cls.field(i)) for i in (0,1,2,3,5,4)]
     nside = 32
     lmax = 64
     fwhm_deg = 7.
     seed = 12345
     np.random.seed(seed)
     self.mapiqu = hp.synfast(self.cliqu, nside, lmax=lmax, pixwin=False,
                              fwhm=np.radians(fwhm_deg), new=False)
コード例 #4
0
def read_and_diff_files_fast(f1,f2,nside=256,tmask=None,return_map=False):
    #assume tmask input is already degraded
    
    mm1=hp.read_map(f1,[0,1,2],verbose=False)
    mm2=hp.read_map(f2,[0,1,2],verbose=False)

    mmm1=[]
    mmm2=[]
    for m1,m2 in zip(mm1,mm2):
        m1=hp.ud_grade(hp.ma(m1),nside_out=nside)
        m2=hp.ud_grade(hp.ma(m2),nside_out=nside)
        tmask=m1.mask | m2.mask | tmask
        mmm1.append(m1)
        mmm2.append(m2)
    
    diff=[]
    for m1,m2 in zip(mmm1,mmm2):
        d=m1-m2
        d.mask=tmask
        diff.append(d)
    
    skyfrac=1-float(tmask.sum())/len(tmask)
        
    cldata=hp.anafast(diff)
    cldata_out=[]
    for cl in cldata:
        cldata_out.append(cl/skyfrac)
        
    if return_map is False:
        return cldata_out
    if return_map is True:
        return cldata_out,diff
コード例 #5
0
ファイル: test_visufunc.py プロジェクト: arahlin/healpy
 def setUp(self):
     self.nside = 16
     self.m = np.arange(hp.nside2npix(self.nside), dtype=np.double)
     self.ma = self.m.copy()
     self.ma[3] = hp.UNSEEN
     self.ma = hp.ma(self.ma)
     self.m2 = self.m.copy()
     self.m2[100:] = hp.UNSEEN
     self.ma2 = hp.ma(self.m2)
コード例 #6
0
ファイル: sharpl.py プロジェクト: sarojadhikari/powerasym
def remove_low_l(map1, mask1):
    map1m=hp.ma(map1)
    map1m.mask=np.logical_not(mask1)
    map2=np.ma.masked_values(map1, UNSEEN)
    alm2=hp.map2alm(map2, lmax=LMAX)
    fac2=np.array([1.]*(LMAX+1))
    fac2[0:lREM]=0.
    alm2n=hp.sphtfunc.almxfl(alm2, fac2, inplace=False)
    map2new=hp.ma(hp.alm2map(alm2n, 2048))
    map2new.mask=np.logical_not(mask1)
    return map2new
コード例 #7
0
def plot_figure(metadata):
    try:
        allmap = hp.ma(hp.read_map(os.path.join(root_folder, metadata["file_name"]), (0,1,2)))
    except exceptions.IndexError:
        allmap = [hp.ma(hp.read_map(os.path.join(root_folder, metadata["file_name"])))]
    for comp, m in zip("IQU", allmap):
        if comp in "QU":
            plot_range = 20
        else:
            plot_range = 20
        if len(allmap) == 1: #only T, single ch
            plot_range = 20
        is_single_channel = isinstance(metadata["channel"], basestring) and len(metadata["channel"])==6
        if is_single_channel:
            if int(metadata["channel"][3:5]) < 24: # 70GHz
                plot_range = 20
        test_type = metadata["base_file_name"].split("/")[0]
        if isinstance(metadata["channel"], list):
            metadata["channel"] = "_".join(metadata["channel"])
        if metadata["channel"].find("_") < 0:
            try:
                if int(metadata["channel"]) > 70:
                    plot_range = 5
                    if int(metadata["channel"]) >= 353:
                        if comp in "QU":
                            plot_range = 100
                        else:
                            plot_range = 30
                    if int(metadata["channel"]) >= 545:
                        if comp in "QU":
                            plot_range = 1e6
                        else:
                            plot_range = 500
                    if int(metadata["channel"]) >= 857:
                        plot_range = 1e5
                        if comp in "QU":
                            plot_range = 1e6
                    if test_type == "surveydiff" and int(metadata["channel"]) >= 545 and comp == "Q":
                        plot_range *= 1e2
            except exceptions.ValueError:
                pass

        fig = plt.figure(figsize=(9, 6), dpi=100)
        matplotlib.rcParams.update({'font.size': 14})
        hp.mollview(m * 1e6, min=-plot_range, max=plot_range, unit="uK", title=metadata["title"] + " %s" % comp, xsize=900, hold=True)
        plt.savefig(os.path.join(out_folder, metadata["file_name"].replace(".fits", "_%s.jpg" % comp)), dpi=100)
        plt.close()
        fig = plt.figure(figsize=(9, 6), dpi=20)
        fig.add_axes([0.01, 0.01, 0.98, 0.98])
        matplotlib.rcParams.update({'font.size': 30})
        hp.mollview(m * 1e6, min=-plot_range, max=plot_range, cbar=True, title="", xsize=180, hold=True)
        plt.savefig(os.path.join(out_folder, metadata["file_name"].replace(".fits", "_%s_thumb.jpg" % comp)), dpi=20)
        plt.close()
コード例 #8
0
ファイル: lwa_antenna.py プロジェクト: telegraphic/lwa_ant
    def to_healpix(self, n_side=32):
        """ Convert beam pattern to a healpix 
        
        Returns
        -------
        hmap: np.array
            Numpy array representing healpix map. Array is in observer frame,
            i.e. zenith aligned with equatorial coordinate system.
        """
        beam_azel = self.to_azel()
        
        el = np.linspace(0, np.pi, beam_azel.shape[1])
        az = np.linspace(-np.pi, np.pi, beam_azel.shape[0])[:, None]
        
        # Generate beam pattern with n_side = 32 (for gridding reasons)
        hmap = hp.ma(np.zeros(hp.nside2npix(32)))
        pix = hp.ang2pix(32, el, az)
        hmap[pix] = beam_azel
        
        # Upsample if required
        if n_side != 32:
            hmap = hp.ud_grade(hmap, nside_out=n_side)

        # Apply mask
        n_pix = hp.nside2npix(n_side)
        theta, phi = hp.pix2ang(n_side, np.arange(n_pix))
        mask1 = phi + np.pi/2 >  2 * np.pi 
        mask2 = phi < np.pi / 2
        hmap.mask = np.invert(np.logical_or(mask1, mask2))
                
        return hmap
コード例 #9
0
ファイル: Plots.py プロジェクト: soriaser/ChartGenerator
    def __getMaskedPixels(self, pixels):
        pixels_masked = healpy.ma(pixels)

        if (self.Configuration.isConfigured(ChartHealpixOptions.MASK)):
            pixels_masked.mask = healpy.mask_bad(pixels, badval = self.__getMaskedValue())

        return pixels_masked.filled(fill_value = numpy.nan)
コード例 #10
0
ファイル: mapmaking.py プロジェクト: kellyjanus/cofe
def qubin(pix, NSIDE, q_channel, u_channel, pa):

    npix = hp.nside2npix(NSIDE)
    q_channel_w, u_channel_w = get_qu_weights(pa)

    #create M
    M = {}
    for c1,c2 in itertools.combinations_with_replacement('QU',2):
        print (c1,c2)
        M[(c1,c2)] = pix2map(pix, NSIDE, q_channel_w[c1] * q_channel_w[c2] + u_channel_w[c1] * u_channel_w[c2], dividebyhits=False)[1].filled()

    #invert M
    invM = np.array([ 
            [[ M[('Q','Q')][x], M[('Q','U')][x] ],
                           [ M[('Q','U')][x], M[('U','U')][x] ]] for x in range(npix) ])
    for x,blockM in enumerate(invM):
        if blockM[0,0] != 0:
            invM[x] = np.linalg.inv(blockM)

    summap = hp.ma(np.hstack([
        pix2map(pix, NSIDE, q_channel * q_channel_w[c] + u_channel * u_channel_w[c], dividebyhits=False)[1].filled()[:,None] for c in 'QU'
        ]))
    summap.mask = summap == 0

    return M, invM, summap, weightmap(summap, invM)
コード例 #11
0
def test_smoothcombine():

    nside = 256
    freq = 30
    m = hp.ma(np.random.standard_normal(hp.nside2npix(nside)))
    m.mask = np.zeros(len(m), dtype=np.bool)

    Reader = reader.Readers[os.environ["NULLTESTS_ENV"]]

    mapreader = Reader(os.environ["DDX9_LFI"], nside=nside, baseline_length=None)

    ps_mask, gal_mask = mapreader.read_masks(freq)
    # ps_mask = False
    # gal_mask = False
    smooth_combine(
        [(m, 1)],
        [(np.ones_like(m), 1)],
        spectra=True,
        smooth_mask=ps_mask,
        spectra_mask=gal_mask,
        metadata={"file_type": ""},
    )

    # check chi-square
    metadata = json.load(open("out_map.json", "r"))
    assert np.abs(metadata["map_unsm_chi2"] - 1) < 0.01
    assert np.abs(metadata["map_chi2"] - 1) < 0.1
    # check spectrum
    cl = hp.read_cl("out_cl.fits")
    realization_wn = cl[200:].mean()
    assert np.abs(realization_wn - metadata["whitenoise_cl"]) < 1e-5
コード例 #12
0
def mask_map(sky_map, binary_mask=None, pol=True, ret_mask=False, fill_zeros=False):

    if binary_mask is None:
        binary_mask = np.logical_not(np.isnan(sky_map[0] if pol else sky_map))

    if fill_zeros:
        if pol:
            sky_map[0][np.isnan(sky_map[0])] = 0.0
            sky_map[1][np.isnan(sky_map[1])] = 0.0
            sky_map[2][np.isnan(sky_map[2])] = 0.0
        else:
            sky_map[np.isnan(sky_map)] = 0.0

    sky_map_masked = hp.ma(sky_map)

    if pol:
        sky_map_masked[0].mask = np.logical_not(binary_mask)
        sky_map_masked[1].mask = np.logical_not(binary_mask)
        sky_map_masked[2].mask = np.logical_not(binary_mask)
    else:
        sky_map_masked.mask = np.logical_not(binary_mask)

    if ret_mask:
        return sky_map_masked, binary_mask
    else:
        return sky_map_masked
コード例 #13
0
ファイル: cmbutils.py プロジェクト: sarojadhikari/powerasym
def get_hem_Cls(skymap, direction, LMAX=256, deg=90.):
    """
    from the given healpix skymap, return Cls for two hemispheres defined by the
    direction given, useful to study the possible scale dependence of power modulation

    direction should be a unit vector
    """
    # generate hemispherical mask
    NPIX=len(skymap)
    NSIDE=hp.npix2nside(NPIX)
    maskp=np.array([0.]*NPIX)
    disc=hp.query_disc(nside=NSIDE, vec=direction, radius=0.0174532925*deg)
    maskp[disc]=1.
    #skymap=hp.remove_monopole(skymap)
    map1=hp.ma(skymap)
    map1.mask=maskp
    Clsp=hp.anafast(map1, lmax=LMAX)
    if (deg<90.):
        maskm=np.array([0.]*NPIX)
        disc=hp.query_disc(nside=NSIDE, vec=-direction, radius=0.0174532925*deg)
        maskm[disc]=1.
        map1.mask=maskm
    else:
        map1.mask=np.logical_not(maskp)

    Clsm=hp.anafast(map1, lmax=LMAX)

    return [Clsp, Clsm]
コード例 #14
0
ファイル: test_sphtfunc.py プロジェクト: PaulPrice/healpy
 def setUp(self):
     self.path = os.path.dirname( os.path.realpath( __file__ ) )
     try:
         self.map1 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_W_v4.fits'), (0,1,2))]
         self.map2 = [hp.ma(m) for m in hp.read_map(os.path.join(self.path, 'data', 'wmap_band_iqumap_r9_7yr_V_v4.fits'), (0,1,2))]
         self.mask = hp.read_map(os.path.join(self.path, 'data', 'wmap_temperature_analysis_mask_r9_7yr_v4.fits')).astype(np.bool)
     except exceptions.IOError:
         warnings.warn("""Missing Wmap test maps from the data folder, please download them from Lambda and copy them in the test/data folder:
         http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_iqumap_r9_7yr_W_v4.fits
         http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_iqumap_r9_7yr_V_v4.fits
         http://lambda.gsfc.nasa.gov/data/map/dr4/ancillary/masks/wmap_temperature_analysis_mask_r9_7yr_v4.fits
         on Mac or Linux you can run the bash script get_wmap_maps.sh from the same folder
         """)
         raise
     for m in chain(self.map1, self.map2):
         m.mask = np.logical_not(self.mask)
     self.cla = hp.read_cl(os.path.join(self.path, 'data', 'cl_wmap_fortran.fits'))
コード例 #15
0
def pix2map(pix, nside, tod=None):
    """Pixel array to hitmap, if TOD with same lenght of PIX is provided, 
    it is binned to a map"""
    #TODO test case
    pix = pix.astype(np.int)
    ids = np.bincount(pix, weights=None)
    hitmap = np.ones(hp.nside2npix(nside)+1) * hp.UNSEEN
    hitmap[:len(ids)] = ids
    hitmap = hp.ma(hitmap[:-1])
    if tod is None:
        return hitmap
    else:
        ids_binned = np.bincount(pix, weights=tod)
        binned = np.ones(hp.nside2npix(nside)+1) * hp.UNSEEN
        binned[:len(ids_binned)] = ids_binned
        binned = hp.ma(binned[:-1])/hitmap
        return hitmap, binned
コード例 #16
0
ファイル: test_sphtfunc.py プロジェクト: Rbeaty88/healpy
 def test_smoothing_masked(self):
     smoothed = hp.smoothing(self.map1, fwhm=np.radians(10), lmax=self.lmax, regression=False)
     smoothed_f90 = hp.ma(hp.read_map(os.path.join(self.path, 'data',
               'wmap_band_iqumap_r9_7yr_W_v4_udgraded32_masked_smoothed10deg_fortran.fits'), (0,1,2)))
     # fortran does not restore the mask
     for mm in smoothed_f90:
         mm.mask = smoothed[0].mask
     for i in range(3):
         np.testing.assert_array_almost_equal(smoothed[i].filled(), smoothed_f90[i].filled(), decimal=6)
コード例 #17
0
 def load_CTBCORE_data(self):
     self.CTB_en_bins, self.CTB_count_maps, self.CTB_exposure_maps, self.CTB_psc_masks_temp = CTB.get_CTB(self.CTB_dir, self.nside, self.CTB_en_min, self.CTB_en_max,is_p8 = self.is_p8, eventclass = self.eventclass, eventtype = self.eventtype, newstyle = self.newstyle)
     self.CTB_map_um=np.sum(self.CTB_count_maps,axis=0)
     self.CTB_count_psc_maps = [hp.ma(self.CTB_count_map) for self.CTB_count_map in self.CTB_count_maps]
     for i in range(len(self.CTB_count_psc_maps)):
         self.CTB_count_psc_maps[i].mask = self.CTB_psc_masks_temp[i]
         
     self.total_exposure_map = np.mean(self.CTB_exposure_maps,axis=0)
     self.NPIX_total = np.size(self.total_exposure_map)
コード例 #18
0
ファイル: wmap_temperatures.py プロジェクト: BTY2684/astroML
def fetch_wmap_temperatures(masked=False, data_home=None,
                            download_if_missing=True):
    """Loader for WMAP temperature map data

    Parameters
    ----------
    masked : optional, default=False
        If True, then return the foreground-masked healpix array of data
        If False, then return the raw temperature array
    data_home : optional, default=None
        Specify another download and cache folder for the datasets. By default
        all scikit learn data is stored in '~/astroML_data' subfolders.

    download_if_missing : optional, default=True
        If False, raise a IOError if the data is not locally available
        instead of trying to download the data from the source site.

    Returns
    -------
    data : np.ndarray or np.ma.MaskedArray
        record array containing (masked) temperature data
    """
    # because of a bug in healpy, pylab must be imported before healpy is
    # or else a segmentation fault can result.
    import pylab
    import healpy as hp

    data_home = get_data_home(data_home)
    if not os.path.exists(data_home):
        os.makedirs(data_home)

    data_file = os.path.join(data_home, os.path.basename(DATA_URL))
    mask_file = os.path.join(data_home, os.path.basename(MASK_URL))

    if not os.path.exists(data_file):
        if not download_if_missing:
            raise IOError('data not present on disk. '
                          'set download_if_missing=True to download')
        data_buffer = download_with_progress_bar(DATA_URL)
        open(data_file, 'wb').write(data_buffer)

    data = hp.read_map(data_file)

    if masked:
        if not os.path.exists(mask_file):
            if not download_if_missing:
                raise IOError('mask data not present on disk. '
                              'set download_if_missing=True to download')
            mask_buffer = download_with_progress_bar(MASK_URL)
            open(mask_file, 'w').write(mask_buffer)

        mask = hp.read_map(mask_file)

        data = hp.ma(data)
        data.mask = np.logical_not(mask)  # WMAP mask has 0=bad. We need 1=bad

    return data
コード例 #19
0
def read_and_diff_files_fast_hitweight(f1,f2,fh1,fh2,fhfull,nside=256,tmask=None,return_map=False,remove_monopole=True):
    #assume tmask is already degraded, remove_monopole=True: remove from differences before anafast
    #monopole is removed from I,Q U maps after masking
  
    mm1=hp.read_map(f1,[0,1,2],verbose=False)
    mm2=hp.read_map(f2,[0,1,2],verbose=False)
    h1=hp.read_map(fh1,[3],verbose=False)
    h2=hp.read_map(fh2,[3],verbose=False)
    hit_full=hp.read_map(fhfull,[3],verbose=False)
    
    nsh=hp.npix2nside(len(h1))
    if nsh != nside:
        h1=hp.ud_grade(h1,nside_out=nside,power=-2)
        h2=hp.ud_grade(h2,nside_out=nside,power=-2)
        hit_full=hp.ud_grade(hit_full,nside_out=nside,power=-2)
    
    mmm1=[]
    mmm2=[]
    for m1,m2 in zip(mm1,mm2):
        m1=hp.ud_grade(hp.ma(m1),nside_out=nside)
        m2=hp.ud_grade(hp.ma(m2),nside_out=nside)
        tmask=m1.mask | m2.mask | tmask
        mmm1.append(m1)
        mmm2.append(m2)
    whit = np.sqrt(hit_full*(1./h1+1./h2)) # m1[3] and m2[3] are the hit counts of the two maps to be nulled
    diff=[]
    
    for m1,m2 in zip(mmm1,mmm2):
        d=(m1-m2)/whit
        d.mask=tmask
        if remove_monopole:
            d=hp.remove_monopole(d)
        diff.append(d)
    
    skyfrac=1-float(tmask.sum())/len(tmask)
    cldata=hp.anafast(diff)
    cldata_out=[]
    for cl in cldata:
        cldata_out.append(cl/skyfrac)
        
    if return_map is False:
        return cldata_out
    if return_map is True:
        return cldata_out,diff
コード例 #20
0
ファイル: planck.py プロジェクト: yiheng-wu/cosmology_0
def Cl_from_map_mask(mask_path, data_path):
    mask = hp.read_map(mask_path)
    map = hp.read_map(data_path)

    map_masked = hp.ma(map)
    map_masked.mask = np.logical_not(mask)

    Cl = hp.anafast(map_masked.filled(), lmax=LMAX)
    ell = np.arange(len(Cl))

    return ell, Cl
コード例 #21
0
def apply_mask(parameters,ar,mask_badval,mask_unseen):
    mask = sp.zeros_like(ar)
    if mask_badval:
        mask[ar == parameters.badval] = 1
    if mask_unseen:
        mask[ar == parameters.unseen] = 1

    ar_masked = hp.ma(ar)
    ar_masked.mask = mask
    
    return ar_masked, mask
コード例 #22
0
ファイル: planck.py プロジェクト: yiheng-wu/cosmology_0
def map_masked(mask_path, data_path):
    mask = hp.read_map(mask_path)
    map = hp.read_map(data_path)

    map_masked = hp.ma(map)
    map_masked.mask = np.logical_not(mask)

    plt.figure()
    hp.mollview(map_masked.filled())
    plt.savefig("/home/yhwu/pic/map_masked.png", dpi=1000)
    plt.clf()
コード例 #23
0
def LoadPlanck(filemap,
               filemask,
               nside,
               do_plots=False,
               filt_plank_lmin=0,
               pess=True):
    print("...reading Planck map & mask...")
    planck_mask = hp.read_map(filemask, verbose=False)
    planck_map = hp.read_map(filemap, verbose=False)
    print("...done...")
    print("...degrading Planck map & mask to N_side = %d ..." % nside)
    planck_mask = hp.ud_grade(planck_mask, nside, pess=pess)
    planck_mask[
        planck_mask < 1.] = 0.  # Pessimistc cut, retains only M(n) = 1 pixels
    planck_map = hp.ud_grade(planck_map, nside, pess=pess)
    if do_plots:
        planck_map = hp.ma(planck_map)
        planck_map.mask = np.logical_not(planck_mask)
        hp.mollview(planck_map, title='Planck 2015 No filtering')
        hp.graticule()
        # plt.tight_layout()
        plt.savefig('figs/planck.pdf')  #, bbox_inches='tight')
        plt.close()
    if filt_plank_lmin != 0:
        planck_map_alm = hp.map2alm(planck_map)
        filt = np.ones(hp.Alm.getlmax(len(planck_map_alm)))
        filt[:filt_plank_lmin + 1] = 0.
        planck_map_alm = hp.almxfl(planck_map_alm, filt)
        planck_map = hp.alm2map(planck_map_alm, nside)
    if do_plots:
        planck_map = hp.ma(planck_map)
        planck_map.mask = np.logical_not(planck_mask)
        hp.mollview(planck_map,
                    title=r'Planck 2015 $\ell_{\rm min}^{\rm filt} = %d$' %
                    filt_plank_lmin)
        hp.graticule()
        plt.savefig('figs/planck_filt.pdf')
        plt.close()
    print("...done...")

    return planck_map, planck_mask
コード例 #24
0
ファイル: cl.py プロジェクト: mehdirezaie/LSSutils
def compute_cl(map1, weight1, mask1, lmax=None):
    nside = hp.get_nside(map1)
    t1 = time()
    log = '! ---- computing cl ------ \n'
    if lmax is None:
        lmax = 3 * nside - 1
        log += 'lmax is calculated from the input map(nside = %d) %d! \n' % (
            nside, lmax)
    #
    # will use gauss-leg points to do the integral
    x, w = np.polynomial.legendre.leggauss(lmax + 1)
    log += 'generated the leg gauss points ... \n'
    #
    # map
    mapmasked = hp.ma(map1 * weight1)
    mapmasked.mask = np.logical_not(mask1)
    clmap = hp.anafast(mapmasked.filled(), lmax=lmax)
    ximap = cl2xi(clmap, x)
    log += 'computed the map cl .... \n'
    #
    # weight
    maskmasked = hp.ma(weight1)
    maskmasked.mask = np.logical_not(mask1)
    clmask = hp.anafast(maskmasked.filled(), lmax=lmax)
    ximask = cl2xi(clmask, x)
    log += 'computed the weight cl .... \n'
    #
    # correct for the mask
    xifinal = ximap / ximask
    clfinal = xi2cl(x, w, xifinal, lmax)
    log += 'correct for the "mask" (xi_map / xi_weight) .... \n'
    # update
    output = {}
    output['attr'] = {'nside': nside, 'lmax': lmax}
    output['cl'] = (np.arange(clfinal.size), clfinal)
    output['cl_u'] = clmap
    output['xi_mask'] = ximask
    output['xi'] = (np.rad2deg(np.arccos(x)), xifinal)
    log += 'Finished C_l in %.2f [sec]' % (time() - t1)
    print(log)
    return output
コード例 #25
0
def cmplx_map_2_full_alm(residuals, lmax):
    """
    Get the full set of alm for a complex healpix map. NaN will be treated as
    a mask. Healpy UNSEEN may be buggy for imaginary components.
    """
    l, m = hp.Alm.getlm(lmax)
    # healpix only calculates m >= 0
    pos_lm_idx = pd.MultiIndex.from_arrays((l, m), names=['l', 'm'])
    neg_lm_idx = pd.MultiIndex.from_arrays((l, -m), names=['l', 'm'])

    # mask for healpix masked array
    mask = np.isnan(residuals)
    # FIXME: add healpy UNSEEN pixels to the mask?

    # healpix assumes a real map, so split into real & imaginary parts
    real_map = hp.ma(np.real(residuals))
    real_map.mask = mask
    real_alm = hp.map2alm(real_map, lmax=lmax)

    imag_map = hp.ma(np.imag(residuals))
    imag_map.mask = mask
    imag_alm = hp.map2alm(imag_map, lmax=lmax)

    alm_real_pos = pd.Series(data=real_alm, index=pos_lm_idx)
    # al,-m = (-1)^m(al,m)*
    sign = (-1)**m
    alm_real_neg = pd.Series(data=np.conj(real_alm) * sign, index=neg_lm_idx)
    alm_real_neg.drop([0], level='m', inplace=True)
    alm_real = pd.concat([alm_real_pos, alm_real_neg])
    alm_real.sort_index(level='l', inplace=True)

    alm_imag_pos = pd.Series(data=imag_alm, index=pos_lm_idx)
    alm_imag_neg = pd.Series(data=np.conj(imag_alm) * sign, index=neg_lm_idx)
    alm_imag_neg.drop([0], level='m', inplace=True)
    alm_imag = pd.concat([alm_imag_pos, alm_imag_neg])
    alm_imag.sort_index(level='l', inplace=True)

    # spherical harmonics are a complete basis: can just add real & imaginary parts
    alm = alm_real + 1j * alm_imag

    return alm
コード例 #26
0
def alm2map_masked(mask_path,data_path):
	mask=hp.read_map(mask_path)
	alm=hp.read_alm(data_path)
	map=hp.sphtfunc.alm2map(alm,nside)

	map_masked=hp.ma(map)
	map_masked.mask=np.logical_not(mask)

	plt.figure()
	hp.mollview(map_masked)
	plt.savefig("/home/yhwu/pic/alm2map_masked.png",dpi=1000)
	plt.clf()
コード例 #27
0
def surv_diff(surveydict,ss1=1,ss2=2,nside=128,freq=70,mask_ps=True,fwhm=10.0):
    """
    function to make differences among 5 surveys from andreas interactive destriper/binner. uses common masks smooth to 10 degrees
    fwhm is in degrees, if zero or negative don't smooth at all, default 10 degrees
    """
    m1=hp.ma(np.array(surveydict[ss1]))
    m2=hp.ma(np.array(surveydict[ss2])) 
    totalmask=m1.mask|m2.mask
    if mask_ps==True:
        psmask = np.logical_not(np.floor(hp.ud_grade(hp.read_map(glob('/project/projectdirs/planck/data/mission/DPC_maps/dx8/lfi/DX8_MASKs/' + 'mask_ps_%GHz_*.fits' % freq)[0]), nside,order_out='NEST')))
        totalmask=m1.mask | m2.mask | psmask
    dif=(m1-m2)/2.
    dif.mask=totalmask
    dif.mask |= np.isnan(dif)
    dif=hp.ud_grade(dif,nside,order_in='NEST',order_out='RING')
    if fwhm>0:
        difsm=hp.ma(hp.smoothing(dif.filled(),fwhm*np.pi/180.))
        difsm.mask=dif.mask
    if fwhm<=0:
        difsm=dif
    return difsm
コード例 #28
0
def local_mean_map(map1, mask1, deg):
    """ return the local mean map for map1 with mask mask1
    """
    mp1=hp.ma(map1)
    mp1.mask=np.logical_not(mask1)
    NSIDE1=hp.npix2nside(len(map1))
    NSIDE2=nside(deg)
    NPIX2=hp.nside2npix(NSIDE2)
    mp2=np.array([0.]*NPIX2)
    for pix2 in range(0, NPIX2):
        disc1=hp.query_disc(nside=NSIDE1, vec=hp.pix2vec(NSIDE2, pix2), radius=deg2rad(deg))
        mp2[pix2]=np.mean(mp1[disc1])
    return mp2
コード例 #29
0
 def test_smoothing_masked(self):
     smoothed = hp.smoothing(self.map1, fwhm=np.radians(10), lmax=self.lmax)
     smoothed_f90 = hp.ma(
         hp.read_map(
             os.path.join(
                 self.path, 'data',
                 'wmap_band_iqumap_r9_7yr_W_v4_udgraded32_masked_smoothed10deg_fortran.fits'
             ), (0, 1, 2)))
     # fortran does not restore the mask
     smoothed_f90.mask = smoothed.mask
     np.testing.assert_array_almost_equal(smoothed.filled(),
                                          smoothed_f90.filled(),
                                          decimal=6)
コード例 #30
0
ファイル: addStars.py プロジェクト: slosar/fastcat
 def __init__(self, zmean=default_zmean, zsigma=default_zsigma, star_file=default_star_file, options=None):
     if options is not None:
         self.zmean=options.star_zmean
         self.zsigma=options.star_zsigma
         self.star_file=options.star_file
     else:
         self.zmean=zmean
         self.zsigma=zsigma
         self.star_file=star_file
         
     table=aptable.Table.read(self.star_file)
     self.smap = hp.ma(table['values'].data)
     self.smap.mask = table['mask'].data
コード例 #31
0
def plot_map(nside, pix, v, vmin=None, vmax=None, cmap='jet', title=None, save_path=None,
             xsize=None, dpi=None, show=True, timing=True, nest=False):

    if xsize is None:
        xsize = default_xsize[nside]

    if dpi is None:
        dpi = default_dpi[nside]

    npix = hp.nside2npix(nside)

    v = np.array(v)

    # Density map
    map_values = np.zeros(npix, dtype=v.dtype)
    hp_mask = np.zeros(npix, dtype=bool)
    map_values[pix] = v
    hp_mask[pix] = True
    mplot = hp.ma(map_values)
    mplot.mask = ~hp_mask

    # Galactic plane
    org = 120
    tmpn = 1000
    cs = SkyCoord(l=np.linspace(0, 360, tmpn) * units.deg, b=np.zeros(tmpn) * units.deg, frame="galactic")
    ras, decs = cs.icrs.ra.degree, cs.icrs.dec.degree
    ras = np.remainder(ras + 360 - org, 360)  # shift ra values
    ras[ras > 180] -= 360  # scale conversion to [-180, 180]
    ii = ras.argsort()
    ras, decs = ras[ii], decs[ii]

    if timing:
        time_start = time.time()

    projview(mplot, min=vmin, max=vmax,
             rot=(120, 0, 0), cmap=cmap, xsize=xsize,
             graticule=True, graticule_labels=True, projection_type="mollweide", nest=nest,
             title=title,
             xlabel='RA (deg)', ylabel='Dec (deg)',
             custom_xtick_labels=[r'$240\degree$', r'$180\degree$', r'$120\degree$', r'$60\degree$', r'$0\degree$'],
             fontsize=fontsize_dict)
    newprojplot(theta=np.radians(90-decs), phi=np.radians(ras), color='k', lw=1)
    if save_path is not None:
        plt.savefig(save_path, bbox_inches="tight", dpi=dpi)
    if show:
        plt.show()
    else:
        plt.close()

    if timing:
        print('Done!', time.strftime("%H:%M:%S", time.gmtime(time.time() - time_start)))
コード例 #32
0
    def updateMask(self, idx_mask):
        """Set the mask used to analyze the data

        Parameters
        ----------
        idx_mask : `np.ndarray`
            Masks
        """
        self.idx_mask = idx_mask
        self.f_sky = 1. - len(idx_mask[0]) / float(Defaults.NPIXEL)
        countsmap = self.countsmap_fullsky.copy() + 0. # +0. to convert to float array
        for i in range(Defaults.NEbin):
            countsmap[i][idx_mask] = hp.UNSEEN
        self.countsmap = hp.ma(countsmap)
コード例 #33
0
ファイル: test_cut.py プロジェクト: railroad2/lk_cut
def test_single(cl, mask, rseed, lmin=0, lmax=100, nside=256):
    print_message('Single test with my code')
    print_message('Mask coverage = %f' % (np.average(mask)))
    #mask = np.logical_not(hp.ud_grade(mask, nside_out = nside))
    mask = hp.ud_grade(mask, nside_out=nside)
    print_message('Mask coverage for nside %d = %f' %
                  (nside, np.average(mask)))

    np.random.seed(rseed)
    st = time.time()
    m = hp.synfast(cl, nside=nside, new=True)
    mm = hp.ma(m)
    mm.mask = np.logical_not(mask)
    hp.mollview(mm.filled()[0])
    print_debug('Time for synfast:', time.time() - st)

    st = time.time()
    cl_ana = hp.anafast(m)
    print_debug('Time for get anafast:', time.time() - st)

    st = time.time()
    Wl = hp.anafast(mask)
    Mll = coupling_matrix(Wl, lmin=lmin, lmax=lmax)
    Mlli = np.linalg.pinv(Mll)
    print_debug('Time for getting Mll:', time.time() - st)

    st = time.time()
    biased = hp.anafast(mm.filled())
    print_debug('Time for getting biased spectrum:', time.time() - st)
    st = time.time()
    Cl_tmp = np.concatenate(biased[:4, lmin:lmax], 0)
    unbiased = np.dot(Mlli, Cl_tmp).reshape(4, lmax - lmin)
    print_debug('Time for get unbiased spectra:', time.time() - st)

    st = time.time()
    cl_ana = hp.anafast(m)
    print_debug('Time for get anafast:', time.time() - st)

    plt.figure()
    plt.loglog(np.abs(cl2dl(cl[:3].T)))
    plt.loglog(np.abs(cl2dl(cl_ana[:3].T)), '*-', lw=0.5)
    plt.loglog(np.abs(cl2dl(biased[:3].T)), 'x-', lw=0.5)
    plt.loglog(np.abs(cl2dl(unbiased[:3])).T, '+-', lw=0.5)
    plt.ylim(1e-6, 1e4)
    plt.xlabel = 'multipole moment ($l$)'
    plt.ylabel = '$D_l (K^2)'
    plt.title("Coupling matrix")

    return biased, unbiased
コード例 #34
0
def plot_HPcart(ra, dec, nside, title, norm=None, border=1.):
    plt.clf()
    # from matplotlib import rc
    # rc('font',**{'family':'sans-serif','sans-serif':['DejaVu'], 'size': 8})
    # rc('font',**{'size': 8})
    # rc('text', usetex=True)
    ramin = np.min(ra)
    ramax = np.max(ra)
    decmin = np.min(dec)
    decmax = np.max(dec)
    aspect = (decmax - decmin) / ((ramax - ramin) * np.cos (np.deg2rad((decmin + decmax) / 2.)))

    ipix = hp.ang2pix(nside, (90-dec)/180*np.pi, ra/180*np.pi, nest=True)
    HPX = np.bincount(ipix, minlength=hp.nside2npix(nside))
    pixarea = hp.pixelfunc.nside2pixarea(nside, degrees=True)
    ext = [ramax + border, ramin - border, decmin - border, decmax + border]
    HPX = HPX / (3600. * pixarea)
    mask = np.zeros(hp.nside2npix(nside), dtype=np.bool)
    mask[HPX == 0.] = 1
    m = hp.ma(HPX)
    m.mask = mask
    d_op = hp.visufunc.cartview(map=m, lonra=[ramin - border, ramax + border],
                                latra=[decmin - border, decmax + border], flip='astro', format='%.3g', cmap=cmap,
                                nest=True, norm=norm, return_projected_map=True)
    fig = plt.figure(figsize = [6.4, 6.4 * aspect])
    ax1 = fig.add_subplot(111)
    ax1.set_facecolor('dimgray')
    
    if norm==None:
        plt.imshow(d_op, extent=ext, aspect='auto', origin='lower', interpolation=None, vmin=np.min(m),
                   vmax=np.max(m), cmap=cmap)
    else:
        plt.imshow(d_op, extent=ext, aspect='auto', origin='lower', interpolation=None, vmin=np.log10(np.min(m)),
                   vmax=np.log10(np.max(m)), cmap=cmap)
    # plt.title('$\mathrm{%s}$' % title.replace('_', '\ '))
    plt.xlabel('ra')
    plt.ylabel('dec')
    plt.ylim([decmin - border, decmax + border])
    plt.xlim([ramax + border, ramin - border])
    plt.grid(c='grey', lw=0.25, alpha=0.5)
    cbaxes = fig.add_axes([0.90, 0.11, 0.015, 0.77])
    if norm==None:
        cb = plt.colorbar(cax=cbaxes, label=r'$\mathrm{density\ (objects\ per\ sq.\ arcmin)}$')
        plt.savefig('linear.png', bbox_inches='tight')
    else:
        cb = plt.colorbar(cax=cbaxes, label=r'$\mathrm{log(density\ [objects\ per\ sq.\ arcmin])}$')
        plt.savefig('%slogscale.png' % title, bbox_inches='tight')
    plt.close()
    return
コード例 #35
0
    def get_gain(
        self,
        el,
        az,
        n_pix=4,
        nside_exp_grid=10,
        nside_exp_syn=10,
        lmax=4,
        mmax=4,
        interpolate="default",
    ):
        if self.recalculate:
            self.recalculate = False
            self.values = np.array(self.values)
            self.distances = np.array(self.distances)
            self.i_map, self.pixel_dict = gen_interpolation_map(
                self.points, self.values, self.antenna_num, nside_exp_grid)
            if interpolate == "increasing_neighborhood":
                self.interp_gain = lambda el, az: hp_interpolator(
                    self.i_map, el, az, n_pix)

                # self.interp_gain(el, az)
                np.power(2, nside_exp_syn)
                self.norm_map = self.i_map
            else:
                alms = hp.sphtfunc.map2alm(hp.ma(self.i_map),
                                           lmax=lmax,
                                           mmax=mmax,
                                           iter=10,
                                           pol=False)
                print(hp.Alm.getlmax(len(alms), mmax=mmax))
                norm_map = hp.sphtfunc.alm2map(
                    alms,
                    np.power(2, nside_exp_syn),
                    lmax=lmax,
                    mmax=mmax,
                    pixwin=False,
                    pol=False,
                )
                norm_map_min = norm_map.min()
                norm_map = norm_map - norm_map_min
                norm_map_max = norm_map.max()
                norm_map = norm_map / norm_map_max
                self.norm_map = norm_map
                # self.interp_gain = lambda el, az: (hp_spheric_harmonics(alms, el, az, mmax=mmax)-norm_map_min)/norm_map_max
                self.interp_gain = lambda el, az: self.norm_map[
                    hp.pixelfunc.ang2pix(np.power(2, nside_exp_syn), np.pi / 2
                                         - el.to_rad(), az.to_rad())]
        return self.interp_gain(el, az)
コード例 #36
0
def get_lfi_dx11_mask(nside,masktype='int',ps=True):
    """
    now using masks suggested by AZa on 1/27/2015, common mask, should already have PS
    apo=true is apodized, masktype='pol' is polarized mask, masktype='int' intensity mask
    """
    maskdir='/global/homes/p/peterm/masks/'
    f=maskdir+'dx11_v2_common_%s_mask_010a_1024.fits' %masktype
    tmask=hp.ma(hp.read_map(f)) 
    tmask=degrade_mask(tmask,nside_out=nside)
    tmask=np.logical_not(tmask)
    if ps:
        fpsmask30='/global/project/projectdirs/planck/data/mission/DPC_maps/dx9_delta/lfi/MASKs/mask_ps_30GHz_beam33amin_nside2048.00_DX9_nonblind_holesize3.fits'
        psmask30 = np.logical_not(np.floor(hp.ud_grade(hp.read_map(fpsmask30), nside)))
        tmask=psmask30.astype(np.bool)|tmask.astype(np.bool)
    return tmask
コード例 #37
0
def local_mean_map(map1, mask1, deg):
    """ return the local mean map for map1 with mask mask1
    """
    mp1 = hp.ma(map1)
    mp1.mask = np.logical_not(mask1)
    NSIDE1 = hp.npix2nside(len(map1))
    NSIDE2 = nside(deg)
    NPIX2 = hp.nside2npix(NSIDE2)
    mp2 = np.array([0.] * NPIX2)
    for pix2 in range(0, NPIX2):
        disc1 = hp.query_disc(nside=NSIDE1,
                              vec=hp.pix2vec(NSIDE2, pix2),
                              radius=deg2rad(deg))
        mp2[pix2] = np.mean(mp1[disc1])
    return mp2
コード例 #38
0
def read_and_diff_2_files_fast(f1,f2,f3,nside=256,tmask=None,return_map=False):
    #assume tmask is already degraded- this version subtracts (f2+f3)/2 from f1
    
    mm1=hp.read_map(f1,[0,1,2],verbose=False)
    mm2=hp.read_map(f2,[0,1,2],verbose=False)
    mm3=hp.read_map(f3,[0,1,2],verbose=False)

    mmm1=[]
    mmm2=[]
    mmm3=[]
    for m1,m2,m3 in zip(mm1,mm2,mm3):
        m1=hp.ud_grade(hp.ma(m1),nside_out=nside)
        m2=hp.ud_grade(hp.ma(m2),nside_out=nside)
        m3=hp.ud_grade(hp.ma(m3),nside_out=nside)
        tmask=m1.mask | m2.mask | m3.mask | tmask
        mmm1.append(m1)
        mmm2.append(m2)
        mmm3.append(m3)
    
    diff=[]
    for m1,m2,m3 in zip(mmm1,mmm2,mmm3):
        d=m1-(m2+m3)/2
        d.mask=tmask
        diff.append(d)
    
    skyfrac=1-float(tmask.sum())/len(tmask)
        
    cldata=hp.anafast(diff)
    cldata_out=[]
    for cl in cldata:
        cldata_out.append(cl/skyfrac)
        
    if return_map is False:
        return cldata_out
    if return_map is True:
        return cldata_out,diff
コード例 #39
0
    def mask_map(self, binary_mask):
        """
        MASKS THE GIVEN SKY MAP WITH THE BINARY MASK.
        FOR EXAMPLE, PASSING A SINGLE BINARY MASK FOR A SET OF I,Q,U SKY MAPS, WILL APPLY THE SAME MASK TO ALL THE THREE.
        IN GENERAL, THE MASKS ARE ASSIGNED IN A CYCLIC MANNER. FOR EXAMPLE IF SKY_MAP HAS 4 MAPS AND BINARY_MASK HAS 2 MASKS, THEN:
        binary_mask[0] -> sky_map[0]
        binary_mask[1] -> sky_map[1]
        binary_mask[0] -> sky_map[2]
        binary_mask[1] -> sky_map[3]
        """
        # CHECKING THAT THE SKY MAP AND MASK HAVE THE SAME RESOLUTION
        assert self.nside == binary_mask.nside, "nside of sky map and mask does not match.\nnside of sky map : {}\nnside of mask : {}".format(self.nside, binary_mask.nside)

        self.sky_map = hp.ma(self.sky_map) 
        self.sky_map.mask = np.logical_not(binary_mask.sky_map)
コード例 #40
0
ファイル: mkforecl.py プロジェクト: mahmoud-lsw/GRATools
def mkForeCl(**kwargs):
    """                                      
    """
    get_var_from_file(kwargs['config'])

    logger.info('Starting Cl analysis...')
    in_label = data.IN_LABEL
    out_label = data.OUT_LABEL
    binning_label = data.BINNING_LABEL
    from GRATools.utils.gFTools import get_cl_param
    cl_param_file = os.path.join(
        GRATOOLS_OUT, '%s_%s_parameters.txt' % (in_label, binning_label))
    _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file)
    cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \
                                   %(out_label, binning_label)), 'w')
    for i, (emin, emax) in enumerate(zip(_emin, _emax)):
        mask_file = data.MASK_FILE
        if type(mask_file) == list:
            mask_file = mask_file[i]
        mask = hp.read_map(mask_file)
        logger.info('Considering bin %.2f - %.2f ...' % (emin, emax))
        cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, _emean[i]))
        l_max = 1000
        _l = np.arange(l_max)
        flux_map_name = in_label + '_fore_%i-%i.fits' % (emin, emax)
        flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FORE, flux_map_name))
        flux_map_masked = hp.ma(flux_map)
        flux_map_masked.mask = np.logical_not(mask)
        fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\
                       float(len(flux_map)))
        if kwargs['show'] == True:
            hp.mollview(flux_map_masked.filled(),
                        title='f$_{sky}$ = %.3f' % fsky,
                        min=1e-7,
                        max=1e-4,
                        norm='log')
            plt.show()
        print 'fsky = ', fsky
        nside = hp.npix2nside(len(flux_map))
        wpix = hp.sphtfunc.pixwin(nside)[:l_max]
        _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \
                                      iter=5)
        _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4)
        cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \
                         replace(']','').replace(', ', ' '))
    cl_txt.close()
    logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \
                                               %(out_label, binning_label))))
コード例 #41
0
def local_variance_map(map1, mask1, deg):
    """ return the local variance map for map1 with mask mask1, given the error tolerance tol for mask2
    """
    mp1=hp.ma(map1)
    mp1.mask=np.logical_not(mask1)

    NSIDE1=hp.npix2nside(len(map1))
    NSIDE2=nside(deg)
    NPIX2=hp.nside2npix(NSIDE2)
    #mask2=np.round(hp.ud_grade(mask1, nside_out=NSIDE2)+tol)
    mp2=np.array([0.]*NPIX2)
    for pix2 in range(0, NPIX2):
        disc1=hp.query_disc(nside=NSIDE1, vec=hp.pix2vec(NSIDE2, pix2), radius=deg2rad(deg))
        mp2[pix2]=np.var(mp1[disc1])
    #varmp2.mask=np.logical_not(mask2)
    return mp2
コード例 #42
0
ファイル: addStars.py プロジェクト: slosar/fastcat
    def __init__(self,
                 zmean=default_zmean,
                 zsigma=default_zsigma,
                 star_file=default_star_file,
                 options=None):
        if options is not None:
            self.zmean = options.star_zmean
            self.zsigma = options.star_zsigma
            self.star_file = options.star_file
        else:
            self.zmean = zmean
            self.zsigma = zsigma
            self.star_file = star_file

        table = aptable.Table.read(self.star_file)
        self.smap = hp.ma(table['values'].data)
        self.smap.mask = table['mask'].data
コード例 #43
0
ファイル: test_sphtfunc.py プロジェクト: arahlin/healpy
 def test_smoothing_masked(self):
     smoothed = hp.smoothing(self.map1, fwhm=np.radians(10), lmax=self.lmax)
     smoothed_f90 = hp.ma(
         hp.read_map(
             os.path.join(
                 self.path,
                 "data",
                 "wmap_band_iqumap_r9_7yr_W_v4_udgraded32_masked_smoothed10deg_fortran.fits",
             ),
             (0, 1, 2),
         )
     )
     # fortran does not restore the mask
     smoothed_f90.mask = smoothed.mask
     np.testing.assert_array_almost_equal(
         smoothed.filled(), smoothed_f90.filled(), decimal=6
     )
コード例 #44
0
ファイル: mkforecl.py プロジェクト: nmik/GRATools
def mkForeCl(**kwargs):
    """                                      
    """
    get_var_from_file(kwargs['config'])

    logger.info('Starting Cl analysis...')
    in_label = data.IN_LABEL
    out_label = data.OUT_LABEL
    binning_label = data.BINNING_LABEL
    from GRATools.utils.gFTools import get_cl_param
    cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt'
                                 %(in_label, binning_label))
    _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file)
    cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \
                                   %(out_label, binning_label)), 'w')
    for i, (emin, emax) in enumerate(zip(_emin, _emax)):
        mask_file = data.MASK_FILE
        if type(mask_file) == list:
            mask_file = mask_file[i]
        mask = hp.read_map(mask_file)
        logger.info('Considering bin %.2f - %.2f ...'%(emin, emax))
        cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i]))
        l_max= 1000
        _l = np.arange(l_max)
        flux_map_name = in_label+'_fore_%i-%i.fits'%(emin, emax)
        flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FORE, flux_map_name))
        flux_map_masked = hp.ma(flux_map)
        flux_map_masked.mask = np.logical_not(mask)
        fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\
                       float(len(flux_map)))
        if kwargs['show'] == True:
            hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f'%fsky,
                        min=1e-7, max=1e-4, norm='log')
            plt.show()
        print 'fsky = ', fsky
        nside = hp.npix2nside(len(flux_map))
        wpix = hp.sphtfunc.pixwin(nside)[:l_max]
        _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \
                                      iter=5)
        _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4)
        cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \
                         replace(']','').replace(', ', ' '))
    cl_txt.close()
    logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \
                                               %(out_label, binning_label))))
コード例 #45
0
def local_variance_map(map1, mask1, deg):
    """ return the local variance map for map1 with mask mask1, given the error tolerance tol for mask2
    """
    mp1 = hp.ma(map1)
    mp1.mask = np.logical_not(mask1)

    NSIDE1 = hp.npix2nside(len(map1))
    NSIDE2 = nside(deg)
    NPIX2 = hp.nside2npix(NSIDE2)
    #mask2=np.round(hp.ud_grade(mask1, nside_out=NSIDE2)+tol)
    mp2 = np.array([0.] * NPIX2)
    for pix2 in range(0, NPIX2):
        disc1 = hp.query_disc(nside=NSIDE1,
                              vec=hp.pix2vec(NSIDE2, pix2),
                              radius=deg2rad(deg))
        mp2[pix2] = np.var(mp1[disc1])
    #varmp2.mask=np.logical_not(mask2)
    return mp2
コード例 #46
0
def local_power_spectrum(map1, mask1, deg, LMAX=256):
    """ return the local power spectrum for each disk given by the radius [deg]
    """
    mp1=hp.ma(map1)
    mp1.mask=np.logical_not(mask1)
    NSIDE1=hp.npix2nside(len(map1))
    NSIDE2=nside(deg)
    NPIX2=hp.nside2npix(NSIDE2)
    NPIX1=hp.nside2npix(NSIDE1)
    mp2=[0]*NPIX2
    for pix2 in range(0, NPIX2):
        newmask=np.array([0.]*NPIX1)
        disc1=hp.query_disc(nside=NSIDE1, vec=hp.pix2vec(NSIDE2, pix2), radius=deg2rad(deg))
        newmask[disc1]=1.
        newmask=newmask*mask1
        mp1.mask=np.logical_not(mask1)
        mp2[pix2]=hp.anafast(mp1, lmax=LMAX)
    return mp2
コード例 #47
0
def subtract_mono_di(map_in, mask_in, nside):

    map_masked = hp.ma(map_in)
    map_masked.mask = (mask_in < 1)
    mono, dipole = hp.pixelfunc.fit_dipole(map_masked)
    print(mono, dipole)
    m = map_in.copy()
    npix = hp.nside2npix(nside)
    bunchsize = npix // 24
    bad = hp.UNSEEN
    for ibunch in range(npix // bunchsize):
        ipix = np.arange(ibunch * bunchsize, (ibunch + 1) * bunchsize)
        ipix = ipix[(np.isfinite(m.flat[ipix]))]
        x, y, z = hp.pix2vec(nside, ipix, False)
        m.flat[ipix] -= dipole[0] * x
        m.flat[ipix] -= dipole[1] * y
        m.flat[ipix] -= dipole[2] * z
        m.flat[ipix] -= mono
    return m
コード例 #48
0
ファイル: sphere.py プロジェクト: cosmo-ethz/hide
def rotate_map(Map, rotator, mask = None):
    """
    Map is map in system A
    rotator is rotator from system B to A
    mask is a mask in system B
    returns new map in system B
    """
    npix = Map.shape[0]
    nside = hp.npix2nside(npix)
    thetas, phis = hp.pix2ang(nside, np.arange(npix))
    rts, rps = rotator.I(thetas, phis)
    vals = hp.pixelfunc.get_interp_val(Map, rts,  rps)
    nm = vals
    if mask == None:
        return nm
    else:
        nm = hp.ma(nm)
        nm.mask = mask
        return nm
コード例 #49
0
def rotate_map(Map, rotator, mask=None):
    """
    Map is map in system A
    rotator is rotator from system B to A
    mask is a mask in system B
    returns new map in system B
    """
    npix = Map.shape[0]
    nside = hp.npix2nside(npix)
    thetas, phis = hp.pix2ang(nside, np.arange(npix))
    rts, rps = rotator.I(thetas, phis)
    vals = hp.pixelfunc.get_interp_val(Map, rts, rps)
    nm = vals
    if mask == None:
        return nm
    else:
        nm = hp.ma(nm)
        nm.mask = mask
        return nm
コード例 #50
0
def changeSides(map, nSides):
    """
    Take a HEALPix map upsample or downsample the map to make it have
    'nSides' sides and conserve flux.
    """

    nSidesOld = hp.pixelfunc.npix2nside(map.size)

    if getattr(map, 'mask', None) is not None:
        map2 = hp.pixelfunc.ud_grade(map.data, nSides)

        mask2 = hp.pixelfunc.ud_grade(map.mask.astype(np.float64), nSides)

        map2 = hp.ma(map2)
        map2.mask = np.where(mask2 >= 0.005, 1, 0).astype(np.bool)
    else:
        map2 = hp.pixelfunc.ud_grade(map, nSides)

    return map2
コード例 #51
0
        def generate(self, freq):
            """ Generate the observed sky for the observer, based on the GSM.
	
			Parameters
			----------
			freq: float
				Frequency of map to generate, in units of MHz (default).
	
			Returns
			-------
			observed_sky: np.array
				Numpy array representing the healpix image, centered on zenith,
				with below the horizon masked.
			"""
            import numpy as np
            import healpy as hp
            self.gsm.generate(freq)
            sky = self.gsm.generated_map_data

            # Get RA and DEC of zenith
            ra_rad, dec_rad = self.radec_of(0, np.pi / 2)
            ra_deg = ra_rad / np.pi * 180
            dec_deg = dec_rad / np.pi * 180

            # Apply rotation
            hrot = hp.Rotator(rot=[ra_deg, dec_deg],
                              coord=['G', 'C'],
                              inv=True)
            g0, g1 = hrot(self._theta, self._phi)
            n_side = hp.npix2nside(self.gsm.generated_map_data.shape[0])
            pix0 = hp.ang2pix(n_side, g0, g1)
            sky_rotated = sky[pix0]

            # Generate a mask for below horizon
            mask1 = self._phi + np.pi / 2 > 2 * np.pi
            mask2 = self._phi < np.pi / 2
            mask = np.invert(np.logical_or(mask1, mask2))

            self.observed_sky = hp.ma(sky_rotated)
            self.observed_sky.mask = mask

            return self.observed_sky
コード例 #52
0
def klm_2_product(klmname, width, maskname, nsides, lmin, subtract_mf=False, writename=None):

    # read in planck alm convergence data
    planck_lensing_alm = hp.read_alm(klmname)
    lmax = hp.Alm.getlmax(len(planck_lensing_alm))

    if subtract_mf:
        mf_alm = hp.read_alm('maps/mf_klm.fits')
        planck_lensing_alm = planck_lensing_alm - mf_alm

    # if you want to smooth with a gaussian
    if width > 0:
        # transform a gaussian of FWHM=width in real space to harmonic space
        k_space_gauss_beam = hp.gauss_beam(fwhm=width.to('radian').value, lmax=lmax)
        # if truncating small l modes
        if lmin > 0:
            # zero out small l modes in k-space filter
            k_space_gauss_beam[:lmin] = 0

        # smooth in harmonic space
        filtered_alm = hp.smoothalm(planck_lensing_alm, beam_window=k_space_gauss_beam)
    else:
        # if not smoothing with gaussian, just remove small l modes
        filtered_alm = zero_modes(planck_lensing_alm, lmin)

    planck_lensing_map = hp.sphtfunc.alm2map(filtered_alm, nsides, lmax=lmax)

    # mask map
    importmask = hp.read_map(maskname)
    if nsides < 2048:
        mask_lowres_proper = hp.ud_grade(importmask.astype(float), nside_out=1024).astype(float)
        finalmask = np.where(mask_lowres_proper == 1., True, False).astype(bool)
    else:
        finalmask = importmask.astype(np.bool)
    # set mask, invert
    smoothed_masked_map = hp.ma(planck_lensing_map)
    smoothed_masked_map.mask = np.logical_not(finalmask)

    if writename:
        hp.write_map('%s.fits' % writename, smoothed_masked_map.filled(), overwrite=True, dtype=np.single)

    return smoothed_masked_map.filled()
コード例 #53
0
ファイル: test_sphtfunc.py プロジェクト: dhanson/healpy
 def setUp(self):
     try:
         self.map = hp.ma(
             hp.read_map(
                 os.path.join('data', 'wmap_band_imap_r9_7yr_W_v4.fits')))
         self.mask = hp.read_map(
             os.path.join(
                 'data',
                 'wmap_temperature_analysis_mask_r9_7yr_v4.fits')).astype(
                     np.bool)
     except exceptions.IOError:
         warnings.warn(
             """Missing Wmap test maps from the data folder, please download them from Lambda and copy them in the test/data folder:
         http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_imap_r9_7yr_W_v4.fits
         http://lambda.gsfc.nasa.gov/data/map/dr4/ancillary/masks/wmap_temperature_analysis_mask_r9_7yr_v4.fits
         on Mac or Linux you can run the bash script get_wmap_maps.sh from the same folder
         """)
         raise
     self.map.mask = np.logical_not(self.mask)
     self.cla = hp.read_cl(os.path.join('data', 'cl_wmap_fortran.fits'))
コード例 #54
0
ファイル: maptools.py プロジェクト: xzackli/nawrapper
def sub_mono_di(map_in, mask_in, nside, sub_dipole=True, verbose=False):
    """Subtract monopole and dipole from a healpix map."""
    map_masked = hp.ma(map_in)
    map_masked.mask = mask_in < 1
    mono, dipole = hp.pixelfunc.fit_dipole(map_masked)
    if verbose:
        print("mono:", mono, ", dipole:", dipole)
    m = map_in.copy()
    npix = hp.nside2npix(nside)
    bunchsize = npix // 24
    for ibunch in range(npix // bunchsize):  # adapted from healpy
        ipix = np.arange(ibunch * bunchsize, (ibunch + 1) * bunchsize)
        ipix = ipix[(np.isfinite(m.flat[ipix]))]
        x, y, z = hp.pix2vec(nside, ipix, False)
        if sub_dipole:
            m.flat[ipix] -= dipole[0] * x
            m.flat[ipix] -= dipole[1] * y
            m.flat[ipix] -= dipole[2] * z
        m.flat[ipix] -= mono
    return m
コード例 #55
0
def local_power_spectrum(map1, mask1, deg, LMAX=256):
    """ return the local power spectrum for each disk given by the radius [deg]
    """
    mp1 = hp.ma(map1)
    mp1.mask = np.logical_not(mask1)
    NSIDE1 = hp.npix2nside(len(map1))
    NSIDE2 = nside(deg)
    NPIX2 = hp.nside2npix(NSIDE2)
    NPIX1 = hp.nside2npix(NSIDE1)
    mp2 = [0] * NPIX2
    for pix2 in range(0, NPIX2):
        newmask = np.array([0.] * NPIX1)
        disc1 = hp.query_disc(nside=NSIDE1,
                              vec=hp.pix2vec(NSIDE2, pix2),
                              radius=deg2rad(deg))
        newmask[disc1] = 1.
        newmask = newmask * mask1
        mp1.mask = np.logical_not(mask1)
        mp2[pix2] = hp.anafast(mp1, lmax=LMAX)
    return mp2
コード例 #56
0
    def from_array(
        cls,
        map_array: np.array,
        opening_angle: float,
        quantity: str,
        dir_in: str,
        map_file: Optional[str] = None,
    ) -> "SkyMap":
        """
        Initialize class by reading the skymap data from np.ndarray.

        Args:
            map_filename:
                File path with which skymap pd.DataFrame can be loaded.
            file_dsc:
                Dictionary pointing to a file via {path, root, extension}.
                Use when multiple skymaps need to be loaded.
        """
        dirs = {"sim": dir_in}
        map_array = hp.ma(map_array)  # mask out bad values (e.g Nan)
        return cls(map_array, opening_angle, quantity, dirs, map_file)
コード例 #57
0
    def test_weighting(self, tag):
        stokes, mask = tag.split('_')
        instrument = 'dict_vary'
        components = 'fixedpowerlaw'
        nside = 32
        sky_tag = '%s__nside_%i__nsidepar_%i__%s__%s__%s' % (
            stokes, nside, 0, components, mask, instrument)
        data, _, _ = _get_sky(sky_tag)
        instrument = _get_instrument(instrument, nside)
        components = _get_component(components)

        cov = self._get_cov(instrument, sky_tag.split('__')[0])
        np.random.seed(0)
        data += np.random.normal(size=cov.size).reshape(cov.shape) * cov**0.5
        res = weighted_comp_sep(components, instrument, data, cov)

        mask = hp.ma(data[0]).mask
        chi2s = (res.chi[:, ~mask]**2).sum(axis=0).flatten()
        dof = data.shape[0] - 1
        _, p_value = kstest(chi2s.flatten(), 'chi2', (dof, ))
        assert p_value > 0.10
コード例 #58
0
def Counts2Delta(counts, mask=None):
    """
    Converts a number counts Healpix map into a density contrast map.

    Note
    ----
    Use only binary mask.
    """
    counts = np.asarray(counts)

    if mask is not None:
        mask = np.asarray(mask)
        counts = hp.ma(counts)
        counts.mask = np.logical_not(mask)

    mean = np.mean(counts)
    delta = (counts - mean) / mean
    delta = delta.filled(
        counts.fill_value)  # To avoid pixels with fill_value = 1e+20

    return delta
コード例 #59
0
def remove_md(m, remove_dipole, remove_monopole, nside):
    if remove_monopole:
        dip_mask_name = remove_monopole
    if remove_dipole:
        dip_mask_name = remove_dipole
    # Mask map for dipole estimation
    if dip_mask_name == 'auto':
        mono, dip = hp.fit_dipole(m, gal_cut=30)
    else:
        m_masked = hp.ma(m)
        m_masked.mask = np.logical_not(
            hp.read_map(
                dip_mask_name,
                verbose=False,
                dtype=None,
            ))

        # Fit dipole to masked map
        mono, dip = hp.fit_dipole(m_masked)

    # Subtract dipole map from data
    if remove_dipole:
        click.echo(click.style("Removing dipole:", fg="yellow"))
        click.echo(click.style("Dipole vector:", fg="green") + f" {dip}")
        click.echo(
            click.style("Dipole amplitude:", fg="green") +
            f" {np.sqrt(np.sum(dip ** 2))}")

        # Create dipole template
        nside = int(nside)
        ray = range(hp.nside2npix(nside))
        vecs = hp.pix2vec(nside, ray)
        dipole = np.dot(dip, vecs)

        m = m - dipole
    if remove_monopole:
        click.echo(click.style("Removing monopole:", fg="yellow"))
        click.echo(click.style("Mono:", fg="green") + f" {mono}")
        m = m - mono
    return m
コード例 #60
0
def mask_map(sky_map, binary_mask):
    """
    Masks the given sky map with the binary mask.
    At least with healpy version >= 1.11.0, the number of sky maps does not matter.
    For example, passing a single binary mask for a set of I,Q,U sky maps, will apply the same mask for all the three.
    Passing a set of masks in the form of an array will assign the masks to the corresponding sky maps.
    In general, the masks are assigned in a cyclic manner. For example if sky_map has 4 maps and binary_mask has 2 masks, then:
    binary_mask[0] -> sky_map[0]
    binary_mask[1] -> sky_map[1]
    binary_mask[0] -> sky_map[2]
    binary_mask[1] -> sky_map[3]
    """
    # Checking that the sky map and mask have the same resolution
    assert hp.get_nside(sky_map) == hp.get_nside(
        binary_mask
    ), "nside of sky map and mask does not match.\nnside of sky map : {}\nnside of mask : {}".format(
        hp.get_nside(sky_map), hp.get_nside(binary_mask))

    sky_map_masked = hp.ma(sky_map)
    sky_map_masked.mask = np.logical_not(binary_mask)

    return sky_map_masked