"""
km_diagnose7.py
For the long-path problem cases, show exactly what location numbers
PA specifies for B-side, and what locations are actually in LO for those call signs.
"""
import os, pandas as pd

ULS_DIR = r"D:\FCC_ULS"

# Problem call signs from long paths
PROBLEM_B = ["WNTM247","WNTJ673","WNTA287","WQV68","WQV69","WQBD387",
             "WBD92","WBD93","WQBL390","KBB27","WPNJ908","KOE37","KOE39",
             "WEE492","WQW47","WNTJ675","WNTJ676","WNTC258"]

# Load PA for these B-side call signs (as receiver)
print("Loading PA.dat...")
pa = pd.read_csv(os.path.join(ULS_DIR,"PA.dat"), sep="|", header=None,
                 dtype=str, on_bad_lines="skip", encoding="latin-1")
pa.columns = list(range(len(pa.columns)))

# Show PA records where these are the receiver (col[16])
pa_rx = pa[pa[16].str.strip().isin(PROBLEM_B)]
print(f"PA records where problem calls are receiver: {len(pa_rx)}")
print(pa_rx[[4,6,7,8,9,16]].rename(columns={
    4:'tx_cs', 6:'tx_loc', 7:'tx_ant', 8:'rx_loc', 9:'rx_ant', 16:'rx_cs'
}).head(30).to_string())

# Load LO for these call signs - show all location numbers present
print("\nLoading LO.dat for problem B-side call signs...")
lo = pd.read_csv(os.path.join(ULS_DIR,"LO.dat"), sep="|", header=None,
                 dtype=str, on_bad_lines="skip", encoding="latin-1")
lo.columns = list(range(len(lo.columns)))
lo["call_sign"]      = lo[4].str.strip()
lo["loc_num"]        = lo[8].str.strip()
lo["loc_type"]       = lo[7].str.strip()
lo["city"]           = lo[12].str.strip()
lo["state"]          = lo[14].str.strip()
lo["lat_deg"]        = lo[19].str.strip()
lo["lon_deg"]        = lo[23].str.strip()

lo_prob = lo[lo["call_sign"].isin(PROBLEM_B)]
print(f"\nLO records for problem B call signs: {len(lo_prob)}")
print(lo_prob[["call_sign","loc_num","loc_type","city","state","lat_deg","lon_deg"]].to_string())
