In [5]:
# This changes the current directory to the base saga directory - make sure to run this first!
# This is necessary to be able to import the py files and use the right directories,
# while keeping all the notebooks in their own directory.
import os
import sys
import numpy as np

if 'saga_base_dir' not in locals():
    saga_base_dir = os.path.abspath('..')
if saga_base_dir not in sys.path:
    os.chdir(saga_base_dir)

In [6]:
for module in ['hosts', 'targeting', 'mmthecto']:
    if module in globals():
        reload(globals()[module])
    else:
        globals()[module] = __import__(module)
g = targeting.get_gama() #re-caches the gama catalo

In [7]:
hostd=hosts.load_all_hosts()


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-7-1baa1a72cab1> in <module>()
----> 1 hostd=hosts.load_all_hosts()

/Users/erik/projects/saga/hosts.pyc in load_all_hosts(hostsfile, existinghosts, usedlgname, keyonname)
    635 
    636     i = 1
--> 637     with open(hostsfile) as f:
    638         f.readline()  # header
    639         for l in f:

IOError: [Errno 2] No such file or directory: 'hosts.dat'

In [16]:
tab = table.Table()
tab.add_column(table.Column(name='nsaid', data=[h.nsaid for h in hostd.values()]))
tab.add_column(table.Column(name='ra', data=[h.ra for h in hostd.values()]))
tab.add_column(table.Column(name='dec', data=[h.dec for h in hostd.values()]))
tab.add_column(table.Column(name='distmpcoh', data=[h.distmpc * .704 for h in hostd.values()]))
tab.add_column(table.Column(name='arcmin300kpc', data=[h.physical_to_projected(300) for h in hostd.values()]))

In [28]:
tab.write('hoststab.dat',format='ascii')
tab


Out[28]:
nsaidradecdistmpcoharcmin300kpc
13129532.2447161027-7.0589262045538.751888971618.7359155194
2327432.4108133056-10.146168560137.643031176119.28782288
146210232.692190849-2.8294945768826.170573896927.7430720798
1272268.69484030832-8.3964449227415.123529959848.0081118574
591610.866033784-0.12494100366541.154935886217.6419207649
166313234.13201128216.607773796119.523695686837.1882521443
1261571.27242867652-7.0933808104438.326682207518.9437769244
12938721.016275403612.917462362126.059939291227.8608522405
12943121.13886788893.8006638381521.242015339434.180001586
12778212.3589675368-1.7712327659838.673899900118.7736980202
12914019.998321310114.786226831240.19696549818.0623613995
...............
13227338.792035442120.851379314942.364106149317.1383792551
15520441.6414475364-0.2471374513826.907708302426.9830529539
13312045.995662763943.398429640229.137666818724.9179909465
13335550.5273286228-15.399928765721.858304655633.2163051724
15557652.5552726224-5.5433160727938.05980212219.0766130539
13381067.076919548-5.178771543941.875341786817.3384165241
13421282.0082992538-5.310740295827.121720044326.7701354046
148472284.90117241819.42430715631.680384532622.9180336256
147103248.16305696778.198238078716.891206118542.9840304415
53553224.27828573649.669011141440.083265742718.1135968973
165952227.30487889852.528519203634.274386017321.1835193087

In [58]:
hids = np.array([h.nsaid for h in hostd.values()])
hds = np.array([h.distmpc for h in hostd.values()])
h300kpc = np.array([h.physical_to_projected(300) for h in hostd.values()])
hra = np.array([h.ra for h in hostd.values()])
hdec = np.array([h.dec for h in hostd.values()])
scatter(hra,hdec)


Out[58]:
<matplotlib.collections.PathCollection at 0x1278e0390>

