In [15]:
import numpy as np
# Set up matplotlib and use a nicer set of plot parameters
%config InlineBackend.rc = {}
import matplotlib
matplotlib.rc_file("templates/matplotlibrc")
import matplotlib.pyplot as plt
%matplotlib inline
In [16]:
from astropy.io import fits
##OR
#import pyfits as fits
We open the table and list its content.
In [17]:
SDSS_tab = fits.open('templates/SDSS_galaxy_tab.fits')
In [18]:
SDSS_tab.info()
I'm interested in reading EVENTS, which contains information about each X-ray photon that hit the detector.
To find out what information the table contains, I will print the column names.
In [19]:
print(SDSS_tab[1].columns)
Now I'll load the data into a separate variable.
In [20]:
SDSS_data = SDSS_tab[1].data
We can extract data from the table by referencing the column name.
For example, I'll make a histogram for the galaxy stellar masses of SDSS.
In [21]:
bins = numpy.arange(8,12,0.1)
mass_hist = plt.hist(SDSS_data['tot_mass'], bins=bins)
Next, we create a 2d plot of the specific SFR against the stellar mass for SDSS
In [22]:
mass = SDSS_data['tot_mass']
sSFR = SDSS_data['sSFR']
select_good = (mass>6.0) & (mass<12.0) & (sSFR>-80)
NBINS = (100,50)
mdensity, yedges, xedges = np.histogram2d(sSFR[select_good],mass[select_good], NBINS, range=[[-13,-9],[9.0,12.0]])
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.imshow(mdensity, extent=extent, interpolation='nearest', origin='lower')
plt.xlabel(r'log(stellar mass/[M${}_\odot$]) ')
plt.ylabel(r'log(sSFR/[M${}_\odot$Gyr${}^{-1}$]')
Out[22]:
In [23]:
SDSS_tab.close()
First, we create a short arrays that contain data to be stored.
In [24]:
objects = ['SDSS J1230+3291','SDSS 1109+3019']
mag = [15.6,numpy.nan]
z = [0.1,0.04]
flag = [0,-100]
Now, we create the columns for the table.
In [25]:
columns=[]
columns.append(fits.Column(name='Object',format='15A',unit='',array=objects))
columns.append(fits.Column(name='mag',format='E',unit='mag',array=mag))
columns.append(fits.Column(name='z',format='E',array=z))
columns.append(fits.Column(name='flag',format='I',null=-100,array=flag))
Next, we prepare and store the FITS file.
In [26]:
## before astropy v0.4
table = fits.new_table(columns)
## after astropy v0.4
##table = fits.BinTableHDU.from_columns(columns)
hdulist = fits.HDUList([fits.PrimaryHDU(),table])
In [27]:
hdulist.writeto('test_table.fits',clobber=True)
Now, we reopen the file to update some information.
In [28]:
HDU_update = fits.open('test_table.fits',mode='update')
tab_data=HDU_update[1].data
tab_header = HDU_update[1].header
tab_data['object'][1] = 'SDSS J0012-0010'
tab_data['mag'][1] = 15.0
tab_data['z'][1] = 0.06
tab_data['flag'][1] = 0
## ADD comment entries for each column that can be read by topcat
tab_header['TCOMM1'] = 'Object names'
tab_header['TCOMM2'] = 'Object magnitudes'
tab_header['TCOMM3'] = 'Object redshift'
tab_header['TCOMM4'] = 'Flag'
HDU_update.flush()
In [28]: