In [197]:
%%bash
curl -O https://raw.githubusercontent.com/astropy/specutils/master/specutils/io/tests/files/gbt_1d.fits
ls -lh gbt_1d.fits
In [198]:
ls -lh gbt_1d.fits
In [13]:
pwd
Out[13]:
In [14]:
!pwd
In [17]:
!ls -lh gbt_1d.fits
In [19]:
from astropy.io import fits
In [54]:
fh = fits.open('gbt_1d.fits')
header = fits.getheader('gbt_1d.fits')
data = fits.getdata('gbt_1d.fits')
In [55]:
len(fh)
Out[55]:
In [23]:
fh[0]
Out[23]:
In [26]:
fh[0].data.shape
Out[26]:
In [28]:
%matplotlib inline
import pylab as pl
pl.plot(fh[0].data)
Out[28]:
In [44]:
hdr = fh[0].header
In [59]:
d = {'a' : (1,2,3), 'b': 2}
In [60]:
d['a'][0]
Out[60]:
In [61]:
d[0] # not valid!
In [62]:
hdr['NAXIS1']
Out[62]:
In [63]:
data.size
Out[63]:
In [31]:
xarr_pixels = np.arange(hdr['NAXIS1'])
xarr_pixels
Out[31]:
In [65]:
cdelt = hdr['CDELT1'] # spacing between pixels
crval = hdr['CRVAL1'] # reference coordinate
crpix = hdr['CRPIX1'] # pixel of the reference coordinate
cunit = hdr['CUNIT1']
In [66]:
cdelt, crval, crpix, cunit
Out[66]:
In [33]:
# +1 because FITS is 1-indexed, python is 0-indexed
xarr = (xarr_pixels - crpix + 1)*cdelt + crval
In [39]:
cunit
Out[39]:
In [40]:
xarr
Out[40]:
In [41]:
pl.plot(xarr, fh[0].data)
Out[41]:
In [67]:
from astropy import units as u
In [68]:
u.km/u.s
Out[68]:
In [71]:
xarr_u = xarr * u.km/u.s
xarr_u
Out[71]:
In [72]:
xarr_u.to(u.m/u.s)
Out[72]:
In [75]:
xarr_u = xarr * u.Unit(hdr['CUNIT1'])
xarr_u
Out[75]:
In [77]:
type(u.m), type(xarr_u)
Out[77]:
In [78]:
xarr_u.cgs
Out[78]:
In [80]:
xarr_u.si
Out[80]:
In [82]:
(500*u.M_jup/u.yr).to(u.g/u.s)
Out[82]:
In [83]:
from astropy import constants
In [85]:
# how many protons in earth?
constants.M_earth / constants.m_p
Out[85]:
In [89]:
xarr_u.unit.to_string(format='latex')
Out[89]:
In [91]:
xarr_u.unit.to_string()
Out[91]:
In [92]:
np.cos(5*u.rad)
Out[92]:
In [93]:
np.cos(45*u.deg)
Out[93]:
In [95]:
np.cos(45*u.arcsec)
Out[95]:
In [97]:
xarr_u
Out[97]:
In [98]:
data
Out[98]:
In [99]:
pl.plot(xarr_u, data)
Out[99]:
In [101]:
pl.plot(xarr_u, data)
pl.xlabel(xarr_u.unit.to_string(format='latex'), fontsize=40)
pl.ylabel(u.Unit(hdr['BUNIT']).to_string(format='latex'), fontsize=40)
Out[101]:
In [103]:
pl.plot(xarr_u, data)
pl.xlabel(xarr_u.unit.to_string(format='latex_inline'), fontsize=40)
pl.ylabel(u.Unit(hdr['BUNIT']).to_string(format='latex'), fontsize=40)
Out[103]:
In [105]:
xarr_u.to?
In [106]:
from astropy.wcs import WCS
In [107]:
mywcs = WCS(hdr)
In [108]:
mywcs
Out[108]:
In [139]:
xarr_pixels = np.arange(hdr['NAXIS1'])
xarr_pixels_1_indexed = np.arange(1, hdr['NAXIS1']+1)
# 0 tells you where to start counting:
# 1 for FITS (1,2,3...)
# 0 for python, c, etc. (0,1,2,....)
# 0 or 1 should correspond to the first element of xarr_pixels
xarr_wcs, = mywcs.wcs_pix2world(xarr_pixels, 0)
In [127]:
# CUNIT1, CRVAL1
mywcs.wcs.cunit[0], mywcs.wcs.crval[0]
Out[127]:
In [128]:
xarr_wcs_u = xarr_wcs * mywcs.wcs.cunit[0]
In [129]:
xarr_wcs_u
Out[129]:
In [124]:
x, = (1,)
In [125]:
x
Out[125]:
In [130]:
x,y,z = (1,2,3)
In [131]:
xarr_wcs_u == xarr_u
Out[131]:
In [132]:
xarr_u
Out[132]:
In [133]:
xarr_wcs_u
Out[133]:
In [136]:
xarr_u[1], xarr_wcs_u[1]
Out[136]:
In [137]:
xarr_u[1] - xarr_wcs_u[1]
Out[137]:
In [138]:
xarr_wcs_u[1] - xarr_u[1]
Out[138]:
In [140]:
xarr_wcs_u[0] - xarr_u[0]
Out[140]:
In [143]:
np.isclose?
In [144]:
np.abs(xarr_wcs_u-xarr_u) <= (1e-8*u.m/u.s) + 1e-5*np.abs(xarr_u)
Out[144]:
In [145]:
np.isclose(xarr_wcs_u, xarr_u, atol=1e-8*u.m/u.s)
Out[145]:
In [150]:
all(np.isclose(xarr_wcs_u, xarr_u, atol=1e-8*u.m/u.s))
Out[150]:
In [155]:
np.min((xarr_wcs_u - xarr_u))
Out[155]:
In [156]:
all(xarr_wcs_u == xarr_u)
Out[156]:
In [157]:
np.all(xarr_wcs_u == xarr_u)
Out[157]:
In [158]:
any(xarr_wcs_u == xarr_u)
Out[158]:
In [161]:
not_equal = xarr_wcs_u != xarr_u
In [163]:
not_equal
Out[163]:
In [167]:
np.where(not_equal)
Out[167]:
In [168]:
np.arange(4096)[not_equal]
Out[168]:
In [166]:
len(not_equal), np.count_nonzero(not_equal)
Out[166]:
In [162]:
xarr_u[not_equal]
Out[162]:
In [169]:
xarr_u[np.where(not_equal)]
Out[169]:
In [170]:
xarr_u[[1,5,7]]
Out[170]:
In [173]:
not_equal.shape, xarr_u[not_equal].shape, np.where(not_equal)[0].shape, xarr_u[np.where(not_equal)].shape
Out[173]:
In [177]:
from specutils.io import fits
In [179]:
!which pip
In [ ]:
# pip install https://github.com/astropy/astroquery/archive/master.zip
In [176]:
%%bash
pip install specutils
pip install pyspeckit
In [180]:
from specutils.io import fits
In [181]:
spec = fits.read_fits_spectrum1d('gbt_1d.fits')
In [186]:
spec
Out[186]:
In [194]:
import pyspeckit
In [195]:
sp = pyspeckit.Spectrum('gbt_1d.fits')
In [196]:
sp.plotter()
This notebook: https://goo.gl/1AM21P
In [199]:
np.linspace(-50, 50, 101)
Out[199]: