Spaces:
Sleeping
Sleeping
Update astro_core.py
Browse files- astro_core.py +42 -35
astro_core.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
# astro_core.py
|
| 3 |
"""
|
| 4 |
Core astrology calculation module.
|
|
@@ -6,7 +5,7 @@ Handles birth chart calculations using flatlib.
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
from datetime import datetime
|
| 9 |
-
import pytz
|
| 10 |
from typing import Dict, Any
|
| 11 |
from flatlib.datetime import Datetime
|
| 12 |
from flatlib.geopos import GeoPos
|
|
@@ -29,9 +28,7 @@ class ChartCalculator:
|
|
| 29 |
]
|
| 30 |
|
| 31 |
def _get_house_number(self, lon: float, chart: Chart) -> int:
|
| 32 |
-
"""
|
| 33 |
-
Determine house number for a given longitude
|
| 34 |
-
"""
|
| 35 |
house_lons = []
|
| 36 |
for i in range(1, 13):
|
| 37 |
try:
|
|
@@ -40,51 +37,48 @@ class ChartCalculator:
|
|
| 40 |
except (KeyError, AttributeError):
|
| 41 |
house_lons.append(0)
|
| 42 |
|
| 43 |
-
#
|
|
|
|
|
|
|
| 44 |
for i in range(12):
|
| 45 |
-
|
| 46 |
-
if (house_lons[i] <= lon < house_lons[next_i]) or \
|
| 47 |
-
(house_lons[i] > house_lons[next_i] and # Handle zodiac wrap
|
| 48 |
-
(lon >= house_lons[i] or lon < house_lons[next_i])):
|
| 49 |
return i + 1
|
| 50 |
-
return 1 # Default to first house if no match
|
| 51 |
-
|
| 52 |
def _normalize_longitude(self, lon: float) -> float:
|
| 53 |
"""Normalize longitude to 0-360 range"""
|
| 54 |
return lon % 360
|
| 55 |
|
| 56 |
def _get_zodiac_sign(self, longitude: float) -> str:
|
| 57 |
-
"""
|
| 58 |
-
Get zodiac sign from longitude
|
| 59 |
-
"""
|
| 60 |
sign_num = int(self._normalize_longitude(longitude) / 30)
|
| 61 |
return ZODIAC_SIGNS[sign_num]
|
| 62 |
|
| 63 |
def calculate_birth_chart(self,
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
"""
|
| 69 |
Calculate birth chart positions
|
| 70 |
Returns a dictionary of planetary positions and house cusps
|
| 71 |
"""
|
| 72 |
try:
|
| 73 |
-
#
|
| 74 |
-
tz =
|
| 75 |
-
birth_datetime =
|
| 76 |
|
| 77 |
# Extract date and time components from birth_datetime
|
| 78 |
date_str = birth_datetime.strftime('%Y/%m/%d')
|
| 79 |
time_str = birth_datetime.strftime('%H:%M')
|
| 80 |
-
|
| 81 |
# Convert to flatlib datetime
|
| 82 |
date = Datetime(date_str, time_str, timezone)
|
| 83 |
pos = GeoPos(latitude, longitude)
|
| 84 |
-
|
| 85 |
# Calculate chart
|
| 86 |
chart = Chart(date, pos)
|
| 87 |
-
|
| 88 |
# Extract planetary positions
|
| 89 |
positions = {}
|
| 90 |
for planet in self.planets:
|
|
@@ -93,13 +87,13 @@ class ChartCalculator:
|
|
| 93 |
if obj:
|
| 94 |
# Normalize longitude
|
| 95 |
lon = self._normalize_longitude(obj.lon)
|
| 96 |
-
|
| 97 |
# Get sign
|
| 98 |
sign = self._get_zodiac_sign(lon)
|
| 99 |
-
|
| 100 |
# Get house
|
| 101 |
house = self._get_house_number(lon, chart)
|
| 102 |
-
|
| 103 |
positions[planet] = {
|
| 104 |
'sign': sign,
|
| 105 |
'degrees': lon,
|
|
@@ -112,7 +106,7 @@ class ChartCalculator:
|
|
| 112 |
'house': 1,
|
| 113 |
'error': str(e)
|
| 114 |
}
|
| 115 |
-
|
| 116 |
# Extract house cusps
|
| 117 |
houses = {}
|
| 118 |
for i in range(1, 13):
|
|
@@ -130,20 +124,33 @@ class ChartCalculator:
|
|
| 130 |
'degrees': 0,
|
| 131 |
'error': str(e)
|
| 132 |
}
|
| 133 |
-
|
| 134 |
return {
|
| 135 |
'planets': positions,
|
| 136 |
'houses': houses
|
| 137 |
}
|
| 138 |
-
|
| 139 |
-
except
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
return {
|
| 141 |
'error': f"Failed to calculate birth chart: {str(e)}",
|
| 142 |
'planets': {},
|
| 143 |
'houses': {}
|
| 144 |
}
|
| 145 |
-
|
| 146 |
def get_planet_aspects(self, chart_data: Dict[str, Any]) -> Dict[str, list]:
|
| 147 |
"""Calculate major aspects between planets"""
|
| 148 |
-
# TODO: Implement aspect calculation
|
| 149 |
-
return {}
|
|
|
|
|
|
|
| 1 |
# astro_core.py
|
| 2 |
"""
|
| 3 |
Core astrology calculation module.
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
from datetime import datetime
|
| 8 |
+
import zoneinfo # Use zoneinfo instead of pytz
|
| 9 |
from typing import Dict, Any
|
| 10 |
from flatlib.datetime import Datetime
|
| 11 |
from flatlib.geopos import GeoPos
|
|
|
|
| 28 |
]
|
| 29 |
|
| 30 |
def _get_house_number(self, lon: float, chart: Chart) -> int:
|
| 31 |
+
"""Determine house number for a given longitude"""
|
|
|
|
|
|
|
| 32 |
house_lons = []
|
| 33 |
for i in range(1, 13):
|
| 34 |
try:
|
|
|
|
| 37 |
except (KeyError, AttributeError):
|
| 38 |
house_lons.append(0)
|
| 39 |
|
| 40 |
+
# Simplified zodiac wrap-around handling (may need adjustments)
|
| 41 |
+
lon = self._normalize_longitude(lon)
|
| 42 |
+
house_lons = [self._normalize_longitude(lon) for lon in house_lons]
|
| 43 |
for i in range(12):
|
| 44 |
+
if house_lons[i] <= lon < house_lons[(i + 1) % 12]:
|
|
|
|
|
|
|
|
|
|
| 45 |
return i + 1
|
| 46 |
+
return 1 # Default to first house if no match
|
| 47 |
+
|
| 48 |
def _normalize_longitude(self, lon: float) -> float:
|
| 49 |
"""Normalize longitude to 0-360 range"""
|
| 50 |
return lon % 360
|
| 51 |
|
| 52 |
def _get_zodiac_sign(self, longitude: float) -> str:
|
| 53 |
+
"""Get zodiac sign from longitude"""
|
|
|
|
|
|
|
| 54 |
sign_num = int(self._normalize_longitude(longitude) / 30)
|
| 55 |
return ZODIAC_SIGNS[sign_num]
|
| 56 |
|
| 57 |
def calculate_birth_chart(self,
|
| 58 |
+
birth_datetime: datetime,
|
| 59 |
+
latitude: float,
|
| 60 |
+
longitude: float,
|
| 61 |
+
timezone: str) -> Dict[str, Any]:
|
| 62 |
"""
|
| 63 |
Calculate birth chart positions
|
| 64 |
Returns a dictionary of planetary positions and house cusps
|
| 65 |
"""
|
| 66 |
try:
|
| 67 |
+
# Timezone handling with zoneinfo
|
| 68 |
+
tz = zoneinfo.ZoneInfo(timezone)
|
| 69 |
+
birth_datetime = birth_datetime.replace(tzinfo=tz)
|
| 70 |
|
| 71 |
# Extract date and time components from birth_datetime
|
| 72 |
date_str = birth_datetime.strftime('%Y/%m/%d')
|
| 73 |
time_str = birth_datetime.strftime('%H:%M')
|
| 74 |
+
|
| 75 |
# Convert to flatlib datetime
|
| 76 |
date = Datetime(date_str, time_str, timezone)
|
| 77 |
pos = GeoPos(latitude, longitude)
|
| 78 |
+
|
| 79 |
# Calculate chart
|
| 80 |
chart = Chart(date, pos)
|
| 81 |
+
|
| 82 |
# Extract planetary positions
|
| 83 |
positions = {}
|
| 84 |
for planet in self.planets:
|
|
|
|
| 87 |
if obj:
|
| 88 |
# Normalize longitude
|
| 89 |
lon = self._normalize_longitude(obj.lon)
|
| 90 |
+
|
| 91 |
# Get sign
|
| 92 |
sign = self._get_zodiac_sign(lon)
|
| 93 |
+
|
| 94 |
# Get house
|
| 95 |
house = self._get_house_number(lon, chart)
|
| 96 |
+
|
| 97 |
positions[planet] = {
|
| 98 |
'sign': sign,
|
| 99 |
'degrees': lon,
|
|
|
|
| 106 |
'house': 1,
|
| 107 |
'error': str(e)
|
| 108 |
}
|
| 109 |
+
|
| 110 |
# Extract house cusps
|
| 111 |
houses = {}
|
| 112 |
for i in range(1, 13):
|
|
|
|
| 124 |
'degrees': 0,
|
| 125 |
'error': str(e)
|
| 126 |
}
|
| 127 |
+
|
| 128 |
return {
|
| 129 |
'planets': positions,
|
| 130 |
'houses': houses
|
| 131 |
}
|
| 132 |
+
|
| 133 |
+
except zoneinfo.ZoneInfoNotFoundError:
|
| 134 |
+
return {
|
| 135 |
+
'error': f"Invalid timezone: {timezone}",
|
| 136 |
+
'planets': {},
|
| 137 |
+
'houses': {}
|
| 138 |
+
}
|
| 139 |
+
except KeyError as e:
|
| 140 |
+
# Example of catching a specific exception
|
| 141 |
+
return {
|
| 142 |
+
'error': f"KeyError during chart calculation: {str(e)}",
|
| 143 |
+
'planets': {},
|
| 144 |
+
'houses': {}
|
| 145 |
+
}
|
| 146 |
+
except Exception as e: # Catch-all for unexpected errors
|
| 147 |
return {
|
| 148 |
'error': f"Failed to calculate birth chart: {str(e)}",
|
| 149 |
'planets': {},
|
| 150 |
'houses': {}
|
| 151 |
}
|
| 152 |
+
|
| 153 |
def get_planet_aspects(self, chart_data: Dict[str, Any]) -> Dict[str, list]:
|
| 154 |
"""Calculate major aspects between planets"""
|
| 155 |
+
# TODO: Implement aspect calculation using flatlib
|
| 156 |
+
return {}
|