Skip to content

Instantly share code, notes, and snippets.

@granttremblay
Last active May 23, 2018 17:44
Show Gist options
  • Save granttremblay/583715b60338d29a725f726750148afb to your computer and use it in GitHub Desktop.
Save granttremblay/583715b60338d29a725f726750148afb to your computer and use it in GitHub Desktop.
Stack Mainak's FITS tables quickly & flexibly
#!/usr/bin/env python
import glob
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.table import Table, vstack
'''
Make a list of FITS files.
Insist they are sorted alphanumerically, so that our final table
is in ascending order of HE number.
'''
infiles = sorted(glob.glob('./*name.fits'))
# Instantiate an empty dictionary from which we will make our final table
tabledict = {}
# Populate the dictionary with Astropy Tables from each FITS files
for file in infiles:
hdulist = fits.open(file)
bintab = hdulist[1].data
table = Table(bintab)
name = table['Source'][0]
tabledict[name] = table
# Create a master Astropy Table by vertically stacking a LIST of our dictionary 'values',
# which are all Astropy Table objects. Easy!
master_table = vstack(list(tabledict.values()))
# Write that new table to any file format you'd like, e.g.
master_table.write('master_table.fits', format='fits', overwrite=True)
master_table.write('master_table.txt', format='ascii', overwrite=True)
master_table.write('master_table.csv', format='csv', overwrite=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment