def match_galfit_catalog(self): for b in self.galfit_bands: broot = self.root + '_%s' % b os.chdir(broot) c1 = sextractor('run%d.cat' % self.n) # SExtractor catalog c2 = sextractor('gfit%d_s.cat' % self.n) # GALFIT catalog ncolumns1 = len(c1._colnames) ncolumns2 = len(c2._colnames) ncolumns_tot = ncolumns1 + ncolumns2 header = "" for i in range(ncolumns1): header += "# %d %s\n" % ((i + 1), c1._colnames[i].upper()) for i in range(ncolumns2): header += "# %d %s\n" % ( (i + ncolumns1 + 1), c2._colnames[i].upper()) f = open('gfit%d_s_matched.cat' % self.n, 'wb') f.write(header) for i in range(len(c1)): j = np.arange(len(c2))[c2.objid == c1.number[i]][0] f.write(' '.join(c1._colentries[i])) f.write(' ') f.write(' '.join(c2._colentries[j])) f.write(' ') f.write('\n') print "Made matched catalog in %s for iteration %d." % (b, self.n) f.close() os.chdir('../')
def collect_fluxerr(band, root='skyrms', apercol='aper_1', fit_sky=False, ra_center=-99, dec_center=-99, radius=20): """ Collect the SExtractor flux error and Gaussian-fitted flux error, within the same aperture Optionally, the function can return just the fluxes around a given coordinate within some given radius (in arcsec). By default, return fluxes across the entire image. """ allruns = glob.glob('%s_%s/run*.cat' % (root, band)) SEx_flux_aper = np.zeros(0) SEx_fluxerr_aper = np.zeros(0) sky_fluxerr_aper = np.zeros(0) if os.path.exists('%s_%s/%s_%s_output.cat' % (root, band, root, band)): catalog = '%s_%s/%s_%s_output.cat' % (root, band, root, band) print "Reading %s..." % catalog c = sextractor(catalog) within = np.ones(len(c), 'bool') if (ra_center > -99) and (dec_center > -99): angdist = angsep.angsep(c.alpha_j2000, c.delta_j2000, ra_center, dec_center) within = ((angdist * 3600.) <= radius) SEx_flux_aper = getattr(c, 'flux_%s' % apercol) SEx_fluxerr_aper = getattr(c, 'fluxerr_%s' % apercol) SEx_flux_aper = SEx_flux_aper[(c.imaflags_iso == 0) & within] SEx_fluxerr_aper = SEx_fluxerr_aper[(c.imaflags_iso == 0) & within] else: for r in allruns: c = sextractor(r) if (ra_center > -99) and (dec_center > -99): angdist = angsep.angsep(c.alpha_j2000, c.delta_j2000, ra_center, dec_center) within = ((angdist * 3600.) <= radius) else: within = np.ones(len(c), 'bool') flux_aper_col = getattr(c, 'flux_%s' % apercol)[(c.imaflags_iso == 0) & within] fluxerr_aper_col = getattr(c, 'fluxerr_%s' % apercol)[(c.imaflags_iso == 0) & within] within = np.ones(len(c), 'bool') SEx_flux_aper = np.concatenate([SEx_flux_aper, flux_aper_col]) SEx_fluxerr_aper = np.concatenate( [SEx_fluxerr_aper, fluxerr_aper_col]) if fit_sky: sky_fluxerr_aper = np.concatenate([ sky_fluxerr_aper, c.sky_sigma_aper[(c.imaflags_iso == 0) & within] ]) if fit_sky: return SEx_flux_aper, SEx_fluxerr_aper, sky_fluxerr_aper else: return SEx_flux_aper, SEx_fluxerr_aper
def collect_results(self): # First get the MAG_ISO from the high-res catalog ch = sextractor(os.path.join(self.hiresdir, self.hires_cat)) mag_iso = {} magerr_iso = {} f = open( '%s_%s_tfit_%s.cat' % (self.lores_band, self.cluster_name, self.sample), 'wb') f.write('# 1 OBJNAME\n') f.write('# 2 RA\n') f.write('# 3 DEC\n') f.write('# 4 %s_MAG\n' % self.lores_band) f.write('# 5 %s_MAG_ERR\n' % self.lores_band) f.write('# 6 %s_MED_BKGD\n' % self.lores_band) f.write('# 7 %s_SIG_BKGD\n' % self.lores_band) f.write('# 8 %s_AVG_RESID\n' % self.lores_band) f.write('# 9 %s_MED_RESID\n' % self.lores_band) f.write('# 10 %s_CHI2NU\n' % self.lores_band) f.write('# 11 %s_MAG_ISO\n' % self.hires_band) f.write('# 12 %s_MAGERR_ISO\n' % self.hires_band) f.write('# 13 %s_FLUX\n' % self.lores_band) f.write('# 14 %s_FLUXERR\n' % self.lores_band) for obj_name in self.objnames: j = np.arange(len(self.ra))[self.objnames == obj_name][0] objid = self.objectid[j] mag_iso[obj_name] = ch.mag_iso[ch.number == objid][0] magerr_iso[obj_name] = ch.magerr_iso[ch.number == objid][0] # outcat = os.path.splitext(self.fitcat)[0] + '_%s.cat_best_mag' % obj_name outcat = "%s_%s_tphot_pass2_%s.cat_best_mag" % ( self.lores_band.lower(), self.cluster_name, obj_name) c = sextractor('%s/%s' % (obj_name, outcat)) mag = c.mag[c.objectid == objid][0] mag_err = c.magerr[c.objectid == objid][0] flux = c.fitqty[c.objectid == objid][0] fluxerr = c.fitquerr[c.objectid == objid][0] resid2 = '%s/resid_%s_%s_collage_tphot_pass2_%s.fits' % ( obj_name, self.lores_band, self.cluster_name, obj_name) hdr = pyfits.getheader(resid2) medbkgd = hdr['medbkgd'] sigbkgd = hdr['sigbkgd'] avgresid = hdr['avgresid'] medresid = hdr['medresid'] chi2nu = hdr['chi2nu'] f.write('%-15s %f %f ' % (obj_name, self.ra[j], self.dec[j])) f.write('%.3f %.3f ' % (mag, mag_err)) f.write('%f %f ' % (medbkgd, sigbkgd)) f.write('%f %f ' % (avgresid, medresid)) f.write('%f ' % chi2nu) f.write('%f %f ' % (mag_iso[obj_name], magerr_iso[obj_name])) f.write('%f %f ' % (flux, fluxerr)) f.write('\n') f.close()
def fit_sky(self, band, niter, skyaper=0.4, width=64, plot=False): # Fit the local sky fluctuation... c = sextractor('%s_%s/run%d.cat' % (self.root, band, niter)) print "number of regions to fit: ", len(c) if len(c) > 0: sky_sigma = np.zeros(len(c)) sky_sigma_aper = np.zeros(len(c)) # update segmentation map for sky fitting h = pyfits.open(self.segimages[band], mode='update') seg = h[0].data flg = pyfits.getdata(self.flagimages[band]) h[0].data = np.where((seg == 0) & (flg > 0), seg.max() + 1, seg) h.flush() h.close() for i in range(len(c)): skyest = SkyEstimate.SkyEstimate(self.realimages[band], self.segimages[band], band) bkgd, sigma = skyest.skyest_xy(c.x_image[i], c.y_image[i], width=width, plot=plot) sky_sigma[i] = sigma sky_sigma_aper[i] = skyest.calc_aperture_noise(skyaper) c.addcolumn('sky_sigma', sky_sigma, '%f') c.addcolumn('sky_sigma_aper', sky_sigma_aper, '%f') c.writeto(c._fname, clobber=True)
def find_matches(self): """ Given the list of RA and DEC, find the ID numbers for the corresponding objects in the high-res catalog. """ os.chdir(self.hiresdir) hc = sextractor(self.hires_cat) self.objid = np.zeros(len(self.ra), 'int') self.match_dist = np.zeros(len(self.ra)) # in arcsec self.x_hires = np.zeros(len(self.ra)) self.y_hires = np.zeros(len(self.ra)) self.x_lores = np.zeros(len(self.ra)) self.y_lores = np.zeros(len(self.ra)) self.xmin_hr = np.zeros(len(self.ra)) self.xmax_hr = np.zeros(len(self.ra)) self.ymin_hr = np.zeros(len(self.ra)) self.ymax_hr = np.zeros(len(self.ra)) self.xmin_bkgd = np.zeros(len(self.ra)) self.xmax_bkgd = np.zeros(len(self.ra)) self.ymin_bkgd = np.zeros(len(self.ra)) self.ymax_bkgd = np.zeros(len(self.ra)) for i in range(len(self.ra)): angdist = angsep.angsep(self.ra[i], self.dec[i], hc.alpha_j2000, hc.delta_j2000) index_min = np.argsort(angdist)[0] self.objid[i] = hc.number[index_min] self.match_dist[i] = np.min(angdist) * 3600. self.x_hires[i] = hc.x_image[index_min] self.y_hires[i] = hc.y_image[index_min] # Now determine the pixel coordinates in the low-res image # The pixel coordinates here are 1-based lores_xy = self.lr_wcs.wcs_sky2pix([[self.ra[i], self.dec[i]]], 1)[0] self.x_lores[i] = lores_xy[0] self.y_lores[i] = lores_xy[1]
def __call__(self, simcat, field, imaflags_thresh=4, bstonlim=5.0): # use simcat to make differential K-correction kernels in each M1500 bin c = sextractor(simcat) # do dropout selection if field == 'goods': zmagcut = 26.5 elif field == 'udf': zmagcut = 28.5 if self.drop == 'b': dropcrit, bmags, vmags, zmags = dss.bdrops_sel_sim(c, field=field,\ imaflags_thresh=imaflags_thresh) if self.drop == 'v': dropcrit, vmags, imags, zmags = dss.vdrops_sel_sim(c, field=field,\ imaflags_thresh=imaflags_thresh, bstonlim=bstonlim) # iterates through each M1500 bin for i in range(len(self.Mlolims)): for j in range(len(self.logRlolims)): Mbincrit = ((c.m1500_input >= self.Mlolims[i]) & (c.m1500_input < self.Mhilims[i])) logrbincrit = ((N.log10(c.re_input) >= self.logRlolims[j]) & (N.log10(c.re_input) <\ self.logRhilims[j])) # calls makedkcorr() to make kernels completeness, p_dk, n_ztarget, n_zselect, zarr = makedkcorr(c, Mbincrit,\ logrbincrit,dropcrit, self.dmz0, self.dmz1, self.ddmz, self.drop, z0=self.z0,\ z1=self.z1) #Fz, Cz, zarray, nin_array = makedkcorr2(c, Mbincrit, dropcrit, self.dmzarray,\ # self.zarray, self.z0, self.z1) #print i, self.Mlolims[i], self.Mhilims[i], max(Cz) #k = dkcorr(self.Mlolims[i], self.Mhilims[i], self.dmzarray, Fz, Cz, nin_array) k = dkcorr(self.Mlolims[i], self.Mhilims[i], self.logRlolims[j],\ self.logRhilims[j], completeness, p_dk, n_ztarget, n_zselect, zarr) self.dkcorr[(i, j)] = k
def read_positions(self): """ Read image coordinates for each object. """ os.chdir(self.hiresdir) hc = sextractor(self.hires_cat) self.x_hires = np.zeros(len(self.ra)) self.y_hires = np.zeros(len(self.ra)) self.x_lores = np.zeros(len(self.ra)) self.y_lores = np.zeros(len(self.ra)) self.xmin_hr = np.zeros(len(self.ra)) self.xmax_hr = np.zeros(len(self.ra)) self.ymin_hr = np.zeros(len(self.ra)) self.ymax_hr = np.zeros(len(self.ra)) self.xmin_bkgd = np.zeros(len(self.ra)) self.xmax_bkgd = np.zeros(len(self.ra)) self.ymin_bkgd = np.zeros(len(self.ra)) self.ymax_bkgd = np.zeros(len(self.ra)) for i in range(len(self.ra)): objid = self.objectid[i] self.x_hires[i] = hc.x_image[hc.number == objid][0] self.y_hires[i] = hc.y_image[hc.number == objid][0] lores_xy = self.lr_wcs.wcs_sky2pix([[self.ra[i], self.dec[i]]], 1)[0] self.x_lores[i] = lores_xy[0] self.y_lores[i] = lores_xy[1]
def sex2fits(self, f): # Convert SExtractor catalogs into FITS tables print "Convert SExtractor catalogs into FITS tables..." if os.path.exists(self.sex_fitstable[f]): os.remove(self.sex_fitstable[f]) c = sextractor(self.sex_catalog[f]) fitsutil.sex2fits(c, self.sex_fitstable[f])
def add_crashes(outputfile, inputdir): """ Add entries for objects that GALFIT crashed on. Look for GALFIT input files with file names obj[xxx], where [xxx] is the object ID. Use default non-measurement values for these objects that GALFIT failed. """ inputfiles = glob.glob(os.path.join(inputdir, 'obj*[0-9]')) # Assume that the GALFIT input files have file names obj[ID] c0 = sextractor(outputfile) newlines = [] for f in inputfiles: f = os.path.split(f)[-1] objid = int(f.split('_')[0][3:]) fout = 'obj%d_out.fits' % objid if fout not in c0.filename: l = ['obj%d_crashed' % objid, -1., -1., -1., -1., 99.0, 99.0, -1., -1., -1., -1., -1., -1., 999.0, 999.0, -1., 1, 1, 1, 1, 1, 1, 1, 0] newlines += [' '.join(str(x) for x in l) + '\n'] f = open(outputfile) oldlines = f.readlines() f.close() f = open(outputfile, 'wb') lines = oldlines + newlines for i in range(len(lines)): f.write(lines[i]) f.close()
def combine_catalog(self): # combine the cold and hot mode catalogs. for b in self.filters: chot = sextractor(self.hotpipe.sex_catalog[b]) ccold = sextractor(self.coldpipe.sex_catalog[b]) output_name = self.hotpipe.sex_catalog[b].replace('hot', 'warm') f = open(output_name, 'wb') f.write(ccold._header) # include ALL objects from cold catalog for i in range(len(ccold)): f.write(ccold.line(i)) # now incldue objects from hot catalog that are added by hot_addition for j in range(len(chot)): if (chot.number[j] + self.hotstart) in self.objectid: f.write(chot.line(j)) f.close() print "Hot-cold catalog in %s complete." % b.upper()
def calcmag(outcat, filters=default_filters, normmag=10.0, normband='f606w'): """ Reads the spectra for all *.dat and compute the magnitudes in the filters supplied by the dictionary filters. All magnitudes are in AB mag, and AB mag is normalized to normmag (default is 10) mag in V band (f606w). """ datafiles = glob.glob( '/Users/khuang/Dropbox/research/stellar_library/dat/uk*.dat') # only use spectra with coverage extending to mid-IR (UVK library) mags = {} bands = filters.keys() for i in range(len(bands)): # make sure the first band is normband if bands[i] == normband: bands[i] = bands[0] bands[0] = normband break spectype_list = [] for df in datafiles: spectype = df.split('/')[-1] spectype = spectype.split('.')[0] # read the spectral type name mags[spectype] = N.zeros(len(bands), 'float') spectype_list += [spectype] print spectype # read the data file c = sextractor(df) wave = c._1 flux = c._2 sp = S.ArraySpectrum(wave, flux, 'angstrom', 'flam') # initialize a pysynphot spectrum for i in range(len(bands)): # now iterate through all bands b = bands[i] m = ABmag(sp, filters[b]) if b == normband: dmag = normmag - m mags[spectype][i] = normmag # set f606w magnitude to normmag else: mags[spectype][ i] = m + dmag # normalize the magnitudes in other bands # now write to an output catalog... use a SExtractor table header = "" header += "# 1 SPECTYPE\n" ni = 1 for i in range(len(bands)): header += "# %d %s_mag\n" % ((ni + i + 1), bands[i]) # now write the output f = open(outcat, 'w') f.write(header) for i in range(len(spectype_list)): f.write('%10s ' % spectype_list[i]) sptype = spectype_list[i] for j in range(len(bands)): f.write('%10.2f ' % mags[sptype][j]) f.write('\n') f.close()
def use_catalog(self, catalog, objid='id', ra='ra', dec='dec', format='fits'): if format.lower() == 'fits': self.c = Ftable(catalog) self.Nc = len(self.c.d) else: self.c = sextractor(catalog) self.Nc = len(self.c) self.objid = getattr(self.c, objid) self.ra = getattr(self.c, ra) self.dec = getattr(self.c, dec)
def merge_runs(self, band, refcat='run0.cat'): curdir = os.getcwd() os.chdir('%s_%s' % (self.root, band)) cr = sextractor(refcat) columns = cr._colnames allcats = glob.glob('run*.cat') for ac in allcats: if ac == refcat: continue else: ca = sextractor(ac) for col in columns: assert hasattr( ca, col), "Column %s does not exist in %s..." % (col, ac) newcol = np.concatenate( [getattr(cr, col), getattr(ca, col)]) setattr(cr, col, newcol) cr.writeto('%s_%s_output.cat' % (self.root, band), clobber=True) os.chdir(curdir)
def __init__(self, catalog): """ Set the attributes of the class. """ c = sextractor(catalog) bands = [] for col in c._colnames: setattr(self, col, c.__getattribute__(col)) b = col[:-4] # get the name of the band bands += [b] self.bands = bands
def get_maxcvratio(self, obj_name): if type(obj_name) == type(''): objid = self.objectid[self.objnames == obj_name][0] else: objid = obj_name obj_name = self.objnames[self.objectid == objid][0] curdir = os.getcwd() os.chdir(os.path.join(self.loresdir, 'tphot', obj_name)) c = sextractor( os.path.splitext(self.fitcat)[0] + '_%s.cat_best_mag' % obj_name) os.chdir(curdir) return c.maxcvratio[c.objectid == objid][0]
def get_xy_input(self, numbers): # get the (x, y) coordinates for a list of objects that will be used later # for geomap N = len(numbers) # self.x_input, self.y_input should be pais of (x, y) coordinates # they should match self.x_ref, self.y_ref defined later. c = sextractor(self.input_cat) self.x_input = np.zeros(N) self.y_input = np.zeros(N) for i in range(len(numbers)): self.x_input[i] = c.x_image[c.number == numbers[i]][0] self.y_input[i] = c.y_image[c.number == numbers[i]][0]
def readMCResults(self, objid): assert os.path.exists( 'MonteCarlo'), "The directory MonteCarlo does not exist." c = sextractor('MonteCarlo/MCOutput_OBJ%d.txt' % objid) self.MCresults = {} self.MCresults['photz'] = c._2 self.MCresults['chi2'] = c._3 self.MCresults['log_age'] = np.log10(c._4) self.MCresults['ebmv'] = c._5 self.MCresults['log_mass'] = c._6 self.MCresults['log_sfr'] = c._7 self.MCresults['log_ssfr'] = c._7 - c._6
def hot_addition(self, kron_factor=3.0): # search in the hot-mode catalog and select objects that are at least # kron_factor*kron_radius away from any cold-mode object # The objects added from hot mode will be their ID in hot mode + hotstart b0 = self.hotpipe.detectband chot = sextractor(self.hotpipe.sex_catalog[b0]) ccold = sextractor(self.coldpipe.sex_catalog[b0]) self.objectid = np.concatenate([self.objectid, ccold.number]) hot_id = [] for i in range(len(chot)): x = chot.x_image[i] y = chot.y_image[i] # calculate distances to all cold-mode objects dist = np.sqrt((x - ccold.x_image)**2 + (y - ccold.y_image)**2) min_cold_index = np.argsort(dist)[0] # the index in cold catalog of the nearest neighbor mindist = dist.min() if mindist > (kron_factor * ccold.kron_radius[min_cold_index]): hot_id += [chot.number[i]] print "Number of hot additions: ", len(hot_id) new_hot_id = np.array(hot_id) + self.hotstart self.objectid = np.concatenate([self.objectid, new_hot_id])
def run_alignment(self, reflist, database, suffix='geo'): # Run all alignment steps AFTER running SExtractor and identified a list # of bright objects to calculate shifts. print "If you have not run SExtractor on both the input and the reference" print "image... do it now and identify a list of objects to calculate the" print "shifts. Then write the segmentation numbers of these objects to " print "the file as the reflist argument." r = sextractor(reflist) self.get_xy_input(r.segnum_in) self.get_xy_ref(r.segnum_ref) self.write_xy_map(self.coofile) self.geomap(database) self.apply_shift(database, suffix=suffix)
def plotTempIRACColors(templates=localTemplates): fig = plt.figure(figsize=(10,7)) ax1 = fig.add_subplot(1,2,1) # i - IRAC1 ax2 = fig.add_subplot(1,2,2) # i - IRAC2 for spec in templates: filename = '%s_irac12.mag' % spec c = sextractor(filename) ax1.plot(c.z, c.isubaru - c.irac1, label=spec) ax2.plot(c.z, c.isubaru - c.irac2, label=spec) for ax in [ax1, ax2]: ax.set_xlabel('Redshift') ax.legend(loc=2) ax.set_ylim(-1.,6.) ax1.set_ylabel(r'$i - [3.6]$') ax2.set_ylabel(r'$i - [4.5]$')
def add_header_prefix(catalog, prefix): # Add a prefix (e.g., filter name) to all columns in the catalog c = sextractor(catalog) nobj = len(c) colnames = c._colnames for i in range(len(colnames)): colnames[i] = '_'.join([prefix, colnames[i]]) os.system('cp %s %s.copy' % (catalog, catalog)) f = open(catalog, 'wb') for i in range(len(colnames)): f.write('# %d %s\n' % (i+1, colnames[i].upper())) for i in range(nobj): for j in range(len(c._d)): f.write('%s ' % c._colentries[i][j]) f.write('\n') f.close()
def step2(): # now re-format this and make object IDs as the first column c = sextractor('udrops_goodss_h13_130305_galfit.cat') objectid = N.zeros(len(c),'int') for i in range(len(c)): root = os.path.splitext(c.filename[i])[0] # in the format of objXXXX, where XXXX is the object ID objectid[i] = int(root[3:-4]) # the object ID header = c._header.replace('FILENAME','OBJECTID') # Now write to the catalog f = open('udrops_goodss_h13_130305_galfit2.cat','wb') f.write(header) for i in range(len(c)): f.write('%8d ' % objectid[i]) for j in range(1,len(c._colnames)): f.write('%s ' % c._colentries[i][j]) f.write('\n') f.close()
def __init__(self, catalog, image, band, width, norm=True, save=False): """ The catalog (in SExtractor format) needs to have the following three columns: NUMBER RA DEC so that we can figure out where the sources are. filter: the name of the filter width: the width of the cutouts (in arcsec) """ cutouts.__init__(self, [image], [band]) self.width = width self.c = sextractor(catalog) self.band = band self.norm = norm self.save = save print "There are a total of %d stars in the list." % len(self.c)
def plotTempColors(templates=localTemplates): fig = plt.figure(figsize=(12,10)) ax1 = fig.add_subplot(2,2,1) # i - J ax2 = fig.add_subplot(2,2,2) # i - H ax3 = fig.add_subplot(2,2,3) # i - K for spec in templates: filename = '%s_subaruiJHK.mag' % spec c = sextractor(filename) ax1.plot(c.z, c.isubaru - c.jsubaru, label=spec) ax2.plot(c.z, c.isubaru - c.hsubaru, label=spec) ax3.plot(c.z, c.isubaru - c.ksubaru, label=spec) for ax in [ax1, ax2, ax3]: ax.set_xlabel('Redshift') ax.legend(loc=2) ax.set_ylim(-1.,6.) ax1.set_ylabel(r'$i - J$') ax2.set_ylabel(r'$i - H$') ax3.set_ylabel(r'$i - K$')