from astroquery.vizier import Vizier
import numpy as np
import time, re
from astropy.table import vstack

Vizier.ROW_LIMIT = -1

def get(name):
    for attempt in range(6):
        try:
            return Vizier.get_catalogs(name)[0]
        except Exception as e:
            print("retry", name, attempt, e); time.sleep(8)
    raise RuntimeError("failed "+name)

t1 = get("J/MNRAS/504/2968/tablea1")
t2 = get("J/MNRAS/504/2968/tablea2")
print("tablea1", len(t1), "tablea2", len(t2))

# Inspect units/meta of distance columns
for c in ['plx','DistMode','DistHDIl','DistBJ','DistSH']:
    print(c, "unit=", t1[c].unit, "desc=", t1[c].info.description)

# Stack both - they share columns
common = [c for c in t1.colnames if c in t2.colnames]
t = vstack([t1[common], t2[common]])
print("combined", len(t))

# Check a sample of SpType and DistMode
for row in t[:8]:
    print(repr(str(row['SpType'])), row['DistMode'], row['plx'])

import pickle
with open("/orange/adamginsburg/galactic_plane_surveys/uchii_2026/als_combined.pkl","wb") as f:
    pickle.dump(t, f)
print("saved")
