98 lines
4.1 KiB
Python
98 lines
4.1 KiB
Python
"""Nearest natal varga ascendant sign changes, using the chart's own frame."""
|
||
|
||
from datetime import datetime
|
||
from functools import lru_cache
|
||
|
||
from ayanamsa_utils import temporary_ayanamsa
|
||
from domain_calculation_service import swiss_ephemeris_lock
|
||
from jyotish_engine import _birth_hour_decimal, _sidereal_calculation_profile, swe
|
||
from varga import calc_varga
|
||
|
||
from .birth_precision_window import crosses_birth_window
|
||
|
||
|
||
_CAP_SECONDS = 240 * 60
|
||
_STEP_SECONDS = 30
|
||
|
||
|
||
def _first_change(longitude_at, sign_at, natal_sign, part_width, near, far):
|
||
"""Check both halves if a step can cross more than one sign boundary."""
|
||
delta = abs((longitude_at(far) - longitude_at(near) + 180) % 360 - 180)
|
||
if abs(far - near) > 1 and delta >= part_width:
|
||
mid = (near + far) // 2
|
||
return _first_change(longitude_at, sign_at, natal_sign, part_width, near, mid) or _first_change(
|
||
longitude_at, sign_at, natal_sign, part_width, mid, far,
|
||
)
|
||
if sign_at(far) == natal_sign:
|
||
return None
|
||
while abs(far - near) > 1:
|
||
mid = (near + far) // 2
|
||
if sign_at(mid) == natal_sign:
|
||
near = mid
|
||
else:
|
||
far = mid
|
||
return far
|
||
|
||
|
||
def _margin(longitude_at, division, direction, cap_seconds):
|
||
def sign_at(seconds):
|
||
longitude = longitude_at(seconds)
|
||
return int(longitude // 30) if division == 1 else calc_varga(longitude, division)["sign_idx"]
|
||
|
||
natal_sign = sign_at(0)
|
||
for seconds in range(_STEP_SECONDS, cap_seconds + 1, _STEP_SECONDS):
|
||
change = _first_change(
|
||
longitude_at, sign_at, natal_sign, 30 / division,
|
||
direction * (seconds - _STEP_SECONDS), direction * seconds,
|
||
)
|
||
if change is not None:
|
||
return round(abs(change) / 60, 3)
|
||
return None
|
||
|
||
|
||
def _margin_summary_zh(before: float | None, after: float | None, cap: float, window: dict) -> str:
|
||
earlier = (f"出生时间提前 {before:g} 分钟上升就换座" if before is not None
|
||
else f"出生时间提前 {cap:g} 分钟内上升未换座")
|
||
later = (f"推后 {after:g} 分钟才换座" if after is not None
|
||
else f"推后 {cap:g} 分钟内未换座")
|
||
crosses = crosses_birth_window(before, after, window)
|
||
conclusion = "区间内会换座" if crosses else "区间内不换座"
|
||
return f"{earlier};{later}。{conclusion}。"
|
||
|
||
|
||
def ascendant_margins(birth: dict, divisions: set[int], *, window: dict) -> dict:
|
||
"""Return distances in minutes, searching strictly past the report window."""
|
||
try:
|
||
natal = datetime.fromisoformat(f"{birth['date']}T{birth['time']}")
|
||
tz = float(str(birth["tz"]).removeprefix("UTC"))
|
||
latitude, longitude = float(birth["lat"]), float(birth["lon"])
|
||
ayanamsa = birth["ayanamsa_name"]
|
||
except (KeyError, TypeError, ValueError):
|
||
return {}
|
||
hour_ut = _birth_hour_decimal(natal.hour, natal.minute, natal.second) - tz
|
||
natal_jd = swe.julday(natal.year, natal.month, natal.day, hour_ut)
|
||
# A capped distance is outside the tolerance, including for windows of 240 minutes or more.
|
||
search_minutes = max(window["before_minutes"], window["after_minutes"])
|
||
cap_seconds = max(_CAP_SECONDS, (int(search_minutes * 60) // _STEP_SECONDS + 1) * _STEP_SECONDS)
|
||
|
||
@lru_cache(maxsize=None)
|
||
def longitude_at(seconds):
|
||
jd = natal_jd + seconds / 86_400
|
||
# Match compute_chart_data's apparent frame and equal-house Ascendant.
|
||
_, _, offset = _sidereal_calculation_profile(jd, "legacy")
|
||
cusps, _ = swe.houses(jd, latitude, longitude, b"A")
|
||
return (cusps[0] - offset) % 360
|
||
|
||
with swiss_ephemeris_lock(), temporary_ayanamsa(ayanamsa, swe):
|
||
rows = {}
|
||
cap = cap_seconds / 60
|
||
for division in sorted(divisions):
|
||
before = _margin(longitude_at, division, -1, cap_seconds)
|
||
after = _margin(longitude_at, division, 1, cap_seconds)
|
||
rows[f"D{division}"] = {
|
||
"before_minutes": cap if before is None else before,
|
||
"after_minutes": cap if after is None else after,
|
||
"summary_zh": _margin_summary_zh(before, after, cap, window),
|
||
}
|
||
return rows
|