Commit 261f74b9 authored by LUSTIG Peter's avatar LUSTIG Peter

'added function to combine simulation results to one fits file'

parent 70aec6bf
from astropy.table import Table
from astropy.io.fits import PrimaryHDU, BinTableHDU, HDUList, open
from pathlib import Path
import astropy.units as u
import os
print(os.getcwd())
directory = 'montecarlo_results/'
outname = 'combined_tables.fits'
fnames = list(sorted(Path(directory).glob('*nsim50.fits')))
print(fnames)
hdul = [PrimaryHDU()]
n = 0
for i, ifile in enumerate(fnames):
_file = open(ifile)
_flux = u.Quantity(_file[0].header['influx'])
try:
_dt = Table.read(_file['DETECTED_SOURCES'])
hdul.append(BinTableHDU(data=_dt, name=('DETECTED_SOURCES{}'
.format(_flux))))
except KeyError:
print('no sources detected in {}'.format(ifile))
_ft = Table.read(_file['FAKE_SOURCES'])
hdul.append(BinTableHDU(data=_ft, name='FAKE_SOURCES{}'.format(_flux)))
n += 1
hdul[0].header['flux{}'.format(i)] = '{}'.format(_flux)
hdul[0].header['nfluxes'] = n
hdul = HDUList(hdul)
hdul.writeto(outname, overwrite=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment