%matplotlib inline from __future__ import division import numpy as np import pandas as pd import mechanize from StringIO import StringIO # To read a string like a file # This IPython magic generates a table with version information #https://github.com/jrjohansson/version_information %load_ext version_information %version_information numpy, pandas, StringIO # URL to the SDSS SQL Search DR10 url = "http://skyserver.sdss3.org/dr10/en/tools/search/sql.aspx" s = 'SELECT TOP 10 \ objID, ra, dec, modelMag_u, modelMag_g \ FROM \ PhotoPrimary \ WHERE \ ra BETWEEN 140 and 141 \ AND dec BETWEEN 20 and 21 \ AND type = 6 \ AND clean = 1 \ AND modelMag_u - modelMag_g < 0.5' br = mechanize.Browser() resp = br.open(url) resp.info() for f in br.forms(): print f.name for f in br.forms(): print f.name for c in f.controls: print '\t',c.name, '\t', c.type br.select_form(name="sql") br['cmd'] = s # This is the string with the sql query br['format']=['csv'] # data output format response = br.submit() print response.get_data() file_like = StringIO(response.get_data()) df =pd.read_csv(file_like, skiprows = 1) # skip the first row df df.columns = ['objID','ra','dec','u','g'] df['u-g'] = df['u']-df['g'] df def SDSS_select(sql): '''input: string with a valid SQL query output: a Pandas dataframe ''' 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) # URL a SQL Search DR10 url = "http://skyserver.sdss3.org/dr10/en/tools/search/sql.aspx" sql = 'SELECT TOP 10 \ objID, ra, dec, modelMag_u,modelMag_g,modelMag_r,modelMag_i,modelMag_z \ FROM Star \ WHERE ra BETWEEN 150 and 152 AND dec BETWEEN 30 and 31 AND clean = 1' df = SDSS_select(sql) df