236 lines
12 KiB
Python
236 lines
12 KiB
Python
"""Native projections for the optional offline purpose layers."""
|
|
|
|
from datetime import timezone
|
|
from functools import cache
|
|
|
|
from scripts.chart_choice import boundaries
|
|
|
|
# Use the same engines as the native chart adapter.
|
|
import ashtakavarga
|
|
import domain_calculation_service
|
|
import functional_benefics
|
|
import jaimini
|
|
import solar_return
|
|
import yogas_doshas
|
|
|
|
from scripts.annual_service import package as annual
|
|
|
|
from .scope import profile_clock
|
|
|
|
GRAHAS = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu", "Ketu")
|
|
PLANET_VARGAS = {f"planet_vargas:D{division}": division for division in (9, 10, 2, 11)}
|
|
SIGN_LAYERS = frozenset({
|
|
"house_lords", "lord_placements", "planet_signs", "aspects", "ashtakavarga",
|
|
"wealth_yogas", "raj_yogas",
|
|
})
|
|
LAYER_NAMES = SIGN_LAYERS | PLANET_VARGAS.keys() | {
|
|
"functional_roles", "venus_jupiter", "chara_karakas", "transit_relations",
|
|
"annual_chart", "strength_shadbala",
|
|
}
|
|
|
|
|
|
def _functional_roles(asc_sign, planets, profile):
|
|
paksha = functional_benefics.moon_paksha_from_planets(planets)
|
|
result = functional_benefics.derive_functional_benefic_malefic(
|
|
asc_sign, functional_profile=profile, moon_paksha=paksha,
|
|
)
|
|
if result["status"] != "used" or result["blocked_planets"]:
|
|
return None
|
|
return {
|
|
"profile": result["profile"],
|
|
"benefics": result["functional_benefics"],
|
|
"malefics": result["functional_malefics"],
|
|
"neutrals": result["functional_neutrals"],
|
|
"yogakarakas": result["yogakarakas"],
|
|
}
|
|
|
|
|
|
def _ashtakavarga(planet_rows, asc_index):
|
|
result = ashtakavarga.calc_ashtakavarga(planet_rows, asc_index)
|
|
return {
|
|
"bav": {
|
|
name: [house["sav_score"] for house in ashtakavarga._map_to_houses_values(row["bindus"], asc_index).values()]
|
|
for name, row in result["bav"].items()
|
|
},
|
|
"sav": [row["sav_score"] for row in result["house_scores"].values()],
|
|
}
|
|
|
|
|
|
def _yogas(detector, planet_rows, house_lords):
|
|
houses = {f"H{i}_Lord": lord for i, lord in house_lords.items()}
|
|
matches = detector(planet_rows, houses)["yogas"]
|
|
return {"matches": [
|
|
{"name": name, "subtype": subtype, "combination": combination}
|
|
for name, subtype, combination in sorted({
|
|
(row["type"], row.get("subtype", ""), row.get("combination", "")) for row in matches
|
|
})
|
|
]}
|
|
|
|
|
|
def _transit_context(start, ayanamsa, node_mode):
|
|
return annual._Context({
|
|
"birth": {"year": start.year, "month": start.month, "day": start.day,
|
|
"hour": start.hour, "minute": start.minute, "tz": 0},
|
|
"observation_date": start.date(), "ayanamsa": ayanamsa, "node_mode": node_mode,
|
|
})
|
|
|
|
|
|
def _transit_sky(forecast, ayanamsa, node_mode):
|
|
start, end = forecast
|
|
# Only the portions of the annual transit producer that are common to every natal chart.
|
|
ctx = _transit_context(start, ayanamsa, node_mode)
|
|
snapshot = annual._slow_transits(ctx, {
|
|
"status": "computed", "lagna_sign_idx": 0, "moon_sign_idx": 0, "longitudes": {},
|
|
}, start, end)
|
|
return [
|
|
(body, start.isoformat(), annual.SIGNS.index(row["sign"]))
|
|
for body, row in snapshot["positions_at_range_start"].items()
|
|
] + [
|
|
(row["body"], row["instant"]["utc"], annual.SIGNS.index(row["to_sign"]))
|
|
for row in snapshot["events"] if row["kind"] == "ingress"
|
|
]
|
|
|
|
|
|
def _transit_natal_contacts(forecast, profile, asc, planets):
|
|
longitudes = {"Lagna": asc, **{name: planets[name] for name in annual.NATAL_POINTS[1:]}}
|
|
start, end = forecast
|
|
context = _transit_context(start, profile["ayanamsa"], profile["node_mode"])
|
|
with domain_calculation_service.swiss_ephemeris_lock():
|
|
snapshot = annual._slow_transits(context, {
|
|
"status": "computed", "lagna_sign_idx": boundaries.varga_sign(asc, 1),
|
|
"moon_sign_idx": boundaries.varga_sign(planets["Moon"], 1), "longitudes": longitudes,
|
|
}, start, end)
|
|
|
|
def contacts(field):
|
|
return [
|
|
{"body": row["body"], "natal_point": row["natal_point"], "contact": row["contact"],
|
|
**({"aspect_nth": row["aspect_nth"]} if "aspect_nth" in row else {}),
|
|
"crossings": [
|
|
{"at": crossing["instant"]["utc"], "retrograde": crossing["retrograde"]}
|
|
for crossing in row["crossings"]
|
|
]}
|
|
for row in snapshot[field]
|
|
]
|
|
|
|
return {"conjunctions": contacts("natal_point_contacts"), "aspects": contacts("exact_aspect_contacts")}
|
|
|
|
|
|
def _annual_charts(at, profile, asc_index, forecast):
|
|
start, end = forecast
|
|
birth_year = profile_clock(at, profile).year
|
|
if start < at:
|
|
return None
|
|
with domain_calculation_service.swiss_ephemeris_lock():
|
|
jd = solar_return._datetime_to_jd_ut(at)
|
|
# Match calc_solar_return_chart's solver-frame Sun instead of rerunning the natal chart.
|
|
target = solar_return._get_sun_lon_jd(jd, ayanamsa_name=profile["ayanamsa"])
|
|
if target is None:
|
|
return None
|
|
returns = []
|
|
for year in range(max(birth_year, start.year - 1), end.year + 1):
|
|
solar = solar_return.find_solar_return_ut(jd, target, year, ayanamsa_name=profile["ayanamsa"])
|
|
if solar.get("method") != "swisseph_newton":
|
|
return None
|
|
instant = solar["dt_ut"].replace(tzinfo=timezone.utc)
|
|
if instant < end:
|
|
returns.append((instant, year))
|
|
active = [row for row in returns if row[0] <= start]
|
|
relevant = active[-1:] + [row for row in returns if start < row[0]]
|
|
charts = []
|
|
for instant, year in relevant:
|
|
request = boundaries.ChartRequest(instant.date().isoformat(), profile["lat"], profile["lon"], 0,
|
|
profile["ayanamsa"], profile["node_mode"])
|
|
asc = boundaries.ascendant_longitude(request, instant)
|
|
muntha = solar_return._muntha_from_natal_ascendant(asc_index, year - birth_year)
|
|
charts.append({"year": year, "ascendant_sign": boundaries.varga_sign(asc, 1),
|
|
"muntha_sign": muntha["muntha_sign_idx"]})
|
|
return {"charts": charts} if charts else None
|
|
|
|
|
|
def native_layer_values(required, forecast):
|
|
layers = (LAYER_NAMES | {"transit_natal_contacts"}).intersection(required)
|
|
|
|
@cache
|
|
def sky(ayanamsa, node_mode):
|
|
return _transit_sky(forecast, ayanamsa, node_mode)
|
|
|
|
def compute(at, profile, chart, asc, planets, ref, output):
|
|
values = {}
|
|
if not layers:
|
|
return values
|
|
asc_index = boundaries.varga_sign(asc, 1)
|
|
asc_sign = chart["ascendant"]["sign"]
|
|
planet_rows = chart["planets"]
|
|
has_grahas = all(name in planets and planet_rows[name].get("sign") in annual.SIGNS for name in GRAHAS)
|
|
house_lords = {str(i): chart["houses"].get(f"house_{i}", {}).get("lord") for i in range(1, 13)}
|
|
has_lords = all(house_lords.values())
|
|
for layer in sorted(layers):
|
|
if layer == "house_lords":
|
|
value, method, dependencies = (house_lords if has_lords else None, "native_d1_house_lords", ["lagna_sign"])
|
|
elif layer == "lord_placements":
|
|
value = {i: planet_rows.get(lord, {}).get("house") for i, lord in house_lords.items()}
|
|
value, method, dependencies = (
|
|
value if has_lords and all(value.values()) else None, "native_d1_lord_placements",
|
|
["lagna_sign", "planet_signs"],
|
|
)
|
|
elif layer == "planet_signs":
|
|
value = {name: planet_rows[name]["sign"] for name in GRAHAS} if has_grahas else None
|
|
method, dependencies = "native_d1_planet_signs", ["planet_longitudes"]
|
|
elif layer in PLANET_VARGAS:
|
|
value = {name: boundaries.varga_sign(planets[name], PLANET_VARGAS[layer])
|
|
for name in GRAHAS} if has_grahas else None
|
|
method, dependencies = "native_divisional_planet_signs", ["planet_longitudes", "varga_mode"]
|
|
elif layer == "functional_roles":
|
|
profile_id = profile.get("functional_profile", functional_benefics.DEFAULT_FUNCTIONAL_PROFILE)
|
|
value = _functional_roles(asc_sign, planets, profile_id)
|
|
method, dependencies = "native_functional_roles", ["lagna_sign", "moon_paksha", "functional_profile"]
|
|
elif layer == "venus_jupiter":
|
|
value = {
|
|
name: {key: planet_rows[name][key] for key in ("sign", "house", "status")}
|
|
for name in ("Venus", "Jupiter")
|
|
} if has_grahas else None
|
|
method, dependencies = "native_d1_venus_jupiter", ["lagna_sign", "planet_signs", "planet_degrees_in_sign"]
|
|
elif layer == "aspects":
|
|
value = {
|
|
name: [target for target in range(12)
|
|
if jaimini._rasi_drishti(boundaries.varga_sign(planets[name], 1), target)]
|
|
for name in GRAHAS
|
|
} if has_grahas else None
|
|
method, dependencies = "native_jaimini_rasi_drishti", ["planet_signs"]
|
|
elif layer == "chara_karakas":
|
|
degrees = {name: planets[name] % 30 for name in GRAHAS[:7]} if has_grahas else {}
|
|
table = jaimini.calc_chara_karaka_7(degrees)["karaka_table"] if degrees else {}
|
|
value = {role: row["planet"] for role, row in table.items()} or None
|
|
method, dependencies = "native_chara_karakas_7", ["seven_planet_degrees_in_sign"]
|
|
elif layer == "ashtakavarga":
|
|
value = _ashtakavarga(planet_rows, asc_index) if has_grahas else None
|
|
method, dependencies = "native_ashtakavarga_bav_sav", ["lagna_sign", "planet_signs"]
|
|
elif layer in ("wealth_yogas", "raj_yogas"):
|
|
detector = yogas_doshas.calc_dhana_yogas if layer == "wealth_yogas" else yogas_doshas.calc_raj_yogas
|
|
value = _yogas(detector, planet_rows, house_lords) if has_lords and has_grahas else None
|
|
method, dependencies = detector.__name__, ["lagna_sign", "planet_signs", "house_lords"]
|
|
elif layer == "transit_relations":
|
|
value = {"whole_sign_relations": [
|
|
{"body": body, "at": instant, "sign": sign, "from_lagna": annual._house(sign, asc_index),
|
|
"from_moon": annual._house(sign, boundaries.varga_sign(planets["Moon"], 1))}
|
|
for body, instant, sign in sky(profile["ayanamsa"], profile["node_mode"])
|
|
]} if forecast and "Moon" in planets else None
|
|
method, dependencies = "native_whole_sign_slow_transits", ["lagna_sign", "moon_sign", "forecast_window"]
|
|
elif layer == "transit_natal_contacts":
|
|
value = _transit_natal_contacts(forecast, profile, asc, planets) if forecast and has_grahas else None
|
|
method, dependencies = (
|
|
"native_exact_slow_transit_contacts",
|
|
["ascendant_longitude", "planet_longitudes", "forecast_window", "ayanamsa", "node_mode"],
|
|
)
|
|
elif layer == "annual_chart":
|
|
value = _annual_charts(at, profile, asc_index, forecast) if forecast else None
|
|
method, dependencies = (
|
|
"native_solar_return_and_muntha", ["candidate_at", "natal_sun_longitude", "lagna_sign", "forecast_window", "profile"],
|
|
)
|
|
else:
|
|
value, method, dependencies = None, "shadbala_pending_review", []
|
|
values[layer] = output(value, method, ref, dependencies)
|
|
return values
|
|
|
|
return compute
|