transit_date counted house_from_moon from the transit Moon when the caller gave no natal Moon; the natal reference is now only what the caller supplied. resolve_profile now refuses an unsupported ayanamsa name instead of passing it to the engine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
172 lines
6.5 KiB
Python
172 lines
6.5 KiB
Python
"""按需计算(一):指定日期的行运(#173,BUG-1816)。
|
|
|
|
占星师 2026-11-07 Q9 第一类:`transit --year --month --day --tz`,日期、地点、时区、
|
|
岁差、交点、位置口径**全部显式传入**。不调用 `cmd_transit`(它没有岁差/经纬度参数,
|
|
并且按当地正午算、把 tz=0 当成 8),也不碰 `transit_search.py`。
|
|
|
|
返回结果只有原始数字:九曜(含罗睺、计都)的恒星黄经、星座、度数、星宿与 pada、
|
|
是否逆行、日速度,以及按传进来的本命上升/月亮算的宫位;UTC 与当地时间都给。
|
|
解释留给模型,计算不给结论。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
# 引擎按顶层模块导入(scripts/ 在 sys.path 上),这里补一次,独立调用时也能用。
|
|
_SCRIPTS_DIR = Path(__file__).resolve().parents[1]
|
|
if str(_SCRIPTS_DIR) not in sys.path:
|
|
sys.path.insert(0, str(_SCRIPTS_DIR))
|
|
|
|
from . import contract
|
|
from .contract import OnDemandInputError
|
|
|
|
TOOL = 'transit_date'
|
|
PLANETS = ('Sun', 'Moon', 'Mars', 'Mercury', 'Jupiter', 'Venus', 'Saturn', 'Rahu', 'Ketu')
|
|
|
|
|
|
def _engine():
|
|
try: # scripts/ 作为包运行时
|
|
from scripts import jyotish_engine as engine
|
|
except ModuleNotFoundError: # pragma: no cover - 直接跑 scripts/
|
|
import jyotish_engine as engine # type: ignore[no-redef]
|
|
return engine
|
|
|
|
|
|
def _nakshatra(engine: Any, longitude: float) -> Dict[str, Any]:
|
|
try:
|
|
return dict(engine._nakshatra_fields(longitude)) # noqa: SLF001
|
|
except Exception: # noqa: BLE001 - 星宿层缺失只影响该字段,不影响黄经
|
|
return {}
|
|
|
|
|
|
def _house_from(reference_sign: Optional[str], sign: Optional[str], engine: Any) -> Optional[int]:
|
|
if not reference_sign or not sign:
|
|
return None
|
|
signs = getattr(engine, 'SIGNS', None) or []
|
|
if reference_sign not in signs or sign not in signs:
|
|
return None
|
|
return (signs.index(sign) - signs.index(reference_sign)) % 12 + 1
|
|
|
|
|
|
def run(body: Dict[str, Any]) -> Dict[str, Any]:
|
|
try:
|
|
common = contract.validate_common(body)
|
|
profile = contract.resolve_profile(body, common)
|
|
time_text = contract.parse_time(body.get('time'))
|
|
except OnDemandInputError as error:
|
|
return contract.blocked_envelope(tool=TOOL, body=body, error=error)
|
|
|
|
requested = dict(body)
|
|
time_source = 'caller_supplied' if time_text else 'local_noon_default'
|
|
local_hours = 12.0
|
|
if time_text:
|
|
hour, minute = (int(part) for part in time_text.split(':'))
|
|
local_hours = hour + minute / 60.0
|
|
|
|
if profile['position_mode'] not in contract.SUPPORTED_POSITION_MODES:
|
|
profile = {**profile, 'time_source': time_source}
|
|
return contract.build_envelope(
|
|
tool=TOOL,
|
|
requested=requested,
|
|
profile=profile,
|
|
status='blocked',
|
|
raw={},
|
|
gaps=[{'field': 'position_mode', 'reason': 'position_mode_unsupported'}],
|
|
time_source=time_source,
|
|
)
|
|
|
|
engine = _engine()
|
|
try:
|
|
# 用与本命计算同一套星历文件(不配路径时会退回 Moshier,月亮差约 0.5 角秒)。
|
|
if hasattr(engine, '_configure_ephemeris_path'):
|
|
engine._configure_ephemeris_path() # noqa: SLF001
|
|
jd = contract.jd_for_local(common['date'], local_hours, profile['tz'])
|
|
planets, ayanamsa = engine._calc_sidereal_planets_for_jd( # noqa: SLF001
|
|
jd,
|
|
node_mode=profile['node_mode'],
|
|
include_ketu=True,
|
|
ayanamsa_name=profile['ayanamsa'],
|
|
)
|
|
except Exception as error: # noqa: BLE001 - 失败只留缺口,不编任何日期
|
|
profile = {**profile, 'time_source': time_source}
|
|
return contract.build_envelope(
|
|
tool=TOOL,
|
|
requested=requested,
|
|
profile=profile,
|
|
status='blocked',
|
|
raw={},
|
|
gaps=[{'field': 'calculation', 'reason': f'engine_failed:{type(error).__name__}'}],
|
|
time_source=time_source,
|
|
)
|
|
|
|
if not planets:
|
|
profile = {**profile, 'time_source': time_source}
|
|
return contract.build_envelope(
|
|
tool=TOOL,
|
|
requested=requested,
|
|
profile=profile,
|
|
status='blocked',
|
|
raw={},
|
|
gaps=[{'field': 'calculation', 'reason': 'ephemeris_unavailable'}],
|
|
time_source=time_source,
|
|
)
|
|
|
|
natal = body.get('natal') if isinstance(body.get('natal'), dict) else {}
|
|
natal_asc = natal.get('ascendant_sign') or natal.get('ascendant')
|
|
# 验收修正:原来没给本命月亮时用当天行运月亮的星座顶替,house_from_moon 就成了从行运月亮数宫;
|
|
# 本命参考只认调用方给的,没给就是 null。
|
|
natal_moon = natal.get('moon_sign')
|
|
|
|
rows: List[Dict[str, Any]] = []
|
|
for name in PLANETS:
|
|
data = planets.get(name)
|
|
if not data:
|
|
continue
|
|
longitude = float(data.get('degree_raw', data.get('degree', 0.0)))
|
|
row = {
|
|
'planet': name,
|
|
'longitude': round(longitude, 6),
|
|
'sign': data.get('sign'),
|
|
'degree_in_sign': data.get('degree_in_sign'),
|
|
'retrograde': bool(data.get('retrograde')),
|
|
'speed': data.get('speed'),
|
|
'house_from_ascendant': _house_from(natal_asc, data.get('sign'), engine),
|
|
'house_from_moon': _house_from(natal_moon, data.get('sign'), engine),
|
|
}
|
|
row.update(_nakshatra(engine, longitude))
|
|
rows.append(row)
|
|
|
|
utc_hours = local_hours - profile['tz']
|
|
raw = {
|
|
'date': common['date'].strftime('%Y-%m-%d'),
|
|
'local_time': time_text or '12:00',
|
|
'utc_time': _hhmm(utc_hours),
|
|
'jd_ut': round(jd, 8),
|
|
'ayanamsa_degrees': round(float(ayanamsa or 0.0), 6),
|
|
'planets': rows,
|
|
'natal_reference': {
|
|
'ascendant_sign': natal_asc,
|
|
'moon_sign': natal_moon,
|
|
'source': 'caller_supplied' if natal_asc or natal_moon else 'not_provided',
|
|
},
|
|
}
|
|
# 本命盘是可选的补充:没给时宫位为 null、来源写明没给,但行运本身已经算完。
|
|
profile = {**profile, 'time_source': time_source}
|
|
return contract.build_envelope(
|
|
tool=TOOL,
|
|
requested=requested,
|
|
profile=profile,
|
|
status='executed',
|
|
raw=raw,
|
|
gaps=[],
|
|
time_source=time_source,
|
|
)
|
|
|
|
|
|
def _hhmm(hours: float) -> str:
|
|
total = int(round(hours * 60)) % (24 * 60)
|
|
return f'{total // 60:02d}:{total % 60:02d}'
|