Files
Jyotisha/scripts/tajika_mudda.py
T
ClaudeandClaude Opus 5.5 178e6d6216 feat(annual): sync Tajika named-yoga inputs, special points, Mudda profiles and Harsha with the astrologer's upstream
产品 2026-11-07 与占星师上游年运同步 (yinduzhanxing 5fb8379c):
- tajika_named_yogas gets upstream's full producer inputs (annual house,
  Harsha+Panchavargiya grade, Tajika rasi relation, lagnesh/karyesh); listed
  only, not judgment factors (3509ccd7). combined_strength stays blocked.
- annual_special_points: annual AL, Bhrigu Bindu, Gulika (upstream port).
- mudda_profiles: moon_at_birth_time (default, BUG-1216 ruling),
  moon_in_varshaphala, without_dasha_balance (upstream pl9-export profile).
- Harsha Bala: PVR 28.3 four-source native rule (upstream native function);
  PyJHora never runs.
- Carried into annual package blocks, report facts year overview, the
  consultation annual layer and nativeAnnualKeys; golden recaptured; English
  reader titles the special-points section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-07 16:56:46 +08:00

267 lines
13 KiB
Python

#!/usr/bin/env python3
"""Annual Mudda Dasha from the natal Moon nakshatra (BUG-1216).
Astrologer round-5 ruling 2026-10-03, Q7: the first Mudda lord is
(completed years + natal Moon nakshatra number 1..27 - 2) mod 9, remainder
1 Sun, 2 Moon, 3 Mars, 4 Rahu, 5 Jupiter, 6 Saturn, 7 Mercury, 8 Ketu,
0 Venus. It does not depend on the year lord.
The rule itself is `tajika.calc_mudda_with_dasha_balance`, ported verbatim
from upstream yinduzhanxing c7117f92. This module is the single product
entry both annual producers (`solar_return.solar_return_full_report` and
`jyotish_engine.cmd_tajika`) call, so `annual_tajika_pack` compares two runs
of the same rule. What it adds on top of the upstream function:
- the balance comes from the natal Moon (`moon_at_birth_time`);
- the year length is named: `solar_year_365_25` (product default, the
upstream default) or `savana_360` (360-day year, research only);
- `periods` are cropped to the annual window [annual_start, annual_end) and
carry `months` (share of 12) for the existing month-window consumers; the
upstream ten-group surface (the last group runs past the year end) stays
in `periods_full`. Sub-periods already computed by
`calc_mudda_with_dasha_balance` are copied as `antardasha_periods` and
cropped to the same annual window. Day rows are not added here.
`annual_mudda_profiles` (产品 2026-11-07 与占星师上游年运同步) adds upstream's
two other balance conventions as labelled alternatives next to the default:
`moon_in_varshaphala` (same first lord, balance from the annual-chart Moon)
and `without_dasha_balance` (upstream's PL9 p135 / pl9-export profile: no
balance, first lord = the Varsha lord). The default stays
`moon_at_birth_time` because the astrologer's round-5 Q7 ruling (BUG-1216)
fixes the first lord from the natal nakshatra, independent of the year lord.
The legacy `tajika.calc_mudda_dasha` (first lord = year lord) is no longer
called from any product path.
"""
from __future__ import annotations
from datetime import datetime
from typing import Any, Dict, Optional
try: # scripts/ on sys.path
from tajika import calc_mudda_with_dasha_balance, calc_mudda_without_dasha_balance
except ImportError: # pragma: no cover - package import
from scripts.tajika import calc_mudda_with_dasha_balance, calc_mudda_without_dasha_balance
BALANCE_PROFILE = "moon_at_birth_time"
YEAR_LENGTHS = {"solar_year_365_25": 365.25, "savana_360": 360.0}
DEFAULT_YEAR_LENGTH = "solar_year_365_25"
SOURCE = "tajika.calc_mudda_with_dasha_balance (upstream yinduzhanxing c7117f92)"
RULE = "astrologer_round5_q7_completed_years_plus_natal_nakshatra_minus_two_mod_nine"
RULE_TEXT = "起运星 =(已满年数 + 出生月宿序号 − 2)mod 9,与年主无关"
RULE_TEXT_EN = "First lord = (completed years + natal Moon nakshatra number - 2) mod 9; independent of the year lord"
def _parse(value: str) -> datetime:
return datetime.fromisoformat(str(value))
def annual_mudda_dasha(
*,
annual_start: Optional[datetime],
completed_years: Optional[int],
natal_moon_longitude: Optional[float],
year_length: str = DEFAULT_YEAR_LENGTH,
) -> Dict[str, Any]:
"""Mudda Dasha for one Varsha year, anchored at the solar-return instant."""
if year_length not in YEAR_LENGTHS:
return {"status": "blocked", "reason": "unknown_year_length", "year_length_basis": year_length}
if completed_years is None or natal_moon_longitude is None:
return {"status": "blocked", "reason": "completed_years_and_natal_moon_required_for_mudda",
"balance_profile": BALANCE_PROFILE}
raw = calc_mudda_with_dasha_balance(
annual_start=annual_start,
completed_years=int(completed_years),
natal_moon_longitude=natal_moon_longitude,
balance_moon_longitude=natal_moon_longitude,
balance_profile=BALANCE_PROFILE,
annual_duration_days=YEAR_LENGTHS[year_length],
)
if raw.get("status") == "blocked":
return {**raw, "year_length_basis": year_length, "source": SOURCE}
start = _parse(raw["annual_start"])
end = _parse(raw["annual_end"])
year_days = float(raw["annual_duration_days"])
periods = []
for row in raw.get("periods") or []:
p_start = max(_parse(row["start"]), start)
p_end = min(_parse(row["end"]), end)
if p_start >= p_end:
continue
days = (p_end - p_start).total_seconds() / 86400.0
periods.append({
"order": len(periods) + 1,
"lord": row["lord"],
"months": round(days / year_days * 12.0, 2),
"duration_days": days,
"full_duration_days": row.get("full_duration_days"),
"start": p_start.isoformat(sep=" "),
"end": p_end.isoformat(sep=" "),
})
antardasha_periods = []
for row in raw.get("antardasha_periods") or []:
if not isinstance(row, dict) or not row.get("start") or not row.get("end"):
continue
p_start = max(_parse(row["start"]), start)
p_end = min(_parse(row["end"]), end)
if p_start >= p_end:
continue
antardasha_periods.append({
"main_order": row.get("main_order"),
"sub_order": row.get("sub_order"),
"main_lord": row.get("main_lord"),
"sub_lord": row.get("sub_lord"),
"duration_days": (p_end - p_start).total_seconds() / 86400.0,
"start": p_start.isoformat(sep=" "),
"end": p_end.isoformat(sep=" "),
})
return {
# The first-lord rule is the astrologer's round-5 ruling; dates are
# not externally replayed (upstream keeps its own parameter_sensitive).
"status": "partial_verified",
"upstream_status": raw.get("status"),
"starting_lord": raw.get("starting_lord"),
"starting_lord_basis": raw.get("starting_lord_basis"),
"rule": RULE,
"rule_text": RULE_TEXT,
"rule_text_en": RULE_TEXT_EN,
"annual_start": raw.get("annual_start"),
"annual_end": raw.get("annual_end"),
"annual_duration_days": year_days,
"year_length_basis": year_length,
"completed_years": raw.get("completed_years"),
"natal_moon_longitude": raw.get("natal_moon_longitude"),
"natal_moon_nakshatra_number": raw.get("natal_moon_nakshatra_number"),
"balance_profile": raw.get("balance_profile"),
"balance_fraction_remaining": raw.get("balance_fraction_remaining"),
"periods": periods,
"antardasha_periods": antardasha_periods,
"periods_full": raw.get("periods"),
"independent_of_year_lord": True,
"source": SOURCE,
}
# ── 产品 2026-11-07 与占星师上游年运同步:三种余额口径并列 ─────────────────────
DEFAULT_PROFILE = BALANCE_PROFILE
PROFILE_ORDER = ("moon_at_birth_time", "moon_in_varshaphala", "without_dasha_balance")
PROFILE_LABELS = {
# Labels follow upstream annual_report_table_groups.py / jyotish_engine.py.
"moon_at_birth_time": ("出生月亮口径", "Birth Moon profile (Moon at birth time)"),
"moon_in_varshaphala": ("年盘月亮口径", "Annual Moon profile (Moon in Varshaphala)"),
"without_dasha_balance": ("不扣余运口径", "No birth-balance profile (without Dasha balance)"),
}
PROFILE_RULES = {
"moon_at_birth_time": "起运星按占星师第五轮问 7;首段余额按出生时月亮在月宿内的剩余比例",
"moon_in_varshaphala": "起运星按占星师第五轮问 7;首段余额按年盘月亮在月宿内的剩余比例",
"without_dasha_balance": "不计余额,从年主起排九段(上游 PL9 p135 / pl9 导出口径;与第五轮问 7「与年主无关」不一致)",
}
DEFAULT_BASIS = "astrologer_round5_q7_bug_1216"
DEFAULT_BASIS_TEXT = "默认用出生月亮口径:占星师第五轮问 7(BUG-1216)裁定起运星由出生月宿推算、与年主无关"
def _crop(raw: Dict[str, Any], start: datetime, end: datetime) -> tuple[list, list]:
year_days = (end - start).total_seconds() / 86400.0
periods = []
for row in raw.get("periods") or []:
p_start = max(_parse(row["start"]), start)
p_end = min(_parse(row["end"]), end)
if p_start >= p_end:
continue
days = (p_end - p_start).total_seconds() / 86400.0
periods.append({
"order": len(periods) + 1,
"lord": row["lord"],
"months": round(days / year_days * 12.0, 2),
"duration_days": days,
"full_duration_days": row.get("full_duration_days"),
"start": p_start.isoformat(sep=" "),
"end": p_end.isoformat(sep=" "),
})
antardasha = []
for row in raw.get("antardasha_periods") or []:
if not isinstance(row, dict) or not row.get("start") or not row.get("end"):
continue
p_start = max(_parse(row["start"]), start)
p_end = min(_parse(row["end"]), end)
if p_start >= p_end:
continue
antardasha.append({
"main_order": row.get("main_order"),
"sub_order": row.get("sub_order"),
"main_lord": row.get("main_lord"),
"sub_lord": row.get("sub_lord"),
"duration_days": (p_end - p_start).total_seconds() / 86400.0,
"start": p_start.isoformat(sep=" "),
"end": p_end.isoformat(sep=" "),
})
return periods, antardasha
def annual_mudda_profiles(
*,
annual_start: Optional[datetime],
completed_years: Optional[int],
natal_moon_longitude: Optional[float],
annual_moon_longitude: Optional[float],
varsha_lord: Optional[str],
year_length: str = DEFAULT_YEAR_LENGTH,
) -> Dict[str, Any]:
"""All three upstream Mudda balance conventions, labelled; default first."""
profiles: Dict[str, Dict[str, Any]] = {}
days = YEAR_LENGTHS.get(year_length)
for profile in PROFILE_ORDER:
label, label_en = PROFILE_LABELS[profile]
base = {"balance_profile": profile, "label": label, "label_en": label_en,
"rule_text": PROFILE_RULES[profile], "is_default": profile == DEFAULT_PROFILE}
if days is None or not isinstance(annual_start, datetime):
profiles[profile] = {**base, "status": "blocked", "reason": "annual_start_or_year_length_missing"}
continue
if profile == "without_dasha_balance":
raw = calc_mudda_without_dasha_balance(varsha_lord, annual_start, annual_duration_days=days)
else:
balance_moon = natal_moon_longitude if profile == "moon_at_birth_time" else annual_moon_longitude
if completed_years is None or natal_moon_longitude is None or balance_moon is None:
profiles[profile] = {**base, "status": "blocked", "reason": "completed_years_and_moon_longitudes_required"}
continue
raw = calc_mudda_with_dasha_balance(
annual_start=annual_start,
completed_years=int(completed_years),
natal_moon_longitude=natal_moon_longitude,
balance_moon_longitude=balance_moon,
balance_profile=profile,
annual_duration_days=days,
)
if raw.get("status") == "blocked":
profiles[profile] = {**base, "status": "blocked", "reason": raw.get("reason")}
continue
periods, antardasha = _crop(raw, _parse(raw["annual_start"]), _parse(raw["annual_end"]))
first = periods[0]["lord"] if periods else None
profiles[profile] = {
**base,
"status": "parameter_sensitive",
"starting_lord": raw.get("starting_lord") or first,
"starting_lord_basis": raw.get("starting_lord_basis"),
"varsha_lord": raw.get("varsha_lord"),
"balance_moon_longitude": raw.get("balance_moon_longitude"),
"balance_fraction_remaining": raw.get("balance_fraction_remaining"),
"annual_start": raw.get("annual_start"),
"annual_end": raw.get("annual_end"),
"annual_duration_days": float(raw["annual_duration_days"]),
"year_length_basis": year_length,
"periods": periods,
"antardasha_periods": antardasha,
}
return {
# The wrapper's status is the default schedule's (round-5 Q7 ruling, same
# as `annual_mudda_dasha`); the two alternatives keep parameter_sensitive.
"status": "partial_verified" if profiles[DEFAULT_PROFILE].get("status") != "blocked" else "blocked",
"default_profile": DEFAULT_PROFILE,
"default_basis": DEFAULT_BASIS,
"default_basis_text": DEFAULT_BASIS_TEXT,
"upstream_export_profile": "without_dasha_balance",
"profiles": profiles,
"source": "tajika.calc_mudda_with_dasha_balance / calc_mudda_without_dasha_balance (upstream yinduzhanxing 5fb8379c)",
}