#!/usr/bin/env python3 """Convert 3-line TLE file to SQL COPY format for pg_orrery benchmark.""" import sys def main(): lines = open(sys.argv[1]).read().strip().split('\n') print("-- CelesTrak active satellite catalog") print("-- Auto-generated for SP-GiST benchmark") print("CREATE TABLE IF NOT EXISTS bench_catalog (") print(" norad_id integer PRIMARY KEY,") print(" name text NOT NULL,") print(" tle tle NOT NULL") print(");") print("TRUNCATE bench_catalog;") print() count = 0 errors = 0 i = 0 while i + 2 < len(lines): name = lines[i].strip() line1 = lines[i + 1].strip() line2 = lines[i + 2].strip() # Validate TLE format if not line1.startswith('1 ') or not line2.startswith('2 '): i += 1 # skip and try to resync errors += 1 continue # Extract NORAD ID from line 1 (cols 3-7) try: norad_id = int(line1[2:7].strip()) except ValueError: i += 3 errors += 1 continue # Escape single quotes in name name_escaped = name.replace("'", "''") tle_str = line1 + '\n' + line2 print(f"INSERT INTO bench_catalog VALUES ({norad_id}, '{name_escaped}', E'{tle_str}') ON CONFLICT (norad_id) DO NOTHING;") count += 1 i += 3 print(f"\n-- Loaded {count} satellites ({errors} parse errors skipped)") if __name__ == '__main__': main()