Skip to content

Commit

Permalink
refactor fill_field
Browse files Browse the repository at this point in the history
  • Loading branch information
tnakazato committed Feb 3, 2025
1 parent 6699c93 commit cf81d36
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 63 deletions.
7 changes: 1 addition & 6 deletions src/nro45data/psw/ms2/filler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .antenna import fill_antenna
from .data_description import fill_data_description
from .feed import fill_feed
from .field import _fill_field_columns, _get_field_columns
from .field import fill_field
from .observation import _fill_observation_columns, _get_observation_columns
from .polarization import _fill_polarization_columns, _get_polarization_columns
from .main import fill_main
Expand All @@ -26,11 +26,6 @@
LOG = logging.getLogger(__name__)


def fill_field(msfile: str, hdu: BinTableHDU):
columns = _get_field_columns(hdu)
_fill_field_columns(msfile, columns)


def fill_observation(msfile: str, hdu: BinTableHDU):
columns = _get_observation_columns(hdu)
_fill_observation_columns(msfile, columns)
Expand Down
71 changes: 42 additions & 29 deletions src/nro45data/psw/ms2/filler/field.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING
from typing import Generator, TYPE_CHECKING

import numpy as np

from .._casa import open_table
from .._casa import datestr2mjd
from .._casa import convert_str_angle_to_rad
from .utils import fix_nrow_to
from .utils import fill_ms_table

if TYPE_CHECKING:
from astropy.io.fits.hdu.BinTableHDU import BinTableHDU

LOG = logging.getLogger(__name__)


def _get_field_columns(hdu: "BinTableHDU") -> dict:
def _get_field_row(hdu: BinTableHDU) -> Generator[dict, None, dict]:
"""Provide field row information.
Args:
hdu: NRO45m psw data in the form of BinTableHDU object.
Yields:
Dictionary containing field row information.
Returns:
Dictionary containing data-dependent column keywords.
"""
# NAME
field_name = hdu.header["OBJECT"].strip()
LOG.debug("field_name: %s", field_name)
Expand All @@ -25,8 +38,11 @@ def _get_field_columns(hdu: "BinTableHDU") -> dict:
# use start time of the observation
history_cards = hdu.header["HISTORY"]
start_time_card = [x for x in history_cards if x.startswith("NEWSTAR START-TIME")]
field_time = float(start_time_card[0].split("=")[-1].strip(" '"))
LOG.debug("field_time: %s", field_time)
sstr = start_time_card[0].split("=")[-1].strip(" '")
LOG.debug("sstr: %s", sstr)
datestr = sstr[0:4] + "/" + sstr[4:6] + "/" + sstr[6:8] + " " + sstr[8:10] + ":" + sstr[10:12] + ":" + sstr[12:14]
LOG.debug("formatted sstr: %s", datestr)
field_time = datestr2mjd(datestr) - 9 * 3600

# NUM_POLY
num_poly = 0
Expand Down Expand Up @@ -62,37 +78,34 @@ def _get_field_columns(hdu: "BinTableHDU") -> dict:
# FLAG_ROW
flag_row = False

