示例#1
0
def main():
	parser = argparse.ArgumentParser(prog='sdss2ps.py',
	                                 description='Transform template library and '
	                                             'luminosity function from SDSS '
	                                             'bandpasses to PanSTARRS bandpasses.',
	                                 add_help=True)
	parser.add_argument('SDSScolors', type=str, help="Mario's SDSS template library.")
	parser.add_argument('PScolors', type=str,
	                        help='Output filename for PanSTARRS template library.')
	parser.add_argument('SDSSlf', type=str, help="Mario's SDSS luminosity function.")
	parser.add_argument('PSlf', type=str,
	                        help='Output filename for PanSTARRS luminosity function.')
	parser.add_argument('--Delta-grizy', '-D', type=float, nargs=5, default=[0.,0.,0.,0.,0.],
	                        help='Offset to add to each PS1 band (in millimags)')
	if 'python' in sys.argv[0]:
		offset = 2
	else:
		offset = 1
	args = parser.parse_args(sys.argv[offset:])
	
	SDSSugriz, MrFeH = load_SDSS_templates(args.SDSScolors)
	PSgrizy = ps.pssdsstransformall(SDSSugriz)
	
	for i in xrange(5):
		PSgrizy[:,i] += 0.001 * args.Delta_grizy[i]
	
	save_PS_templates(args.PScolors, PSgrizy, MrFeH)
	
	SDSSlf = np.loadtxt(abspath(args.SDSSlf), usecols=(0,1), dtype='f4')
	save_PS_lf(args.PSlf, SDSSlf, MrFeH, PSgrizy[:,1])
	
	return 0
示例#2
0
def main():
	parser = argparse.ArgumentParser(prog='sdss2ps.py', description='Transform template library and luminosity function from SDSS bandpasses to PanSTARRS bandpasses.', add_help=True)
	parser.add_argument('SDSStemp', type=str, help="Mario's SDSS template library.")
	parser.add_argument('PStemp', type=str, help='Output filename for PanSTARRS template library.')
	parser.add_argument('SDSSlf', type=str, help="Mario's SDSS luminosity function.")
	parser.add_argument('PSlf', type=str, help='Output filename for PanSTARRS luminosity function.')
	if 'python' in sys.argv[0]:
		offset = 2
	else:
		offset = 1
	values = parser.parse_args(sys.argv[offset:])
	
	SDSSugriz, MrFeH = load_SDSS_templates(values.SDSStemp)
	PSgrizy = ps.pssdsstransformall(SDSSugriz)
	save_PS_templates(values.PStemp, PSgrizy, MrFeH)
	
	SDSSlf = np.loadtxt(abspath(values.SDSSlf), usecols=(0,1), dtype=np.float32)
	save_PS_lf(values.PSlf, SDSSlf, MrFeH, PSgrizy[:,1])
	
	return 0
