コード例 #1
0
ファイル: test_gsm.py プロジェクト: cosmo-ethz/hide
    def test_load_signal(self):
        params = Struct(beam_nside = GSM_NSIDE)
        ctx = Struct(params = params)
        
        ctx.frequency = 980.0
        astro_signal = gsm.load_signal(ctx)
        assert astro_signal is not None
        assert hp.get_nside(astro_signal) == ctx.params.beam_nside

        root_file_path = resource_filename(hide.__name__, gsm.GSM_FILE_PATH)        
        file_path = os.path.join(root_file_path, str(params.beam_nside), "gsm_%s.fits"%(ctx.frequency))
        gsm_map = hp.read_map(file_path)
        assert np.all(gsm_map == astro_signal)
        
        ctx.frequency = 1000.0
        astro_signal = gsm.load_signal(ctx)
        assert astro_signal is not None
        assert hp.get_nside(astro_signal) == ctx.params.beam_nside
        
        file_path = os.path.join(root_file_path, str(params.beam_nside), "gsm_%s.fits"%(ctx.frequency))
        gsm_map = hp.read_map(file_path)
        assert np.all(gsm_map == astro_signal)
        
        ctx.frequency = 1280.0
        astro_signal = gsm.load_signal(ctx)
        assert astro_signal is not None
        assert hp.get_nside(astro_signal) == ctx.params.beam_nside
        
        file_path = os.path.join(root_file_path, str(params.beam_nside), "gsm_%s.fits"%(ctx.frequency))
        gsm_map = hp.read_map(file_path)
        assert np.all(gsm_map == astro_signal)
コード例 #2
0
ファイル: explore.py プロジェクト: veragluscevic/npoint-fgs
def prepare_map(mapname='HFI_SkyMap_143_2048_R2.02_full.fits',
                maskname='HFI_Mask_GalPlane-apo0_2048_R2.00.fits',
                field = (0,1,2),
                fwhm=0.0,
                nside_out=128,
                rewrite_map=False,
                masktype=None):

    newname = mapname[:-5] + '_fwhm_{:.3f}rad_nside_{}_mask_{}.fits'.format(fwhm, nside_out,masktype)
    if not os.path.exists(data_path + newname) or rewrite_map:
        
        print 'reading mask...'
        mask = hp.read_map(data_path + maskname, field=2)
        masknside = hp.get_nside(mask)
        
        if masknside != nside_out:
            print 'matching mask to map resolution...'
            mask = hp.pixelfunc.ud_grade(mask, nside_out=nside_out)
        print 'done'

            
        print 'processing map...'
        Imap, Qmap, Umap = hp.read_map( data_path + mapname,
                                        hdu=1, field=(0,1,2) )
        mapnside = hp.get_nside(Imap)


        if not np.isclose(fwhm, 0.):
            Imap = hp.sphtfunc.smoothing( Imap,fwhm=fwhm )
            Qmap = hp.sphtfunc.smoothing( Qmap,fwhm=fwhm )
            Umap = hp.sphtfunc.smoothing( Umap,fwhm=fwhm )

        if mapnside != nside_out:
            Imap = hp.pixelfunc.ud_grade( Imap,nside_out=nside_out )
            Qmap = hp.pixelfunc.ud_grade( Qmap,nside_out=nside_out )
            Umap = hp.pixelfunc.ud_grade( Umap,nside_out=nside_out )

        Imap *= mask
        Qmap *= mask
        Umap *= mask
        
        hp.fitsfunc.write_map( data_path + newname, [Imap, Qmap, Umap])

        print 'done'

    print 'reading map...'
    maps = hp.read_map( data_path + newname,
                        field=field )
    return maps
コード例 #3
0
ファイル: cltools.py プロジェクト: jegudmunds/code
def map2patch(map,n):
    """
    Incomplete

    Creates small patches of a larger map
    

    """

    nside = hp.get_nside(map)
    pix = np.arange((hp.nside2npix(nside)))
    (theta, phi) = hp.pix2ang(nside,pix)
    dec = (theta-np.pi/2.0)/np.pi*180.0; ra = phi/2.0/np.pi*360.0
    obspix = (map > 0.5)
    ri = np.mod(n,5); di = n/5
    rap = np.percentile(ra[obspix],[0,20,40,60,80,100])
    decp = np.percentile(dec[obspix],50)
    ragood = np.logical_and((rap[ri] < ra),(ra < rap[ri+1]))
    if di < 1:
        decgood = (dec < decp)
    else:
        decgood = (dec > decp)
    gdidx = np.logical_and(np.logical_and(ragood,decgood),obspix)
    outmap = np.copy(map)
    outmap[gdidx] = 1.0
    outmap[np.logical_not(gdidx)] = 0.0
    return outmap
コード例 #4
0
ファイル: Tmap2Vis.py プロジェクト: jizhi/huangqizhi_git
	def BeamMapComplex( self, RAnow, Bmap, freq ) : 
		''' return shape = (1, Nv, 12*nside**2) '''
		freq = jp.Num(freq, float)
		RAnow = jp.Num(RAnow, float)
		Bmap = jp.npfmt(Bmap)
		coordtrans = jp.CoordTrans()
		nside = hp.get_nside(Bmap)
		theta, phi = hp.pix2ang(nside, np.arange(12*nside**2))
		#--------------------------------------------------
		# For Bmap, rotate points in CeleXYZ from RA=0(+x) to RA=RAnow. Rotate points is negetive angle, so az=-RAnow
		az = -RAnow
		if (az != 0) : 
			pixnowB = hp.ang2pix(nside, theta, phi+az*np.pi/180)
			Bmap = Bmap[pixnowB]
			pixnowB = 0 #@
		# Now Bmapnow is still in CeleXYZ
		#--------------------------------------------------
		# For Tmap, don't need to rotate its points
		# Then for Tmap and Bmap(now), convert from CeleXYZ to AntXYZ. Note that here rotate XYZ, NOT points, so az=RAnow+90 from lon=0 to +AntX
		az = 90 + RAnow
		if (az != 0) : hpixXYZ = coordtrans.xyzRotation(self.hpixCeleXYZ.T, az=az).T
		else : hpixXYZ = self.hpixCeleXYZ.copy()
		#--------------------------------------------------
		# phase = 2pi/c * \vec(B) * \vec(s)
		phase = (2*np.pi/300*freq * self.blXYZ[:,None,:] * hpixXYZ[None,:,:]).sum(-1)  # (Nv, 12*nside**2) for 1 freq
		hpixXYZ = 0 #@
		#--------------------------------------------------
		Bmap = Bmap[None,:] * np.exp(1j*phase)
		return Bmap[None,:]  # None for Nfreq=1
コード例 #5
0
ファイル: rotate_tqu.py プロジェクト: mkolopanis/python
def rotate_tqu(map_in,wl,alpha):  #rotates tqu map by phase
	npix=hp.nside2npix(hp.get_nside(map_in))
	tmp_map=np.zeros((3,npix))
	tmp_map[0]=map_in[0]
	tmp_map[1]=map_in[1]*np.cos(2*wl**2*alpha)+map_in[2]*np.sin(2*wl**2*alpha) 
	tmp_map[2]=map_in[2]*np.cos(2*wl**2*alpha)-map_in[1]*np.sin(2*wl**2*alpha)
	return tmp_map
コード例 #6
0
ファイル: test_static_gsm.py プロジェクト: cosmo-ethz/hide
 def test_load_signal_normal(self):
     params = Struct(beam_nside=static_gsm.GSM_NSIDE)
     ctx = Struct(params = params)
     
     astro_signal = static_gsm.load_signal(ctx)
     assert astro_signal is not None
     assert hp.get_nside(astro_signal) == ctx.params.beam_nside
コード例 #7
0
ファイル: fermi.py プロジェクト: drJfunk/gbmgeometry
    def read_healpix_map(self, healpix_map, cmap='viridis'):

        # collect the nside of the map

        nside = hp.get_nside(healpix_map)

        # get the colors of the rays based of their values

        _, colors = array_to_cmap(healpix_map, cmap=cmap, use_log=False)

        # now go thru all the points on the sphere

        for idx, val in enumerate(healpix_map):

            if val > 0:

                # if the probability is greater than 0
                # then we need to get the sky position

                ra, dec = Fermi._pix_to_sky(idx, nside)

                # mark the color

                color = colors[idx]

                # now make a point source

                ps = SkyCoord(ra, dec, unit='deg', frame='icrs')

                # transform into the fermi frame

                ps_fermi = ps.transform_to(frame=self._frame)

                self.add_ray(ps_fermi, color=color)
コード例 #8
0
ファイル: test_sphtfunc.py プロジェクト: arahlin/healpy
 def test_map2alm_pol_gal_cut(self):
     tmp = [np.empty(o.size * 2) for o in self.mapiqu]
     for t, o in zip(tmp, self.mapiqu):
         t[::2] = o
     maps = [
         self.mapiqu,
         [o.astype(np.float32) for o in self.mapiqu],
         [t[::2] for t in tmp],
     ]
     for use_weights in [False, True]:
         for input in maps:
             gal_cut = 30
             nside = hp.get_nside(input)
             npix = hp.nside2npix(nside)
             gal_mask = (
                 np.abs(hp.pix2ang(nside, np.arange(npix), lonlat=True)[1]) < gal_cut
             )
             alm = hp.map2alm(
                 input, iter=10, use_weights=use_weights, gal_cut=gal_cut
             )
             output = hp.alm2map(alm, 32)
             for i, o in zip(input, output):
                 # Testing requires low tolerances because of the
                 # mask boundary
                 i[gal_mask] = 0
                 np.testing.assert_allclose(i, o, atol=1e-2)
コード例 #9
0
ファイル: CoordTrans.py プロジェクト: jizhi/huangqizhi_git
	def CelestialHealpix( self, healpixmap, ordering, coordin, coordout, epochin='2000', epochout='2000' ) : 
		'''
		healpixmap:
			(1) np.ndarray with shape=(12*nside**2,)
			(2) int number =nside

		ordering:
			'RINGE' | 'NESTED'

		return:
			Case (1) [hpix_in2out, healpixmap_in2out]
			Case (2) hpix_in2out

				Usage: healpixmap_in2out = healpixmap[hpix_in2out]
		'''
		try : nside = hp.get_nside(healpixmap)
		except : nside = self._Nside(healpixmap)
		ordering, nest = self._Ordering(ordering)
		hpix_in2out = np.arange(12*nside**2)
		Decb, RAl = hp.pix2ang(nside, hpix_in2out, nest=nest)
		Decb = np.pi/2 - Decb
		RAl, Decb = self.Celestial(RAl, Decb, coordout, coordin, epochout, epochin)
		hpix_in2out =hp.ang2pix(nside, np.pi/2-Decb,RAl, nest=nest) 
		try : return [hpix_in2out, healpixmap[hpix_in2out]]
		except : return hpix_in2out
コード例 #10
0
ファイル: Tools.py プロジェクト: mahmoud-lsw/GammaLike
def ApplyPSF(hpix, E_min, E_max, PSFFile='P7Clean_Front+Back.fits', sigma=.1, smoothed=False):
    """
    WARNING INCOMPLETE: Normalization off and smoothing below healpix scale explodes. 
    This method takes a healpix input 'hpix', and a spectrally averaged energy 'E'. It then looks up the corresponding
    PSF from tables -> calculates the legendre transform coefficients -> transforms the input pixles into spherical
    harmonics -> re-weight the alm coefficients according to the PSF -> returns the inverse transform
    **WARNING: DO NOT USE. THIS METHOD IS NOT YET COMPLETED**
    """

    # Spherical Harmonic transform 
    alm  = healpy.sphtfunc.map2alm(hpix)
    
    # get maximum value of l 
    l_max = healpy.sphtfunc.Alm.getlmax(len(alm))
    x = np.linspace(-30, 30, 10000)  # Cos(0) to Cos(pi)
    psf_y = (sigma*np.sqrt(2*np.pi))*np.exp(-x*x/(2*sigma**2))
    psf_x = np.cos(np.deg2rad(x))
    
    # Fit the PSF to l_max legendre polynomials
    cls = np.sqrt(4*np.pi/(2*np.arange(0, l_max+1)+1))*np.polynomial.legendre.legfit(psf_x, psf_y, l_max)
    conv_alm=healpy.sphtfunc.almxfl(alm, cls)

    # Find nside and return inverse transformed map. 
    nside = healpy.get_nside(hpix)
    if smoothed is False:
        return healpy.sphtfunc.alm2map(conv_alm, nside=nside, verbose=False)
    else:
        return healpy.sphtfunc.alm2map(alm, nside=nside, sigma=np.deg2rad(sigma), verbose=False)
コード例 #11
0
ファイル: hptools.py プロジェクト: jegudmunds/code
def hits2mask(mapin):
    nside = _hp.get_nside(mapin)
    badidx = _np.where(mapin <= 0.0)[0]
    mask = np.copy(mapin)
    
    mask[badidx] = 0.0; mask[~badidx] = 1.0

    return mask
コード例 #12
0
def deconvolve_map(map_in, fwhm_in=0.0, fwhm_out=0.0, lmax=None, binary_mask=None, pol=False, wiener=True, sky_prior=None):
    
    if fwhm_in == fwhm_out:
        return map_in

    if lmax is None:
        lmax = 3*hp.get_nside(map_in) - 1

    if binary_mask is None:
        binary_mask = np.logical_not(np.isnan(map_in[0] if pol else map_in))
    f_sky = float(np.sum(binary_mask))/binary_mask.size

    alm_in = estimate_alm(map_in, lmax, binary_mask, pol)
    alm_dec = deconvolve_alm(alm_in, fwhm_in=fwhm_in, fwhm_out=fwhm_out, f_sky=f_sky, pol=pol, wiener=True, sky_prior=sky_prior)
    map_dec = hp.alm2map(alm_dec, nside=hp.get_nside(map_in), pol=pol)
    
    return map_dec
コード例 #13
0
ファイル: Tmap2Vis.py プロジェクト: jizhi/huangqizhi_git
	def NsideConvert( self, nsideout=None, orderingin=None, inmap=None ) :
		''' '''
		nsideout = self._NsideCheck(nsideout)
		orderingin = self._OrderingCheck(orderingin)[0]
		if (self.verbose and jp.SysFrame(0,2)[-1][-2]=='') : strfont = ''
		else : strfont = '    '
		if (self.verbose) : 
			print strfont+'Tmap2Vis.NsideConvert'
		nsideinstr = ''
		if (inmap is None) : 
			print strfont+strfont+"nsidein="+str(hp.get_nside(self.Tmap))+", nsideout="+str(nsideout)+", orderingin='"+orderingin+"'"
			if(self.verbose and jp.SysFrame(0,2)[-1][-2]==''): print
			self.Tmap = hp.ud_grade(self.Tmap, nsideout, order_in=orderingin)
		else : 
			print strfont+strfont+"nsidein="+str(hp.get_nside(inmap))+", nsideout="+str(nsideout)+", orderingin='"+orderingin+"'"
			if(self.verbose and jp.SysFrame(0,2)[-1][-2]==''): print
			return hp.ud_grade(inmap, nsideout, order_in=orderingin)
コード例 #14
0
ファイル: test_gsm.py プロジェクト: cosmo-ethz/hide
 def test_load_signal_rescale(self):
     params = Struct(beam_nside = 2**6)
     ctx = Struct(params = params)
     
     ctx.frequency = 980
     astro_signal = gsm.load_signal(ctx)
     assert astro_signal is not None
     assert hp.get_nside(astro_signal) == ctx.params.beam_nside
コード例 #15
0
ファイル: healmap.py プロジェクト: rwogburn/smallfield
 def __init__(self,fname,dir='.',nside=[],cal=1.0):
   self.dir=dir
   self.fname=fname
   self.nside=nside
   self.cal=cal
   self.map = self._get_a_map_tqu()
   if self.nside == []:
     self.nside = hp.get_nside(self.map)
   return
コード例 #16
0
ファイル: hptools.py プロジェクト: jegudmunds/code
def get_outline(mapin):
    nside = _hp.get_nside(mapin)
    outline = _np.array([_hp.UNSEEN]*_hp.nside2npix(nside))
    pix = _np.where(mapin>0.0)[0]
    for p in pix:
        ptmp = _hp.get_all_neighbours(nside,p)
        i = _np.where(mapin[ptmp] == 0.0)[0]
        outline[ptmp[i]] = 1.0

    return outline
コード例 #17
0
def TQU_TO_TEB_maps(sky_TQU, lmax):
    nside = hp.get_nside(sky_TQU)

    alm_in = hp.map2alm(sky_TQU, lmax, pol=True)

    sky_TEB = np.empty((3, 12*nside**2))
    for i in range(3):
        sky_TEB[i] = hp.alm2map(alm_in[i], nside, pol=False)

    return sky_TEB
コード例 #18
0
def get_temp_gradient_map(T_sky, fwhm_in=0.0, fwhm_out=0.0, nside=None, lmax=None):
    if nside==None:
        nside = hp.get_nside(T_sky)
    if lmax==None:
        lmax = 3*nside - 1

    alm = estimate_alm(T_sky, lmax)
    alm_dec = deconvolve_alm(alm, lmax, fwhm_in, fwhm_out)
    grad_sky_T = hp.alm2map_der1(alm_dec, nside, lmax)

    return grad_sky_T[1:]
コード例 #19
0
def TEB_to_TQU_maps(sky_TEB, lmax):
    nside = hp.get_nside(sky_TEB)

    alm = np.empty((3, (lmax + 1)**2))
    
    for i in range(3):
        alm[i] = hp.map2alm(sky_TEB[i], lmax, pol=False)

    sky_TQU = hp.alm2map(alm, nside, lmax, pol=True)

    return sky_TQU
コード例 #20
0
def get_dust_opacity(msk,l,b):	
	deg2rad = np.pi/180.
	nside   = hp.get_nside(msk)
	opac    = [8.4e-27, 7.1e-27, 6.8e-27, 6.5e-27 ]
	err     = [3.0e-27, 1.9e-27, 1.8e-27, 1.8e-27 ]

	theta = (90.0 - b)*deg2rad
	phi   = l*deg2rad
	pix   = hp.ang2pix(nside, theta, phi, nest=False)
	val   = msk[pix]
	return [ opac[int(val)], err[int(val)] ]
コード例 #21
0
ファイル: Tmap2Vis.py プロジェクト: jizhi/huangqizhi_git
	def OrderingConvert( self, orderingin=None, orderingout=None, inmap=None ) : 
		''' '''
		orderingin, nestin, orderingout = self._OrderingCheck(orderingin, orderingout)[:3]
		if (self.verbose and jp.SysFrame(0,2)[-1][-2]=='') : strfont = ''
		else : strfont = '    '
		if (self.verbose) : 
			print strfont+'Tmap2Vis.OrderingConvert'
			print strfont+strfont+"orderingin='"+orderingin+"', orderingout='"+orderingout+"'"
		if (self.verbose and jp.SysFrame(0,2)[-1][-2]=='') : print
		if (inmap is None) : self.Tmap = hp.ud_grade(self.Tmap, self.nsidein, order_in=orderingin, order_out=orderingout)
		else : return hp.ud_grade(inmap, hp.get_nside(inmap), order_in=orderingin, order_out=orderingout)
コード例 #22
0
def mask_map_apodised(sky_map, apodised_mask, pol=True):
    nside = hp.get_nside(sky_map)
    if pol:
        masked_map = np.empty((3, 12*nside**2))
        for i in range(3):
            masked_map[i] = sky_map[i]*apodised_mask
    else:
        masked_map = sky_map*apodised_mask

    f_sky = np.mean(apodised_mask**2)

    return masked_map, f_sky
