def load_signal(ctx): """ Models effect of the horizon :param ctx: context object containing params :returns earth_signal: healpy map with the signal """ nside = ctx.params.beam_nside idx = np.arange(hp.nside2npix(nside)) thetas, _ = hp.pix2ang(nside, idx) dec = sphere.theta2dec(thetas) min_mask = (dec<ctx.params.vmin) max_mask = (dec>ctx.params.vmax) mask = min_mask | max_mask earth_signal = np.zeros(hp.nside2npix(nside)) fit = np.poly1d(ctx.params.fit_coeffs) earth_signal[~mask] = fit(dec[~mask]) earth_signal[min_mask] = fit(ctx.params.vmin) earth_signal[max_mask] = fit(ctx.params.vmax) earth_signal[:] = ctx.params.log_base**earth_signal return earth_signal
def fisher_single(par,v) : # v -> seen pixels nb=len(par.bins)-1 npix=hp.nside2npix(par.nside) npix_seen=len(par.ip_seen) lmax=3*par.nside-1 larr=np.arange(lmax+1) fisher=np.zeros([nb,nb]) pixsize=4*np.pi/hp.nside2npix(par.nside) v_map=np.zeros(npix); v_map[par.ip_seen]=v vcm1=invert_covar(par,v_map) v_lm=hp.map2alm(v_map,iter=0) vcm1_lm=hp.map2alm(vcm1,iter=0) for iba in np.arange(nb) : # print " Row %d"%iba transfer=np.zeros(lmax+1); transfer[par.bins[iba]:par.bins[iba+1]]=1. v_map2=hp.alm2map(hp.almxfl(v_lm,transfer),par.nside,verbose=False)/pixsize #Q_a * v v_map2cm1=invert_covar(par,v_map2) #C^-1 * Q_a * v va_lm=hp.map2alm(v_map2cm1,iter=0) cl_vcm1_va=(2*larr+1)*hp.alm2cl(vcm1_lm,alms2=va_lm) for ibb in np.arange(nb-iba)+iba : fisher[iba,ibb]=np.sum(cl_vcm1_va[par.bins[ibb]:par.bins[ibb+1]])/pixsize**2 if iba!=ibb : fisher[ibb,iba]=fisher[iba,ibb] return fisher
def noiser(self): """Calculate white noise maps for given sensitivities. Returns signal + noise, and noise maps at the given nside in (T, Q, U). Input sensitivities are expected to be in uK_CMB amin for the rest of PySM. :param map_array: array of maps to which we add noise. :type map_array: numpy.ndarray. :return: map plus noise, and noise -- numpy.ndarray """ try: npix = len(self.pixel_indices) except TypeError: npix = hp.nside2npix(self.Nside) if not self.Add_Noise: return np.zeros((len(self.Sens_I), 3, npix)) elif self.Add_Noise: # solid angle per pixel in amin2 pix_amin2 = 4. * np.pi / float(hp.nside2npix(self.Nside)) * (180. * 60. / np.pi) ** 2 """sigma_pix_I/P is std of noise per pixel. It is an array of length equal to the number of input maps.""" sigma_pix_I = np.sqrt(self.Sens_I ** 2 / pix_amin2) sigma_pix_P = np.sqrt(self.Sens_P ** 2 / pix_amin2) np.random.seed(seed = self.Noise_Seed) noise = np.random.randn(len(self.Sens_I), 3, npix) noise[:, 0, :] *= sigma_pix_I[:, None] noise[:, 1, :] *= sigma_pix_P[:, None] noise[:, 2, :] *= sigma_pix_P[:, None] return noise else: print("Please set 'Add_Noise' in Instrument object.") sys.exit(1)
def get_gsm_cube(self): if os.path.exists('data4/paper/'): sys.path.append('/data4/paper/zionos/polskysim') else: sys.path.append('/lustre/aoc/projects/hera/zmartino/zionos/polskysim') import gsm2016_mod # import astropy.coordinates as coord # import astropy.units as units nside_in = 64 npix_in = hp.nside2npix(nside_in) I_gal = np.zeros((self.nfreq, npix_in)) for fi, f in enumerate(self.nu_axis): I_gal[fi] = gsm2016_mod.get_gsm_map_lowres(Hz2GHz(f)) x_c = np.array([1.,0,0]) # unit vectors to be transformed by astropy y_c = np.array([0,1.,0]) z_c = np.array([0,0,1.]) # The GSM is given in galactic coordinates. We will rotate it to J2000 equatorial coordinates. axes_icrs = coord.SkyCoord(x=x_c, y=y_c, z=z_c, frame='icrs', representation='cartesian') axes_gal = axes_icrs.transform_to('galactic') axes_gal.representation = 'cartesian' R = np.array(axes_gal.cartesian.xyz) # The 3D rotation matrix that defines the coordinate transformation. npix_out = hp.nside2npix(self.nside) I = np.zeros((self.nfreq, npix_out)) for i in range(self.nfreq): I[i] = irf.rotate_healpix_map(I_gal[i], R) I[i] = irf.harmonic_ud_grade(I[i], nside_in, self.nside) return I
def readSparseHealpixMap(infile, field, extension='PIX_DATA', default_value=healpy.UNSEEN, construct_map=True): """ Open a sparse HEALPix map fits file. Convert the contents into a HEALPix map or simply return the contents. Flexibility to handle """ reader = pyfits.open(infile,memmap=False) nside = reader[extension].header['NSIDE'] # Trying to fix avoid a memory leak pix = numpy.array(reader[extension].data.field('PIX'),copy=True) value = numpy.array(reader[extension].data.field(field),copy=True) reader.close() if construct_map: if len(value.shape) == 1: map = default_value * numpy.ones(healpy.nside2npix(nside)) map[pix] = value else: map = default_value * numpy.ones([value.shape[1], healpy.nside2npix(nside)]) for ii in range(0, value.shape[1]): map[ii][pix] = numpy.take(value, [ii], axis=1) ret = map else: if len(value.shape) == 1: ret = (pix,value) else: ret = (pix,value.transpose()) return ret
def healpixellize(f_in,theta_in,phi_in,nside,fancy=False): """ A dumb method for converting data f sampled at points theta and phi (not on a healpix grid) into a healpix at resolution nside """ # Input arrays are likely to be rectangular, but this is inconvenient f = f_in.flatten() theta = theta_in.flatten() phi = phi_in.flatten() pix = hp.ang2pix(nside,theta,phi) map = np.zeros(hp.nside2npix(nside)) hits = np.zeros(hp.nside2npix(nside)) # Simplest gridding is map[pix] = val. This tries to do some #averaging Better would be to do some weighting by distance from #pixel center or something ... if (fancy): for i,v in enumerate(f): # Find the nearest pixels to the pixel in question neighbours,weights = hp.get_neighbours(nside,theta[i],phi[i]) # Add weighted values to map map[neighbours] += v*weights # Keep track of weights hits[neighbours] += weights map = map/hits wh_no_hits = np.where(hits == 0) map[wh_no_hits[0]] = hp.UNSEEN else: for i,v in enumerate(f): map[pix[i]] += v hits[pix[i]] +=1 map = map/hits return map
def grid_theta_phi_to_healpix(theta,phi,inbeam): """ inputs: theta (angle down from pixel 0, deg) phi (CW longitude angle, deg) inbeam: input beam values matching the corresponding theta and phi coords """ print len(theta),len(phi),len(inbeam) nside = hp.npix2nside(len(inbeam)) pixes = hp.ang2pix(nside,theta,phi) beam = np.zeros(hp.nside2npix(nside)) rms = np.zeros(hp.nside2npix(nside)) counts = np.zeros(hp.nside2npix(nside)) for i,pix in enumerate(pixes): beam[pix] += inbeam[i] rms[pix] += inbeam[i]**2 counts[pix] += 1 beam[counts>0] /= counts[counts>0] rms[counts>0] /= counts[counts>0] rms -= beam**2 rms = np.sqrt(rms) beam[counts==0] = hp.UNSEEN counts[counts==0] = hp.UNSEEN rms[counts==0] = hp.UNSEEN return beam,rms,counts
def to_healpix(self, n_side=32): """ Convert beam pattern to a healpix Returns ------- hmap: np.array Numpy array representing healpix map. Array is in observer frame, i.e. zenith aligned with equatorial coordinate system. """ beam_azel = self.to_azel() el = np.linspace(0, np.pi, beam_azel.shape[1]) az = np.linspace(-np.pi, np.pi, beam_azel.shape[0])[:, None] # Generate beam pattern with n_side = 32 (for gridding reasons) hmap = hp.ma(np.zeros(hp.nside2npix(32))) pix = hp.ang2pix(32, el, az) hmap[pix] = beam_azel # Upsample if required if n_side != 32: hmap = hp.ud_grade(hmap, nside_out=n_side) # Apply mask n_pix = hp.nside2npix(n_side) theta, phi = hp.pix2ang(n_side, np.arange(n_pix)) mask1 = phi + np.pi/2 > 2 * np.pi mask2 = phi < np.pi / 2 hmap.mask = np.invert(np.logical_or(mask1, mask2)) return hmap
def see_mapper(catalog_dir, nside, out_map): # create empty map hmap = np.zeros(hp.nside2npix(nside)) npix = hp.nside2npix(nside) pix_seeing = np.zeros(npix) pix_totalcounts = np.zeros(npix) for cat in listdir(catalog_dir): if cat.endswith(".csv") and cat.startswith("with"): # read catalog, cols [ra,dec,psffwhm_i] c = pandas.read_csv(join(catalog_dir, cat), sep=',', header=0, dtype={'ra' : np.float64, 'dec' : np.float64, "psffwhm_i" : np.float64}, engine=None, usecols=[1,2,292]) ra = c["ra"] dec = c["dec"] seeing = c["psffwhm_i"] # generate object pixel_IDs & pixel_counts theta = np.deg2rad(90.0 - dec) phi = np.deg2rad(ra) pix_IDs = np.array(hp.ang2pix(nside, theta, phi, nest=False)) pix_counts = np.bincount(pix_IDs, minlength=npix) assert len(pix_counts) == npix, ("pixel numbers mismatched") # calculate "total" seeing in each pixel for (i, pix) in enumerate(pix_IDs): pix_seeing[pix] += seeing[i] pix_totalcounts[pix] += 1 del c, ra, dec, seeing, pix_counts, pix_IDs gc.collect() pix_avg_seeing = pix_seeing/pix_totalcounts # map seeing hp.write_map(out_map, pix_avg_seeing) # CHANGE FILENAMES????
def lunarOcclusion(lunDec, lunRA, nside, dead_zone, coord="C"): ''' Find the accesible part of the sky in equatorial coordinate. "t" must be specified in gps seconds and the "dead_zone" must be specified in radians. returns a mask which corresponds to only those parts of the sky that are further than "dead_zone" from the sun at this time adapted from Hsin-Yu Chen's code ''' if coord!="C": print "we only know how to handle coord=\"C\" at the moment. Returning a mask that removes nothing..." return np.ones((hp.nside2npix(nside),), dtype="int") npix = hp.nside2npix( nside ) ### get solar position in spherical coordinate lunTheta = pi2 - lunDec ### get the cartesian coordinates of all the other pixels pix = np.arange( npix ) theta, phi = hp.pix2ang( nside, pix ) ### compute cos(theta) between all pixels and the sun in spherical coordinates cosdtheta = np.cos(lunTheta)*np.cos(theta) + np.sin(lunTheta)*np.sin(theta)*np.cos(lunRA-phi) return (cosdtheta <= np.cos(dead_zone)).astype(int)
def setUp(self): self.nside = 1024 sigma_T = 4. sigma_P = np.sqrt(2.) * sigma_T self.instrument_config = { 'frequencies' : np.array([23.]), 'sens_I' : np.array([sigma_T]), 'sens_P' : np.array([sigma_P]), 'nside' : self.nside, 'noise_seed' : 1234, 'use_bandpass' : False, 'add_noise' : True, 'output_units' : 'uK_CMB', 'use_smoothing' : False, 'output_directory' : os.path.dirname(os.path.abspath(__file__)), 'output_prefix' : 'test', } s1 = models("s1", self.nside) s1[0]['A_I'] = np.zeros(hp.nside2npix(self.nside)) s1[0]['A_Q'] = np.zeros(hp.nside2npix(self.nside)) s1[0]['A_U'] = np.zeros(hp.nside2npix(self.nside)) sky_config = {'synchrotron' : s1} self.sky = pysm.Sky(sky_config) pix2amin = np.sqrt(4. * np.pi * (180. / np.pi * 60.) ** 2 / float(hp.nside2npix(self.nside))) self.expected_T_std = sigma_T / pix2amin self.expected_P_std = sigma_P / pix2amin self.test_file = get_testdata("test_nu0023p00GHz_noise_nside%04d.fits"%self.nside)
def select_random_pts(nran,hpmap,rannside=262144,masknside=4096): """ This function does the work on each processor for create_random_cat(). Options are passed from that function. """ import healpy as hp import numpy.random as rand ranmap=hp.nside2npix(rannside) print ranmap hpmin=np.min(hpmap) hpmax=np.max(hpmap) tmp0=hp.nside2npix(rannside)//hp.nside2npix(masknside) ran=[] while len(ran)<nran: print nran,len(ran) tmp=rand.randint(hpmin*tmp0,high=hpmax*tmp0,size=nran) mask=np.in1d(tmp//tmp0,hpmap,assume_unique=False) ran=np.append(ran,tmp[mask]) ran=ran[:nran] dec,ra=hp.pix2ang(rannside,ran.astype(int),nest=True) dec=90.-dec*180./np.pi ra=ra*180./np.pi return ra,dec,ran
def RebinMap(map,nside,nest=True): # # Convert map from nside0 to nside for nside0 < nside # import healpy as hp import numpy as np nside0 = hp.npix2nside(len(map)) if nside0 <= nside: print 'ERROR in RebinMap : nside0 >= nside, should call ChangeNside instead.' return pix = np.arange(len(map)) ang = hp.pix2ang(nside0,pix,nest=nest) nmap = np.zeros(hp.nside2npix(nside)) nentries = np.zeros(hp.nside2npix(nside)) old2newpix = hp.ang2pix(nside,ang[0],ang[1],nest=nest) for ip,ival in zip(old2newpix,map): # if ival!=0: nmap[ip] += ival nentries[ip] += 1 nmap[nentries>0]/=nentries[nentries>0] return nmap
def __init__(self,cat,nside,theta_apo) : """ Initializes Mask object from catalog and resolution parameters Parameters ---------- cat : fc.Catalog Catalog containing window information nside : int HEALPix resolution index theta_apo : float Apodization scale in degrees """ if cat.window.typestr=='decbcut': npix=hp.nside2npix(nside) ind_aux=np.arange(0,npix) theta,phi=hp.pix2ang(nside,ind_aux) self.weights=cat.window(phi*180/np.pi,90-theta*180/np.pi) elif cat.window.typestr=='base': self.weights=np.ones(12*nside**2) else: self.weights=cat.window.map.copy() if debug: hp.mollview(self.weights) plt.show() #Figure out which pixels are empty self.ip_nomask=np.where(self.weights>0)[0] #Generate sky mask and apply apodization self.binary=np.zeros_like(self.weights) self.binary[self.ip_nomask]=1. if theta_apo>0 : self.mask_apo=nmt.mask_apodization(self.binary,theta_apo,apotype='C1') else : self.mask_apo=self.binary.copy() #Final weights are a combination of mask + depth fluctuations self.total=self.mask_apo*self.weights #Can't generate maps with pixels larger than the mask if (cat.window.typestr!='decbcut') & (cat.window.typestr!='base'): if nside<cat.window.nside: #If mask pixelization is higher, upgrade output maps self.nside=cat.window.nside else : #If mask pixelization is lower, upgrade mask self.nside=nside self.total =hp.ud_grade(self.total ,nside_out=nside) self.binary =hp.ud_grade(self.binary ,nside_out=nside) self.weights=hp.ud_grade(self.weights,nside_out=nside) self.ip_nomask=np.where(self.weights>0)[0] else: self.nside=nside self.total =hp.ud_grade(self.total ,nside_out=nside) self.binary =hp.ud_grade(self.binary ,nside_out=nside) self.weights=hp.ud_grade(self.weights,nside_out=nside) self.ip_nomask=np.where(self.weights>0)[0] #Pixel area self.pixOmega=4*np.pi/hp.nside2npix(self.nside) if debug: hp.mollview(self.weights) plt.show()
def pixelsize(nside,arcmin=True): """ Given a `nside` :mod:`healpy` gridding parameter returns the pixel size of the chosen pixelization in arcmin (or in radians if `arcmin= False`) """ if arcmin: return np.sqrt(4./np.pi /hp.nside2npix(nside))*(180*60.) else : return np.sqrt(4.*np.pi /hp.nside2npix(nside))
def export_fits_prisim(self,fitsfile,pol_list,freq_list,scheme='RING',nside_out=None): ''' export fits-file at channel chan and polarization pol Args: fitsfile, str, name of file to save .fits to pol_list, list of labels of polarizations to write chan_list, list of frequencies to write ''' if nside_out is None: nside_out=self.nside pol_list=n.array(pol_list) freq_list=n.array(freq_list) pol_inds=[] freq_inds=[] for pol in pol_list: assert pol in self.pols pol_inds.append(n.where(n.array(self.pols)==pol)[0][0]) for freq in freq_list: assert freq in self.fAxis freq_inds.append(n.where(n.array(self.fAxis)==freq)[0][0]) data=self.data[:,freq_inds,:].reshape(-1,1) theta_out,phi_out=hp.pix2ang(nside_out,n.arange(hp.nside2npix(nside_out))) #freq_col=[fits.Column(name='Frequency [MHz]',format='D',array=n.array(freq_list))] #freq_columns=fits.ColDefs(freq_col,ascii=False) #freq_tbhdu = fits.BinTableHDU.from_columns(freq_col) #freq_tbhdu = fits.BinTableHDU.from_columns(n.array(freq_list)) hduprimary=fits.PrimaryHDU() hduprimary.header.set('EXTNAME','PRIMARY') hduprimary.header.set('NEXTEN',2) hduprimary.header.set('FITSTYPE','IMAGE') hduprimary.header['NSIDE']=(nside_out,'NSIDE') hduprimary.header['PIXAREA']=(hp.nside2pixarea(nside_out),'pixel solid angle (steradians)') hduprimary.header['NEXTEN']=(2,'Number of extensions') hduprimary.header['NPOL'] = (len(pol_inds), 'Number of polarizations') hduprimary.header['SOURCE'] = ('HERA-CST', 'Source of data') hdulist=[hduprimary] fits.HDUList(hdulist).writeto(fitsfile,clobber=True) for pol in pol_list: #freq_tbhdu.header.set('EXTNAME','FREQS_{0}'.format(pol)) freq_tbhdu=fits.ImageHDU(freq_list,name='FREQS_{0}'.format(pol)) fits.append(fitsfile,freq_tbhdu.data,freq_tbhdu.header,verify=False) data_interp=n.zeros((hp.nside2npix(nside_out),len(freq_inds))) for polind,pol in zip(pol_inds,pol_list): for fi,freqind in enumerate(freq_inds): data=self.data[polind,freqind,:].flatten() data_interp[:,fi]=hp.get_interp_val(data,theta_out,phi_out) #if DEBUG: # hp.mollview(data_interp[:,fi]) # plt.show() imghdu = fits.ImageHDU(data_interp, name='BEAM_{0}'.format(pol)) imghdu.header['PIXTYPE'] = ('HEALPIX', 'Type of pixelization') imghdu.header['ORDERING'] = (scheme, 'Pixel ordering scheme, either RING or NESTED') imghdu.header['NSIDE'] = (nside_out, 'NSIDE parameter of HEALPIX') imghdu.header['NPIX'] = (hp.nside2npix(nside_out), 'Number of HEALPIX pixels') imghdu.header['FIRSTPIX'] = (0, 'First pixel # (0 based)') imghdu.header['LASTPIX'] = (len(data_interp)-1, 'Last pixel # (0 based)') fits.append(fitsfile,imghdu.data,imghdu.header,verify=False)
def mk_map_gsm2016_mod(freqs, nside_out): import sys sys.path.append('/data4/paper/zionos/polskysim') import gsm2016_mod import astropy.coordinates as coord import astropy.units as units def harmonic_ud_grade(m, nside_in, nside_out): """ Decompose a map at a resolution nside_in into spherical harmonic components and then resynthesize the map at nside_out. """ lmax = 3 * nside_in - 1 alm = hp.map2alm(m, lmax=lmax) return hp.alm2map(alm, nside_out, lmax=lmax, verbose=False) def rotate_healpix_map(m, R): """ Performs a scalar rotation of the map relative to the Healpix coordinate frame by interpolating the map at the coordinates of new coordinate frame. """ npix = len(m) nside = hp.npix2nside(npix) hpxidx = np.arange(npix) c, a = hp.pix2ang(nside, hpxidx) t, p = rotate_sphr_coords(R, c, a) return hp.get_interp_val(m, t, p) nside_in = 64 npix_in = hp.nside2npix(nside_in) nfreq = len(freqs) npix_out = hp.nside2npix(nside_out) I_gal = np.zeros((nfreq, npix_in)) for fi, f in enumerate(freqs): I_gal[fi] = gsm2016_mod.get_gsm_map_lowres(f/1e3) # freqs is in MHz, gsm2016 generates x_c = np.array([1.,0,0]) # unit vectors to be transformed by astropy y_c = np.array([0,1.,0]) z_c = np.array([0,0,1.]) # The GSM is given in galactic coordinates. We will rotate it to J2000 equatorial coordinates. axes_icrs = coord.SkyCoord(x=x_c, y=y_c, z=z_c, frame='icrs', representation='cartesian') axes_gal = axes_icrs.transform_to('galactic') axes_gal.representation = 'cartesian' R = np.array(axes_gal.cartesian.xyz) # The 3D rotation matrix that defines the coordinate transformation. npix_out = hp.nside2npix(nside_out) I = np.zeros((nfreq, npix_out)) for i in range(nfreq): I[i] = rotate_healpix_map(I_gal[i], R) if nside_out != nside_in: I[i] = harmonic_ud_grade(I_gal[i], nside_in, nside_out) return I
def __init__(self): ''' Initiate a dummy extinction map with nside=1. ''' self.nested = True self.nside = 1 self.Ar = np.zeros((1, hp.nside2npix(self.nside)), dtype=np.float64) self.mu = np.zeros(1, dtype=np.float64) self.N_stars = np.zeros(hp.nside2npix(self.nside), dtype=np.uint32) self.measure = np.zeros(hp.nside2npix(self.nside), dtype=np.float64)
def toy_background(self,mc_source_id=2,seed=None): """ Quick uniform background generation. """ logger.info("Running toy background simulation...") size = 20000 nstar = np.random.poisson(size) #np.random.seed(0) logger.info("Simulating %i background stars..."%nstar) ### # Random points from roi pixels ### idx = np.random.randint(len(self.roi.pixels)-1,size=nstar) ### pix = self.roi.pixels[idx] # Random points drawn from subpixels logger.info("Generating uniform positions...") idx = np.random.randint(0,len(self.subpix)-1,size=nstar) lon,lat = pix2ang(self.nside_subpixel,self.subpix[idx]) pix = ang2pix(self.nside_pixel, lon, lat) lon,lat = pix2ang(self.nside_pixel,pix) # Single color #mag_1 = 19.05*np.ones(len(pix)) #mag_2 = 19.10*np.ones(len(pix)) # Uniform in color logger.info("Generating uniform CMD...") mag_1 = np.random.uniform(self.config['mag']['min'],self.config['mag']['max'],size=nstar) color = np.random.uniform(self.config['color']['min'],self.config['color']['max'],size=nstar) mag_2 = mag_1 - color # There is probably a better way to do this step without creating the full HEALPix map mask = -1. * numpy.ones(healpy.nside2npix(self.nside_pixel)) mask[self.roi.pixels] = self.mask.mask_1.mask_roi_sparse mag_lim_1 = mask[pix] mask = -1. * numpy.ones(healpy.nside2npix(self.nside_pixel)) mask[self.roi.pixels] = self.mask.mask_2.mask_roi_sparse mag_lim_2 = mask[pix] #mag_err_1 = 1.0*np.ones(len(pix)) #mag_err_2 = 1.0*np.ones(len(pix)) mag_err_1 = self.photo_err_1(mag_lim_1 - mag_1) mag_err_2 = self.photo_err_2(mag_lim_2 - mag_2) mc_source_id = mc_source_id * numpy.ones(len(mag_1)) select = (mag_lim_1>mag_1)&(mag_lim_2>mag_2) hdu = ugali.observation.catalog.makeHDU(self.config,mag_1[select],mag_err_1[select], mag_2[select],mag_err_2[select], lon[select],lat[select],mc_source_id[select]) catalog = ugali.observation.catalog.Catalog(self.config, data=hdu.data) return catalog
def radius2skyfrac(radius, nside=256): """Calculate sky fraction for a circular cap on the sky w/ specified radius (in radians).""" pix = _np.array(range(_hp.nside2npix(nside))) theta, phi = _hp.pix2ang(nside, pix) theta0 = _np.radians(90.0) phi0 = _np.radians(0.0) d = gcdist(theta, phi, theta0, phi0) frac = float(len(_np.where(d < radius)[0]))/float(_hp.nside2npix(nside)) return frac
def __init__(self, NSIDE, lmax=10, clv=True): """ Args: NSIDE (int) : the healpix NSIDE parameter, must be a power of 2, less than 2**30 npix (int) : number of pixel in the X and Y axis of the final projected map rot_velocity (float) : rotation velocity of the star in the equator in km/s Returns: None """ self.NSIDE = int(NSIDE) self.hp_npix = hp.nside2npix(NSIDE) self.lmax = lmax self.n_coef_max = (1+lmax)**2 self.epsilon = 1e-3 self.l = np.arange(self.lmax+1) self.alpha = np.zeros(self.n_coef_max) self.beta = np.zeros(self.n_coef_max) self.gamma = np.zeros(self.n_coef_max) # self.rot_velocity = rot_velocity self.clv = clv # Generate the indices of all healpix pixels self.indices = np.arange(hp.nside2npix(NSIDE), dtype='int') self.n_healpix_pxl = len(self.indices) self.polar_angle, self.azimuthal_angle = hp.pixelfunc.pix2ang(self.NSIDE, np.arange(self.n_healpix_pxl)) self.pixel_vectors = np.array(hp.pixelfunc.pix2vec(self.NSIDE, self.indices)) # Compute LOS rotation velocity as v=w x r self.rotation_velocity = np.cross(np.array([0.0,0.0,1.0])[:,None], self.pixel_vectors, axisa=0, axisb=0, axisc=0) self.vec_boundaries = np.zeros((3,4,self.n_healpix_pxl)) for i in range(self.n_healpix_pxl): self.vec_boundaries[:,:,i] = hp.boundaries(self.NSIDE, i) self.Plm = np.zeros((self.lmax+2, self.lmax+2, self.n_healpix_pxl)) self.dPlm = np.zeros((self.lmax+2, self.lmax+2, self.n_healpix_pxl)) self.Clm = np.zeros((self.lmax+2, self.lmax+2)) for i in range(self.n_healpix_pxl): self.Plm[:,:,i], self.dPlm[:,:,i] = sp.lpmn(self.lmax+1, self.lmax+1, np.cos(self.polar_angle[i])) self.dPlm[:,:,i] *= -np.sin(self.polar_angle[i]) for l in range(self.lmax+2): for m in range(0, l+1): self.Clm[m,l] = np.sqrt((2.0*l+1) / (4.0*np.pi) * mi.factorial(l-m) / mi.factorial(l+m)) self.lambda0 = 5000.0 self.lande = 1.2 self.constant = -4.6686e-13 * self.lambda0 * self.lande * 3e5
def mapper1(catalog_dir, nside, out_dir): # create empty map hmap = np.zeros(hp.nside2npix(nside)) npix = hp.nside2npix(nside) num_s = 0 for cat in listdir(catalog_dir): if cat.endswith(".csv") and cat.startswith("with"): # read catalog c = pandas.read_csv(join(catalog_dir, cat), sep=',', header=0, dtype={'ra' : np.float64, 'dec' : np.float64, 'modelMag_i' : np.float64, 'modelMag_r' : np.float64, 'extinction_i' : np.float64, 'extinction_r' : np.float64}, engine=None, usecols=['ra', 'dec', 'clean', 'type', 'modelMag_i', 'modelMag_r', 'extinction_i'. 'extinction_r']) ra = c["ra"] dec = c["dec"] i = np.array(c["modelMag_i"] - c["extinction_i"]) r = np.array(c["modelMag_r"] - c["extinction_r"]) r_cut = np.where((r > 18.0) & (r < 18.5), True, False) i_cut = i < 21.3 cleancut = c["clean"] == True typecut = c["type"] == 6 totalcut = cleancut & typecut & r_cut & i_cut ra = ra[totalcut] dec = dec[totalcut] # ifib2 = np.array(ifib2[totalcut]) # star count num_s += len(ra) # generate theta/phi vectors theta = np.deg2rad(90.0 - dec) phi = np.deg2rad(ra) # generate corresponding pixel_IDs pix_IDs = hp.ang2pix(nside, theta, phi, nest=False) # distribute stars according to pixel_ID cmap = np.bincount(pix_IDs, minlength=npix) assert len(cmap) == npix, ("pixel numbers mismatched") # sum to hmap hmap = cmap # assign filenames & write to file out_filename = "countmap_" + cat[:-4] + ".fits" hp.write_map(join(out_dir, out_filename), hmap) del c, ra, dec, cleancut, typecut, r_cut, i_cut gc.collect() # print("num_s =", num_s) # count = open(join(out_dir, "count.txt"), "w") # count.write(str(num_s)) # count.close() return None
def test(): fname = '/Users/bl/Dropbox/Projects/Quicksip/data/SVA1_COADD_ASTROM_PSF_INFO.fits' #fname = '/Users/bl/Dropbox/Projects/Quicksip/data/Y1A1_IMAGEINFO_and_COADDINFO.fits' pixoffset = 10 hdulist = pyfits.open(fname) tbdata = hdulist[1].data hdulist.close() nside = 1024 ratiores = 4 treemap = HealTree(nside) #results = pool.map(treemap.addElem, [imagedata for imagedata in tbdata]) print tbdata.dtype #ind = np.ndarray([0]) ind = np.where( tbdata['band'] == 'i' ) import numpy.random ind = numpy.random.choice(ind[0], 1 ) print 'Number of images :', len(ind) hpxmap = np.zeros(hp.nside2npix(nside)) ras_c = [] decs_c = [] for i, propertyArray in enumerate(tbdata[ind]): ras_c.append(propertyArray['RA']) decs_c.append(propertyArray['DEC']) plt.figure() for i, propertyArray in enumerate(tbdata[ind]): print i propertyArray.dtype = tbdata.dtype listpix, weights, thetas_c, phis_c, listpix_sup = computeHPXpix_sequ_new(nside, propertyArray, pixoffset=pixoffset, ratiores=ratiores) #listpix2, weights2, thetas_c2, phis_c2 = computeHPXpix_sequ(nside, propertyArray, pixoffset=pixoffset, ratiores=ratiores) hpxmap = np.zeros(hp.nside2npix(nside)) hpxmap[listpix] = weights hpxmap_sup = np.zeros(hp.nside2npix(ratiores*nside)) hpxmap_sup[listpix_sup] = 1.0 listpix_hi, weights_hi, thetas_c_hi, phis_c_hi, superind_hi = computeHPXpix_sequ_new(ratiores*nside, propertyArray, pixoffset=pixoffset, ratiores=1) hpxmap_hi = np.zeros(hp.nside2npix(ratiores*nside)) hpxmap_hi[listpix_hi] = weights_hi hpxmap_hitolo = hp.ud_grade(hpxmap_hi, nside) print 'valid hpxmap_hi', np.where(hpxmap_hi > 0)[0] print 'hpxmap', zip(np.where(hpxmap > 0)[0], hpxmap[hpxmap > 0]) print 'hpxmap_sup', zip(np.where(hpxmap_sup > 0)[0], hpxmap_sup[hpxmap_sup > 0]) print 'hpxmap_hitolo', zip(np.where(hpxmap_hitolo > 0)[0], hpxmap_hitolo[hpxmap_hitolo > 0]) hp.gnomview(hpxmap_hi, title='hpxmap_hi', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2) hp.gnomview(hpxmap_sup, title='hpxmap_sup', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2) hp.gnomview(hpxmap_hitolo, title='hpxmap_hitolo', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2) hp.gnomview(hpxmap, title='hpxmap', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2) #plt.plot(phis_c, thetas_c) thetas, phis = hp.pix2ang(nside, listpix) #plt.scatter(phis, thetas, color='red', marker='o', s=50*weights) #plt.scatter(propertyArray['RA']*np.pi/180, np.pi/2 - propertyArray['DEC']*np.pi/180) #plt.text(propertyArray['RA']*np.pi/180, np.pi/2 - propertyArray['DEC']*np.pi/180, str(i)) plt.show() stop
def test_tree_time(random_postions=True): def tree_xyz(lat, lon): """ convert positions on a sphere to 3d coordinates """ x = np.cos(lat) * np.cos(lon) y = np.cos(lat) * np.sin(lon) z = np.sin(lat) return x, y, z npts = 3e6 np.random.seed(42) if random_postions: # random points on a sphere. lat = np.arccos(2.*np.random.uniform(low=0, high=1, size=npts) - 1.) - np.pi/2. lon = np.random.uniform(low=0, high=2*np.pi, size=npts) else: nside = 16 lat, lon = hp.pix2ang(nside, np.arange(hp.nside2npix(nside))) npix = lat.size lat = np.repeat(lat, np.ceil(npts/npix)) lon = np.repeat(lon, np.ceil(npts/npix)) lat = lat[0:npts] lon = lon[0:npts] x, y, z = tree_xyz(lat, lon) # Postiions to search on nside = 128 position_lat, position_lon = hp.pix2ang(nside, np.arange(hp.nside2npix(nside))) position_lat = np.pi/2. - position_lat pos_x, pos_y, pos_z = tree_xyz(position_lat, position_lon) leafsize = 100 # Trying out solution from: https://github.com/scipy/scipy/issues/6108#issuecomment-217117065 tree = kdtree(zip(x, y, z), leafsize=leafsize, balanced_tree=False, compact_nodes=False) # Set a search radius radius = 2. x0, y0, z0 = (1, 0, 0) x1, y1, z1 = tree_xyz(np.radians(radius), 0) search_rad = np.sqrt((x1-x0)**2+(y1-y0)**2+(z1-z0)**2) found_points = 0 for px, py, pz in zip(pos_x, pos_y, pos_z): indices = tree.query_ball_point((px, py, pz), search_rad) found_points += len(indices)
def main(): plane_mask=True pmval=1 bcut=False bmin=-90 bmax=90 lcut=False lmin=-180 lmax=180 mask_ring=True inner=0 outer=30 maps_dir = '/zfs/bsafdi/data/' fermi_data_dir = '/zfs/tslatyer/fermimaps/allsky/' work_dir = '/zfs/nrodd/NPTFWorking/' emin=8 emax=9 nside=256 eventclass=5 eventtype=3 newstyle=1 ps_file = '/zfs/nrodd/NPTFWorking/data/ps_data/ps_lists/3FGL_0to15_tiny.txt' n_ps_run=10 indeg=True #loadforpsf = fp.fermi_plugin(maps_dir,fermi_data_dir=fermi_data_dir,work_dir=work_dir,CTB_en_min = emin,CTB_en_max=emax,nside=nside,eventclass=eventclass,eventtype=eventtype,newstyle=newstyle) f = sim.fake_fermi_data(maps_dir,fermi_data_dir=fermi_data_dir,work_dir=work_dir,CTB_en_min = emin,CTB_en_max=emax,nside=nside,eventclass=eventclass,eventtype=eventtype,newstyle=newstyle) f.load_psf() sigma_PSF_deg = f.sigma_PSF_deg[0:-1] print "sigma_PSF_deg:",sigma_PSF_deg print "Ebin:",emin print "Eventclass:",eventclass print "Eventtype:",eventtype print "Gaussian results:" start = time.time() the_map = np.ones(hp.nside2npix(256)) simt.draw_points(sigma_PSF_deg,1000000,0.,0.,the_map) end = time.time() #print "Time taken:",end - start f.load_king_param() print "King results:" start = time.time() the_map = np.ones(hp.nside2npix(256)) simt.draw_points_king(f.fcore_arr[0],f.score_arr[0],f.gcore_arr[0],f.stail_arr[0],f.gtail_arr[0],f.SpE_arr[0],1000000,0.,0.,the_map) end = time.time()
def healpy_hist(l,b,NSIDE=4,mask_zero=False,nest=_nest): """ """ l = np.array(l) b = np.array(b) pixels = hp.ang2pix(NSIDE,(90-b)*_d2r,l*_d2r,nest=nest) pix_hist = np.histogram(pixels,bins=range(hp.nside2npix(NSIDE)+1))[0] if mask_zero: return (pix_hist[pix_hist>0], np.arange(hp.nside2npix(NSIDE))[pix_hist>0]) else: return pix_hist, np.arange(hp.nside2npix(NSIDE))
def satellite(self,stellar_mass,distance_modulus,mc_source_id=1,seed=None,**kwargs): """ Create a simulated satellite. Returns a catalog object. """ if seed is not None: np.random.seed(seed) isochrone = kwargs.pop('isochrone',self.isochrone) kernel = kwargs.pop('kernel',self.kernel) for k,v in kwargs.items(): if k in kernel.params.keys(): setattr(kernel,k,v) mag_1, mag_2 = isochrone.simulate(stellar_mass, distance_modulus) lon, lat = kernel.simulate(len(mag_1)) logger.info("Simulating %i satellite stars..."%len(mag_1)) pix = ang2pix(self.config['coords']['nside_pixel'], lon, lat) # There is probably a better way to do this step without creating the full HEALPix map mask = -1. * numpy.ones(healpy.nside2npix(self.config['coords']['nside_pixel'])) mask[self.roi.pixels] = self.mask.mask_1.mask_roi_sparse mag_lim_1 = mask[pix] mask = -1. * numpy.ones(healpy.nside2npix(self.config['coords']['nside_pixel'])) mask[self.roi.pixels] = self.mask.mask_2.mask_roi_sparse mag_lim_2 = mask[pix] mag_err_1 = self.photo_err_1(mag_lim_1 - mag_1) mag_err_2 = self.photo_err_2(mag_lim_2 - mag_2) # Randomize magnitudes by their errors mag_obs_1 = mag_1+numpy.random.normal(size=len(mag_1))*mag_err_1 mag_obs_2 = mag_2+numpy.random.normal(size=len(mag_2))*mag_err_2 #mag_obs_1 = mag_1 #mag_obs_2 = mag_2 #select = numpy.logical_and(mag_obs_1 < mag_lim_1, mag_obs_2 < mag_lim_2) select = (mag_lim_1>mag_obs_1)&(mag_lim_2>mag_obs_2) # Make sure objects lie within the original cmd (should also be done later...) #select &= (ugali.utils.binning.take2D(self.mask.solid_angle_cmd, mag_obs_1 - mag_obs_2, mag_obs_1,self.roi.bins_color, self.roi.bins_mag) > 0) #return mag_1_obs[cut], mag_2_obs[cut], lon[cut], lat[cut] logger.info("Clipping %i simulated satellite stars..."%(~select).sum()) mc_source_id = mc_source_id * numpy.ones(len(mag_1)) hdu = ugali.observation.catalog.makeHDU(self.config,mag_obs_1[select],mag_err_1[select], mag_obs_2[select],mag_err_2[select], lon[select],lat[select],mc_source_id[select]) catalog = ugali.observation.catalog.Catalog(self.config, data=hdu.data) return catalog
def aladin_to_healpix(data): """ Convert Aladin's weird all-sky image tile to a respectable healpix array """ inds = natural_order(8, 0, 512).ravel() result = np.zeros(hp.nside2npix(512), dtype=data.dtype) for tile in xrange(hp.nside2npix(8)): i, j = tile / 27, tile % 27 sub = data[data.shape[0] - i * 64 - 64: data.shape[0] - i * 64, j * 64: j * 64 + 64] sub = sub[::-1].T.ravel() result[inds + tile * 64 * 64] = sub return result
def fill(self): self.values = np.zeros(hp.nside2npix(self.nside)) self.pix = np.arange(hp.nside2npix(self.nside)) self.lon,self.lat = pix2ang(self.nside,self.pix) for target in self.roster.values(): print(target) if self.coord == 'gal': idx = query_disc(self.nside,target.glon,target.glat,target.psi_max) lon,lat = gal2cel(self.lon[idx],self.lat[idx]) else: idx = query_disc(self.nside,target.ra,target.dec,target.psi_max) lon,lat = self.lon[idx],self.lat[idx] self.values[idx] += target.jvalue(lon,lat)
def healpixellize(f_in, theta_in, phi_in, nside=16, verbose=False): ''' A dumb method for converting data f sampled at points theta and phi (not on a healpix grid) into a healpix at resolution nside Parameters ---------- f_in | array: square map theta_in | array: latitude to grid to phi_in | array in: longitude to grid to nside | Optional[int]: healpix value (IONEX maps give nside=16 natively) verbose | Optional[bool]: whether to print values and info or not Returns ------- array: healpixellized version of square map input ''' # Input arrays are likely to be rectangular, but this is inconvenient f = f_in.flatten() theta = theta_in.flatten() phi = phi_in.flatten() pix = hp.ang2pix(nside, theta, phi) hp_map = np.zeros(hp.nside2npix(nside)) hits = np.zeros(hp.nside2npix(nside)) for i, v in enumerate(f): # Find the nearest pixels to the pixel in question neighbours, weights = hp.get_interp_weights(nside, theta[i], phi[i]) # Add weighted values to hp_map hp_map[neighbours] += v * weights # Keep track of weights hits[neighbours] += weights hp_map = hp_map / hits wh_no_hits = np.where(hits == 0) if verbose: print('pixels with no hits', wh_no_hits[0].shape) hp_map[wh_no_hits[0]] = hp.UNSEEN wh = np.where(hp_map == np.nan)[0] for i, w in enumerate(wh): neighbors = hp.get_interp_weights(nside, theta[i], phi[i]) hp_map[w] = np.median(neighbors) return hp_map
def _initialise_healpix(self): self.npix = np.int(hp.nside2npix(nside))
def generate_cartesian_file(hdus, lon, lat, label, index=1): content = hdus[index].header theta_0 = content['THETA_0'] psi_0 = content['PSI_0'] size_x = content['SIZE_X'] size_y = content['SIZE_Y'] dangle = content['ALPHAINT'] content = hdus[index].data pixels = content['PIXEL'] Jtotal = content['Jtot'] Jsmooth = content['Jsmooth'] Jsub = content['Jsub'] NSIDE = hdus[2].header['NSIDE'] ordering = hdus[2].header['ORDERING'] if ordering == "NESTED": nested = True else: nested = False nPix = hp.nside2npix(NSIDE) num_used_pixels = len(pixels) theta_rad, phi_rad = hp.pixelfunc.pix2ang(NSIDE, pixels, nest=nested) # change the angles according to our conventions theta, phi = -np.degrees(theta_rad-np.pi/2.-lon), np.degrees(np.pi*2.-phi_rad+lat) phi[np.where(phi>360)] -= 360. # Process the data for 3ML use nxPix = 140 nyPix = 140 refX = 70.5 refY = 70.5 delX = -dangle delY = dangle x = np.zeros(nPix) x[pixels] = Jtotal/np.max(Jtotal) dmROI = np.zeros([nxPix, nyPix]) for i in range(nxPix): for j in range(nyPix): ra_roi = (i-np.int(refX))*delX dec_roi = (j-np.int(refY))*delY hpix = hp.pixelfunc.ang2pix(NSIDE,np.radians(-dec_roi+90.),np.radians(360.-ra_roi), nest=nested) #print(hpix) dmROI[i,j] = x[hpix] dmROI = np.multiply(dmROI, (np.degrees(1)**2/delX**2)) # convert from galactic coordinates to fk5 coords = SkyCoord(lon*u.degree, lat*u.degree, frame='galactic', unit='degree') RA = coords.fk5.ra.degree DEC = coords.fk5.dec.degree # write the output to the new file new_hdu = fits.PrimaryHDU(dmROI) new_hdu.header['CTYPE1'] = 'RA' new_hdu.header['CTYPE2'] = 'DEC' new_hdu.header['CUNIT1'] = 'deg' new_hdu.header['CUNIT2'] = 'deg' new_hdu.header['CRPIX1'] = refX new_hdu.header['CRPIX2'] = refY new_hdu.header['CRVAL1'] = RA new_hdu.header['CRVAL2'] = DEC new_hdu.header['CDELT1'] = delX new_hdu.header['CDELT2'] = delY #new_hdu.header['DMAX'] = np.log10(Jtotal.max()) hdulist = fits.HDUList([new_hdu]) hdulist.writeto('../templates/{}_Jfactor_template.fits'.format(label)) f = open('../templates/{}_Jfactor_template.txt'.format(label), "w") f.write("For {}: Jmax={}".format(label, np.log10(Jtotal.max())))
def set_window(shear_zbins={}, f_sky=0.3, nside=256, mask_start_pix=0, window_cl_fact=None, unit_win=False, scheduler_info=None, mask=None, delta_W=True): from skylens.skylens_main import Skylens w_lmax = 3 * nside l0 = np.arange(w_lmax, dtype='int') corr = ('galaxy', 'galaxy') kappa0 = Skylens(galaxy_zbins=shear_zbins, do_cov=False, bin_cl=False, l_bins=None, l=l0, use_window=False, corrs=[corr], f_sky=f_sky, scheduler_info=scheduler_info) cl0G = kappa0.cl_tomo() npix0 = hp.nside2npix(nside) npix = np.int(npix0 * f_sky) if mask is None: mask = np.zeros(npix0, dtype='bool') # mask[int(npix):]=0 mask[mask_start_pix:mask_start_pix + int(npix)] = 1 cl_map0 = hp.ma(np.ones(npix0)) cl_map0[~mask] = hp.UNSEEN if scheduler_info is None: client = get_client() else: client = get_client(address=scheduler_info['address']) for i in np.arange(shear_zbins['n_bins']): cl_i = client.compute(cl0G['cl'][corr][(i, i)]).result() if np.any(np.isnan(cl_i)): print('survey utils, set_window:', cl_i) crash if unit_win: cl_map = hp.ma(np.ones(12 * nside * nside)) # cl_i=1 else: cl_i += shear_zbins['SN']['galaxy'][:, i, i] if window_cl_fact is not None: cl_i *= window_cl_fact cl_map = hp.ma(1 + hp.synfast(cl_i, nside=nside)) cl_map[cl_map <= 0] = 1.e-4 cl_map[~mask] = hp.UNSEEN cl_t = hp.anafast(cl_map) # cl_map/=cl_map[mask].mean() # if not unit_win: # cl_map/=np.sqrt(cl_t[0]) #this is important for shear map normalization in correlation functions. cl_map[~mask] = hp.UNSEEN cl_map_noise = np.sqrt(cl_map) cl_map_noise[~mask] = hp.UNSEEN # cl_map.mask=mask shear_zbins[i]['window_cl0'] = cl_i shear_zbins[i]['window'] = cl_map if delta_W: shear_zbins[i]['window_N'] = np.sqrt(shear_zbins[i]['window']) shear_zbins[i]['window_N'][~mask] = hp.UNSEEN else: print('not using delta_W window') shear_zbins[i]['window_N'] = np.sqrt(1. / shear_zbins[i]['window']) shear_zbins[i]['window_N'][~mask] = hp.UNSEEN shear_zbins[i]['window'][:] = 1 shear_zbins[i]['window'][~mask] = hp.UNSEEN del cl0G, kappa0 return shear_zbins
def mask_gal(nside): gl, gb = hp.pix2ang(nside, np.arange(hp.nside2npix(nside)), lonlat=True) mask = np.zeros(hp.nside2npix(nside)) mask[np.where(gb < -15.)] = 1. mask[np.where(gb > 15.)] = 1. return mask
import numpy as np import healpy as hp import matplotlib.pyplot as plt import utils as ut from matplotlib import rc from astropy.io import fits rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica'], 'size': 15}) rc('text', usetex=True) nside = 2048 npix = hp.nside2npix(nside) # 2 mJy depth p_map2 = hp.read_map("outputs/p_map_rms_median_Imin2.000.fits", dtype=None, verbose=False).astype(float) p_map2 /= np.amax(p_map2) p_map2 = hp.ud_grade(p_map2, nside_out=nside) # Binary mask msk_b = hp.read_map("outputs/pointings_hp2048_mask_good.fits.gz", dtype=None, verbose=False).astype(float) mask_lofar = p_map2 * msk_b mask_lofar[mask_lofar < 0.5] = 0 # 0.1 mJy depth p_map0p1 = hp.read_map("outputs/p_map_rms_median_Imin0.100.fits", dtype=None, verbose=False).astype(float) p_map0p1 /= np.amax(p_map0p1)
args = parser.parse_args() if args.stars == 'allstars': starNames = '' else: starNames = args.stars + '_' filterName = args.filtername colName = filterName + 'mag' # Set up healpy map and ra, dec centers nside = 64 # Set the min to 15 since we saturate there. CatSim max is 28 bins = np.arange(15., 28.2, .2) starDensity = np.zeros((hp.nside2npix(nside), np.size(bins) - 1), dtype=float) overMaxMask = np.zeros(hp.nside2npix(nside), dtype=bool) lat, ra = hp.pix2ang(nside, np.arange(0, hp.nside2npix(nside))) dec = np.pi / 2. - lat # Square root of pixel area. hpsizeDeg = hp.nside2resol(nside, arcmin=True) / 60. # Limit things to a 10 arcmin radius hpsizeDeg = np.min([10. / 60., hpsizeDeg]) # Options include galaxyBase, cepheidstars, wdstars, rrlystars, msstars, bhbstars, allstars, and more... dbobj = CatalogDBObject.from_objid(args.stars) indxMin = 0
print '++++++++++++++++++++++++++' print '==========================' print(time.strftime("%H:%M:%S")), (time.strftime("%d/%m/%Y")) print '++++++++++++++++++++++++++' print '==========================' print '++++++++++++++++++++++++++' print '==========================' print 'NOISE LEVEL: ', noise_lvl #################################################################### # sampling rate; resolutions in/out fs = 4096 nside_in = 32 nside_out = 8 npix_out = hp.nside2npix(nside_out) # load the LIGO file list ligo_data_dir = data_path filelist = rl.FileList(directory=ligo_data_dir) # declare whether to simulate (correlated) data (in frequency space) sim = False # frequency cuts (integrate over this range) low_f = 30. high_f = 500. # spectral shape of the GWB
def test_scan_map(): # The purpose of this test is to simulate the motion of the spacecraft # for one year (see `time_span_s`) and produce *two* maps: the first # is associated with the Observation `obs1` and is built using # `scan_map_in_observations` and `make_bin_map`, the second is associated # the Observation `obs2` and is built directly filling in the test # `tod`, `psi` and `pixind` and then using `make_bin_map` # In the final test `out_map1` is compared with both `out_map2` and the # input map. Both simulations use two orthogonal detectors at the boresight # and input maps generated with `np.random.normal`. start_time = 0 time_span_s = 365 * 24 * 3600 nside = 16 sampling_hz = 1 hwp_radpsec = 4.084_070_449_666_731 npix = hp.nside2npix(nside) sim = lbs.Simulation(start_time=start_time, duration_s=time_span_s) scanning = lbs.SpinningScanningStrategy( spin_sun_angle_rad=0.785_398_163_397_448_3, precession_rate_hz=8.664_850_513_998_931e-05, spin_rate_hz=0.000_833_333_333_333_333_4, start_time=start_time, ) spin2ecliptic_quats = scanning.generate_spin2ecl_quaternions( start_time, time_span_s, delta_time_s=7200) instr = lbs.InstrumentInfo( boresight_rotangle_rad=0.0, spin_boresight_angle_rad=0.872_664_625_997_164_8, spin_rotangle_rad=3.141_592_653_589_793, ) detT = lbs.DetectorInfo( name="Boresight_detector_T", sampling_rate_hz=sampling_hz, bandcenter_ghz=100.0, quat=[0.0, 0.0, 0.0, 1.0], ) detB = lbs.DetectorInfo( name="Boresight_detector_B", sampling_rate_hz=sampling_hz, bandcenter_ghz=100.0, quat=[0.0, 0.0, 1.0 / np.sqrt(2.0), 1.0 / np.sqrt(2.0)], ) np.random.seed(seed=123_456_789) maps = np.random.normal(0, 1, (3, npix)) in_map = {"Boresight_detector_T": maps, "Boresight_detector_B": maps} (obs1, ) = sim.create_observations(detectors=[detT, detB]) (obs2, ) = sim.create_observations(detectors=[detT, detB]) pointings = lbs.scanning.get_pointings( obs1, spin2ecliptic_quats=spin2ecliptic_quats, detector_quats=[detT.quat, detB.quat], bore2spin_quat=instr.bore2spin_quat, ) lbs.scan_map_in_observations(obs1, pointings, hwp_radpsec, in_map, fill_psi_and_pixind_in_obs=True) out_map1 = lbs.make_bin_map(obs1, nside).T times = obs2.get_times() obs2.pixind = np.empty(obs2.tod.shape, dtype=np.int32) obs2.psi = np.empty(obs2.tod.shape) for idet in range(obs2.n_detectors): obs2.pixind[idet, :] = hp.ang2pix(nside, pointings[idet, :, 0], pointings[idet, :, 1]) obs2.psi[idet, :] = np.mod( pointings[idet, :, 2] + 2 * times * hwp_radpsec, 2 * np.pi) for idet in range(obs2.n_detectors): obs2.tod[idet, :] = ( maps[0, obs2.pixind[idet, :]] + np.cos(2 * obs2.psi[idet, :]) * maps[1, obs2.pixind[idet, :]] + np.sin(2 * obs2.psi[idet, :]) * maps[2, obs2.pixind[idet, :]]) out_map2 = lbs.make_bin_map(obs2, nside).T np.testing.assert_allclose(out_map1, in_map["Boresight_detector_T"], rtol=1e-6, atol=1e-6) np.testing.assert_allclose(out_map1, out_map2, rtol=1e-6, atol=1e-6)
def main(): warnings.filterwarnings("ignore", category=ErfaWarning) sim = lbs.Simulation( parameter_file=sys.argv[1], name="Observation of planets", description=""" This report contains the result of a simulation of the observation of the sky, particularly with respect to the observation of planets. """, ) params = load_parameters(sim) if lbs.MPI_ENABLED: log.info("Using MPI with %d processes", lbs.MPI_COMM_WORLD.size) else: log.info("Not using MPI, serial execution") log.info("Generating the quaternions") scanning_strategy = read_scanning_strategy( sim.parameters["scanning_strategy"], sim.imo, sim.start_time) sim.generate_spin2ecl_quaternions( scanning_strategy=scanning_strategy, delta_time_s=params.spin2ecl_delta_time_s) log.info("Creating the observations") instr = lbs.InstrumentInfo( name="instrum", spin_boresight_angle_rad=params.spin_boresight_angle_rad) detector = lbs.DetectorInfo( sampling_rate_hz=params.detector_sampling_rate_hz) conversions = [ ("years", astropy.units.year), ("year", astropy.units.year), ("days", astropy.units.day), ("day", astropy.units.day), ("hours", astropy.units.hour), ("hour", astropy.units.hour), ("minutes", astropy.units.minute), ("min", astropy.units.minute), ("sec", astropy.units.second), ("s", astropy.units.second), ("km", astropy.units.kilometer), ("Km", astropy.units.kilometer), ("au", astropy.units.au), ("AU", astropy.units.au), ("deg", astropy.units.deg), ("rad", astropy.units.rad), ] def conversion(x, new_unit): if isinstance(x, str): for conv_str, conv_unit in conversions: if x.endswith(" " + conv_str): value = float(x.replace(conv_str, "")) return (value * conv_unit).to(new_unit).value break else: return float(x) sim_params = sim.parameters["simulation"] durations = ["duration_s", "duration_of_obs_s"] for dur in durations: sim_params[dur] = conversion(sim_params[dur], "s") delta_t_s = sim_params["duration_of_obs_s"] sim.create_observations( detectors=[detector], num_of_obs_per_detector=int(sim_params["duration_s"] / sim_params["duration_of_obs_s"]), ) ################################################################# # Here begins the juicy part log.info("The loop starts on %d processes", lbs.MPI_COMM_WORLD.size) sky_hitmap = np.zeros(healpy.nside2npix(params.output_nside), dtype=np.int32) detector_hitmap = np.zeros(healpy.nside2npix(params.output_nside), dtype=np.int32) dist_map_m2 = np.zeros(len(detector_hitmap)) iterator = tqdm if lbs.MPI_ENABLED and lbs.MPI_COMM_WORLD.rank != 0: iterator = lambda x: x # Time variable inizialized at the beginning of the simulation t = 0.0 for obs in iterator(sim.observations): solar_system_ephemeris.set("builtin") times = obs.get_times(astropy_times=True) # We only compute the planet's position for the first sample in # the observation and then assume that it does not move # significantly. (In Ecliptic coordinates, Jupiter moves by # fractions of an arcmin over a time span of one hour) time0 = times[0] icrs_pos = get_ecliptic_vec( get_body_barycentric(params.planet_name, time0)) earth_pos = get_ecliptic_vec(get_body_barycentric("earth", time0)) # Move the spacecraft to L2 L2_pos = earth_pos * ( 1.0 + params.earth_L2_distance_km / norm(earth_pos).to("km").value) # Creating a Lissajous orbit R1 = conversion(params.radius_au[0], "au") R2 = conversion(params.radius_au[1], "au") phi1_t = params.L2_orbital_velocity_rad_s[0] * t phi2_t = params.L2_orbital_velocity_rad_s[1] * t phase = conversion(params.phase_rad, "rad") orbit_pos = np.array([ -R1 * np.sin(np.arctan(L2_pos[1] / L2_pos[0])) * np.cos(phi1_t), R1 * np.cos(np.arctan(L2_pos[1] / L2_pos[0])) * np.cos(phi1_t), R2 * np.sin(phi2_t + phase), ]) orbit_pos = astropy.units.Quantity(orbit_pos, unit="AU") # Move the spacecraft to a Lissajous orbit around L2 sat_pos = orbit_pos + L2_pos # Compute the distance between the spacecraft and the planet distance_m = norm(sat_pos - icrs_pos).to("m").value # This is the direction of the solar system body with respect # to the spacecraft, in Ecliptic coordinates ecl_vec = (icrs_pos - sat_pos).value # The variable ecl_vec is a 3-element vector. We normalize it so # that it has length one (using the L_2 norm, hence ord=2) ecl_vec /= np.linalg.norm(ecl_vec, axis=0, ord=2) # Convert the matrix to a N×3 shape by repeating the vector: # planets move slowly, so we assume that the planet stays fixed # during this observation. ecl_vec = np.repeat(ecl_vec.reshape(1, 3), len(times), axis=0) # Calculate the quaternions that convert the Ecliptic # reference system into the detector's reference system quats = lbs.get_ecl2det_quaternions( obs, sim.spin2ecliptic_quats, detector_quats=[detector.quat], bore2spin_quat=instr.bore2spin_quat, ) # Make room for the xyz vectors in the detector's reference frame det_vec = np.empty_like(ecl_vec) # Do the rotation! lbs.all_rotate_vectors(det_vec, quats[0], ecl_vec) pixidx = healpy.vec2pix(params.output_nside, det_vec[:, 0], det_vec[:, 1], det_vec[:, 2]) bincount = np.bincount(pixidx, minlength=len(detector_hitmap)) detector_hitmap += bincount dist_map_m2 += bincount / ((4 * np.pi * (distance_m**2))**2) pointings = lbs.get_pointings(obs, sim.spin2ecliptic_quats, [detector.quat], instr.bore2spin_quat)[0] pixidx = healpy.ang2pix(params.output_nside, pointings[:, 0], pointings[:, 1]) bincount = np.bincount(pixidx, minlength=len(sky_hitmap)) sky_hitmap += bincount # updating the time variable t += delta_t_s if lbs.MPI_ENABLED: sky_hitmap = lbs.MPI_COMM_WORLD.allreduce(sky_hitmap) detector_hitmap = lbs.MPI_COMM_WORLD.allreduce(detector_hitmap) dist_map_m2 = lbs.MPI_COMM_WORLD.allreduce(dist_map_m2) time_map_s = detector_hitmap / params.detector_sampling_rate_hz dist_map_m2[dist_map_m2 > 0] = np.power(dist_map_m2[dist_map_m2 > 0], -0.5) obs_time_per_radius_s = [ time_per_radius(time_map_s, angular_radius_rad=np.deg2rad(radius_deg)) for radius_deg in params.radii_deg ] if lbs.MPI_COMM_WORLD.rank == 0: # Create a plot of the observation time of the planet as a # function of the angular radius fig, ax = plt.subplots() ax.loglog(params.radii_deg, obs_time_per_radius_s) ax.set_xlabel("Radius [deg]") ax.set_ylabel("Observation time [s]") # Create a map showing how the observation time is distributed on # the sphere (in the reference frame of the detector) healpy.orthview(time_map_s, title="Time spent observing the source", unit="s") if scanning_strategy.spin_rate_hz != 0: spin_period_min = 1.0 / (60.0 * scanning_strategy.spin_rate_hz) else: spin_period_min = 0.0 if scanning_strategy.precession_rate_hz != 0: precession_period_min = 1.0 / ( 60.0 * scanning_strategy.precession_rate_hz) else: precession_period_min = 0.0 sim.append_to_report( """ ## Scanning strategy parameters Parameter | Value --------- | -------------- Angle between the spin axis and the Sun-Earth axis | {{ sun_earth_angle_deg }} deg Angle between the spin axis and the boresight | {{ spin_boresight_angle_deg }} deg Precession period | {{ precession_period_min }} min Spin period | {{ spin_period_min }} min ## Observation of {{ params.planet_name | capitalize }} ![](detector_hitmap.png) The overall time spent in the map is {{ overall_time_s }} seconds. The time resolution of the simulation was {{ delta_time_s }} seconds. Angular radius [deg] | Time spent [s] -------------------- | ------------------------ {% for row in radius_vs_time_s -%} {{ "%.1f"|format(row[0]) }} | {{ "%.1f"|format(row[1]) }} {% endfor -%} ![](radius_vs_time.svg) """, figures=[(plt.gcf(), "detector_hitmap.png"), (fig, "radius_vs_time.svg")], params=params, overall_time_s=np.sum(detector_hitmap) / params.detector_sampling_rate_hz, radius_vs_time_s=list(zip(params.radii_deg, obs_time_per_radius_s)), delta_time_s=1.0 / params.detector_sampling_rate_hz, sun_earth_angle_deg=np.rad2deg( scanning_strategy.spin_sun_angle_rad), spin_boresight_angle_deg=np.rad2deg( params.spin_boresight_angle_rad), precession_period_min=precession_period_min, spin_period_min=spin_period_min, det=detector, ) healpy.write_map( params.output_map_file_name, (detector_hitmap, time_map_s, dist_map_m2, sky_hitmap), coord="DETECTOR", column_names=["HITS", "OBSTIME", "SQDIST", "SKYHITS"], column_units=["", "s", "m^2", ""], dtype=[np.int32, np.float32, np.float64, np.int32], overwrite=True, ) np.savetxt( params.output_table_file_name, np.array(list(zip(params.radii_deg, obs_time_per_radius_s))), fmt=["%.2f", "%.5e"], ) with (sim.base_path / "parameters.json").open("wt") as outf: time_value = scanning_strategy.start_time if not isinstance(time_value, (int, float)): time_value = str(time_value) json.dump( { "scanning_strategy": { "spin_sun_angle_rad": scanning_strategy.spin_sun_angle_rad, "precession_rate_hz": scanning_strategy.precession_rate_hz, "spin_rate_hz": scanning_strategy.spin_rate_hz, "start_time": time_value, }, "detector": { "sampling_rate_hz": params.detector_sampling_rate_hz }, "planet": { "name": params.planet_name }, }, outf, indent=2, ) sim.flush()
trials_per_sig = args.ntrials seed_counter = args.seed f = FastResponseAnalysis(skymap_files[0], start, stop, save=False, alert_event=True) f.llh.nbackground = args.bkg * args.deltaT / 1000. #inj = f.initialize_injector(gamma=2.5) #just put this here to initialize f.spatial_prior #print f.llh.nbackground #results_array = [] npix = hp.nside2npix(f.nside) shape = (args.ntrials, npix) maps = sparse.lil_matrix(shape, dtype=float) for jj in range(trials_per_sig): seed_counter += 1 val = f.llh.scan( 0.0, 0.0, scramble=True, seed=seed_counter, #spatial_prior = f.spatial_prior, time_mask=[deltaT / 2., (start_mjd + stop_mjd) / 2.], pixel_scan=[f.nside, 4.0], inject=None) if val['TS'] is not None: dtype = [('ts', float), ('pixel', float)]
def mk_map_gsm2016_mod(freqs, nside_out): import sys sys.path.append('/data4/paper/zionos/polskysim') import gsm2016_mod import astropy.coordinates as coord import astropy.units as units def harmonic_ud_grade(m, nside_in, nside_out): """ Decompose a map at a resolution nside_in into spherical harmonic components and then resynthesize the map at nside_out. """ lmax = 3 * nside_in - 1 alm = hp.map2alm(m, lmax=lmax) return hp.alm2map(alm, nside_out, lmax=lmax, verbose=False) def rotate_healpix_map(m, R): """ Performs a scalar rotation of the map relative to the Healpix coordinate frame by interpolating the map at the coordinates of new coordinate frame. """ npix = len(m) nside = hp.npix2nside(npix) hpxidx = np.arange(npix) c, a = hp.pix2ang(nside, hpxidx) t, p = rotate_sphr_coords(R, c, a) return hp.get_interp_val(m, t, p) nside_in = 64 npix_in = hp.nside2npix(nside_in) nfreq = len(freqs) npix_out = hp.nside2npix(nside_out) I_gal = np.zeros((nfreq, npix_in)) for fi, f in enumerate(freqs): I_gal[fi] = gsm2016_mod.get_gsm_map_lowres( f / 1e3) # freqs is in MHz, gsm2016 generates x_c = np.array([1., 0, 0]) # unit vectors to be transformed by astropy y_c = np.array([0, 1., 0]) z_c = np.array([0, 0, 1.]) # The GSM is given in galactic coordinates. We will rotate it to J2000 equatorial coordinates. axes_icrs = coord.SkyCoord(x=x_c, y=y_c, z=z_c, frame='icrs', representation='cartesian') axes_gal = axes_icrs.transform_to('galactic') axes_gal.representation = 'cartesian' R = np.array( axes_gal.cartesian.xyz ) # The 3D rotation matrix that defines the coordinate transformation. npix_out = hp.nside2npix(nside_out) I = np.zeros((nfreq, npix_out)) for i in range(nfreq): I[i] = rotate_healpix_map(I_gal[i], R) if nside_out != nside_in: I[i] = harmonic_ud_grade(I_gal[i], nside_in, nside_out) return I
def propOpp(cube=None, flo=100., fhi=200., lmax=100, npznamelist=None, save_cubes=True): """ Propogate the Q and U components of an IQUV cube through the Oppermann et al. 2012 RM map. The cube must be 4 or 2 by npix by nfreq. If 4, the middle two arrays will be assumed to be Q & U IN THAT ORDER. Or if fromnpz=True, then provide an array of npz names (cube length assumptionsremain) """ ## load the maps if npznamelist != None: assert (cube == None) nNpz = len(npznamelist) assert (nNpz == 2 or nNpz == 4) if nNpz == 2: Q = np.load(npznamelist[0])['maps'] U = np.load(npznamelist[1])['maps'] else: Q = np.load(npznamelist[1])['maps'] U = np.load(npznamelist[2])['maps'] elif cube != None: Q = cube[1] U = cube[2] else: raise ImplmentationError('No map information provided.') ## nbins = Q.shape[0] nu = np.linspace(flo, fhi, num=nbins) lam = 3e8 / (nu * 1e6) lam2 = np.power(lam, 2) d = pyfits.open('opp2012.fits') RM = d[3].data.field(0) """ The RM map is nside=128. Everything else is nside=512. We're smoothing on scales larger than the pixellization this introduces, so no worries. RMmap=hp.ud_grade(RM,nside_out=512) hp.mollview(RMmap,title='Oppermann map') RMmap=hp.smoothing(RMmap,fwhm=np.radians(1.)) hp.mollview(RMmap,title='Oppermann map smoothed') plt.show() """ #Downsample RM variance in alm space #Upsample it in pixellization #Is this kosher? Qn, Un = [np.zeros((nbins, hp.nside2npix(128))) for i in range(2)] for i in range(Q.shape[0]): # print Q[:,i].shape Qn[i] = hp.alm2map(hp.map2alm(Q[i], lmax=3 * 512 - 1), nside=128) Un[i] = hp.alm2map(hp.map2alm(U[i], lmax=3 * 512 - -1), nside=128) RMmap = RM # RMmap = hp.alm2map(hp.map2alm(RM,lmax=lmax),nside=512) Qmaps_rot = np.zeros_like(Qn) Umaps_rot = np.zeros_like(Un) # phi = np.outer(RMmap,lam2) for i in range(nbins): phi = RMmap * lam2[i] fara_rot = (Qn[i] + 1.j * Un[i]) * np.exp( -2.j * phi) #Eq. 9 of Moore et al. 2013 Qmaps_rot[i] = fara_rot.real Umaps_rot[i] = fara_rot.imag QU = [Qmaps_rot, Umaps_rot] if save_cubes == True: print 'Saving Q U rotated' np.savez('cube_Qrot_%s-%sMHz.npz' % (str(flo), str(fhi)), maps=Qmaps_rot) np.savez('cube_Urot_%s-%sMHz.npz' % (str(flo), str(fhi)), maps=Umaps_rot) return np.array(QU)
def mk_fg_cube(onescale=True, pfrac=0.002, flo=100., fhi=200., nbins=203, alo=-2.7, ahi=-2.3, alpha_map=None, raw_map=None, intermediates=True, save_cubes=True, verbose=False): """ Make a Stokes IQUV cube. pfrac: fraction Q,U/I flo: lowest frequency fhi: highest frequency nbins: number of frequency bins alo: lowest spectral index ahi: highest spectral index alpha_map: a healpix map of spectral indices. Useful if splitting-up frequency runs to avoid memory errors onescale: I'm not feeling smart enough to make an argument interpreter for different "mk_map" cases. If onescale=True, diffuse emission is modelled using a single power law. Otherwise, we use the SCK model. XXX currently only Galactic Synchrotron intermediates: Save Q and U realizations @ flo MHz, and spectral index maps I'm making the : - large assumption that Q and U trace the spectral distribution of diffuse Stokes I power at a fixed polarization fraction. - assumption that spectral indicies are randomly distributed on scales > 3deg - small assumption that Stokes V power is vanishingly small. TODO: - map Q<->U using polarization angle map - maybe move away from assumption that Galactic Sync. is dominant source of polarization? - could use a "correlation length" ala Shaw et al. 2014 to get more realistic spectral index distribution """ nu = np.linspace(flo, fhi, num=nbins, endpoint=True) nside = 512 #to match GSM nside npix = hp.nside2npix(nside) ipix = np.arange(npix) #Spectral indices -2.7 to -2.3 are the 2sigma range of Rogers & Bowman '08 (in K) if alpha_map == None: alpha = np.random.uniform(low=alo, high=ahi, size=npix) alpha = hp.smoothing(alpha, fwhm=np.radians(3.)) if intermediates: #XXX bug -- need to init date np.savez('alpha_%i-%i-%i.npz' % (date[0], date[1], date[2]), maps=alpha) else: if verbose: ' Loading %s' % alpha_map alpha = np.load(alpha_map)['maps'] if verbose: print 'Creating Stokes I spectral cube with PyGSM...' I = np.transpose(mk_map_GSM(frange=[flo, fhi], nbins=nbins), (-1, 0)) #use GSM for Stokes I ## I.shape = (nfreq, npix) ## eh, this will require more work, might not be necessary # freqs = np.linspace(flo,fhi, nbins, endpoint=True) # I = mk_map_gsm2016_mod if verbose: print 'Stokes I done' if raw_map == None: if onescale: Q0 = mk_map_onescale(512) U0 = mk_map_onescale(512) #different realizations of same scaling #XXX with a polarization angle map, I could link Q,U instead of having them independent else: Q0 = mk_map_SCK(512, flo, 700, 2.4, 2.80) U0 = mk_map_SCK(512, flo, 700, 2.4, 2.80) #XXX as above wrt pol angle, but also this currently assumes we are dominated by Galactic Synchrotron _Q0, _U0 = Q0 - Q0.min(), U0 - U0.min() #XXX is this OK? Q0 = (2 * _Q0 / _Q0.max()) - 1 #scale to be -1 to 1 U0 = (2 * _U0 / _U0.max()) - 1 #scale to be -1 to 1 if intermediates: if verbose: plot_maps([Q0, U0], titles=['raw Q', 'raw U']) np.savez('rawcube_%sMHz.npz' % str(flo), maps=[I[:, 0], Q0, U0]) else: if verbose: print ' Loading %s' % raw_map raw = np.load(raw_map)['maps'] #Stokes I is deterministic, Q and U are not Q0 = raw[1] U0 = raw[2] Qmaps, Umaps, Vmaps = np.zeros((len(nu), npix)), np.zeros( (len(nu), npix)), np.zeros((len(nu), npix)) #If only I could take the log! Then this would be vectorizable #stoopid Q and U with their non +ve definition if verbose: print 'Begin loop over spectral index for frequency scaling' # for i in ipix: # Qmaps[i,:] = Q0[i] * np.power(nu/130.,alpha[i]) #XXX BEWARE HARDCODED 130 MHz # Umaps[i,:] = U0[i] * np.power(nu/130.,alpha[i]) freq_scale = np.power(np.outer(nu, np.ones(npix)) / 130., alpha) #XXX BEWARE HARDCODED 130 MHz Qmaps = Q0 * freq_scale Umaps = U0 * freq_scale date = datetime.now().timetuple() if verbose: print 'Frequency scaling done' #XXX is this OK? #impose polarization fraction as fraction of sky-average Stokes I power per frequency Qmaps *= np.outer(np.nanmean(I, axis=1) * pfrac, np.ones(npix)) Umaps *= np.outer(np.nanmean(I, axis=1) * pfrac, np.ones(npix)) spols = ['I', 'Q', 'U', 'V'] cube = [I, Qmaps, Umaps, Vmaps] if save_cubes == True: for i, m in enumerate(cube): N = 'cube_%s_%s-%sMHz_%sbins.npz' % (spols[i], str(flo), str(fhi), str(len(nu))) print ' Saving %s' % N np.savez(N, maps=m) return np.array(cube)
def log_jacobian(dgrid, nside): # get the number of pixels for the healpy nside npix = np.int(hp.nside2npix(nside)) # calculate the jacobian on the d_grid, copy over for the required number of pixels, appropriately reshape the array and return return np.array([2. * np.log(d) for d in dgrid] * npix).reshape(npix, -1).T
gmst_rad_inj = lal.GreenwichMeanSiderealTime(GPSTime) dist_inj = injection.distance print 'injection values -->', dist_inj, ra_inj, dec_inj, tc else: injection = None samples = np.genfromtxt(input_file, names=True) print samples.dtype.names if "dist" in samples.dtype.names: samples = np.column_stack( (samples["dist"], samples["ra"], samples["dec"], samples["time"])) else: samples = np.column_stack((samples["distance"], samples["ra"], samples["dec"], samples["time"])) npix = np.int(hp.nside2npix(nside)) print 'The number of grid points in the sky is :', hp.nside2npix( nside), 'resolution = ', hp.nside2pixarea(nside, degrees=True), ' deg^2' print 'The number of grid points in distance is :', n_dist, 'resolution = ', ( options.dmax - 1.0) / n_dist, ' Mpc' print 'Total grid size is :', n_dist * hp.nside2npix(nside) print 'Volume resolution is :', hp.nside2pixarea(nside) * ( options.dmax - 1.0) / n_dist, ' Mpc^3' samps = [] gmst_rad = [] if options.nsamps is not None: idx = np.random.choice(range(0, len(samples[:, 0])), size=options.nsamps)
index = (x[iMag] > m1) * (x[iMag] < m2) * (x[iz] > z1) * ( x[iz] < z2) #*(flag_g==nn) if (m1 > 0): index = (x[imag] > m1) * (x[imag] < m2) * (x[iz] > z1) * ( x[iz] < z2) #*(flag_g==nn) ra_t = x[ira] dec_t = x[idec] nside_mask = 256 name_pre, name_file = 'bass', 'BASS' mask = np.genfromtxt('../data/' + name_pre + 'mask.dat') fname_edge = 'bassldp-pos2galaxy20-21R2.npy' x_edge = np.load('../data/' + name_file + '/' + fname_edge) theta, phi = np.radians(90. - dec_t[index]), np.radians(ra_t[index]) nside_ldp = 4096 npix_ldp = hp.nside2npix(nside_ldp) id_ldp = np.arange(npix_ldp) Radius = 5 #arcmin radius = np.radians(Radius / 60.) vec_g = hp.ang2vec(theta, phi) mask_ldp = np.ones(npix_ldp) if (nside_mask != nside_ldp): mask_new = pre.mask_nside12(mask, nside_ldp, nside_mask) for i in np.arange(theta.shape[0]): idx_search = hp.query_disc(nside_ldp, vec_g[i], radius, nest=True) #idx on grid map mask_ldp[idx_search] = 0 mask_ldp[mask_new == 0] = 0 mask_ldp[np.int32(x_edge[2])] = 0
norm_file = './P8UCVA_base_norm.npy' # Pre Norm: print "Pre Norm:" for key in f_global.template_dict.keys(): print "key:", key print "shape:", np.shape(f_global.template_dict[key]) print "mean:", np.mean(f_global.template_dict[key]) np.load('fake') f_global.use_template_normalization_file(norm_file, key_suffix='-0') print "Post Norm:" for key in f_global.template_dict.keys(): print "key:", key print "shape:", np.shape(f_global.template_dict[key]) print "mean:", np.mean(f_global.template_dict[key]) sum_sim_normed = {} sum_sim_normed['sim'] = np.zeros((emax_bin - emin_bin, hp.nside2npix(nside))) for key in f_global.template_dict.keys(): sum_sim_normed['sim'] += np.array(f_global.template_dict[key]) nodm_map = sum_sim_normed['sim'] nodm_out = np.zeros(shape=(len(nodm_map), hp.nside2npix(nside))) for i in range(40): print "Ebin:", i print "Mean:", np.mean(nodm_map[i])
def plotPhotometryMap(band, vmin=0.0, vmax=1.0, mjdmax='', prop='zptvar', op='min', rel='DR0', survey='surveyname', nside='1024', oversamp='1'): import fitsio from matplotlib import pyplot as plt import matplotlib.cm as cm from numpy import zeros, array import healpix from healpix import pix2ang_ring, thphi2radec import healpy as hp # Survey inputs mjdw = mjdmax if rel == 'DR2': fname = inputdir + 'decals-ccds-annotated.fits' catalogue_name = 'DECaLS_DR2' + mjdw if rel == 'DR3': inputdir = '/project/projectdirs/cosmo/data/legacysurvey/dr3/' # where I get my data fname = inputdir + 'ccds-annotated-decals.fits.gz' catalogue_name = 'DECaLS_DR3' + mjdw if rel == 'DR4': inputdir = '/global/projecta/projectdirs/cosmo/work/dr4/' if (band == 'g' or band == 'r'): fname = inputdir + 'ccds-annotated-dr4-90prime.fits.gz' catalogue_name = '90prime_DR4' + mjdw if band == 'z': fname = inputdir + 'ccds-annotated-dr4-mzls.fits.gz' catalogue_name = 'MZLS_DR4' + mjdw fname = localdir + catalogue_name + '/nside' + nside + '_oversamp' + oversamp + '/' + catalogue_name + '_band_' + band + '_nside' + nside + '_oversamp' + oversamp + '_' + prop + '__' + op + '.fits.gz' f = fitsio.read(fname) ral = [] decl = [] val = f['SIGNAL'] pix = f['PIXEL'] # -------------- plot of values ------------------ if (prop == 'zptvar' and opt == 'min'): print 'Plotting min zpt rms' myval = [] for i in range(0, len(val)): myval.append(1.086 * np.sqrt(val[i])) #1.086 converts dm into d(flux) th, phi = hp.pix2ang(int(nside), pix[i]) ra, dec = thphi2radec(th, phi) ral.append(ra) decl.append(dec) mylabel = 'min-zpt-rms-flux' vmin = 0.0 #min(myval) vmax = 0.03 #max(myval) npix = len(myval) below = 0 print 'Min and Max values of ', mylabel, ' values is ', min( myval), max(myval) print 'Number of pixels is ', npix print 'Number of pixels offplot with ', mylabel, ' < ', vmin, ' is', below print 'Area is ', npix / (float(nside)**2. * 12) * 360 * 360. / pi, ' sq. deg.' map = plt.scatter(ral, decl, c=myval, cmap=cm.rainbow, s=2., vmin=vmin, vmax=vmax, lw=0, edgecolors='none') cbar = plt.colorbar(map) plt.xlabel('r.a. (degrees)') plt.ylabel('declination (degrees)') plt.title('Map of ' + mylabel + ' for ' + catalogue_name + ' ' + band + '-band') plt.xlim(0, 360) plt.ylim(-30, 90) plt.savefig(localdir + mylabel + '_' + band + '_' + catalogue_name + str(nside) + '.png') plt.close() # -------------- plot of status in udgrade maps of 1.406 deg pix size ------------------ #Bands inputs if band == 'g': phreq = 0.01 if band == 'r': phreq = 0.01 if band == 'z': phreq = 0.02 # Obtain values to plot if (prop == 'zptvar' and opt == 'min'): nside2 = 64 # 1.40625 deg per pixel npix2 = hp.nside2npix(nside2) myreq = np.zeros( npix2 ) # 0 off footprint, 1 at least one pass requirement, -1 none pass requirement ral = np.zeros(npix2) decl = np.zeros(npix2) mylabel = 'photometric-pixels' print 'Plotting photometric requirement' for i in range(0, len(val)): th, phi = hp.pix2ang(int(nside), pix[i]) ipix = hp.ang2pix(nside2, th, phi) dF = 1.086 * (sqrt(val[i]) ) # 1.086 converts d(magnitudes) into d(flux) if (dF < phreq): myreq[ipix] = 1 else: if (myreq[ipix] == 0): myreq[ipix] = -1 for i in range(0, len(myreq)): th, phi = hp.pix2ang(int(nside2), pix[i]) ra, dec = thphi2radec(th, phi) ral[i] = ra decl[i] = dec #myval = np.zeros(npix2) #mycount = np.zeros(pix2) #myval[ipix] += dF #mycount[ipix] += 1. below = sum(x for x in myreq if x < phreq) print 'Number of pixels offplot with ', mylabel, ' < ', phreq, ' is', below vmin = min(myreq) vmax = max(myreq) map = plt.scatter(ral, decl, c=myreq, cmap=cm.rainbow, s=5., vmin=vmin, vmax=vmax, lw=0, edgecolors='none') cbar = plt.colorbar(map) plt.xlabel('r.a. (degrees)') plt.ylabel('declination (degrees)') plt.title('Map of ' + mylabel + ' for ' + catalogue_name + ' ' + band + '-band') plt.xlim(0, 360) plt.ylim(-30, 90) plt.savefig(localdir + mylabel + '_' + band + '_' + catalogue_name + str(nside) + '.png') plt.close() #plt.show() #cbar.set_label(r'5$\sigma$ galaxy depth', rotation=270,labelpad=1) #plt.xscale('log') return True
def QU_sampler(QU, qu, x_mean, x_err, mask, data_mean,\ Nside=256, Niter=1000, R_Pp=None): """ Input: - QU, 2d array of submm polarization (2, Npix) - qu, 2d array of visual polarization (2, Npix) - x_mean, seq. of mean parameters values to be fitted. (R_Pp, Qbkgr, Ubkgr) ... Return: QU stat and background. list of arrays """ print(x_mean) print(x_err) burnin = int(Niter/2) print(burnin) cov0 = Cov(x_mean) pixels = np.arange(hp.nside2npix(Nside)) QU_model = np.zeros((2, hp.nside2npix(Nside))) QU_err = np.zeros((2, hp.nside2npix(Nside))) params_maxL = np.zeros((len(x_mean), hp.nside2npix(Nside))) params = np.zeros((Niter, len(x_mean), hp.nside2npix(Nside))) #print(R_Pp) dt_mean = np.array([-R_Pp, np.zeros(len(mask)), np.zeros(len(mask))]) print('-----------') mod = QU_func(data_mean, qu[:,mask]) mod2 = QU_func(dt_mean, qu[:,mask]) #print(QU[:,mask], np.mean(QU[:,mask], axis=1), np.std(QU[:,mask], axis=1)) #print(mod, np.mean(mod, axis=1), np.std(mod, axis=1)) #print(mod2, np.mean(mod2, axis=1), np.std(mod2, axis=1)) #print(logLike(mod, QU[:,mask])) #print(QU_func(np.array([-R_Pp[0], -0.005, 0.025]), qu[:,mask[0]])) res = (QU[:,mask] - mod)/QU[:,mask] res2 = (QU[:,mask] - mod2)/QU[:,mask] #print(res/res2) print(np.mean(QU[:,mask]/qu[:,mask], axis=1)) print(np.mean(R_Pp*qu[0,mask]), np.mean(R_Pp*qu[1,mask])) """ plt.scatter(res[0,:], res[1,:], marker='.', c=R_Pp, cmap='jet', vmin=3.8,\ vmax=5.2) plt.colorbar() plt.grid(True) plt.figure() plt.scatter(qu[0,mask], QU[0,mask], marker='x', c='k') plt.scatter(qu[1,mask], QU[1,mask], marker='x', c='b') #plt.scatter(qu[0,mask], mod[0], marker='.', c=R_Pp, cmap='jet') #plt.scatter(qu[1,mask], mod[1], marker='.', c=R_Pp, cmap='jet') plt.scatter(qu[0,mask], mod2[0], marker='.', c=R_Pp, cmap='brg',\ vmin=3.8, vmax=5.2) plt.scatter(qu[1,mask], mod2[1], marker='.', c=R_Pp, cmap='brg',\ vmin=3.8, vmax=5.2) plt.colorbar() """ t0 = time.time() for i, pix in enumerate(pixels[mask]): t01 = time.time() # Initiate functions: log_like = partial(logLike, data=QU[:,pix]) log_prior = partial(logPrior, mu=data_mean, sigma=x_err) func = partial(QU_func, qu=qu[:,pix]) # Initialize: params0, model0, loglike0, logprior0 = Initialize(log_like,\ log_prior,\ func, x_mean,\ cov0) # Metropolis Hastrings: params_maxL[:,pix], params[:,:,pix] = MH(log_like, log_prior,\ func, params0, model0,\ loglike0, logprior0,\ x_mean, cov0, burnin, Niter) # print(QU[:,pix], QU_func(params_maxL[:,pix], qu[:,pix])) #QU_model[:,pix] = QU_func(params_maxL[:,pix], qu[:,pix]) #QU_err[:,pix] = None #print(np.mean(params[:,:,pix],axis=0), np.std(params[:,:,pix],axis=0)) t11 = time.time() print('Sampling time for pixel {}: {} s'.format(pix, t11-t01)) print('-->') #break # t2 = time.time() print('Total sampling time: {} s'.format(t2-t0)) print('===================') #plot_params(params_maxL, xlab=[], ylab=[]) #plot_params(params, hist=True, xlab=[], ylab=[]) #plot_params(params, xlab=[], ylab=[]) #print(params_maxL[:, mask]) plt.subplot(311) plt.plot(params_maxL[0,mask], '.r') plt.subplot(312) plt.plot(params_maxL[1,mask], '.k') plt.subplot(313) plt.plot(params_maxL[2,mask], '.b') print(np.mean(params_maxL[:,mask], axis=1), np.std(params_maxL[:,mask], axis=1)) print(np.shape(QU_err[:,mask])) params_err = error_est(params[burnin:,:,mask], model=False) QU_model[:, mask] = QU_func(params_maxL[:,mask], qu[:,mask]) QU_err[:,mask] = error_est(params[burnin:,:,mask], qu[:,mask], model=True) res_mod = (QU[:,mask]-QU_model[:,mask])/QU[:,mask] #print(QU_model[:,mask]) print(np.mean(QU[:,mask], axis=1), np.std(QU[:,mask], axis=1)) print(np.mean(QU_model[:,mask], axis=1), np.std(QU_model[:,mask], axis=1)) #print(res_mod) R_mod = np.sqrt((QU_model[0,mask]**2 + QU_model[1,mask]**2) \ / (qu[0,mask]**2 + qu[1,mask]**2)) plt.figure() plt.plot(R_Pp, R_mod, '.g') plt.plot(R_Pp, -params_maxL[0,mask], '.r') print('Stellar and background polarisation') QU_star = QUstar(params_maxL[0,mask], qu[:,mask]) QU_bkgr = QUbkgr(params_maxL[1:,mask]) print(np.mean(QU[:,mask], axis=1)) print(np.mean(QU_star, axis=1), np.std(QU_star, axis=1)) print(np.mean(QU_bkgr, axis=1), np.std(QU_bkgr, axis=1)) print(np.mean(R_mod), np.std(R_mod)) plot_model1(qu, QU, QU_model, R_Pp) #""" plt.figure() plt.scatter(qu[0,mask], QU_star[0,:], c='k', marker='^') plt.scatter(qu[1,mask], QU_star[1,:], c='b', marker='^') plt.scatter(qu[0,mask], QU_model[0,mask], c='grey', marker='^') plt.scatter(qu[1,mask], QU_model[1,mask], c='skyblue', marker='^') plt.figure() plt.scatter(qu[0,mask], QU_bkgr[0,:], c='k', marker='^') plt.scatter(qu[1,mask], QU_bkgr[1,:], c='b', marker='^') #plt.ylim(-0.01, 0.015) #""" return None
#!/usr/bin/env python import numpy as np import healpy as hp from lsst.sims.catUtils.dust import EBVbase if __name__ == '__main__': # Read in the Schelgel dust maps and convert them to a healpix map dustmap = EBVbase() dustmap.load_ebvMapNorth() dustmap.load_ebvMapSouth() # Set up the Healpixel map nsides = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] for nside in nsides: lat, ra = hp.pix2ang(nside, np.arange(hp.nside2npix(nside))) # Move dec to +/- 90 degrees dec = np.pi / 2.0 - lat ebvMap = dustmap.calculateEbv(equatorialCoordinates=np.array([ra, dec]), interp=False) np.savez('dust_nside_%s.npz' % nside, ebvMap=ebvMap)
def mwa_fee_model(out_dir, nside, pointings=[0, 2, 4, 41], flags=[]): """ Create MWA FEE beam models at multiple pointings, with dipoles flagged. :param out_dir: Path to output directory where beam maps and sample plots will be saved :param nside: The :samp:`NSIDE` of healpix output map :class:`~int` :param pointings: :class:`~list` of pointings at which to make beam maps :param flags: :class:`~list` of dipoles which are to be flagged with values from 1 to 32. 1-16 are dipoles of XX pol while 17-32 are for YY. Ex: flags=[1,17] represents the first dipole of the XX & YY tiles as being flagged and having a amplitude of 0 :returns: - A set of images of the beam maps, save to :samp:`out_dir` - A :func:`~numpy.savez_compressed` :samp:`.npz` file containing all the fee beam maps """ # make output directory if it doesn't exist out_dir = f"{out_dir}/mwa_fee" Path(out_dir).mkdir(parents=True, exist_ok=True) # Custom Jade colormap jd, _ = jade() # Default amplitudes of 1 for all dipoles amps = np.ones((2, 16)) # Assign aplitudes of 0 to dipoles identified in flags if len(flags) != 0: xx_flags = [f - 1 for f in flags if f <= 16] yy_flags = [f - 17 for f in flags if f > 16] amps[0][xx_flags] = 0 amps[1][yy_flags] = 0 fee_beam = {} for p in pointings: # Empty array for model beam npix = hp.nside2npix(nside) beam_response_XX = np.zeros(npix) beam_response_YY = np.zeros(npix) # healpix indices above horizon # convert to zenith angle and azimuth above_horizon = range(int(npix / 2)) beam_zas, beam_azs = hp.pix2ang(nside, above_horizon) # Sweet-spot pointing delays from mwa_pb delay_point = np.array( [all_grid_points[p][-1], all_grid_points[p][-1]]) # Make beam response response = local_beam( [list(beam_zas)], [list(beam_azs)], freq=137e6, delays=delay_point, zenithnorm=True, power=True, interp=False, amps=amps, ) response_XX = response[0][0] response_YY = response[1][0] # Stick in an array, convert to decibels, and noralise beam_response_XX[above_horizon] = response_XX decibel_beam_XX = 10 * np.log10(beam_response_XX) normed_beam_XX = decibel_beam_XX - decibel_beam_XX.max() beam_response_YY[above_horizon] = response_YY decibel_beam_YY = 10 * np.log10(beam_response_YY) normed_beam_YY = decibel_beam_YY - decibel_beam_YY.max() fee_beam[str(p)] = [normed_beam_XX, normed_beam_YY] plt.style.use("seaborn") fig = plt.figure(figsize=(6, 6)) fig.suptitle(f"MWA FEE MAP @ pointing [{p}] XX", fontsize=16, y=0.92) plot_healpix(data_map=normed_beam_XX, sub=(1, 1, 1), cmap=jd, vmin=-50, vmax=0) plt.savefig(f"{out_dir}/mwa_fee_beam_{p}_XX.png", bbox_inches="tight") plt.close() fig = plt.figure(figsize=(6, 6)) fig.suptitle(f"MWA FEE MAP @ pointing [{p}] YY", fontsize=16, y=0.92) plot_healpix(data_map=normed_beam_YY, sub=(1, 1, 1), cmap=jd, vmin=-50, vmax=0) plt.savefig(f"{out_dir}/mwa_fee_beam_{p}_YY.png", bbox_inches="tight") plt.close() np.savez_compressed(f"{out_dir}/mwa_fee_beam.npz", **fee_beam)
def ang_power_spectra(self): #ngular power spectra cls = np.zeros(shape=(len(self.maps), self.freq_channels, self.cl_length)) j = self.counter for f in self.sorted_files: print("File number: {}".format(j)) files = h5py.File(f, 'r') info = files['index_map'] freq = info['freq'][:] map_dset = files['map'] # row and column sharing for i in range(freq.shape[0]): #fig = plt.figure(figsize=(15.0, 4.0)) #Mask maps map_I = map_dset[i, self.pol] #Unmasked map mask = map_dset[i, self.pol].astype(np.bool) map_I_masked = hp.ma(map_I) #loads a map as a masked array pixel_theta, pixel_phi = hp.pix2ang( self.nside, np.arange(hp.nside2npix(self.nside))) #print(pixel_theta, pixel_phi) mask[pixel_theta < self.theta_min * np.pi / 180] = 0 mask[pixel_theta > self.theta_max * np.pi / 180] = 0 map_I_masked.mask = np.logical_not(mask) fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15.0, 4.0)) ax3.xaxis.labelpad = 15 plt.subplots_adjust(top=2.0) #ax1 = fig.add_subplot(1, 2, 1) #ax2 = fig.add_subplot(1, 2, 2) #ax3 = fig.add_subplot(1, 2, 3) if i == 0: fig.suptitle('{}'.format(self.name)) plt.axes(ax3) hp.mollview(map_I_masked.filled(), coord=['C'], \ unit='K', norm='hist', xsize=2000, return_projected_map=True, hold=True) #plt.savefig('/home/elimboto/Desktop/Forecast_Codes/hi_im_pipeline/BINGO_strip%s.pdf'% ("%.4f" % round(i, 4)), format='pdf', bbox_inches='tight') #hp.graticule() #Remove all the masked pixels and return a standard array compressed_map = map_I_masked.compressed() print("The number of UNSEEN pixels:", 12 * self.nside**2 - len(compressed_map)) ax1.hist(compressed_map, bins=50, color='green', facecolor='lightblue') for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) #ax1.set_title('{} Hist, freq={}'.format(self.name, ("%.4f" % round(freq[i][0], 4))), fontsize=20) #plt.show() cl = hp.sphtfunc.anafast(map_I_masked.filled(), lmax=None) #ax2.set_title("freq = {}".format("%.4f" % round(freq[i][0], 4)), fontsize=20) ax2.set_xlabel(r"$\ell$") ax2.loglog(cl, label="freq = %s" % ("%.4f" % round(freq[i][0], 4))) ax2.legend(loc=1) ax2.set_ylim(ymax=9.e-10, ymin=1.e-15) ax2.set_xlim(xmax=10e3, xmin=0) fig.tight_layout() fig.savefig( '/home/elimboto/Desktop/Forecast_Codes/hi_im_pipeline/BINGO_cl%s.pdf' % ("%.4f" % round(i, 4)), format='pdf', bbox_inches='tight') #plt.show() cls[j][i] = cl j += 1 return cls
def analyzeephemskycatalog(params): filters = params["filters"] colors = params["colors"] for filter, color in zip(filters, colors): plotDir = os.path.join(params["analyzeephemplotpath"], filter) if not os.path.isdir(plotDir): os.mkdir(plotDir) # Read in all the photometry and package it up and save it # Set up LSST observatory filename = os.path.join(params["ephemplotpath"], "skyData_%s.npz" % filter) data_out = np.load(filename) skyData = data_out['skyData'] # Let's look at some of the photometry data nside = 16 #good=np.where(skyData['mjd'] == skyData['mjd'][0])[0] #plt.scatter(skyData['ra'][good], skyData['dec'][good], c=skyData['sky'][good]) hpid = radec2pix(nside, np.radians(skyData['ra']), np.radians(skyData['dec'])) sids = np.unique(hpid) count = 0 for sid in sids: continue plt.figure() good = np.where((hpid == sid) & (skyData['sunAlt'] < np.radians(-20.)))[0] plt.scatter(np.degrees(skyData['moonAlt'][good]), skyData['moonPhase'][good], c=skyData['sky'][good], alpha=.1) plt.xlabel('Moon Altitude (deg)') plt.ylabel('Moon Phase') plt.title('Sun down, healpix id = %i' % sid) cb = plt.colorbar() cb.set_label('Sky brightness (mags/area)') #plotName = os.path.join(plotDir,'moonSB_%i.png'%sid) plotName = os.path.join(plotDir, 'moonSB_%i.png' % count) plt.savefig(plotName) plt.figure() good = np.where((hpid == sid) & (skyData['alt'] > np.radians(10.)) & (skyData['sunAlt'] < np.radians(-20.)) & (skyData['moonAlt'] < np.radians(-20.)))[0] plt.plot(np.degrees(skyData['alt'][good]), skyData['sky'][good], 'ko', alpha=.1) plt.gca().invert_yaxis() # mags! plt.xlabel('Altitude (degrees)') plt.ylabel('Sky (mags/area)') plt.title('Moon and Sun down, healpix id = %i' % sid) #plotName = os.path.join(plotDir,'altSB_%i.png'%sid) plotName = os.path.join(plotDir, 'altSB_%i.png' % count) plt.savefig(plotName) plt.figure() good = np.where((hpid == sid) & (skyData['sunAlt'] > np.radians(-20)))[0] plt.scatter(np.degrees(skyData['sunAlt'][good]), skyData['sky'][good], c=np.degrees(skyData['moonAlt'][good]), alpha=.5) plt.gca().invert_yaxis() # mags! plt.xlabel('Sun Altitude (degrees)') plt.ylabel('Sky (mags/area)') plt.title('Sun up, healpix id = %i' % sid) cb = plt.colorbar() cb.set_label('Moon Alt (deg)') #plotName = os.path.join(plotDir,'sunSB_%i.png'%sid) plotName = os.path.join(plotDir, 'sunSB_%i.png' % count) plt.savefig(plotName) count = count + 1 moviedir = os.path.join(plotDir, "movie") if not os.path.isdir(moviedir): os.mkdir(moviedir) count = 0 mjds = np.unique(skyData['mjd'])[0:1000] for mjd in mjds: #continue # Try plotting a single sky shot: good = np.where(skyData['mjd'] == mjd)[0] # So how do we model things? Each HP gets a base spectrum, and then some added flux based on increased airmass, moon, and sun? dayMap = healbin(np.radians(skyData['ra'][good]), np.radians(skyData['dec'][good]), skyData['sky'][good], nside=nside) hp.mollview(dayMap, title='single Frame MJD=%f' % skyData['mjd'][good][0], unit='Surface Brightness (mag/area)') plotName = os.path.join(moviedir, 'singleFrameMap-%04d.png' % count) plt.savefig(plotName) count = count + 1 moviefiles = os.path.join(moviedir, "singleFrameMap-%04d.png") filename = os.path.join(moviedir, "singleFrameMap.mpg") ffmpeg_command = 'ffmpeg -an -y -r 20 -i %s -b:v %s %s' % ( moviefiles, '5000k', filename) os.system(ffmpeg_command) filename = os.path.join(moviedir, "singleFrameMap.gif") ffmpeg_command = 'ffmpeg -an -y -r 20 -i %s -b:v %s %s' % ( moviefiles, '5000k', filename) os.system(ffmpeg_command) rm_command = "rm %s/*.png" % (moviedir) #os.system(rm_command) # Let's make a minimum brightness map over the sky minSkyMap = np.ma.masked_all(hp.nside2npix(nside), dtype=float) minSkyMap.fill_value = hp.UNSEEN maxAltMap = minSkyMap.copy() altPad = 5. # Degrees perClip = 90. # Percentile clip to get rid of clouds and outliers order = np.argsort(hpid) skyData = skyData[order] hpid = hpid[order] ids = np.arange(hp.nside2npix(nside)) left = np.searchsorted(hpid, ids) right = np.searchsorted(hpid, ids, side='right') for sid, le, ri in zip(ids, left, right): if le != ri: if not np.isnan(np.max(skyData['alt'][le:ri])): maxAlt = np.max(skyData['alt'][le:ri]) good2 = np.where(skyData['alt'][le:ri] > maxAlt - altPad) brightness = skyData['sky'][le:ri][good2] minSkyMap[sid] = np.percentile(brightness, perClip) maxAltMap[sid] = maxAlt hp.mollview(minSkyMap, unit='mag/area', title='Faintest Observed Sky Values') plotName = os.path.join(plotDir, 'minSkyMap.png') plt.savefig(plotName) hp.mollview(np.degrees(maxAltMap), unit='degrees', title='Max Altitude') plotName = os.path.join(plotDir, 'maxAltMap.png') plt.savefig(plotName)
#one was a dec band, so checked that def is_in_dec(region, data_dec, data_ra): ras = region['ras'] decs = region['decs'] out = np.zeros(len(data_dec), dtype=bool) d1, d2 = decs out += ((d1 <= data_dec) & (data_dec < d2) & ~in_region(region, data_dec, data_ra)) return out #This determines the resolution of your map, in terms of n (number of pixels) n = hp.nside2npix(2**8) #made an empty space the size of the resolution binspace = np.zeros(n) #and made one for coordinates bin_dec = np.zeros(n) bin_ra = np.zeros(n) #so this makes a set of angles (phi, theta) for the pixels #you literally have to calculate them... #Then I appended the angles to the dec and ra arrays, where #dec just needed to be converted from phi for i in range(len(bin_dec)): [theta, phi] = hp.pix2ang(2**8, i)
def pixelNumberValid(NSIDE,pixelNumber): return int(pixelNumber)>=0 and int(pixelNumber)<hp.nside2npix(NSIDE)
# https://healpy.readthedocs.org/en/latest/generated/healpy.visufunc.projplot.html#healpy.visufunc.projplot ## to plot a 2D histogram in the Mollweide projection # define resolution of map # NSIDE = 32 NSIDE = 128 # find the pixel ID for each point # pix = hp.pixelfunc.ang2pix(NSIDE, theta, phi) pix_lamost = hp.pixelfunc.ang2pix(NSIDE, theta_lamost, phi_lamost) pix_apogee = hp.pixelfunc.ang2pix(NSIDE, theta_apogee, phi_apogee) pix_all = hp.pixelfunc.ang2pix(NSIDE, theta_all, phi_all) # pix is in the order of ra and dec # prepare the map array m_lamost = hp.ma(np.zeros(hp.nside2npix(NSIDE), dtype='float')) mask_lamost = np.zeros(hp.nside2npix(NSIDE), dtype='bool') for pix_val in np.unique(pix_lamost): choose = np.where(pix_lamost == pix_val)[0] if len(choose) == 1: m_lamost[pix_val] = am_lamost[choose[0]] else: m_lamost[pix_val] = np.median(am_lamost[choose]) mask_lamost[np.setdiff1d(np.arange(len(m_lamost)), pix_lamost)] = 1 m_lamost.mask = mask_lamost m_apogee = hp.ma(np.zeros(hp.nside2npix(NSIDE), dtype='float')) mask_apogee = np.zeros(hp.nside2npix(NSIDE), dtype='bool')
def __init__(self,NSIDE,nest=True): self.NSIDE = NSIDE self.nest = nest self.galaxyCounts = np.zeros(hp.nside2npix(self.NSIDE)) return
def scattomap(dec, ra, nside=32): hmap = np.bincount(hp.ang2pix(nside, np.deg2rad(90. - dec), np.deg2rad(ra)), minlength=hp.nside2npix(nside)) return hmap
def get_sky_table(database, table, output, tiles=None, tile_nside=32, candidate_nside=32768, min_separation=10, ra_column='ra', dec_column='dec', mag_column=None, is_flux=False, radius_column=None, flux_unit='nMgy', scale_a=0.2, scale_b=1.0, mag_threshold=None, calculate_min_separation=True, nsample=2048, downsample_data=None, downsample_nside=256, n_cpus=1, seed=None): r"""Identifies skies from a database table. Skies are selected using the following procedure: - The sky is divided in HEALPix "tiles" of nside ``tile_nside``. For each tile the catalogue is queried to retrieve all the targets that lie in the tile footprint. This assumes that the ``healpix_ang2ipix_nest`` Postgresql function from `pg_healpix <https://github.com/segasai/pg_healpix>`__ is available. It's recommended that an index is created for the tiling norder to speed the query. - Each tile is subsequently divided in pixels of nside ``candidate_nside``. Pixels that contain a catalogue target are removed as possible sky candidates. For each target with magnitude less than ``mag_threshold`` all pixels within a ``min_separation`` are rejected. The remaining pixels are considered valid skies. Alternatively, if ``mag_column`` and ``mag_threshold`` are defined, the minimum separation to valid pixels is corrected using the expression :math:`s^* = s + \dfrac{(m_{thr}-m)^{\beta}}{a}` where :math:`s` is the minimum separation, :math:`m_{thr}` is the magnitude threshold, :math:`a=0.2` and :math:`\beta=1.0` are factors that control the relationship between the star's magnitude and the exclusion radius. - If ``nsample``, only that number of skies are returned for each tile. The tile is divided in pixels of nside ``downsample_nside`` (which must be smaller than ``candidate_nside`` but larger than ``tile_nside``) and for each downsample pixel ``int(downsample / downsample_npix) + 1`` skies are selected. If not enough valid positions can be selected in this way, each downsample pixel quota is completed with the invalid positions that have a larger separation to their nearest neighbour. - The process can be parallelised for each tile and the results are compiled in a single Pandas data frame that is saved to an HDF5 file. All pixel values use the nested ordering. Parameters ---------- database : ~sdssdb.connection.PeeweeDatabaseConnection A valid database connection. table : str Name of the table to query, optionally schema-qualified. output : str Path to the HDF5 file to write. tiles : list A list of HEALPix pixels of nside ``tile_nside`` for which the sky selection will be done. If `None`, runs for all the pixels of nside ``tile_nside``. tile_nside : int The HEALPix nside to use to tile the all-sky catalogue. candidate_nside : int The HEALPix nside used to identify candidate pixels in each tile. Candidates are then checked to confirm that their closest neighbour is at least ``min_separation`` arcsec away. min_separation : int The minimum separation, in arcsec, between skies and their closest neighbour in the catalogue. ra_column : str The name of the column in ``table`` that contains the Right Ascension coordinates, in degrees. dec_column : str The name of the column in ``table`` that contains the Declination coordinates, in degrees. mag_column : str The name of the column in ``table`` with the magnitude to be used to scale ``min_separation``. is_flux : bool If `True`, assumes the ``mag_column`` values are given as fluxes (units of flux_unit). flux_unit : str Gives the units of flux in the 'mag_column' - known values 'nMgy', 'Jy' mag_threshold : float The value below which the separation to neighbouring sources will be scaled. radius_column : str Name of the database column that provided the object radius (an alternative to mag_column useful for extended sources) scale_a : float Value of :math:`a` in the radius vs mag relationship scale_b : float Value of :math:`\beta` in the radius vs mag relationship calculate_min_separation : bool If `True`, calculates the separation to the nearest neighbour for each candidate sky position. nsample : int or None The total number of skies to retrieve for each tile. If `None`, returns all candidate skies. downsample_nside : int The HEALPix nside used for downsampling. If ``nside``, the resulting valid skies will be grouped by HEALPix pixels of this resolution. For each pixel a random sample will be drawn so that the total number of skies selected matches ``nsample``. downsample_data : pandas.DataFrame A data frame with previously selected skies that will be used to downsample the sky candidates. This is useful when trying to create a sky catalogue from multiple tables to ensure that the selected sky positions match across the various tables. If not enough valid skies can be selected from the sample in the data frame, they will be completed up to ``nsample``. n_cpus : int Number of CPUs to use for multiprocessing. seed : int The random state seed. Returns ------- skies : pandas.DataFrame The list of selected skies. """ assert database.connected, 'database is not connected.' columns = (f'healpix_ang2ipix_nest(' f'{tile_nside}, {ra_column}, {dec_column}) ' f'AS tile_{tile_nside}, ' f'{ra_column} AS ra, {dec_column} AS dec') if mag_column and mag_threshold: columns += f', {mag_column} AS mag' if radius_column: columns += f', {radius_column} as radius' if tiles is None: tiles = numpy.arange(healpy.nside2npix(tile_nside)) if downsample_data is not None: downsample_data = downsample_data.loc[:, ['tile_32', 'valid']] downsample_data = downsample_data.loc[downsample_data.valid] query = (f'SELECT {columns} FROM {table} ' f'WHERE healpix_ang2ipix_nest(' f'{tile_nside}, {ra_column}, {dec_column}) = {{tile}};') pbar = enlighten.Counter(total=len(tiles), desc=output, unit='tiles') pbar.refresh() database_params = database.connection_params database_params.update({'database': database.dbname}) process_tile = partial(_process_tile, database_params=database_params, query=query, candidate_nside=candidate_nside, tile_nside=tile_nside, min_separation=min_separation, is_flux=is_flux, flux_unit=flux_unit, scale_a=scale_a, scale_b=scale_b, mag_threshold=mag_threshold, nsample=nsample, downsample_data=downsample_data, calculate_min_separation=calculate_min_separation, downsample_nside=downsample_nside, seed=seed) all_skies = None with multiprocessing.Pool(n_cpus) as pool: for tile_skies in pool.imap_unordered(process_tile, tiles, chunksize=5): if tile_skies is not False and len(tile_skies) > 0: if all_skies is None: all_skies = tile_skies else: all_skies = all_skies.append(tile_skies) pbar.update() if all_skies is None: return all_skies # Not sure why this is needed but it seems downsampling sometimes # converts the "valid" column to object. all_skies.valid = all_skies.valid.astype(bool) # Downcast some columns all_skies = all_skies.astype({f'tile_{tile_nside}': numpy.int16}) if 'sep_neighbour' in all_skies: all_skies = all_skies.astype({'sep_neighbour': numpy.float32}) if 'mag_neighbour' in all_skies: all_skies = all_skies.astype({'mag_neighbour': numpy.float32}) if downsample_nside is not None: all_skies = all_skies.astype({'down_pix': numpy.int32}) all_skies.to_hdf(output, 'data') return all_skies
phi = ra * np.pi/180. theta = (90.0 - dec) * np.pi/180. # to just plot all points, do hp.visufunc.projplot(theta, phi, 'bo') hp.visufunc.graticule() # more examples are here # https://healpy.readthedocs.org/en/latest/generated/healpy.visufunc.projplot.html#healpy.visufunc.projplot ## to plot a 2D histogram in the Mollweide projection # define the HEALPIX level NSIDE = 32 # find the pixel ID for each point pix = hp.pixelfunc.ang2pix(NSIDE, theta, phi) # select all points above Dec > -30 pix_unique = np.unique(pix[dec > -30]) # prepare the map array m = np.zeros(hp.nside2npix(NSIDE), dtype='u2') # tag the map array with pixels above Dec > -30 m[pix_unique] = 1 # plot map ('C' means the input coordinates were in the equatorial system) hp.visufunc.mollview(m, coord=['C'], rot=(0, 0, 0), notext=True, title='', cbar=False) hp.visufunc.graticule() plt.show()