def stack(ras, decs, map1, mask1, map2 = None, mask2 = None, width = 20.): stack = 0 divisor = 0 for i in range(len(ras)): tempdec, tempra = np.deg2rad([decs[i], ras[i]]) tempwid = np.deg2rad(width/60.) box = [[tempdec-tempwid,tempra-tempwid],[tempdec+tempwid,tempra+tempwid]] maskstamp = mask1.submap(box) #Check if the stamp is entirely within borders, i.e. if the mask stamp is entirely ones if np.any(maskstamp[0]): stamp = reproject.postage_stamp(map1, ras[i], decs[i], width, 0.5) if stamp is None: continue stack += stamp[0] divisor += 1 if map2 is None: continue #If a second map is to be checked, repeat above with second map maskstamp = mask2.submap(box) if np.any(maskstamp[0]): print(i) stamp = reproject.postage_stamp(map2, ras[i], decs[i], width, 0.5) if stamp is None: continue stack += stamp[0] divisor += 1 stack /= divisor print(divisor) return stack
def get_extraction_test_results(yaml_file): print("Starting tests from ", yaml_file) with open(yaml_file) as f: config = yaml.safe_load(f) geos = get_geometries(config['geometries']) lmax = config['lmax'] lmax_pad = config['lmax_pad'] spectra = get_spectra(config['spectra'], lmax, lmax_pad) seed = config['seed'] results = {} for g in geos.keys(): results[g] = {} for s in spectra.keys(): results[g][s] = {} imap = generate_map(geos[g][0][-2:], geos[g][1], spectra[s], lmax, seed) # Do write and read test filename = "temporary_map.fits" # NOT THREAD SAFE enmap.write_map(filename, imap) imap_in = enmap.read_map(filename) check_equality(imap, imap_in) for e in config['extracts']: print("Doing test for extract ", e['name'], " with geometry ", g, " and spectrum ", s, "...") if e['type'] == 'slice': box = np.deg2rad(np.array(e['box_deg'])) cutout = enmap.read_map(filename, box=box) cutout_internal = imap.submap(box=box) elif e['type'] == 'postage': dec_deg, ra_deg = e['center_deg'] width_arcmin = e['width_arcmin'] res_arcmin = e['res_arcmin'] cutout = reproject.postage_stamp(filename, ra_deg, dec_deg, width_arcmin, res_arcmin, proj='gnomonic') cutout_internal = reproject.postage_stamp(imap, ra_deg, dec_deg, width_arcmin, res_arcmin, proj='gnomonic') check_equality(cutout, cutout_internal) pixels = get_reference_pixels(cutout.shape) results[g][s]['refpixels'] = get_pixel_values(cutout, pixels) results[g][s]['meansquare'] = get_meansquare(cutout) os.remove(filename) return results, config['result_name']
def freqStamp(ra, dec, fmap, name, width = 0.5, write = True): stamp = reproject.postage_stamp(fmap, ra, dec, width*60, 0.5) if write: stamp.wcs.wcs.crval = [ra,dec] enmap.write_map('./for_tony/{}.fits'.format(name), stamp) return stamp
def s18dStamp(ra, dec, data, name, width=0.5, write=True): #Find tile corresponding to RA, Dec path = '/scratch/r/rbond/jorlo/S18d_202006/filteredMaps/' tileName = tileFinder(ra, dec, data) if tileName == None: return None tile = enmap.read_map(path + tileName + '/Arnaud_M2e14_z0p4#' + tileName + '_filteredMap.fits') stamp = reproject.postage_stamp(tile, ra, dec, width * 60, 0.5) if write: #tempdec, tempra = np.deg2rad([dec, ra]) #tempwid = np.deg2rad(width) #box = [[tempdec-tempwid,tempra-tempwid],[tempdec+tempwid,tempra+tempwid]] #stampgeo = tile.submap(box) box = np.array([[ra - width / 2, dec - width / 2], [ra + width / 2, dec + width / 2]]) * utils.degree shape, wcs = enmap.geometry(pos=box, res=0.5 * utils.arcmin, proj='car') print(shape) #print(stampgeo.wcs) #print(stamp.wcs) stamp.wcs = wcs print(stamp.wcs) print(stamp[0].shape) #plt.imshow(stamp[0]) #plt.show() #Return map plot = enplot.plot(stamp, mask=0) enplot.show(plot) enmap.write_map('./for_tony/{}.fits'.format(name), stamp) return stamp
def stack(ras, decs, map): stack = 0 for i in range(len(ras)): stamp = reproject.postage_stamp(map, ras[i], decs[i], 20., 0.5) if stamp is None: continue stack += stamp[0] stack /= len(ras) return stack
def s18dStamp(ra, dec, data, name, width = 0.5, write = True): #Find tile corresponding to RA, Dec path = '/scratch/r/rbond/jorlo/S18d_202006/filteredMaps/' tileName = tileFinder(ra, dec, data) if tileName == None: return None tile = enmap.read_map(path+tileName+'/Arnaud_M2e14_z0p4#'+tileName+'_filteredMap.fits') stamp = reproject.postage_stamp(tile, ra, dec, width*60, 0.5) if write: stamp.wcs.wcs.crval = [ra,dec] enmap.write_map('./for_tony/{}.fits'.format(name), stamp) return stamp
def s18dStamp(ra, dec, data, name, width=0.5, write=False): #Find tile corresponding to RA, Dec path = '/project/r/rbond/jorlo/S18d_202006/filteredMaps/' tileName = tileFinder(ra, dec, data) if tileName == None: return None tile = enmap.read_map(path + tileName + '/Arnaud_M2e14_z0p4#' + tileName + '_filteredMap.fits') stamp = reproject.postage_stamp(tile, ra, dec, width * 60, 0.5) #print(stamp) if write: temp = np.ndarray((2, ), buffer=np.array([ra, dec])) stamp.wcs.wcs.crval = temp enmap.write_map( './for_tony/mustang2/for_charles/y0_{}.fits'.format(name), stamp) return stamp
def extractStamp(submap, ra_deg, dec_deg, r_ring_arcmin, repixelize=True, reprojection=True): extractedSubmap = submap.copy() if repixelize: extractedSubmap = enmap.resample(extractedSubmap, 10 * np.array(submap.shape)) if reprojection: extractedSubmap = reproject.postage_stamp(extractedSubmap, ra_deg, dec_deg, 2 * r_ring_arcmin, 0.5 / 5.)[0, :, :] else: # this was added for backwards compatibility assert len(extractedSubmap.shape) == 2 extractedSubmap = submapTools.getSubmap_originalPixelization( theMap=extractedSubmap, # noqa ra_deg=ra_deg, dec_deg=dec_deg, # noqa semiWidth_deg=r_ring_arcmin / 60.) # noqa return extractedSubmap
def get_aperturePhotometry(submap, ra_deg, dec_deg, r_ring_arcmin, r_disk_arcmin, repixelize=True, reprojection=True): '''Gets T_disk and T_ring for a given submap. Needs ra and dec of the center of the submap and the radius of the ring and disk for the aperture. Can repixelize by a factor of 10 finer resolution and reproject. submap: submap in the original pixelization ra, dec_deg: ra and dec in degrees of the center of the aperture r_ring/disk_arcmin: radius for the aperture photometry repixelize: True if you want to do it as enmap.resample by a factor of 10 reprojection: True if you want to extract the postage stamp by reprojection at the equator. submapsFilename: None if you dont want to save submaps. String with a hdf5 filename if you want to save the submaps.''' submapForPhotometry = submap.copy() if repixelize: submapForPhotometry = enmap.resample(submapForPhotometry, 10 * np.array(submap.shape)) if reprojection: try: submapForPhotometry = reproject.postage_stamp( submapForPhotometry, ra_deg, dec_deg, 3.0 * r_ring_arcmin, 0.5 / 10.)[0, :, :] except: # noqa T_disk, T_ring = np.nan, np.nan return 0, 0 r_arcmin = 60. * np.rad2deg(submapForPhotometry.modrmap()) sel_disk = r_arcmin < r_disk_arcmin sel_ring = np.logical_and(r_arcmin > r_disk_arcmin, r_arcmin < r_ring_arcmin) T_disk = submapForPhotometry[sel_disk].mean() T_ring = submapForPhotometry[sel_ring].mean() return T_disk, T_ring
def s18dStack(ras, decs, data, width=20., weight=False): stack = 0 divisor = 0 num = 0 for i in range(len(ras)): path = '/scratch/r/rbond/jorlo/S18d_202006/filteredMaps/' tileName = tileFinder(ras[i], decs[i], data) #print(ras[i], decs[i]) #print(tileName) if tileName is None: continue tile = enmap.read_map(path + tileName + '/Arnaud_M2e14_z0p4#' + tileName + '_filteredMap.fits') stamp = reproject.postage_stamp(tile, ras[i], decs[i], width, 0.5) if weight: rms = fits.open( '/scratch/r/rbond/jorlo/S18d_202006/selFn/RMSMap_Arnaud_M2e14_z0p4.fits' ) #print(tileName) for j in range(1, 500): if rms[j].header['EXTNAME'] == tileName: break #print(j) rmsmap = rms[j].data #print(ras[i], decs[i]) coords = np.deg2rad(np.array((decs[i], ras[i]))) ypix, xpix = enmap.sky2pix(tile.shape, tile.wcs, coords) weight = rmsmap[int(ypix), int(xpix)]**2 #print(rmsmap[int(ypix),int(xpix)]) if weight < 10**-20: continue try: stack += stamp[0] / weight except: print(tileName) print(tile) print(stamp) num += 1 divisor += 1 / weight else: try: stack += stamp[0] except: print(tileName) print(tile) print(stamp) divisor += 1 num += 1 try: stack /= divisor except: print("Error: no items in stack") return None, None print("Number in stack: {}".format(num)) return stack, num
def do(ymap, cmap, mask, ras, decs, wt): combined = list(zip(ras, decs)) random.shuffle(combined) ras[:], decs[:] = zip(*combined) njobs = len(ras) comm, rank, my_tasks = mpi.distribute(njobs) print("Rank %d starting" % rank) s = stats.Stats(comm) i = 0 for task in my_tasks: ra = ras[task] dec = decs[task] # mcut = reproject.cutout(mask, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) mcut = reproject.postage_stamp(mask, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) if mcut is None: continue if np.any(mcut) <= 0: continue # ycut = reproject.cutout(ymap, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) # ccut = reproject.cutout(cmap, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) # wcut = reproject.cutout(wt, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) ycut = reproject.postage_stamp(ymap, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) ccut = reproject.postage_stamp(cmap, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) wcut = reproject.postage_stamp(wt, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) weight = wcut.mean() # if i==0: # modrmap = np.rad2deg(ycut.modrmap())*60. # bin_edges = np.arange(0.,15.,1.0) # binner = stats.bin2D(modrmap,bin_edges) # cents,y1d = binner.bin(ycut) # cents,c1d = binner.bin(ccut) # s.add_to_stats("c1d",c1d*1e6) # s.add_to_stats("y1d",y1d*1e6) s.add_to_stack("cstack", ccut * 1e6 * weight) s.add_to_stack("ystack", ycut * 1e6 * weight) s.add_to_stats("sum", (weight, )) i = i + 1 if i % 10 == 0 and rank == 0: print(i) print("Rank %d done " % rank) s.get_stats() s.get_stacks() if rank == 0: N = s.vectors['sum'].sum() ystack = s.stacks['ystack'] * N cstack = s.stacks['cstack'] * N _, nwcs = enmap.geometry(pos=(0, 0), shape=ystack.shape, res=np.deg2rad(0.5 / 60.)) return rank, enmap.enmap(ystack, nwcs), enmap.enmap(cstack, nwcs), N else: return rank, None, None, None
ra_temp, dec_temp = ra[flag], dec[flag] if boot: cur_boot = [] for k in range(40): print(freq, j, k, end='\r') flags = np.random.randint(len(ra_temp), size=len(ra_temp)) ra_temp2, dec_temp2 = ra_temp[flags], dec_temp[flags] stack = 0 divisor = 0 for l in range(len(ra_temp)): stamp = reproject.postage_stamp(cur_map, ra_temp2[l], dec_temp2[l], 20., 0.5) if stamp is None: continue if 0 in stamp[0][19:21, 19:21]: continue stack += stamp[0] divisor += 1 stack /= divisor cur_boot.append(np.mean(stack[19:21, 19:21])) cur_dict['boots'].append(cur_boot) cur_dict['central_t'].append(np.mean(cur_boot)) cur_dict['map_var'].append(np.std(cur_boot)) else: stack = 0 divisor = 0