コード例 #23
0
ファイル: mapsampler.py プロジェクト: hpc4cmb/toast
def plug_holes(m, verbose=False, in_place=True, nest=False):
    """
    Use simple downgrading to derive estimates of the missing pixel values
    """
    autotimer = timing.auto_timer()
    nbad_start = np.sum(np.isclose(m, hp.UNSEEN))

    if nbad_start == m.size:
        if verbose:
            print('plug_holes: All map pixels are empty. Cannot plug holes',
                  flush=True)
        return

    if nbad_start == 0:
        return

    nside = hp.get_nside(m)
    npix = m.size
    if nest:
        mnest = m.copy()
    else:
        mnest = hp.reorder(m, r2n=True)

    lowres = mnest
    nside_lowres = nside
    bad = np.isclose(mnest, hp.UNSEEN)
    while np.any(bad) and nside_lowres > 1:
        nside_lowres //= 2
        lowres = hp.ud_grade(lowres, nside_lowres, order_in='NESTED')
        hires = hp.ud_grade(lowres, nside, order_in='NESTED')
        bad = np.isclose(mnest, hp.UNSEEN)
        mnest[bad] = hires[bad]

    nbad_end = np.sum(bad)

    if nbad_end != 0:
        mn = np.mean(mnest[np.logical_not(bad)])
        mnest[bad] = mn

    if not in_place:
        m = m.copy()
    if nest:
        m[:] = mnest
    else:
        m[:] = hp.reorder(mnest, n2r=True)

    if verbose and nbad_start != 0:
        print('plug_holes: Filled {} missing pixels ({:.2f}%), lowest '
              'resolution was Nside={}.'.format(
                  nbad_start, (100.*nbad_start) // npix, nside_lowres))
    del autotimer
    return m
コード例 #24
0
	def Equa2GalaHeal( self, healpixmap, ordering='RING', epoch='2000' ):
		'''
		healpixmap:
			Healpix map with nside=2**n, total pixels=12*nside**2

		ordering: 
			'RING' or 'NESTED'
		'''
		nside = hp.get_nside(healpixmap)
		l, b, nest = self._Nside2LonLat(nside, ordering)
		RA, Dec = self.Gala2Equa(l, b, epoch)
		n =hp.ang2pix(nside, (90-Dec)*np.pi/180, RA*np.pi/180, nest=nest)
		return healpixmap[n]
コード例 #25
0
ファイル: galaxy.py プロジェクト: radiocosmology/cora
def map_variance(input_map, nside):

    inp_nside = healpy.get_nside(input_map)

    # Convert to NESTED and then take advantage of this to group into lower
    # resolution pixels
    map_nest = healpy.reorder(input_map, r2n=True)
    map_nest = map_nest.reshape(-1, (inp_nside / nside) ** 2)

    # Calculate the variance in each low resolution pixel
    var_map = map_nest.var(axis=1)

    # Convert back to RING and return
    return healpy.reorder(var_map, n2r=True)
コード例 #26
0
ファイル: hputil.py プロジェクト: liamconnor/simulations_21cm
def sph_ps(map1, map2=None, lmax=None):

    lmax = lmax if lmax is not None else (3*healpy.get_nside(map1) - 1)

    alm1 = sphtrans_real(map1, lmax)
    alm2 = sphtrans_real(map2, lmax) if map is not None else alm1

    prod = alm1 * alm2.conj()

    s = prod[:, 0] + 2 * prod[:, 1:].sum(axis=1).real

    cl = s / (2.0*np.arange(lmax+1)+1.0)
    
    return cl
コード例 #27
0
ファイル: hptools.py プロジェクト: jegudmunds/code
def rotate_map(hpmap,coord=['C','G'],cov=True):
    '''
    Rotates the coordinates of a map
    By default, rotates from C to G
    '''
    (theta,phi) = _hp.pix2ang(_hp.get_nside(hpmap),
      _np.arange(0, len(hpmap)))
    r = _hp.Rotator(coord=coord, inv = True)
    #rotate new coordinate system to original coordinates
    theta_map, phi_map = r(theta, phi)
    rotated = _hp.get_interp_val(hpmap, theta_map, phi_map)
    if cov:
        ind1 = (rotated > 0.5); rotated[ind1] = 1.0; rotated[~ind1] = 0.0

    return rotated
コード例 #28
0
ファイル: ho_prep_data.py プロジェクト: annis/desgw-cos
def local_prob (vgal_ra, vgal_dec, map_ra, map_dec, map_val, target_ra, target_dec, inj_ra,  inj_dec) :
    import healpy as hp
    import rotate
    # rotate the galaxy coordinates to that place on the map
    # the way this works is that first the target galaxy is rotated onto the origin, bringing
    # its friends with it, then the origin is shifted onto the sim map position.
    ra, dec = rotate.rotateSkyToOrigin(vgal_ra, vgal_dec, target_ra, target_dec)
    ra, dec = rotate.rotateSkyAwayFromOrigin(ra, dec, inj_ra, inj_dec)
    # get the weights for that map. this works because the galaxy vector remains ordered.
    nsides = hp.get_nside(map_val)
    phi = ra*2*np.pi/360.;
    theta = (90-dec)*2*np.pi/360.
    pix = hp.ang2pix(nsides,theta,phi)
    local = map_val[pix]
    ix = np.invert(np.isnan(local)) & ( local > 3e-7)  # 99% inside GW map
    return local, ix
コード例 #29
0
ファイル: cltools.py プロジェクト: jegudmunds/code
def map2patch_100(map,n):

    nside = hp.get_nside(map)
    pix = np.arange((hp.nside2npix(nside)))
    (theta, phi) = hp.pix2ang(nside,pix)
    dec = (theta-np.pi/2.0)/np.pi*180.0; ra = phi/2.0/np.pi*360.0
    obspix = (map > 0.5)
    ri = np.mod(n,10); di = n/10
    bins = range(0,110,10)
    rap = np.percentile(ra[obspix],bins)
    decp = np.percentile(dec[obspix],bins)
    ragood = np.logical_and((rap[ri] < ra),(ra < rap[ri+1]))
    decgood = np.logical_and((decp[di] < dec),(dec < decp[di+1]))
    gdidx = np.logical_and(np.logical_and(ragood,decgood),obspix)
    outmap = np.copy(map)
    outmap[gdidx] = 1.0
    outmap[np.logical_not(gdidx)] = 0.0
    return outmap
コード例 #30
0
ファイル: bolo.py プロジェクト: ranajoy-cosmo/core-plus
    def get_sky_map(self):
        if self.config.sim_pol_type == "noise_only":
            self.sky_map = None
        elif self.config.sim_pol_type == "T":
            self.sky_map = hp.read_map(self.config.input_map, verbose=False)
        elif self.config.sim_pol_type == "QU":
            self.sky_map = hp.read_map(self.config.input_map, field=(0,1), verbose=False)
        elif self.config.sim_pol_type == "_QU":
            self.sky_map = hp.read_map(self.config.input_map, field=(1,2), verbose=False)
        else:
            self.sky_map = hp.read_map(self.config.input_map, field=(0,1,2), verbose=False)

        if not self.config.sim_pol_type == "noise_only":
            map_nside = hp.get_nside(self.sky_map)
            if map_nside != self.config.nside_in:
                prompter.prompt_warning("NSIDE of config does not match NSIDE of map")
                sys.exit()
        self.npix = hp.nside2npix(self.config.nside_in)
コード例 #31
0
def generate_from_fitsfile(filename, language):
    healpix_data, header = healpy.read_map(filename, h=True)
    try:
        healpix_data, distmu, distsigma, distnorm = healpy.read_map(
            filename, field=[0, 1, 2, 3])
        dim = 3
    except:
        dim = 2

    print("dimension = %d" % dim)
    if dim == 3:
        ii = (~numpy.isinf(distmu)) & (distmu > 0.0)
        print("good percent %.2f" % (numpy.sum(ii) * 100.0 / len(distmu)))
        dm = distmu[ii]
        prob = healpix_data[ii]
        x = numpy.multiply(prob, dm)
        print("Average distance %.2f " % (numpy.sum(x) / numpy.sum(prob)))

    wanted = [
        'DATE', 'DATE-OBS', 'INSTRUME', 'MJD-OBS', 'DISTMEAN', 'DISTSTD',
        'REFERENC'
    ]
    meta = {}
    for (k, v) in header:
        if k in wanted:
            meta[k] = v

    print("Generating contours...")
    skymapID = get_md(filename)

    contours = []
    levels = []
    deciles = []
    percentile = numpy.linspace(0.9, 0.1, 9)
    for p in percentile:
        (level, area) = get_level(healpix_data, p)
        levels.append(level)
        deciles.append([p * 100, area])
    meta['deciles'] = deciles

    for l in levels:
        print(l)

    # make latitude histogram
    nside = healpy.get_nside(healpix_data)
    allipix = numpy.arange(0, len(healpix_data))
    (th, ph) = healpy.pix2ang(nside, allipix)

    h = []
    max = 0
    for i in range(len(healpix_data)):
        if healpix_data[i] > max:
            max = healpix_data[i]
            imax = i
        if healpix_data[i] > levels[2]:
            ira = int(ph[i] * radian / 5.0)
            idec = int(th[i] * radian / 5.0)
            pair = [ira, idec]
            if pair not in h:
                h.append(pair)

    print(h)
    meta['histogram'] = h

    # make countours
    ngrid = 800
    apointRA = ph[imax] * radian
    apointDec = 90 - th[imax] * radian
    meta['apointRA'] = apointRA
    meta['apointDec'] = apointDec
    print('max prob %f %f' % (apointRA, apointDec))

    theta = numpy.linspace(0, numpy.pi, ngrid)
    phi = numpy.linspace(0, 2 * numpy.pi, 2 * ngrid)
    p, t = numpy.meshgrid(phi, theta)
    data_interpolated_to_grid = healpy.pixelfunc.get_interp_val(
        healpix_data, t, p)
    contourplot = plt.contour(p, t, data_interpolated_to_grid, levels=levels)
    numcontours = 0
    contours = []
    for n, c in enumerate(contourplot.collections):
        color = hsv2rgb(n * 1.0 / len(levels), 1.0, 1.0)
        for contourPath, path in enumerate(c.get_paths()):
            plist = []
            for v in path.vertices:
                plist.append([v[0] * radian, 90 - v[1] * radian])
            spaths = splitPaths(plist)
            for sp in spaths:
                contours.append({
                    "name": "%d-percentile" % ((n + 1) * 10),
                    "color": color,
                    "coords": sp
                })
                #            contours.append({"name":"%d-percentile"%((n+1)*10), "color":color, "coords":plist})
                numcontours += 1
    print("made %d polylines" % numcontours)

    # galaxies from Glade
    gc = galaxyCatalog.gc(glade_filename)
    gc.info()

    de = gc.declination
    ra = gc.right_ascension
    name = numpy.array(gc.name)
    print("has %d sources" % len(ra))

    ph_at_sources = ra / radian
    th_at_sources = (90 - de) / radian

    skymap_at_sources = numpy.zeros(len(ra))
    weighted_sources = numpy.zeros(len(ra))

    # distribution of log distance
    ldc = gc.log_distance_centres
    dh = gc.distance_hist

    if dim == 2:
        distance = gc.distance
        for i in range(len(ra)):
            ipix = healpy.ang2pix(nside, th_at_sources[i], ph_at_sources[i])
            skymap_at_sources[i] = healpix_data[ipix]
            weighted_sources[i] = skymap_at_sources[i]

    if dim == 3:
        distance = gc.distance
        distance_pdf = numpy.zeros(len(ra))
        for i in range(len(ra)):
            ipix = healpy.ang2pix(nside, th_at_sources[i], ph_at_sources[i])
            skymap_at_sources[i] = healpix_data[ipix]
            norm = distnorm[ipix]
            sigma = distsigma[ipix]
            mu = distmu[ipix]
            if math.isnan(distance[i]):
                distance_pdf[i] = 0.0
                for k in range(len(ldc)):
                    d = math.pow(10.0, ldc[k])
                    r = math.exp(-(d - mu)**2 / (2 * sigma**2)) * norm * (d**2)
                    distance_pdf[i] += r * dh[k]
            else:
                d = distance[i]
                distance_pdf[i] = math.exp(-(d - mu)**2 /
                                           (2 * sigma**2)) * norm * (d**2)
            weighted_sources[i] = skymap_at_sources[i] * distance_pdf[i]

    weighted_sources = numpy.array(weighted_sources)
    maxw = numpy.max(weighted_sources)
    sumw = numpy.sum(weighted_sources)
    print('max weight = ', maxw)

    index = numpy.argsort(-weighted_sources)

    num = len(index)
    if num > 200: num = 200
    selected_sources = (index[:num])
    print("Selected %d galaxies" % len(selected_sources))

    sources = []
    for de, ra, nm, w, distance in zip(de[selected_sources],
                                       ra[selected_sources],
                                       name[selected_sources],
                                       weighted_sources[selected_sources],
                                       distance[selected_sources]):

        absw = w / sumw  # weight compared to total weight
        sw = math.sqrt(w / maxw)  # normalized sqrt of weight
        if sw < 1.e-3: continue
        if math.isnan(distance): distance = 0.0
        sources.append({
            "coords": [ra, de],
            "name": nm,
            "sw": sw,
            "absw": absw,
            "distance": distance
        })
        n += 1

    print("Sources complete with %d" % n)

    print("Language = ", language)
    return make_json(meta, contours, sources)
コード例 #32
0
if mpiutil.rank0:
    ## Useful output
    print("==================================")
    print("Projecting file:\n    %s\ninto:\n    %s" %
          (args.mapfile, args.outfile))
    print("Using beamtransfer: %s" % args.teldir)
    print("Truncating to modes with S/N > %f" % cut)
    print("==================================")

    # Calculate alm's and broadcast
    print("Read in skymap.")
    f = h5py.File(args.mapfile)
    skymap = f["map"][:]
    f.close()
    nside = healpy.get_nside(skymap[0])

    alm = hputil.sphtrans_sky(skymap, lmax=cyl.lmax)
# else:
#    almr = None

mpiutil.world.Bcast([alm, MPI.COMPLEX16], root=0)

cb = cyl.baselines + np.array([[cyl.u_width, 0.0]])

if cyl.positive_m_only:
    taumax = (cb**2).sum(axis=-1)**0.5 / 3e8
else:
    taumax = (np.concatenate((cb, cb))**2).sum(axis=-1)**0.5 / 3e8
tau = np.fft.fftfreq(cyl.nfreq,
                     (cyl.frequencies[1] - cyl.frequencies[0]) * 1e6)
コード例 #33
0
# coding: utf-8

import numpy as np, curvedsky as cs, healpy as hp, local, warnings, misctools, tools_cib
warnings.filterwarnings("ignore")

ascale = 1.0

for freq in ['545', '857']:

    for field, wind in zip([0, 1, 2, 3], ['G20', 'G40', 'G60', 'G70']):

        iobj = tools_cib.init_cib(wind=wind, ascale=ascale)

        if misctools.check_path([iobj.fmask, iobj.famask],
                                overwrite=False,
                                verbose=False):
            continue

        # bad pixel
        Imask = hp.fitsfunc.read_map(
            '/global/homes/t/toshiyan/scratch/PR2/mask/HFI_Mask_GalPlane-apo0_2048_R2.00.fits',
            field=field)
        nside = hp.get_nside(Imask)

        # apodized
        amask = cs.utils.apodize(nside, Imask, ascale)

        # save
        hp.fitsfunc.write_map(iobj.fmask, Imask, overwrite=True)
        hp.fitsfunc.write_map(iobj.famask, amask, overwrite=True)
コード例 #34
0
    def fe_jump(self, x, iter, beta):

        q = x.copy()
        lqxy = 0
        
        fe_limit = np.max(self.fe)
        
        #draw skylocation and frequency from f-stat map
        accepted = False
        while accepted==False:
            log_f_new = self.params[self.pimap['log10_fgw']].sample()
            f_idx = (np.abs(np.log10(self.fe_freqs) - log_f_new)).argmin()

            gw_theta = np.arccos(self.params[self.pimap['cos_gwtheta']].sample())
            gw_phi = self.params[self.pimap['gwphi']].sample()
            hp_idx = hp.ang2pix(hp.get_nside(self.fe), gw_theta, gw_phi)

            fe_new_point = self.fe[f_idx, hp_idx]
            if np.random.uniform()<(fe_new_point/fe_limit):
                accepted = True

        #draw other parameters from prior
        cos_inc = self.params[self.pimap['cos_inc']].sample()
        psi = self.params[self.pimap['psi']].sample()
        phase0 = self.params[self.pimap['phase0']].sample()
        log10_h = self.params[self.pimap['log10_h']].sample()
        

        #put new parameters into q
        signal_name = 'cw'
        for param_name, new_param in zip(['log10_fgw','gwphi','cos_gwtheta','cos_inc','psi','phase0','log10_h'],
                                           [log_f_new, gw_phi, np.cos(gw_theta), cos_inc, psi, phase0, log10_h]):
            q[self.pimap[param_name]] = new_param
        
        #calculate Hastings ratio
        log_f_old = x[self.pimap['log10_fgw']]
        f_idx_old = (np.abs(np.log10(self.fe_freqs) - log_f_old)).argmin()
        
        gw_theta_old = np.arccos(x[self.pimap['cos_gwtheta']])
        gw_phi_old = x[self.pimap['gwphi']]
        hp_idx_old = hp.ang2pix(hp.get_nside(self.fe), gw_theta_old, gw_phi_old)
        
        fe_old_point = self.fe[f_idx_old, hp_idx_old]
        if fe_old_point>fe_limit:
            fe_old_point = fe_limit
            
        log10_h_old = x[self.pimap['log10_h']]
        phase0_old = x[self.pimap['phase0']]
        psi_old = x[self.pimap['psi']]
        cos_inc_old = x[self.pimap['cos_inc']]
        
        hastings_extra_factor = self.params[self.pimap['log10_h']].get_pdf(log10_h_old)
        hastings_extra_factor *= 1/self.params[self.pimap['log10_h']].get_pdf(log10_h)
        hastings_extra_factor = self.params[self.pimap['phase0']].get_pdf(phase0_old)
        hastings_extra_factor *= 1/self.params[self.pimap['phase0']].get_pdf(phase0)
        hastings_extra_factor = self.params[self.pimap['psi']].get_pdf(psi_old)
        hastings_extra_factor *= 1/self.params[self.pimap['psi']].get_pdf(psi)
        hastings_extra_factor = self.params[self.pimap['cos_inc']].get_pdf(cos_inc_old)
        hastings_extra_factor *= 1/self.params[self.pimap['cos_inc']].get_pdf(cos_inc)        
        
        lqxy = np.log(fe_old_point/fe_new_point * hastings_extra_factor)

        return q, float(lqxy)
コード例 #35
0
ファイル: powspec_calc.py プロジェクト: m33ra/powspechi
def maps2cld(maps):
    r"""Calculate the angular power spectrum of a given map or maps.

    Parameters
    ----------
    maps : array_like
        A single map or array/list of maps. It must be a HEALPix map, i.e.,
        the number of indices must correspond to a nside value.

    Returns
    -------
    cld : dict
        A dictionary whose keys correspond to the 'full' power spectrum
        and the same without the :math:`a_{\ell 0}` modes, denoted 'mdz'. The 
        values of `cld` are ndarrays with dimensions dependent on the number 
        of entry maps and their resolution.
    averd : dict
        If more than one map is given, the averaged power spectrum is calculated.
        Its keys are also 'full' and 'mdz'. Its values are lists of arrays: index
        0 corresponds to the mean `cld` value, while index 1 is the error on the 
        mean.

    Notes
    -----
    A 'full' angular power spectrum has the following expression:
    
    .. math:: C_{\ell} = \frac{1}{2\ell + 1}\sum_{m = -\ell}^{m = \ell} |a_{\ell m}|^2,

    while 'mdz', which stands for :math:`m\neq0` has the form

    .. math:: C^{m\neq0}_{\ell} = C_{\ell} - \frac{1}{2\ell + 1} |a_{\ell 0}|^2,

    :math:`a_{\ell m}` are the coefficients associated with the spherical harmonics :math:`Y_{\ell m}`.

    """

    if maps[0].ndim == 0:
        nevts, nside = 1, hp.get_nside(maps)
        maps = [maps]
    else:
        nevts, nside = len(maps), hp.get_nside(maps[0])

    js = np.arange(3 * nside)
    cld = {
        'full': np.zeros((nevts, 3 * nside)),
        'mdz': np.zeros((nevts, 3 * nside - 1))
    }
    ii = 0

    for emap in maps:
        cld['full'][ii, :], alms = hp.anafast(emap, alm=True)
        c0s = 1. / (2. * js + 1.) * np.abs(alms[js])**2
        cld['mdz'][ii, :] = cld['full'][ii, 1:] - c0s[1:]
        ii += 1

    if nevts != 1:
        averd = {}
        for key in cld.keys():
            averd[key] = [
                np.mean(cld[key], axis=0),
                np.std(cld[key], axis=0, ddof=1) / np.sqrt(nevts)
            ]
        return cld, averd
    else:
        for key in cld.keys():
            cld[key] = cld[key][0]
        return cld
コード例 #36
0
def multi_res_comp_sep(components, instrument, data, nsides,
                       **minimize_kwargs):
    """ Basic component separation

    Parameters
    ----------
    components: list
        List storing the :class:`Component` s of the mixing matrix
    instrument:
        Object that provides the following as a key or an attribute.

        - **frequency**
        - **depth_i** or **depth_p** (optional, frequencies are inverse-noise
          weighted according to these noise levels)

        They can be anything that is convertible to a float numpy array.
    data: ndarray or MaskedArray
        Data vector to be separated. Shape *(n_freq, ..., n_pix).*
        *...* can be

        - absent or 1: temperature maps
        - 2: polarization maps
        - 3: temperature and polarization maps (see note)

        Values equal to `hp.UNSEEN` or, if `MaskedArray`, masked values are
        neglected during the component separation process.
    nsides: seq
        Specify the ``nside`` for each free parameter of the components

    Returns
    -------
    result: dict
	It includes

	- **param**: *(list)* - Names of the parameters fitted
	- **x**: *(seq)* - ``x[i]`` is the best-fit (map of) the *i*-th
          parameter. The map has ``nside = nsides[i]``
        - **Sigma**: *(ndarray)* - ``Sigma[i, j]`` is the (map of) the
          semi-analytic covariance between the *i*-th and the *j*-th parameter.
          It is meaningful only in the high signal-to-noise regime and when the
          *cov* is the true covariance of the data
        - **s**: *(ndarray)* - Component amplitude maps
        - **mask_good**: *(ndarray)* - mask of the entries actually used in the
          component separation

    Note
    ----

    * During the component separation, a pixel is masked if at least one of
      its frequencies is masked.
    * If you provide temperature and polarization maps, they will constrain the
      **same** set of parameters. In particular, separation is **not** done
      independently for temperature and polarization. If you want an
      independent fitting for temperature and polarization, please launch

      >>> res_T = basic_comp_sep(component_T, instrument, data[:, 0], **kwargs)
      >>> res_P = basic_comp_sep(component_P, instrument, data[:, 1:], **kwargs)

    """
    instrument = standardize_instrument(instrument)
    max_nside = max(nsides)
    if max_nside == 0:
        return basic_comp_sep(components, instrument, data, **minimize_kwargs)

    # Prepare mask and set to zero all the frequencies in the masked pixels:
    # NOTE: mask are bad pixels
    mask = _intersect_mask(data)
    data = hp.pixelfunc.ma_to_array(data).copy()
    data[..., mask] = 0  # Thus no contribution to the spectral likelihood

    try:
        data_nside = hp.get_nside(data[0])
    except TypeError:
        raise ValueError("data has to be a stack of healpix maps")

    invN = _get_prewhiten_factors(instrument, data.shape, data_nside)
    invN = np.diag(invN**2)

    def array2maps(x):
        i = 0
        maps = []
        for nside in nsides:
            npix = _my_nside2npix(nside)
            maps.append(x[i:i + npix])
            i += npix
        return maps

    extra_dim = [1] * (data.ndim - 1)
    unpack = lambda x: [
        _my_ud_grade(m, max_nside).reshape(-1, *extra_dim)
        for m in array2maps(x)
    ]

    # Traspose the the data and put the pixels that share the same spectral
    # indices next to each other
    n_pix_max_nside = hp.nside2npix(max_nside)
    pix_ids = np.argsort(hp.ud_grade(np.arange(n_pix_max_nside), data_nside))
    data = data.T[pix_ids].reshape(n_pix_max_nside,
                                   (data_nside // max_nside)**2,
                                   *data.T.shape[1:])
    back_pix_ids = np.argsort(pix_ids)

    A = MixingMatrix(*components)
    assert A.n_param == len(nsides), ("%i free parameters but %i nsides" %
                                      (len(A.defaults), len(nsides)))
    A_ev = A.evaluator(instrument.frequency, unpack)
    A_dB_ev = A.diff_evaluator(instrument.frequency, unpack)
    x0 = [x for c in components for x in c.defaults]
    x0 = [
        np.full(_my_nside2npix(nside), px0) for nside, px0 in zip(nsides, x0)
    ]
    x0 = np.concatenate(x0)

    if len(x0) == 0:
        A_ev = A_ev()

    comp_of_dB = [(c_db,
                   _my_ud_grade(np.arange(_my_nside2npix(p_nside)), max_nside))
                  for p_nside, c_db in zip(nsides, A.comp_of_dB)]

    # Component separation
    res = alg.comp_sep(A_ev, data, invN, A_dB_ev, comp_of_dB, x0,
                       **minimize_kwargs)

    # Craft output
    # 1) Apply the mask, if any
    # 2) Restore the ordering of the input data (pixel dimension last)
    def restore_index_mask_transpose(x):
        x = x.reshape(-1, *x.shape[2:])[back_pix_ids]
        x[mask] = hp.UNSEEN
        return x.T

    res.params = A.params
    res.s = restore_index_mask_transpose(res.s)
    res.chi = restore_index_mask_transpose(res.chi)
    if 'chi_dB' in res:
        for i in range(len(res.chi_dB)):
            res.chi_dB[i] = restore_index_mask_transpose(res.chi_dB[i])

    if len(x0) > 0:
        x_masks = [
            _my_ud_grade(mask.astype(float), nside) == 1. for nside in nsides
        ]
        res.x = array2maps(res.x)
        for x, x_mask in zip(res.x, x_masks):
            x[x_mask] = hp.UNSEEN

    res.mask_good = ~mask
    return res
コード例 #37
0
ファイル: test.py プロジェクト: hoangducthuong/beamconv
def test_ghosts(lmax=700,
                mmax=5,
                fwhm=43,
                ra0=-10,
                dec0=-57.5,
                az_throw=50,
                scan_speed=2.8,
                rot_period=4.5 * 60 * 60,
                hwp_mode=None):
    '''
    Similar test to `scan_bicep`, but includes reflected ghosts

    Simulates a 24h BICEP2-like scan strategy
    using a random LCDM realisation and a 3 x 3 grid
    of Gaussian beams pairs. Bins tods into maps and
    compares to smoothed input maps (no pair-
    differencing). MPI-enabled.

    Keyword arguments
    ---------

    lmax : int,
        bandlimit (default : 700)
    mmax : int,
        assumed azimuthal bandlimit beams (symmetric in this example
        so 2 would suffice) (default : 5)
    fwhm : float,
        The beam FWHM in arcmin (default : 40)
    ra0 : float,
        Ra coord of centre region (default : -10)
    dec0 : float,  (default : -57.5)
        Ra coord of centre region
    az_throw : float,
        Scan width in azimuth (in degrees) (default : 50)
    scan_speed : float,
        Scan speed in deg/s (default : 1)
    rot_period : float,
        The instrument rotation period in sec
        (default : 600)
    hwp_mode : str, None
        HWP modulation mode, either "continuous",
        "stepped" or None. Use freq of 1 or 1/10800 Hz
        respectively (default : None)
    '''

    mlen = 24 * 60 * 60  # hardcoded mission length

    # Create LCDM realization
    ell, cls = get_cls()
    np.random.seed(25)  # make sure all MPI ranks use the same seed
    alm = hp.synalm(cls, lmax=lmax, new=True, verbose=True)  # uK

    b2 = ScanStrategy(
        mlen,  # mission duration in sec.
        sample_rate=12.01,  # sample rate in Hz
        location='spole')  # Instrument at south pole

    # Create a 3 x 3 square grid of Gaussian beams
    b2.create_focal_plane(nrow=3, ncol=3, fov=5, lmax=lmax, fwhm=fwhm)

    # Create reflected ghosts for every detector
    # We create two ghosts per detector. They overlap
    # but have different fwhm. First ghost is just a
    # scaled down version of the main beam, the second
    # has a much wider Gaussian shape.
    # After this initialization, the code takes
    # the ghosts into account without modifications
    b2.create_reflected_ghosts(b2.beams,
                               amplitude=0.01,
                               ghost_tag='ghost_1',
                               dead=False)
    b2.create_reflected_ghosts(b2.beams,
                               amplitude=0.01,
                               fwhm=100,
                               ghost_tag='ghost_2',
                               dead=False)

    # calculate tods in two chunks
    b2.partition_mission(0.5 * b2.nsamp)

    # Allocate and assign parameters for mapmaking
    b2.allocate_maps(nside=256)

    # set instrument rotation
    b2.set_instr_rot(period=rot_period, angles=[68, 113, 248, 293])

    # Set HWP rotation
    if hwp_mode == 'continuous':
        b2.set_hwp_mod(mode='continuous', freq=1.)
    elif hwp_mode == 'stepped':
        b2.set_hwp_mod(mode='stepped', freq=1 / (3 * 60 * 60.))

    # Generate timestreams, bin them and store as attributes
    b2.scan_instrument_mpi(alm,
                           verbose=1,
                           ra0=ra0,
                           dec0=dec0,
                           az_throw=az_throw,
                           nside_spin=256,
                           max_spin=mmax)

    # Solve for the maps
    maps, cond = b2.solve_for_map(fill=np.nan)

    # Plotting
    if b2.mpi_rank == 0:
        print('plotting results')

        cart_opts = dict(
            rot=[ra0, dec0, 0],
            lonra=[-min(0.5 * az_throw, 90),
                   min(0.5 * az_throw, 90)],
            latra=[-min(0.375 * az_throw, 45),
                   min(0.375 * az_throw, 45)],
            unit=r'[$\mu K_{\mathrm{CMB}}$]')

        # plot rescanned maps
        plot_iqu(maps,
                 '../scratch/img/',
                 'rescan_ghost',
                 sym_limits=[250, 5, 5],
                 plot_func=hp.cartview,
                 **cart_opts)

        # plot smoothed input maps
        nside = hp.get_nside(maps[0])
        hp.smoothalm(alm, fwhm=np.radians(fwhm / 60.), verbose=False)
        maps_raw = hp.alm2map(alm, nside, verbose=False)

        plot_iqu(maps_raw,
                 '../scratch/img/',
                 'raw_ghost',
                 sym_limits=[250, 5, 5],
                 plot_func=hp.cartview,
                 **cart_opts)

        # plot difference maps
        for arr in maps_raw:
            # replace stupid UNSEEN crap
            arr[arr == hp.UNSEEN] = np.nan

        diff = maps_raw - maps

        plot_iqu(diff,
                 '../scratch/img/',
                 'diff_ghost',
                 sym_limits=[1e+1, 1e-1, 1e-1],
                 plot_func=hp.cartview,
                 **cart_opts)

        # plot condition number map
        cart_opts.pop('unit', None)

        plot_map(cond,
                 '../scratch/img/',
                 'cond_ghost',
                 min=2,
                 max=5,
                 unit='condition number',
                 plot_func=hp.cartview,
                 **cart_opts)

        # plot input spectrum
        cls[3][cls[3] <= 0.] *= -1.
        dell = ell * (ell + 1) / 2. / np.pi
        plt.figure()
        for i, label in enumerate(['TT', 'EE', 'BB', 'TE']):
            plt.semilogy(ell, dell * cls[i], label=label)

        plt.legend()
        plt.ylabel(r'$D_{\ell}$ [$\mu K^2_{\mathrm{CMB}}$]')
        plt.xlabel(r'Multipole [$\ell$]')
        plt.savefig('../scratch/img/cls_ghost.png')
        plt.close()
コード例 #38
0
def Plotter(
    input,
    dataset,
    nside,
    auto,
    min,
    max,
    mid,
    rng,
    colorbar,
    lmax,
    fwhm,
    mask,
    mfill,
    sig,
    remove_dipole,
    logscale,
    size,
    white_background,
    darkmode,
    png,
    cmap,
    title,
    ltitle,
    unit,
    scale,
    outdir,
    verbose,
    data,
):
    rcParams["backend"] = "agg" if png else "pdf"
    rcParams["legend.fancybox"] = True
    rcParams["lines.linewidth"] = 2
    rcParams["savefig.dpi"] = 300
    rcParams["axes.linewidth"] = 1

    masked = False

    if darkmode:
        rcParams["text.color"] = "white"  # axes background color
        rcParams["axes.facecolor"] = "white"  # axes background color
        rcParams["axes.edgecolor"] = "white"  # axes edge color
        rcParams["axes.labelcolor"] = "white"
        rcParams["xtick.color"] = "white"  # color of the tick labels
        rcParams["ytick.color"] = "white"  # color of the tick labels
        rcParams["grid.color"] = "white"  # grid color
        rcParams[
            "legend.facecolor"] = "inherit"  # legend background color (when 'inherit' uses axes.facecolor)
        rcParams[
            "legend.edgecolor"] = "white"  # legend edge color (when 'inherit' uses axes.edgecolor)

    rc(
        "text.latex",
        preamble=r"\usepackage{sfmath}",
    )

    # Which signal to plot
    print("")
    print("")
    print("{:#^48}".format(""))
    #if len(input)==1: input = input[0]
    print("Plotting", input)

    #######################
    ####   READ MAP   #####
    #######################

    # Get maps array if .h5 file
    if input.endswith(".h5"):
        from src.commands import h5map2fits
        from src.tools import alm2fits_tool

        # Get maps from alm data in .h5
        if dataset.endswith("alm"):
            print("Converting alms to map")
            (
                maps,
                nsid,
                lmax,
                fwhm,
                outfile,
            ) = alm2fits_tool(
                input,
                dataset,
                nside,
                lmax,
                fwhm,
                save=False,
            )

        # Get maps from map data in .h5
        elif dataset.endswith("map"):
            print("Reading map from h5")
            (
                maps,
                nsid,
                lmax,
                outfile,
            ) = h5map2fits(input, dataset, save=False)

        # Found no data specified kind in .h5
        else:
            print("Dataset not found. Breaking.")
            print(f"Does {input}/{dataset} exist?")
            sys.exit()

    # Plot all signals specified
    print(f"Plotting the following signals: {sig}")
    print("{:#^48}".format(""))
    for polt in sig:
        signal_label = get_signallabel(polt)
        if data is not None:
            m = data.copy()
            m = hp.ma(m)
            nsid = hp.get_nside(m)
            outfile = input.replace(".fits", "")
        else:
            try:
                if input.endswith(".fits"):
                    map, header = hp.read_map(
                        input,
                        field=polt,
                        verbose=False,
                        h=True,
                        dtype=None,
                    )
                    header = dict(header)
                    try:
                        signal_label = header[f"TTYPE{polt+1}"]
                        if signal_label in ["TEMPERATURE", "TEMP"]:
                            signal_label = "T"
                        if signal_label in [
                                "Q-POLARISATION", "Q_POLARISATION"
                        ]:
                            signal_label = "Q"
                        if signal_label in [
                                "U-POLARISATION", "U_POLARISATION"
                        ]:
                            signal_label = "U"

                    except:
                        pass

                    m = hp.ma(map)  # Dont use header for this
                    nsid = hp.get_nside(m)
                    outfile = input.replace(".fits", "")

                elif input.endswith(".h5"):
                    m = maps[polt]
            except:
                print(f"{polt} not found")
                sys.exit()

        ############
        #  SMOOTH  #
        ############
        if fwhm > 0 and input.endswith(".fits"):
            print("")
            print(f"Smoothing fits map to {fwhm} arcmin fwhm")
            print("")
            m = hp.smoothing(
                m,
                fwhm=arcmin2rad(fwhm),
                lmax=lmax,
            )

        ############
        # UD_GRADE #
        ############
        if nside is not None and input.endswith(".fits"):
            if nsid != nside:
                print(f"UDgrading map from {nsid} to {nside}")
                m = hp.ud_grade(
                    m,
                    nside,
                )
        else:
            nside = nsid

        ########################
        #### remove dipole #####
        ########################
        if remove_dipole:
            starttime = time.time()
            print("Removing dipole:")
            dip_mask_name = remove_dipole
            # Mask map for dipole estimation
            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)
            print(f"Dipole vector: {dip}")
            print(f"Dipole amplitude: {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)

            # Subtract dipole map from data
            m = m - dipole
            print(f"Dipole removal : {(time.time() - starttime)}"
                  ) if verbose else None

        #######################
        #### Auto-param   #####
        #######################
        # Reset these every signal
        tempmin = min
        tempmax = max
        temptitle = title
        templtitle = ltitle
        tempunit = unit
        templogscale = logscale
        tempcmap = cmap

        # ttl, unt and cmb are temporary variables for title, unit and colormap
        if auto:
            (
                _title,
                ticks,
                cmp,
                lgscale,
                scale,
            ) = get_params(
                m,
                outfile,
                polt,
                signal_label,
            )

            # Title
            if _title["stddev"]:
                ttl = _title["param"] + r"$_{\mathrm{" + _title[
                    "comp"] + "}}^{\sigma}$"
            elif _title["mean"]:
                ttl = r"$\langle$" + _title[
                    "param"] + r"$\rangle$" + r"$_{\mathrm{" + _title[
                        "comp"] + "}}^{ }$"
            elif _title["diff"]:
                ttl = r"$\Delta$ " + _title["param"] + r"$_{\mathrm{" + _title[
                    "comp"] + "}}^{" + _title["diff_label"] + "}$"
            else:
                ttl = _title["param"] + r"$_{\mathrm{" + _title[
                    "comp"] + "}}^{ }$"
            # Left signal label
            lttl = r"$" + _title["sig"] + "$"
            if lttl == "$I$":
                lttl = "$T$"
            elif lttl == "$QU$":
                lttl = "$P$"
            #elif lttl == "$P$":
            #    ticks *= 2

            # Tick bug fix
            mn = ticks[0]
            mx = ticks[-1]
            if len(ticks) > 2:
                mid = ticks[1:-1]

            # Unit
            unt = _title["unit"]
        else:
            ttl = ""
            lttl = ""
            unt = ""
            ticks = [False, False]
            cmp = "planck"
            lgscale = False

        # Scale map
        m *= scale

        # If range has been specified, set.
        """
        if rng:
            if rng == "auto":
                if minmax:
                    mn = np.min(m)
                    mx = np.max(m)
                else:
                    mn, mx = get_ticks(m, 97.5)
                if min is False:
                    min = mn
                if max is False:
                    max = mx
            else:
                rng = float(rng)
                min = -rng
                max = rng
            
            ticks = [min, 0.0, max]
            print("hihi",rng)
        else: 
            print("pre", ticks, min, max)
            # If min and max have been specified, set.
            if min is not False:
                ticks[0] = float(min)

            if max is not False:
                ticks[-1] = float(max)
        """
        # If min and max have been specified, set.
        if rng == "auto" and not auto:
            print("Setting range from 97.5th percentile of data")
            mn, mx = get_ticks(m, 97.5)
        elif rng == "minmax":
            print("Setting range from min to max of data")
            mn = np.min(m)
            mx = np.max(m)
        else:
            try:
                if float(rng) > 0.0:
                    mn = -float(rng)
                    mx = float(rng)
                    ticks = [False, 0.0, False]
            except:
                pass

        if min is False:
            min = mn
        else:
            min = float(min)

        if max is False:
            max = mx
        else:
            max = float(max)

        ticks[0] = min
        ticks[-1] = max
        if mid:
            ticks = [min, *mid, max]

        ##########################
        #### Plotting Params #####
        ##########################

        # Upper right title
        if not title:
            title = ttl

        # Upper left title
        if not ltitle:
            ltitle = lttl

        # Unit under colorbar
        if not unit:
            unit = unt

        # Image size -  ratio is always 1/2
        xsize = 2000
        ysize = int(xsize / 2.0)

        ticklabels = ticks
        #######################
        ####   logscale   #####
        #######################
        # Some maps turns on logscale automatically
        # -logscale command overwrites this
        if logscale == None:
            logscale = lgscale

        if logscale:
            print("Applying logscale")
            starttime = time.time()

            m = np.log10(0.5 * (m + np.sqrt(4.0 + m * m)))

            ticks = []
            for i in ticklabels:
                if i > 0:
                    ticks.append(np.log10(i))
                elif i < 0:
                    ticks.append(-np.log10(abs(i)))
                else:
                    ticks.append(i)

            m = np.maximum(np.minimum(m, ticks[-1]), ticks[0])

            print(
                "Logscale",
                (time.time() - starttime),
            ) if verbose else None

        ######################
        #### COLOR SETUP #####
        ######################
        # Chose colormap manually
        if cmap == None:
            # If not defined autoset or goto planck
            cmap = cmp

        if cmap == "planck":
            from pathlib import Path

            cmap = Path(__file__).parent / "parchment1.dat"
            cmap = col.ListedColormap(np.loadtxt(cmap) / 255.0, "planck")
        else:
            try:
                import cmasher
                cmap = eval(f"cmasher.{cmap}")
            except:
                cmap = plt.get_cmap(cmap)

        print(f"Colormap: {cmap.name}")
        #######################
        ####  Projection? #####
        #######################
        theta = np.linspace(np.pi, 0, ysize)
        phi = np.linspace(-np.pi, np.pi, xsize)
        longitude = np.radians(np.linspace(-180, 180, xsize))
        latitude = np.radians(np.linspace(-90, 90, ysize))

        # project the map to a rectangular matrix xsize x ysize
        PHI, THETA = np.meshgrid(phi, theta)
        grid_pix = hp.ang2pix(nside, THETA, PHI)

        ######################
        ######## Mask ########
        ######################
        if mask:
            print(f"Masking using {mask}")
            masked = True

            # Apply mask
            hp.ma(m)
            m.mask = np.logical_not(hp.read_map(mask, field=polt))

            # Don't know what this does, from paperplots by Zonca.
            grid_mask = m.mask[grid_pix]
            grid_map = np.ma.MaskedArray(m[grid_pix], grid_mask)

            if mfill:
                cmap.set_bad(mfill)  # color of missing pixels
                # cmap.set_under("white") # color of background, necessary if you want to use
                # using directly matplotlib instead of mollview has higher quality output
        else:
            grid_map = m[grid_pix]

        ######################
        #### Formatting ######
        ######################
        from matplotlib.projections.geo import GeoAxes

        class ThetaFormatterShiftPi(GeoAxes.ThetaFormatter):
            """Shifts labelling by pi
            Shifts labelling from -180,180 to 0-360"""
            def __call__(self, x, pos=None):
                if x != 0:
                    x *= -1
                if x < 0:
                    x += 2 * np.pi
                return GeoAxes.ThetaFormatter.__call__(self, x, pos)

        sizes = get_sizes(size)
        for width in sizes:
            print("Size: " + str(width))
            height = width / 2.0

            # Make sure text doesnt change with colorbar
            height *= 1.275 if colorbar else 1

            ################
            ##### font #####
            ################
            if width > 12.0:
                fontsize = 8
            elif width == 12.0:
                fontsize = 7
            else:
                fontsize = 6

            fig = plt.figure(figsize=(
                cm2inch(width),
                cm2inch(height),
            ), )
            ax = fig.add_subplot(111, projection="mollweide")

            # rasterized makes the map bitmap while the labels remain vectorial
            # flip longitude to the astro convention
            image = plt.pcolormesh(
                longitude[::-1],
                latitude,
                grid_map,
                vmin=ticks[0],
                vmax=ticks[-1],
                rasterized=True,
                cmap=cmap,
                shading='auto',
            )
            # graticule
            ax.set_longitude_grid(60)
            ax.xaxis.set_major_formatter(ThetaFormatterShiftPi(60))

            if width < 10:
                ax.set_latitude_grid(45)
                ax.set_longitude_grid_ends(90)

            ################
            ### COLORBAR ###
            ################
            if colorbar:
                # colorbar
                from matplotlib.ticker import FuncFormatter

                cb = fig.colorbar(
                    image,
                    orientation="horizontal",
                    shrink=0.3,
                    pad=0.08,
                    ticks=ticks,
                    format=FuncFormatter(fmt),
                )

                # Format tick labels
                print("Ticks: ", ticklabels)
                ticklabels = [fmt(i, 1) for i in ticklabels]
                cb.ax.set_xticklabels(ticklabels)

                cb.ax.xaxis.set_label_text(unit)
                cb.ax.xaxis.label.set_size(fontsize)
                # cb.ax.minorticks_on()

                cb.ax.tick_params(
                    which="both",
                    axis="x",
                    direction="in",
                    labelsize=fontsize,
                )
                cb.ax.xaxis.labelpad = 4  # -11
                # workaround for issue with viewers, see colorbar docstring
                cb.solids.set_edgecolor("face")

            # remove longitude tick labels
            ax.xaxis.set_ticklabels([])
            # remove horizontal grid
            ax.xaxis.set_ticks([])
            ax.yaxis.set_ticklabels([])
            ax.yaxis.set_ticks([])
            plt.grid(True)

            ###################
            ## RIGHT TITLE ####
            ###################
            plt.text(
                6.0,
                1.3,
                r"%s" % title,
                ha="center",
                va="center",
                fontsize=fontsize,
            )

            ##################
            ## LEFT TITLE ####
            ##################
            plt.text(
                -6.0,
                1.3,
                r"%s" % ltitle,
                ha="center",
                va="center",
                fontsize=fontsize,
            )

            ##############
            #### SAVE ####
            ##############
            plt.tight_layout()
            filetype = "png" if png else "pdf"
            # Turn on transparency unless told otherwise
            tp = False if white_background else True

            ##############
            ## filename ##
            ##############
            print(f"Signal label: {signal_label}")

            outfile = outfile.replace("_IQU_", "_")
            outfile = outfile.replace("_I_", "_")

            filename = []
            filename.append(f"{str(int(fwhm))}arcmin") if fwhm > 0 else None
            filename.append("cb") if colorbar else None
            filename.append("masked") if masked else None
            filename.append("nodip") if remove_dipole else None
            filename.append("dark") if darkmode else None
            filename.append(f"c-{cmap.name}")

            nside_tag = "_n" + str(int(nside))
            if nside_tag in outfile:
                outfile = outfile.replace(nside_tag, "")

            fn = outfile + f"_{signal_label}_w{str(int(width))}" + nside_tag

            for i in filename:
                fn += f"_{i}"
            fn += f".{filetype}"

            starttime = time.time()
            if outdir:
                fn = outdir + "/" + os.path.split(fn)[-1]

            print(f"Output: {fn}")
            plt.savefig(
                fn,
                bbox_inches="tight",
                pad_inches=0.02,
                transparent=tp,
                format=filetype,
            )
            print(
                "Savefig",
                (time.time() - starttime),
            ) if verbose else None

            plt.close()
            print(
                "Totaltime:",
                (time.time() - totaltime),
            ) if verbose else None

        min = tempmin
        max = tempmax
        title = temptitle
        ltitle = temptitle
        unit = tempunit
        logscale = templogscale
        cmap = tempcmap
コード例 #39
0
def basic_comp_sep(components, instrument, data, nside=0, **minimize_kwargs):
    """ Basic component separation

    Parameters
    ----------
    components: list
        List storing the :class:`Component` s of the mixing matrix
    instrument:
        Object that provides the following as a key or an attribute.

        - **frequency**
        - **depth_i** or **depth_p** (optional, frequencies are inverse-noise
          weighted according to these noise levels)

        They can be anything that is convertible to a float numpy array.
    data: ndarray or MaskedArray
        Data vector to be separated. Shape *(n_freq, ..., n_pix).*
        *...* can be

        - absent or 1: temperature maps
        - 2: polarization maps
        - 3: temperature and polarization maps (see note)

        Values equal to `hp.UNSEEN` or, if `MaskedArray`, masked values are
        neglected during the component separation process.
    nside:
        For each pixel of a HEALPix map with this nside, the non-linear
        parameters are estimated independently

    Returns
    -------
    result: dict
	It includes

	- **param**: *(list)* - Names of the parameters fitted
	- **x**: *(ndarray)* - ``x[i]`` is the best-fit (map of) the *i*-th
          parameter
        - **Sigma**: *(ndarray)* - ``Sigma[i, j]`` is the (map of) the
          semi-analytic covariance between the *i*-th and the *j*-th parameter.
          It is meaningful only in the high signal-to-noise regime and when the
          *cov* is the true covariance of the data
        - **s**: *(ndarray)* - Component amplitude maps
        - **mask_good**: *(ndarray)* - mask of the entries actually used in the
          component separation

    Note
    ----

    * During the component separation, a pixel is masked if at least one of
      its frequencies is masked.
    * If you provide temperature and polarization maps, they will constrain the
      **same** set of parameters. In particular, separation is **not** done
      independently for temperature and polarization. If you want an
      independent fitting for temperature and polarization, please launch

      >>> res_T = basic_comp_sep(component_T, instrument, data[:, 0], **kwargs)
      >>> res_P = basic_comp_sep(component_P, instrument, data[:, 1:], **kwargs)

    """
    instrument = standardize_instrument(instrument)
    # Prepare mask and set to zero all the frequencies in the masked pixels:
    # NOTE: mask are bad pixels
    mask = _intersect_mask(data)
    data = hp.pixelfunc.ma_to_array(data).copy()
    data[..., mask] = 0  # Thus no contribution to the spectral likelihood

    try:
        data_nside = hp.get_nside(data[0])
    except TypeError:
        data_nside = 0
    prewhiten_factors = _get_prewhiten_factors(instrument, data.shape,
                                               data_nside)
    A_ev, A_dB_ev, comp_of_param, x0, params = _A_evaluator(
        components, instrument, prewhiten_factors=prewhiten_factors)
    if len(x0) == 0:
        A_ev = A_ev()
    if prewhiten_factors is None:
        prewhitened_data = data.T
    else:
        prewhitened_data = prewhiten_factors * data.T

    # Component separation
    if nside:
        patch_ids = hp.ud_grade(np.arange(hp.nside2npix(nside)),
                                hp.npix2nside(data.shape[-1]))
        res = alg.multi_comp_sep(A_ev, prewhitened_data, None, A_dB_ev,
                                 comp_of_param, patch_ids, x0,
                                 **minimize_kwargs)
    else:
        res = alg.comp_sep(A_ev, prewhitened_data, None, A_dB_ev,
                           comp_of_param, x0, **minimize_kwargs)

    # Craft output
    # 1) Apply the mask, if any
    # 2) Restore the ordering of the input data (pixel dimension last)
    res.params = params
    res.s = res.s.T
    res.s[..., mask] = hp.UNSEEN
    res.chi = res.chi.T
    res.chi[..., mask] = hp.UNSEEN
    if 'chi_dB' in res:
        for i in range(len(res.chi_dB)):
            res.chi_dB[i] = res.chi_dB[i].T
            res.chi_dB[i][..., mask] = hp.UNSEEN
    if nside and len(x0) > 0:
        x_mask = hp.ud_grade(mask.astype(float), nside) == 1.
        res.x[x_mask] = hp.UNSEEN
        res.Sigma[x_mask] = hp.UNSEEN
        res.x = res.x.T
        res.Sigma = res.Sigma.T

    res.mask_good = ~mask
    return res
コード例 #40
0
ファイル: template.py プロジェクト: healpy/pysm
def read_map(path, nside, unit=None, field=0, map_dist=None):
    """Wrapper of `healpy.read_map` for PySM data. This function also extracts
    the units from the fits HDU and applies them to the data array to form an
    `astropy.units.Quantity` object.
    This function requires that the fits file contains a TUNIT key for each
    populated field.

    Parameters
    ----------
    path : object `pathlib.Path`, or str
        Path of HEALPix map to be read.
    nside : int
        Resolution at which to return map. Map is read in at whatever resolution
        it is stored, and `healpy.ud_grade` is applied.

    Returns
    -------
    map : ndarray
        Numpy array containing HEALPix map in RING ordering.
    """
    mpi_comm = None if map_dist is None else map_dist.mpi_comm
    pixel_indices = None if map_dist is None else map_dist.pixel_indices
    filename = utils.RemoteData().get(path)

    if (mpi_comm is not None and mpi_comm.rank == 0) or (mpi_comm is None):
        output_map = hp.read_map(filename, field=field, dtype=None)
        dtype = output_map.dtype
        # numba only supports little endian
        if dtype.byteorder == ">":
            dtype = dtype.newbyteorder()
        # mpi4py has issues if the dtype is a string like ">f4"
        if dtype == np.dtype(np.float32):
            dtype = np.dtype(np.float32)
        elif dtype == np.dtype(np.float64):
            dtype = np.dtype(np.float64)
        nside_in = hp.get_nside(output_map)
        if nside < nside_in:  # do downgrading in double precision
            output_map = hp.ud_grade(output_map.astype(np.float64), nside_out=nside)
        else:
            output_map = hp.ud_grade(output_map, nside_out=nside)
        output_map = output_map.astype(dtype, copy=False)
        if unit is None:
            unit = extract_hdu_unit(filename)
        shape = output_map.shape
    elif mpi_comm is not None and mpi_comm.rank > 0:
        npix = hp.nside2npix(nside)
        try:
            ncomp = len(field)
        except TypeError:  # field is int
            ncomp = 1
        shape = npix if ncomp == 1 else (len(field), npix)
        unit = ""
        dtype = None

    if mpi_comm is not None:
        from mpi4py import MPI

        dtype = mpi_comm.bcast(dtype, root=0)
        unit = mpi_comm.bcast(unit, root=0)

        node_comm = mpi_comm.Split_type(MPI.COMM_TYPE_SHARED)
        mpi_type = MPI._typedict[dtype.char]
        mpi_type_size = mpi_type.Get_size()
        win = MPI.Win.Allocate_shared(
            np.prod(shape) * mpi_type_size if node_comm.rank == 0 else 0,
            mpi_type_size,
            comm=node_comm,
        )
        shared_buffer, item_size = win.Shared_query(0)
        assert item_size == mpi_type_size
        shared_buffer = np.array(shared_buffer, dtype="B", copy=False)
        node_shared_map = np.ndarray(buffer=shared_buffer, dtype=dtype, shape=shape)

        # only the first MPI process in each node is in this communicator
        rank_comm = mpi_comm.Split(0 if node_comm.rank == 0 else MPI.UNDEFINED)
        if mpi_comm.rank == 0:
            node_shared_map[:] = output_map
        if node_comm.rank == 0:
            rank_comm.Bcast(node_shared_map, root=0)

        mpi_comm.barrier()
        # code with broadcast to whole communicator
        # if mpi_comm.rank > 0:
        #     output_map = np.empty(shape, dtype=dtype)
        # mpi_comm.Bcast(output_map, root=0)
    else:  # without MPI node_shared_map is just another reference to output_map
        node_shared_map = output_map

    if pixel_indices is not None:
        # make copies so that Python can release the full array
        try:  # multiple components
            output_map = np.array(
                [each[pixel_indices].copy() for each in node_shared_map]
            )
        except IndexError:  # single component
            output_map = node_shared_map[pixel_indices].copy()

    if mpi_comm is not None:
        del node_shared_map
        del shared_buffer
        win.Free()
        gc.collect()

    return u.Quantity(output_map, unit, copy=False)
コード例 #41
0
ファイル: MPI_stack2healpix.py プロジェクト: sopXx/picasso
def main(args):
    comm    = MPI.COMM_WORLD
    rank    = comm.Get_rank()
    nprocs  = comm.Get_size()
    Npix = 128 ## WARNING: This is hard-coded because of the architecture of both CNN

    glob_ra,glob_dec, _  = np.loadtxt(args.ptsourcefile ,unpack=True)

    localsize =np.int_(  glob_ra.shape[0]/nprocs  ) ## WARNING:  this MUST  evenly divide!!!!!!

    ra =  glob_ra[slice( rank *localsize ,  (rank +1)* localsize)]
    dec =  glob_dec[slice( rank *localsize ,  (rank +1)* localsize)]
    Nstacks= ra.shape [0]

    if args.pol :
        keys = ['T', 'Q', 'U']
        inputmap = hp.read_map(args.hpxmap  ,field=[0,1,2] )
    else:
        keys = ['T' ]
        inputmap = [hp.read_map( args.hpxmap) ]


    mask = np.zeros_like (inputmap[0] )

    nside = hp.get_nside(inputmap)

    size_im = {2048: 192.  ,4096 : 64., 32 :360. }
    beam =np.deg2rad( args.beamsize /60.)

    Inpainter =  HoleInpainter (args.method,
                    modeldir = args.checkpoint_dir,
                    verbose  =args.debug )

    for i in range(Nstacks):

        sizepatch = size_im[nside]*1. /Npix/60.
        header       = set_header(ra[i],dec[i], sizepatch )
        tht,phi      = rd2tp(ra[i],dec[i])
        vec          = hp.ang2vec( theta = tht,phi =phi )
        pixs         = hp.query_disc(nside,vec,3* beam)
        mask [pixs]  = 1.
        for k,j  in  zip(keys, range(len(inputmap)) ) :
            fname = args.stackfile+k+'_{:.5f}_{:.5f}_masked.npy'.format(ra[i],dec[i] )
            fname = args.stackfile

            Inpainter.setup_input( fname  )
            predicted = Inpainter.exec ()

            np.save(args.stackfile+k+'_{:.5f}_{:.5f}_inpainted.npy'.format(ra[i],dec[i] ), predicted)
            maskmap =  f2h (predicted ,header, nside )
            inputmap[j][pixs] = inpaintedmap[pixs]
        break

        maps  = np.concatenate(inputmap).reshape(hp.nside2npix(nside), len(inputmap))
        reducmaps = np.zeros_like(maps)
        globmask= np.zeros_like(mask)

        comm.Allreduce(maps, reducmaps, op=MPI.SUM)
        comm.Allreduce(mask, globmask , op=MPI.SUM)
        if rank ==0 :
            hp.write_map(args.inpaintedmap , [inputmap[k] *(1- globmask) + reducmaps[:,k]  *globmask for k in range(len(inputmap))]  )

        comm.Barrier()

        comm.Disconnect
コード例 #42
0
    def sample(self, src_ra, mean_mu, poisson=True):
        r"""
        Inject mean_mu (more below) events using rejection sampling from
        the healpy src_map.

        Events from MC are selected from the same declination band as the
        sampled position because the detector acceptance is declination
        dependent and only events from the same declination band are
        representative for an event position at the same declination.

        `src_ra` is kept as a dummy argument for compatibility but has
        no effect.

        Parameters
        ----------
        mean_mu : float
            Mean number of events to sample
        poisson : bool
            Use poisson fluctuations, otherwise sample exactly *mean_mu*.
            (default: True)

        Returns
        --------
        num : int
            Number of events
        sam_ev : iterator
            Sampled events for each loop iteration, either as simple array or
            as dictionary for each sample, having the same fields as exp.
        """
        def assign_and_drop(sam_ev, inj_ra, inj_dec):
            r"""
            Assign sampled ra/dec positions and drop mc fields from
            injected sample. This replaces the rotate function in the
            PointSourceInjector class.
            """
            # Assign sampled locations from src map.
            sam_ev["ra"] = inj_ra
            sam_ev["dec"] = inj_dec
            # Drop MC fields from the injected events
            mc_names = ['ow', 'trueDec', 'trueE', 'trueRa']
            return drop_fields(sam_ev, mc_names)

        # Here starts the generator part
        while True:
            # Sample mu_mean directly or get n_inj from a poisson distribution
            # if poisson = True was given
            n_inj = (self.random.poisson(mean_mu) if poisson else int(
                np.around(mean_mu)))

            # If no events should be sampled, return nothing
            if n_inj < 1:
                yield n_inj, None
                continue

            # Otherwise sample pixel indices from combined soruce map
            inj_ind, _ = amp_hp.healpy_rejection_sampler(self.src_map, n=n_inj)

            # Assign indices to healpy map coordinates
            NSIDE = hp.get_nside(self.src_map)
            inj_th, inj_phi = hp.pix2ang(NSIDE, inj_ind)

            # Get equatorial coordinates for the injected signal
            inj_dec, inj_ra = amp_hp.ThetaPhiToDecRa(inj_th, inj_phi)

            # Temporary recarray to hold the sampled mc_arr events
            sam_idx = np.empty((n_inj, ), dtype=self.mc_arr.dtype)

            # Loop over injected evts, select MC evt from all given samples
            # and fill it into inj.
            for i, (_dec, ra) in enumerate(zip(inj_dec, inj_ra)):
                # First get the zenith band per event with correct boundaries
                # This is from psLLH _select_events()
                dec = (np.pi - 2. * self._dec_bandwidth) / np.pi * _dec
                min_dec = max(-np.pi / 2., dec - self._dec_bandwidth)
                max_dec = min(+np.pi / 2., dec + self._dec_bandwidth)

                # Pick a random event from the MC data in the dec band.
                # Events are weighted using the _norm_w set up in _weight()
                mask = np.logical_and(self.mc_arr["dec"] > min_dec,
                                      self.mc_arr["dec"] < max_dec)

                # Choose one event from mc_arr per sampled location
                sam_idx[i] = np.random.choice(self.mc_arr,
                                              size=1,
                                              p=self._norm_w)

            # When we use multiple samples, store injected events from each
            # given sample seperately
            enums = np.unique(sam_idx["enum"])

            # Case if only one rec array (single sample) was given in fill()
            if len(enums) == 1 and enums[0] < 0:
                # Only one sample, just return recarray
                sam_ev = np.copy(self.mc[enums[0]][sam_idx["idx"]])
                # Assign sampled ra/dec to single output array
                yield n_inj, assign_and_drop(sam_ev, inj_ra, inj_dec)
                continue

            # For multiple samples return each injected event in a dict
            # indicating from which original sample it came
            sam_ev = dict()
            for enum in enums:
                mask = (sam_idx["enum"] == enum)
                idx = sam_idx[mask]["idx"]
                sam_ev_i = np.copy(self.mc[enum][idx])
                # Assign sampled ra/dec to each out array
                sam_ev[enum] = assign_and_drop(sam_ev_i, inj_ra[mask],
                                               inj_dec[mask])

            yield n_inj, sam_ev
コード例 #43
0
ファイル: test.py プロジェクト: hoangducthuong/beamconv
def test_satellite_scan(lmax=700,
                        mmax=2,
                        fwhm=43,
                        ra0=-10,
                        dec0=-57.5,
                        az_throw=50,
                        scan_speed=2.8,
                        hwp_mode=None,
                        alpha=45.,
                        beta=45.,
                        alpha_period=5400.,
                        beta_period=600.,
                        delta_az=0.,
                        delta_el=0.,
                        delta_psi=0.,
                        jitter_amp=1.0):
    '''
    Simulates a satellite scan strategy 
    using a random LCDM realisation and a 3 x 3 grid
    of Gaussian beams pairs. Bins tods into maps and
    compares to smoothed input maps (no pair-
    differencing). MPI-enabled.

    Keyword arguments
    ---------

    lmax : int,
        bandlimit (default : 700)
    mmax : int, 
        assumed azimuthal bandlimit beams (symmetric in this example
        so 2 would suffice) (default : 2)
    fwhm : float,
        The beam FWHM in arcmin (default : 40)
    ra0 : float,
        Ra coord of centre region (default : -10)
    dec0 : float,  (default : -57.5)
        Ra coord of centre region
    az_throw : float,
        Scan width in azimuth (in degrees) (default : 50)
    scan_speed : float,
        Scan speed in deg/s (default : 1)
    hwp_mode : str, None
        HWP modulation mode, either "continuous",
        "stepped" or None. Use freq of 1 or 1/10800 Hz
        respectively (default : None)
    '''

    print('Simulating a satellite...')
    mlen = 3 * 24 * 60 * 60  # hardcoded mission length

    # Create LCDM realization
    ell, cls = get_cls()
    np.random.seed(25)  # make sure all MPI ranks use the same seed
    alm = hp.synalm(cls, lmax=lmax, new=True, verbose=True)  # uK

    sat = ScanStrategy(
        mlen,  # mission duration in sec.
        external_pointing=True,  # Telling code to use non-standard scanning
        sample_rate=12.01,  # sample rate in Hz
        location='space')  # Instrument at south pole

    # Create a 3 x 3 square grid of Gaussian beams
    sat.create_focal_plane(nrow=7, ncol=7, fov=15, lmax=lmax, fwhm=fwhm)

    # calculate tods in two chunks
    sat.partition_mission(0.5 * sat.nsamp)

    # Allocate and assign parameters for mapmaking
    sat.allocate_maps(nside=256)

    scan_opts = dict(q_bore_func=sat.satellite_scan,
                     ctime_func=sat.satellite_ctime,
                     q_bore_kwargs=dict(),
                     ctime_kwargs=dict())

    # Generate timestreams, bin them and store as attributes
    sat.scan_instrument_mpi(alm,
                            verbose=1,
                            ra0=ra0,
                            dec0=dec0,
                            az_throw=az_throw,
                            nside_spin=256,
                            max_spin=mmax,
                            **scan_opts)

    # Solve for the maps
    maps, cond, proj = sat.solve_for_map(fill=np.nan, return_proj=True)

    # Plotting
    if sat.mpi_rank == 0:
        print('plotting results')

        cart_opts = dict(unit=r'[$\mu K_{\mathrm{CMB}}$]')

        # plot rescanned maps

        plot_iqu(maps,
                 '../scratch/img/',
                 'rescan_satellite',
                 sym_limits=[250, 5, 5],
                 plot_func=hp.mollview,
                 **cart_opts)

        # plot smoothed input maps
        nside = hp.get_nside(maps[0])
        hp.smoothalm(alm, fwhm=np.radians(fwhm / 60.), verbose=False)
        maps_raw = hp.alm2map(alm, nside, verbose=False)

        plot_iqu(maps_raw,
                 '../scratch/img/',
                 'raw_satellite',
                 sym_limits=[250, 5, 5],
                 plot_func=hp.mollview,
                 **cart_opts)

        # plot difference maps
        for arr in maps_raw:
            # replace stupid UNSEEN crap
            arr[arr == hp.UNSEEN] = np.nan

        diff = maps_raw - maps

        plot_iqu(diff,
                 '../scratch/img/',
                 'diff_satellite',
                 sym_limits=[1e-6, 1e-6, 1e-6],
                 plot_func=hp.mollview,
                 **cart_opts)

        # plot condition number map
        cart_opts.pop('unit', None)

        plot_map(cond,
                 '../scratch/img/',
                 'cond_satellite',
                 min=2,
                 max=5,
                 unit='condition number',
                 plot_func=hp.mollview,
                 **cart_opts)

        plot_map(proj[0],
                 '../scratch/img/',
                 'hits_satellite',
                 unit='Hits',
                 plot_func=hp.mollview,
                 **cart_opts)
コード例 #44
0
def harmonic_ilc(components,
                 instrument,
                 data,
                 lbins=None,
                 weights=None,
                 iter=3):
    """ Harmonic Internal Linear Combination

    Parameters
    ----------
    components: list or tuple of lists
        `Components` of the mixing matrix. They must have no free parameter.
    instrument:
        Object that provides the following as a key or an attribute.

        - **frequency**
        - **fwhm** (arcmin) they are deconvolved before ILC

        They can be anything that is convertible to a float numpy array.
    data: ndarray or MaskedArray
        Data vector to be separated. Shape ``(n_freq, ..., n_pix)``.
        ``...`` can be 1, 3 or absent. If 3, the separation is done independently
	fot T, E and B.
        Values equal to hp.UNSEEN or, if MaskedArray, masked values are
        neglected during the component separation process.
    lbins: array
        It stores the edges of the bins that will have the same ILC weights.
        If a multipole is not in a bin but is the alms, an independent bin
	will be assigned to it
    weights: array
        If provided data are multiplied by the weights map before computing alms

    Returns
    -------
    result : dict
	It includes

        - **W**: *(ndarray)* - ILC weights for each component and possibly
	  each index of the `...` dimension in the alms.
        - **s**: *(ndarray)* - Component maps
        - **cl_in**: *(ndarray)* - Spectra of the input alm
        - **cl_out**: *(ndarray)* - Spectra of the output alm
        - **fsky**: *(ndarray)* - The input fsky used to correct the cls 

    Note
    ----

    * During the component separation, a pixel is masked if at least one of its
      frequencies is masked.
    * Output spectra are divided by the fsky. fsky is computed with the MASTER
      formula if `weights` is provided, otherwise it is the fraction of unmasked
      pixels

    """
    instrument = standardize_instrument(instrument)
    nside = hp.get_nside(data[0])
    lmax = 3 * nside - 1
    lmax = min(lmax, lbins.max())
    n_comp = len(components)
    if weights is not None:
        assert not np.any(_intersect_mask(data) * weights.astype(bool)), \
            "Weights are non-zero where the data is masked"
        fsky = np.mean(weights**2)**2 / np.mean(weights**4)
    else:
        mask = _intersect_mask(data)
        fsky = float(mask.sum()) / mask.size

    logging.info('Computing alms')
    try:
        assert np.any(instrument.Beams)
    except (AttributeError, AssertionError):
        beams = None
    else:  # Deconvolve the beam
        beams = instrument.Beams

    alms = _get_alms(data, beams, lmax, weights, iter=iter)

    logging.info('Computing ILC')
    res = harmonic_ilc_alm(components, instrument, alms, lbins, fsky)

    logging.info('Back to real')
    alms = res.s
    res.s = np.empty((n_comp, ) + data.shape[1:], dtype=data.dtype)
    for c in range(n_comp):
        res.s[c] = hp.alm2map(alms[c], nside)

    return res
コード例 #45
0
ファイル: test.py プロジェクト: hoangducthuong/beamconv
def scan_atacama(lmax=700,
                 mmax=5,
                 fwhm=40,
                 mlen=48 * 60 * 60,
                 nrow=3,
                 ncol=3,
                 fov=5.0,
                 ra0=[-10, 170],
                 dec0=[-57.5, 0],
                 el_min=45.,
                 cut_el_min=False,
                 az_throw=50,
                 scan_speed=1,
                 rot_period=0,
                 hwp_mode='continuous'):
    '''
    Simulates 48h of an atacama-based telescope with a 3 x 3 grid
    of Gaussian beams pairs. Prefers to scan the bicep patch but
    will try to scan the ABS_B patch if the first is not visible.

    Keyword arguments
    ---------

    lmax : int
        bandlimit (default : 700)
    mmax : int
        assumed azimuthal bandlimit beams (symmetric in this example
        so 2 would suffice) (default : 5)
    fwhm : float
        The beam FWHM in arcmin (default : 40)
    mlen : int
        The mission length [seconds] (default : 48 * 60 * 60)
    nrow : int
        Number of detectors along row direction (default : 3)
    ncol : int
        Number of detectors along column direction (default : 3)
    fov : float
        The field of view in degrees (default : 5.0)
    ra0 : float, array-like
        Ra coord of centre region (default : [-10., 85.])
    dec0 : float, array-like
        Ra coord of centre region (default : [-57.5, 0.])
    el_min : float
        Minimum elevation range [deg] (default : 45)
    cut_el_min: bool
            If True, excludes timelines where el would be less than el_min
    az_throw : float
        Scan width in azimuth (in degrees) (default : 10)
    scan_speed : float
        Scan speed in deg/s (default : 1)
    rot_period : float
        The instrument rotation period in sec
        (default : 600)
    hwp_mode : str, None
        HWP modulation mode, either "continuous",
        "stepped" or None. Use freq of 1 or 1/10800 Hz
        respectively (default : continuous)
    '''

    # hardcoded mission length

    # Create LCDM realization
    ell, cls = get_cls()
    np.random.seed(25)  # make sure all MPI ranks use the same seed
    alm = hp.synalm(cls, lmax=lmax, new=True, verbose=True)  # uK

    ac = ScanStrategy(
        mlen,  # mission duration in sec.
        sample_rate=12.01,  # sample rate in Hz
        location='atacama')  # Instrument at south pole

    # Create a 3 x 3 square grid of Gaussian beams
    ac.create_focal_plane(nrow=nrow, ncol=ncol, fov=fov, lmax=lmax, fwhm=fwhm)

    # calculate tods in two chunks
    ac.partition_mission(0.5 * ac.mlen * ac.fsamp)

    # Allocate and assign parameters for mapmaking
    ac.allocate_maps(nside=256)

    # set instrument rotation
    ac.set_instr_rot(period=rot_period)

    # Set HWP rotation
    if hwp_mode == 'continuous':
        ac.set_hwp_mod(mode='continuous', freq=1.)
    elif hwp_mode == 'stepped':
        ac.set_hwp_mod(mode='stepped', freq=1 / (3 * 60 * 60.))

    # Generate timestreams, bin them and store as attributes
    ac.scan_instrument_mpi(alm,
                           verbose=2,
                           ra0=ra0,
                           dec0=dec0,
                           az_throw=az_throw,
                           nside_spin=256,
                           el_min=el_min,
                           cut_el_min=cut_el_min,
                           create_memmap=True)

    # Solve for the maps
    maps, cond = ac.solve_for_map(fill=np.nan)

    # Plotting
    if ac.mpi_rank == 0:
        print('plotting results')
        img_out_path = '../scratch/img/'

        moll_opts = dict(unit=r'[$\mu K_{\mathrm{CMB}}$]')

        # plot rescanned maps
        plot_iqu(maps,
                 img_out_path,
                 'rescan_atacama',
                 sym_limits=[250, 5, 5],
                 plot_func=hp.mollview,
                 **moll_opts)

        # plot smoothed input maps
        nside = hp.get_nside(maps[0])
        hp.smoothalm(alm, fwhm=np.radians(fwhm / 60.), verbose=False)
        maps_raw = hp.alm2map(alm, nside, verbose=False)

        plot_iqu(maps_raw,
                 img_out_path,
                 'raw_atacama',
                 sym_limits=[250, 5, 5],
                 plot_func=hp.mollview,
                 **moll_opts)

        # plot difference maps
        for arr in maps_raw:
            # replace stupid UNSEEN crap
            arr[arr == hp.UNSEEN] = np.nan

        diff = maps_raw - maps

        plot_iqu(diff,
                 img_out_path,
                 'diff_atacama',
                 sym_limits=[1e-6, 1e-6, 1e-6],
                 plot_func=hp.mollview,
                 **moll_opts)

        # plot condition number map
        moll_opts.pop('unit', None)

        plot_map(cond,
                 img_out_path,
                 'cond_atacama',
                 min=2,
                 max=5,
                 unit='condition number',
                 plot_func=hp.mollview,
                 **moll_opts)

        # plot input spectrum
        cls[3][cls[3] <= 0.] *= -1.
        dell = ell * (ell + 1) / 2. / np.pi
        plt.figure()
        for i, label in enumerate(['TT', 'EE', 'BB', 'TE']):
            plt.semilogy(ell, dell * cls[i], label=label)

        plt.legend()
        plt.ylabel(r'$D_{\ell}$ [$\mu K^2_{\mathrm{CMB}}$]')
        plt.xlabel(r'Multipole [$\ell$]')
        plt.savefig('../scratch/img/cls_atacama.png')
        plt.close()

        print("Results written to {}".format(os.path.abspath(img_out_path)))
コード例 #46
0
def powerSpectrum(hMap,
                  mask=None,
                  theory=[],
                  fsCl=None,
                  nBins=51,
                  lRange=None,
                  plot=False,
                  path="./figures/tests/test_power.pdf",
                  save=True):
    """Compute the power spectrum of a healpix map.
   """

    nSide = hp.get_nside(hMap)
    if mask is not None:
        hMap *= mask

    # define ell bins
    lMax = 3 * nSide - 1
    if lRange is None:
        lEdges = np.logspace(np.log10(1.), np.log10(lMax), nBins, 10.)
    else:
        lEdges = np.logspace(np.log10(lRange[0]), np.log10(lRange[-1]), nBins,
                             10.)

    # Unbinned power spectrum
    power = hp.anafast(hMap)
    power = np.nan_to_num(power)

    # corresponding ell values
    ell = np.arange(len(power))

    # Bin the power spectrum
    Cl, lEdges, binIndices = stats.binned_statistic(ell,
                                                    power,
                                                    statistic='mean',
                                                    bins=lEdges)

    # correct for fsky from the mask
    if mask is not None:
        fsky = np.sum(mask) / len(mask)
        Cl /= fsky

    # bin centers
    lCen, lEdges, binIndices = stats.binned_statistic(ell,
                                                      ell,
                                                      statistic='mean',
                                                      bins=lEdges)
    # when bin is empty, replace lCen by a naive expectation
    lCenNaive = 0.5 * (lEdges[:-1] + lEdges[1:])
    lCen[np.where(np.isnan(lCen))] = lCenNaive[np.where(np.isnan(lCen))]
    # number of modes
    Nmodes, lEdges, binIndices = stats.binned_statistic(ell,
                                                        2 * ell + 1,
                                                        statistic='sum',
                                                        bins=lEdges)
    Nmodes = np.nan_to_num(Nmodes)

    # 1sigma uncertainty on Cl
    if fsCl is None:
        sCl = Cl * np.sqrt(2)
    else:
        sCl = np.array(map(fsCl, lCen))
    sCl /= np.sqrt(Nmodes)
    sCl = np.nan_to_num(sCl)

    if plot:
        factor = lCen**2  # 1.

        fig = plt.figure(0)
        ax = fig.add_subplot(111)
        #
        ax.errorbar(lCen, factor * Cl, yerr=factor * sCl, c='b', fmt='.')
        ax.errorbar(lCen, -factor * Cl, yerr=factor * sCl, c='r', fmt='.')
        #
        for f in theory:
            L = np.logspace(np.log10(1.), np.log10(np.max(ell)), 201, 10.)
            ClExpected = np.array(map(f, L))
            ax.plot(L, L**2 * ClExpected, 'k')
        #
        ax.set_xscale('log', nonposx='clip')
        ax.set_yscale('log', nonposy='clip')
        #ax.set_xlim(1.e1, 4.e4)
        #ax.set_ylim(1.e-5, 2.e5)
        ax.set_xlabel(r'$\ell$')
        #ax.set_ylabel(r'$\ell^2 C_\ell$')
        ax.set_ylabel(r'$C_\ell$')
        #
        if save == True:
            print "saving plot to " + path
            fig.savefig(path, bbox_inches='tight')
#          fig.clf()
        else:
            plt.show()

    return lCen, Cl, sCl
コード例 #47
0
ファイル: template.py プロジェクト: hoangducthuong/pysm
def read_map(path, nside, unit=None, field=0, map_dist=None, dataurl=None):
    """Wrapper of `healpy.read_map` for PySM data. This function also extracts
    the units from the fits HDU and applies them to the data array to form an
    `astropy.units.Quantity` object.
    This function requires that the fits file contains a TUNIT key for each
    populated field.

    Parameters
    ----------
    path : object `pathlib.Path`, or str
        Path of HEALPix map to be read.
    nside : int
        Resolution at which to return map. Map is read in at whatever resolution
        it is stored, and `healpy.ud_grade` is applied.

    Returns
    -------
    map : ndarray
        Numpy array containing HEALPix map in RING ordering.
    """
    mpi_comm = None if map_dist is None else map_dist.mpi_comm
    pixel_indices = None if map_dist is None else map_dist.pixel_indices
    if dataurl is None:
        dataurl = DATAURL
    # read map. Add `str()` operator in case dealing with `Path` object.
    if os.path.exists(
            str(path)):  # Python 3.5 requires turning a Path object to str
        filename = str(path)
    else:
        with data.conf.set_temp("dataurl", dataurl), data.conf.set_temp(
                "remote_timeout", 30):
            filename = data.get_pkg_data_filename(path)
    # inmap = hp.read_map(filename, field=field, verbose=False)
    if (mpi_comm is not None and mpi_comm.rank == 0) or (mpi_comm is None):
        output_map = hp.read_map(filename,
                                 field=field,
                                 verbose=False,
                                 dtype=None)
        dtype = output_map.dtype
        # numba only supports little endian
        if dtype.byteorder == ">":
            dtype = dtype.newbyteorder()
        # mpi4py has issues if the dtype is a string like ">f4"
        if dtype == np.dtype(np.float32):
            dtype = np.dtype(np.float32)
        elif dtype == np.dtype(np.float64):
            dtype = np.dtype(np.float64)
        nside_in = hp.get_nside(output_map)
        if nside < nside_in:  # do downgrading in double precision
            output_map = hp.ud_grade(output_map.astype(np.float64),
                                     nside_out=nside)
        else:
            output_map = hp.ud_grade(output_map, nside_out=nside)
        output_map = output_map.astype(dtype, copy=False)
        if unit is None:
            unit = extract_hdu_unit(filename)
    elif mpi_comm is not None and mpi_comm.rank > 0:
        npix = hp.nside2npix(nside)
        try:
            ncomp = len(field)
        except TypeError:  # field is int
            ncomp = 1
        shape = npix if ncomp == 1 else (len(field), npix)
        unit = ""
        dtype = None

    if mpi_comm is not None:
        dtype = mpi_comm.bcast(dtype, root=0)
        if mpi_comm.rank > 0:
            output_map = np.empty(shape, dtype=dtype)
        mpi_comm.Bcast(output_map, root=0)
        unit = mpi_comm.bcast(unit, root=0)

    if pixel_indices is not None:
        # make copies so that Python can release the full array
        try:  # multiple components
            output_map = np.array(
                [each[pixel_indices].copy() for each in output_map])
        except IndexError:  # single component
            output_map = output_map[pixel_indices].copy()

    return u.Quantity(output_map, unit, copy=False)
コード例 #48
0
    def simulate_high_galactic_latitude_CO(self):
        """
        Coadd High Galactic Latitude CO emission, simulated with  MCMole3D.
        """
        if self.run_mcmole3d:
            import mcmole3d as cl

            # params to MCMole
            N = 40000
            L_0 = 20.4  # pc
            L_min = .3
            L_max = 60.
            R_ring = 5.8
            sigma_ring = 2.7  # kpc
            R_bulge = 3.
            R_z = 10  # kpc
            z_0 = 0.1
            Em_0 = 240.
            R_em = 6.6
            model = "LogSpiral"

            nside = self.nside
            Itot_o, _ = cl.integrate_intensity_map(
                self.planck_templatemap,
                hp.get_nside(self.planck_templatemap),
                planck_map=True,
            )
            Pop = cl.Cloud_Population(N, model, randseed=self.random_seed)

            Pop.set_parameters(
                radial_distr=[R_ring, sigma_ring, R_bulge],
                typical_size=L_0,
                size_range=[L_min, L_max],
                thickness_distr=[z_0, R_z],
                emissivity=[Em_0, R_em],
            )
            Pop()

            if self.verbose:
                Pop.print_parameters()
            # project into  Healpix maps
            mapclouds = cl.do_healpy_map(
                Pop,
                nside,
                highgalcut=np.deg2rad(90. -
                                      self.theta_high_galactic_latitude_deg),
                apodization="gaussian",
                verbose=self.verbose,
            )
            Itot_m, _ = cl.integrate_intensity_map(mapclouds, nside)
            # convert simulated map into the units of the Planck one
            rescaling_factor = Itot_m / Itot_o
            mapclouds /= rescaling_factor
            hglmask = np.zeros_like(mapclouds)
            # Apply mask to low galactic latitudes
            listhgl = hp.query_strip(
                nside,
                np.deg2rad(90. + self.theta_high_galactic_latitude_deg),
                np.deg2rad(90 - self.theta_high_galactic_latitude_deg),
            )
            hglmask[listhgl] = 1.
            rmsplanck = self.planck_templatemap[listhgl].std()
            rmssim = mapclouds[listhgl].std()
            if rmssim == 0.:
                belowplanck = 1.
            else:
                belowplanck = rmssim / rmsplanck

            return mapclouds * hglmask / belowplanck
        else:
            mapclouds = self.read_map(
                "co/mcmoleCO_HGL_{}.fits".format(self.template_nside),
                field=self.line_index,
            )

            return mapclouds
コード例 #49
0
def apply_so_mask(hpix_map, weight=True):
    nside = hp.get_nside(hpix_map)
    cut = np.logical_not(so_mask_fitting(nside))
    hits = np.sqrt(so_mask_hits(nside))
    masked = np.ma.masked_array(data=hpix_map, mask=cut, fill_value=hp.UNSEEN)
    return masked
コード例 #50
0
                        type=int,
                        default=None)
    parser.add_argument('--map_type',
                        help='type of mat to display, EFLUX, FLUX, TS or SIG',
                        required=False,
                        type=str,
                        default='EFLUX')

    args = parser.parse_args()
    # Read map

    hpx_ul = hp.read_map(args.map)

    # Get nside

    nside = hp.get_nside(hpx_ul)
    print nside

    # Get some meaningful limits for the color bar among the points which are larger than 0 and finite

    idx = (hpx_ul > 0) & np.isfinite(hpx_ul)
    if np.sum(idx) == 0: idx = (hpx_ul >= 0) & np.isfinite(hpx_ul)

    # Use the provided percentiles
    if args.min_percentile != 0:

        mmin = np.percentile(hpx_ul[idx], args.min_percentile)

    else:

        mmin = hpx_ul[idx].min()
コード例 #51
0
ファイル: plotMercator.py プロジェクト: jasonfan1997/HAWCCODE
def main():
    p = argparse.ArgumentParser(
        description="Plot map with Mercator projection")
    p.add_argument("fitsfile",
                   nargs="*",
                   help="Input HEALPix FITS file. "
                   "To plot the weighted sum of multiple files, put a list of "
                   "file weight file weight pairs.")
    p.add_argument("-a",
                   "--abthresh",
                   default=None,
                   type=float,
                   help="Apply an absolute (lower/upper) threshold to the map")
    p.add_argument("-c",
                   "--coords",
                   default="C",
                   help="C=equatorial (default), G=galactic")
    p.add_argument("-C",
                   "--column",
                   dest="col",
                   default=0,
                   type=int,
                   help="FITS column in which healpix map resides")
    p.add_argument(
        "--file-diff",
        dest="diff",
        default="",
        type=str,
        help=
        "FITS File to take the difference from (optional) otherwise uses positional argument"
    )
    p.add_argument("-D",
                   "--coldiff",
                   default=-1,
                   type=int,
                   help="FITS column in which healpix map to be subtracted "
                   "(if any) resides")
    p.add_argument(
        "--mjd",
        default=None,
        type=float,
        help=
        "MJD of the map. Will be plotted in J2000. Supersedes info in header")
    p.add_argument("-l",
                   "--threshold",
                   type=float,
                   default=None,
                   help="Apply a lower threshold to the plot.")
    p.add_argument(
        "--norm-sqdeg",
        dest="sqdeg",
        action="store_true",
        default=False,
        help="Normalize values to square degree, according to nSides"
        " (makes sense only certain kinds of maps)")
    p.add_argument("--milagro",
                   action="store_true",
                   help="Use Milagro color scale")
    p.add_argument(
        "--contours",
        dest="contours",
        type=float,
        nargs='*',
        default=None,
        help="plot contours. If --contourmap is provided, it will be"
        " used instead of plotted map. If no contours are"
        " provided, it will plot confidence intervals"
        " (1, 2, 3 sigma CI). Else, absolute sigmas are used,"
        " e.g. --contours 1 2 3 4 [sigma] --contours")
    p.add_argument("--contourscolor",
                   nargs='*',
                   default=None,
                   help="Draw options for contours")
    p.add_argument("--contoursstyle",
                   nargs='*',
                   default=None,
                   help="Draw options for contours")
    p.add_argument("--contourswidth",
                   type=float,
                   nargs='*',
                   default=None,
                   help="Draw options for contours")
    p.add_argument("--contourfile",
                   nargs='*',
                   default=None,
                   help="Draw contours from file with RA/Dec pairs")
    p.add_argument("--gamma",
                   action="store_true",
                   help="Use GeV/TeV gamma-ray color scale")
    p.add_argument("-L", "--label", default=None, help="Color bar label")
    p.add_argument("--cross",
                   action="store_true",
                   help="Plot a cross at the center of the plot")
    p.add_argument("--moon",
                   action="store_true",
                   dest="isMoon",
                   help="is in moon centered coordinates")
    p.add_argument("-m",
                   "--min",
                   type=float,
                   default=None,
                   help="Plot minimum value")
    p.add_argument("-M",
                   "--max",
                   type=float,
                   default=None,
                   help="Plot maximum value")
    p.add_argument("-n",
                   "--ncolors",
                   type=int,
                   default=256,
                   help="Number of contours to use in the colormap")
    p.add_argument("--nocolorbar",
                   dest="colorbar",
                   action="store_false",
                   help="Do not draw color bar.")
    p.add_argument("--nofill",
                   action="store_true",
                   help="Do not draw fill plot with colors. "
                   "Useful for contours.")
    p.add_argument(
        "-o",
        "--output",
        default=None,
        help="Output file name (optional). "
        "If file name ends in .fits or .fit, save as a healpix map with the pixels outside the plot area set to hp.UNSEEN."
    )
    p.add_argument("-s",
                   "--scale",
                   type=float,
                   default=1.,
                   help="scale up or down values in map")
    p.add_argument("--sun",
                   action="store_true",
                   dest="isSun",
                   help="is in Sun centered coordinates")
    p.add_argument("--dpar",
                   type=float,
                   default=1.,
                   help="Interval in degrees between parallels")
    p.add_argument("--dmer",
                   type=float,
                   default=1.,
                   help="Interval in degrees between meridians.")
    p.add_argument('--sexagesimal',
                   action='store_true',
                   help='Display RA in hh:mm.')
    p.add_argument("--nogrid",
                   action="store_true",
                   help="Do not plot gridlines.")
    p.add_argument("--interpolation",
                   action='store_true',
                   help="Uses bilinear interpolation in data map.")
    p.add_argument("--xsize",
                   type=int,
                   default=1000,
                   help="Number of X pixels, Y will be scaled acordingly.")
    p.add_argument("-t",
                   "--ticks",
                   nargs="+",
                   type=float,
                   help="List of ticks for color bar, space-separated")
    p.add_argument("-T", "--title", help="title of map")
    p.add_argument("--squareaspect",
                   action='store_true',
                   help="Better Mercator X/Y ratio.")
    p.add_argument("--dpi",
                   type=int,
                   default=300,
                   help="Change the dpi of the output")
    p.add_argument("--preliminary",
                   action="store_true",
                   help="Add a watermark indicating the plot as preliminary")
    p.add_argument("--onlystats",
                   action="store_true",
                   help="Exits after returning min, max and value at center. "
                   "If map is called 'flux', it will give the error if there "
                   "is a map called 'flux_error'")

    # Mutually exclusive option to specify plot xy range OR center + WxH
    argRange = p.add_mutually_exclusive_group(required=False)
    argRange.add_argument("--xyrange",
                          type=float,
                          nargs=4,
                          help="Xmin Xmax Ymin Ymax plot range [degree]")
    argRange.add_argument(
        "--origin",
        type=float,
        nargs=4,
        help="Central (x,y) coords + width + height [degree]")

    # Plot P-Value instead of sigma, expects location and scale of
    # a right-skewed Gumble distribution
    p.add_argument("--gumbel",
                   type=float,
                   nargs=2,
                   help="Plot P-Value instead of significance. "
                   "Arguments: location and scale of right-skewed "
                   "Gumble distribution.")

    # Download/plotting options for Fermi catalog sources
    p.add_argument(
        "--download-fermi",
        dest="dfermi",
        default=None,
        help="Download Fermi FITS data from NASA and exit. Enter version number"
    )
    p.add_argument("--fermicat",
                   default=None,
                   help="Fermi xFGL catalog FITS file name")
    p.add_argument("--fermicat-labels",
                   dest="fermicatLabels",
                   action="store_true",
                   help="Draw Fermi sources with labels.")

    # plotting options for Fermi Healpix file
    p.add_argument("--contourmap",
                   default=None,
                   help="Healpix map from which to grab contours.")
    p.add_argument(
        "--contourmapcoord",
        default="G",
        help=
        "Coordinate system of contourmap. C=equatorial, G=galactic (default)")

    # Download/plotting options for TeVCat sources
    p.add_argument("--download-tevcat",
                   dest="dtevcat",
                   action="store_true",
                   help="Download data from tevcat.uchicago.edu and exit")
    p.add_argument("--tevcat",
                   default=None,
                   help="Draw TeVCat sources using TeVCat ASCII file")
    p.add_argument("--tevcat-labels",
                   dest="tevcatLabels",
                   action="store_true",
                   help="Draw TeVCat sources with labels.")
    p.add_argument("--cat-labels-angle",
                   dest="catLabelsAngle",
                   default=90,
                   help="Oriantation of catalog labels.")
    p.add_argument("--cat-labels-size",
                   dest="catLabelsSize",
                   default=8,
                   help="Size of catalog labels.")

    # Highlight hotspots listed in an ASCII file
    p.add_argument("--hotspots",
                   default=None,
                   help="Hotspot coordinates in an ASCII file")
    p.add_argument("--hotspot-labels",
                   dest="hotspotLabels",
                   action="store_true",
                   help="Draw hotspots sources with labels.")
    # Plot 2D Gaussian fit result listed in an ASCII file from fitMap.py
    p.add_argument("--gaussfit",
                   default=None,
                   help="2D-Gaussian fit result in an ASCII file")

    args = p.parse_args()

    #Sanity checks
    if (args.mjd is not None) and (not canPrecess):
        print("Missing necessary packages to make precession")
        raise SystemExit

    if args.nofill:
        # If we don't fill with colors, we don't plot the colorbar either
        args.colorbar = False

    # Download TeVCat
    if args.dtevcat:
        if haveTeVCat:
            print("Fetching data from tevcat.uchicago.edu.")
            tevcat = TeVCat.TeVCat()
            return None
        else:
            print("Sorry, AERIE TeVCat python module is not available.")
            raise SystemExit

    # Downlaod Fermi catalog
    if args.dfermi:
        if haveFermi:
            print("Fetching 2FGL catalog, version %s" % args.dfermi)
            FGLCatalog.fetch_catalog(args.dfermi)
            return None
        else:
            print("Sorry, AERIE Fermi python module is not available.")
            raise SystemExit

    # Start normal processing
    fitsfile = args.fitsfile
    if fitsfile == []:
        print("Please specify an FITS file to plot.")
        raise SystemExit

    # Fill 2D array
    xmin = -180.
    xmax = 180.
    ymax = 90
    ymin = -90

    if (args.xyrange):
        xmin, xmax, ymin, ymax = args.xyrange
        xC = (xmin + xmax) / 2.
        yC = (ymin + ymax) / 2.
    elif (args.origin):
        xC, yC, w, h = args.origin
        xmin = xC - 0.5 * w
        xmax = xC + 0.5 * w
        ymin = yC - 0.5 * h
        ymax = yC + 0.5 * h
    else:
        print("Using default zoom window. "
              "To customize the zoom window you must specify either "
              "(xyrange) or (origin and width and height).")

    if args.isMoon or args.isSun:
        xmin += 180.
        xmax += 180.

    # Move to range expected by healpy
    while xmin < -180:
        xmin += 360
    while xmin > 180:
        xmin -= 360
    while xmax < -180:
        xmax += 360
    while xmax > 180:
        xmax -= 360

    if xmax < xmin:
        tmax = xmax
        tmin = xmin
        xmin = tmax
        xmax = tmin

    cxmin = xmin
    cxmax = xmax
    frot = 0.
    if xmax > 90. and xmin < -90.:
        frot = 180.
        cxmin = xmax - 180.
        cxmax = xmin + 180.

    if args.origin:
        while xC > 180:
            xC -= 360

    # Read in the skymap and mask out empty pixels
    skymap, skymapHeader = hp.read_map(fitsfile[0], args.col, h=True)
    if len(fitsfile) > 1:
        skymap *= float(fitsfile[1])
    # If fitsfile has more parameters, they should be "mapfile weight" pairs
    for i in range(2, len(fitsfile), 2):
        skymap2 = hp.read_map(fitsfile[i], args.col)
        skymap2 *= float(fitsfile[i + 1])
        skymap += skymap2
    # remove naughty values
    skymap[np.isnan(skymap)] = hp.UNSEEN
    skymap *= args.scale
    nside1 = hp.get_nside(skymap)
    npix = hp.nside2npix(nside1)

    if args.coldiff > -1:
        if os.path.isfile(args.diff):
            print("Taking difference with {0}".format(args.diff))
            skymap2 = hp.read_map(args.diff, args.coldiff)
        else:
            print("No extra file provided, using same file as input")
            skymap2 = hp.read_map(fitsfile[0], args.coldiff)
            if len(fitsfile) > 1:
                skymap2 *= float(fitsfile[1])

        print("Subtracting column {0} from skymap...".format(args.coldiff))
        skymap -= skymap2
        # If fitsfile has more parameters, they should be "mapfile weight" pairs
        for i in range(2, len(fitsfile), 2):
            skymap2 = hp.read_map(fitsfile[i], args.coldiff)
            skymap2 *= float(fitsfile[i + 1])
            skymap -= skymap2

    if (args.gumbel):
        assert haveGumbel
        gumbel_location, gumbel_scale = args.gumbel
        gumbel = gumbel_r(loc=gumbel_location, scale=gumbel_scale)
        skymap = gumbel.logsf(skymap) / log(10)

        def inf_suppressor(x):
            return x if np.isfinite(x) else 0.

        inf_suppressor = np.vectorize(inf_suppressor)
        skymap = inf_suppressor(skymap)

    # Normalize value to square degree
    if args.sqdeg:
        pixsizestr = 4 * np.pi / (12 * nside1**2)
        str2sqdeg = (180 / np.pi)**2
        pixsizesqdeg = pixsizestr * str2sqdeg
        skymap /= pixsizesqdeg

    # I did not find header handler, thats all I came up with...
    toFind = 'TTYPE' + str(args.col + 1)
    #print type(skymapHeader), type(skymapHeader[0]), skymapHeader, toFind, dict(skymapHeader)[toFind]
    skymapName = dict(skymapHeader)[toFind]

    #Check if it is flux
    isFlux = False
    hasFluxError = False
    if skymapName == 'flux':
        isFlux = True

        keyname = dict((v, n) for n, v in skymapHeader).get("flux_error")

        if keyname is not None:
            if keyname.find('TTYPE') == 0 and len(keyname) == 6:
                hasFluxError = True
                fluxerrmap = hp.read_map(fitsfile[0], int(keyname[5]) - 1)

        if not hasFluxError:
            print("Map called 'flux_error' not present, will not print errors")

    isFluxError = False
    if skymapName == "flux_error":
        isFluxError = True

    # Find FOV
    pxls = np.arange(skymap.size)
    nZeroPix = pxls[(skymap != 0)]
    pxTh, pxPh = hp.pix2ang(nside1, pxls)

    # Mask outside FOV
    values = np.ma.masked_where((pxTh > pxTh[nZeroPix].max())
                                | (pxTh < pxTh[nZeroPix].min())
                                | (skymap == hp.UNSEEN)
                                | (skymap == 1e20), skymap)

    # Plot the skymap as an image, setting up the color palette on the way
    mpl.rc("font", size=14, family="serif")
    faspect = abs(cxmax - cxmin) / abs(ymax - ymin)
    fysize = 4
    # Set up the figure frame and coordinate rotation angle
    coords = ["C", "C"]
    gratcoord = "C"
    if args.coords == "G":
        coords = ["C", "G"]
        gratcoord = "G"

    if args.mjd is not None:
        rotMap = precess.mjd2J2000ang(mjd=args.mjd, coord=coords, rot=frot)
        rotVertex = precess.mjd2J2000ang(mjd=args.mjd, coord=coords)
    elif not (args.isMoon or args.isSun):
        mjd = False
        if havePyfits:
            hdulist = pf.open(fitsfile[0])
            header = hdulist[0].header
            if 'EPOCH' in header:
                epoch = header['EPOCH']
                if epoch == 'J2000':
                    pass
                elif epoch == 'current':
                    if 'STARTMJD' in header and 'STOPMJD' in header:
                        startmjd = header['STARTMJD']
                        stopmjd = header['STOPMJD']
                        if (startmjd > 0
                                and stopmjd > 0) or startmjd > stopmjd:
                            mjd = (stopmjd + startmjd) / 2
                        else:
                            print(
                                "STARTMJD/STOPMJD are not well-definned, will not attempt to precess!"
                            )
                    else:
                        print(
                            "STARTMJD or STOPMJD not present in header, will not attempt to precess!"
                        )
                else:
                    print(
                        "Currently EPOCH can be J2000 or current. Will not attempt to precess"
                    )
            else:
                print("Key EPOCH not in header, will not attempt to precess!")

        else:
            print(
                "Pyfits not available -> Can't check map epoch -> Will not attempt to precess!"
            )

        if mjd:
            print(
                "Current epoch detected in header, will precess from MJD%g to J2000"
                % mjd)
            rotMap = precess.mjd2J2000ang(mjd=mjd, coord=coords, rot=frot)
            rotVertex = precess.mjd2J2000ang(mjd=mjd, coord=coords)
        else:
            rotMap = frot
            rotVertex = 0
    else:
        rotMap = frot
        rotVertex = 0

    # Get extrema
    angverts = [[xmin,ymin],[xmax,ymin],\
                [xmax,ymax],[xmin,ymax]]
    vertlist = []
    cRot = hp.Rotator(coord=coords, rot=rotVertex)
    for x, y in angverts:
        ctht, cph = np.deg2rad((y, x))
        ctht = 0.5 * np.pi - ctht
        if cph < 0:
            cph = 2. * np.pi + cph
        vertlist.append(cRot.I(hp.ang2vec(ctht, cph)))

    # Get pixels in image
    imgpix = hp.query_polygon(nside1, vertlist, inclusive=True)

    seenpix = imgpix[values[imgpix] > hp.UNSEEN]

    #if output is fits file: clip input healpy map by setting all pixels outside the plotted area to UNSEEN,
    #then save the result.
    if args.output and (args.output.endswith(".fits")
                        or args.output.endswith(".fit")):
        pix = np.ones(npix, dtype=bool)
        pix[seenpix] = False
        values[pix] = hp.UNSEEN
        print("Saving clipped healpy map to %s" % args.output)
        hp.write_map(args.output,
                     values,
                     partial=True,
                     column_names=[skymapName])
        exit()

    #Get stats
    precRot = hp.Rotator(coord=coords, rot=rotVertex)

    pixMin, pixMax = seenpix[[
        np.argmin(values[seenpix]),
        np.argmax(values[seenpix])
    ]]
    dMin, dMax = values[[pixMin, pixMax]]
    [[thMin, thMax],
     [phMin, phMax]] = np.rad2deg(precRot(hp.pix2ang(nside1,
                                                     [pixMin, pixMax])))

    if args.coords == 'C':
        while phMin < 0:
            phMin += 360

        while phMax < 0:
            phMax += 360

    th0, ph0 = precRot.I((90. - yC) * degree, xC * degree)
    pix0 = hp.ang2pix(nside1, th0, ph0)

    if isFlux:
        if hasFluxError:
            fluxerrMin, fluxerrMax, fluxerr0 = fluxerrmap[[
                pixMin, pixMax, pix0
            ]]
        else:
            fluxerrMin, fluxerrMax, fluxerr0 = [0, 0, 0]

        print("Flux units: TeV^-1 cm^-2 s^-1")
        print("Coord units: deg")
        print("Min:                 %11.2e +/- %11.2e (%6.2f, %5.2f)" %
              (dMin, fluxerrMin, phMin, 90 - thMin))
        print("Max:                 %11.2e +/- %11.2e (%6.2f, %5.2f)" %
              (dMax, fluxerrMax, phMax, 90 - thMax))
        print("Map value at origin: %11.2e +/- %11.2e" %
              (skymap[pix0], fluxerr0))

    elif isFluxError:
        print("Min:                 %5.4e (%6.2f, %5.2f)" %
              (dMin, phMin, 90 - thMin))
        print("Max:                 %5.4e (%6.2f, %5.2f)" %
              (dMax, phMax, 90 - thMax))
        print("Map value at origin: %5.4e" % skymap[pix0])
    else:
        print("Min:                 %5.2f (%6.2f, %5.2f)" %
              (dMin, phMin, 90 - thMin))
        print("Max:                 %5.2f (%6.2f, %5.2f)" %
              (dMax, phMax, 90 - thMax))
        print("Map value at origin: %5.2f" % skymap[pix0])

    if args.onlystats:
        return 0

    figsize = (fysize * faspect + 2, fysize + 2.75)
    fig = plt.figure(num=1, figsize=figsize)

    # Set  min/max value of map
    if args.min is not None:
        dMin = args.min
        values[(skymap < dMin) & (values > hp.UNSEEN)] = dMin
    if args.max is not None:
        dMax = args.max
        values[(skymap > dMax) & (values > hp.UNSEEN)] = dMax

    textcolor, colormap = MapPalette.setupDefaultColormap(args.ncolors)

    # Use the Fermi/HESS/VERITAS purply-red-yellow color map
    if args.gamma:
        textcolor, colormap = MapPalette.setupGammaColormap(args.ncolors)

    # Use the Milagro color map
    if args.milagro:
        dMin = -5
        dMax = 15
        dMin = args.min if args.min != None else -5
        dMax = args.max if args.max != None else 15
        thresh = args.threshold if args.threshold != None else 2.
        textcolor, colormap = \
            MapPalette.setupMilagroColormap(dMin, dMax, thresh, args.ncolors)
        print("Milagro", dMin, dMax, thresh)
    # Use a thresholded grayscale map with colors for extreme values
    else:
        if args.threshold != None:
            textcolor, colormap = \
                MapPalette.setupThresholdColormap(dMin, dMax, args.threshold,
                                                args.ncolors)
        elif args.abthresh != None:
            textcolor, colormap = \
                MapPalette.setupAbsThresholdColormap(dMin, dMax, args.abthresh,
                                                    args.ncolors)

    if args.interpolation:
        cRot = hp.Rotator(rot=rotMap, coord=coords)
        phi = np.linspace(np.deg2rad(xmax), np.deg2rad(xmin), args.xsize)
        if xmin < 0 and xmax > 0 and (xmax - xmin) > 180.:
            phi = np.linspace(
                np.deg2rad(xmin) + 2. * np.pi, np.deg2rad(xmax), args.xsize)
            phi[(phi > 2. * np.pi)] -= 2. * np.pi
        theta = 0.5 * np.pi - np.linspace(np.deg2rad(ymin), np.deg2rad(ymax),
                                          args.xsize / faspect)
        Phi, Theta = np.meshgrid(phi, theta)
        rTheta,rPhi = cRot.I(Theta.reshape(phi.size*theta.size),\
                           Phi.reshape(phi.size*theta.size))
        rotimg = hp.get_interp_val(values, rTheta.reshape(Phi.shape),\
                                           rPhi.reshape(Theta.shape))
    else:
        tfig = plt.figure(num=2, figsize=figsize)
        rotimg = hp.cartview(values, fig=2,coord=coords,title="",\
                            cmap=colormap, cbar=False,\
                            lonra=[cxmin,cxmax],latra=[ymin,ymax],rot=rotMap,
                            notext=True, xsize=args.xsize,
                            return_projected_map=True)
        plt.close(tfig)

    ax = fig.add_subplot(111)
    ax.set_aspect(1.)

    # if plotting contours
    if args.contours != None:
        if args.contourmap:
            contourmap = args.contourmap
            contourmapcoord = args.contourmapcoord
        else:
            contourmap = fitsfile[0]
            contourmapcoord = 'C'

        contourSkyMap, contourSkyMapHeader = hp.read_map(contourmap, h=True)
        fnside1 = hp.get_nside(contourSkyMap)
        ftoFind = 'TTYPE' + str(1)
        contourSkyMapName = dict(contourSkyMapHeader)[ftoFind]
        #<<<<<<< .mine
        #        fvalues = contourSkyMap #np.ma.masked_where(contourSkyMap == 0, contourSkyMap)
        #=======
        #>>>>>>> .r38901
        if args.interpolation:
            cRot = hp.Rotator(rot=rotMap, coord=[contourmapcoord, coords[-1]])
            phi = np.linspace(np.deg2rad(xmax), np.deg2rad(xmin), args.xsize)
            if xmin < 0 and xmax > 0 and (xmax - xmin) > 180.:
                phi = np.linspace(
                    np.deg2rad(xmin) + 2. * np.pi, np.deg2rad(xmax),
                    args.xsize)
                phi[(phi > 2. * np.pi)] -= 2. * np.pi
            theta = 0.5 * np.pi - np.linspace(
                np.deg2rad(ymin), np.deg2rad(ymax), args.xsize / faspect)
            Phi, Theta = np.meshgrid(phi, theta)
            rTheta,rPhi = cRot.I(Theta.reshape(phi.size*theta.size),\
                                Phi.reshape(phi.size*theta.size))
            frotimg = hp.get_interp_val(contourSkyMap,rTheta.reshape(Phi.shape),\
                                        rPhi.reshape(Theta.shape))
        else:
            tfig = plt.figure(num=3, figsize=figsize)
            frotimg = hp.cartview(contourSkyMap,
                                  fig=3,
                                  coord=[contourmapcoord, coords[-1]],
                                  title="",
                                  cmap=colormap,
                                  cbar=False,
                                  lonra=[xmin, xmax],
                                  latra=[ymin, ymax],
                                  rot=rotMap,
                                  notext=True,
                                  xsize=1000,
                                  min=dMin,
                                  max=dMax,
                                  return_projected_map=True)
            plt.close(tfig)

        if args.contours == []:
            rMaxSig2 = (contourSkyMap[imgpix].max())**2
            if rMaxSig2 < 11.83:
                print(
                    "No spot detected with at least a 3sigma confidence contour"
                )
                contours = [-float("inf")]
            else:
                contours = [
                    sqrt(rMaxSig2 - 2.30),
                    sqrt(rMaxSig2 - 6.18),
                    sqrt(rMaxSig2 - 11.83)
                ]
        else:
            contours = args.contours

        ccolor = args.contourscolor or 'g'
        cstyle = args.contoursstyle or '-'
        cwidth = args.contourswidth or 2.
        print('Contours style: ', ccolor, cstyle, cwidth)

        contp = ax.contour(frotimg,
                           levels=np.sort(contours),
                           colors=ccolor,
                           linestyles=cstyle,
                           linewidths=cwidth,
                           origin='upper',
                           extent=[cxmax, cxmin, ymax, ymin])

    rotimg[(rotimg > dMax)] = dMax
    rotimg[(rotimg < dMin)] = dMin
    if not args.nofill:
        imgp = ax.imshow(rotimg,extent=[cxmax, cxmin, ymax, ymin],\
                         vmin=dMin,vmax=dMax,cmap=colormap)

        imgp.get_cmap().set_under('w', alpha=0.)
        imgp.get_cmap().set_over('w', alpha=0.)

    #User defined contour
    if args.contourfile is not None:

        ccolor = args.contourscolor or ['g']
        cstyle = args.contoursstyle or ['-']
        cwidth = args.contourswidth or [2.]

        if len(ccolor) == 1:
            ccolor = np.repeat(ccolor[0], len(args.contourfile))

        if len(cstyle) == 1:
            cstyle = np.repeat(cstyle[0], len(args.contourfile))

        if len(cwidth) == 1:
            cwidth = np.repeat(cwidth[0], len(args.contourfile))

        for i, f in enumerate(args.contourfile):

            ra, dec = np.loadtxt(f, unpack=True)

            if xmax > 90. and xmin < -90.:
                #Map at the discontinuity boundary
                ra -= 180

            ra[ra > 180] -= 360
            ra[ra < -180] += 360

            ax.plot(ra,
                    dec,
                    color=ccolor[i],
                    linestyle=cstyle[i],
                    linewidth=cwidth[i])

    # Draw grid lines
    xts = np.arange(np.floor(xmin), np.ceil(xmax + args.dmer), args.dmer)[1:-1]
    xtlbs = [
        '%g' % (xt + 360) if args.coords == 'C' and xt < 0 else '%g' % xt
        for xt in xts
    ]
    if xmin < 0. and xmax > 0. and (xmax - xmin) > 180.:
        xts = np.arange(np.floor(cxmin), np.ceil(cxmax + args.dmer),
                        args.dmer)[1:-1]
        xtlbs = []
        for xt in xts:
            cval = xt - 180.
            if xt < 0:
                cval = 180. + xt
            if cval == -180:
                cval = 180
            if args.isMoon or args.isSun:
                cval -= 180.
                if cval < -180.:
                    cval += 360.
            elif args.coords == 'C' and cval < 0:
                cval += 360

            xtlbs.append('%g' % (cval))
    yts = np.arange(np.floor(ymin), np.ceil(ymax + args.dpar), args.dpar)[1:-1]

    if args.nogrid == False:
        ax.grid(color=textcolor)
        ax.xaxis.set_ticks(xts)
        ax.xaxis.set_ticklabels(xtlbs)
        ax.yaxis.set_ticks(yts)

    if args.preliminary:
        plt.text((xmin + xmax) / 2.,
                 ymin + 0.85 * (ymax - ymin),
                 "PRELIMINARY",
                 color=textcolor,
                 alpha=0.8,
                 fontdict={
                     "family": "sans-serif",
                     "weight": "bold",
                     "size": 28
                 },
                 horizontalalignment='center')

    # If TeVCat data are available, plot them
    if args.tevcat or args.tevcatLabels:
        if haveTeVCat:
            try:
                if args.tevcat:
                    tevcat = TeVCat.TeVCat(args.tevcat)
                elif args.tevcatLabels:
                    tevcat = TeVCat.TeVCat(args.tevcatLabels)
            except IOError as e:
                print(e)
                print("Downloading data from tevcat.uchicago.edu")
                tevcat = TeVCat.TeVCat()
            except:
                print("Why caught here?")
                print("Downloading data from tevcat.uchicago.edu")
                tevcat = TeVCat.TeVCat()

            cRot = hp.Rotator(coord=["C", coords[-1]])
            tnside = 512
            fpix = np.zeros(hp.nside2npix(tnside))
            for cId in (1, 2):
                catalog = tevcat.GetCatalog(cId)
                ra = catalog.GetRA()
                dec = catalog.GetDec()
                assoc = catalog.GetCanonicalName()
                cut = (assoc != 'Crab Pulsar')
                ra = ra[cut]
                dec = dec[cut]
                assoc = assoc[cut]
                cpix = hp.ang2pix(tnside, np.pi * 0.5 - dec, ra)
                slpx = []
                for sx, px in enumerate(cpix):
                    fpix[px] += 1
                    if fpix[px] != 1:
                        print("%s is a duplicate" % (assoc[sx]))
                        slpx.append(sx)

                ra = np.delete(ra, slpx)
                dec = np.delete(dec, slpx)
                assoc = np.delete(assoc, slpx)
                y, x = cRot(np.pi * 0.5 - dec, ra)
                x = np.rad2deg(x) + frot
                x[(x > 180.)] -= 360.
                y = 90. - np.rad2deg(y)
                cut = (x > xmin) & (x < xmax) & (y > ymin) & (y < ymax)
                x = x[cut]
                y = y[cut]
                assoc = assoc[cut]
                ax.scatter(x,
                           y,
                           color=textcolor,
                           facecolors="none",
                           marker="s")

                if args.tevcatLabels:
                    for r, d, s in zip(x, y, assoc):
                        print(r, d, s)
                        ax.text(r,
                                d,
                                s + '   .',
                                color=textcolor,
                                rotation=args.catLabelsAngle,
                                fontdict={
                                    'family': 'sans-serif',
                                    'size': args.catLabelsSize,
                                    'weight': 'bold'
                                })
        else:
            print("Sorry, TeVCat could not be loaded.")

    # If Fermi data are available, plot them
    if args.fermicat:
        if haveFermi:
            fcat = None
            try:
                fcat = FGLCatalog.FGLCatalog(args.fermicat)
                aflag = fcat.GetAnalysisFlags()
                acut = (aflag == 0)  # cut analysis errors

                flux = fcat.GetFlux1000()  # 1-100 GeV flux
                dflx = fcat.GetFlux1000Error()  # flux uncertainty
                fcut = dflx / flux < 0.5  # cut poorly measured srcs

                cuts = np.logical_and(acut, fcut)
                print('Using FGL')
            except:
                try:
                    fcat = FHLCatalog.FHLCatalog(args.fermicat)
                    cuts = (fcat.GetFlux() > 0.)  # Dummy cut
                    print('Using FHL')
                except:
                    print('Fermi catalog could not be loaded!')
        if fcat != None:
            # Don't show TeV associations if plotting from TeVCat
            if args.tevcat or args.tevcatLabels:
                tcfg = fcat.GetTeVCatFlag()
                tcut = (tcfg == "N") | (tcfg == "C")
                cuts = np.logical_and(cuts, tcut)
            ra = fcat.GetRA()[cuts]
            dec = fcat.GetDec()[cuts]
            assoc = fcat.GetSourceName()[cuts]
            catnms = fcat.GetCatalogName()[cuts]
            for i in xrange(len(assoc)):
                if assoc[i] == '':
                    assoc[i] = catnms[i]

            cRot = hp.Rotator(coord=["C", coords[-1]])
            y, x = cRot(np.pi * 0.5 - dec, ra)
            x = np.rad2deg(x) + frot
            x[(x > 180.)] -= 360.
            y = 90. - np.rad2deg(y)
            cut = (x > xmin) & (x < xmax) & (y > ymin) & (y < ymax)
            x = x[cut]
            y = y[cut]
            assoc = assoc[cut]
            ax.scatter(x, y, color=textcolor, facecolors="none", marker="o")

            if args.fermicatLabels:
                for r, d, s in zip(x, y, assoc):
                    ax.text(r,
                            d,
                            s,
                            color=textcolor,
                            rotation=args.catLabelsAngle,
                            fontdict={
                                'family': 'sans-serif',
                                'size': args.catLabelsSize,
                                'weight': 'bold'
                            })
        else:
            print("Sorry, the Fermi xFGL catalog could not be loaded.")

    # If a hotspot list is given, plot it
    if args.hotspots:
        fhot = open(args.hotspots, "r")
        ra = []
        dec = []
        assoc = []
        for line in fhot:
            if line.startswith("#"):
                continue
            larr = line.strip().split()
            ra.append(float(larr[1]))
            dec.append(float(larr[2]))
            assoc.append(larr[4])

        ra = np.deg2rad(ra)
        dec = np.deg2rad(dec)
        assoc = np.array(assoc)
        cRot = hp.Rotator(coord=["C", coords[-1]])
        y, x = cRot(np.pi * 0.5 - dec, ra)
        x = np.rad2deg(x) + frot
        x[(x > 180.)] -= 360.
        y = 90. - np.rad2deg(y)
        cut = (x > xmin) & (x < xmax) & (y > ymin) & (y < ymax)
        x = x[cut]
        y = y[cut]
        assoc = assoc[cut]
        ax.scatter(x, y, color=textcolor, facecolors="none", marker="o")
        if args.hotspotLabels:
            for r, d, s in zip(x, y, assoc):
                print(r, d, s)
                ax.text(r,
                        d,
                        s + '   .',
                        color=textcolor,
                        rotation=90,
                        fontdict={
                            'family': 'sans-serif',
                            'size': 8,
                            'weight': 'bold'
                        })

    # If a gaussian fit file is given, plot it
    if args.gaussfit:
        gfit = open(args.gaussfit, "r")
        gfit.next()
        gfit.next()
        ra, raErr = [float(i) for i in gfit.next().strip().split()]
        dec, decErr = [float(i) for i in gfit.next().strip().split()]
        raW, raWErr = [float(i) for i in gfit.next().strip().split()]
        decW, decWErr = [float(i) for i in gfit.next().strip().split()]
        gfit.close()

        mrot = -180. if args.isMoon or args.isSun else 0.

        cRot = hp.Rotator(coord=["C", coords[-1]])
        y, x = cRot(np.pi * 0.5 - np.deg2rad(dec), np.deg2rad(ra + mrot))
        x = np.rad2deg(x) + frot
        x = x - 360. if x > 180. else x
        y = 90. - np.rad2deg(y)
        ellip0 = Ellipse((x, y),
                         width=2 * raW,
                         height=2 * decW,
                         edgecolor='black',
                         facecolor='None')
        ax.add_patch(ellip0)
        ax.scatter(x, y, s=20, color='black', facecolors="black", marker="o")

        print(x, y, xmin, xmax, ymin, ymax)
        ax.text(
            x - 1,
            y + 1,
            "%s=%.02f$\pm$%.02f\n$\Delta\delta$=%.02f$\pm$%.02f\n%s=%.02f$\pm$%.02f\n$\sigma_\delta$=%.02f$\pm$%.02f"
            % (r"$\Delta\alpha$", ra, raErr, dec, decErr, r"$\sigma_\alpha$",
               raW, raWErr, decW, decWErr),
            color=textcolor,
            rotation=0,
            fontdict={"size": 12})

    # Set up the color bar
    # Setup color tick marks
    if args.colorbar:
        cticks = args.ticks
        if args.ticks == None:
            if (dMax - dMin) > 3:
                clrmin = np.floor(dMin)
                clrmax = np.ceil(dMax)
                ctspc = np.round((clrmax - clrmin) / 10.)
                if ctspc == 0.:
                    ctspc = 1.
                if clrmin < 0 and clrmax > 0:
                    cticks = -np.arange(0, -clrmin, ctspc)
                    cticks = np.sort(
                        np.unique(
                            np.append(cticks, np.arange(0, clrmax, ctspc))))
                else:
                    cticks = np.arange(clrmin, clrmax + ctspc, ctspc)
            else:
                cticks = None

        cb = fig.colorbar(
            imgp,
            orientation="horizontal",
            shrink=0.85,
            fraction=0.1,
            #aspect=25,
            pad=0.1,
            ax=ax,
            ticks=cticks)

    if args.label:
        skymapName = args.label
    else:
        if re.match("significance", skymapName):
            skymapName = r"significance [$\sigma$]"
    if args.gumbel:
        skymapName = "log10(P-Value)"

    if args.colorbar:
        cb.set_label(skymapName)

    xUnit = " [$^\circ$]"
    if args.sexagesimal:
        xUnit = ""
    yUnit = " [$^\circ$]"

    xlabel = r"$\alpha$%s" % xUnit
    ylabel = r"$\delta$%s" % yUnit

    # Set up the color bar and tick axis
    if args.coords == "G":
        xlabel = r"$l$%s" % xUnit
        ylabel = r"$b$%s" % yUnit
    if args.isMoon or args.isSun:
        xlabel = r"$\Delta\alpha$%s" % xUnit
        ylabel = r"$\Delta\delta$%s" % yUnit

    # X axis label
    ax.set_xlabel(xlabel, color='k')
    # Y axis label
    ax.set_ylabel(ylabel, color='k')

    if args.sexagesimal:
        if haveAstropy:
            # Display RA with hh:mm nonsense.
            ticksLocationsRA = plt.xticks()[0]

            def degrees_to_hhmmss(ra):
                return Angle(ra, u.degree).to_string(unit=u.hour, fields=2)

            ticksLabelsRA = degrees_to_hhmmss(ticksLocationsRA)
            plt.xticks(ticksLocationsRA, ticksLabelsRA)
            # The same with DEC coordinates. Actually no, we never plot less than 1 degree...
            if False:
                ticksLocationsDEC = plt.yticks()[0]

                def degrees_to_ddmmss(dec):
                    return Angle(dec, u.degree).to_string(unit=u.degree,
                                                          fields=2)

                ticksLabelsDEC = degrees_to_ddmmss(ticksLocationsDEC)
                plt.yticks(ticksLocationsDEC, ticksLabelsDEC)
        else:
            print('Error: "--sexagesimal" ignored (needs astropy module)')

    # Title
    if args.title != None:
        ax.set_title(r"{0}".format(args.title.replace("\\n", "\n")), color='k')

    if args.cross:
        # Indicate the center of the plot with a thick black cross
        ax.scatter(xC,
                   yC,
                   s=20**2,
                   marker="+",
                   facecolor="#000000",
                   color="#000000")

    ax.set_ylim(ymin, ymax)
    ax.set_xlim(cxmax, cxmin)
    if args.squareaspect:
        plt.axes().set_aspect(1. / cos((ymax + ymin) / 2 * pi / 180))

    # Either output the image to a file and quit or plot it in a window
    if args.output:
        if not args.nofill:
            fig.savefig(args.output, dpi=args.dpi)
        else:
            fig.savefig(args.output, dpi=args.dpi, transparent=True)
        print("File %s created" % args.output)
    else:
        plt.show()
コード例 #52
0
 def test_load_signal_rescaling(self):
     params = Struct(beam_nside=2**6)
     ctx = Struct(params=params)
     astro_signal = static_gsm.load_signal(ctx)
     assert astro_signal is not None
     assert hp.get_nside(astro_signal) == ctx.params.beam_nside
コード例 #53
0
if args.taper_coords:
    try:
        ra0, dec0 = map(float, args.taper_coords.split(','))
    except:
        exit(
            'Cannot convert --taper_coords into ra,dec - must be two numbers separated by a comma'
        )

##Read in healpix map and get nside
##Convert in Jy if necessary
flux = hp.read_map(args.healpix)

if unit == 'mK' or unit == 'K':
    flux = convert2Jy(flux, float(frequency), unit)

nside = hp.get_nside(flux)
##Find out ra,dec and convert to degrees
dec, ra = hp.pix2ang(nside, arange(hp.nside2npix(nside)))
dec = dec * (180. / pi) - 90.0
ra *= (180 / pi)

if args.taper_coords:
    ra0, dec0 = map(float, args.taper_coords.split(','))

    #if args.use_minimum_flux_as_floor:
    #noise_floor = flux.min()
    #print 'Noise floor is',noise_floor,flux.max()
    #else:
    #noise_floor = condon_fixbase(bmax=float(args.max_baseline),freq=float(frequency))

    #if noise_floor <= 0:
コード例 #54
0
    temps[catalog[:, -1] == 1] = (
        catalog[:, 3][catalog[:, -1] == 1] * 1e-9 * fluxfacs[j] *
        fac) / np.square(np.radians(
            catalog[:, 4][catalog[:, -1] == 1]))  #Using GAUFLUX if EXTENDED
    #catalog[:,2:] * fluxfacs[:,None].T * 0.001 #mK

    sigmas = np.radians(catalog[:, 4] / 60.) / (
        2. * mh.sqrt(2. * mh.log(2.))
    )  #np.radians(fwhms) / (2.*mh.sqrt(2.*mh.log(2.))) #sigma in radians
    sigmas[catalog[:, -1] == 0] = fwhms_rad[j] / (
        2. * mh.sqrt(2. * mh.log(2.)))  #Use beamFWHM if not EXTENDED

    for i in xrange(len(coords)):  #Loop over point sources
        print 'Subtracting Gaussian profile for point source', i + 1, '/', len(
            coords), 'map', j
        samppixs = hp.query_disc(hp.get_nside(maps[j]),
                                 hp.ang2vec(coords[i, 1], coords[i, 0]),
                                 5. * sigmas[i])
        maps[j] = gauss_source(maps[j], temps[i], sigmas[i],
                               (coords[i, 1], coords[i, 0]), samppixs)
        #samppixsall = np.concatenate((samppixsall,samppixs)) #Slows program down

    #Save point source residuals
    resid[j] = origmaps[j] - maps[j]
'''testmap = cp.deepcopy(maps[j])
testmap[:] = 0.
testmap[samppixsall[1:]] = 1.'''

#Write new maps to FITS files
for i in xrange(len(maps)):
    hp.write_map(outfits[i], maps[i])
コード例 #55
0
ファイル: Beam.py プロジェクト: jizhi/jizhipy
def BeamMap( pointRA=None, pointDec=None, dwl=None, freq=None, uniform=False, Bmap0=None, dtype='float32', nside=None ) : 
	'''
	pointRA, pointDec:
		[degree]
		Where does the antenna point to?
		default points to (RA, Dec) = (0, 0)

	dwl:
		[meter]
		d - diameter: for dish: one value
		w - width, l - length: for cylinder: (w,l)

	freq: 
		[MHz]
		Used to get FWHM

	uniform:
		True | False
		If ==True: return =1 map

	Bmap0:
		Use this Bmap0 as the basic and rotate it to (pointRA, pointDec)
	'''
	import healpy as hp
	import numpy as np
	from jizhipy.Array import Asarray
	from jizhipy.Basic import Raise
	from jizhipy.Transform import CoordTrans
	try : dtype = np.dtype(dtype)
	except : dtype = np.dtype(None)
	if (nside is not None) : nside = int(round(nside))
	elif (Bmap0 is not None) : nside = hp.get_nside(Bmap0)
	else : Raise(Exception, 'nside = None')
	#--------------------------------------------------
	if (uniform) : 
		Bmap = np.ones(12*nside**2, dtype)
		if (Bmap0 is not None) : Bmap *= Bmap0[Bmap0.size/2]
		return Bmap
	#--------------------------------------------------
	if (Bmap0 is not None) : 
		nside0 = hp.get_nside(Bmap0)
		if (nside0 != nside) : Bmap0 = hp.ud_grade(nside, Bmap0)
		Bmap0 = Bmap0.astype(dtype)
	#--------------------------------------------------
	else : 
		n = hp.ang2pix(nside, np.pi/2, 0)
		Bmap0 = np.zeros(12*nside**2, dtype)
		Bmap0[n] = 10000
		D = Asarray(dwl)[0]
		fwhm = 1.03 * 300/freq / D
		Bmap0 = hp.smoothing(Bmap0, fwhm, verbose=False)
		Bmap0[Bmap0<0] = 0
		Bmap0 /= Bmap0.sum()
	#--------------------------------------------------
	if (pointRA is None) : pointRA = 0
	if (pointDec is None) : pointDec = 0
	if (abs(pointRA)<1e-4 and abs(pointDec)<1e-4) : return Bmap0
	#--------------------------------------------------
	theta, phi = hp.pix2ang(nside, np.arange(12*nside**2))
	theta, phi = CoordTrans.thetaphiRotation([theta, phi], az=pointRA, ay=-pointDec)
	n = hp.ang2pix(nside, theta, phi)
	Bmap = Bmap0[n]
	return Bmap
コード例 #56
0
ファイル: test.py プロジェクト: hoangducthuong/beamconv
def idea_jon():

    nside_spin = 512
    ra0 = 0
    dec0 = -90
    az_throw = 10
    max_spin = 5
    fwhm = 32.2
    scan_opts = dict(verbose=1,
                     ra0=ra0,
                     dec0=dec0,
                     az_throw=az_throw,
                     nside_spin=nside_spin,
                     max_spin=max_spin,
                     binning=True)

    lmax = 800

    alm = tools.gauss_blm(1e-5, lmax, pol=False)
    ell = np.arange(lmax + 1)
    fl = np.sqrt((2 * ell + 1) / 4. / np.pi)
    hp.almxfl(alm, fl, mmax=None, inplace=True)
    fm = (-1)**(hp.Alm.getlm(lmax)[1])
    alm *= fm
    alm = tools.get_copol_blm(alm)

    # create Beam properties and pickle (this is just to test load_focal_plane)
    import tempfile
    import shutil
    import pickle
    opj = os.path.join

    blm_dir = os.path.abspath(
        opj(os.path.dirname(__file__), '../tests/test_data/example_blms'))
    po_file = opj(blm_dir, 'blm_hp_X1T1R1C8A_800_800.npy')
    eg_file = opj(blm_dir, 'blm_hp_eg_X1T1R1C8A_800_800.npy')

    tmp_dir = tempfile.mkdtemp()

    beam_file = opj(tmp_dir, 'beam_opts.pkl')
    beam_opts = dict(az=0,
                     el=0,
                     polang=0.,
                     btype='Gaussian',
                     name='X1T1R1C8',
                     fwhm=fwhm,
                     lmax=800,
                     mmax=800,
                     amplitude=1.,
                     po_file=po_file,
                     eg_file=eg_file)

    with open(beam_file, 'wb') as handle:
        pickle.dump(beam_opts, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # init scan strategy and instrument
    ss = ScanStrategy(
        1.,  # mission duration in sec.
        sample_rate=10000,
        location='spole')

    ss.allocate_maps(nside=1024)
    ss.load_focal_plane(tmp_dir, no_pairs=True)

    # remove tmp dir and contents
    shutil.rmtree(tmp_dir)

    ss.set_el_steps(0.01, steps=np.linspace(-10, 10, 100))

    # Generate maps with Gaussian beams
    ss.scan_instrument_mpi(alm, **scan_opts)
    ss.reset_el_steps()

    # Solve for the maps
    maps_g, cond_g = ss.solve_for_map(fill=np.nan)

    # Generate maps with elliptical Gaussian beams
    ss.allocate_maps(nside=1024)
    ss.beams[0][0].btype = 'EG'
    ss.scan_instrument_mpi(alm, **scan_opts)
    ss.reset_el_steps()

    # Solve for the maps
    maps_eg, cond_eg = ss.solve_for_map(fill=np.nan)

    # Generate map with Physical Optics beams and plot them
    ss.allocate_maps(nside=1024)
    ss.beams[0][0].btype = 'PO'
    ss.scan_instrument_mpi(alm, **scan_opts)
    ss.reset_el_steps()

    # Solve for the maps
    maps_po, cond_po = ss.solve_for_map(fill=np.nan)

    # Plotting
    print('plotting results')

    cart_opts = dict(  #rot=[ra0, dec0, 0],
        lonra=[-min(0.5 * az_throw, 10),
               min(0.5 * az_throw, 10)],
        latra=[-min(0.375 * az_throw, 10),
               min(0.375 * az_throw, 10)],
        unit=r'[$\mu K_{\mathrm{CMB}}$]')

    # plot smoothed input maps
    nside = hp.get_nside(maps_g[0])
    hp.smoothalm(alm, fwhm=np.radians(fwhm / 60.), verbose=False)
    maps_raw = hp.alm2map(alm, nside, verbose=False)

    plot_iqu(maps_raw,
             '../scratch/img/',
             'raw_delta',
             sym_limits=[1, 1, 1],
             plot_func=hp.cartview,
             **cart_opts)
コード例 #57
0
def trygveplot(input,
               dataset=None,
               nside=None,
               auto=False,
               min=False,
               max=False,
               mid=[],
               rng="auto",
               colorbar=False,
               graticule=False,
               lmax=None,
               fwhm=0.0,
               mask=None,
               mfill=None,
               sig=[
                   0,
               ],
               remove_dipole=None,
               remove_monopole=None,
               logscale=False,
               size="m",
               white_background=False,
               darkmode=False,
               png=False,
               cmap=None,
               title=None,
               ltitle=None,
               unit=None,
               scale=None,
               outdir='.',
               outname=None,
               verbose=False,
               fontsize=11,
               gif=False,
               oldfont=False,
               dpi=300,
               xsize=2000,
               hires=False):
    """
    Plots a fits file, a h5 data structure or a data array in Mollview with pretty formatting.
    Option of autodecting component base on file-string with json look-up table.

    Parameters
    ----------
    input : array, str or list
        String or list of strings with filenames such as "cmb.fits" or "chain_c001.h5" for input data.
    dataset : str
        if input is a .h5 hdf-file specify which dataset to plot, for example "000007/cmb/amp_alm"
        default = None        
    nside : int
        nside for optional ud_grade (required when loading from hdf file)
        default = None
    auto : bool
        Toggle parameter autodetector.
        Automatic identification of plotting parameters based on information from autoparams.json
        default = False
    min :  str 
        Minimum value of colorbar. Overrides autodetector.
        default = False
    max :  str 
        Maximum value of colorbar. Overrides autodetector.
        default = False
    mid : list
        Adds tick values between min and max. Accepts multiple ex. -mid 2 -mid 3.
        default = []
    range : str
        Min and max value around 0 (ex. 10). 
        Also compatible with "auto": sets to 97.5 percentile of data.
        Also compatible with "minmax": sets to min and max values.
        default = "auto"
    colorbar : bool
        Adds a colorbar, and "cb" to output filename.
        default = False
    graticule : bool
        Adds graticule to figure.
        default = False
    lmax : float
        Set the lmax of alm input data from hdf.
        If reading hdf and not set, this is automatically found.
        default = None
    fwhm : float
        Optional map smoothing. FWHM of gaussian smoothing in arcmin.
        default = 0.0
    mask : str
        Apply a mask file (supply filename) to data (uses signal 0). 
        default = None
    mfill : str
        Color to fill masked area, for example "gray".
        Transparent by default.
        defualt : None
    sig : list of int
        Signal indices to be plotted. 0, 1, 2 interprated as IQU.
        Supports multiple, such that -sig 1 -sig 2.
        default = [0,]
    remove_dipole : str
        Fits and removes a dipole. Specify mask for fit, or "auto".
        default = None
    remove_monopole : str
        Fits and removes a monopole. Specify mask for fit, or "auto".
        default = None
    logscale : str
        Normalizes data using a semi-logscale linear between -1 and 1. 
        Autodetector uses this sometimes, you will be warned.
        default = None
    size : str
        Size of output maps. 
        1/3, 1/2 and full page width (8.8/12/18cm) [ex. x, s, m or l, or ex. slm for all]"
        default = "m"
    white_background : bool
        Sets white background. Transparent by default.
        default = False
    darkmode : bool
        Plots all outlines in white for dark backgrounds, and adds "dark" in filename.
        default = False
    png : bool
        Saves file as png as opposed to PDF. Overwritten by outname extension.
        default = False
    cmap : str
        Colormap (ex. sunburst, planck, jet). Both matplotliib and cmasher available as of now.
        Also supports qualitative plotly map, [ex. q-Plotly-4 (q for qualitative 4 for max color)]
        Sets planck as default.
        default = None
    title : str
        Sets the upper right title. Has LaTeX functionaliity (ex. $A_{s}$.)
        default = None
    ltitle : str
        Sets the upper left title. Has LaTeX functionaliity (ex. $A_{s}$.)
        default = None
    unit : str
        Sets uniit under color bar. Has LaTeX functionaliity (ex. $\mu K$".
        default = None
    scale : float
        Scale data by multiplicatiive factor. [ex. 1e-6 for muK to K
        default = None
    outdir : path
        Optiional different output directory.
        default = "."
    outname : str
        Output filename. If not specified, descriptive filename will be generated from chosen params.
        Extension determines filetype and overrides -png flag.
        default = None
    gif : bool
        Make gif is input is list of data.
        defaullt = False
    oldfont : bool
        Use DejaVu font as oppoosed to Times.
        default = False
    fontsize : int
        Fontsize
        default = 11
    dpi : int
        DPI (Dots per inch). For figure resolution.
        default = 300
    xsize : int
        Figure width in pixels. Uses xsize*(xsize/2)
        default = 2000
    hires : bool
        For high resolution figures. Sets dpi to 3000 and xsize to 10000.
        default = False
    verbose : bool
        Verbose mode. Mainly timer outputs.
        default = False
    """
    if hires:
        xsize = 10000
        dpi = 1000
    if not oldfont:
        plt.rcParams["mathtext.fontset"] = "stix"
    plt.rcParams["backend"] = "agg" if png else "pdf"
    if outname:
        plt.rcParams["backend"] = "agg" if outname.endswith("png") else "pdf"
    plt.rcParams["axes.linewidth"] = 1
    if darkmode:
        params = [
            "text.color", "axes.facecolor", "axes.edgecolor",
            "axes.labelcolor", "xtick.color", "ytick.color", "grid.color",
            "legend.facecolor", "legend.edgecolor"
        ]
        for p in params:
            plt.rcParams[p] = "white"

    ### Terminal output for
    click.echo("")
    click.echo(click.style("{:#^48}".format(""), fg="green"))
    click.echo(
        click.style("Plotting", fg="green") + f" {input} dset={dataset}")

    ####   READ MAP   #####
    maps_, lmax, outfile, signal_labels = get_map(
        input,
        sig,
        dataset,
        nside,
        lmax,
        fwhm,
    )

    # Terminal output specified signals
    click.echo(click.style("Using signals ", fg="green") + f"{sig}")
    click.echo(click.style("{:#^48}".format(""), fg="green"))

    #### Main plotting loop ####
    imgs = []  # Store images for gif
    for i, maps in enumerate(maps_):
        if maps.ndim == 1: maps = maps.reshape(1, -1)
        for pl, polt, in enumerate(sig):

            #### Select data column  #####
            m = hp.ma(maps[pl])
            signal_label = signal_labels[
                polt] if signal_labels else get_signallabel(polt)
            nsid = hp.get_nside(m)

            #### Smooth  #####
            if float(fwhm) > 0 and input[i].endswith(".fits"):
                click.echo(
                    click.style(f"Smoothing fits map to {fwhm} arcmin fwhm",
                                fg="yellow"))
                m = hp.smoothing(
                    m,
                    fwhm=arcmin2rad(fwhm),
                    lmax=lmax,
                )

            #### ud_grade #####
            if nside is not None and input[i].endswith(".fits"):
                if nsid != nside:
                    click.echo(
                        click.style(f"UDgrading map from {nsid} to {nside}",
                                    fg="yellow"))
                    m = hp.ud_grade(
                        m,
                        nside,
                    )
            else:
                nside = nsid

            #### Remove monopole or dipole #####
            if remove_dipole or remove_monopole:
                m = remove_md(m, remove_dipole, remove_monopole, nside)

            #### Scaling factor #####
            if scale:
                datastring = f"{outfile}{outname}"
                if "chisq" in datastring:
                    click.echo(
                        click.style(f"Scaling chisq data with dof={scale}",
                                    fg="yellow"))
                    m = (m - scale) / np.sqrt(2 * scale)
                else:
                    click.echo(
                        click.style(f"Scaling data by {scale}", fg="yellow"))
                    m *= scale

            #### Automatic variables #####
            if auto:
                (
                    m,
                    ttl,
                    lttl,
                    unt,
                    ticks,
                    cmp,
                    lgscale,
                ) = get_params(
                    m,
                    outfile,
                    outname,
                    signal_label,
                )
                # Tick bug fix

                mn, md, mx = (ticks[0], None, ticks[-1])
                if not mid and len(ticks) > 2:
                    if ticks[0] < ticks[1] and ticks[-2] < ticks[-1]:
                        md = ticks[1:-1]
                    else:
                        ticks.pop(1)
            else:
                ttl = lttl = unt = ""
                mn = md = mx = None
                ticks = [False, False]
                lgscale = False
                cmp = "planck"

            #if "diff" in outname+outfile:
            #    click.echo(click.style("Changing cmap to planck because diff map!", fg="yellow", blink=True, bold=True))
            #    cmp = "planck"

            #### Commandline priority ####
            if logscale != None: lgscale = logscale
            if title: ttl = title
            if ltitle: lttl = ltitle
            if unit: unt = unit

            #### Colorbar ticks ####
            ticks = get_ticks(m, ticks, mn, md, mx, min, mid, max, rng, auto)
            ticklabels = [fmt(i, 1) for i in ticks]

            # Terminal ouput
            click.echo(click.style("FWHM: ", fg="green") + f"{fwhm}")
            click.echo(click.style("nside: ", fg="green") + f"{nside}")
            click.echo(click.style("Ticks: ", fg="green") + f"{ticks}")
            click.echo(click.style("Unit: ", fg="green") + f"{unt}")
            click.echo(click.style("Title: ", fg="green") + f"{ttl}")

            #### Logscale ####
            if lgscale: m, ticks = apply_logscale(m, ticks, linthresh=1)

            #### Color map #####
            cmap_ = get_cmap(cmap, cmp, logscale=lgscale)

            #### Projection ####
            grid_pix, longitude, latitude = project_map(
                nside,
                xsize=xsize,
                ysize=int(xsize / 2.0),
            )

            #### Mask ##########
            grid_map, cmap_ = apply_mask(m, mask, grid_pix, mfill, polt,
                                         cmap_) if mask else (m[grid_pix],
                                                              cmap_)

            for width in get_sizes(size):
                click.echo(click.style("Size: ", fg="green") + str(width))
                #### Make figure ####
                height = width / 2.0
                if colorbar:
                    height *= 1.275  # Make sure text doesnt change with colorbar
                if gif:
                    # Hacky gif implementation
                    if i == 0:
                        fig = plt.figure(figsize=(
                            cm2inch(width),
                            cm2inch(height),
                        ), )
                        ax = fig.add_subplot(111, projection="mollweide")
                else:
                    fig = plt.figure(figsize=(
                        cm2inch(width),
                        cm2inch(height),
                    ), )
                    ax = fig.add_subplot(111, projection="mollweide")

                #norm=col.SymLogNorm(linthresh=1, base=10) if logscale else None
                image = plt.pcolormesh(longitude[::-1],
                                       latitude,
                                       grid_map,
                                       vmin=ticks[0],
                                       vmax=ticks[-1],
                                       rasterized=True,
                                       cmap=cmap_,
                                       shading='auto',
                                       animated=gif)

                # Save img for gif
                if gif: imgs.append([image])

                #### Graticule ####
                if graticule: apply_graticule(ax, width)
                ax.xaxis.set_ticklabels([])
                ax.yaxis.set_ticklabels([])  # rm lonlat ticklabs

                #### Colorbar ####
                if colorbar:
                    apply_colorbar(fig,
                                   image,
                                   ticks,
                                   ticklabels,
                                   unt,
                                   fontsize,
                                   linthresh=1,
                                   logscale=lgscale)

                #### Right Title ####
                plt.text(
                    4.5,
                    1.1,
                    r"%s" % ttl,
                    ha="center",
                    va="center",
                    fontsize=fontsize,
                )

                #### Left Title ####
                plt.text(
                    -4.5,
                    1.1,
                    r"%s" % lttl,
                    ha="center",
                    va="center",
                    fontsize=fontsize,
                )

                #### Output file ####
                plt.tight_layout()
                if gif:  #output gif on last iteration only
                    if i == len(input) - 1:
                        output_map(fig, outfile, png, fwhm, colorbar, mask,
                                   remove_dipole, darkmode, white_background,
                                   cmap_, nside, signal_label, width, outdir,
                                   gif, imgs, dpi, verbose, outname)
                else:
                    output_map(fig, outfile, png, fwhm, colorbar, mask,
                               remove_dipole, darkmode, white_background,
                               cmap_, nside, signal_label, width, outdir, gif,
                               imgs, dpi, verbose, outname)
                    click.echo("Saved, closing fig")
                    plt.close()
                click.echo(
                    "Totaltime:",
                    (time.time() - totaltime),
                ) if verbose else None
#####################################################################
print "Reading healpix maps"

sehgalTMap = hp.read_map("./input/sehgal_maps/" + nuStr.zfill(3) +
                         "_rad_pts_healpix.fits")

#pathOut = "/global/cscratch1/sd/eschaan/SehgalRadioPolarizedSources/output/sehgal_maps/radio_sources/"
pathOut = "./output/sehgal_maps/radio_sources/"
tMap = hp.read_map(pathOut + "t_radio_sehgal_" + nuStr +
                   "ghz_muk_scatter.fits")
qMap = hp.read_map(pathOut + "q_radio_sehgal_" + nuStr +
                   "ghz_muk_scatter.fits")
uMap = hp.read_map(pathOut + "u_radio_sehgal_" + nuStr +
                   "ghz_muk_scatter.fits")

nSide = hp.get_nside(tMap)

cmb = CMB(beam=beamFwhm,
          noise=noiseT,
          nu1=nu,
          nu2=nu,
          lMin=30.,
          lMaxT=3.e3,
          lMaxP=5.e3,
          fg=True,
          atm=True,
          atmProp=[lKnee, aKnee, lKnee, aKnee],
          name=None)

####################################################################
# ## Read the kappa map
コード例 #59
0
ファイル: Healpix.py プロジェクト: jizhi/jizhipy
    def __init__(self,
                 inputwhat=None,
                 which=(1, 0),
                 ordering=None,
                 coordsys=None,
                 unit=None,
                 nside=None,
                 freq=None,
                 dtype=None,
                 Nprocess=None,
                 verbose=False):
        '''
		Input map must be healpix map

		inputwhat, which:
			(1) Path of healpix map with ".fits": 
				which = (hdu, field)

			(2) Path of healpix map with ".hdf5": 
				hpmap = h5py.open(inputwhat)[which]
				which is the key (str)

			(3) np.ndarray with healpix pixelization (size=12*2**n)
				which is invalid here

		ordering:
			'RING' | 'NESTED'
			the output ordering of self.hpmap

		coordsys:
			'Equatorial' | 'Galactic'
			the output coordsys of self.hpmap

		unit:
			'K' | 'mK' | 'uK'
			the output unit of self.hpmap
		
		nside:
			the output nside of self.hpmap

		dtype:
			'float32' | 'float64' | np.float32 | np.float64
			the output self.hpmap.dtype
		'''
        import os, pyfits
        import healpy as hp
        import numpy as np
        from jizhipy.Process import NprocessCPU
        from jizhipy.Basic import IsType
        self.verbose = bool(verbose)
        if (self.verbose): print('HealpixMap.__init__:')
        self.Nprocess = NprocessCPU(Nprocess)[0]
        self.ordering, self.nest = self._OrderingCheck(None, ordering)[2:]
        self.coordsys = self._CoordsysCheck(None, coordsys)[1]
        self.nside = self._NsideCheck(nside)
        self.unit = self._UnitCheck(None, unit)[1]
        if (dtype is not None):
            try:
                dtype = np.dtype(dtype)
            except:
                dtype = None
        self.dtype = dtype
        self.freq = freq
        if (self.verbose):
            try:
                freqstr = ('%.1fMHz' % self.freq)
            except:
                freqstr = str(self.freq)
            print("    ordering=" + str(self.ordering) + "(nest=" +
                  str(self.nest) + "), coordsys=" + str(self.coordsys) +
                  ", nside=" + str(self.nside) + ", unit=" + str(self.unit) +
                  ", dtype=" + str(self.dtype) + ', freq=' + freqstr)
        #--------------------------------------------------

        if (not IsType.isstr(inputwhat)):
            self.hpmap = np.array(inputwhat)
            self.nsidein = hp.get_nside(self.hpmap)
            self.dtypein = self.hpmap.dtype
            self.hpmap = np.array(self.hpmap, self.dtype)
            self.dtype = self.hpmap.dtype
            self.inputwhat = None
            self.orderingin = None
            self.coordsysin = None
            self.unitin = None
            self.freqin = None
            printstrNone = ['orderingin', 'coordsysin', 'unitin', 'freqin']
            if (self.verbose):
                print('    inputwhat is np.ndarray')
                print(
                    '    orderingin = coordsysin = unitin = freqin = None. You can use HealpixMap.InProperty() to set them'
                )
        #--------------------------------------------------

        elif ('.fits' == str(inputwhat).lower()[-5:]):
            printstrNone = []
            self.inputwhat = os.path.abspath(os.path.expanduser(inputwhat))
            fo = pyfits.open(self.inputwhat)
            hdr = fo[0].header
            for i in range(1, which[0] + 1):
                hdr.update(fo[i].header)
            fo = fo[which[0]]
            self.dtypein = np.dtype(fo.data.dtype[which[1]].name)
            self.hpmap = np.array(fo.data[fo.data.names[which[1]]],
                                  self.dtype)  # Read data directly
            self.dtype = self.hpmap.dtype
            self.nsidein = hp.get_nside(self.hpmap)
            #--------------------------------------------------
            try:
                orderingin = str(hdr['ORDERING']).upper()
                orderingin, nestin = self._OrderingCheck(orderingin)[:2]
            except:
                printstrNone.append('orderingin')
                orderingin, nestin = None, None
            self.orderingin, self.nestin = orderingin, nestin
            #--------------------------------------------------
            try:
                try:
                    coordsysin = str(hdr['COORDSYS']).lower()
                except:
                    coordsysin = str(hdr['SKYCOORD']).lower()
                coordsysin = self._CoordsysCheck(coordsysin)[0]
            except:
                printstrNone.append('coordsysin')
                coordsysin = None
            self.coordsysin = coordsysin
            #--------------------------------------------------
            unitin = 'None'
            try:
                unitin = str(hdr['TUNIT' + str(which[1] + 1)])
                unitin = self._UnitCheck(unitin)[0]
            except:
                if (self.verbose):
                    print("    Warning: unitin = " + unitin +
                          " not in ['K', 'mK', 'uK']")
                printstrNone.append('unitin')
                unitin = None
            self.unitin = unitin
            #--------------------------------------------------
            printstr = ''
            try:
                n = hdr.keys().index('FREQ')
                freqin, com = str(hdr['FREQ']).lower(), hdr.comments[n].lower()
                if ('ghz' in freqin or 'ghz' in com):
                    freqin = 1e3 * float(freqin.split('ghz')[0])
                elif ('mhz' in freqin or 'mhz' in com):
                    freqin = float(freqin.split('mhz')[0])
                elif ('hz' in freqin or 'hz' in com):
                    freqin = float(freqin.split('hz')[0])
                else:
                    printstr = 'FREQ : ' + freqin + ' / ' + com + ", NOT with unit ['Hz', 'MHz', 'GHz']"
            except:
                printstrNone.append('freqin')
                freqin = None
            self.freqin = freqin
            if (self.verbose):
                try:
                    freqstrin = ('%.1fMHz' % self.freqin)
                except:
                    freqstrin = str(self.freqin)
                print('    inputwhat="' + inputwhat + '"')
                print("    orderingin=" + str(self.orderingin) + "(nestin=" +
                      str(self.nestin) + "), coordsysin=" +
                      str(self.coordsysin) + ", nsidein=" + str(self.nsidein) +
                      ", unitin=" + str(self.unitin) + ", dtypein=" +
                      str(self.dtypein) + ', freqin=' + freqstrin)
        #--------------------------------------------------

        # Do
        if (self.orderingin is not None):
            # coordsys
            if (self.coordsysin is None and self.coordsys is not None):
                if (self.verbose):
                    print("    Warning: coordsysin = None, coordsys = '" +
                          self.coordsys + "', can NOT change")
                self.coordsys = None
            elif (self.coordsysin is None and self.coordsys is None):
                pass
            elif (self.coordsysin is not None and self.coordsys is None):
                self.coordsys = self.coordsysin
            elif (self.coordsysin != self.coordsys):
                self.CoordsysConvert()
            #--------------------------------------------------

            # ordering
            if (self.ordering is None):
                self.ordering, self.nest = self.orderingin, self.nestin
            if (self.orderingin != self.ordering): self.OrderingConvert()
            #--------------------------------------------------

            # nside
            if (self.nside is None): self.nside = self.nsidein
            if (self.nsidein != self.nside):
                self.NsideConvert(None, self.ordering)
        #--------------------------------------------------

        try:
            self.unit + self.unitin
            self.UnitConvert()
        except:
            pass

        try:
            self.freq + self.freqin
            self.FreqConvert()
        except:
            pass
コード例 #60
0
def get_map(
    input,
    sig,
    dataset,
    nside,
    lmax,
    fwhm,
):
    # Get maps array if .h5 file
    signal_labels = None
    maps = []
    input = [input] if isinstance(input, (str, np.ndarray)) else input
    if not input:
        print("No input specified")
        sys.exit()
    for input_ in input:
        if isinstance(input_, np.ndarray):
            if not nside: nside = hp.get_nside(input_)
            if not lmax: lmax = 2.5 * nside
            outfile = "figure"
            if input_.ndim > 1:
                maps_ = input_[sig]
            else:
                maps_ = input_

        elif input_.endswith(".h5"):
            from src.commands_hdf import h5map2fits
            from src.tools import alm2fits_tool
            # Get maps from alm data in .h5
            if dataset.endswith("alm"):
                if not nside:
                    click.echo(
                        click.style("Specify nside for h5 files", fg="red"))
                    sys.exit()

                click.echo(click.style("Converting alms to map", fg="green"))
                (
                    maps_,
                    _,
                    _,
                    _,
                    outfile,
                ) = alm2fits_tool(
                    input_,
                    dataset,
                    nside,
                    lmax,
                    fwhm,
                    save=False,
                )
                maps_ = maps_[sig]
            # Get maps from map data in .h5
            elif dataset.endswith("map"):
                click.echo(click.style("Reading map from hdf", fg="green"))
                (
                    maps_,
                    _,
                    _,
                    outfile,
                ) = h5map2fits(input_, dataset, save=False)
                maps_ = maps_[sig]

            # Found no data specified kind in .h5
            else:
                click.echo(
                    click.style("Dataset not found. Breaking.", fg="red"))
                click.echo(
                    click.style(f"Does {input_}/{dataset} exist?", fg="red"))
                sys.exit()

        elif input_.endswith(".fits"):
            maps_, header = hp.read_map(
                input_,
                field=sig,
                verbose=False,
                h=True,
                dtype=None,
            )
            header = dict(header)
            signal_labels = []
            for i in range(int(header["TFIELDS"])):
                signal_label = header[f"TTYPE{i+1}"]
                if signal_label in ["TEMPERATURE", "TEMP"]:
                    signal_label = "T"
                if signal_label in ["Q-POLARISATION", "Q_POLARISATION"]:
                    signal_label = "Q"
                if signal_label in ["U-POLARISATION", "U_POLARISATION"]:
                    signal_label = "U"
                signal_labels.append(signal_label)
            outfile = input_.replace(".fits", "")
        else:
            click.echo(click.style("Did not recognize data.", fg="red"))
            sys.exit()

        if maps_.ndim == 1: maps_ = maps_.reshape(1, -1)
        maps.append(maps_)
    return maps, lmax, outfile, signal_labels