210 lines
10 KiB
Python
210 lines
10 KiB
Python
"""Appendix observations from the annual slow-transit scan and natal chart."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
from math import isfinite
|
|
|
|
from . import package as ap
|
|
|
|
SLOW_PAIR = ("Jupiter", "Saturn")
|
|
BHRIGU_YEAR_DAYS = 365.25
|
|
DOUBLE_TRANSIT = "jyotish_engine._check_pac"
|
|
SUDARSHANA = "sudarshana_chakra.calc_sudarshana_chakra"
|
|
BHRIGU_PADA = "bhrigu_pada_dasha.bhrigu_pada_dasha_full_report"
|
|
SUDARSHANA_NOTE = "已算本命三参照汇聚;逐年推进没有实现(上游也没有)"
|
|
BHRIGU_BOUNDARY = "不能据此确定事件时间或婚姻结论"
|
|
|
|
|
|
def _finite(value):
|
|
return isinstance(value, (int, float)) and not isinstance(value, bool) and isfinite(value)
|
|
|
|
|
|
def _sign_index(sign):
|
|
return ap.SIGNS.index(sign) if sign in ap.SIGNS else None
|
|
|
|
|
|
def _check_house(pac, asc, house, natal, sign_pairs):
|
|
sign = (asc + house - 1) % 12
|
|
lord = ap.SIGN_LORDS[ap.SIGNS[sign]]
|
|
lord_longitude = natal.get(lord)
|
|
if not _finite(lord_longitude):
|
|
return None
|
|
targets = (("house", None, sign), ("house_lord", lord, ap._sign_idx(lord_longitude)))
|
|
hits = {}
|
|
for signs in sign_pairs:
|
|
contacts = []
|
|
for target, planet, target_sign in targets:
|
|
effects = {
|
|
body: [{key: value for key, value in row.items() if key in ("type", "offset")}
|
|
for row in pac(body, position * 30 + 15, target_sign * 30 + 15, asc)
|
|
if row["type"] in ("Position", "Aspect")]
|
|
for body, position in zip(SLOW_PAIR, signs, strict=True)
|
|
}
|
|
if all(effects.values()):
|
|
contacts.append({"target": target, "planet": planet, "effects": effects})
|
|
if contacts:
|
|
hits[signs] = contacts
|
|
return hits
|
|
|
|
|
|
def _transit_segments(slow, start, end):
|
|
first = slow.get("positions_at_range_start") or {}
|
|
signs = {body: _sign_index((first.get(body) or {}).get("sign")) for body in SLOW_PAIR}
|
|
if None in signs.values():
|
|
return None
|
|
events = {}
|
|
for event in slow.get("events") or []:
|
|
body = event.get("body")
|
|
if body not in SLOW_PAIR or event.get("kind") != "ingress":
|
|
continue
|
|
instant = event["instant"]
|
|
if start["jd_ut"] <= instant["jd_ut"] < end["jd_ut"]:
|
|
events.setdefault(instant["jd_ut"], []).append(event)
|
|
segments = []
|
|
left = start
|
|
for jd, rows in sorted(events.items()):
|
|
instant = rows[0]["instant"]
|
|
if left["jd_ut"] < jd:
|
|
segments.append((left, instant, tuple(signs.values())))
|
|
for row in rows:
|
|
body = row["body"]
|
|
new_sign = _sign_index(row.get("to_sign"))
|
|
old_sign = _sign_index(row.get("from_sign"))
|
|
if new_sign is None or old_sign is None:
|
|
return None
|
|
if signs[body] != old_sign and not (jd == start["jd_ut"] and signs[body] == new_sign):
|
|
return None
|
|
signs[body] = new_sign
|
|
left = instant
|
|
segments.append((left, end, tuple(signs.values())))
|
|
return segments
|
|
|
|
|
|
def _windows(segments, hits):
|
|
windows = []
|
|
for start, end, signs in segments:
|
|
if not (contacts := hits.get(signs)):
|
|
continue
|
|
if not windows or windows[-1]["end"]["jd_ut"] != start["jd_ut"]:
|
|
windows.append({"start": start, "end": end, "segments": []})
|
|
current = windows[-1]
|
|
current["end"] = end
|
|
parts = current["segments"]
|
|
if parts and parts[-1]["contacts"] == contacts:
|
|
parts[-1]["end"] = end
|
|
else:
|
|
parts.append({"start": start, "end": end, "contacts": contacts})
|
|
return windows
|
|
|
|
|
|
def double_transit(natal, slow, start, end, domains):
|
|
if start is None or end is None or slow.get("status") != "computed":
|
|
return ap.block("blocked", DOUBLE_TRANSIT, reason="缺少报告区间或木星、土星的行运资料。")
|
|
asc = natal.get("lagna_sign_idx")
|
|
if type(asc) is not int or not 0 <= asc < 12:
|
|
return ap.block("blocked", DOUBLE_TRANSIT, reason="缺少本命上升,无法确定主题宫位。")
|
|
segments = _transit_segments(slow, start, end)
|
|
if segments is None:
|
|
return ap.block("blocked", DOUBLE_TRANSIT, reason="木星或土星的换座记录不完整。")
|
|
check = ap._engine()._check_pac
|
|
lons = natal.get("longitudes") or {}
|
|
sign_pairs = dict.fromkeys(signs for _, _, signs in segments)
|
|
per_house = {}
|
|
for house in dict.fromkeys(house for houses in domains.values() for house in houses):
|
|
hits = _check_house(check, asc, house, lons, sign_pairs)
|
|
per_house[house] = ({"house": house, "status": "blocked", "reason": "缺少本命宫主星经度。"}
|
|
if hits is None else {"house": house, "status": "computed", "windows": _windows(segments, hits)})
|
|
results = {
|
|
key: {"checked_houses": [house for house in houses if per_house[house]["status"] == "computed"],
|
|
"rows": [per_house[house] for house in houses]}
|
|
for key, houses in domains.items()
|
|
}
|
|
checked = sorted({house for result in results.values() for house in result["checked_houses"]})
|
|
complete = len(checked) == len(per_house)
|
|
return ap.block("computed" if checked else "blocked", DOUBLE_TRANSIT,
|
|
producer_status="computed" if complete else "partial" if checked else "blocked",
|
|
reason=None if complete else "有宫位缺少本命宫主星经度。",
|
|
checked_houses=checked, domains=results, profile="annual_whole_sign_house_and_lord")
|
|
|
|
|
|
def sudarshana(natal, domains):
|
|
lons = natal.get("longitudes") or {}
|
|
if not all(_finite(lons.get(name)) for name in ("Lagna", "Moon", "Sun")):
|
|
return ap.block("blocked", SUDARSHANA, reason="缺少本命上升、月亮或太阳经度。")
|
|
calc = ap._module("sudarshana_chakra").calc_sudarshana_chakra
|
|
planets = {name: lon for name, lon in lons.items() if name != "Lagna" and _finite(lon)}
|
|
per_house = {}
|
|
for house in dict.fromkeys(house for houses in domains.values() for house in houses):
|
|
raw = calc(planets, lons["Lagna"], house=house)
|
|
row = raw["specific_house"]
|
|
per_house[house] = {
|
|
"house": house,
|
|
**{ref: row[ref] for ref in ("asc_lagna", "moon_lagna", "sun_lagna")},
|
|
"convergence": [item for item in raw["convergence"]["items"] if item["house"] == house],
|
|
"composite_strength": row["composite_strength"],
|
|
"judgment": row["interpretation"],
|
|
}
|
|
return ap.block("computed", SUDARSHANA, producer_status="partial", profile="natal_three_reference_points", note=SUDARSHANA_NOTE,
|
|
domains={key: [per_house[house] for house in houses] for key, houses in domains.items()})
|
|
|
|
|
|
def bhrigu_pada(ctx, natal, start, end):
|
|
lons = natal.get("longitudes") or {}
|
|
if start is None or end is None:
|
|
return ap.block("blocked", BHRIGU_PADA, reason="缺少报告区间。")
|
|
if not _finite(lons.get("Moon")):
|
|
return ap.block("blocked", BHRIGU_PADA, reason="缺少本命月亮经度。")
|
|
if not _finite(lons.get("Lagna")):
|
|
return ap.block("blocked", BHRIGU_PADA, reason="缺少九分盘上升资料。")
|
|
varga = ap._module("varga")
|
|
planets = {name: lon for name, lon in lons.items() if name != "Lagna" and _finite(lon)}
|
|
d9 = varga.calc_all_vargas(planets, lons["Lagna"], [9]).get("D9_Navamsa") or {}
|
|
asc = _sign_index((d9.get("Ascendant") or {}).get("sign"))
|
|
if asc is None:
|
|
return ap.block("blocked", BHRIGU_PADA, reason="缺少九分盘上升资料。")
|
|
seventh_lord = ap.SIGN_LORDS[ap.SIGNS[(asc + 6) % 12]]
|
|
lord_sign = _sign_index((d9.get(seventh_lord) or {}).get("sign"))
|
|
if lord_sign is None or "Venus" not in d9:
|
|
return ap.block("blocked", BHRIGU_PADA, reason="缺少九分盘第七宫主星或金星的位置。")
|
|
positions = {name: d9[name]["sign_idx"] * 30 + d9[name]["degree_in_sign"] for name in planets if name in d9}
|
|
birth_jd = ap._jd_from_utc(ctx.birth_utc)
|
|
calc = ap._module("bhrigu_pada_dasha").bhrigu_pada_dasha_full_report
|
|
raw = calc(lons["Moon"], birth_jd, d9_7lord_sign=lord_sign, d9_planets=positions,
|
|
target_date_jd=ap._jd_from_utc(start))
|
|
|
|
def age_bands(key):
|
|
rows = []
|
|
for age in raw[key]:
|
|
# The producer emits integer-age samples. Show their annual buckets at the native year length.
|
|
left = ctx.birth_utc + timedelta(days=BHRIGU_YEAR_DAYS * age)
|
|
right = left + timedelta(days=BHRIGU_YEAR_DAYS)
|
|
if overlap := ap._clip(left, right, start, end, ctx.zone):
|
|
rows.append({"age": age, "sign": raw["dasha_sequence"][age]["progressed_sign"], **overlap})
|
|
return rows
|
|
|
|
coverage_end = ctx.birth_utc + timedelta(days=len(raw["dasha_sequence"]) * BHRIGU_YEAR_DAYS)
|
|
coverage = ap._clip(ctx.birth_utc, coverage_end, start, end, ctx.zone)
|
|
if coverage is None:
|
|
return ap.block("blocked", BHRIGU_PADA, reason="本期间超出原生引擎的年龄样点范围。")
|
|
return ap.block("computed", BHRIGU_PADA, producer_status="parameter_sensitive", profile="bhrigu_pada_approximate_progression",
|
|
domain="relationship", claim_boundary=BHRIGU_BOUNDARY, coverage=coverage,
|
|
coverage_status="partial" if ctx.birth_utc > start or coverage_end < end else "complete",
|
|
year_basis_days=BHRIGU_YEAR_DAYS, reference=raw["bcp_reference"],
|
|
marriage_windows=age_bands("marriage_windows"), venus_encounter_windows=age_bands("venus_encounter_windows"))
|
|
|
|
|
|
def build(ctx, natal, slow, start, end, domains):
|
|
def run(producer, calc):
|
|
try:
|
|
return calc()
|
|
except Exception: # noqa: BLE001 - isolate a failed appendix producer and keep its internals out of reports.
|
|
return ap.block("blocked", producer, reason="原生计算未返回可用的结果。")
|
|
|
|
return {
|
|
"double_transit": run(DOUBLE_TRANSIT, lambda: double_transit(
|
|
natal, slow, ap.instant(start, ctx.zone) if start else None, ap.instant(end, ctx.zone) if end else None, domains,
|
|
)),
|
|
"sudarshana": run(SUDARSHANA, lambda: sudarshana(natal, domains)),
|
|
"bhrigu_pada": run(BHRIGU_PADA, lambda: bhrigu_pada(ctx, natal, start, end)),
|
|
}
|