columns = {
row = {
"NAME": field_name,
"CODE": field_code,
"TIME": field_time,
"NUM_POLY": num_poly,
"FIELD_EPOCH": field_epoch,
"DELAY_DIR": delay_dir,
"PHASE_DIR": phase_dir,
"REFERENCE_DIR": reference_dir,
"SOURCE_ID": source_id,
"FLAG_ROW": flag_row,
}

return columns


def _fill_field_columns(msfile: str, columns: dict):
with open_table(msfile + "/FIELD", read_only=False) as tb:
fix_nrow_to(1, tb)

tb.putcell("NAME", 0, columns["NAME"])
tb.putcell("CODE", 0, columns["CODE"])
tb.putcell("TIME", 0, columns["TIME"])
tb.putcell("NUM_POLY", 0, columns["NUM_POLY"])
tb.putcell("DELAY_DIR", 0, columns["DELAY_DIR"])
colkeywords = tb.getcolkeywords("DELAY_DIR")
colkeywords["MEASINFO"]["Ref"] = columns["FIELD_EPOCH"]
tb.putcolkeywords("DELAY_DIR", colkeywords)
tb.putcell("PHASE_DIR", 0, columns["PHASE_DIR"])
tb.putcolkeywords("PHASE_DIR", colkeywords)
tb.putcell("REFERENCE_DIR", 0, columns["REFERENCE_DIR"])
tb.putcolkeywords("REFERENCE_DIR", colkeywords)
tb.putcell("SOURCE_ID", 0, columns["SOURCE_ID"])
tb.putcell("FLAG_ROW", 0, columns["FLAG_ROW"])
yield row

column_keywords = {
"DELAY_DIR": {"MEASINFO": {"Ref": field_epoch}},
"PHASE_DIR": {"MEASINFO": {"Ref": field_epoch}},
"REFERENCE_DIR": {"MEASINFO": {"Ref": field_epoch}}
}

return column_keywords # noqa


def fill_field(msfile: str, hdu: BinTableHDU):
"""Fill MS FIELD table.
Args:
msfile: Name of MS file.
hdu: NRO45m psw data in the form of BinTableHDU object.
"""
fill_ms_table(msfile, hdu, "FIELD", _get_field_row)
61 changes: 33 additions & 28 deletions src/nro45data/psw/ms2/filler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ def fill_ms_table(
msfile: str,
hdu: BinTableHDU,
table_name: str,
row_generator: Callable,
column_keywords: Optional[dict[str, dict[str, Any]]] = None
row_generator: Callable
):
"""Fill MS table.
Expand All @@ -291,36 +290,42 @@ def fill_ms_table(
hdu: NRO45m psw data in the form of BinTableHDU object.
table_name: Name of subtable or "MAIN" for MAIN table.
row_generator: Generator to yield table row.
column_keywords: Optional information to update column
keywords. Defaults to None.
Key is column name and value is a dictionary of
column keywords to be updated.
"""
if table_name.upper() == "MAIN":
table_path = msfile
else:
table_path = f"{msfile}/{table_name.upper()}"

with open_table(table_path, read_only=False) as tb:
# update table column keywords
if column_keywords is None:
column_keywords = {}

for colname, colkeywords_new in column_keywords.items():
colkeywords = tb.getcolkeywords(colname)
for key, value_new in colkeywords_new.items():
if key in colkeywords and isinstance(value_new, dict):
colkeywords[key].update(value_new)
else:
colkeywords[key] = value_new
tb.putcolkeywords(colname, colkeywords)

# fill table rows
for row_id, row in enumerate(row_generator(hdu)):
if tb.nrows() <= row_id:
tb.addrows(tb.nrows() - row_id + 1)

for key, value in row.items():
LOG.debug("row %d key %s", row_id, key)
tb.putcell(key, row_id, value)
LOG.debug("%s table %d row %s", table_name, row_id, row)
# iterator should provide row dictionary in order,
# and then, if necessary, column keywords dictionary
# should be returned additionally
iterator = enumerate(row_generator(hdu))
try:
# fill table rows
while True:
row_id, row = next(iterator)
if tb.nrows() <= row_id:
tb.addrows(tb.nrows() - row_id + 1)

for key, value in row.items():
LOG.debug("row %d key %s", row_id, key)
tb.putcell(key, row_id, value)
LOG.debug("%s table %d row %s", table_name, row_id, row)

except StopIteration as s:
# update column keywords if necessary
column_keywords = s.value
LOG.debug("return value: %s", s.value)
if isinstance(column_keywords, dict):
LOG.debug("column_keywords: %s", column_keywords)
for colname, colkeywords_new in column_keywords.items():
colkeywords = tb.getcolkeywords(colname)
for key, value_new in colkeywords_new.items():
if key in colkeywords and isinstance(value_new, dict):
LOG.debug("updating column keyword %s: %s, %s", colname, key, value_new)
colkeywords[key].update(value_new)
else:
LOG.debug("adding column keyword %s: %s, %s", colname, key, value_new)
colkeywords[key] = value_new
tb.putcolkeywords(colname, colkeywords)

0 comments on commit cf81d36

Please sign in to comment.