def slicing_hdf5alhambra(infile, bpz): """ This routine generates subsample of P(z,T) applying a magnitude-based criteria. ------------------------------------------ import alhambrahdf5 from alhambrahdf5 import * slicing_hdf5alhambra(infile,bpz) """ ruta = '/Volumes/amb22/catalogos/reduction_v4f/globalPDZ/' basem = U.arange(19., 25.5, 0.5) ns = len(basem) mo, stflag = U.get_data(bpz, (64, 71)) for ii in range(ns - 1): cond = U.greater_equal(mo, basem[ii]) * U.less_equal(mo, basem[ii + 1]) cond *= U.less_equal(stflag, 0.8) infile2 = infile.split('/')[-1:][0] finalname = ruta + infile2 + '.%.1fm%.1f.mat' % (basem[ii], basem[ii + 1]) if not os.path.exists(finalname): print 'generating new file %s' % (finalname) mat = AH.alhambra_get2Dmatrix_HDF5(infile, cond, finalname, 1)
def runZPcal_catalogue(reference, frame, final): """ ---- filter_ref_cat,alig_frame_cat,alig_cal_frame_cat """ plots = 1 data2 = C.loaddata(frame) # Loading the whole catalog2 content. head2 = C.loadheader(frame) # Loading the original header2. pos_mags = 12 # ([12,20,21,22]) mag_r = U.get_data(reference, 12) mag_f = U.get_data(frame, 12) # good_sample = U.greater_equal(mag_r,16.) * U.less_equal(mag_r,21.5) good_sample = U.greater_equal(mag_r, 16.) * U.less_equal(mag_r, 19.) mag_r2, mag_f2 = U.multicompress(good_sample, (mag_r, mag_f)) offset = U.mean_robust(mag_f2 - mag_r2) if plots: plt.figure(11, figsize=(12, 9), dpi=80, facecolor='w', edgecolor='k') plt.clf() plt.plot(mag_r, (mag_f - mag_r - offset), 'ko', ms=10, alpha=0.1) plt.xlim(16, 25) plt.ylim(-5, 5.) plt.xlabel('AB', size=25) plt.ylabel('Mf-Mr', size=25) plt.xticks(fontsize=25) plt.yticks(fontsize=25) plt.legend(['Offset: %.4f' % (offset)], loc='upper right', numpoints=1) plt.title(A.getfilename(frame), size=15) plt.grid() figurename = final[:-3] + 'png' print 'figurename: ', figurename plt.savefig(figurename, dpi=100) plt.close() # Here it saves the offset in an ASCII file fileout = open(final[:-3] + 'txt', 'w') linea = '%s %.5f \n' % (final, offset) fileout.write(linea) fileout.close() # The offset is only applied to m!=99. magnitudes. new_mags = U.where(abs(mag_f) < 99, mag_f - offset, mag_f) data2[:, pos_mags] = new_mags C.savedata(data2, final, dir="", header=head2) print ' '
def match_spz_sample(cluster): # TO CHECK finalcat1 = catalog2[:-3]+'CLASH.redu.cat' finalcat2 = catalog2[:-3]+'nada.cat' # if not os.path.exists(finalcat1): if not os.path.exists(finalcat2): # print 'Final catalog does not exist yet.' if os.path.exists(catalog1) and os.path.exists(catalog2): # It matches up detections to its Spectroscopic Sample. # Reading specz catalog print 'Reading info1 before matching...' speczsample = catalog1 idsp,xsp,ysp = U.get_data(speczsample,(0,3,4)) goodsp = U.greater_equal(xsp,1500) * U.less_equal(xsp,3500) goodsp *= U.greater_equal(ysp,1500) * U.less_equal(ysp,3500) idsp,xsp,ysp = U.multicompress(goodsp,(idsp,xsp,ysp)) print 'New dimension for specz catalogue: ',len(xsp) # rasp,decsp,xsp,ysp,zsp = get_data(speczsample,(0,1,2,3,4)) # xsp,ysp,zsp = get_data(speczsample,(1,2,7)) ####### idsp = U.arange(len(xsp))+1 # idsp = arange(len(rasp))+1 # Reading ColorPro catalog print 'Reading info2 before matching...' idcol,xcol,ycol = U.get_data(catalog2,(0,3,4)) print 'Dimension for input catalogue before compressing: ',len(idcol) gsp = U.greater_equal(xcol,1500) * U.less_equal(xcol,3500) gsp *= U.greater_equal(ycol,1500) * U.less_equal(ycol,3500) idcol,xcol,ycol = U.multicompress(gsp,(idcol,xcol,ycol)) print 'Dimension for input catalogue after compressing: ',len(idcol) # Using "matching_vects" to match up samples... print 'Matching samples....' pepe = CT.matching_vects(idcol,xcol,ycol,idsp,xsp,ysp,1.1) # We use now X,Y instead RA,Dec # Compressing matches for ColorPro... print 'Compressing matches...' matchidcol = pepe[:,0].astype(int) gdet_col = U.greater(matchidcol,0) # Excluding 0's (non matched detections) matchidcol = U.compress(gdet_col,(matchidcol)) # Compressing matches for Spectroscopic... matchidsp = pepe[:,1].astype(int) gdet_spz = U.greater(matchidsp,0) # Excluding 0's (non matched detections) matchidsp = U.compress(gdet_spz,(matchidsp)) print 'len(idcol)',len(idcol) print 'len(idsp)',len(idsp) if len(matchidcol) == len(matchidsp): print 'Creating idredu & zsredu ' print 'Dimension of matchidsp ',len(matchidsp) idredu = U.zeros(len(matchidsp)) idspredu = U.zeros(len(matchidsp)) for ii in range(len(matchidsp)): colindex = A.id2pos(idcol,matchidcol[ii]) # Position for Index idcol spzindex = A.id2pos(idsp,matchidsp[ii]) # Position for Index idsp idredu[ii] = idcol[colindex] # ID for ColorPro idspredu[ii] = idsp[spzindex] # Specz for Specz # A new smaller catalog will be created containing specz info as an extra column. print 'Selecting by rows... ' finalcat1 = catalog2[:-3]+'UDF.redu.cat' finalcat2 = catalog2[:-3]+'CLASH.redu.cat' U.put_data(catalog2[:-3]+'idsfrommatch.txt',(idredu,idspredu)) A.select_rows_bylist_sorted(catalog1,idspredu,finalcat1) A.select_rows_bylist_sorted(catalog2,idredu,finalcat2)
def figura33(lista): """ I'm using the stellar classification from the version_e. ---- import alhambra_completeness as alhc lista = '/Volumes/amb22/catalogos/reduction_v4d/globalcats/lista.list' alhc.figura33(lista) """ blue = 0 red = 1 cats = U.get_str(lista, 0) cats2 = U.get_str(lista, 1) nc = len(cats) dx = 0.2 dy = 0.4 nxbins = 4 nybins = 2 ods = 0.05 mmin = 16.0 mmax = 23.75 zbmin = 0.0001 zbmax = 1.4 Mmin = -24 Mmax = -17 if red: Tbmin = 1 # 7. Tbmax = 5 # 11. resolmag = 0.2 # 0.2 resolz = 0.05 if blue: Tbmin = 7. Tbmax = 11. resolmag = 0.2 resolz = 0.05 resol = 0.025 areas = ([0.45, 0.47, 0.23, 0.24, 0.47, 0.47, 0.46, 2.79]) plt.figure(111, figsize=(21.5, 11.5), dpi=70, facecolor='w', edgecolor='k') ss = 0 for jj in range(nybins): for ii in range(nxbins): # Reading data from catalogs. mo, zb, tb, odds, m814 = U.get_data(cats[ss], (81, 72, 75, 76, 62)) sf = U.get_data(cats2[ss], 71) # mo,zb,tb,sf,odds,m814 = U.get_data(cats[ss],(81,72,75,71,76,62)) g = U.greater_equal(abs(m814), mmin) * U.less_equal( abs(m814), mmax) # g* = U.greater_equal(odds,ods) g *= U.greater_equal(tb, Tbmin) * U.less_equal(tb, Tbmax) g *= U.less_equal(sf, 0.8) yy = -0.014 * m814 + 0.38 g *= U.greater(odds, yy) g *= U.less_equal(mo, Mmax + resol) * U.greater(mo, Mmin - resol) g *= U.greater(zb, zbmin) * U.less_equal(zb, zbmax) mo, zb, tb, odds = U.multicompress(g, (mo, zb, tb, odds)) print 'dimension', len(mo) # Plotting density. # cuadrado = plt.axes([.1+(ii*dx),.1+((nybins-jj-1)*dy),dx,dy]) if ii == nxbins - 1: cuadrado = plt.axes([ .1 + (ii * dx), .1 + ((nybins - jj - 1) * dy), dx + (dx * 0.2), dy ]) else: cuadrado = plt.axes( [.1 + (ii * dx), .1 + ((nybins - jj - 1) * dy), dx, dy]) matrix, axis2, axis1 = rs.CC_numberdensity_contour_zvolume( zb, mo, resolz, resolmag, 1) if blue: plt.contourf(axis2, axis1, U.log10(matrix / areas[ss]), 250, vmin=-11., vmax=-7.) # blue galaxies if red: plt.contourf(axis2, axis1, U.log10(matrix / areas[ss]), 250, vmin=-12., vmax=-7.65) # red galaxies if ii == nxbins - 1: aa = plt.colorbar(pad=0., format='%.1f') aa.set_label('Log. Density [N/Mpc$^{3}$/deg$^{2}$]', size=18) if jj != nybins - 1: plt.setp(cuadrado, xticks=[]) if ii != 0: plt.setp(cuadrado, yticks=[]) if jj == nybins - 1: plt.xlabel('M$_{B}$', size=27) plt.xticks(fontsize=17) if ii == 0: plt.ylabel('redshift', size=28) plt.yticks(fontsize=17) # plotting axis manually base1 = U.arange(Mmin, Mmax + 1., 1.) base2 = U.arange(0, zbmax + (2. * resol), resol) dim1 = len(base1) dim2 = len(base2) for rr in range(dim1): plt.plot(base2 * 0. + base1[rr], base2, 'k--', linewidth=1., alpha=0.25) for rr in range(dim2): plt.plot(base1, base1 * 0. + base2[rr], 'k--', linewidth=1., alpha=0.25) # plt.grid() plt.ylim(zbmin + 0.0001, zbmax - 0.001) plt.xlim(Mmin + 0.0001, Mmax - 0.0001) if ss == 7: labelleg = 'Global' else: labelleg = 'A%i' % (ss + 2) xypos = (Mmax - 1.6, zbmax - 0.18) if ss == 7: xypos = (Mmax - 3.5, zbmax - 0.18) plt.annotate(labelleg, xy=xypos, fontsize=40, color='black') ss += 1 plt.savefig('completeness.alhambra.png', dpi=200)
def flagging_dobledetections(cat1,cat2): """ This serves to append an extra column (each to both inputted catalogs) indicating either a detection was repeated and with the lowest S/N of the two. Sources flagged as 1 are those detections to be excluded when combining both catalogs into a single one. -------- import alhambra_overlap as alhov cat1 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p02_colorproext_1_ISO.cat' cat2 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p01_colorproext_1_ISO.cat' alhov.flagging_dobledetections(cat1,cat2) """ id1,ra1,dec1,x1,y1,s2n1 = U.get_data(cat1,(0,1,2,3,4,14)) id2,ra2,dec2,x2,y2,s2n2 = U.get_data(cat2,(0,1,2,3,4,14)) ne1 = len(id1) ne2 = len(id2) g1 = U.greater_equal(ra1,min(ra2)) g2 = U.less_equal(ra2,max(ra1)) id1r,ra1r,dec1r,x1r,y1r,s2n1r = U.multicompress(g1,(id1,ra1,dec1,x1,y1,s2n1)) id2r,ra2r,dec2r,x2r,y2r,s2n2r = U.multicompress(g2,(id2,ra2,dec2,x2,y2,s2n2)) flag1 = U.zeros(ne1) flag2 = U.zeros(ne2) dim1 = len(id1r) dim2 = len(id2r) print 'dim1,dim2',dim1,dim2 if dim1>0 and dim2>0: print 'Matching samples....' pepe = matching_vects_ddet(id1r,ra1r,dec1r,id2r,ra2r,dec2r,0.000312) # We use now X,Y instead RA,Dec # Purging null elements matchidcol = pepe[:,0].astype(int) good_det1 = U.greater(matchidcol,0) # Excluding 0's (non matched detections) matchidcol = U.compress(good_det1,(matchidcol)) matchidsp = pepe[:,1].astype(int) good_det2 = U.greater(matchidsp,0) # Excluding 0's (non matched detections) matchidsp = U.compress(good_det2,(matchidsp)) if len(matchidcol) == len(matchidsp) and len(matchidcol) >0 : newdim = len(matchidsp) print 'Dimension of matching',newdim idr1 = U.zeros(newdim) idr2 = U.zeros(newdim) s2nr1 = U.zeros(newdim) s2nr2 = U.zeros(newdim) for ii in range(newdim): idr1index = ap.id2pos(id1r,matchidcol[ii]) idr2index = ap.id2pos(id2r,matchidsp[ii]) idr1[ii] = id1r[idr1index] s2nr1[ii] = s2n1r[idr1index] idr2[ii] = id2r[idr2index] s2nr2[ii] = s2n2r[idr2index] # Select/Purge detections according to its S/N marcador1 = U.zeros(newdim) marcador2 = U.zeros(newdim) for ss in range(newdim): cociente = s2nr1[ss]/s2nr2[ss] if cociente >= 1.: marcador1[ss] = 1. else: marcador2[ss] = 1. cond1 = U.less(marcador1,1) cond2 = U.less(marcador2,1) idr1b = U.compress(cond1,idr1) dim1rr = len(idr1b) idr2b = U.compress(cond2,idr2) dim2rr = len(idr2b) # Two new IDs (finalid1 & finalid2) are generated with # the final elements to be included in the output catalog. for hh1 in range(ne1): if id1[hh1] in idr1b: flag1[hh1] = 1 for hh2 in range(ne2): if id2[hh2] in idr2b: flag2[hh2] = 1 # A new smaller catalog will be created containing specz info as an extra column. outcat1 = ap.decapfile(cat1)+'.doubledetect.cat' outcat2 = ap.decapfile(cat2)+'.doubledetect.cat' print 'outcat1',outcat1 print 'outcat2',outcat2 ap.appendcol(cat1,flag1,'Flag2Detected',outcat1) ap.appendcol(cat2,flag2,'Flag2Detected',outcat2) # Renaming files ap.renamefile(cat1,cat1+'.old.cat') if not os.path.exists(cat1): ap.renamefile(outcat1,cat1) ap.renamefile(cat2,cat2+'.old.cat') if not os.path.exists(cat2): ap.renamefile(outcat2,cat2) else: print 'No common sources in betwen the catalogs' # A new smaller catalog will be created containing specz info as an extra column. outcat1 = ap.decapfile(cat1)+'.doubledetect.cat' outcat2 = ap.decapfile(cat2)+'.doubledetect.cat' print 'outcat1',outcat1 print 'outcat2',outcat2 ap.appendcol(cat1,flag1*0,'Flag2Detected',outcat1) ap.appendcol(cat2,flag2*0,'Flag2Detected',outcat2) # Renaming files ap.renamefile(cat1,cat1+'.old.cat') if not os.path.exists(cat1): ap.renamefile(outcat1,cat1) ap.renamefile(cat2,cat2+'.old.cat') if not os.path.exists(cat2): ap.renamefile(outcat2,cat2)
def purging_dobledetections(cat1,cat2): """ import alhambra_overlap from alhambra_overlap import * cat1 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p02_colorproext_1_ISO.cat' cat2 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p01_colorproext_1_ISO.cat' purging_dobledetections(cat1,cat2) """ id1,ra1,dec1,x1,y1,s2n1 = U.get_data(cat1,(0,1,2,3,4,14)) id2,ra2,dec2,x2,y2,s2n2 = U.get_data(cat2,(0,1,2,3,4,14)) ne1 = len(id1) ne2 = len(id2) g1 = U.greater_equal(ra1,min(ra2)) g2 = U.less_equal(ra2,max(ra1)) id1r,ra1r,dec1r,x1r,y1r,s2n1r = U.multicompress(g1,(id1,ra1,dec1,x1,y1,s2n1)) id2r,ra2r,dec2r,x2r,y2r,s2n2r = U.multicompress(g2,(id2,ra2,dec2,x2,y2,s2n2)) dim1 = len(id1r) dim2 = len(id2r) print 'dim1,dim2',dim1,dim2 if dim1>0 and dim2>0: print 'Matching samples....' pepe = matching_vects_ddet(id1r,ra1r,dec1r,id2r,ra2r,dec2r,0.000312) # We use now X,Y instead RA,Dec # Purging null elements matchidcol = pepe[:,0].astype(int) good_det1 = U.greater(matchidcol,0) # Excluding 0's (non matched detections) matchidcol = U.compress(good_det1,(matchidcol)) matchidsp = pepe[:,1].astype(int) good_det2 = U.greater(matchidsp,0) # Excluding 0's (non matched detections) matchidsp = U.compress(good_det2,(matchidsp)) if len(matchidcol) == len(matchidsp) and len(matchidcol) >0 : newdim = len(matchidsp) print 'Dimension of matching',newdim idr1 = U.zeros(newdim) idr2 = U.zeros(newdim) s2nr1 = U.zeros(newdim) s2nr2 = U.zeros(newdim) for ii in range(newdim): idr1index = ap.id2pos(id1r,matchidcol[ii]) idr2index = ap.id2pos(id2r,matchidsp[ii]) idr1[ii] = id1r[idr1index] s2nr1[ii] = s2n1r[idr1index] idr2[ii] = id2r[idr2index] s2nr2[ii] = s2n2r[idr2index] # Select/Purge detections according to its S/N marcador1 = U.zeros(newdim) marcador2 = U.zeros(newdim) for ss in range(newdim): cociente = s2nr1[ss]/s2nr2[ss] if cociente >= 1.: marcador1[ss] = 1. else: marcador2[ss] = 1. cond1 = U.less(marcador1,1) cond2 = U.less(marcador2,1) idr1b = U.compress(cond1,idr1) dim1rr = len(idr1b) idr2b = U.compress(cond2,idr2) dim2rr = len(idr2b) print '' print 'Number of detections to be removed from cat1: ', dim1rr print 'Number of detections to be removed from cat2: ', dim2rr print '' # Two new IDs (finalid1 & finalid2) are generated with # the final elements to be included in the output catalog. finalid1 = U.zeros((ne1-dim1rr)) finalid2 = U.zeros((ne2-dim2rr)) kk1 = 0 for hh1 in range(ne1): if id1[hh1] not in idr1b: finalid1[kk1] = id1[hh1] kk1 += 1 print 'kk1',kk1 kk2 = 0 for hh2 in range(ne2): if id2[hh2] not in idr2b: if kk2 <= (ne2-dim2rr-1): finalid2[kk2] = id2[hh2] kk2+=1 print 'kk2',kk2 # A new smaller catalog will be created containing specz info as an extra column. outcat1 = ap.decapfile(cat1)+'.wo2detect.cat' outcat2 = ap.decapfile(cat2)+'.wo2detect.cat' print 'outcat1',outcat1 print 'outcat2',outcat2 ap.select_rows_bylist(cat1,finalid1,outcat1) ap.select_rows_bylist(cat2,finalid2,outcat2) else: print 'No common sources in betwen the catalogs'
def getsample_4_alhambra_multiple_mosaic(pepe): """ import os,sys import useful as U import alhambra_photools as alh import alhambra_mosaics as alhmos import pyfits alhmos.getsample_4_alhambra_multiple_mosaic(4) """ manual = 0 pepa = open('mosaico.list', 'w') for ii in range(7): for jj in range(4): for kk in range(4): catalog = root_catalogs + 'f0%i/alhambra.f0%ip0%ic0%i.ColorProBPZ.Prior1Peak.cat' % ( ii + 2, ii + 2, jj + 1, kk + 1) colorimage = root_images + 'f0%i/color_images/f0%ip0%i_OPTICAL_%i.png' % ( ii + 2, ii + 2, jj + 1, kk + 1) if os.path.exists(catalog): ids = U.get_str(catalog, 0) xx, yy = U.get_data(catalog, (3, 4)) area, mm = U.get_data(catalog, (5, 62)) sf = U.get_data( '/Volumes/amb22/catalogos/reduction_v4e/f0%i/f0%ip0%i_colorproext_%i_ISO.cat' % (ii + 2, ii + 2, jj + 1, kk + 1), 71) g = U.greater_equal(area, 100) * U.less_equal( area, 500) * U.greater_equal(mm, 17) * U.less_equal( mm, 21) * U.less(sf, 0.2) # g = U.greater_equal(area,10000) * U.less_equal(mm,17) * U.less(sf,0.1) # g = U.less_equal(mm,23.) * U.greater_equal(mm,18) * U.greater_equal(sf,0.8) if manual != 1: for ss in range(len(ids)): if g[ss] == True: linea = '%s \n' % (ids[ss]) pepa.write(linea) else: for ss in range(len(ids)): if g[ss] == True: print xx[ss], yy[ss] print round(xx[ss]), round(yy[ss]) x = int(round(xx[ss])) y = int(round(yy[ss])) colorfile = colorimage im = Image.open(colorfile) imsize = nx, ny = im.size dx = 70 dy = 70 dxo = dyo = 0 stamp = im.crop( (x - dx - dxo, ny - y - dy + dyo, x + dx - dxo, ny - y + dy + dyo)) draw = ImageDraw.Draw(stamp) ressize = 300 stamp2 = stamp.resize((ressize, ressize), Image.NEAREST) stamp2.show() buena = raw_input( 'Include this source in the mosaic? [y/n]') if buena == 'y': linea = '%s \n' % (ids[ss]) pepa.write(linea) pepa.close()
# Reading data from catalogs. ft1, fob1, efob1 = P.getinfo4_mosaic_uncert_obsvsmod(columns, fluxcomparison) mag1 = U.get_data(fluxcomparison, 1) #emob = B.e_frac2mag(efob1[ii][:])/float(fob1[ii][:]) plt.figure(1, figsize=(24, 12), dpi=80, facecolor='w', edgecolor='k') plt.clf() bin_labels = [0, 6] base_x = N.linspace(-3.0, 3.0, 300) for ss in range(12): plt.subplot(2, 6, ss + 1) if ss == 0: g_1 = U.greater_equal(mag1, mmin) * U.less_equal(mag1, mmax + 3) else: g_1 = U.greater_equal(mag1, mmin) * U.less_equal(mag1, mmax) dm2_1 = B.flux2mag(fob1[ss][g_1]) - B.flux2mag(ft1[ss][g_1]) emob = B.e_frac2mag(efob1[ss][g_1]) / fob1[ss][g_1] sense_values_1 = N.less(abs(dm2_1), 5.) sense_values_1 *= N.less(abs(emob), 1.) dm2_1, emob2 = U.multicompress(sense_values_1, (dm2_1, emob)) #dm2_1 = N.compress(sense_values_1,dm2_1) valor = base_x * 0. for ii in range(len(emob2)): temporal = A.gaussian(base_x, emob2[ss], 0.00, 1) valor += temporal * temporal valor = N.sqrt(valor)