#!/usr/bin/env python3 """Source-bounded Dwisaptatisama (72-year) Mahadasha producer.""" from __future__ import annotations from datetime import datetime, timedelta from typing import Any YEAR_DAYS = 365.25636 NAKSHATRA_SPAN = 360.0 / 27.0 MULA_INDEX = 18 TOTAL_CYCLE = 72 DASHA_SEQUENCE = ( ("Sun", 9), ("Moon", 9), ("Mars", 9), ("Mercury", 9), ("Jupiter", 9), ("Venus", 9), ("Saturn", 9), ("Rahu", 9), ) _YEARS = dict(DASHA_SEQUENCE) def _sign_index(longitude: float) -> int: return int(float(longitude) % 360.0 // 30.0) def _house_from_lagna(*, ascendant_longitude: float, planet_sign_index: int) -> int: return ((int(planet_sign_index) % 12 - _sign_index(ascendant_longitude)) % 12) + 1 def dwisaptatisama_applicability( *, ascendant_longitude: float, lagna_lord_sign_index: int ) -> dict[str, Any]: """Apply the BPHS condition: Lagna lord in Lagna or seventh.""" ascendant_sign = _sign_index(ascendant_longitude) lord_sign = int(lagna_lord_sign_index) % 12 lord_house = _house_from_lagna( ascendant_longitude=ascendant_longitude, planet_sign_index=lord_sign, ) applicable = lord_house in (1, 7) return { "applicable": applicable, "ascendant_sign_index": ascendant_sign, "lagna_lord_sign_index": lord_sign, "lagna_lord_house": lord_house, "rule_family": "bphs_dwisaptatisama_lagna_lord_in_first_or_seventh", "reason": ( "Lagna lord is placed in the 1st or 7th house." if applicable else "Dwisaptatisama requires Lagna lord in the 1st or 7th house." ), } def _sequence_from(lord: str) -> list[tuple[str, int]]: start = next(index for index, (item, _years) in enumerate(DASHA_SEQUENCE) if item == lord) return list(DASHA_SEQUENCE[start:] + DASHA_SEQUENCE[:start]) def _birth_lord_and_balance(moon_longitude: float) -> tuple[str, float, int]: moon = float(moon_longitude) % 360.0 nakshatra = int(moon // NAKSHATRA_SPAN) lord = DASHA_SEQUENCE[((nakshatra - MULA_INDEX) % 27) % len(DASHA_SEQUENCE)][0] balance = _YEARS[lord] * (1.0 - (moon % NAKSHATRA_SPAN) / NAKSHATRA_SPAN) return lord, balance, nakshatra def calculate_dwisaptatisama_dasha(birth_info: dict[str, Any]) -> dict[str, Any]: """Return birth-forward MD rows; child-period boundaries stay blocked.""" required = ("birth_datetime", "ascendant_longitude", "lagna_lord_sign_index", "moon_longitude") missing = [field for field in required if birth_info.get(field) is None] if missing: return { "applicable": False, "execution_status": "blocked", "confidence_status": "blocked", "reason": "Missing required input: " + ", ".join(missing), "major": [], } birth = birth_info["birth_datetime"] if isinstance(birth, str): birth = datetime.fromisoformat(birth) applicability = dwisaptatisama_applicability( ascendant_longitude=float(birth_info["ascendant_longitude"]), lagna_lord_sign_index=int(birth_info["lagna_lord_sign_index"]), ) if not applicability["applicable"]: if birth_info.get("standard_table_profile") == "pl9_reports_all_dasha_tables_v1": applicability = { **applicability, "classical_applicable": False, "applicable": True, "standard_table_profile": "pl9_reports_all_dasha_tables_v1", "applicability_reason": applicability["reason"], "reason": ( "PL9 standard-table profile requested: generate the report table while preserving " "the unmet classical applicability gate in the audit fields." ), } else: return { **applicability, "execution_status": "not_applicable", "confidence_status": "not_applicable", "total_cycle": TOTAL_CYCLE, "major": [], } year_days = float(birth_info.get("dasha_year_days") or YEAR_DAYS) lord, balance, nakshatra = _birth_lord_and_balance(float(birth_info["moon_longitude"])) cursor = birth rows = [] for index, (row_lord, full_years) in enumerate(_sequence_from(lord)): years = balance if index == 0 else float(full_years) end = cursor + timedelta(days=years * year_days) rows.append( { "level": "mahadasha", "lord": row_lord, "planet": row_lord, "years": years, "full_years": full_years, "is_birth_balance": index == 0, "start_date": cursor.isoformat(timespec="seconds"), "end_date": end.isoformat(timespec="seconds"), } ) cursor = end return { **applicability, "execution_status": "executed", "confidence_status": "parameter_sensitive", "verification_status": "source_bounded_local_candidate", "period_depth_status": "mahadasha_executed_ad_pd_blocked_pending_row_level_replay", "total_cycle": TOTAL_CYCLE, "dasha_year_days": year_days, "moon_nakshatra_index": nakshatra, "starting_lord": lord, "dasha_balance_at_birth_years": balance, "major": rows, "reason": "Local source-bounded MD calculation; AD/PD are intentionally blocked pending row-level parity and runtime wiring.", "source_locator": "BPHS conditional Dasha passages: Lagna-lord-in-1st-or-7th gate, Mula count, Sun-to-Rahu order, and 9-year allotments.", }