Skip to content

Commit

Permalink
read and write SENSFUNC spline order via Table FITS header, and allow…
Browse files Browse the repository at this point in the history
… a Table's header to be a simple dict
  • Loading branch information
chris-simpson committed Aug 13, 2020
1 parent ac66df6 commit 351e946
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 27 additions & 2 deletions astrodata/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ def new_imagehdu(data, header, name=None):


def table_to_bintablehdu(table, extname=None):
"""
Convert an astropy Table object to a BinTableHDU before writing to disk.
Suitable FITS header keywords describing the column datatypes are added,
and an existing header (either a Header object or a dict) is also added.
Parameters
----------
table: astropy.table.Table instance
the table to be converted to a BinTableHDU
extname: str
name to go in the EXTNAME field of the FITS header
Returns
-------
BinTableHDU
"""
add_header_to_table(table)
array = table.as_array()
header = table.meta['header'].copy()
Expand Down Expand Up @@ -268,8 +284,17 @@ def header_for_table(table):
columns.append(fits.Column(array=col.data, **descr))

fits_header = fits.BinTableHDU.from_columns(columns).header
if 'header' in table.meta:
fits_header = update_header(table.meta['header'], fits_header)
try:
existing_header = table.meta['header']
except KeyError:
pass
else:
if isinstance(existing_header, fits.Header):
fits_header = update_header(existing_header, fits_header)
else:
existing_fits_header = fits.Header()
existing_fits_header.update(existing_header)
fits_header = update_header(existing_fits_header, fits_header)
return fits_header


Expand Down
4 changes: 3 additions & 1 deletion geminidr/core/primitives_spect.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def calculateSensitivity(self, adinputs=None, **params):
knots, coeffs, degree = spline.tck
sensfunc = Table([knots * wave.unit, coeffs * zpt.unit],
names=('knots', 'coefficients'))
sensfunc.meta['header'] = {'ORDER': (3, 'Order of spline fit')}
ext.SENSFUNC = sensfunc
calculated = True

Expand Down Expand Up @@ -1752,7 +1753,8 @@ def fluxCalibrate(self, adinputs=None, **params):
pixel_sizes = abs(np.diff(all_waves[::2]))

# Reconstruct the spline and evaluate it at every wavelength
spline = BSpline(sensfunc['knots'].data, sensfunc['coefficients'].data, 3)
order = sensfunc.meta['header'].get('ORDER', 3)
spline = BSpline(sensfunc['knots'].data, sensfunc['coefficients'].data, order)
sens_factor = spline(waves.to(sensfunc['knots'].unit)) * sensfunc['coefficients'].unit
try: # conversion from magnitude/logarithmic units
sens_factor = sens_factor.physical
Expand Down

0 comments on commit 351e946

Please sign in to comment.