Selecting Galaxies from SDSS

We need about 10,000 galaxies from SDSS, in a narrow redshift range around z = 0.1, to match with our halos. We can grab this data from the SDSS skyserver SQL server from python, using the mechanize web browser, and then manipulate the catalog with pandas.


In [1]:
# Querying skyserver, using code from Eduardo Martin's blog post at
# http://balbuceosastropy.blogspot.com/2013/10/an-easy-way-to-make-sql-queries-from.html

import pandas as pd

def SDSS_select(sql):
    from StringIO import StringIO # To read a string like a file
    import mechanize
    url = "http://skyserver.sdss3.org/dr10/en/tools/search/sql.aspx"
    br = mechanize.Browser()
    br.open(url)
    br.select_form(name="sql")
    br['cmd'] = sql
    br['format']=['csv']
    response = br.submit()
    file_like = StringIO(response.get_data())
    return pd.read_csv(file_like,  skiprows=1)

We want a sample of galaxies and their 5-band photometry, plus their redshifts and possibly positions.


In [2]:
galaxies = "SELECT top 10000 \
objid, ra, dec, z, \
dered_u AS mag_u, \
dered_g AS mag_g, \
dered_r AS mag_r, \
dered_i AS mag_i, \
dered_z AS mag_z \
FROM SpecPhoto \
WHERE \
(class = 'Galaxy')"
print galaxies


SELECT top 10000 objid, ra, dec, z, dered_u AS mag_u, dered_g AS mag_g, dered_r AS mag_r, dered_i AS mag_i, dered_z AS mag_z FROM SpecPhoto WHERE (class = 'Galaxy')

This SQL checks out at the SkyServer SQL box, which has a syntax checking option.


In [3]:
data = SDSS_select(galaxies)
data