def main():
	parser = argparse.ArgumentParser(
	           prog='s82_completeness.py',
	           description='Estimate PS1 completeness by comparison with deep SDSS Stripe 82 photometry.',
	           add_help=True)
	#parser.add_argument('out', type=str, help='Filename for query output.')
	parser.add_argument('-b', '--bounds', type=float, nargs=4, default=None,
	                    help='Restrict pixels to region enclosed by: RA_min, RA_max, Dec_min, Dec_max.')
	parser.add_argument('--n-bands', type=int, default=4,
	                    help='Min. # of PS1 passbands with detection.')
	parser.add_argument('--n-det', type=int, default=4,
	                    help='Min. # of PS1 detections.')
	parser.add_argument('-w', '--n-workers', type=int, default=5,
	                    help='# of workers for LSD to use.')
	if 'python' in sys.argv[0]:
		offset = 2
	else:
		offset = 1
	args = parser.parse_args(sys.argv[offset:])
	
	mplib.rc('text', usetex=True)
	
	n_pointlike = args.n_bands - 1
	if n_pointlike == 0:
		n_pointlike = 1
	
	# Determine the query bounds
	query_bounds = None
	
	if args.bounds != None:
		query_bounds = lsd.bounds.rectangle(args.bounds[0], args.bounds[2],
		                                    args.bounds[1], args.bounds[3],
		                                    coordsys='equ')
	
	query_bounds = lsd.bounds.make_canonical(query_bounds)
	
	# Set up the query
	db = lsd.DB(os.environ['LSD_DB'])
	
	query = ("select s82coadd.l as l, s82coadd.b as b, "
	                "s82coadd.ra as ra_s82, "
	                "s82coadd.dec as dec_s82, "
	                "s82coadd.psfcounts as s82_counts, "
	                "s82coadd.psfcountserr as s82_counts_err, "
	                "ucal_magsqx_noref.ra as ra_ps1, "
	                "ucal_magsqx_noref.dec as dec_ps1, "
	                "ucal_magsqx_noref.mean as ps1_mean, "
	                "ucal_magsqx_noref.err as ps1_err, "
	                "ucal_magsqx_noref.mean_ap as ps1_mean_ap, "
	                "ucal_magsqx_noref.maglimit as ps1_maglim, "
	                "ucal_magsqx_noref.nmag_ok as ps1_nmag_ok "
	         "from s82coadd, ucal_magsqx_noref(outer, matchedto=s82coadd, nmax=1, dmax=30) "
	         "where (s82coadd.objc_type == 6)")
	
	query = db.query(query)
	
	# Execute query
	rows = query.fetch(bounds=query_bounds, nworkers=args.n_workers)
	
	
	# Transform from luptitudes to AB magnitudes
	b_s82 = [1.0e-11, 0.43e-11, 0.81e-11, 1.4e-11, 3.7e-11]	# Stripe-82 ugriz softening parameters
	sdss_bands = 'ugriz'
	
	dtype = [(b, 'f8') for b in sdss_bands]
	s82_mags = np.empty(rows['s82_counts'].shape[0], dtype=dtype)
	
	for band, (name, b) in enumerate(zip(sdss_bands, b_s82)):
		s82_mags[name][:] = luptitudes2mags(rows['s82_counts'][:, band], b)
	
	
	# Transform SDSS magnitudes to synthetic PS1 magnitudes
	s82_ps1_mags = pssdsstransformall(s82_mags)
	
	
	# Filter objects which do not have 5-band Stripe-82 detections
	idx = np.isfinite(s82_ps1_mags) & (s82_ps1_mags > 0.) & (s82_ps1_mags < 30.)
	idx = np.all(idx, axis=1)
	
	print 'Stripe-82 objects filtered: %d of %d' % (np.sum(~idx), len(idx))
	
	rows = rows[idx]
	s82_mags = s82_mags[idx]
	s82_ps1_mags = s82_ps1_mags[idx]
	
	
	# Which Stripe-82 objects have PS1 matches
	ps1_mags = rows['ps1_mean']
	ps1_mask = (ps1_mags > 0.)
	
	match_dist = gc_dist(rows['ra_s82'], rows['dec_s82'], rows['ra_ps1'], rows['dec_ps1'])
	max_dist = 1. * np.pi / 180. / 3600. # One arcsecond
	idx = (match_dist < max_dist)
	
	for band in xrange(5):
		ps1_mask[:, band] = ps1_mask[:, band] & idx
	
	'''
	ps1_row_mask = (  (rows['ps1_nmag_ok'][:,0] > 0)
	                & (np.sum(rows['ps1_nmag_ok'] > 0, axis=1) >= args.n_bands)
	                & (np.sum(rows['ps1_nmag_ok'], axis=1) >= args.n_det)
	                & (np.sum(rows['ps1_mean'] - rows['ps1_mean_ap'] < 0.1, axis=1) >= n_pointlike)
	               )
	
	for band in xrange(ps1_mask.shape[1]):
		ps1_mask[:, band] = ps1_mask[:, band] & ps1_row_mask
	'''
	
	print 'Stripe-82 detections:', len(rows)
	print 'PS1 griz matches:', np.sum(ps1_mask, axis=0)
	
	
	# PS1 completeness in each magnitude bin
	mag_bins = np.linspace(-2., 4., 60)
	bin_min = mag_bins[:-1]
	bin_max = mag_bins[1:]
	mag_bin_center = 0.5 * (bin_min + bin_max)
	
	'''
	ps1_pct = np.empty((mag_bins.size-1, 5), dtype='f8')
	
	for band in xrange(5):
		for i, (mag_min, mag_max) in enumerate(zip(bin_min, bin_max)):
			idx = (s82_ps1_mags[:, band] >= mag_min) & (s82_ps1_mags[:, band] < mag_max)
			
			ps1_pct[i, band] = np.sum(ps1_mask[idx, band], axis=0) / float(np.sum(idx))
	'''
	
	# PS1 completeness in different spatial pixels
	pix_idx = lb2pix(128, rows['ra_s82'], rows['dec_s82'])
	
	pix_idx_unique = np.unique(pix_idx)
	n_pix = pix_idx_unique.size
	
	ps1_pct_area = np.empty((n_pix, mag_bins.size-1, 5), dtype='f8')
	maglim_area = np.empty((n_pix, 5), dtype='f8')
	n_stars = np.empty(n_pix)
	
	for k, p_idx in enumerate(pix_idx_unique):
		in_pix = (pix_idx == p_idx)
		n_stars[k] = np.sum(in_pix)
		
		print '%d stars in pixel %d.' % (n_stars[k], k + 1)
		
		s82_ps1_tmp = s82_ps1_mags[in_pix]
		ps1_mask_tmp = ps1_mask[in_pix]
		
		for band in xrange(5):
			maglim = rows['ps1_maglim'][:, band]
			idx = (maglim > 0.)
			maglim_area[k, band] = np.median(maglim[idx])
			maglim = maglim_area[k, band]
			
			for i, (mag_min, mag_max) in enumerate(zip(bin_min, bin_max)):
				idx = (s82_ps1_tmp[:, band] - maglim >= mag_min) & (s82_ps1_tmp[:, band] - maglim < mag_max)
				
				ps1_pct_area[k, i, band] = np.sum(ps1_mask_tmp[idx, band], axis=0) / float(np.sum(idx))
	
	idx = (n_stars > 1000)
	ps1_pct_area = np.percentile(ps1_pct_area[idx], [15.87, 50., 84.13], axis=0)
	ps1_pct_area = np.array(ps1_pct_area)
	
	
	idx = (ps1_pct_area[0, :, :] < 1.e-10)
	ps1_pct_area[0, idx] = 1.e-10 * (0.5 + np.random.random(ps1_pct_area[0, idx].shape))
	
	idx = (ps1_pct_area[1, :, :] < 1.e-9)
	ps1_pct_area[1, idx] = 1.e-9 * (0.5 + np.random.random(ps1_pct_area[0, idx].shape))
	
	idx = (ps1_pct_area[2, :, :] < 1.e-8)
	ps1_pct_area[2, idx] = 1.e-8 * (0.5 + np.random.random(ps1_pct_area[0, idx].shape))
	
	
	# PS1 completeness as a function of mag - maglimit
	mag_diff_bins = np.linspace(-6., 4., 100)
	bin_min = mag_diff_bins[:-1]
	bin_max = mag_diff_bins[1:]
	diff_bin_center = 0.5 * (bin_min + bin_max)
	
	ps1_pct_diff = np.empty((mag_diff_bins.size-1, 5), dtype='f8')
	s82_ps1_diff = s82_ps1_mags - rows['ps1_maglim']
	
	for band in xrange(5):
		idx_maglim = (rows['ps1_maglim'][:, band] > 0.)
		print band, np.median(rows['ps1_maglim'][idx_maglim, band])
		
		for i, (mag_min, mag_max) in enumerate(zip(bin_min, bin_max)):
			idx_bin = (s82_ps1_diff[:, band] >= mag_min) & (s82_ps1_diff[:, band] < mag_max)
			idx = idx_maglim & idx_bin
			
			ps1_pct_diff[i, band] = np.sum(ps1_mask[idx, band], axis=0) / float(np.sum(idx))
	
	
	# Completeness parameterization
	tmp_pct = ps1_pct_area[1, -20:, :]
	idx = (tmp_pct > 1.e-5) & np.isfinite(tmp_pct)
	comp_floor = np.median(tmp_pct[idx])
	print 'Completeness floor: %.2g' % comp_floor
	
	#dm_0 = [0.15, 0.23, 0.17, 0.12, 0.15]
	dm_0 = [0.16, 0.16, 0.16, 0.16, 0.16]
	dm_1 = 0.20
	
	'''
	comp_fit = 0.5 * (1. - scipy.special.erf((dm - 0.15) / 0.47))
	ax.plot(dm, comp_fit, lw=2., alpha=0.3, c='orange')
	comp_fit = (1. - comp_floor) * comp_fit + comp_floor
	'''
	
	#comp_fit = 1. / (1. + np.exp((dm - 0.13) / 0.20))
	#comp_fit_floor = (1. - comp_floor) * comp_fit + comp_floor
	
	
	# Plot completeness
	fig = plt.figure(figsize=(9,6), dpi=150)
	
	band_names = ['g', 'r', 'i', 'z', 'y']
	plt_colors = ['c', 'b', 'g', 'r', 'gray']
	
	for band, (name, color) in enumerate(zip(band_names, plt_colors)):
		maglim = rows['ps1_maglim'][:, band]
		idx = (maglim > 0.)
		maglim = np.percentile(maglim[idx], [15.87, 50., 84.13])
		
		print 'maglim_%d: %.2f + %.2f - %.2f' % (band,
		                                         maglim[1],
		                                         maglim[1] - maglim[0],
		                                         maglim[2] - maglim[1])
		
		ax = fig.add_subplot(2, 3, band+1)
		
		ax.axvline(x=0., c='k', ls=':', lw=1., alpha=0.2)
		ax.axhline(y=1., c='k', ls=':', lw=1., alpha=0.2)
		
		pos = np.all(ps1_pct_area[:, :, band] > 0, axis=0)
		
		ax.fill_between(mag_bin_center,
		                ps1_pct_area[0, :, band],
		                ps1_pct_area[2, :, band],
		                where=pos,
		                color=color,
		                edgecolor=color,
		                alpha=0.5,
		                label=r'$%s_{\mathrm{P1}}$' % name)
		
		ax.semilogy(mag_bin_center, ps1_pct_area[1, :, band],
		            c=color, alpha=0.5, label=r'$%s_{\mathrm{P1}}$' % name)
		
		tmp_pct = ps1_pct_area[1, -25:, band]
		idx = (tmp_pct > 1.e-5) & np.isfinite(tmp_pct)
		comp_floor = np.median(tmp_pct[idx])
		print 'Completeness floor %d: %.2g' % (band, comp_floor)
		comp_floor = 0.01
		
		dm = np.linspace(-2., 4., 1000)
		comp_fit = 1. / (1. + np.exp((dm - dm_0[band]) / dm_1))
		comp_fit_floor = (1. - comp_floor) * comp_fit + comp_floor
		
		ax.semilogy(dm, comp_fit, lw=2., alpha=0.5,
	                              c='k', ls='-',
	                              label=r'$\mathrm{Fit}$')
		
		ax.semilogy(dm, comp_fit_floor, lw=1., alpha=0.25,
		                                c='k', ls='--')
		
		ax.set_yscale('log')
		ax.set_xlim(-1., 2.)
		ax.set_ylim(0.005, 1.5)
		
		if band < 3:
			ax.xaxis.set_label_position('top')
			ax.xaxis.tick_top()
		
		ax.set_xticks([-0.5, 0.0, 0.5, 1.0, 1.5])
		ax.set_xlabel(r'$\Delta %s_{\mathrm{P1}} \ (\mathrm{mag})$' % name, fontsize=16)
		
		ax.yaxis.set_major_formatter(FormatStrFormatter(r'$%.2f$'))
		
		if band not in [0, 3]:
			ax.set_yticklabels([])
	
	fig.text(0.06, 0.5, r'$\mathrm{PS1 \ Completeness}$',
	         fontsize=16, va='center', ha='center', rotation='vertical')
	
	# Legend
	ax = fig.add_subplot(2, 3, 6)
	
	ax.fill_between([0.1, 0.35], [0.7, 0.7], [0.8, 0.8],
	                color='orange', edgecolor='orange', alpha=0.5)
	ax.plot([0.1, 0.35], [0.75, 0.75], color='orange', alpha=0.5)
	ax.text(0.45, 0.75, r'$1 \sigma \ \mathrm{Region}$',
	        ha='left', va='center', fontsize=16)
	
	ax.plot([0.1, 0.35], [0.5, 0.5], lw=2., alpha=0.5,
	                                c='k', ls='-')
	ax.text(0.45, 0.5, r'$\mathrm{Fit}$',
	        ha='left', va='center', fontsize=16)
	
	ax.plot([0.1, 0.35], [0.3, 0.3], lw=1., alpha=0.25,
	                                c='k', ls='--')
	ax.text(0.45, 0.3, r'$\mathrm{Fit \ with \ floor}$',
	        ha='left', va='center', fontsize=16)
	
	ax.set_xlim([0., 1.])
	ax.set_ylim([0.05, 1.05])
	
	ax.axis('off')
	
	
	fig.subplots_adjust(top=0.85, bottom=0.13,
	                    left=0.13, right=0.95,
	                    hspace=0., wspace=0.)
	
	fig.savefig('tmp.png', dpi=300)
	
	#ax.set_title(r'$\mathrm{Absolute}$', fontsize=16)
	#ax.set_xlabel(r'$\mathrm{\Delta m_{P1} \ (mag)}$', fontsize=16)
	#ax.set_xlabel(r'$\mathrm{Stripe-82 \ asinh \ mag}$', fontsize=14)
	#ax.set_ylabel(r'$\mathrm{PS1 \ Completeness}$', fontsize=16)
	
	'''
	ax = fig.add_subplot(2,1,2)
	
	band_names = ['g', 'r', 'i', 'z', 'y']
	plt_colors = ['c', 'b', 'g', 'r', 'gray']
	
	for band, (name, color) in enumerate(zip(band_names, plt_colors)):
		ax.semilogy(diff_bin_center, ps1_pct_diff[:, band], c=color, label=r'$%s$' % name)
	
	ax.set_title(r'$\mathrm{Relative}$', fontsize=16)
	ax.set_xlabel(r'$\mathrm{mag \ - \ maglim}$', fontsize=14)
	#ax.set_xlabel(r'$\mathrm{Stripe-82 \ asinh \ mag}$', fontsize=14)
	ax.set_ylabel(r'$\mathrm{PS1 \ Completeness}$', fontsize=14)
	ax.legend()
	
	ax.set_xlim(-1., 2.)
	ax.set_ylim(0.005, 1.5)
	
	
	fig.savefig('tmp.png', dpi=200)
	'''
	
	# Histograms of synthetic PS1 vs real PS1 magnitudes
	fig = plt.figure()
	
	diff = ps1_mags - s82_ps1_mags
	
	for band, name in enumerate(band_names):
		ax = fig.add_subplot(1, 5, band+1)
		
		ax.hist(diff[ps1_mask[:, band], band], bins=50)
		
		ax.set_title(r'$\Delta %s$' % name, fontsize=16)
		ax.set_xlabel(r'$\Delta %s \ (\mathrm{mag})$' % name, fontsize=14)
	
	
	# Color-color diagrams of synthetic PS1 photometry
	fig = plt.figure()
	
	for c2 in xrange(4):
		for c1 in xrange(c2):
			ax = fig.add_subplot(3, 4, 1 + 3*c1 + c2)
			
			diff_1 = s82_ps1_mags[:, c1] - s82_ps1_mags[:, c1+1]
			diff_2 = s82_ps1_mags[:, c2] - s82_ps1_mags[:, c2+1]
			
			ax.scatter(diff_1, diff_2, s=1.5, alpha=0.05, edgecolor='none')
			
			xlim = ax.get_xlim()
			ylim = ax.get_ylim()
			
			ax.set_xlim(xlim[1], xlim[0])
			ax.set_ylim(ylim[1], ylim[0])
	
	plt.show()
		
	
	return 0