generated from microsoft/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from tnakazato/refactor-filler-feed
refactor: filler for FEED table
- Loading branch information
Showing
5 changed files
with
204 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,119 @@ | ||
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 .utils import fix_nrow_to, get_array_configuration | ||
from .._casa import datestr2mjd | ||
from .utils import get_array_configuration, fill_ms_table | ||
|
||
if TYPE_CHECKING: | ||
from astropy.io.fits.hdu.BinTableHDU import BinTableHDU | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def _get_feed_columns(hdu: "BinTableHDU") -> dict: | ||
array_conf = get_array_configuration(hdu) | ||
num_beam = len(np.unique([v[1] for v in array_conf.values()])) | ||
|
||
antenna_id = np.arange(num_beam, dtype=int) | ||
|
||
feed_id = np.zeros(num_beam, dtype=int) | ||
|
||
spw_id = np.ones(num_beam, dtype=int) * -1 | ||
def _get_feed_row(hdu: BinTableHDU) -> Generator[dict, None, None]: | ||
"""Provide feed row information. | ||
num_receptors = np.ones(num_beam, dtype=int) * 2 | ||
Args: | ||
hdu: NRO45m psw data in the form of BinTableHDU object. | ||
beam_id = np.zeros(num_beam, dtype=int) | ||
|
||
history_cards = hdu.header["HISTORY"] | ||
start_time_card = [x for x in history_cards if x.startswith("NEWSTAR START-TIME")] | ||
start_time = float(start_time_card[0].split("=")[-1].strip(" '")) | ||
end_time_card = [x for x in history_cards if x.startswith("NEWSTAR END-TIME")] | ||
end_time = float(end_time_card[0].split("=")[-1].strip(" '")) | ||
feed_time = np.ones(num_beam, dtype=int) * (end_time + start_time) / 2 | ||
feed_interval = np.ones(num_beam, dtype=int) * (end_time - start_time) | ||
|
||
beam_offset = [np.zeros((2, 2), dtype=float) for _ in range(num_beam)] | ||
Yields: | ||
Dictionary containing feed row information. | ||
""" | ||
array_conf = get_array_configuration(hdu) | ||
num_beam = len(np.unique([v[1] for v in array_conf.values()])) | ||
|
||
rx_str = np.unique(hdu.data["RX"]) | ||
if rx_str[0][-1] in ("H", "V"): | ||
poltp_str = np.unique(hdu.data["POLTP"]) | ||
if np.all(poltp_str == ""): | ||
pol_spec = rx_str[0].strip()[-1] | ||
else: | ||
pol_spec = poltp_str[0].strip()[-1] | ||
if pol_spec in ("H", "V"): | ||
_pol_type = ["X", "Y"] | ||
elif rx_str[0][-1] in ("R", "L"): | ||
elif pol_spec in ("R", "L"): | ||
_pol_type = ["R", "L"] | ||
else: | ||
# fall back to ["X", "Y"] | ||
_pol_type = ["X", "Y"] | ||
pol_type = [np.array(_pol_type) for _ in range(num_beam)] | ||
|
||
pol_response = [np.zeros((2, 2), dtype=complex) for _ in range(num_beam)] | ||
|
||
feed_position = [np.zeros(3, dtype=float) for _ in range(num_beam)] | ||
|
||
receptor_angle = [np.zeros(2, dtype=float) for _ in range(num_beam)] | ||
|
||
columns = { | ||
"ANTENNA_ID": antenna_id, | ||
"FEED_ID": feed_id, | ||
"SPECTRAL_WINDOW_ID": spw_id, | ||
"TIME": feed_time, | ||
"INTERVAL": feed_interval, | ||
"NUM_RECEPTORS": num_receptors, | ||
"BEAM_ID": beam_id, | ||
"BEAM_OFFSET": beam_offset, | ||
"POLARIZATION_TYPE": pol_type, | ||
"POL_RESPONSE": pol_response, | ||
"POSITION": feed_position, | ||
"RECEPTOR_ANGLE": receptor_angle, | ||
} | ||
|
||
return columns | ||
|
||
|
||
def _fill_feed_columns(msfile: str, columns: dict): | ||
with open_table(msfile + "/FEED", read_only=False) as tb: | ||
num_feed = len(columns["ANTENNA_ID"]) | ||
fix_nrow_to(num_feed, tb) | ||
|
||
tb.putcol("ANTENNA_ID", columns["ANTENNA_ID"]) | ||
tb.putcol("FEED_ID", columns["FEED_ID"]) | ||
tb.putcol("SPECTRAL_WINDOW_ID", columns["SPECTRAL_WINDOW_ID"]) | ||
tb.putcol("TIME", columns["TIME"]) | ||
tb.putcol("INTERVAL", columns["INTERVAL"]) | ||
tb.putcol("NUM_RECEPTORS", columns["NUM_RECEPTORS"]) | ||
tb.putcol("BEAM_ID", columns["BEAM_ID"]) | ||
|
||
for i in range(num_feed): | ||
tb.putcell("BEAM_OFFSET", i, columns["BEAM_OFFSET"][i]) | ||
tb.putcell("POLARIZATION_TYPE", i, columns["POLARIZATION_TYPE"][i]) | ||
tb.putcell("POL_RESPONSE", i, columns["POL_RESPONSE"][i]) | ||
tb.putcell("POSITION", i, columns["POSITION"][i]) | ||
tb.putcell("RECEPTOR_ANGLE", i, columns["RECEPTOR_ANGLE"][i]) | ||
|
||
for i in range(num_beam): | ||
# ANTENNA_ID | ||
antenna_id = i | ||
|
||
# FEED_ID | ||
feed_id = 0 | ||
|
||
# SPECTRAL_WINDOW_ID | ||
spw_id = -1 | ||
|
||
# NUM_RECEPTORS | ||
num_receptors = 2 | ||
|
||
# BEAM_ID | ||
beam_id = 0 | ||
|
||
# TIME and INTERVAL | ||
history_cards = hdu.header["HISTORY"] | ||
start_time_card = [x for x in history_cards if x.startswith("NEWSTAR START-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) | ||
start_time = datestr2mjd(datestr) - 9 * 3600 | ||
end_time_card = [x for x in history_cards if x.startswith("NEWSTAR END-TIME")] | ||
estr = end_time_card[0].split("=")[-1].strip(" '") | ||
LOG.debug("estr: %s", estr) | ||
datestr = estr[0:4] + "/" + estr[4:6] + "/" + estr[6:8] + " " + \ | ||
estr[8:10] + ":" + estr[10:12] + ":" + estr[12:14] | ||
LOG.debug("formatted estr: %s", datestr) | ||
end_time = datestr2mjd(datestr) - 9 * 3600 | ||
|
||
feed_time = (end_time + start_time) / 2 | ||
feed_interval = end_time - start_time | ||
|
||
# BEAM_OFFSET | ||
beam_offset = np.zeros((2, 2), dtype=float) | ||
|
||
# POLARIZATION_TYPE | ||
pol_type = np.array(_pol_type) | ||
|
||
# POL_RESPONSE | ||
pol_response = np.zeros((2, 2), dtype=complex) | ||
|
||
# POSITION | ||
feed_position = np.zeros(3, dtype=float) | ||
|
||
# RECEPTOR_ANGLE | ||
receptor_angle = np.zeros(2, dtype=float) | ||
|
||
row = { | ||
"ANTENNA_ID": antenna_id, | ||
"FEED_ID": feed_id, | ||
"SPECTRAL_WINDOW_ID": spw_id, | ||
"TIME": feed_time, | ||
"INTERVAL": feed_interval, | ||
"NUM_RECEPTORS": num_receptors, | ||
"BEAM_ID": beam_id, | ||
"BEAM_OFFSET": beam_offset, | ||
"POLARIZATION_TYPE": pol_type, | ||
"POL_RESPONSE": pol_response, | ||
"POSITION": feed_position, | ||
"RECEPTOR_ANGLE": receptor_angle, | ||
} | ||
|
||
yield row | ||
|
||
|
||
def fill_feed(msfile: str, hdu: BinTableHDU): | ||
"""Fill MS FEED table. | ||
Args: | ||
msfile: Name of MS file. | ||
hdu: NRO45m psw data in the form of BinTableHDU object. | ||
""" | ||
fill_ms_table(msfile, hdu, "FEED", _get_feed_row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters