Commit fb155307 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

add table utils

parent 86a46ad6
Loading
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -12,6 +12,4 @@ Modified-History:
from ._module_docstr import ModuleHeader
from ._retry import retry
from .tempfile import randfile


from .table_utils import table_to_hdu, hdu_to_table
+27 −0
Original line number Diff line number Diff line
"""
Identifier:     csst_dadel/csst_dadel/utils.py
Name:           utils.py
Description:    Table utils.
Author:         Bo Zhang
Created:        2024-02-18
Modified-History:
    2024-02-18, Bo Zhang, implement utils for conversion between Table and BinTableHDU
"""
from astropy import table
from astropy.io import fits


def table_to_hdu(tbl: table.Table, name: str = "") -> fits.BinTableHDU:
    """Convert a table to a BinTableHDU."""
    # encode meta
    encoded_tbl = fits.connect._encode_mixins(tbl)
    # convert to HDU
    hdu = fits.table_to_hdu(encoded_tbl)
    # add HDU name
    hdu.name = name
    return hdu


def hdu_to_table(hdu: fits.BinTableHDU) -> table.Table:
    """Convert a BinTableHDU to a table."""
    return table.Table.read(hdu)