In [77]:
from pyspherematch import spherematch
degtol = 3
losmpc = 8
i1,i2, dd = spherematch(hra,hdec,hra,hdec,nnearest=2)
dra = hra - hra[i2]
ddec = hdec - hdec[i2]
inmsk = (dd < degtol) & (np.abs(hds[i1] - hds[i2])<losmpc)
scatter(dra[inmsk],ddec[inmsk],c='b')
scatter(dra[~inmsk],ddec[~inmsk],c='r')
xlim(-30,30)
ylim(-30,30)
print sum(inmsk),'host pairs w/i',degtol,'deg and',losmpc,'Mpc LOS sep:\n',zip(hids[i1[inmsk]],hids[i2[inmsk]])
print 'Corresponding LOS distances:\n',zip(hds[i1[inmsk]],hds[i2[inmsk]])
print 'Corresponding proj distances:\n',dd[inmsk]
print 'Corresponding arcmin 300 kpc:\n',zip(h300kpc[i1[inmsk]],h300kpc[i2[inmsk]])


8 host pairs w/i 3 deg and 8 Mpc LOS sep:
[(5916, 127782), (127782, 5916), (144045, 143975), (143975, 144045), (25728, 151874), (151874, 25728), (160608, 102480), (102480, 160608)]
Corresponding LOS distances:
[(58.45871574738635, 54.934516903611289), (54.934516903611289, 58.45871574738635), (37.959864674193661, 41.352117940698548), (41.352117940698548, 37.959864674193661), (57.200635425104174, 57.035095365840114), (57.035095365840114, 57.200635425104174), (55.46590286838024, 51.378599544161759), (51.378599544161759, 55.46590286838024)]
Corresponding proj distances:
[ 2.22224302  2.22224302  1.93120446  1.93120446  1.03235386  1.03235386
  2.4934044   2.4934044 ]
Corresponding arcmin 300 kpc:
[(17.641920764938998, 18.773698020227506), (18.773698020227506, 17.641920764938998), (27.168801577330417, 24.940053438483204), (24.940053438483204, 27.168801577330417), (18.029940114666189, 18.082270654940821), (18.082270654940821, 18.029940114666189), (18.593838338533825, 20.073027299022069), (20.073027299022069, 18.593838338533825)]

In [80]:
scatter(hra[inmsk]/15,hdec[inmsk])


Out[80]:
<matplotlib.collections.PathCollection at 0x12793d490>

CFHTLS Objects


In [12]:
hostd = hosts.load_all_hosts(keyonname=True)
hcfhtl1 = hostd['NSA155005']
hcfhtl2 = hosts.NSAHost(53145)

In [16]:
targs1 = targeting.select_targets(hcfhtl1, outercutrad=300)
targs2 = targeting.select_targets(hcfhtl2, outercutrad=300)

In [23]:
targs1.write('NSA%i_targets.dat' % hcfhtl1.nsaid,format='ascii')
targs2.write('NSA%i_targets.dat' % hcfhtl2.nsaid,format='ascii')

Testing SDSS image viewier


In [44]:
import hosts
import targeting

In [72]:
h = hosts.aiw

In [36]:
h.sdss_image_cutout(scale=60*u.arcmin)


Out[36]:

In [74]:
h.sdss_image_cutout(scale=8*u.arcmin, targets=targeting.select_targets(h),imagesize=(1024, 1024),raoffset=3*u.arcmin)


Found host AliceInWonderland in GAMA!
Removing 552 GAMA objects
Out[74]:

In [ ]:

Writing Target Catalogs


In [2]:
import targeting

In [16]:
otargs = targeting.select_targets(hosts.odyssey, outercutrad=300, removegalsathighz=False)
otargs.write('odyssey_targets.csv', format='ascii', delimiter=',')

In [17]:
btargs = targeting.select_targets(hosts.beowulf, outercutrad=300, removegalsathighz=False)
otargs.write('beowulf_targets.csv', format='ascii', delimiter=',')

In [18]:
targeting.select_targets?

Sub-select from master catalog


In [194]:
from astropy.io import ascii
from astropy import table
from astropy.coordinates import Angle
from astropy import units as u

In [220]:
ml = ascii.read('masterlist/masterlist.csv')

In [221]:
dm = 5*np.log10(ml['distance']) + 25
Mr = ml['r'] - dm

In [222]:
ml.add_column(table.MaskedColumn(name='r_abs', data=Mr))

In [223]:
rawrap = Angle(ml['RA'], u.deg).wrap_at(180*u.deg).value
rawrap = np.ma.masked_where(ml['RA'].mask, rawrap)  # angle does not preserve the mask right now

In [224]:
def mpctodiam(dmpc, rvirkpc):
    onempcdiam = ((2*rvirkpc/1000.)*u.radian).to(u.degree).value
    return onempcdiam / dmpc
fovdiam = mpctodiam(ml['distance'], 300)
ml.add_column(table.MaskedColumn(name='FOVdiamforRv300kpc', data=fovdiam))

In [225]:
vmsk = ml['vhelio'] < 3000 # w/i 42 Mpc
magmsk = (-20.7< Mr ) & (Mr < -20.1) #~MW-like

In [226]:
#selects ~ 1 deg diameter FOV = 300 kpc Rvir
szmsk = (0.75 < fovdiam) & (fovdiam < 1.1)

In [227]:
#this selects stripe82
s82ramsk = (-50 < rawrap)&(rawrap < 59)
s82decmsk = (-1.25 < ml['Dec']) & (ml['Dec'] < +1.25)
#a stricter definition that accounts for the edges to ~ 20"
s82ramsk_edges = (-49.5 < rawrap)&(rawrap < 58.5)
s82decmsk_edges = (-0.92 < ml['Dec']) & (ml['Dec'] < +0.92)

ml.add_column(table.MaskedColumn(name='strictlyinsideS82', data=((s82ramsk_edges)&(s82decmsk_edges)).astype(int)))

In [229]:
msk = vmsk & s82ramsk & s82decmsk
print 'w/o mag cut', sum(msk)
print 'w mag cut', sum(msk & magmsk)
print 'w mag cut w szmsk', sum(msk & magmsk & szmsk)
print 'relaxed mag cut', sum(msk & (Mr < -18) & szmsk)


w/o mag cut 180
w mag cut 4
w mag cut w szmsk 2
relaxed mag cut 7

In [230]:
np.ma.masked_print_option.set_display('')  # otherwise the empty entries have '--' instead of emptiness
mskml= ml[msk]
mskml.sort(['r_abs','FOVdiamforRv300kpc', 'strictlyinsideS82'])
mskml.write('s82masterlist.csv', format='ascii', delimiter=',')

In [231]:
mskml.show_in_browser()


Out[231]:
<open file '<fdopen>', mode 'w+b' at 0x10dff4660>

In [214]:
mski = ml['NSAID']== 132339
print msk[mski], vmsk[mski], s82ramsk[mski], s82decmsk[mski], szmsk[mski]
ml[mski].show_in_browser()


[False] [True] [ True] [False] [ True]
Out[214]:
<open file '<fdopen>', mode 'w+b' at 0x10dd35780>

Getting K-band mags for hosts


In [8]:
from astropy.io import ascii

In [12]:
hs = hosts.get_saga_hosts_from_google(googleun, passwd)

In [13]:
mslst=ascii.read('newmasterlist.csv')

In [16]:
ks=[]
kes=[]
for h in hs:
    msk=h.nsaid == mslst['NSAID']
    mmsk = mslst[msk]
    dm = -5*np.log10(mmsk['distance'])-25
    ks.append((mmsk['K']+dm)[0])
    kes.append(mmsk['K_err'][0])
hostnames = np.array([h.name for h in hs])
hostks = np.array(ks)
hostk_errs = np.array(kes)

nsatsdct = {'Odyssey':8,
 'Iliad':-1,
 'Alice':1,
 'Beowulf':4,
 'Gilgamesh':2,
 'Hamlet':2,
 'Aeneid':1,
 'AnaK':5,
 'Ulysses':-1,
 'Sopranos':-1,
 'Dune':-1}
nsats = np.array([nsatsdct[nm] for nm in hostnames])

In [17]:
compldct = {'Odyssey':.95,
 'Iliad':-1,
 'Alice':.1,
 'Beowulf':.3,
 'Gilgamesh':.96,
 'Hamlet':.44,
 'Aeneid':.62,
 'AnaK':.72,
 'Ulysses':-1,
 'Sopranos':-1,
 'Dune':-1}
compls = np.array([compldct[nm] for nm in hostnames])

In [18]:
plt.figure(figsize=(10,10))
plt.errorbar(hostks, nsats, None, hostk_errs,fmt='o')
plt.xlim(-24.6,-23.2)
plt.ylim(-1.2,8.2)
plt.axhline(0,color='k')
plt.xlabel('$M_K$',fontsize=24)
plt.ylabel('$N_{\\rm sats}$',fontsize=24)
plt.xlim(*plt.xlim()[::-1])


Out[18]:
(-23.199999999999999, -24.600000000000001)

In [34]:
plt.figure(figsize=(10,10))
msk = nsats>-1
plt.errorbar(hostks[msk], nsats[msk], nsats[msk]**0.5, hostk_errs[msk],fmt='o', ms=10)
plt.scatter(hostks[msk], nsats[msk]/compls[msk], facecolor='none',edgecolor='r',lw=2,s=70)
#plt.errorbar(hostks[msk], nsats[msk]/compls[msk], None, hostk_errs[msk],fmt='o')
plt.xlim(-24.6,-23.2)
plt.ylim(.1,12)
#plt.axhline(0,color='k')
plt.xlabel('$M_{\\rm K,Vega}$',fontsize=48)
plt.ylabel('$N_{\\rm sats}$',fontsize=48)
plt.xlim(*plt.xlim()[::-1])
plt.tight_layout()
plt.savefig('/Users/erik/Desktop/SAGAnums.pdf')


Compare GAMA to hosts


In [113]:
from astropy.coordinates import ICRS, Galactic
from astropy import units as u
from astropy.io import ascii

In [27]:
hs = hosts.get_saga_hosts_from_google(googleun, googlepw)
print [h.name for h in hs]
ras = [h.ra for h in hs]*u.deg
decs = [h.dec for h in hs]*u.deg
hcoo = ICRS(ras, decs)


['Odyssey', 'Iliad', 'Alice', 'Beowulf', 'Gilgamesh', 'Hamlet', 'Aeneid', 'AnaK', 'Ulysses']

In [251]:
hall = ascii.read('host_catalog_all.csv')
print hall.colnames


['NSAID', 'RA', 'Dec', 'PGC#', 'vhelio', 'distance', 'r', 'i', 'z', 'I', 'K', 'K_err', 'flag', 'K_abs', 'r_abs', 'b', 'dist_to_group', 'brightest_K_nearby', 'SDSS_FRACGOOD']

In [252]:
hallcoo = ICRS(np.array(hall['RA'])*u.deg, np.array(hall['Dec'])*u.deg)

In [253]:
g = hosts.get_gama()
g.dtype.names


Out[253]:
('GAMA_IAU_ID',
 'GAMA_ID',
 'SDSS_ID',
 'RA_J2000',
 'DEC_J2000',
 'r_PETRO',
 'Z_HELIO',
 'Z_QUALITY',
 'Z_SOURCE',
 'Z_DATE',
 'Z_SN',
 'Z_ID',
 'PHOT_SOURCE',
 'u_KRON',
 'g_KRON',
 'r_KRON',
 'i_KRON',
 'z_KRON',
 'Y_KRON',
 'J_KRON',
 'H_KRON',
 'K_KRON',
 'u_KRON_ERR',
 'g_KRON_ERR',
 'r_KRON_ERR',
 'i_KRON_ERR',
 'z_KRON_ERR',
 'Y_KRON_ERR',
 'J_KRON_ERR',
 'H_KRON_ERR',
 'K_KRON_ERR',
 'EXTINCTION_r',
 'r_SERS_MAG_10RE')

In [254]:
gcoo = ICRS(np.array(g['RA_J2000'])*u.deg, np.array(g['DEC_J2000'])*u.deg)

In [255]:
idx, d2d, d3d = hallcoo.match_to_catalog_sky(gcoo)

plt.figure(figsize=(10,6))
plt.subplot(1,2,1)
plt.hist(d2d.arcmin,bins=np.linspace(0,30,100))
plt.xlabel('arcmin')
plt.subplot(1,2,2)
plt.hist(d2d.arcsec,bins=np.linspace(0,10,100))
plt.xlabel('arcsec')

