def find_point_sources(image, seeing_pix=4, threshold_sigma=3, size_lim=5, out_file=None, plot=False): """sourceInIm takes a direct image and find all sources above some detection threshold in it. Used by find_sources_in_direct_image Inputs: image: an array representing the direct image seeing_pix: seeing size in pixel threshold_sigma: detection threshold in sigmas away from the standard deviation in background fluctuation size_lim: the limit of stddev, in pixel, over which we don't accept the fit as a source out_file: If defined then a .reg file for ds9 is created in the XY format. """ threshold = threshold_sigma * np.sqrt(np.var(np.nan_to_num(image))) #get all cutouts cutouts = pointFinder(image, seeing_pix, threshold) #list to collect results all_results = [] #loop through cutouts, fit gaussian to get position of sources for i in cutouts: #make cutouts from indices, then run fit_gaussian_to_cutout if np.logical_and( len(np.ravel(image[i])) < 200, len(np.ravel(image[i])) > 10): #a legit cutout shouldn't be large if plot: plt.imshow(image[i], interpolation='none') res = fit_gaussian_to_cutout(image[i], seeing_pix) #get x y x = (i[1].start) + res[0].x_mean.value y = (i[0].start) + res[0].y_mean.value x_stddev = res[0].x_stddev.value y_stddev = res[0].y_stddev.value #filter out bad fits. stddev should be of some reasonable number if x_stddev < size_lim and y_stddev < size_lim: all_results += [(y, x, y_stddev, x_stddev)] else: None #print(i, ' is invalid.' ) #return cutouts if out_file != None: f = open(out_file, mode='w') for i in all_results: f.write(str(i[1] + 1) + ' ' + str(i[0] + 1) + '\n') f.close() return all_results
def putstar_me(gfluxmin, south=True): if south == True: fls = sfs else: fls = sfn f = fitsio.read(fls[0]) w0 = starsel_sweep(f, gfluxmin) fw = f[w0] #dt = fw.dtype dt = [] cols = ['BRICKID','OBJID','RA','DEC','DCHISQ','EBV','FLUX_G','FLUX_R','FLUX_Z','MW_TRANSMISSION_G','MW_TRANSMISSION_R','MW_TRANSMISSION_Z',\ 'PSFDEPTH_G','PSFDEPTH_R','PSFDEPTH_Z','GAIA_PHOT_G_MEAN_MAG','GAIA_ASTROMETRIC_EXCESS_NOISE','MASKBITS'] for colname in cols: dt.append((colname, fw.dtype[colname])) new = np.empty(len(fw), dtype=dt) for colname in cols: new[colname][...] = fw[colname][...] tm = new #for i in range(1,10): for i in range(1, len(fls)): #try: f = fitsio.read(fls[i]) w = starsel_sweep(f, gfluxmin) fw = f[w] new = np.empty(len(fw), dtype=dt) for colname in cols: new[colname][...] = fw[colname][...] tmn = np.concatenate((tm, new), axis=0) print(i, len(tmn)) tm = tmn #except: # print(i) NS = 'south' if south != True: NS = 'north' s = 0 es = '' while s == 0: outf = outdir + 'mysweeps/stars_gfluxg' + str( gfluxmin) + '_' + NS + es + '.fits' try: fitsio.read(outf) es += 'n' print(es) if len(es) > 10: return 'es too long, probably a bug' except: s = 1 #tm = Table(tm,names=fi.dtype.names) fits = fitsio.FITS(outf, 'rw') fits.write(tm, names=cols, overwrite=True)
def puttype(type, south=True, ebvfac=1., Rv=3.1): if south == True: fls = sfs else: fls = sfn f = fitsio.read(fls[0]) w0 = typesel(f, type, south=south, ebvfac=ebvfac, Rv=Rv) fw = f[w0] dt = fw.dtype new = np.empty(len(fw), dtype=dt) #cols = ['BRICKID','OBJID','RA','DEC','DCHISQ','EBV','FLUX_G','FLUX_R','FLUX_Z','MW_TRANSMISSION_G','MW_TRANSMISSION_R','MW_TRANSMISSION_Z',\ #'PSFDEPTH_G','PSFDEPTH_R','PSFDEPTH_Z','GAIA_PHOT_G_MEAN_MAG','GAIA_ASTROMETRIC_EXCESS_NOISE','MASKBITS'] #for colname in fw.dtype.names: cols = fw.dtype.names for colname in cols: new[colname][...] = fw[colname][...] tm = new for i in range(1, len(fls)): #try: f = fitsio.read(fls[i]) w = typesel(f, type, south=south, ebvfac=ebvfac, Rv=Rv) fw = f[w] new = np.empty(len(fw), dtype=dt) for colname in cols: new[colname][...] = fw[colname][...] tmn = np.concatenate((tm, new), axis=0) print(i, len(tmn)) tm = tmn #except: # print(i) NS = 'south' if south != True: NS = 'north' s = 0 es = 'ebvfac' + str(ebvfac) + 'Rv' + str(Rv) outf = outdir + 'mysweeps/' + type + 'dr8_' + NS + es + '.fits' # while s == 0: # # try: # fitsio.read(outf) # es += 'n' # print(es) # if len(es) > 10: # return 'es too long, probably a bug' # except: # s = 1 #tm = Table(tm,names=fi.dtype.names) fits = fitsio.FITS(outf, 'rw') fits.write(tm, names=dt.names, overwrite=True)
def fits(zl,pzl,zs,pzs,filename): try: os.remove(filename) print 'Removing file ',filename except OSError: pass fits=fio.FITS(filename,'rw') hdr={'extname':'NZ_POSITION','NZDATA':True} out=np.empty(np.shape(zl),dtype=[('Z_MID','f8')]) out['Z_MID']=zl fits.write(out,header=hdr) fits[-1].insert_column('Z_LOW',np.abs(zl-(zl[1]-zl[0])/2.)) fits[-1].insert_column('Z_HIGH',zl+(zl[1]-zl[0])/2.) if 'notomo' in filename: fits[-1].insert_column('BIN1',pzl) else: for i in range(len(pzl)): fits[-1].insert_column('BIN'+str(i+1),pzl[i,:]) hdr={'extname':'NZ_SHEAR','NZDATA':True} out=np.empty(np.shape(zs),dtype=[('Z_MID','f8')]) out['Z_MID']=zs fits.write(out,header=hdr) fits[-1].insert_column('Z_LOW',np.abs(zs-(zs[1]-zs[0])/2.)) fits[-1].insert_column('Z_HIGH',zs+(zs[1]-zs[0])/2.) if 'notomo' in filename: fits[-1].insert_column('BIN1',pzs) else: for i in range(len(pzs)): fits[-1].insert_column('BIN'+str(i+1),pzs[i,:]) fits.close() return
def sigma_l2fits( filename, nchains, burnin, path, outname, save=True, ): """ Converts c3-h5 dataset to fits for c1 BR and GBR estimator analysis. ex. c3pp sigma-l2fits chains_v1/chain 5 10 cmb_sigma_l_GBRlike.fits If "chain_c0001.h5", filename is cut to "chain" and will look in same directory for "chain_c*****.h5". See comm_like_tools for further information about BR and GBR post processing """ click.echo("{:-^48}".format("Formatting sigma_l data to fits file")) import h5py if filename.endswith(".h5"): filename = filename.rsplit("_", 1)[0] temp = np.zeros(nchains) for nc in range(1, nchains + 1): with h5py.File( filename + "_c" + str(nc).zfill(4) + ".h5", "r", ) as f: groups = list(f.keys()) temp[nc - 1] = len(groups) nsamples_max = int(max(temp[:])) click.echo( f"Largest chain has {nsamples_max} samples, using burnin {burnin}\n") for nc in range(1, nchains + 1): fn = filename + "_c" + str(nc).zfill(4) + ".h5" with h5py.File( fn, "r", ) as f: click.echo(f"Reading {fn}") groups = list(f.keys()) nsamples = len(groups) if nc == 1: dset = np.zeros(( nsamples_max + 1, 1, len(f[groups[0] + "/" + path]), len(f[groups[0] + "/" + path][0]), )) nspec = len(f[groups[0] + "/" + path]) lmax = len(f[groups[0] + "/" + path][0]) - 1 else: dset = np.append( dset, np.zeros(( nsamples_max + 1, 1, nspec, lmax + 1, )), axis=1, ) click.echo( f"Dataset: {path} \n# samples: {nsamples} \n# spectra: {nspec} \nlmax: {lmax}" ) for i in range(nsamples): for j in range(nspec): dset[i + 1, nc - 1, j, :] = np.asarray(f[groups[i] + "/" + path][j][:]) click.echo("") # Optimize with jit? ell = np.arange(lmax + 1) for nc in range(1, nchains + 1): for i in range(1, nsamples_max + 1): for j in range(nspec): dset[i, nc - 1, j, :] = dset[i, nc - 1, j, :] * ell[:] * (ell[:] + 1.0) / 2.0 / np.pi dset[0, :, :, :] = nsamples - burnin if save: click.echo(f"Dumping fits file: {outname}") dset = np.asarray(dset, dtype="f4") from astropy.io import fits head = fits.Header() head["FUNCNAME"] = ("Gibbs sampled power spectra", "Full function name") head["LMAX"] = (lmax, "Maximum multipole moment") head["NUMSAMP"] = (nsamples_max, "Number of samples") head["NUMCHAIN"] = (nchains, "Number of independent chains") head["NUMSPEC"] = (nspec, "Number of power spectra") fits.writeto(outname, dset, head, overwrite=True) # FITSIO Saving Deprecated (Use astropy) if False: import fitsio fits = fitsio.FITS( outname, mode="rw", clobber=True, verbose=True, ) h_dict = [ { "name": "FUNCNAME", "value": "Gibbs sampled power spectra", "comment": "Full function name", }, { "name": "LMAX", "value": lmax, "comment": "Maximum multipole moment", }, { "name": "NUMSAMP", "value": nsamples_max, "comment": "Number of samples", }, { "name": "NUMCHAIN", "value": nchains, "comment": "Number of independent chains", }, { "name": "NUMSPEC", "value": nspec, "comment": "Number of power spectra", }, ] fits.write( dset[:, :, :, :], header=h_dict, clobber=True, ) fits.close() return dset
if rank == 0: dt = [('g1p', 'f8'), ('g1m', 'f8'), ('g1', 'f8'), ('g2p', 'f8'), ('g2m', 'f8'), ('g2', 'f8')] dplus0 = np.array(pres0, dtype=dt) dminus0 = np.array(mres0, dtype=dt) dplus1 = np.array(pres1, dtype=dt) dminus1 = np.array(mres1, dtype=dt) dplus2 = np.array(pres2, dtype=dt) dminus2 = np.array(mres2, dtype=dt) dplus3 = np.array(pres3, dtype=dt) dminus3 = np.array(mres3, dtype=dt) dplus4 = np.array(pres4, dtype=dt) dminus4 = np.array(mres4, dtype=dt) with fitsio.FITS('data.fits', 'rw') as fits: fits.write(dplus0, extname='plus0') fits.write(dminus0, extname='minus0') fits.write(dplus1, extname='plus1') fits.write(dminus1, extname='minus1') fits.write(dplus2, extname='plus2') fits.write(dminus2, extname='minus2') fits.write(dplus3, extname='plus3') fits.write(dminus3, extname='minus3') fits.write(dplus4, extname='plus4') fits.write(dminus4, extname='minus4') m, msd, c, csd = estimate_m_and_c(pres0, mres0, 0.02, swap12=SWAP12) print("""\ # of sims: {n_sims} noise cancel m : {m:f} +/- {msd:f} noise cancel c : {c:f} +/- {csd:f}""".format(n_sims=len(pres0),