def map2alm(maps, lmax=None, mmax=None, iter=3, pol=True, use_weights=False, regression=True, datapath=None): """Computes the alm of an Healpix map. Parameters ---------- maps : array-like, shape (Npix,) or (n, Npix) The input map or a list of n input maps. lmax : int, scalar, optional Maximum l of the power spectrum. Default: 3*nside-1 mmax : int, scalar, optional Maximum m of the alm. Default: lmax iter : int, scalar, optional Number of iteration (default: 3) pol : bool, optional If True, assumes input maps are TQU. Output will be TEB alm's. (input must be 1 or 3 maps) If False, apply spin 0 harmonic transform to each map. (input can be any number of maps) If there is only one input map, it has no effect. Default: True. use_weights: bool, scalar, optional If True, use the ring weighting. Default: False. regression: bool, scalar, optional If True, subtract map average before computing alm. Default: True. datapath : None or str, optional If given, the directory where to find the weights data. Returns ------- alms : array or tuple of array alm or a tuple of 3 alm (almT, almE, almB) if polarized input. Notes ----- The pixels which have the special `UNSEEN` value are replaced by zeros before spherical harmonic transform. They are converted back to `UNSEEN` value, so that the input maps are not modified. Each map have its own, independent mask. """ maps = ma_to_array(maps) info = maptype(maps) if pol or info in (0, 1): alms = _sphtools.map2alm( maps, niter=iter, regression=regression, datapath=datapath, use_weights=use_weights, lmax=lmax, mmax=mmax ) else: # info >= 2 and pol is False : spin 0 spht for each map alms = [ _sphtools.map2alm( mm, niter=iter, regression=regression, datapath=datapath, use_weights=use_weights, lmax=lmax, mmax=mmax ) for mm in maps ] return alms
def mollview(map=None,fig=None,rot=None,coord=None,unit='', xsize=800,title='Mollweide view', title_font_size=14,label_font_size=14, nest=False, min=None,max=None,flip='astro', remove_dip=False,remove_mono=False, gal_cut=0, format='%g',format2='%g', cbar=True, cmap=None, notext=False, cbar_aspect=25, cbar_fraction=0.1, cbar_shrink=0.5, cbar_pad=0.05, norm=None, hold=False, margins=None, sub=None, return_projected_map=False): """Plot an healpix map (given as an array) in Mollweide projection. Parameters ---------- map : float, array-like or None An array containing the map, supports masked maps, see the `ma` function. If None, will display a blank map, useful for overplotting. fig : int or None, optional The figure number to use. Default: create a new figure rot : scalar or sequence, optional Describe the rotation to apply. In the form (lon, lat, psi) (unit: degrees) : the point at longitude *lon* and latitude *lat* will be at the center. An additional rotation of angle *psi* around this direction is applied. coord : sequence of character, optional Either one of 'G', 'E' or 'C' to describe the coordinate system of the map, or a sequence of 2 of these to rotate the map from the first to the second coordinate system. unit : str, optional A text describing the unit of the data. Default: '' xsize : int, optional The size of the image. Default: 800 title : str, optional The title of the plot. Default: 'Mollweide view' nest : bool, optional If True, ordering scheme is NESTED. Default: False (RING) min : float, optional The minimum range value max : float, optional The maximum range value flip : {'astro', 'geo'}, optional Defines the convention of projection : 'astro' (default, east towards left, west towards right) or 'geo' (east towards roght, west towards left) remove_dip : bool, optional If :const:`True`, remove the dipole+monopole remove_mono : bool, optional If :const:`True`, remove the monopole gal_cut : float, scalar, optional Symmetric galactic cut for the dipole/monopole fit. Removes points in latitude range [-gal_cut, +gal_cut] format : str, optional The format of the scale label. Default: '%g' format2 : str, optional Format of the pixel value under mouse. Default: '%g' cbar : bool, optional Display the colorbar. Default: True notext : bool, optional If True, no text is printed around the map norm : {'hist', 'log', None} Color normalization, hist= histogram equalized color mapping, log= logarithmic color mapping, default: None (linear color mapping) hold : bool, optional If True, replace the current Axes by a MollweideAxes. use this if you want to have multiple maps on the same figure. Default: False sub : int, scalar or sequence, optional Use only a zone of the current figure (same syntax as subplot). Default: None margins : None or sequence, optional Either None, or a sequence (left,bottom,right,top) giving the margins on left,bottom,right and top of the axes. Values are relative to figure (0-1). Default: None return_projected_map : bool if True returns the projected map in a 2d numpy array See Also -------- gnomview, cartview, orthview """ # Create the figure import pylab if not (hold or sub): f=pylab.figure(fig,figsize=(8.5,5.4)) extent = (0.02,0.05,0.96,0.9) elif hold: f=pylab.gcf() left,bottom,right,top = np.array(f.gca().get_position()).ravel() extent = (left,bottom,right-left,top-bottom) f.delaxes(f.gca()) else: # using subplot syntax f=pylab.gcf() if hasattr(sub,'__len__'): nrows, ncols, idx = sub else: nrows, ncols, idx = sub/100, (sub%100)/10, (sub%10) if idx < 1 or idx > ncols*nrows: raise ValueError('Wrong values for sub: %d, %d, %d'%(nrows, ncols, idx)) c,r = (idx-1)%ncols,(idx-1)/ncols if not margins: margins = (0.01,0.0,0.0,0.02) extent = (c*1./ncols+margins[0], 1.-(r+1)*1./nrows+margins[1], 1./ncols-margins[2]-margins[0], 1./nrows-margins[3]-margins[1]) extent = (extent[0]+margins[0], extent[1]+margins[1], extent[2]-margins[2]-margins[0], extent[3]-margins[3]-margins[1]) #extent = (c*1./ncols, 1.-(r+1)*1./nrows,1./ncols,1./nrows) #f=pylab.figure(fig,figsize=(8.5,5.4)) # Starting to draw : turn interactive off wasinteractive = pylab.isinteractive() pylab.ioff() try: if map is None: map = np.zeros(12)+np.inf cbar=False map = pixelfunc.ma_to_array(map) ax=PA.HpxMollweideAxes(f,extent,coord=coord,rot=rot, format=format2,flipconv=flip) f.add_axes(ax) if remove_dip: map=pixelfunc.remove_dipole(map,gal_cut=gal_cut, nest=nest,copy=True, verbose=True) elif remove_mono: map=pixelfunc.remove_monopole(map,gal_cut=gal_cut,nest=nest, copy=True,verbose=True) img = ax.projmap(map,nest=nest,xsize=xsize,coord=coord,vmin=min,vmax=max, cmap=cmap,norm=norm) if cbar: im = ax.get_images()[0] b = im.norm.inverse(np.linspace(0,1,im.cmap.N+1)) v = np.linspace(im.norm.vmin,im.norm.vmax,im.cmap.N) if matplotlib.__version__ >= '0.91.0': cb=f.colorbar(im,ax=ax, orientation='horizontal', shrink=cbar_shrink,aspect=cbar_aspect,ticks=PA.BoundaryLocator(), pad=cbar_pad,fraction=cbar_fraction,boundaries=b,values=v, format=format) else: # for older matplotlib versions, no ax kwarg cb=f.colorbar(im,orientation='horizontal', shrink=cbar_shrink,aspect=cbar_aspect,ticks=PA.BoundaryLocator(), pad=cbar_pad,fraction=cbar_fraction,boundaries=b,values=v, format=format) title_text_props = \ {'fontsize': title_font_size, 'fontweight' : 'medium', 'verticalalignment': 'baseline', 'horizontalalignment': 'center'} ax.set_title(title, fontdict=title_text_props) if not notext: ax.text(0.86,0.05,ax.proj.coordsysstr,fontsize=label_font_size, fontweight='medium',transform=ax.transAxes) if cbar: cb.ax.text(0.5,-1.0,unit,fontsize=label_font_size, fontweight='medium', transform=cb.ax.transAxes, ha='center',va='top') for l in cb.ax.xaxis.get_ticklabels(): l.set_size(label_font_size) f.sca(ax) finally: pylab.draw() if wasinteractive: pylab.ion() #pylab.show() if return_projected_map: return img
def gnomview(map=None,fig=None,rot=None,coord=None,unit='', xsize=200,ysize=None,reso=1.5,degree=False, title='Gnomonic view',nest=False,remove_dip=False, remove_mono=False,gal_cut=0, min=None,max=None,flip='astro', format='%.3g',cbar=True, cmap=None, norm=None, hold=False,sub=None,margins=None,notext=False, return_projected_map=False): """Plot an healpix map (given as an array) in Gnomonic projection. Parameters ---------- map : array-like The map to project, supports masked maps, see the `ma` function. If None, use a blank map, useful for overplotting. fig : None or int, optional A figure number. Default: None= create a new figure rot : scalar or sequence, optional Describe the rotation to apply. In the form (lon, lat, psi) (unit: degrees) : the point at longitude *lon* and latitude *lat* will be at the center. An additional rotation of angle *psi* around this direction is applied. coord : sequence of character, optional Either one of 'G', 'E' or 'C' to describe the coordinate system of the map, or a sequence of 2 of these to rotate the map from the first to the second coordinate system. unit : str, optional A text describing the unit of the data. Default: '' xsize : int, optional The size of the image. Default: 200 ysize : None or int, optional The size of the image. Default: None= xsize reso : float, optional Resolution (in arcmin if degree is False). Default: 1.5 arcmin degree : bool, optional if True, reso is in degree. Default: False title : str, optional The title of the plot. Default: 'Gnomonic view' nest : bool, optional If True, ordering scheme is NESTED. Default: False (RING) min : float, scalar, optional The minimum range value max : float, scalar, optional The maximum range value flip : {'astro', 'geo'}, optional Defines the convention of projection : 'astro' (default, east towards left, west towards right) or 'geo' (east towards roght, west towards left) remove_dip : bool, optional If :const:`True`, remove the dipole+monopole remove_mono : bool, optional If :const:`True`, remove the monopole gal_cut : float, scalar, optional Symmetric galactic cut for the dipole/monopole fit. Removes points in latitude range [-gal_cut, +gal_cut] format : str, optional The format of the scale label. Default: '%g' hold : bool, optional If True, replace the current Axes by a MollweideAxes. use this if you want to have multiple maps on the same figure. Default: False sub : int or sequence, optional Use only a zone of the current figure (same syntax as subplot). Default: None margins : None or sequence, optional Either None, or a sequence (left,bottom,right,top) giving the margins on left,bottom,right and top of the axes. Values are relative to figure (0-1). Default: None notext: bool, optional If True: do not add resolution info text. Default=False return_projected_map : bool if True returns the projected map in a 2d numpy array See Also -------- mollview, cartview, orthview """ import pylab if not (hold or sub): f=pylab.figure(fig,figsize=(5.8,6.4)) if not margins: margins = (0.075,0.05,0.075,0.05) extent = (0.0,0.0,1.0,1.0) elif hold: f=pylab.gcf() left,bottom,right,top = np.array(pylab.gca().get_position()).ravel() if not margins: margins = (0.0,0.0,0.0,0.0) extent = (left,bottom,right-left,top-bottom) f.delaxes(pylab.gca()) else: # using subplot syntax f=pylab.gcf() if hasattr(sub,'__len__'): nrows, ncols, idx = sub else: nrows, ncols, idx = sub/100, (sub%100)/10, (sub%10) if idx < 1 or idx > ncols*nrows: raise ValueError('Wrong values for sub: %d, %d, %d'%(nrows, ncols, idx)) c,r = (idx-1)%ncols,(idx-1)/ncols if not margins: margins = (0.01,0.0,0.0,0.02) extent = (c*1./ncols+margins[0], 1.-(r+1)*1./nrows+margins[1], 1./ncols-margins[2]-margins[0], 1./nrows-margins[3]-margins[1]) extent = (extent[0]+margins[0], extent[1]+margins[1], extent[2]-margins[2]-margins[0], extent[3]-margins[3]-margins[1]) #f=pylab.figure(fig,figsize=(5.5,6)) # Starting to draw : turn interactive off wasinteractive = pylab.isinteractive() pylab.ioff() try: if map is None: map = np.zeros(12)+np.inf cbar=False map = pixelfunc.ma_to_array(map) ax=PA.HpxGnomonicAxes(f,extent,coord=coord,rot=rot, format=format,flipconv=flip) f.add_axes(ax) if remove_dip: map=pixelfunc.remove_dipole(map,gal_cut=gal_cut,nest=nest,copy=True) elif remove_mono: map=pixelfunc.remove_monopole(map,gal_cut=gal_cut,nest=nest,copy=True) img = ax.projmap(map,nest=nest,coord=coord,vmin=min,vmax=max, xsize=xsize,ysize=ysize,reso=reso,cmap=cmap,norm=norm) if cbar: im = ax.get_images()[0] b = im.norm.inverse(np.linspace(0,1,im.cmap.N+1)) v = np.linspace(im.norm.vmin,im.norm.vmax,im.cmap.N) if matplotlib.__version__ >= '0.91.0': cb=f.colorbar(im,ax=ax, orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.08,fraction=0.1,boundaries=b,values=v, format=format) else: cb=f.colorbar(im,orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.08,fraction=0.1,boundaries=b,values=v, format=format) ax.set_title(title) if not notext: ax.text(-0.07,0.02, "%g '/pix, %dx%d pix"%(ax.proj.arrayinfo['reso'], ax.proj.arrayinfo['xsize'], ax.proj.arrayinfo['ysize']), fontsize=12,verticalalignment='bottom', transform=ax.transAxes,rotation=90) ax.text(-0.07,0.6,ax.proj.coordsysstr,fontsize=14, fontweight='bold',rotation=90,transform=ax.transAxes) lon,lat = np.around(ax.proj.get_center(lonlat=True),ax._coordprec) ax.text(0.5,-0.03,'(%g,%g)'%(lon,lat), verticalalignment='center', horizontalalignment='center', transform=ax.transAxes) if cbar: cb.ax.text(1.05,0.30,unit,fontsize=14,fontweight='bold', transform=cb.ax.transAxes,ha='left',va='center') f.sca(ax) finally: pylab.draw() if wasinteractive: pylab.ion() #pylab.show() if return_projected_map: return img
def mollview(map=None,fig=None,plot=False,filenme=None, rot=None,coord=None,unit='', xsize=800,title='Mollweide view',nest=False, min=None,max=None,flip='astro', remove_dip=False,remove_mono=False, gal_cut=0, format='%g',format2='%g', cbar=True,cmap=None, notext=False, norm=None,hold=False,margins=None,sub=None, return_projected_map=False): """Plot an healpix map (given as an array) in Mollweide projection. Requires the healpy package. This function is taken from the Healpy package and slightly modified. Parameters ---------- map : float, array-like or None An array containing the map, supports masked maps, see the `ma` function. If None, will display a blank map, useful for overplotting. fig : figure object or None, optional The figure to use. Default: create a new figure plot : bool (default: False) if True the image is plotted filename : string (default: None) Filename to be written to (if a filename is specified) rot : scalar or sequence, optional Describe the rotation to apply. In the form (lon, lat, psi) (unit: degrees) : the point at longitude *lon* and latitude *lat* will be at the center. An additional rotation of angle *psi* around this direction is applied. coord : sequence of character, optional Either one of 'G', 'E' or 'C' to describe the coordinate system of the map, or a sequence of 2 of these to rotate the map from the first to the second coordinate system. unit : str, optional A text describing the unit of the data. Default: '' xsize : int, optional The size of the image. Default: 800 title : str, optional The title of the plot. Default: 'Mollweide view' nest : bool, optional If True, ordering scheme is NESTED. Default: False (RING) min : float, optional The minimum range value max : float, optional The maximum range value flip : {'astro', 'geo'}, optional Defines the convention of projection : 'astro' (default, east towards left, west towards right) or 'geo' (east towards right, west towards left) remove_dip : bool, optional If :const:`True`, remove the dipole+monopole remove_mono : bool, optional If :const:`True`, remove the monopole gal_cut : float, scalar, optional Symmetric galactic cut for the dipole/monopole fit. Removes points in latitude range [-gal_cut, +gal_cut] format : str, optional The format of the scale label. Default: '%g' format2 : str, optional Format of the pixel value under mouse. Default: '%g' cbar : bool, optional Display the colorbar. Default: True notext : bool, optional If True, no text is printed around the map norm : {'hist', 'log', None} Color normalization, hist= histogram equalized color mapping, log= logarithmic color mapping, default: None (linear color mapping) hold : bool, optional If True, replace the current Axes by a MollweideAxes. use this if you want to have multiple maps on the same figure. Default: False sub : int, scalar or sequence, optional Use only a zone of the current figure (same syntax as subplot). Default: None margins : None or sequence, optional Either None, or a sequence (left,bottom,right,top) giving the margins on left,bottom,right and top of the axes. Values are relative to figure (0-1). Default: None return_projected_map : bool if True returns the projected map in a 2d numpy array See Also -------- gnomview, cartview, orthview, azeqview """ try: from healpy import projaxes as PA from healpy import pixelfunc except ImportError: warnings.warn( "Could not load healpy package. If you want to use this feature, " "plaese install the healpy package from here: http://healpy.readthedocs.io/en/latest/" "or via pip or conda.", RuntimeWarning) return # Create the figure if not (hold or sub): if fig == None: f=plt.figure(figsize=(8.5,5.4)) extent = (0.02,0.05,0.96,0.9) else: f=fig extent = (0.02,0.05,0.96,0.9) elif hold: f=plt.gcf() left,bottom,right,top = np.array(f.gca().get_position()).ravel() extent = (left,bottom,right-left,top-bottom) f.delaxes(f.gca()) else: # using subplot syntax f=plt.gcf() if hasattr(sub,'__len__'): nrows, ncols, idx = sub else: nrows, ncols, idx = sub//100, (sub%100)//10, (sub%10) if idx < 1 or idx > ncols*nrows: raise ValueError('Wrong values for sub: %d, %d, %d'%(nrows, ncols, idx)) c,r = (idx-1)%ncols,(idx-1)//ncols if not margins: margins = (0.01,0.0,0.0,0.02) extent = (c*1./ncols+margins[0], 1.-(r+1)*1./nrows+margins[1], 1./ncols-margins[2]-margins[0], 1./nrows-margins[3]-margins[1]) extent = (extent[0]+margins[0], extent[1]+margins[1], extent[2]-margins[2]-margins[0], extent[3]-margins[3]-margins[1]) # Starting to draw : turn interactive off wasinteractive = plt.isinteractive() plt.ioff() try: if map is None: map = np.zeros(12)+np.inf cbar=False map = pixelfunc.ma_to_array(map) ax=PA.HpxMollweideAxes(f,extent,coord=coord,rot=rot, format=format2,flipconv=flip) f.add_axes(ax) if remove_dip: map=pixelfunc.remove_dipole(map,gal_cut=gal_cut, nest=nest,copy=True, verbose=True) elif remove_mono: map=pixelfunc.remove_monopole(map,gal_cut=gal_cut,nest=nest, copy=True,verbose=True) img = ax.projmap(map,nest=nest,xsize=xsize,coord=coord,vmin=min,vmax=max, cmap=cmap,norm=norm) if cbar: im = ax.get_images()[0] b = im.norm.inverse(np.linspace(0,1,im.cmap.N+1)) v = np.linspace(im.norm.vmin,im.norm.vmax,im.cmap.N) if matplotlib.__version__ >= '0.91.0': cb=f.colorbar(im,ax=ax, orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.05,fraction=0.1,boundaries=b,values=v, format=format) else: # for older matplotlib versions, no ax kwarg cb=f.colorbar(im,orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.05,fraction=0.1,boundaries=b,values=v, format=format) cb.solids.set_rasterized(True) ax.set_title(title) if not notext: ax.text(0.86,0.05,ax.proj.coordsysstr,fontsize=14, fontweight='bold',transform=ax.transAxes) if cbar: cb.ax.text(0.5,-1.0,unit,fontsize=14, transform=cb.ax.transAxes,ha='center',va='center') f.sca(ax) finally: if plot: plt.draw() if wasinteractive: plt.ion() #plt.show() if return_projected_map: return img
def anafast( map1, map2=None, nspec=None, lmax=None, mmax=None, iter=3, alm=False, pol=True, use_weights=False, regression=True, datapath=None, ): """Computes the power spectrum of an Healpix map, or the cross-spectrum between two maps if *map2* is given. Parameters ---------- map1 : float, array-like shape (Npix,) or (3, Npix) Either an array representing a map, or a sequence of 3 arrays representing I, Q, U maps map2 : float, array-like shape (Npix,) or (3, Npix) Either an array representing a map, or a sequence of 3 arrays representing I, Q, U maps nspec : None or int, optional The number of spectra to return. If None, returns all, otherwise returns cls[:nspec] lmax : int, scalar, optional Maximum l of the power spectrum (default: 3*nside-1) mmax : int, scalar, optional Maximum m of the alm (default: lmax) iter : int, scalar, optional Number of iteration (default: 3) alm : bool, scalar, optional If True, returns both cl and alm, otherwise only cl is returned pol : bool, optional If True, assumes input maps are TQU. Output will be TEB cl's and correlations (input must be 1 or 3 maps). If False, maps are assumed to be described by spin 0 spherical harmonics. (input can be any number of maps) If there is only one input map, it has no effect. Default: True. regression : bool, scalar, optional If True, map average is removed before computing alm. Default: True. datapath : None or str, optional If given, the directory where to find the weights data. Returns ------- res : array or sequence of arrays If *alm* is False, returns cl or a list of cl's (TT, EE, BB, TE, EB, TB for polarized input map) Otherwise, returns a tuple (cl, alm), where cl is as above and alm is the spherical harmonic transform or a list of almT, almE, almB for polarized input """ map1 = ma_to_array(map1) alms1 = map2alm( map1, lmax=lmax, mmax=mmax, pol=pol, iter=iter, use_weights=use_weights, regression=regression, datapath=datapath, ) if map2 is not None: map2 = ma_to_array(map2) alms2 = map2alm( map2, lmax=lmax, mmax=mmax, pol=pol, iter=iter, use_weights=use_weights, regression=regression, datapath=datapath, ) else: alms2 = None cls = alm2cl(alms1, alms2=alms2, lmax=lmax, mmax=mmax, lmax_out=lmax, nspec=nspec) if alm: if map2 is not None: return (cls, alms1, alms2) else: return (cls, alms1) else: return cls
def cartcontour(map=None,NV=None,fig=None,rot=None,zat=None,coord=None,unit='', xsize=800,ysize=None,lonra=None,latra=None, title='Cartesian view',nest=False,remove_dip=False, remove_mono=False,gal_cut=0, min=None,max=None,flip='astro', format='%.3g',cbar=True, cmap=None, norm=None,aspect=None, hold=False,sub=None,margins=None,notext=False, return_projected_map=False): """Plot an healpix map (given as an array) as contours in Cartesian projection. Parameters ---------- map : float, array-like or None An array containing the map, supports masked maps, see the `ma` function. If None, will display a blank map, useful for overplotting. NV : int, array-like, or None if int, the number of contour levels if an array, the values of the contour levels if None, contour levels will be automatically determined fig : int or None, optional The figure number to use. Default: create a new figure rot : scalar or sequence, optional Describe the rotation to apply. In the form (lon, lat, psi) (unit: degrees) : the point at longitude *lon* and latitude *lat* will be at the center. An additional rotation of angle *psi* around this direction is applied. coord : sequence of character, optional Either one of 'G', 'E' or 'C' to describe the coordinate system of the map, or a sequence of 2 of these to rotate the map from the first to the second coordinate system. unit : str, optional A text describing the unit of the data. Default: '' xsize : int, optional The size of the image. Default: 800 lonra : sequence, optional Range in longitude. Default: [-180,180] latra : sequence, optional Range in latitude. Default: [-90,90] title : str, optional The title of the plot. Default: 'Mollweide view' nest : bool, optional If True, ordering scheme is NESTED. Default: False (RING) min : float, optional The minimum range value max : float, optional The maximum range value flip : {'astro', 'geo'}, optional Defines the convention of projection : 'astro' (default, east towards left, west towards right) or 'geo' (east towards roght, west towards left) remove_dip : bool, optional If :const:`True`, remove the dipole+monopole remove_mono : bool, optional If :const:`True`, remove the monopole gal_cut : float, scalar, optional Symmetric galactic cut for the dipole/monopole fit. Removes points in latitude range [-gal_cut, +gal_cut] format : str, optional The format of the scale label. Default: '%g' cbar : bool, optional Display the colorbar. Default: True notext : bool, optional If True, no text is printed around the map norm : {'hist', 'log', None}, optional Color normalization, hist= histogram equalized color mapping, log= logarithmic color mapping, default: None (linear color mapping) hold : bool, optional If True, replace the current Axes by a CartesianAxes. use this if you want to have multiple maps on the same figure. Default: False sub : int, scalar or sequence, optional Use only a zone of the current figure (same syntax as subplot). Default: None margins : None or sequence, optional Either None, or a sequence (left,bottom,right,top) giving the margins on left,bottom,right and top of the axes. Values are relative to figure (0-1). Default: None return_projected_map : bool if True returns the projected map in a 2d numpy array See Also -------- mollcontour, gnomcontour, orthcontour """ import pylab if not (hold or sub): f=pylab.figure(fig,figsize=(8.5,5.4)) if not margins: margins = (0.075,0.05,0.075,0.05) extent = (0.0,0.0,1.0,1.0) elif hold: f=pylab.gcf() left,bottom,right,top = np.array(pylab.gca().get_position()).ravel() if not margins: margins = (0.0,0.0,0.0,0.0) extent = (left,bottom,right-left,top-bottom) f.delaxes(pylab.gca()) else: # using subplot syntax f=pylab.gcf() if hasattr(sub,'__len__'): nrows, ncols, idx = sub else: nrows, ncols, idx = sub/100, (sub%100)/10, (sub%10) if idx < 1 or idx > ncols*nrows: raise ValueError('Wrong values for sub: %d, %d, %d'%(nrows, ncols, idx)) c,r = (idx-1)%ncols,(idx-1)//ncols if not margins: margins = (0.01,0.0,0.0,0.02) extent = (c*1./ncols+margins[0], 1.-(r+1)*1./nrows+margins[1], 1./ncols-margins[2]-margins[0], 1./nrows-margins[3]-margins[1]) extent = (extent[0]+margins[0], extent[1]+margins[1], extent[2]-margins[2]-margins[0], extent[3]-margins[3]-margins[1]) #f=pylab.figure(fig,figsize=(5.5,6)) # Starting to draw : turn interactive off wasinteractive = pylab.isinteractive() pylab.ioff() try: if map is None: map = np.zeros(12)+np.inf cbar=False map = pixelfunc.ma_to_array(map) if zat and rot: raise ValueError('Only give rot or zat, not both') if zat: rot = np.array(zat,dtype=np.float64) rot.resize(3) rot[1] -= 90 ax=HpxCartesianAxes(f,extent,coord=coord,rot=rot, format=format,flipconv=flip) f.add_axes(ax) if remove_dip: map=pixelfunc.remove_dipole(map,gal_cut=gal_cut,nest=nest,copy=True) elif remove_mono: map=pixelfunc.remove_monopole(map,gal_cut=gal_cut,nest=nest,copy=True) if NV is not None: img, cs = ax.projcontour(map,NV,nest=nest,coord=coord,vmin=min, vmax=max,xsize=xsize,ysize=ysize,lonra=lonra, latra=latra,cmap=cmap,norm=norm,aspect=aspect) else: img, cs = ax.projcontour(map,nest=nest,coord=coord,vmin=min, vmax=max,xsize=xsize,ysize=ysize,lonra=lonra, latra=latra,cmap=cmap,norm=norm,aspect=aspect) if cbar: b = cs.norm.inverse(np.linspace(0,1,cs.cmap.N+1)) v = np.linspace(cs.norm.vmin,cs.norm.vmax,cs.cmap.N) if matplotlib.__version__ >= '0.91.0': cb=f.colorbar(cs,ax=ax, orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.08,fraction=0.1,boundaries=b,values=v, format=format) else: cb=f.colorbar(cs,orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.08,fraction=0.1,boundaries=b,values=v, format=format) cb.solids.set_rasterized(True) ax.set_title(title) if not notext: ax.text(-0.07,0.6,ax.proj.coordsysstr,fontsize=14, fontweight='bold',rotation=90,transform=ax.transAxes) if cbar: cb.ax.text(1.05,0.30,unit,fontsize=14,fontweight='bold', transform=cb.ax.transAxes,ha='left',va='center') f.sca(ax) finally: if wasinteractive: pylab.ion() pylab.draw() #pylab.show() if return_projected_map: return img
def mollcontourf(map=None,NV=None,fig=None,rot=None,coord=None,unit='', xsize=800,title='Mollweide view',nest=False, min=None,max=None,flip='astro', remove_dip=False,remove_mono=False, gal_cut=0, format='%g',format2='%g', cbar=True,cmap=None, notext=False, norm=None,hold=False,margins=None,sub=None, return_projected_map=False): # Create the figure import pylab if not (hold or sub): f=pylab.figure(fig,figsize=(8.5,5.4)) extent = (0.02,0.05,0.96,0.9) elif hold: f=pylab.gcf() left,bottom,right,top = np.array(f.gca().get_position()).ravel() extent = (left,bottom,right-left,top-bottom) f.delaxes(f.gca()) else: # using subplot syntax f=pylab.gcf() if hasattr(sub,'__len__'): nrows, ncols, idx = sub else: nrows, ncols, idx = sub/100, (sub%100)/10, (sub%10) if idx < 1 or idx > ncols*nrows: raise ValueError('Wrong values for sub: %d, %d, %d'%(nrows, ncols, idx)) c,r = (idx-1)%ncols,(idx-1)//ncols if not margins: margins = (0.01,0.0,0.0,0.02) extent = (c*1./ncols+margins[0], 1.-(r+1)*1./nrows+margins[1], 1./ncols-margins[2]-margins[0], 1./nrows-margins[3]-margins[1]) extent = (extent[0]+margins[0], extent[1]+margins[1], extent[2]-margins[2]-margins[0], extent[3]-margins[3]-margins[1]) #extent = (c*1./ncols, 1.-(r+1)*1./nrows,1./ncols,1./nrows) #f=pylab.figure(fig,figsize=(8.5,5.4)) # Starting to draw : turn interactive off wasinteractive = pylab.isinteractive() pylab.ioff() try: if map is None: map = np.zeros(12)+np.inf cbar=False map = pixelfunc.ma_to_array(map) ax=HpxMollweideAxes(f,extent,coord=coord,rot=rot, format=format2,flipconv=flip) f.add_axes(ax) if remove_dip: map=pixelfunc.remove_dipole(map,gal_cut=gal_cut, nest=nest,copy=True, verbose=True) elif remove_mono: map=pixelfunc.remove_monopole(map,gal_cut=gal_cut,nest=nest, copy=True,verbose=True) if NV is not None: img, cs = ax.projcontourf(map,NV,nest=nest,xsize=xsize,coord=coord,vmin=min,vmax=max, cmap=cmap,norm=norm) else: img, cs = ax.projcontourf(map,nest=nest,xsize=xsize,coord=coord,vmin=min,vmax=max, cmap=cmap,norm=norm) if cbar: b = cs.norm.inverse(np.linspace(0,1,cs.cmap.N+1)) v = np.linspace(cs.norm.vmin,cs.norm.vmax,cs.cmap.N) if matplotlib.__version__ >= '0.91.0': cb=f.colorbar(cs,ax=ax, orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.05,fraction=0.1,boundaries=b,values=v, format=format) else: # for older matplotlib versions, no ax kwarg cb=f.colorbar(cs,orientation='horizontal', shrink=0.5,aspect=25,ticks=PA.BoundaryLocator(), pad=0.05,fraction=0.1,boundaries=b,values=v, format=format) #cb.solids.set_rasterized(True) ax.set_title(title) if not notext: ax.text(0.86,0.05,ax.proj.coordsysstr,fontsize=14, fontweight='bold',transform=ax.transAxes) if cbar: cb.ax.text(0.5,-1.0,unit,fontsize=14, transform=cb.ax.transAxes,ha='center',va='center') f.sca(ax) #graticule(dpar=90, dmer=180.0, verbose=False) finally: pylab.draw() if wasinteractive: pylab.ion() #pylab.show() if return_projected_map: return img
def anafast(map1, map2=None, nspec=None, lmax=None, mmax=None, iter=3, alm=False, pol=True, use_weights=False, regression=True, datapath=None): """Computes the power spectrum of an Healpix map, or the cross-spectrum between two maps if *map2* is given. Parameters ---------- map1 : float, array-like shape (Npix,) or (3, Npix) Either an array representing a map, or a sequence of 3 arrays representing I, Q, U maps map2 : float, array-like shape (Npix,) or (3, Npix) Either an array representing a map, or a sequence of 3 arrays representing I, Q, U maps nspec : None or int, optional The number of spectra to return. If None, returns all, otherwise returns cls[:nspec] lmax : int, scalar, optional Maximum l of the power spectrum (default: 3*nside-1) mmax : int, scalar, optional Maximum m of the alm (default: lmax) iter : int, scalar, optional Number of iteration (default: 3) alm : bool, scalar, optional If True, returns both cl and alm, otherwise only cl is returned pol : bool, optional If True, assumes input maps are TQU. Output will be TEB cl's and correlations (input must be 1 or 3 maps). If False, maps are assumed to be described by spin 0 spherical harmonics. (input can be any number of maps) If there is only one input map, it has no effect. Default: True. regression : bool, scalar, optional If True, map average is removed before computing alm. Default: True. datapath : None or str, optional If given, the directory where to find the weights data. Returns ------- res : array or sequence of arrays If *alm* is False, returns cl or a list of cl's (TT, EE, BB, TE, EB, TB for polarized input map) Otherwise, returns a tuple (cl, alm), where cl is as above and alm is the spherical harmonic transform or a list of almT, almE, almB for polarized input """ map1 = ma_to_array(map1) alms1 = map2alm(map1, lmax=lmax, mmax=mmax, pol=pol, iter=iter, use_weights=use_weights, regression=regression, datapath=datapath) if map2 is not None: map2 = ma_to_array(map2) alms2 = map2alm(map2, lmax=lmax, mmax=mmax, pol=pol, iter=iter, use_weights=use_weights, regression=regression, datapath=datapath) else: alms2 = None cls = alm2cl(alms1, alms2=alms2, lmax=lmax, mmax=mmax, lmax_out=lmax, nspec=nspec) if alm: if map2 is not None: return (cls, alms1, alms2) else: return (cls, alms1) else: return cls
def map2alm(maps, lmax=None, mmax=None, iter=3, pol=True, use_weights=False, regression=True, datapath=None): """Computes the alm of an Healpix map. Parameters ---------- maps : array-like, shape (Npix,) or (n, Npix) The input map or a list of n input maps. lmax : int, scalar, optional Maximum l of the power spectrum. Default: 3*nside-1 mmax : int, scalar, optional Maximum m of the alm. Default: lmax iter : int, scalar, optional Number of iteration (default: 3) pol : bool, optional If True, assumes input maps are TQU. Output will be TEB alm's. (input must be 1 or 3 maps) If False, apply spin 0 harmonic transform to each map. (input can be any number of maps) If there is only one input map, it has no effect. Default: True. use_weights: bool, scalar, optional If True, use the ring weighting. Default: False. regression: bool, scalar, optional If True, subtract map average before computing alm. Default: True. datapath : None or str, optional If given, the directory where to find the weights data. Returns ------- alms : array or tuple of array alm or a tuple of 3 alm (almT, almE, almB) if polarized input. Notes ----- The pixels which have the special `UNSEEN` value are replaced by zeros before spherical harmonic transform. They are converted back to `UNSEEN` value, so that the input maps are not modified. Each map have its own, independent mask. """ maps = ma_to_array(maps) info = maptype(maps) if pol or info in (0, 1): alms = _sphtools.map2alm(maps, niter=iter, regression=regression, datapath=datapath, use_weights=use_weights, lmax=lmax, mmax=mmax) else: # info >= 2 and pol is False : spin 0 spht for each map alms = [ _sphtools.map2alm(mm, niter=iter, regression=regression, datapath=datapath, use_weights=use_weights, lmax=lmax, mmax=mmax) for mm in maps ] return alms