print '\n'.join([str(s) for s in hall[d2d<1*u.arcmin]['NSAID']])


140594
165392
2493
140930
13927
165536
15199

In [256]:
plt.figure(figsize=(20,15))
ax=plt.axes(projection='hammer')
plt.scatter((gcoo.ra+180*u.deg).wrap_at(np.pi*u.rad).rad, gcoo.dec.rad, lw=0,s=3,alpha=1,color='k', label='GAMA')
plt.scatter((hallcoo.ra+180*u.deg).wrap_at(np.pi*u.rad).rad, hallcoo.dec.rad, lw=0,s=8,alpha=1, color='r', label='hosts_all')
gal0 = Galactic(l=np.roll(np.linspace(0,360,100),67)*u.deg, b=np.zeros(100)*u.deg)
plt.plot((gal0.icrs.ra+180*u.deg).wrap_at(180*u.deg).rad, gal0.icrs.dec.rad, label='$b_{\\rm Gal}=0$',c='b')
plt.legend(loc=0)
a=plt.xticks([-2*np.pi/3,-np.pi/3,0,np.pi/3,2*np.pi/3], [str(n)+u'\xb0' for n in (60, 120, 180, 240, 300)])
plt.tight_layout()


Updataing GAMA


In [299]:
reload(targeting)
g = targeting.get_gama()
u = targeting.update_gama(None)

In [300]:
gamamsk = g['Z_SOURCE']==5
np.sum(g[gamamsk]['Z_HELIO']==-2)/np.sum(gamamsk)


Out[300]:
0.56548523468427958

In [302]:
g.dtype.names


Out[302]:
('GAMA_IAU_ID',
 'GAMA_ID',
 'SDSS_ID',
 'RA_J2000',
 'DEC_J2000',
 'r_PETRO',
 'Z_HELIO',
 'Z_QUALITY',
 'Z_SOURCE',
 'Z_DATE',
 'Z_SN',
 'Z_ID',
 'PHOT_SOURCE',
 'u_KRON',
 'g_KRON',
 'r_KRON',
 'i_KRON',
 'z_KRON',
 'Y_KRON',
 'J_KRON',
 'H_KRON',
 'K_KRON',
 'u_KRON_ERR',
 'g_KRON_ERR',
 'r_KRON_ERR',
 'i_KRON_ERR',
 'z_KRON_ERR',
 'Y_KRON_ERR',
 'J_KRON_ERR',
 'H_KRON_ERR',
 'K_KRON_ERR',
 'EXTINCTION_r',
 'r_SERS_MAG_10RE')

In [303]:
u.dtype.names


Out[303]:
('CATAID',
 'GAMA_NAME',
 'IC_FLAG',
 'N_SPEC',
 'N_GAMA_SPEC',
 'DIST',
 'SPECID',
 'SURVEY',
 'SURVEY_CODE',
 'RA',
 'DEC',
 'WMIN',
 'WMAX',
 'Z',
 'NQ',
 'PROB',
 'FILENAME',
 'URL',
 'URL_IMG')

In [307]:
u['CATAID']


Out[307]:
array([  6802,   6806,   6808, ..., 751313, 751319, 751324], dtype=int32)

In [308]:
np.array(g['GAMA_ID'])


Out[308]:
array([  6802,   6803,   6804, ..., 751313, 751319, 751324])

In [314]:
unfinishedids = np.array(g['GAMA_ID'][g['Z_HELIO']==-2])

In [324]:
np.sum(np.in1d(g['GAMA_ID'], u['CATAID']))/len(g)


Out[324]:
0.63100637009463389

In [340]:
np.unique(u['SURVEY'])


Out[340]:
chararray(['2QZ', '2SLAQ-LRG', '2SLAQ-QSO', '2dFGRS', '6dFGS', 'GAMA',
       'GAMA_LT', 'MGC', 'NED', 'SDSS', 'UZC'], 
      dtype='|S9')