Skip to content

Instantly share code, notes, and snippets.

@Miladiouss
Created April 28, 2022 00:02
Show Gist options
  • Save Miladiouss/64edc0865192be81bf814f154aec47d7 to your computer and use it in GitHub Desktop.
Save Miladiouss/64edc0865192be81bf814f154aec47d7 to your computer and use it in GitHub Desktop.
Convenient utilities for constructing an Astropy SIP model from a FITS header.
from astropy.modeling.polynomial import SIP
import itertools
from astropy.io.fits.header import Header
def get_sip_coeffs(hdr: Header , kind='A') -> dict:
"""Return a SIP polynomial coefficients for the selected
coefficient type: (A, B, AP, BP).
Parameters
----------
hdr : Header
header containing all polynomial coefficients up to the order.
kind : str, optional
'A', 'B', 'AP', or 'BP', by default 'A'
Returns
-------
sip: SIP
An astropy model for the sip polynomail
"""
sip_dict = {}
a_order = hdr[f'{kind}_ORDER']
for i, j in itertools.product(range(a_order+1), range(a_order+1)):
if 1 < i + j <= a_order:
key = f'{kind}_{i}_{j}'
sip_dict[key] = hdr[key]
return sip_dict
def get_sip_model(hdr: Header) -> SIP:
"""Construct a Astropy SIP model from the header
Parameters
----------
hdr : Header
Header containing SIP coefficients
Returns
-------
sip: SIP
A polynomial
"""
a_coeff = get_sip_coeffs(hdr, kind='A')
b_coeff = get_sip_coeffs(hdr, kind='B')
sip = SIP(
crpix=[hdr['crpix1'], hdr['crpix2']],
a_order=hdr['A_ORDER'],
b_order=hdr['B_ORDER'],
a_coeff=a_coeff,
b_coeff=b_coeff,
ap_order=None,
bp_order=None,
ap_coeff={},
bp_coeff={})
return sip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment