In [1]:
import os
import numpy as np
import requests
In [2]:
# get some CSV data from the SDSS SQL server
URL = "http://skyserver.sdss.org/dr12/en/tools/search/x_sql.aspx"
cmd = """
SELECT TOP 1000
p.u, p.g, p.r, p.i, p.z, s.class, s.z, s.zerr
FROM
PhotoObj AS p
JOIN
SpecObj AS s ON s.bestobjid = p.objid
WHERE
p.u BETWEEN 0 AND 19.6 AND
p.g BETWEEN 0 AND 20 AND
s.class = 'GALAXY'
"""
if not os.path.exists('galaxy_colors.csv'):
cmd = ' '.join(map(lambda x: x.strip(), cmd.split('\n')))
response = requests.get(URL, params={'cmd': cmd, 'format':'csv'})
with open('galaxy_colors.csv', 'w') as f:
f.write(response.text)
In [3]:
!ls -lh galaxy_colors.csv
In [4]:
!more galaxy_colors.csv
In [5]:
dtype=[('u', 'f8'),
('g', 'f8'),
('r', 'f8'),
('i', 'f8'),
('z', 'f8'),
('class', 'S10'),
('redshift', 'f8'),
('redshift_err', 'f8')]
data = np.loadtxt('galaxy_colors.csv', skiprows=2, delimiter=',', dtype=dtype)
In [6]:
data[:10]
Out[6]:
In [8]:
from astropy.io import ascii
In [9]:
data = ascii.read('galaxy_colors.csv', format='csv', comment='#')
In [10]:
type(data)
Out[10]:
In [11]:
data[:10]
Out[11]:
In [12]:
import pandas
In [13]:
data = pandas.read_csv('galaxy_colors.csv', comment='#')
In [14]:
type(data)
Out[14]:
In [15]:
data.head()
Out[15]:
In [16]:
data.describe()
Out[16]:
In [ ]:
# Pandas reads from *lots* of different data sources
pandas.read_
In [17]:
# get some data from CDS
prefix = "http://cdsarc.u-strasbg.fr/vizier/ftp/cats/J/ApJ/686/749/"
for fname in ["ReadMe", "table10.dat"]:
if not os.path.exists(fname):
response = requests.get(prefix + fname)
with open(fname, 'w') as f:
f.write(response.text)
In [ ]:
!cat table10.dat
In [ ]:
!cat ReadMe
In [20]:
# must specify the "readme" here.
data = ascii.read("table10.dat", format='cds', readme="ReadMe")
In [21]:
data
Out[21]:
See http://astropy.readthedocs.org/en/stable/io/ascii/index.html for all the supported formats.
In [ ]:
# get an SDSS image (can search for images from http://dr12.sdss3.org/fields/)
if not os.path.exists("frame-g-006728-4-0121.fits.bz2"):
!wget http://dr12.sdss3.org/sas/dr12/boss/photoObj/frames/301/6728/4/frame-g-006728-4-0121.fits.bz2
if not os.path.exists("frame-g-006728-4-0121.fits"):
!bunzip2 frame-g-006728-4-0121.fits.bz2
In [22]:
from astropy.io import fits
hdulist = fits.open("frame-g-006728-4-0121.fits")
In [23]:
hdulist
Out[23]:
In [24]:
hdulist.info()
In [25]:
hdulist[0].data
Out[25]:
In [ ]:
hdulist[0].header
In [27]:
import fitsio
In [28]:
f = fitsio.FITS("frame-g-006728-4-0121.fits")
In [29]:
# summary of file HDUs
f
Out[29]:
In [30]:
# summary of first HDU
f[0]
Out[30]:
In [31]:
# Summary of 3rd HDU
f[2]
Out[31]:
In [32]:
# Actually read the data.
data = f[0].read()
data
Out[32]:
In [34]:
from scipy.io import readsav
In [35]:
# Note: won't work unless you have this sav file!
data = readsav("150623434_det8_8100keV.sav")
In [36]:
data
Out[36]:
In [37]:
len(data.events)
Out[37]:
In [ ]:
!rm galaxy_colors.csv
!rm ReadMe
!rm table10.dat
!rm frame-g-006728-4-0121.fits.bz2
!rm frame-g-006728-4-0121.fits