def find_largest_hole(parameters,ar): minimal_distances = [] all_pixels = sp.array(range(len(ar))) empty_pixels = all_pixels[(ar[all_pixels] == parameters.badval)] if len(empty_pixels) == 0: print "no empty pixels" return 2*sp.pi/(6*parameters.nside) print "the number of empty pixels is", len(empty_pixels) nonempty_pixels = all_pixels[(ar[all_pixels] != parameters.badval)\ & (ar[all_pixels] != parameters.unseen)] for p in empty_pixels: minimal_distance = 3.14 theta,phi = hp.pix2ang(parameters.nside,p) for p_i in nonempty_pixels: theta_i, phi_i = hp.pix2ang(parameters.nside,p_i) angular_distance = hp.rotator.angdist([theta,phi],[theta_i,phi_i]) minimal_distance = sp.minimum(minimal_distance,angular_distance) minimal_distances.append(minimal_distance) radius_of_largest_hole = sp.amax(minimal_distances) print "The angular radius of largest hole = ", radius_of_largest_hole, "rad." return radius_of_largest_hole
def get_hpmask_subpix_indices(submask_nside, submask_hpix, submask_border, nside_mask, hpix): """ """ nside_cutref = np.clip(submask_nside * 4, 256, nside_mask) # Find out which cutref pixels are inside the main pixel theta, phi = hp.pix2ang(nside_cutref, np.arange(hp.nside2npix(nside_cutref))) ipring_coarse = hp.ang2pix(submask_nside, theta, phi) inhpix, = np.where(ipring_coarse == submask_hpix) # If there is a border, we need to find the boundary pixels if submask_border > 0.0: boundaries = hp.boundaries(submask_nside, submask_hpix, step=nside_cutref/submask_nside) # These are all the pixels that touch the boundary for i in xrange(boundaries.shape[1]): pixint = hp.query_disc(nside_cutref, boundaries[:, i], np.radians(submask_border), inclusive=True, fact=8) inhpix = np.append(inhpix, pixint) # Need to uniqify here because of overlapping pixels inhpix = np.unique(inhpix) # And now choose just those depthmap pixels that are in the inhpix region theta, phi = hp.pix2ang(nside_mask, hpix) ipring = hp.ang2pix(nside_cutref, theta, phi) _, use = esutil.numpy_util.match(inhpix, ipring) return use
def get_neighbors(map_fname, pixindex, mu_anchors=None): m = hputils.ExtinctionMap(map_fname, FITS=True) if mu_anchors == None: mu_anchors = m.mu # Query neighboring pixels neighbor_index = hp.pixelfunc.get_all_neighbours(m.nside, pixindex, nest=m.nested) Delta_Ar = m.evaluate(mu_anchors, pix_index=neighbor_index) Delta_Ar[1:] = Delta_Ar[1:] - Delta_Ar[:-1] mask = np.isfinite(Delta_Ar[0,:]) neighbor_index = neighbor_index[mask] Delta_Ar = Delta_Ar[:,mask] # Assign weight to each pixel based on distance theta, phi = hp.pix2ang(m.nside, neighbor_index, nest=m.nested) theta_0, phi_0 = hp.pix2ang(m.nside, pixindex, nest=m.nested) print theta - theta_0 print phi - phi_0 dist = np.arccos(np.sin(theta_0) * np.sin(theta) + np.cos(theta_0) * np.cos(theta) * np.cos(phi - phi_0)) sigma_dist = hp.pixelfunc.nside2resol(m.nside, arcmin=False) weight = np.exp(-dist * dist / (2. * sigma_dist * sigma_dist)) weight /= np.sum(weight) # Return reddening in each bin for each neighboring pixel, as well as weight assigned to each neighbor return Delta_Ar.T, weight
def pixsep(nside,pix1,pix2): """return the seperation between two healpix pixels in degrees""" src1_loc = hpy.pix2ang(nside,pix1) src2_loc = hpy.pix2ang(nside, pix2) aa = a.scripting.get_null_aa() src1 = a.phs.RadioFixedBody(src1_loc[1],n.pi/2 - src1_loc[0]) src2 = a.phs.RadioFixedBody(src2_loc[1],n.pi/2 - src2_loc[0]) src1.compute(aa) src2.compute(aa) return ephem.separation(src1,src2)
def healpixels2dist(nside, hp1,hp2): """ Compute the angular distance between 2 healpixels """ lat1, ra1 = hp.pix2ang(nside,hp1) lat2, ra2 = hp.pix2ang(nside,hp2) dec1 = np.pi/2.0 - lat1 dec2 = np.pi/2.0 - lat2 angDist = haversine(ra1,dec1,ra2,dec2) return angDist
def get_sky_coords(self): """ Get the sky coordinates of all the pixels in this pixelization """ if self._ipix is None: theta, phi = hp.pix2ang( self._nside, list(range(self._npix)), self._nest) else: theta, phi = hp.pix2ang(self._nside, self._ipix, self._nest) lat = np.degrees((np.pi / 2) - theta) lon = np.degrees(phi) return np.vstack([lon, lat]).T
def pix2radec(nside,pix): """input a pixel index into an nside healpix vector return ra,dec (in radians) """ try: hp_loc = [hpy.pix2ang(nside,px) for px in pix] except(TypeError): hp_loc = [hpy.pix2ang(nside,pix)] ra = [loc[1] for loc in hp_loc] dec = [n.pi/2 - loc[0] for loc in hp_loc] return n.array(ra),n.array(dec)
def get_healsparse_subpix_indices(subpix_nside, subpix_hpix, subpix_border, coverage_nside): """ Retrieve the coverage pixels that intersect the region, with a border. Parameters ---------- subpix_nside: `int` Nside for the subregion subpix_hpix: `int` Pixel number for the subregion (ring format) subpix_border: `float` Border radius to cover outside subpix_hpix coverage_nside: `int` Nside of the healsparse coverage map """ # First, we need to know which pixel(s) from nside_coverage are covered by # subpix_hpix if subpix_nside == coverage_nside: # simply convert to nest covpix = hp.ring2nest(subpix_nside, subpix_hpix) elif subpix_nside > coverage_nside: # what pixel is this contained in? theta, phi = hp.pix2ang(subpix_nside, subpix_hpix, nest=False) covpix = hp.ang2pix(coverage_nside, theta, phi, nest=True) else: # This is subpix_nside < coverage_nside # what coverage pixels are contained in subpix_hpix? subpix_hpix_nest = hp.ring2nest(subpix_nside, subpix_hpix) bit_shift = 2 * int(np.round(np.log(coverage_nside / subpix_nside) / np.log(2))) n_pix = 2**bit_shift covpix = np.left_shift(subpix_hpix_nest, bit_shift) + np.arange(n_pix) # And now if we have a border... if subpix_border > 0.0: nside_testing = max([coverage_nside * 4, subpix_nside * 4]) boundaries = hp.boundaries(subpix_nside, subpix_hpix, step=nside_testing/subpix_nside) extrapix = np.zeros(0, dtype=np.int64) # These are pixels that touch the boundary for i in xrange(boundaries.shape[1]): pixint = hp.query_disc(nside_testing, boundaries[:, i], np.radians(subpix_border), inclusive=True, fact=8) extrapix = np.append(extrapix, pixint) extrapix = np.unique(extrapix) theta, phi = hp.pix2ang(nside_testing, extrapix) covpix = np.unique(np.append(covpix, hp.ang2pix(coverage_nside, theta, phi, nest=True))) return covpix
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 hp_pix2radec(nside, pix_inds): """ An RA/Dec version of healpy's pix2ang() function. """ npix = hp.nside2npix(nside) map_pix_inds = np.arange(npix) tm,pm = hp.pix2ang(nside, map_pix_inds) t,p = hp.pix2ang(nside, pix_inds) dec = np.pi/2. - t pm_max = np.amax(pm) ra = pm_max - p return ra, dec
def orfFromMap_fast(psr_locs, usermap, response=None): """ Calculate the correlation basis matrices using the pixel-space transormations @param psr_locs: Location of the pulsars [phi, theta] @param usermap: Provide a healpix map for GW power Note: GW directions are in direction of GW propagation """ if response is None: npsrs = len(psr_locs) pphi = psr_locs[:,0] ptheta = psr_locs[:,1] # Create the pixels nside = hp.npix2nside(len(usermap)) npixels = hp.nside2npix(nside) pixels = hp.pix2ang(nside, np.arange(npixels), nest=False) gwtheta = pixels[0] gwphi = pixels[1] # Create the signal response matrix F_e = signalResponse_fast(ptheta, pphi, gwtheta, gwphi) elif response is not None: F_e = response # Double the power (one for each polarization) sh = np.array([usermap, usermap]).T.flatten() # Create the cross-pulsar covariance hdcov_F = np.dot(F_e * sh, F_e.T) # The pulsar term is added (only diagonals: uncorrelated) return hdcov_F + np.diag(np.diag(hdcov_F))
def Healpix2RaDec(pixels, nside=None, nest=False): """ Compute the RA/DEC for each HEALPixel index in `pixels`. Parameters ---------- pixels (int array) Array of the pixel integers nside (int) HEALPix nside nest (bool) Whether or not to use nested format Returns ------- ra (float array) The RA coordinates of the entries (in degrees) dec (float array) The DEC coordinates of the entries (in degrees) """ _nsideExcept(nside) theta, phi = _hp.pix2ang(nside, pixels, nest=nest) dec = 90.0 - _np.degrees(theta) ra = _np.degrees(phi) return [ra, dec]
def clmFromMap(h, lmax): """ Given a pixel map, and a maximum l-value, return the corresponding C_{lm} values. @param h: Sky power map @param lmax: Up to which order we'll be expanding return: clm values Use real_sph_harm for the map """ npixels = len(h) nside = hp.npix2nside(npixels) pixels = hp.pix2ang(nside, np.arange(npixels), nest=False) clm = np.zeros( (lmax+1)**2 ) ind = 0 for ll in range(lmax+1): for mm in range(-ll, ll+1): clm[ind] += np.sum(h * real_sph_harm(mm, ll, pixels[1], pixels[0])) ind += 1 return clm * 4 * np.pi / npixels
def mapFromClm(clm, nside): """ Given an array of C_{lm} values, produce a pixel-power-map (non-Nested) for healpix pixelation with nside @param clm: Array of C_{lm} values (inc. 0,0 element) @param nside: Nside of the healpix pixelation return: Healpix pixels Use real_sph_harm for the map """ npixels = hp.nside2npix(nside) pixels = hp.pix2ang(nside, np.arange(npixels), nest=False) h = np.zeros(npixels) ind = 0 maxl = int(np.sqrt(len(clm)))-1 for ll in range(maxl+1): for mm in range(-ll, ll+1): h += clm[ind] * real_sph_harm(mm, ll, pixels[1], pixels[0]) ind += 1 return h
def test_transform(self): nside = 2**4 params = Struct(beam_nside = nside) strategy_coord = CoordSpec(0, 0, 0, 0, 0) beam_size = 0.041 angles = np.arange(-1,1,10)*beam_size/2 beam_spec = BeamSpec(angles, angles, 0) time_steps = 2 idx = np.arange(hp.nside2npix(nside)) thetas, phis = hp.pix2ang(nside, idx) tree = sphere.ArcKDTree(thetas, phis) ctx = Struct(params = params, strategy_coords = [strategy_coord for _ in range(time_steps)], beam_spec = beam_spec, arc_tree = tree) plugin = coord_transform.Plugin(ctx) plugin() assert ctx.beams is not None assert len(ctx.beams) == time_steps
def reproject_car_to_hpx(input_data, coord_system_out, nside, order=1, nested=False): import healpy as hp from reproject.wcs_utils import convert_world_coordinates from reproject.healpix.utils import parse_coord_system data, wcs_in = input_data npix = hp.nside2npix(nside) theta, phi = hp.pix2ang(nside, np.arange(npix), nested) lon_out = np.degrees(phi) lat_out = 90.0 - np.degrees(theta) # Convert between celestial coordinates coord_system_out = parse_coord_system(coord_system_out) with np.errstate(invalid="ignore"): lon_in, lat_in = convert_world_coordinates( lon_out, lat_out, (coord_system_out, u.deg, u.deg), wcs_in ) # Look up pixels in input system yinds, xinds = wcs_in.wcs_world2pix(lon_in, lat_in, 0) # Interpolate data = np.pad(data, 3, mode="wrap") healpix_data = map_coordinates( data, [xinds + 3, yinds + 3], order=order, mode="wrap", cval=np.nan ) return healpix_data, (~np.isnan(healpix_data)).astype(float)
def makeSkyMap(samples, lmax, nside=16, psrs=None, cmap='YlOrBr'): # number of pixels total npix = hp.nside2npix(nside) # initialize theta and phi map coordinantes skypos=[] for ii in range(npix): skypos.append(np.array(hp.pix2ang(nside,ii))) skypos = np.array(skypos) harmvals = PALutils.SetupSkymapPlottingGrid(lmax,skypos) pwr = [] for ii in range(len(samples)): samples_tot = np.append(2.*np.sqrt(np.pi), samples[ii]) gwp = PALutils.GWpower(samples_tot, harmvals) pwr.append(gwp) pwr = np.array(pwr) pwr = np.mean(pwr, axis=0) ax = plt.subplot(111, projection='astro mollweide') ax.grid() plot.outline_text(ax) plot.healpix_heatmap(pwr, cmap=cmap) plt.colorbar(orientation='horizontal') # add pulsars locations if np.all(psrs): ax.plot(psrs[:,0], psrs[:,1], '*', color='w', markersize=6, mew=1, mec='w') return pwr
def toHpix(cloudMap): """ Convert a CloudMap to a healpix image @returns a healpix image of the clouds @param cloudMap: a CloudMap with the cloud cover data For each pixel in hpix, sample from the corresponding pixel in cloudMap """ nside = cloudmap2nside(cloudMap.cloudData) cc = nside2cloudConfig(nside) hpix = np.zeros(cc['npix']) (theta, phi) = hp.pix2ang(cc['nside'], np.arange(cc['npix'])) r = np.tan(theta) * cc['z'] x = np.floor(r * np.cos(phi)).astype(int) y = np.floor(r * np.sin(phi)).astype(int) # ignore all pixels with zenith angle higher than thetaMax x = x[theta < cc['thetaMax']] y = y[theta < cc['thetaMax']] ipixes = np.arange(cc['npix'])[theta < cc['thetaMax']] hpix[ipixes] = cloudMap[x + cc['xyCent'], y + cc['xyCent']] return hpix
def eqmap2azelmap(map,longi=-104.245,lat=34.4717,ut=12.0,year=2014,month=5,day=29): """ function to rotate celestial coord map to az el with brute force using tools below from Victor Roytman """ julian_date=jdcnv(year,month,day,ut) obs=ephem.Observer() obs.lon=str(longi) obs.lat=str(lat) obs.date=ephem.date((year,month,day,ut)) nside=np.int(np.sqrt(len(map)/12)) outmap=np.zeros(len(map)) pixlist=range(len(map)) htheta,hphi=hp.pix2ang(nside,pixlist) elevation=np.pi/2. -htheta azimuth=hphi ctheta=[] cphi=[] for az,el in zip(azimuth,elevation): ra,dec=obs.radec_of(az,el) ctheta.append(np.pi/2. -dec) cphi.append(ra) #ra,dec=azel2radec(julian_date, azimuth, elevation, lat*dtr, longi) #ctheta=np.pi/2.-dec #cphi=ra ctheta=np.array(ctheta) cphi=np.array(cphi) rpixlist=hp.ang2pix(nside,ctheta,cphi) outmap[pixlist]=map[rpixlist] return outmap
def _interpolate_level(m): """Recursive multi-resolution interpolation. Modifies `m` in place.""" # Determine resolution. npix = len(m) if npix > 12: # Determine which pixels comprise multi-pixel tiles. ipix = np.flatnonzero( (m[0::4] == m[1::4]) & (m[0::4] == m[2::4]) & (m[0::4] == m[3::4])) if len(ipix): ipix = (4 * ipix + np.expand_dims(np.arange(4, dtype=np.intp), 1)).T.ravel() nside = hp.npix2nside(npix) # Downsample. m_lores = hp.ud_grade( m, nside // 2, order_in='NESTED', order_out='NESTED') # Interpolate recursively. _interpolate_level(m_lores) # Record interpolated multi-pixel tiles. m[ipix] = hp.get_interp_val( m_lores, *hp.pix2ang(nside, ipix, nest=True), nest=True)
def BeamMapComplex( self, RAnow, Bmap, freq ) : ''' return shape = (1, Nv, 12*nside**2) ''' freq = jp.Num(freq, float) RAnow = jp.Num(RAnow, float) Bmap = jp.npfmt(Bmap) coordtrans = jp.CoordTrans() nside = hp.get_nside(Bmap) theta, phi = hp.pix2ang(nside, np.arange(12*nside**2)) #-------------------------------------------------- # For Bmap, rotate points in CeleXYZ from RA=0(+x) to RA=RAnow. Rotate points is negetive angle, so az=-RAnow az = -RAnow if (az != 0) : pixnowB = hp.ang2pix(nside, theta, phi+az*np.pi/180) Bmap = Bmap[pixnowB] pixnowB = 0 #@ # Now Bmapnow is still in CeleXYZ #-------------------------------------------------- # For Tmap, don't need to rotate its points # Then for Tmap and Bmap(now), convert from CeleXYZ to AntXYZ. Note that here rotate XYZ, NOT points, so az=RAnow+90 from lon=0 to +AntX az = 90 + RAnow if (az != 0) : hpixXYZ = coordtrans.xyzRotation(self.hpixCeleXYZ.T, az=az).T else : hpixXYZ = self.hpixCeleXYZ.copy() #-------------------------------------------------- # phase = 2pi/c * \vec(B) * \vec(s) phase = (2*np.pi/300*freq * self.blXYZ[:,None,:] * hpixXYZ[None,:,:]).sum(-1) # (Nv, 12*nside**2) for 1 freq hpixXYZ = 0 #@ #-------------------------------------------------- Bmap = Bmap[None,:] * np.exp(1j*phase) return Bmap[None,:] # None for Nfreq=1
def get_mask_gal(percentage_keep=40, nside_out=512, coordinates='eq', quick=True): import pyfits from astropy.coordinates import FK5 from astropy import units as u savename = datadir+'mask_GAL0%i_%i_'%(percentage_keep,nside_out) savename += coordinates+'_.pkl' if quick: return pickle.load(open(savename,'r')) pp = pyfits.open(datadir+'HFI_Mask_GalPlane_2048_R1.10.fits') mask_gal = pp[1].data['GAL0%i'%percentage_keep] mask_gal = hp.reorder(mask_gal, out='RING', inp='NESTED') if coordinates=='gal': mask_out = hp.ud_grade(mask_gal, nside_out) if coordinates=='eq': nside_up = nside_out*2 mask_gal = hp.ud_grade(mask_gal, nside_up) # Find the indices in an up-sampled *galactic* map that belong to these # *equatorial* coordinates. theta, phi = hp.pix2ang(nside_up, np.arange(hp.nside2npix(nside_up))) ra = phi dec = np.pi/2.-theta coord = FK5(ra=ra, dec=dec, unit=(u.rad, u.rad)) l_gal = coord.galactic.l.rad b_gal = coord.galactic.b.rad phi = l_gal theta = np.pi/2.-b_gal ind_up = hp.ang2pix(nside_up, theta, phi) mask_up_eq = mask_gal[ind_up] mask_out = hp.ud_grade(mask_up_eq, nside_out) pickle.dump(mask_out, open(savename,'w')) return mask_out
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 get_haslam(nside_out=512, coordinates='eq', quick=True): savename = datadir+'haslam_'+coordinates+'_%i.pkl'%nside_out if quick: return pickle.load(open(savename,'r')) radio = hp.read_map(datadir+'lambda_haslam408_dsds.fits') if coordinates=='gal': radio_out = hp.ud_grade(radio, nside_out) if coordinates=='eq': from astropy.coordinates import FK5 from astropy import units as u # Up-sample and convert from galactic to equatorial. nside_up = nside_out*2 radio_up_gal = hp.ud_grade(radio, nside_up) # Find the indices in an up-sampled *galactic* map that belong to these # *equatorial* coordinates. theta, phi = hp.pix2ang(nside_up, np.arange(hp.nside2npix(nside_up))) ra = phi dec = np.pi/2.-theta coord = FK5(ra=ra, dec=dec, unit=(u.rad, u.rad)) l_gal = coord.galactic.l.rad b_gal = coord.galactic.b.rad phi = l_gal theta = np.pi/2.-b_gal ind_up = hp.ang2pix(nside_up, theta, phi) radio_up_eq = radio_up_gal[ind_up] radio_out = hp.ud_grade(radio_up_eq, nside_out) mask_out = np.ones_like(radio_out) pickle.dump((radio_out, mask_out), open(savename,'w')) return radio_out, mask_out
def find_highest_probability_pixel( sky_map ): """ finding the highest probability pixel RA[deg], DEC[deg]. """ import healpy as hp import numpy as np # read probability skymap hpx = hp.read_map( sky_map, verbose = False ) # number of pixels npix = len( hpx ) # nside: resolution for the HEALPix map nside = hp.npix2nside( npix ) # pixel position ipix_max = np.argmax( hpx ) hpx[ ipix_max ] # sky coordinate theta, phi = hp.pix2ang( nside, ipix_max ) ra_max = np.rad2deg( phi ) dec_max = np.rad2deg( 0.5 * np.pi - theta ) print ' or insert the highest probability pixel located \n at RA =', str(( '% .5f' % ra_max))+'°', 'and Dec =',str(( '% .5f' % dec_max))+'°.'
def PAPER_transform_basis(nside, jones, z0_cza, R_z0): npix = hp.nside2npix(nside) hpxidx = np.arange(npix) cza, ra = hp.pix2ang(nside, hpxidx) fR = R_z0 tb, pb = rotate_sphr_coords(fR, cza, ra) cza_v = t_hat_cart(cza, ra) ra_v = p_hat_cart(cza, ra) tb_v = t_hat_cart(tb, pb) fRcza_v = np.einsum('ab...,b...->a...', fR, cza_v) fRra_v = np.einsum('ab...,b...->a...', fR, ra_v) cosX = np.einsum('a...,a...', fRcza_v, tb_v) sinX = np.einsum('a...,a...', fRra_v, tb_v) basis_rot = np.array([[cosX, sinX],[-sinX, cosX]]) basis_rot = np.transpose(basis_rot,(2,0,1)) # return np.einsum('...ab,...bc->...ac', jones, basis_rot) return irnf.M(jones, basis_rot)
def makeSkyMap(samples, lmax, nside=32, tex=True, psrs=None): if tex == True: plt.rcParams['text.usetex'] = True #nside=32 npix = hp.nside2npix(nside) # number of pixels total # initialize theta and phi map coordinantes skypos=[] for ii in range(npix): skypos.append(np.array(hp.pix2ang(nside,ii))) skypos = np.array(skypos) harmvals = utils.SetupSkymapPlottingGrid(lmax,skypos) if np.atleast_2d(samples).shape[0]>1: samples = np.mean(samples, axis=0) samples = np.append(2.*np.sqrt(np.pi), samples) pwr = utils.GWpower(samples, harmvals) ax = plt.subplot(111, projection='astro mollweide') ax.grid() plot.outline_text(ax) plot.healpix_heatmap(pwr[::-1]) plt.colorbar(orientation='horizontal') plt.suptitle(r'$\langle P_{\mathrm{GWB}}(-\hat\Omega)\rangle$', y=0.1) #plt.suptitle(r'$P_{\mathrm{GWB}}^{\mathrm{ML}}(-\hat\Omega)$', y=0.1) # add pulsars locations if psrs is not None: ax.plot(psrs[:,0], psrs[:,1], '*', color='w', markersize=6, mew=1, mec='w')
def hp_pix2rh_pix2ang(nside, pix_inds): """ A right-handed version of healpy's pix2ang() function. """ npix = hp.nside2npix(nside) map_pix_inds = np.arange(npix) tm,pm = hp.pix2ang(nside, map_pix_inds) tl,pl = hp.pix2ang(nside, pix_inds) pm_max = np.amax(pm) t = tl p = pm_max - p return t,p
def test_map2alm_pol_gal_cut(self): tmp = [np.empty(o.size * 2) for o in self.mapiqu] for t, o in zip(tmp, self.mapiqu): t[::2] = o maps = [ self.mapiqu, [o.astype(np.float32) for o in self.mapiqu], [t[::2] for t in tmp], ] for use_weights in [False, True]: for input in maps: gal_cut = 30 nside = hp.get_nside(input) npix = hp.nside2npix(nside) gal_mask = ( np.abs(hp.pix2ang(nside, np.arange(npix), lonlat=True)[1]) < gal_cut ) alm = hp.map2alm( input, iter=10, use_weights=use_weights, gal_cut=gal_cut ) output = hp.alm2map(alm, 32) for i, o in zip(input, output): # Testing requires low tolerances because of the # mask boundary i[gal_mask] = 0 np.testing.assert_allclose(i, o, atol=1e-2)
def rotate_jones(j, rotmat, multiway=True): """ Rotates the scalar components of a complex-valued 2x2 matrix field, relative to the Healpix coordinate frame. """ if multiway == True: j = flatten_jones(j) npix = len(j[:,0]) nside = hp.npix2nside(npix) hpxidx = np.arange(npix) c, a = hp.pix2ang(nside, hpxidx) t, p = rotate_sphr_coords(rotmat, c, a) intp = lambda m: hp.get_interp_val(m, t, p) # This is the fastest by ~2%. 99.1ms vs 101ms for the loop. (at nside=2**6) # ...totally worth it! jones = (np.asarray(map(intp,j.T))).T # 101ms # jR = np.empty_like(j) # for i in range(8): # jR[:,i] = intp(j[:,i]) # 102ms SO SLOW WTF NUMPY? #jR = np.apply_along_axis(intp, 0, j) # ahem. apparently this is just synactic sugar if multiway == True: jones = inverse_flatten_jones(jones) return jones
def convolve_focalplane(bore_nhits, nbolos, fp_radius_amin, boost): """ Given a hit count map, perform the focal plane convolution (that is generate the hit map as if there were `nbolos`, and boosted). Original author: Neil Goeckner-Wald. Modifications by Julien Peloton. Parameters ---------- bore_nhits : 1D array number of hits per pixel for the reference detector. nbolos : int total number of bolometers desired. fp_radius_amin : float radius of the focal plane in arcmin. boost : float boost factor to artificially increase the number of hits. It doesn't change the shape of the survey (just the amplitude). Returns ---------- focalplane_nhits : 1D array Number of hits for the all the detectors. Examples ---------- >>> bore_nhits = np.zeros(hp.nside2npix(128)) ## Put a patch in the center >>> bore_nhits[hp.query_disc(128, hp.ang2vec(np.pi/2, 0.), ... radius=10*np.pi/180.)] = 1. ## Increase the number of detector (x100) and make a boost (x10) >>> conv_bore_nhits = convolve_focalplane(bore_nhits, nbolos=100, ... fp_radius_amin=180, boost=10) >>> print(round(np.max(bore_nhits), 2), round(np.max(conv_bore_nhits), 2)) 1.0 1003.87 """ # Now we want to make the focalplane maps focalplane_nhits = np.zeros(bore_nhits.shape) # Resolution of our healpix map nside = hp.npix2nside(focalplane_nhits.shape[0]) resol_amin = hp.nside2resol(nside, arcmin=True) fp_rad_bins = int(fp_radius_amin * 2. / resol_amin) fp_diam_bins = (fp_rad_bins * 2) + 1 # Build the focal plane model and a list of offsets (x_fp, y_fp) = np.array( np.unravel_index(range(0, fp_diam_bins**2), (fp_diam_bins, fp_diam_bins))).reshape( 2, fp_diam_bins, fp_diam_bins) - (fp_rad_bins) fp_map = ((x_fp**2 + y_fp**2) < (fp_rad_bins)**2) bolo_per_pix = nbolos / float(np.sum(fp_map)) dRA = np.ndarray.flatten((x_fp[fp_map].astype(float) * fp_radius_amin) / (fp_rad_bins * 60. * (180. / (np.pi)))) dDec = np.ndarray.flatten((y_fp[fp_map].astype(float) * fp_radius_amin) / (fp_rad_bins * 60. * (180. / (np.pi)))) pixels_global = np.array(np.where(bore_nhits != 0)[0], dtype=int) for n in pixels_global: n = int(n) # Compute pointing offsets (theta_bore, phi_bore) = hp.pix2ang(nside, n) phi = phi_bore + dRA * np.sin(theta_bore) theta = theta_bore + dDec pixels = hp.ang2pix(nside, theta, phi) npix_loc = len(pixels) ## Necessary because the values in pixels aren't necessarily unique ## This is a poor design choice and should probably be fixed scanning_strategy_f.convolve_focalplane_f(bore_nhits[n], focalplane_nhits, pixels, bolo_per_pix, boost, npix_loc) return focalplane_nhits
def pix2radec(nside, pix, nest=True): __, _ = healpy.pix2ang(nside, pix, nest=nest) ra, dec = np.rad2deg(_), np.rad2deg(np.pi / 2 - __) return ra, dec
def rotate_mock_rescale_nsigma( zbin, # tomographic bin index - 0 to n rlsn, # mock cutout (realisation) seed, # mock seed wfile, # input weight file (optional) out_file='tmp.fits', # output file name + path neff_orig=neff_mcal, # dictionary for neff sig_orig=sig_mcal, # dictionary for sigma_e neff_ratio=0.5, # ratio of original to new neff (default half density) nside=4096): # nside of maps (default 4096) # out = mock.methods.rotate_mock_rescale_nsigma(3, 1, 1, wfile='text/pzrw_metacalibration_snr_0.fits.gz') npix = hp.nside2npix(nside) neff_pix = 1. / (hp.nside2pixarea(nside, degrees=True) * 3600.) neff_new = neff_orig[zbin] * neff_ratio mapfile = '/global/cscratch1/sd/seccolf/y1_patch/seed' + str( seed) + '/kgg-s' + str(seed) + '-f2z' + str(zbin) + '_c' + str( rlsn) + '.fits' fmap = fio.FITS(mapfile)[-1].read( columns=['PIXEL', 'Q_STOKES', 'U_STOKES']) theta, phi = hp.pix2ang( nside, fmap['PIXEL']) #theta and phi of the footprint pixels pix_rotator = hp.Rotator(deg=False, rot=[ euler_angle_1[int(rlsn)] * np.pi / 180., euler_angle_2[int(rlsn)] * np.pi / 180. ]) theta_rot, phi_rot = pix_rotator(theta, phi) rot_pix = hp.ang2pix(nside, theta_rot, phi_rot) if wfile is None: map_ra = phi / np.pi * 180.0 map_dec = (np.pi / 2.0 - theta) / np.pi * 180.0 map_g1 = fmap['Q_STOKES'] map_g2 = fmap['U_STOKES'] map_w = np.ones(len(map_ra)) map_neff = neff_new / neff_pix #*map_w/np.mean(map_w) map_sige = np.sqrt( (sig_orig[zbin]**2 / 2.) * (map_neff)) * np.ones(len(map_ra)) else: w = fio.FITS(wfile)[-1].read() ind = np.argsort(w['pix']) w = w[ind] ind = np.argsort(rot_pix) rot_pix = rot_pix[ind] fmap = fmap[ind] theta = theta[ind] phi = phi[ind] w = w[np.in1d(w['pix'], rot_pix, assume_unique=False)] mask = np.in1d(rot_pix, w['pix'], assume_unique=False) fmap = fmap[mask] rot_pix = rot_pix[mask] theta = theta[mask] phi = phi[mask] diff = np.diff(rot_pix) diff = np.where(diff != 0)[0] + 1 diff = np.append([0], diff) diff = np.append(diff, [None]) w1 = np.zeros(len(rot_pix)) # w2 = np.zeros(len(rot_pix)) for i in xrange(len(diff) - 1): w1[diff[i]:diff[i + 1]] = w['weight'][i] # w2[diff[i]:diff[i+1]] = w['weightsq'][i] w = None map_ra = phi / np.pi * 180.0 map_dec = (np.pi / 2.0 - theta) / np.pi * 180.0 map_g1 = fmap['Q_STOKES'] map_g2 = fmap['U_STOKES'] map_w = w1 map_neff = neff_new / neff_pix #*map_w/np.mean(map_w) # map_sige = np.sqrt((sig_orig[zbin]**2/2.)*(map_neff)*(w2/w1)) map_sige = np.sqrt( (sig_orig[zbin]**2 / 2.) * (map_neff)) * np.ones(len(map_ra)) fmap = None n = np.random.poisson(map_neff, size=len(map_ra)) out = np.zeros(np.sum(n), dtype=[('ra', 'f4')] + [('dec', 'f4')] + [('e1', 'f4')] + [('e2', 'f4')] + [('w', 'f4')]) out['ra'] = np.repeat(map_ra, n) out['dec'] = np.repeat(map_dec, n) out['e1'] = np.repeat( map_g1, n) + np.random.randn(len(out)) * np.repeat(map_sige, n) out['e2'] = np.repeat( map_g2, n) + np.random.randn(len(out)) * np.repeat(map_sige, n) out['w'] = np.repeat(map_w, n) # out['w'] = np.ones(len(out)) # fio.write(out_file,out,clobber=True) return out # original pixel positions (not DES pixel positions)
def gen_batch(pixels_of_choice, batch_size, nside_choice, clusters, retmatr=False, size=64, print_coords=False, centers_in_patch=False): import numpy as np import healpy as hp from astropy.coordinates import SkyCoord from astropy import units as u ipix = np.random.choice(pixels_of_choice, batch_size) theta, phi = hp.pix2ang(nside=nside_choice, nest=True, ipix=ipix, lonlat=True) sc = SkyCoord(l=theta * u.degree, b=phi * u.degree, frame='galactic') ra = sc.icrs.ra.degree dec = sc.icrs.dec.degree pics = [] matrs = [] masks = [] i = 0 while i < batch_size: cl_list = nearest_clusters(clusters, theta[i], phi[i], galactic=True) cl_list = np.stack([cl_list['RA'], cl_list['DEC']]).T pic, mask = None, None matr = None ret = draw_pic_with_mask([ra[i], dec[i]], cl_list, retmatr=retmatr, centers_in_patch=centers_in_patch) pic = ret[0] mask = ret[1] if retmatr: matr = ret[2] if not (pic.shape[0] == size and pic.shape[1] == size) or np.count_nonzero(mask) == 0: pixels_of_choice = pixels_of_choice[pixels_of_choice != ipix[i]] ipix[i] = np.random.choice(pixels_of_choice) theta[i], phi[i] = hp.pix2ang(nside=nside_choice, nest=True, ipix=ipix[i], lonlat=True) sc_cur = SkyCoord(l=theta[i] * u.degree, b=phi[i] * u.degree, frame='galactic') ra[i] = sc_cur.icrs.ra.degree dec[i] = sc_cur.icrs.dec.degree else: pics.append(pic) matrs.append(matr) masks.append(mask) if print_coords: print(ra[i], dec[i]) i += 1 if retmatr: return pics, masks, matrs return pics, masks
print("input file not found") print("Usage: pyton {} [my input file]".format(sys.argv[0])) sys.exit(0) print("# Running createRandom.py with {}".format(sys.argv[1])) np.random.seed(seed=input.SEED_random) # TO BE IMPLEMENTED: read smoothed dndz and force the random to follow it # loads the footprint footprint_res, footprint_zrange, sky_fraction, footprint = input.read_footprint( ) print("# Computing angular extent of footprint...") theta, phi = hp.pix2ang(footprint_res, np.arange(hp.nside2npix(footprint_res))) meanspacing = np.sqrt(4. * np.pi / footprint.size) thmin = np.min(theta[footprint]) - 2. * meanspacing thmax = np.max(theta[footprint]) + 2. * meanspacing phmin = np.min(phi[footprint]) - 2. * meanspacing phmax = np.max(phi[footprint]) + 2. * meanspacing print("# I will populate an area with theta=[%f,%f], phi=[%f,%f]" % (thmin, thmax, phmin, phmax)) print("# Assigning redshifts and fluxes...") if (input.cat_type is not 'pinocchio') | (input.pinocchio_last_run is None): fname = filenames.galcat(input, input.pinocchio_first_run)
def wcs_pix2world(self, ipix): theta, phi = hp.pix2ang(self.nside, ipix, nest=self.nested) return theta, phi
def generate_from_fitsfile(filename, language): healpix_data, header = healpy.read_map(filename, h=True) try: healpix_data, distmu, distsigma, distnorm = healpy.read_map( filename, field=[0, 1, 2, 3]) dim = 3 except: dim = 2 print("dimension = %d" % dim) if dim == 3: ii = (~numpy.isinf(distmu)) & (distmu > 0.0) print("good percent %.2f" % (numpy.sum(ii) * 100.0 / len(distmu))) dm = distmu[ii] prob = healpix_data[ii] x = numpy.multiply(prob, dm) print("Average distance %.2f " % (numpy.sum(x) / numpy.sum(prob))) wanted = [ 'DATE', 'DATE-OBS', 'INSTRUME', 'MJD-OBS', 'DISTMEAN', 'DISTSTD', 'REFERENC' ] meta = {} for (k, v) in header: if k in wanted: meta[k] = v print("Generating contours...") skymapID = get_md(filename) contours = [] levels = [] deciles = [] percentile = numpy.linspace(0.9, 0.1, 9) for p in percentile: (level, area) = get_level(healpix_data, p) levels.append(level) deciles.append([p * 100, area]) meta['deciles'] = deciles for l in levels: print(l) # make latitude histogram nside = healpy.get_nside(healpix_data) allipix = numpy.arange(0, len(healpix_data)) (th, ph) = healpy.pix2ang(nside, allipix) h = [] max = 0 for i in range(len(healpix_data)): if healpix_data[i] > max: max = healpix_data[i] imax = i if healpix_data[i] > levels[2]: ira = int(ph[i] * radian / 5.0) idec = int(th[i] * radian / 5.0) pair = [ira, idec] if pair not in h: h.append(pair) print(h) meta['histogram'] = h # make countours ngrid = 800 apointRA = ph[imax] * radian apointDec = 90 - th[imax] * radian meta['apointRA'] = apointRA meta['apointDec'] = apointDec print('max prob %f %f' % (apointRA, apointDec)) theta = numpy.linspace(0, numpy.pi, ngrid) phi = numpy.linspace(0, 2 * numpy.pi, 2 * ngrid) p, t = numpy.meshgrid(phi, theta) data_interpolated_to_grid = healpy.pixelfunc.get_interp_val( healpix_data, t, p) contourplot = plt.contour(p, t, data_interpolated_to_grid, levels=levels) numcontours = 0 contours = [] for n, c in enumerate(contourplot.collections): color = hsv2rgb(n * 1.0 / len(levels), 1.0, 1.0) for contourPath, path in enumerate(c.get_paths()): plist = [] for v in path.vertices: plist.append([v[0] * radian, 90 - v[1] * radian]) spaths = splitPaths(plist) for sp in spaths: contours.append({ "name": "%d-percentile" % ((n + 1) * 10), "color": color, "coords": sp }) # contours.append({"name":"%d-percentile"%((n+1)*10), "color":color, "coords":plist}) numcontours += 1 print("made %d polylines" % numcontours) # galaxies from Glade gc = galaxyCatalog.gc(glade_filename) gc.info() de = gc.declination ra = gc.right_ascension name = numpy.array(gc.name) print("has %d sources" % len(ra)) ph_at_sources = ra / radian th_at_sources = (90 - de) / radian skymap_at_sources = numpy.zeros(len(ra)) weighted_sources = numpy.zeros(len(ra)) # distribution of log distance ldc = gc.log_distance_centres dh = gc.distance_hist if dim == 2: distance = gc.distance for i in range(len(ra)): ipix = healpy.ang2pix(nside, th_at_sources[i], ph_at_sources[i]) skymap_at_sources[i] = healpix_data[ipix] weighted_sources[i] = skymap_at_sources[i] if dim == 3: distance = gc.distance distance_pdf = numpy.zeros(len(ra)) for i in range(len(ra)): ipix = healpy.ang2pix(nside, th_at_sources[i], ph_at_sources[i]) skymap_at_sources[i] = healpix_data[ipix] norm = distnorm[ipix] sigma = distsigma[ipix] mu = distmu[ipix] if math.isnan(distance[i]): distance_pdf[i] = 0.0 for k in range(len(ldc)): d = math.pow(10.0, ldc[k]) r = math.exp(-(d - mu)**2 / (2 * sigma**2)) * norm * (d**2) distance_pdf[i] += r * dh[k] else: d = distance[i] distance_pdf[i] = math.exp(-(d - mu)**2 / (2 * sigma**2)) * norm * (d**2) weighted_sources[i] = skymap_at_sources[i] * distance_pdf[i] weighted_sources = numpy.array(weighted_sources) maxw = numpy.max(weighted_sources) sumw = numpy.sum(weighted_sources) print('max weight = ', maxw) index = numpy.argsort(-weighted_sources) num = len(index) if num > 200: num = 200 selected_sources = (index[:num]) print("Selected %d galaxies" % len(selected_sources)) sources = [] for de, ra, nm, w, distance in zip(de[selected_sources], ra[selected_sources], name[selected_sources], weighted_sources[selected_sources], distance[selected_sources]): absw = w / sumw # weight compared to total weight sw = math.sqrt(w / maxw) # normalized sqrt of weight if sw < 1.e-3: continue if math.isnan(distance): distance = 0.0 sources.append({ "coords": [ra, de], "name": nm, "sw": sw, "absw": absw, "distance": distance }) n += 1 print("Sources complete with %d" % n) print("Language = ", language) return make_json(meta, contours, sources)
#plot_mwd(allarr[0], allarr[1], org=180., title = "NVSS Raw, +/- 5deg b cut, +/-5deg supergalactic b cut") #plt.show() fout = open(outfile + '_resultnew.txt', "w") fout.write( str(ndecmin) + "," + str(ndecmax) + ',' + str(sdecmin) + "," + str(sdecmax) + "\n") fout.write( str(stotno) + "," + str(sgallatcutno) + "," + str(sn2masscutno) + "," + str(sw1mprocutno) + "," + str(swmproj2masscutno) + "," + str(sj2masscutno) + "," + str(sgbcutno) + "," + str(finalno) + "\n") for i in range(0, npix): dec, ra = np.deg2rad( (90. - np.rad2deg(hp.pix2ang(scanres, i)[0]))), hp.pix2ang(scanres, i)[1] print "DEC. ", ( 90. - np.rad2deg(hp.pix2ang(scanres, i)[0])), " R.A.", np.rad2deg( hp.pix2ang(scanres, i)[1]) dangle = np.rad2deg( np.arccos( np.cos(np.deg2rad(allarr[1])) * np.cos(dec) * np.cos(np.deg2rad(allarr[0]) - ra) + np.sin(np.deg2rad(allarr[1])) * np.sin(dec))) print "UH: ", len(allarr.transpose()[dangle < 90.]), "LH: ", len( allarr.transpose()[dangle > 90.]) fout.write( str(np.rad2deg(dec)) + "," + str(np.rad2deg(ra)) + "," + str(len(allarr.transpose()[dangle < 90.])) + "," + str(len(allarr.transpose()[dangle > 90.])) + "\n")
def ra_dec_from_ipix(nside, ipix, nest=False): (theta, phi) = hp.pix2ang(nside, ipix, nest=nest) return (phi, np.pi / 2. - theta)
#!/usr/bin/env python #DR7 Truth file /global/cscratch1/sd/raichoor/tmpdir/tmp.dr7.41ra44.-1dec0.clean.deep.fits import sys, os import numpy as np import matplotlib.pyplot as plt from pkg_resources import resource_filename from desiutil.log import get_logger from desitarget import cuts #import astropy.io.fits as pyfits import fitsio import healpy as hp import argparse log = get_logger() def mag2flux(mag): return 10**(-0.4 * (mag - 22.5)) def flux2mag(flux): mag = -2.5 * np.log10(flux * (flux > 0) + 0.001 * (flux <= 0)) + 22.5 mag[(flux <= 0)] = 0. return mag def prof(x, y, bins): h0, bins = np.histogram(x, bins=bins) hx, bins = np.histogram(x, bins=bins, weights=x) hy, bins = np.histogram(x, bins=bins, weights=y) hy2, bins = np.histogram(x, bins=bins, weights=y**2) ii = h0 > 2 mx = hx[ii] / h0[ii] my = hy[ii] / h0[ii]
import healpy as hp import matplotlib.pyplot as plt import pymaster as nmt import os #This script describes the computation of polarized power spectra using the #pure-E and B approach #We'll run this many simulations nsim=10 #HEALPix map resolution nside=256 #Let us first create a square mask: msk=np.zeros(hp.nside2npix(nside)) th,ph=hp.pix2ang(nside,np.arange(hp.nside2npix(nside))) ph[np.where(ph>np.pi)[0]]-=2*np.pi msk[np.where((th<2.63) & (th>1.86) & (ph>-np.pi/4) & (ph<np.pi/4))[0]]=1. #Now we apodize the mask. The pure-B formalism requires the mask to be differentiable #along the edges. The 'C1' and 'C2' apodization types supported by mask_apodization #achieve this. msk_apo=nmt.mask_apodization(msk,10.0,apotype='C1') #Select a binning scheme b=nmt.NmtBin(nside,nlb=16) leff=b.get_effective_ells() #Read power spectrum and provide function to generate simulated skies l,cltt,clee,clbb,clte=np.loadtxt('cls.txt',unpack=True); def get_fields() :
def __init__(self, s, freq=None): if freq is not None: self.freq = freq else: print "Now loading in information about the primary beams..." self.freq = s.freq if not s.antennasHaveIdenticalBeams: print "\nWARNING: ALL PRIMARY BEAMS BEING LOADED AS IDENTICAL TO ANTENNA 0\n" self.allBeams = {} for antPol in s.antPolList: for skyPol in s.skyPolList: for antIndex in range((s.nAntennas - 1) * (not s.antennasHaveIdenticalBeams) + 1): #either range(1) or range(nAntennas) for pointIndex in range(s.nPointings): #find two closest values allFrequencies = np.asarray(map(float, s.beamFreqList)) freq1Index = np.abs(allFrequencies - self.freq).argmin() freq1 = allFrequencies[freq1Index] allFrequencies[freq1Index] = 1e10 freq2Index = np.abs(allFrequencies - self.freq).argmin() freq2 = allFrequencies[freq2Index] filename = s.beamFileFormat.replace( '[antIndex]', str(0)).replace('[antPol]', antPol).replace( '[skyPol]', skyPol).replace('[pointIndex]', str(pointIndex)) # filename = s.beamFileFormat.replace('[antIndex]',str(antIndex)).replace('[antPol]',antPol).replace('[skyPol]',skyPol).replace('[pointIndex]',str(pointIndex)) key = str(antIndex) + ";" + str(antPol) + ";" + str( skyPol) + ";" + str(pointIndex) if s.FITSbeam: beam1hdulist = fits.open( filename.replace( '[freq]', '{0:d}'.format( int(s.beamFreqList[freq1Index])))) beam1 = beam1hdulist[1].data.field(0).flatten() beam1hdulist.close() beam2hdulist = fits.open( filename.replace( '[freq]', '{0:d}'.format( int(s.beamFreqList[freq2Index])))) beam2 = beam2hdulist[1].data.field(0).flatten() beam2hdulist.close() beam1[ hp.pix2ang(s.beamNSIDE, np.arange(len( beam1)))[0] > np.pi / 2] = 0 #remove all response below the horizon beam2[hp.pix2ang(s.beamNSIDE, np.arange(len( beam2)))[0] > np.pi / 2] = 0 beam1 = beam1**.5 #this is a proxy of antenna beams beam2 = beam2**.5 beam1 = beam1 / np.max(beam1) beam2 = beam2 / np.max(beam2) else: beam1 = np.load( filename.replace( '[freq]', "{:1.6f}".format( float(s.beamFreqList[freq1Index])))) beam2 = np.load( filename.replace( '[freq]', "{:1.6f}".format( float(s.beamFreqList[freq2Index])))) #linear interpolation self.allBeams[key] = beam1 * ( 1 - (self.freq - freq1) / (freq2 - freq1)) + beam2 * ((self.freq - freq1) / (freq2 - freq1)) #Beam products useful for Stokes I for antPol in s.antPolList: for skyPol in s.skyPolList: for pointIndex in range(s.nPointings): keyIn = str(0) + ";" + str(antPol) + ";" + str( skyPol) + ";" + str(pointIndex) keyOut = str(antPol) + str(antPol) + str(skyPol) + str( skyPol) + str(pointIndex) self.allBeams[keyOut] = self.allBeams[ keyIn] * self.allBeams[keyIn].conj()
def toRaDec(nside, index): """Convert from healpix index to ra,dec in degrees""" vec = hp.pix2ang(nside, index) dec = np.rad2deg(-vec[0]) + 90 ra = np.rad2deg(vec[1]) return np.dstack((ra, dec))[0]
def create_iterative_observations_healpix(ds, lead_time, max_lead_time, nb_timesteps, test_years, nodes): """ Assumptions lead_time : min time to next prediction (6h) max_lead_time: lastest time of predictions (72h) nb_timesteps: nodes --> number of nodes? """ lead_times = np.arange(lead_time, max_lead_time + lead_time, lead_time) data = ds.to_array(dim='level', name='Dataset').transpose('time', 'node', 'level') n_samples = data.isel(time=slice(0, -nb_timesteps * lead_time)).shape[0] - max_lead_time obs_list = [] for lead in lead_times: obs_list.append( data.isel(time=slice(lead, lead + n_samples)).isel(level=slice(0, 2)).values) observations_numpy = np.array(obs_list) # Lat lon coordinates nside = int(np.sqrt(nodes / 12)) out_lon, out_lat = hp.pix2ang(nside, np.arange(nodes), lonlat=True) # Actual times start = np.datetime64(test_years[0], 'h') + np.timedelta64(lead_time, 'h') stop = start + np.timedelta64(n_samples, 'h') times = np.arange(start, stop) # Variables var_dict_out = {var: None for var in ['z', 't']} das = [] lev_idx = 0 for var, levels in var_dict_out.items(): if levels is None: das.append( xr.DataArray(observations_numpy[:, :, :, lev_idx], dims=['lead_time', 'time', 'node'], coords={ 'lead_time': lead_times, 'time': times, 'node': np.arange(nodes) }, name=var)) lev_idx += 1 else: nlevs = len(levels) das.append( xr.DataArray(observations_numpy[:, :, :, lev_idx:lev_idx + nlevs], dims=['lead_time', 'time', 'node', 'level'], coords={ 'lead_time': lead_times, 'time': valid_time, 'node': nodes, 'level': nlevs }, name=var)) lev_idx += nlevs observation_ds = xr.merge(das) observation_ds = observation_ds.assign_coords({ 'lat': out_lat, 'lon': out_lon }) return observation_ds
def img2healpix_planar(img, nside, thetac, phic, delta_theta, delta_phi, rot=None): """Project a 2D image on healpix map Args: * img (array): image to project. It must have shape ``(#img, M, N)`` * nside (int): ``NSIDE`` parameter for the output map. * thetac, phic (float): coordinates (in degrees) where to project the center of the image on the healpix map. They must follow the HEALPix angle convention: - ``0 <= thetac <= 180``, with 0 being the N and 180 the S Pole - ``0 <= phic <= 360``, with 0 being at the center of the map. It increases moving towards W * delta_theta, delta_phi (float): angular size of the projected image * rot: not implemented yet! Returns: The HEALPix map containing the projected image. """ imgf = np.flip(img, axis=2) imgf = np.array(imgf) data = imgf.reshape(img.shape[0], img.shape[1] * img.shape[2]) xsize = img.shape[1] ysize = img.shape[2] theta_min = thetac - delta_theta / 2.0 theta_max = thetac + delta_theta / 2.0 phi_max = phic + delta_phi / 2.0 phi_min = phic - delta_phi / 2.0 theta_min = np.radians(theta_min) theta_max = np.radians(theta_max) phi_min = np.radians(phi_min) phi_max = np.radians(phi_max) img_theta_temp = np.linspace(theta_min, theta_max, ysize) img_phi_temp = np.linspace(phi_min, phi_max, xsize) ipix = np.arange(hp.nside2npix(nside)) if rot == None: theta_r, phi_r = hp.pix2ang(nside, ipix) theta1 = theta_min theta2 = theta_max flg = np.where(theta_r < theta1, 0, 1) flg *= np.where(theta_r > theta2, 0, 1) if phi_min >= 0: phi1 = phi_min phi2 = phi_max flg *= np.where(phi_r < phi1, 0, 1) flg *= np.where(phi_r > phi2, 0, 1) else: phi1 = 2.0 * np.pi + phi_min phi2 = phi_max flg *= np.where((phi2 < phi_r) & (phi_r < phi1), 0, 1) img_phi_temp[ img_phi_temp < 0] = 2 * np.pi + img_phi_temp[img_phi_temp < 0] img_phi, img_theta = np.meshgrid(img_phi_temp, img_theta_temp) img_phi = img_phi.flatten() img_theta = img_theta.flatten() ipix = np.compress(flg, ipix) pl_theta = np.compress(flg, theta_r) pl_phi = np.compress(flg, phi_r) points = np.zeros((len(img_theta), 2), "d") points[:, 0] = img_theta points[:, 1] = img_phi npix = hp.nside2npix(nside) hp_map = np.zeros((data.shape[0], npix), "d") for i in range(data.shape[0]): hp_map[i, ipix] = griddata(points, data[i, :], (pl_theta, pl_phi), method="nearest") return hp_map
def create_iterative_predictions_healpix(model, device, dg): batch_size = dg.batch_size delta_t = dg.dataset.lead_time max_lead_time = dg.dataset.max_lead_time initial_lead_time = delta_t nodes = dg.dataset.nodes nside = int(np.sqrt(nodes / 12)) n_samples = dg.dataset.n_samples in_feat = dg.dataset.in_features out_feat = dg.dataset.out_features data_vars = dg.dataset.mean.level.values.tolist()[:out_feat] train_std = dg.dataset.std.values[:out_feat] train_mean = dg.dataset.mean.values[:out_feat] # Lead times lead_times = np.arange(delta_t, max_lead_time + delta_t, delta_t) # Lat lon coordinates out_lon, out_lat = hp.pix2ang(nside, np.arange(nodes), lonlat=True) # Actual times start = np.datetime64(dg.dataset.years[0], 'h') + np.timedelta64( initial_lead_time, 'h') stop = start + np.timedelta64(dg.dataset.n_samples, 'h') times = np.arange(start, stop) # Variables var_dict_out = {var: None for var in data_vars} # Radiation constants = np.array( dg.dataset.data.isel(level=slice(out_feat, None)).values) dataloader = dg predictions = [] model.eval() for lead in lead_times: outputs = [] state = [] states = np.empty((n_samples, nodes, in_feat)) time1 = time.time() for i, (sample, _) in enumerate(dataloader): inputs = sample[0].to(device) output = model(inputs) outputs.append( output.detach().cpu().clone().numpy()[:, :, :out_feat]) state.append(output.detach().cpu().clone().numpy()) preds = np.concatenate(outputs) states[:, :, :out_feat] = np.concatenate(state) states[:, :, out_feat:] = constants[lead:n_samples + lead, :] predictions.append(preds * train_std + train_mean) new_set = WeatherBenchDatasetIterative(states) dataloader = DataLoader(new_set, batch_size=batch_size, shuffle=False, num_workers=10) time2 = time.time() predictions = np.array(predictions) das = [] lev_idx = 0 for var in data_vars: das.append( xr.DataArray(predictions[:, :, :, lev_idx], dims=['lead_time', 'time', 'node'], coords={ 'lead_time': lead_times, 'time': times, 'node': np.arange(nodes) }, name=var)) lev_idx += 1 prediction_ds = xr.merge(das) prediction_ds = prediction_ds.assign_coords({ 'lat': out_lat, 'lon': out_lon }) return prediction_ds
def _index2coord(nside, index): s2c = Spherical2CartesianOperator('zenith,azimuth') t, p = hp.pix2ang(nside, index) return t, p, s2c(np.concatenate([t[..., None], p[..., None]], axis=-1))
return result # umjd = medDB(full_select='select DISTINCT(mjd) from medskybrightness;', dtypes=float) skyMaps = np.load('sky_maps.npz') umjd = skyMaps['umjd'].copy() nstart = 5506 + 67 # partly cloudy frame #nstart = 6447 # very cloudy frame # nstart = 982 # clear, with moon previous = single_frame(umjd[nstart - 1]) nside = hp.npix2nside(previous.size) mjd = umjd[nstart] site = Site('LSST') dec, ra = hp.pix2ang(nside, np.arange(previous.size)) dec = np.pi / 2. - dec lmst, last = calcLmstLast(mjd, site.longitude_rad) lmst = lmst / 12. * 180. alt, az = stupidFast_RaDec2AltAz(ra, dec, site.latitude_rad, site.longitude_rad, mjd) frame = single_frame(mjd, filter_name='R') seen = np.where((frame != hp.UNSEEN) & (previous != hp.UNSEEN)) unseen = np.where((frame == hp.UNSEEN) | (previous == hp.UNSEEN)) frame = fixBias(frame) previous = fixBias(previous) diff = frame - previous
def isgwb_mich_response(self, f0, tsegmid): ''' Calculate the Antenna pattern/detector transfer function for an isotropic SGWB using basic michelson channels. Note that since this is the response to an isotropic background, the response function is integrated over sky direction and averaged over polarozation. The angular integral is a linear and rectangular in the cos(theta) and phi space. Note also that f0 is (pi*L*f)/c and is input as an array. Parameters ----------- f0 : float A numpy array of scaled frequencies (see above for def) tsegstart : float A numpy array of segment start times tsegmid : float A numpy array of segment midpoints Returns --------- response_tess : float 4D array of covariance matrices for antenna patterns of the three channels, integrated over sky direction and averaged over polarization, across all frequencies and times. ''' npix = hp.nside2npix(self.params['nside']) # Array of pixel indices pix_idx = np.arange(npix) # Angular coordinates of pixel indices theta, phi = hp.pix2ang(self.params['nside'], pix_idx) # Take cosine. ctheta = np.cos(theta) # Area of each pixel in sq.radians dOmega = hp.pixelfunc.nside2pixarea(self.params['nside']) # Create 2D array of (x,y,z) unit vectors for every sky direction. omegahat = np.array([np.sqrt(1-ctheta**2)*np.cos(phi),np.sqrt(1-ctheta**2)*np.sin(phi),ctheta]) # Call lisa_orbits to compute satellite positions at the midpoint of each time segment rs1, rs2, rs3 = self.lisa_orbits(tsegmid) ## Calculate directional unit vector dot products ## Dimensions of udir is time-segs x sky-pixels udir = np.einsum('ij,ik',(rs2-rs1)/LA.norm(rs2-rs1,axis=0)[None, :],omegahat) vdir = np.einsum('ij,ik',(rs3-rs1)/LA.norm(rs3-rs1,axis=0)[None, :],omegahat) wdir = np.einsum('ij,ik',(rs3-rs2)/LA.norm(rs3-rs2,axis=0)[None, :],omegahat) ## NB -- An attempt to directly adapt e.g. (u o u):e+ as implicit tensor calculations ## as opposed to the explicit forms we've previously used. ''' mhat = np.array([np.sin(phi),-np.cos(phi),np.zeros(len(phi))]) nhat = np.array([np.cos(phi)*ctheta,np.sin(phi)*ctheta,-np.sqrt(1-ctheta**2)]) # 1/2 u x u : eplus. These depend only on geometry so they only have a time and directionality dependence and not of frequency Fplus_u = 0.5*np.einsum("ijk,ijl", \ np.einsum("ik,jk -> ijk",(rs2-rs1)/LA.norm(rs2-rs1,axis=0)[None, :], (rs2-rs1)/LA.norm(rs2-rs1,axis=0)[None, :]), \ np.einsum("ik,jk -> ijk",mhat,mhat) - np.einsum("ik,jk -> ijk",nhat,nhat)) Fplus_v = 0.5*np.einsum("ijk,ijl", \ np.einsum("ik,jk -> ijk",(rs3-rs1)/LA.norm(rs3-rs1,axis=0)[None, :],(rs3-rs1)/LA.norm(rs3-rs1,axis=0)[None, :]), \ np.einsum("ik,jk -> ijk",mhat,mhat) - np.einsum("ik,jk -> ijk",nhat,nhat)) Fplus_w = 0.5*np.einsum("ijk,ijl", \ np.einsum("ik,jk -> ijk",(rs3-rs2)/LA.norm(rs3-rs2,axis=0)[None, :],(rs3-rs2)/LA.norm(rs3-rs2,axis=0)[None, :]), \ np.einsum("ik,jk -> ijk",mhat,mhat) - np.einsum("ik,jk -> ijk",nhat,nhat)) # 1/2 u x u : ecross Fcross_u = 0.5*np.einsum("ijk,ijl", \ np.einsum("ik,jk -> ijk",(rs2-rs1)/LA.norm(rs2-rs1,axis=0)[None, :],(rs2-rs1)/LA.norm(rs2-rs1,axis=0)[None, :]), \ np.einsum("ik,jk -> ijk",mhat,mhat) + np.einsum("ik,jk -> ijk",nhat,nhat)) Fcross_v = 0.5*np.einsum("ijk,ijl", \ np.einsum("ik,jk -> ijk",(rs3-rs1)/LA.norm(rs3-rs1,axis=0)[None, :],(rs3-rs1)/LA.norm(rs3-rs1,axis=0)[None, :]), \ np.einsum("ik,jk -> ijk",mhat,mhat) + np.einsum("ik,jk -> ijk",nhat,nhat)) Fcross_w = 0.5*np.einsum("ijk,ijl", \ np.einsum("ik,jk -> ijk",(rs3-rs2)/LA.norm(rs3-rs2,axis=0)[None, :],(rs3-rs2)/LA.norm(rs3-rs2,axis=0)[None, :]), \ np.einsum("ik,jk -> ijk",mhat,mhat) + np.einsum("ik,jk -> ijk",nhat,nhat)) # Initlize arrays for the detector reponse R1 = np.zeros((f0.size, tsegmid.size), dtype='complex') R2 = np.zeros((f0.size, tsegmid.size), dtype='complex') R3 = np.zeros((f0.size, tsegmid.size), dtype='complex') R12 = np.zeros((f0.size, tsegmid.size), dtype='complex') R13 = np.zeros((f0.size, tsegmid.size), dtype='complex') R23 = np.zeros((f0.size, tsegmid.size), dtype='complex') # Calculate the detector response for each frequency for ii in range(0, f0.size): # Calculate GW transfer function for the michelson channels gammaU_plus = 1/2 * (np.sinc((f0[ii])*(1 - udir)/np.pi)*np.exp(-1j*f0[ii]*(3+udir)) + \ np.sinc((f0[ii])*(1 + udir)/np.pi)*np.exp(-1j*f0[ii]*(1+udir))) gammaV_plus = 1/2 * (np.sinc((f0[ii])*(1 - vdir)/np.pi)*np.exp(-1j*f0[ii]*(3+vdir)) + \ np.sinc((f0[ii])*(1 + vdir)/np.pi)*np.exp(-1j*f0[ii]*(1+vdir))) gammaW_plus = 1/2 * (np.sinc((f0[ii])*(1 - wdir)/np.pi)*np.exp(-1j*f0[ii]*(3+wdir)) + \ np.sinc((f0[ii])*(1 + wdir)/np.pi)*np.exp(-1j*f0[ii]*(1+wdir))) # Calculate GW transfer function for the michelson channels gammaU_minus = 1/2 * (np.sinc((f0[ii])*(1 + udir)/np.pi)*np.exp(-1j*f0[ii]*(3 - udir)) + \ np.sinc((f0[ii])*(1 - udir)/np.pi)*np.exp(-1j*f0[ii]*(1 - udir))) gammaV_minus = 1/2 * (np.sinc((f0[ii])*(1 + vdir)/np.pi)*np.exp(-1j*f0[ii]*(3 - vdir)) + \ np.sinc((f0[ii])*(1 - vdir)/np.pi)*np.exp(-1j*f0[ii]*(1 - vdir))) gammaW_minus = 1/2 * (np.sinc((f0[ii])*(1 + wdir)/np.pi)*np.exp(-1j*f0[ii]*(3 - wdir)) + \ np.sinc((f0[ii])*(1 - wdir)/np.pi)*np.exp(-1j*f0[ii]*(1 - wdir))) ## Michelson antenna patterns ## Calculate Fplus Fplus1 = 0.5*(Fplus_u*gammaU_plus - Fplus_v*gammaV_plus)*np.exp(-1j*f0[ii]*(udir + vdir)/np.sqrt(3)) Fplus2 = 0.5*(Fplus_w*gammaW_plus - Fplus_u*gammaU_minus)*np.exp(-1j*f0[ii]*(-udir + vdir)/np.sqrt(3)) Fplus3 = 0.5*(Fplus_v*gammaV_minus - Fplus_w*gammaW_minus)*np.exp(1j*f0[ii]*(vdir + wdir)/np.sqrt(3)) ## Calculate Fcross Fcross1 = 0.5*(Fcross_u*gammaU_plus - Fcross_v*gammaV_plus)*np.exp(-1j*f0[ii]*(udir + vdir)/np.sqrt(3)) Fcross2 = 0.5*(Fcross_w*gammaW_plus - Fcross_u*gammaU_minus)*np.exp(-1j*f0[ii]*(-udir + vdir)/np.sqrt(3)) Fcross3 = 0.5*(Fcross_v*gammaV_minus - Fcross_w*gammaW_minus)*np.exp(1j*f0[ii]*(vdir + wdir)/np.sqrt(3)) ## Detector response summed over polarization and integrated over sky direction ## The travel time phases for the which are relevent for the cross-channel are ## accounted for in the Fplus and Fcross expressions above. R1[ii, :] = dOmega/(8*np.pi)*np.sum( (np.absolute(Fplus1))**2 + (np.absolute(Fcross1))**2, axis=1 ) R2[ii, :] = dOmega/(8*np.pi)*np.sum( (np.absolute(Fplus2))**2 + (np.absolute(Fcross2))**2, axis=1 ) R3[ii, :] = dOmega/(8*np.pi)*np.sum( (np.absolute(Fplus3))**2 + (np.absolute(Fcross3))**2, axis=1 ) R12[ii, :] = dOmega/(8*np.pi)*np.sum( np.conj(Fplus1)*Fplus2 + np.conj(Fcross1)*Fcross2, axis=1) R13[ii, :] = dOmega/(8*np.pi)*np.sum( np.conj(Fplus1)*Fplus3 + np.conj(Fcross1)*Fcross3, axis=1) R23[ii, :] = dOmega/(8*np.pi)*np.sum( np.conj(Fplus2)*Fplus3 + np.conj(Fcross2)*Fcross3, axis=1) response_mat = np.array([ [R1, R12, R13] , [np.conj(R12), R2, R23], [np.conj(R13), np.conj(R23), R3] ]) return response_mat
def _get_synthbeam_(scene, position, area, nu, bandwidth, horn, primary_beam, secondary_beam, spectral_irradiance=1, synthbeam_dtype=np.float32, theta_max=30): """ Return the monochromatic synthetic beam for a specified location on the focal plane, multiplied by a given area and bandwidth. Parameters ---------- scene : QubicScene The scene. x : array-like The X-coordinate in the focal plane where the response is computed, in meters. If not provided, the detector central positions are assumed. y : array-like The Y-coordinate in the focal plane where the response is computed, in meters. If not provided, the detector central positions are assumed. area : array-like The integration area, in m^2. nu : float The frequency for which the response is computed [Hz]. bandwidth : float The filter bandwidth [Hz]. horn : PackedArray The horn layout. primary_beam : Beam The primary beam. secondary_beam : Beam The secondary beam. synthbeam_dtype : dtype, optional The data type for the synthetic beams (default: float32). It is the dtype used to store the values of the pointing matrix. theta_max : float, optional The maximum zenithal angle above which the synthetic beam is assumed to be zero, in degrees. """ MAX_MEMORY_B = 1e9 theta, phi = hp.pix2ang(scene.nside, scene.index) index = np.where(theta <= np.radians(theta_max))[0] nhorn = int(np.sum(horn.open)) npix = len(index) nbytes_B = npix * nhorn * 24 ngroup = np.ceil(nbytes_B / MAX_MEMORY_B) out = np.zeros(position.shape[:-1] + (len(scene), ), dtype=synthbeam_dtype) for s in split(npix, ngroup): index_ = index[s] sb = MultiQubicInstrument._get_response(theta[index_], phi[index_], spectral_irradiance, position, area, nu, horn, primary_beam, secondary_beam) out[..., index_] = abs2(sb, dtype=synthbeam_dtype) return out * bandwidth * deriv_and_const(nu, scene.nside)
# so all the say, patches at helpix #44 should be hpwanted = 8 zps = patchZP[np.where(resultHpIDs == hpwanted)] good = np.where(zps != 0) #plt.plot(zps[good]) frame = 300 good = np.where(resultDateIDs == frame) skymap = np.zeros(hp.nside2npix(nside)) skymap[resultHpIDs[good].astype(int)] = patchZP[good] skymap[np.where(skymap == 0)] = hp.UNSEEN #hp.mollview(skymap, rot=(0,90)) ## XXX--Add a snippet of healpy to convert hpid to alt/az lat, resultAz = hp.pix2ang(nside, resultHpIDs.astype(int)) resultAlt = np.pi / 2. - lat # Convert resultDateIDs to mjd. I think this should work--presumably the dateIDs and mjds are both increasing? resultMjds = intid2id(resultDateIDs, np.unique(dateIDs), np.unique(mjds), dtype=float) # Let's figure out the number of stars per patch: bins = np.zeros(resultPatchIDs.size * 2, dtype=float) bins[::2] = resultPatchIDs - 0.5 bins[1::2] = resultPatchIDs + 0.5 starsPerPatch, bins = np.histogram(patchIDs, bins=bins) starsPerPatch = starsPerPatch[::2] fig = plt.figure()
def isgwb_oaet_response(self, f0, tsegmid): ''' Calcualte the Antenna pattern/ detector transfer function functions to an isotropic SGWB using A, E and T TDI channels. Note that since this is the response to an isotropic background, the response function is integrated over sky direction and averaged over polarization. The angular integral is a linear and rectangular in the cos(theta) and phi space. Note that f0 is (pi*L*f)/c and is input as an array. Parameters ----------- f0 : float A numpy array of scaled frequencies (see above for def) tsegmid : array A numpy array of the midpoints for each time integration segment. rs1, rs2, rs3 : array Satellite position vectors. Returns --------- R1, R2 and R3 : arrays Antenna Patterns for the given sky direction for the three channels, integrated over sky direction and averaged over polarizationevaluated at the midpoint of each time segment. ''' self.rs1, self.rs2, self.rs3 = self.lisa_orbits(tsegmid) ## Setup healpix map npix = hp.nside2npix(self.params['nside']) # Array of pixel indices pix_idx = np.arange(npix) #Angular coordinates of pixel indcides theta, phi = hp.pix2ang(self.params['nside'], pix_idx) # Take cosine. ctheta = np.cos(theta) stheta = np.sqrt(1 - ctheta**2) # Area of each pixel in sq.radians dOmega = hp.pixelfunc.nside2pixarea(self.params['nside']) ## Indices of midpoints array timeindices = np.arange(len(tsegmid)) # Initlize arrays for the detector reponse R1 = np.zeros((len(timeindices), f0.size)) R2 = np.zeros((len(timeindices), f0.size)) R3 = np.zeros((len(timeindices), f0.size)) ## arm vectors uvec = self.rs2 - self.rs1 vvec = self.rs3 - self.rs1 wvec = self.rs3 - self.rs2 ## Calculate arm lengths Lu = np.sqrt(np.sum(uvec * uvec, axis=0)) Lv = np.sqrt(np.sum(vvec * vvec, axis=0)) Lw = np.sqrt(np.sum(wvec * wvec, axis=0)) ## Define x/y/z for each satellite at all times x1, y1, z1 = self.rs1[0, :], self.rs1[1, :], self.rs1[2, :] x2, y2, z2 = self.rs2[0, :], self.rs2[1, :], self.rs2[2, :] x3, y3, z3 = self.rs3[0, :], self.rs3[1, :], self.rs3[2, :] udir = np.tensordot((x2-x1)/Lu, np.cos(phi)*stheta, axes=0) + np.tensordot((y2-y1)/Lu, np.cos(phi)*stheta, axes=0) + \ np.tensordot((z2-z1)/Lu, ctheta, axes = 0) vdir = np.tensordot((x3-x1)/Lv, np.cos(phi)*stheta, axes=0) + np.tensordot((y3-y1)/Lv, np.cos(phi)*stheta, axes=0) + \ np.tensordot((z3-z1)/Lv, ctheta, axes = 0) wdir = np.tensordot((x3-x2)/Lw, np.cos(phi)*stheta, axes=0) + np.tensordot((y3-y2)/Lw, np.cos(phi)*stheta, axes=0) + \ np.tensordot((z3-z2)/Lw, ctheta, axes = 0) ## Calculate 1/2(u x u):eplus Pcontract_u = 1/2*( (np.tensordot((x2-x1)/Lu, np.sin(phi), axes=0) - np.tensordot((y2-y1)/Lu, np.cos(phi), axes=0))**2 - \ (np.tensordot((x2-x1)/Lu, np.cos(phi)*ctheta, axes=0) + np.tensordot((y2-y1)/Lu, np.cos(phi)*ctheta, axes=0) - \ np.tensordot((z2-z1)/Lu, stheta, axes=0))**2 ) Pcontract_v = 1/2*( (np.tensordot((x3-x1)/Lv, np.sin(phi), axes=0) - np.tensordot((y3-y1)/Lv, np.cos(phi), axes=0))**2 - \ (np.tensordot((x3-x1)/Lv, np.cos(phi)*ctheta, axes=0) + np.tensordot((y3-y1)/Lv, np.cos(phi)*ctheta, axes=0) - \ np.tensordot((z3-z1)/Lv, stheta, axes=0))**2 ) Pcontract_w = 1/2*( (np.tensordot((x3-x2)/Lw, np.sin(phi), axes=0) - np.tensordot((y3-y2)/Lw, np.cos(phi), axes=0))**2 - \ (np.tensordot((x3-x2)/Lw, np.cos(phi)*ctheta, axes=0) + np.tensordot((y3-y2)/Lw, np.cos(phi)*ctheta, axes=0) - \ np.tensordot((z3-z2)/Lw, stheta, axes=0))**2 ) Ccontract_u = (np.tensordot((x2-x1)/Lu, np.sin(phi), axes=0) - np.tensordot((y2-y1)/Lu, np.cos(phi), axes=0) ) * \ ( np.tensordot((x2-x1)/Lu, np.cos(phi)*ctheta, axes=0) + np.tensordot((y2-y1)/Lu, np.cos(phi)*ctheta, axes=0) - \ np.tensordot((z2-z1)/Lu, stheta, axes=0) ) Ccontract_v = (np.tensordot((x3-x1)/Lv, np.sin(phi), axes=0) - np.tensordot((y3-y1)/Lv, np.cos(phi), axes=0) ) * \ ( np.tensordot((x3-x1)/Lv, np.cos(phi)*ctheta, axes=0) + np.tensordot((y3-y1)/Lv, np.cos(phi)*ctheta, axes=0) - \ np.tensordot((z3-z1)/Lv, stheta, axes=0) ) Ccontract_w = (np.tensordot((x3-x2)/Lw, np.sin(phi), axes=0) - np.tensordot((y3-y2)/Lw, np.cos(phi), axes=0) ) * \ ( np.tensordot((x3-x2)/Lw, np.cos(phi)*ctheta, axes=0) + np.tensordot((y3-y2)/Lw, np.cos(phi)*ctheta, axes=0) - \ np.tensordot((z3-z2)/Lw, stheta, axes=0) ) # Calculate the detector response for each frequency for ii in range(0, f0.size): # Calculate GW transfer function for the michelson channels gammaU_p = 1/2 * (np.sinc((f0[ii])*(1 - udir)/np.pi)*np.exp(-1j*f0[ii]*(3 + udir)) + \ np.sinc((f0[ii])*(1 + udir)/np.pi)*np.exp(-1j*f0[ii]*(1 + udir))) gammaU_m = 1/2 * (np.sinc((f0[ii])*(1 + udir)/np.pi)*np.exp(-1j*f0[ii]*(3 - udir)) + \ np.sinc((f0[ii])*(1 - udir)/np.pi)*np.exp(-1j*f0[ii]*(1 - udir))) gammaV_p = 1/2 * (np.sinc((f0[ii])*(1 - vdir)/np.pi)*np.exp(-1j*f0[ii]*(3 + vdir)) + \ np.sinc((f0[ii])*(1 + vdir)/np.pi)*np.exp(-1j*f0[ii]*(1+vdir))) gammaV_m = 1/2 * (np.sinc((f0[ii])*(1 + vdir)/np.pi)*np.exp(-1j*f0[ii]*(3 - vdir)) + \ np.sinc((f0[ii])*(1 - vdir)/np.pi)*np.exp(-1j*f0[ii]*(1 - vdir))) gammaW_p = 1/2 * (np.sinc((f0[ii])*(1 - wdir)/np.pi)*np.exp(-1j*f0[ii]*(3 + wdir)) + \ np.sinc((f0[ii])*(1 + wdir)/np.pi)*np.exp(-1j*f0[ii]*(1 + wdir))) gammaW_m = 1/2 * (np.sinc((f0[ii])*(1 + wdir)/np.pi)*np.exp(-1j*f0[ii]*(3 - wdir)) + \ np.sinc((f0[ii])*(1 - wdir)/np.pi)*np.exp(-1j*f0[ii]*(1 - wdir))) ## Michelson Channel Antenna patterns for + pol ## Fplus_u = 1/2(u x u)Gamma(udir, f):eplus Fplus_u_p = Pcontract_u * gammaU_p Fplus_u_m = Pcontract_u * gammaU_m Fplus_v_p = Pcontract_v * gammaV_p Fplus_v_m = Pcontract_v * gammaV_m Fplus_w_p = Pcontract_w * gammaW_p Fplus_w_m = Pcontract_w * gammaW_m ## Michelson Channel Antenna patterns for x pol ## Fcross_u = 1/2(u x u)Gamma(udir, f):ecross Fcross_u_p = Ccontract_u * gammaU_p Fcross_u_m = Ccontract_u * gammaU_m Fcross_v_p = Ccontract_v * gammaV_p Fcross_v_m = Ccontract_v * gammaV_m Fcross_w_p = Ccontract_w * gammaW_p Fcross_w_m = Ccontract_w * gammaW_m ## First Michelson antenna patterns ## Calculate Fplus Fplus1 = (Fplus_u_p - Fplus_v_p) Fplus2 = (Fplus_w_p - Fplus_u_m) Fplus3 = (Fplus_v_m - Fplus_w_m) ## Calculate Fcross Fcross1 = (Fcross_u_p - Fcross_v_p) Fcross2 = (Fcross_w_p - Fcross_u_m) Fcross3 = (Fcross_v_m - Fcross_w_m) ## Calculate antenna patterns for the A, E and T channels - We are switiching to doppler channel. FAplus = (1 / 3) * np.sin( 2 * f0[ii]) * (2 * Fplus1 - Fplus2 - Fplus3) FEplus = (1 / np.sqrt(3)) * np.sin(2 * f0[ii]) * (Fplus3 - Fplus2) FTplus = (1 / 3) * np.sin(2 * f0[ii]) * (Fplus1 + Fplus3 + Fplus2) FAcross = (1 / 3) * np.sin( 2 * f0[ii]) * (2 * Fcross1 - Fcross2 - Fcross3) FEcross = (1 / np.sqrt(3)) * np.sin( 2 * f0[ii]) * (Fcross3 - Fcross2) FTcross = (1 / 3) * np.sin( 2 * f0[ii]) * (Fcross1 + Fcross3 + Fcross2) ## Detector response for the TDI Channels, summed over polarization ## and integrated over sky direction R1[:, ii] = dOmega / (8 * np.pi) * np.sum( (np.absolute(FAplus))**2 + (np.absolute(FAcross))**2, axis=1) R2[:, ii] = dOmega / (8 * np.pi) * np.sum( (np.absolute(FEplus))**2 + (np.absolute(FEcross))**2, axis=1) R3[:, ii] = dOmega / (8 * np.pi) * np.sum( (np.absolute(FTplus))**2 + (np.absolute(FTcross))**2, axis=1) ## Output detector response arrays; these can then be loaded instead of calculated for future analyses of the same data. np.savetxt('R1arrayAET.txt', R1) np.savetxt('R2arrayAET.txt', R2) np.savetxt('R3arrayAET.txt', R3) return R1, R2, R3
a = a.reshape(-1) for i, v in enumerate(a): a[i] = f(v) if __name__ == '__main__': # df = pandas.read_table('../doc/density32.tsv', dtype=None, index_col=0)['count'] nside = 32 for j in range(0, 100): (n, k, l) = spherical.indexes(j) df = [] for i in range(0, 12 * (nside**2)): (theta, phi) = hp.pix2ang(nside, i) df.append(spherical.fvj(j, phi, -theta + np.pi / 2)) plt.clf() fig = plt.figure(1, figsize=(20, 15)) hp.mollview(np.array(df), title=u"Spherical function for j=%d (n=%d, k=%d, l=%d)" % (j, n, k, l), flip='astro', fig=1) hp.graticule() plt.savefig('../../doc/fvj/%d.png' % j, dpi=60)
ax = fig.add_subplot(111, projection=projection, axisbg ='LightCyan') ax.scatter(np.radians(x),np.radians(Dec), alpha=0.6, s=0.001) # convert degrees to radians ax.set_xticklabels(tick_labels) # we add the scale on the x axis ax.set_title(title) ax.title.set_fontsize(15) ax.set_xlabel("RA") ax.xaxis.label.set_fontsize(12) ax.set_ylabel("Dec") ax.yaxis.label.set_fontsize(12) ax.grid(True) #plot_mwd(allarr[0], allarr[1], org=180., title = "NVSS Raw, +/- 5deg b cut, +/-5deg supergalactic b cut") #plt.show() fout = open(outfile+'_result.txt', "w") fout.write(str(decmin)+ "," + str(decmax)+"\n") fout.write(str(totno)+","+str(gallatcutno)+","+str(n2masscutno)+","+str(w1mprocutno)+","+str(wmproj2masscutno)+","+str(j2masscutno)+","+str(sgbcutno)+","+str(finalno)+"\n") for i in range(0, npix): dec, ra = np.deg2rad((90. - np.rad2deg(hp.pix2ang(scanres, i)[0]))), hp.pix2ang(scanres, i)[1] print "DEC. ", (90. - np.rad2deg(hp.pix2ang(scanres, i)[0])), " R.A.", np.rad2deg(hp.pix2ang(scanres, i)[1]) dangle = np.rad2deg(np.arccos(np.cos(np.deg2rad(allarr[1]))*np.cos(dec)*np.cos(np.deg2rad(allarr[0]) - ra)+np.sin(np.deg2rad(allarr[1]))*np.sin(dec))) print "UH: ", len(allarr.transpose()[dangle<90.]), "LH: ", len(allarr.transpose()[dangle>90.]) fout.write(str(np.rad2deg(dec))+","+str(np.rad2deg(ra))+","+str(len(allarr.transpose()[dangle<90.]))+","+str(len(allarr.transpose()[dangle>90.]))+"\n") fout.close() os.system('mv '+ outfile+'_result.txt '+outfolder) os.system('mv '+ outfile+'_galaxyselection.txt '+outfolder)
#hp = HEALPix(nside=2048, order='nested', frame=ICRS() planckpix = np.arange(0, planckImap.size) planckImask = healpy.pixelfunc.ud_grade(scosmask, 2048, order_in='RING', order_out='NEST') planckImap = planckImap[planckImask == 1] planckpix = planckpix[planckImask == 1] planck_ra, planck_dec = healpy.pix2ang(nside=2048, ipix=planckpix, nest=True, lonlat=True) coord = SkyCoord(planck_ra, planck_dec, frame='galactic', unit='deg').icrs planck_ra, planck_dec = coord.ra.deg, coord.dec.deg ''' plt.scatter(planck_ra,planck_dec,s=0.01,c=planckImap,cmap='rainbow',edgecolors='none') plt.xlabel('RA(deg)') plt.ylabel('DEC(deg)') plt.title('Planck map after mask') plt.colorbar() plt.show() input('stop here') ''' print 'planck mean:', np.mean(planckImap)
plt.savefig(filepath_map + 'C_plot_output/density_field_lognormal_map_' + str(j + 1) + '.pdf') ## HEALPY for i in range(patch_count): pixel_id = random.randint(1, density_field_lognormal.size - 1) #print('Patch # '+str(i+1)+': ', pixel_id) patch_center = hp.pix2vec(NSIDE, pixel_id) # find the pixels pixels_indices_patch = hp.query_disc(NSIDE, patch_center, patch_radius) density_field_lognormal_patch = density_field_lognormal[ pixels_indices_patch] DEC = np.pi / 2 - hp.pix2ang(NSIDE, pixels_indices_patch)[0] RA = hp.pix2ang(NSIDE, pixels_indices_patch)[1] RA_data = Column(RA, name='RA', dtype='float') # column having ra DEC_data = Column(DEC, name='DEC', dtype='float') # column having DEC del_data = Column(density_field_lognormal_patch, name='del', dtype='float') # column having pixels_patch_values density_fluctuations_table = Table((RA_data, DEC_data, del_data)) density_fluctuations_table.write( filepath_map + '/A_healpy_patches_produced/del_lognormal_patch_' + str(i + 1) + '.fits', overwrite=True) end = time.time()
def BeamMap( pointRA=None, pointDec=None, dwl=None, freq=None, uniform=False, Bmap0=None, dtype='float32', nside=None ) : ''' pointRA, pointDec: [degree] Where does the antenna point to? default points to (RA, Dec) = (0, 0) dwl: [meter] d - diameter: for dish: one value w - width, l - length: for cylinder: (w,l) freq: [MHz] Used to get FWHM uniform: True | False If ==True: return =1 map Bmap0: Use this Bmap0 as the basic and rotate it to (pointRA, pointDec) ''' import healpy as hp import numpy as np from jizhipy.Array import Asarray from jizhipy.Basic import Raise from jizhipy.Transform import CoordTrans try : dtype = np.dtype(dtype) except : dtype = np.dtype(None) if (nside is not None) : nside = int(round(nside)) elif (Bmap0 is not None) : nside = hp.get_nside(Bmap0) else : Raise(Exception, 'nside = None') #-------------------------------------------------- if (uniform) : Bmap = np.ones(12*nside**2, dtype) if (Bmap0 is not None) : Bmap *= Bmap0[Bmap0.size/2] return Bmap #-------------------------------------------------- if (Bmap0 is not None) : nside0 = hp.get_nside(Bmap0) if (nside0 != nside) : Bmap0 = hp.ud_grade(nside, Bmap0) Bmap0 = Bmap0.astype(dtype) #-------------------------------------------------- else : n = hp.ang2pix(nside, np.pi/2, 0) Bmap0 = np.zeros(12*nside**2, dtype) Bmap0[n] = 10000 D = Asarray(dwl)[0] fwhm = 1.03 * 300/freq / D Bmap0 = hp.smoothing(Bmap0, fwhm, verbose=False) Bmap0[Bmap0<0] = 0 Bmap0 /= Bmap0.sum() #-------------------------------------------------- if (pointRA is None) : pointRA = 0 if (pointDec is None) : pointDec = 0 if (abs(pointRA)<1e-4 and abs(pointDec)<1e-4) : return Bmap0 #-------------------------------------------------- theta, phi = hp.pix2ang(nside, np.arange(12*nside**2)) theta, phi = CoordTrans.thetaphiRotation([theta, phi], az=pointRA, ay=-pointDec) n = hp.ang2pix(nside, theta, phi) Bmap = Bmap0[n] return Bmap
hotspots = [] fhdr = ['#%s'%'Sigma'.rjust(9),\ '%12s'%'Ra','%12s'%'Dec',\ '%10s'%'gLong','%10s'%'gLat'] hotspots.append(fhdr) print "\n%s\n%10s\t%10s\t%6s" % (''.center(26, '-'), 'Ra', 'Dec', 'Sigma') for i in srtidx: if np.any(i == localidx) == False: continue if sigmap[i] < args.minSig: break if any(i == msked): continue nnghrs = hp.query_disc(nside, hp.pix2vec(nside, i), rexcld) msked = np.append(msked, nnghrs) dec, ra = hp.pix2ang(nside, i) ra = ra dec = np.pi / 2. - dec l, b = equ2gal(ra, dec, indegrees=False) if args.otype == 'std': carr=['%10.2f'%sigmap[i],\ '%12s'%np.rad2deg(ra),'%12s'%np.rad2deg(dec),\ '%10.2f'%np.rad2deg(l),'%10.2f'%np.rad2deg(b)] elif args.otype == 'rad': carr=['%10.2f'%sigmap[i],\ '%12s'%(ra),'%12s'%(dec),\ '%10.2f'%(l),'%10.2f'%(b)] elif args.otype == 'hours' and noephem == False: carr=['%10.2f'%sigmap[i],\ '%12s'%epm.hours(ra),'%12s'%epm.degrees(dec),\ '%10.2f'%np.rad2deg(l),'%10.2f'%np.rad2deg(b)]
def add_5(self): diffuse = self.produce_data() self.mask_spur_loopI(diffuse) if self.using_raw_diffuse == True: smooth_diffuse = diffuse self.mask_spur_loopI(smooth_diffuse) if self.using_raw_diffuse == False: if self.index_type == 'pixel_dependence_index_minus_I_E': smooth_diffuse = self.masked_smoothing(diffuse, rad=1.0) if self.index_type == 'constant_index_minus_I_E': smooth_diffuse = self.masked_smoothing(diffuse, rad=1.0) if self.index_type == 'freq_dependence_index_minus_I_E': smooth_diffuse = self.masked_smoothing(diffuse, rad=1.0) self.mask_spur_loopI(smooth_diffuse) result = [] for index in range(smooth_diffuse.size): #for index in np.where(smooth_diffuse != np.nan)[0]: ll, bb = hp.pix2ang(self.nside, index, lonlat=True) #remove small scale terbulance #CAS-a 111.73,-2.13 #Cygnus 71,4 #Cen-A 309.51589573409,19.41727341133 #SMC 302.8084 -44.3277 #LMC 280.4652 -32.8884 #Vela 263.9390 -03.3683 nside = 2**4 a = (hp.get_all_neighbours(nside, 111.73, -2.13, lonlat=True)) b = (hp.get_all_neighbours(nside, 71, 4, lonlat=True)) c = (hp.get_all_neighbours(nside, 309.51589573409, 19.41727341133, lonlat=True)) d = (hp.get_all_neighbours(nside, 302.8084, -44.3277, lonlat=True)) e = (hp.get_all_neighbours(nside, 280.4652, -32.8884, lonlat=True)) f = (hp.get_all_neighbours(nside, 263.9390, -3.3683, lonlat=True)) g = (hp.get_all_neighbours(nside, 0, 0, lonlat=True)) total = list(a) + list(b) + list(c) + list(d) + list(e) + list( f) + list(g) a = (hp.ang2pix(nside, 111.73, -2.13, lonlat=True)) b = (hp.ang2pix(nside, 71, 4, lonlat=True)) c = (hp.ang2pix(nside, 309.51589573409, 19.41727341133, lonlat=True)) d = (hp.ang2pix(nside, 302.8084, -44.3277, lonlat=True)) e = (hp.ang2pix(nside, 280.4652, -32.8884, lonlat=True)) f = (hp.ang2pix(nside, 263.9390, -3.3683, lonlat=True)) g = (hp.ang2pix(nside, 0, 0, lonlat=True)) total2 = [a, b, c, d, e, f, g] pass_pix_num = np.array(total + total2) mask = np.ones_like(smooth_diffuse, dtype=np.bool) mask[pass_pix_num] = False if mask[index] == False: print('pass_pix_number', index) if mask[index] == True: pix_value = smooth_diffuse[index] if ~np.isnan(pix_value): result.append([ll, bb, pix_value]) result = np.array(result) with h5py.File( str(self.v) + 'MHz_Smooth_' + 'after_mask_smooth_diffuse_x.hdf5', 'w') as f: f.create_dataset('smooth_diffuse', data=smooth_diffuse) f.create_dataset('masked_diffuse', data=diffuse) f.create_dataset('diffuse', data=diffuse) print(result[-1, :]) return result
def written_as_a_function_to_save_memory(z_bins, catalogname, single_fre): if catalogname == '1': print('loading SuperCosmos catalog and mask...') #load supercosmos catalog #catalog=np.loadtxt('wiseScosPhotoz160708.csv',skiprows=1,delimiter=",",usecols=(7,8,11,16,18),max_rows=3000) #RA, DEC,Ebv(extinction),z,mask from "all sky survey" #catalog=np.loadtxt('wiseScosPhotoz160708.csv',skiprows=1,delimiter=",",usecols=(7,8,16)) #ra,dec,z (in degrees) #print(catalog) #catalog=catalog.transpose() catalog = np.load('datalog/wisecatalog.npy') scosmask = healpy.read_map('WISExSCOSmask.fits') #num=catalog[0].size num = 30000000 #coord=SkyCoord(catalog[0],catalog[1],frame='galactic',unit='deg').icrs #catalog[0],catalog[1]=coord.ra.deg,coord.dec.deg coord = SkyCoord(catalog[0], catalog[1], frame='icrs', unit='deg').galactic l, b = coord.l.deg, coord.b.deg catalog = catalog[:, scosmask[healpy.ang2pix( 256, l, b, nest=False, lonlat=True)] == 1] catalog = catalog[:, catalog[2].argsort()] if z_bins == 3: catalog = catalog[:, catalog[0].size / 4 * z_bins:] else: catalog = catalog[:, catalog[0].size / 4 * z_bins:catalog[0].size / 4 * (z_bins + 1)] if z_bins == 0: catalog = catalog[:, catalog[2] >= 0.01] #print('bin',z_bins,'size',catalog[2].shape,'z range',catalog[2,0],'~',catalog[2,-1]) #cat_galaxy=treecorr.Catalog(ra=catalog[0],dec=catalog[1],ra_units='deg',dec_units='deg',k=np.ones(catalog[0].size)) cat_galaxy = treecorr.Catalog(ra=catalog[0], dec=catalog[1], ra_units='deg', dec_units='deg') print('Done!\n') print 'generating random galaxy catalog' #plt.scatter(catalog[0],catalog[1],s=0.01) #plt.xlabel('RA(deg)') #plt.ylabel('DEC(deg)') #plt.show() ra_min = np.min(cat_galaxy.ra) ra_max = np.max(cat_galaxy.ra) dec_min = np.min(cat_galaxy.dec) dec_max = np.max(cat_galaxy.dec) print('ra range = %f .. %f' % (ra_min, ra_max)) print('dec range = %f .. %f' % (dec_min, dec_max)) rand_ra = np.random.uniform(ra_min, ra_max, num) rand_sindec = np.random.uniform(np.sin(dec_min), np.sin(dec_max), num) rand_dec = np.arcsin(rand_sindec) coord = SkyCoord(rand_ra, rand_dec, frame='icrs', unit='rad').galactic l, b = coord.l.deg, coord.b.deg rand_ra = rand_ra[scosmask[healpy.ang2pix( 256, l, b, nest=False, lonlat=True)] == 1] rand_dec = rand_dec[scosmask[healpy.ang2pix( 256, l, b, nest=False, lonlat=True)] == 1] #plt.scatter(np.rad2deg(rand_ra),np.rad2deg(rand_dec),s=0.01) #plt.xlabel('RA(deg)') #plt.ylabel('DEC(deg)') #plt.show() print('Done!\n') if catalogname == '2': print('loading MCXC catalog and mask...') num = 1000000 MCXCfile = pyfits.open('MCXC.fits') MCXCdata = MCXCfile[1].data MCXCfile.close() MCXCmaskfile = pyfits.open('HFI_Mask_GalPlane-apo0_2048_R2.00.fits') MCXCmask = MCXCmaskfile[1].data['GAL080'] MCXCmaskfile.close() coord = SkyCoord(MCXCdata['RA'], MCXCdata['DEC'], frame='icrs', unit='deg').galactic l, b = coord.l.deg, coord.b.deg MCXCdata = MCXCdata[MCXCmask[healpy.ang2pix( 2048, l, b, nest=True, lonlat=True)] == 1] #ra260-280 dec60-70 suspicious area MCXCmask2 = np.ones(MCXCdata.size) for i in range(MCXCdata.size): if (260 < MCXCdata[i]['RA'] < 280 and 60 < MCXCdata[i]['DEC'] < 70): MCXCmask2[i] = 0 MCXCdata = MCXCdata[MCXCmask2 == 1] cat_galaxy = treecorr.Catalog(ra=MCXCdata['RA'], dec=MCXCdata['DEC'], ra_units='deg', dec_units='deg') #cat_galaxy=treecorr.Catalog(ra=MCXCdata['RA'],dec=MCXCdata['DEC'],ra_units='deg',dec_units='deg',k=np.ones(MCXCdata['RA'].size)) print('Done!\n') #plt.scatter(MCXCdata['RA'],MCXCdata['DEC'],s=0.5) #plt.xlabel('RA(deg)') #plt.ylabel('DEC(deg)') #plt.show() print 'generating random galaxy catalog' ra_min = np.min(cat_galaxy.ra) ra_max = np.max(cat_galaxy.ra) dec_min = np.min(cat_galaxy.dec) dec_max = np.max(cat_galaxy.dec) print('ra range = %f .. %f' % (ra_min, ra_max)) print('dec range = %f .. %f' % (dec_min, dec_max)) rand_ra = np.random.uniform(ra_min, ra_max, num) rand_sindec = np.random.uniform(np.sin(dec_min), np.sin(dec_max), num) rand_dec = np.arcsin(rand_sindec) #plt.hist(rand_ra) #plt.show() #plt.hist(rand_sindec) #plt.show() #scosmask=healpy.read_map('WISExSCOSmask.fits') planckmask = pyfits.open('HFI_Mask_GalPlane-apo0_2048_R2.00.fits') planckImask = planckmask[1].data['GAL080'] planckmask.close() coord = SkyCoord(rand_ra, rand_dec, frame='icrs', unit='rad').galactic l, b = coord.l.deg, coord.b.deg rand_ra = rand_ra[planckImask[healpy.ang2pix( 2048, l, b, nest=True, lonlat=True)] == 1] rand_dec = rand_dec[planckImask[healpy.ang2pix( 2048, l, b, nest=True, lonlat=True)] == 1] #ra260-280 dec60-70 suspicious area MCXCrandmask2 = np.ones(rand_ra.size) for i in range(rand_ra.size): if (260 < np.rad2deg(rand_ra[i]) < 280 and 60 < np.rad2deg(rand_dec[i]) < 70): MCXCrandmask2[i] = 0 rand_ra = rand_ra[MCXCrandmask2 == 1] rand_dec = rand_dec[MCXCrandmask2 == 1] #plt.scatter(np.rad2deg(rand_ra),np.rad2deg(rand_dec),s=0.5) #plt.scatter(MCXCdata['RA'],MCXCdata['DEC'],s=0.5) #plt.xlabel('RA(deg)') #plt.ylabel('DEC(deg)') #plt.show() #input('stop here') print('Done!\n') if catalogname != '3': cat_rand = treecorr.Catalog(ra=rand_ra, dec=rand_dec, ra_units='radians', dec_units='radians') #load planck data print('loading Planck catalog and mask...') if single_fre == '1': ''' planckdata=fits.open('http://pla.esac.esa.int/pla/aio/product-action?MAP.MAP_ID=COM_CompMap_ISW_0064_R2.00.fits') planckImap=planckdata[1].data['I'] planckImask=planckdata[1].data['I_MASK'] planckdata.close() planckpix=np.arange(0,planckImap.size) planckImap=planckImap[planckImask==1] planckImap=planckImap*2.725 planckpix=planckpix[planckImask==1] planck_ra,planck_dec=healpy.pix2ang(nside=64,ipix=planckpix,nest=True,lonlat=True) coord=SkyCoord(planck_ra,planck_dec,frame='galactic',unit='deg').icrs planck_ra,planck_dec=coord.ra.deg,coord.dec.deg print 'ISW mean:',np.mean(planckImap) ''' planckdata = fits.open( 'http://pla.esac.esa.int/pla/aio/product-action?MAP.MAP_ID=COM_CompMap_ISW_0064_R2.00.fits' ) planckImap = planckdata[1].data['I'] planckImask = planckdata[1].data['I_MASK'] planckdata.close() Imax = np.max(planckImap[planckImask == 1]) Imin = np.min(planckImap[planckImask == 1]) planck2048 = healpy.pixelfunc.ud_grade(planckImap, 2048, order_in='NEST', order_out='NEST') planck2048mask = healpy.pixelfunc.ud_grade(planckImask, 2048, order_in='NEST', order_out='NEST') planckpix = np.arange(0, planck2048.size) planckpix = planckpix[planck2048mask == 1] planck_ra, planck_dec = healpy.pix2ang(nside=2048, ipix=planckpix, nest=True, lonlat=True) planckImap = healpy.pixelfunc.get_interp_val(planckImap, planck_ra, planck_dec, nest=True, lonlat=True) coord = SkyCoord(planck_ra, planck_dec, frame='galactic', unit='deg').icrs planck_ra, planck_dec = coord.ra.deg, coord.dec.deg planckset = np.array([planckImap, planck_ra, planck_dec]) planckset = planckset[:, (planckset[0] >= Imin) & (planckset[0] <= Imax)] planckImap, planck_ra, planck_dec = planckset[0], planckset[ 1], planckset[2] coord = None planckImask = None planckpix = None planck2048 = None planck2048mask = None planckImap = planckImap * 2.725 #print planckImap[5000:5200] #np.save('datalog/test.npy',planckImap) #input('stop here') ''' plt.scatter(planck_ra,planck_dec,s=0.01,c=planckImap,cmap='rainbow',edgecolors='none') plt.xlabel('RA(deg)') plt.ylabel('DEC(deg)') plt.title('Planck map after mask') plt.colorbar() plt.show() input('stop here') ''' if single_fre == '2': planckdata = fits.open( 'http://pla.esac.esa.int/pla/aio/product-action?MAP.MAP_ID=COM_CompMap_CIB-GNILC-F545_2048_R2.00.fits' ) planckImap = planckdata[1].data['I'] planckdata.close() planckpix = np.arange(0, planckImap.size) planckpix = planckpix[planckImap != 0] planckImap = planckImap[planckImap != 0] planck_ra, planck_dec = healpy.pix2ang(nside=2048, ipix=planckpix, nest=False, lonlat=True) coord = SkyCoord(planck_ra, planck_dec, frame='galactic', unit='deg').icrs planck_ra, planck_dec = coord.ra.deg, coord.dec.deg print 'CIB mean:', np.mean(planckImap) ''' plt.scatter(planck_ra,planck_dec,s=0.01,c=planckImap,cmap='rainbow',edgecolors='none') plt.xlabel('RA(deg)') plt.ylabel('DEC(deg)') plt.title('CIB scatter') plt.colorbar() plt.show() input('stop here') ''' if single_fre == '3': planckdata = fits.open( 'http://pla.esac.esa.int/pla/aio/product-action?MAP.MAP_ID=HFI_CompMap_Foregrounds-commander-143_R3.00.fits' ) planckmask = fits.open('HFI_Mask_GalPlane-apo0_2048_R2.00.fits') planckImask = planckmask[1].data['GAL080'] planckImap = healpy.fitsfunc.read_map(planckdata, field=0, nest=True) planckdata.close() planckmask.close() planckpix = np.arange(0, planckImap.size) planckImap = planckImap[planckImask == 1] planckpix = planckpix[planckImask == 1] planck_ra, planck_dec = healpy.pix2ang(nside=2048, ipix=planckpix, nest=True, lonlat=True) coord = SkyCoord(planck_ra, planck_dec, frame='galactic', unit='deg').icrs planck_ra, planck_dec = coord.ra.deg, coord.dec.deg print 'subtracted mean:', np.mean(planckImap) ''' plt.scatter(planck_ra,planck_dec,s=0.005,c=planckImap,cmap='rainbow',edgecolors='none') plt.xlabel('RA(deg)') plt.ylabel('DEC(deg)') plt.title('CMB subtracted scatter') plt.colorbar() plt.show() input('stop here') ''' cat_planck = treecorr.Catalog(ra=planck_ra, dec=planck_dec, ra_units='deg', dec_units='deg', k=planckImap) print('Done!\n') #print planckpix.size,planckImap.size,plancknoise.size print('calculating cross-relation...') #'''cross correlation nk = treecorr.NKCorrelation(min_sep=0.01, max_sep=10, nbins=35, sep_units='deg') rk = treecorr.NKCorrelation(min_sep=0.01, max_sep=10, nbins=35, sep_units='deg') #nk = treecorr.KKCorrelation(min_sep=0.01, max_sep=3.0, nbins=50, sep_units='radians') nk.process(cat_galaxy, cat_planck) rk.process(cat_rand, cat_planck) xi, varxi = nk.calculateXi(rk) sig = np.sqrt(varxi) r = np.exp(nk.meanlogr) #''' '''auto correlation nn = treecorr.NNCorrelation(min_sep=0.01, max_sep=10, nbins=35, sep_units='deg') rr = treecorr.NNCorrelation(min_sep=0.01, max_sep=10, nbins=35, sep_units='deg') nr = treecorr.NNCorrelation(min_sep=0.01, max_sep=10, nbins=35, sep_units='deg') nn.process(cat_galaxy) rr.process(cat_rand) nr.process(cat_galaxy, cat_rand) xi, varxi = nn.calculateXi(rr, nr) sig = np.sqrt(varxi) r = np.exp(nn.meanlogr) ''' #print r #print xi print('Done!\n') if single_fre == '2': xi = xi * 69 * 1e-6 varxi = varxi * 69 * 1e-6 #print('Plotting') #not sure what r is '''auto correlation plot plt.plot(r, xi, color='blue') plt.plot(r,np.zeros(xi.size),color='red') plt.errorbar(r, xi, yerr=sig, lw=1, ls='',ecolor='g') #plt.xlabel(r'$\theta$ (rad)') plt.xlabel(r'$\theta$ (degrees)') plt.xscale('log') if catalogname=='1': plt.title('SuperCosmos x SuperCosmos') if catalogname=='2': plt.title('MCXC x MCXC') plt.show() ''' if single_fre == '1': data = np.load('datalog/SuperCosmos.npy') cmbdata = data[0] cmbr = data[1] plt.plot(cmbr, cmbdata, color='green', label='CMB X galaxy') plt.plot(r, xi, color='blue', label='ISW X galaxy') plt.plot([0, 1, 10], [0, 0, 0], color='red', ls=':') plt.xscale('log') #plt.yscale('log', nonposy='clip') plt.xlabel(r'$\theta$ (degrees)') plt.ylabel(r'$w(\theta)$(K)') plt.legend() if single_fre == '2': plt.plot(r, xi, color='blue', label='CIB X galaxy') plt.plot([0, 1, 10], [0, 0, 0], color='red', ls=':') plt.xscale('log') plt.xlabel(r'$\theta$ (degrees)') plt.ylabel(r'$w(\theta)$(K)') plt.legend() if single_fre == '3': plt.plot(r, xi, color='blue', label='Subtracted X galaxy') plt.plot([0, 1, 10], [0, 0, 0], color='red', ls=':') plt.xscale('log') plt.xlabel(r'$\theta$ (degrees)') plt.ylabel(r'$w(\theta)$(K)') plt.legend() if catalogname == '1': if single_fre == '1': plt.title('SuperCosmos x ISW z {}'.format(str(z_bins))) if single_fre == '2': plt.title('SuperCosmos x CIB z {}'.format(str(z_bins))) if single_fre == '3': plt.title('SuperCosmos x Subtracted z {}'.format(str(z_bins))) if catalogname == '2': if single_fre == '1': plt.title('MCXC x ISW') if single_fre == '2': plt.title('MCXC x CIB') plt.show() #'''save data if single_fre == '1': if catalogname == '1': np.save('datalog/ISW_z_{}.npy'.format(str(z_bins)), np.array([xi, r, sig])) print('datalog saved!') if single_fre == '2': if catalogname == '1': np.save('datalog/CIB_z_{}.npy'.format(str(z_bins)), np.array([xi, r, sig])) print('datalog saved!') if single_fre == '3': if catalogname == '1': np.save('datalog/Subtracted_z_{}.npy'.format(str(z_bins)), np.array([xi, r, sig])) print('datalog saved!') #''' print('4 bins datalog saved!') nk.clear() rk.clear() cat_galaxy.clear_cache() cat_rand.clear_cache() cat_planck.clear_cache() return None