#!/usr/bin/env python3 """SSGEOS's "planetary conjunctions", rebuilt from NASA JPL Horizons positions (analysis 3 of PROTOCOL.md). python3 -I conjunctions.py daily hourly heliocentric ecliptic positions -> for each set of three bodies (the Sun and the eight planets), the smallest "deviation" in each UTC calendar day, 2011-01-01 to 2026-12-31 Standard library only. SSGEOS's data set (2011-2023-M7p-PC.pdf) describes its rule: "Degrees in the first field is the longitude relative to the first celestial (i.e. centric) body. Degrees in the second field is the longitude difference, i.e. the deviation (< 2°) of an exact conjunction", from "Heliocentric coordinates ... changed through vector rotation to arrive at planetary-centric coordinates". Its listings name each line-up once, by the end body nearer the Sun ("Earth-Jupiter-Uranus", "Saturn-Mars-Uranus", "Mercury-Sun-Neptune", "Sun-Earth-Saturn"). So for three bodies we take ecliptic longitudes (x, y only) as seen from each of them; the middle body is the one that sees the other two farthest apart; of the two end bodies, the centric one is the nearer to the Sun (the Sun itself if it is an end); the deviation is the difference in longitude between the other two as seen from the centric body. Times: Horizons gives TDB, about 69 s ahead of UTC; ignored. """ import datetime as dt import glob import itertools import json import math import os import sys BODIES = ["Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] CODES = {"199": "Mercury", "299": "Venus", "399": "Earth", "499": "Mars", "599": "Jupiter", "699": "Saturn", "799": "Uranus", "899": "Neptune"} TRIPLES = list(itertools.combinations(range(9), 3)) def read_positions(folder): """{body: {datetime (hour, TDB as UTC): (x, y)}} from the yearly Horizons files.""" pos = {b: {} for b in BODIES[1:]} for path in sorted(glob.glob(os.path.join(folder, "*_1h.txt"))): code = os.path.basename(path).split("_")[0] body = CODES[code] text = open(path, encoding="utf-8").read() block = text.split("$$SOE", 1)[1].split("$$EOE", 1)[0] for line in block.strip().splitlines(): f = [x.strip() for x in line.split(",")] # f[1] like "A.D. 2011-Jan-01 00:00:00.0000" t = dt.datetime.strptime(f[1].replace("A.D. ", "")[:17], "%Y-%b-%d %H:%M") pos[body][t] = (float(f[2]), float(f[3])) return pos def angle_diff(a, b): d = abs(a - b) % 360.0 return min(d, 360.0 - d) def deviations(xy): """xy: list of 9 (x, y), Sun first at (0, 0). Returns {triple index: deviation in degrees}.""" lon = [[0.0] * 9 for _ in range(9)] for i in range(9): xi, yi = xy[i] for j in range(9): if i != j: lon[i][j] = math.degrees(math.atan2(xy[j][1] - yi, xy[j][0] - xi)) rs = [math.hypot(*p) for p in xy] out = [] for (a, b, c) in TRIPLES: angs = {a: angle_diff(lon[a][b], lon[a][c]), b: angle_diff(lon[b][a], lon[b][c]), c: angle_diff(lon[c][a], lon[c][b])} middle = max(angs, key=angs.get) ends = [v for v in (a, b, c) if v != middle] centric = min(ends, key=lambda v: rs[v]) out.append(angs[centric]) return out def name(triple_index): a, b, c = TRIPLES[triple_index] return "-".join(BODIES[v] for v in (a, b, c)) def daily(folder, out_path): pos = read_positions(folder) hours = sorted(set.intersection(*(set(p) for p in pos.values()))) first, last = dt.date(2011, 1, 1), dt.date(2026, 12, 31) dmin = {} for t in hours: d = t.date() if d < first - dt.timedelta(days=3) or d > last: continue xy = [(0.0, 0.0)] + [pos[b][t] for b in BODIES[1:]] devs = deviations(xy) key = d.isoformat() cur = dmin.get(key) if cur is None: dmin[key] = devs else: dmin[key] = [min(u, v) for u, v in zip(cur, devs)] out = {"bodies": BODIES, "triples": [[BODIES[v] for v in tr] for tr in TRIPLES], "rule": __doc__.split("Standard library only.")[1].strip(), "days": {k: [round(x, 4) for x in v] for k, v in sorted(dmin.items())}} with open(out_path, "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh) print(len(dmin), "days,", len(hours), "hours read") if __name__ == "__main__": if sys.argv[1] == "daily": daily(sys.argv[2], sys.argv[3])