def add_time_column(fitsfile, blockname="EVENTS"):
"""
Add a time column to the specified block of the input fits file.
The time data are generated with a uniform distribution
between TSTART and TSTOP.
Return:
A fits object with the new time column.
"""
if isinstance(fitsfile, str):
fitsfile = fits.open(fitsfile)
table = fitsfile[blockname]
tstart = table.header["TSTART"]
tstop = table.header["TSTOP"]
counts = len(table.data)
time_data = np.random.uniform(tstart, tstop, counts)
time_col = fits.Column(name="time", format="1D", unit="s", array=time_data)
# NOTE: append the new time column to the *last*!
# Otherwise the TLMIN??/TLMAX?? keyword pairs, which record the
# minimum/maximum values of corresponding columns, will become
# *out of order*. Therefore the output FITS file causes weird problems
# with DS9 and DM tools.
newtable = fits.BinTableHDU.from_columns(
table.columns + fits.ColDefs([time_col]))
fitsfile[blockname].data = newtable.data
# update header
fitsfile[blockname].header.update(newtable.header)
return fitsfile
评论列表
文章目录