Commit 7fd23f1e authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

add add_meta_to_table

parent a9d66645
Loading
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ from copy import deepcopy
from typing import Optional

from astropy.io import fits
from astropy import table

from .time import now

@@ -332,3 +333,27 @@ def get_proc_info(**kwargs):
        cards.append((k, v, ""))
    header = fits.Header(cards=cards)
    return reformat_header(header, comment="PROCESSING INFORMATION")


def add_meta_to_table(t: table.Table, meta: dict) -> table.Table:
    """Add meta information to a table.

    Parameters
    ----------
    t : Table
        Table.
    meta : dict
        Meta information.
    """
    # determine number of rows
    n_rows = len(t)
    # extract subset of meta information
    sub_meta = {
        "healpix": meta["healpix"],
        "data_uuid": meta["data_uuid"],
    }
    # create meta table
    t_meta = table.Table([sub_meta] * n_rows, dtype=("i4", "U36"))
    # add meta columns to table
    t.add_columns(t_meta.columns)
    return t
+8 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import os
import unittest

from astropy.io import fits
from astropy import table

from csst_common.io import (
    append_header,
@@ -22,6 +23,7 @@ from csst_common.io import (
    generate_meta,
    append_meta,
    extract_meta,
    add_meta_to_table,
)


@@ -143,3 +145,9 @@ class TestFitsHeaderOps(unittest.TestCase):
        self.assertIn("META", set(hdulist[0].header.keys()))
        meta = extract_meta(hdulist)
        self.assertEqual(meta["obs_id"], "123456")

    def test_add_meta_to_table(self):
        t = table.Table([{"a": 1, "b": 2}] * 5)
        meta = generate_meta(obs_id="123456")
        t = add_meta_to_table(t, meta)
        self.assertEqual(set(t.colnames), ("a", "b", "healpix", "data_uuid"))