import swisseph as swe import math import datetime import os from .models import GeoLocation, PlanetPosition, HexagramAddress, CognitiveProfile class PrecisionEngine: def __init__(self): ephe_path = os.getenv("SE_EPHE_PATH", "/app/ephe") swe.set_ephe_path(ephe_path) def _get_julian_day(self, dt): decimal_hour = dt.hour + dt.minute / 60.0 + dt.second / 3600.0 return swe.julday(dt.year, dt.month, dt.day, decimal_hour) def _decode_fractal(self, longitude): gate = int((longitude % 360) / 360 * 64) + 1 line = int((longitude % (360 / 64)) / (360 / 64) * 6) + 1 return HexagramAddress(gate=gate, line=line, color=1, tone=1, base=1) def calculate_chart(self, natal_dt, location): jd = self._get_julian_day(natal_dt) result = {} planet_ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] planet_names = ['Sun', 'Moon', 'Mercury', 'Venus', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'] for pid, name in zip(planet_ids, planet_names): try: res, _ = swe.calc_ut(jd, pid) lon = res[0] lat = res[1] speed = res[3] fractal = self._decode_fractal(lon) result[name] = PlanetPosition( name=name, longitude=lon, latitude=lat, speed=speed, house=int(((lon + 30) % 360) / 30), is_retrograde=speed < 0, gate=fractal.gate, line=fractal.line ) except: pass return result def get_live_overlay(self): now = datetime.datetime.utcnow() return self.calculate_chart(now, None)