Out[3]:
objid ra dec z mag_u mag_g mag_r mag_i mag_z
0 1237657586562367630 43.775040 0.413649 0.126386 21.03417 19.34688 18.38340 17.95347 17.66356
1 1237663239272989373 43.653400 0.434200 0.547067 24.91928 22.78911 20.91748 19.79025 19.40255
2 1237663239272988917 43.668442 0.439617 0.052173 19.85831 18.94410 18.48233 18.25059 18.19678
3 1237663239273185525 43.989582 0.599770 0.136318 21.39913 19.34047 18.32451 17.88236 17.55692
4 1237678617437012415 43.821293 0.758974 0.073327 20.48200 19.30610 18.76852 18.46322 18.28343
5 1237678617437077917 44.007378 0.792395 0.193352 20.12095 19.38557 18.79778 18.46749 18.30729
6 1237663784216560179 43.832413 0.107809 0.040651 20.03710 18.94928 18.43859 18.17211 17.99428
7 1237663239273185656 44.016762 0.455286 0.188615 20.57470 19.83936 19.31141 19.04830 19.05104
8 1237657586562498655 43.969959 0.381950 0.170142 22.31340 20.41215 19.38978 18.88227 18.52340
9 1237678617437077985 43.899548 0.718535 0.151472 20.81523 19.50208 18.87636 18.46378 18.29541
10 1237663239273185751 44.004189 0.604719 0.066272 20.18939 19.21815 18.56936 18.26274 18.13155
11 1237657587099304470 43.902180 0.665468 0.151065 22.01183 20.26443 19.21065 18.89541 18.60719
12 1237663239273120186 43.972099 0.536306 0.013585 19.86991 18.48728 18.07704 17.96360 18.01252
13 1237657586562498888 43.951332 0.382415 0.286107 25.79793 20.20541 18.77678 18.23209 17.85291
14 1237657586562498772 44.025291 0.251850 0.106721 20.36275 18.93699 18.44948 18.15108 18.30925
15 1237663784216690940 44.025455 0.111749 0.126447 21.76604 19.85276 18.97080 18.54132 18.27745
16 1237663239273119990 43.883636 0.596573 0.262950 21.84823 20.51458 19.25124 18.69886 18.50143
17 1237663784216625298 43.948006 0.198804 0.256940 21.44145 19.94596 19.16494 18.91770 18.86684
18 1237657586562498760 43.944321 0.414512 0.169728 22.07316 19.53397 18.45605 18.00348 17.64622
19 1237663784216690835 44.038860 0.183669 0.153760 20.90098 19.10948 18.06659 17.61707 17.35549
20 1237663784216690968 44.141319 0.111346 0.358172 22.12443 20.23563 18.48760 17.85881 17.52025
21 1237678617437143893 44.123199 0.741500 0.846062 25.07072 21.64779 20.37119 19.24862 18.74790
22 1237660241384833417 44.033955 0.593850 0.065717 21.58032 20.13515 19.42918 19.05468 18.82600
23 1237657586562564189 44.226398 0.331800 0.940458 24.28942 24.89169 24.71314 24.22427 22.76823
24 1237657586562498792 44.092396 0.270505 0.153720 20.63382 19.58934 19.05981 18.75455 18.61685
25 1237657586562498778 44.052094 0.422546 0.129871 20.67114 19.22268 18.31128 17.79123 17.41321
26 1237678617437143322 44.170444 0.754441 0.063084 20.41182 19.31924 18.55593 18.14125 17.85778
27 1237660241384833330 44.033768 0.621798 0.136514 20.44513 19.24904 18.79747 18.43540 18.36422
28 1237678617437143243 44.096973 0.819190 0.234455 21.51809 19.93056 19.13398 18.74038 18.38159
29 1237678617437208674 44.221958 0.728609 0.151745 21.80535 19.71846 18.71607 18.28378 18.00032
... ... ... ... ... ... ... ... ... ...
9970 1237662641733435613 245.913020 26.322251 0.045461 18.61456 17.15639 16.57779 16.27548 16.06593
9971 1237662641196695703 245.991670 25.783972 0.046921 18.84198 16.78484 15.95405 15.52519 15.18845
9972 1237662641733435832 246.015970 26.213544 0.104744 19.40457 17.63408 16.62093 16.15553 15.75502
9973 1237662667499045244 246.027950 25.862946 0.098935 19.99191 17.93497 17.10342 16.63797 16.36509
9974 1237662640659824943 245.605600 25.444592 0.099258 19.33852 17.85586 17.10580 16.69955 16.37242
9975 1237662665888498081 245.449620 24.894538 0.088001 19.90777 18.14365 17.21662 16.74666 16.43968
9976 1237662640659759435 245.562440 25.544576 0.112272 20.02865 18.20974 17.28359 16.90512 16.54567
9977 1237662666425303526 245.573570 25.324495 0.282102 21.92653 19.69144 18.18648 17.63031 17.25218
9978 1237662640659693927 245.395970 25.617593 0.099447 20.04987 18.08176 17.15162 16.77929 16.42136
9979 1237662640659694000 245.484380 25.689203 0.100083 19.20603 16.95294 16.00530 15.58537 15.23311
9980 1237662640659824877 245.618310 25.501157 0.248809 21.61355 19.23965 17.76503 17.22625 16.83906
9981 1237662640659694092 245.484190 25.620033 0.099895 20.46037 18.44607 17.36322 16.90251 16.51910
9982 1237662640123019469 245.407000 24.997351 0.146770 19.47933 17.53217 16.41281 15.96977 15.62795
9983 1237662665888432471 245.291980 24.942623 0.088762 18.38208 16.58669 15.75577 15.38342 15.13564
9984 1237662665888563518 245.567000 24.829575 0.099649 19.89125 17.91728 16.99540 16.61091 16.30472
9985 1237662640123019558 245.457950 24.930652 0.038086 18.24272 16.34269 16.00497 15.73788 15.64803
9986 1237662619727626486 245.627280 25.717068 0.098777 19.58933 17.67497 16.74874 16.35649 16.04684
9987 1237662665888563430 245.349340 24.753535 0.103705 19.62536 18.31580 17.62029 17.21796 16.97219
9988 1237662619727561168 245.591270 25.720734 0.098858 18.39512 17.30279 16.78793 16.44860 16.29597
9989 1237662665888498034 245.418160 24.895221 0.037738 16.91115 15.41512 14.71897 14.35496 14.08578
9990 1237662619190755808 245.359280 25.307671 0.189386 20.72948 18.90032 17.62409 17.07948 16.74047
9991 1237662665888432597 245.268280 25.019420 0.088185 20.51324 18.66868 17.81180 17.40926 17.14170
9992 1237662667499176160 246.311190 25.759922 0.047505 19.32353 18.16437 17.70330 17.54589 17.71808
9993 1237662641733566798 246.218760 26.001769 0.102703 18.96050 17.50391 16.74697 16.31078 16.03536
9994 1237661463307485437 246.479440 26.253861 0.112909 19.77280 17.79402 16.81689 16.42865 16.08155
9995 1237662667499110865 246.204480 25.748434 0.048838 18.78157 17.63042 17.20413 16.92030 16.82409
9996 1237662641733697765 246.479490 26.005265 0.100569 18.67397 17.38310 16.77838 16.42766 16.21534
9997 1237662641733566697 246.208140 26.122755 0.100862 20.03050 18.09996 17.18674 16.82048 16.50355
9998 1237662641733566745 246.216910 26.082081 0.112674 19.92529 18.54171 17.82359 17.43315 17.13722
9999 1237662641733566933 246.260300 26.093193 0.342397 22.62578 20.62342 18.80594 18.18077 17.83417

10000 rows × 9 columns

Visualizing the galaxies

Let's see if we can see the cosmic web. Initial plotting code from Josh Bloom's AstroHackWeek 2014 lecture.


In [4]:
# For pretty plotting
!pip install --upgrade seaborn


Requirement already up-to-date: seaborn in /Users/pjm/miniconda/lib/python2.7/site-packages
Cleaning up...

In [5]:
%pylab inline
import seaborn
seaborn.set()
import copy


Populating the interactive namespace from numpy and matplotlib

First let's see the redshift distribution.


In [6]:
bins =  hist(data['z'].values,bins=100) ; xlabel("redshift") ; ylabel("N")


Out[6]:
<matplotlib.text.Text at 0x1092f8f90>

Now let's look at everything else, colored by redshift.


In [7]:
import matplotlib as mpl
import matplotlib.cm as cm

data = data[(data['mag_r'] > -9999)]

## truncate the color at z=0.6 just to keep some contrast.
norm = mpl.colors.Normalize(vmin=min(data['z'].values), vmax=0.6)
cmap = cm.jet_r
m = cm.ScalarMappable(norm=norm, cmap=cmap)
rez = pd.scatter_matrix(data[0:2000], alpha=0.2,figsize=[15,15],color=m.to_rgba(data['z'].values))


Save this for future use.


In [8]:
data.to_csv('galaxies.clean.csv')

In [ ]: