The Ascendant using Python3 and PySwissEph

I’m not a person who is good with Math, Astronomy, or other Sciences so I really struggled with figuring out how to properly determine the ascending sign in my astrology software.

There are still some issues, in comparison to other Vedic astrology programs, for instance during the bottom half of the chart, roughly after the 6th Zodiac sign (Virgo), things speed up, quite literally as it takes less time to navigate this half of the Zodiac.

Using Python3 and the PySwissEph library here is some information for calculating the ascendants sign position in your astrology program.

from datetime import datetime
from pytz import timezone, all_timezones
import pytz, os, math

import swisseph as swe
swe.set_ephe_path(os.getcwd() + 'swisseph/')


# LOCATION DATA
lat = math.radians(49.246292)
lon = math.radians(-123.116226)

Ok what I want to suggest (with emphasis) is to pay attention to the documentation of the SwissEpheremis API because the little things often explain a lot.

double geolat,      /* geographic latitude, in degrees */

In order to input longitude and latitude in degrees you should use the math.radians() function available in Python. This literally is what threw me off for years.

Anyway, here is the full code drop of my demo python script.

from datetime import datetime
from pytz import timezone, all_timezones
import pytz, os, math

import swisseph as swe
swe.set_ephe_path(os.getcwd() + 'swisseph/')

# LOCATION DATA
lat = math.radians(52.1417)
lon = math.radians(-122.1417)

### SET SID MODE
swe.set_sid_mode(swe.SIDM_LAHIRI)

### DATE INFORMATION
zone = "America/Vancouver"
year = 1983
month = 4
day = 11
hour = 23
minute = 13

### TIMEZONE INFORMATION
tz = timezone(zone)
timecheck = tz.localize(datetime(year, month, day, hour, minute, 0))

### CALCULATE JULIAN DATE
jd = float(swe.utc_to_jd(timecheck.year, timecheck.month, timecheck.day, timecheck.hour, timecheck.minute, 0.1, 1)[1])

### HOUSES AND PLANETS
planets = 0,1,4,2,5,3,6,10

### ASCENDANT
W = bytes('W', encoding='utf8')
ascDeg = float(swe.houses_ex(jd, lat, lon, W)[0][1])
asc = int(ascDeg / 30)

### POSSIBLE FIX FOR MISCALCULATIONS
if asc >= 6:
    ascDeg = float(swe.houses_ex(jd, lat, lon, W, swe.FLG_SIDEREAL)[0][1]) - swe.get_ayanamsa_ut(jd)
    asc = int(ascDeg / 30)

if asc == 0:
    asc = 12

print("--- Ascendant ---")
print(asc)
print("")
for p in planets:

    planet_name = swe.get_planet_name(p)
    
    swe_calc = float(swe.calc_ut(jd, p)[0][0])
    planet_lon = (swe_calc - swe.get_ayanamsa_ut(jd)) % 360

    sign = int(planet_lon / 30) + 1
    if sign == 0:
        sign = 12
    index = (sign + 1 + 12 - asc) % 12
    if index == 0:
        index = 12
    
    if p == 10 or planet_name == "mean Node":
        planet_name = "Rahu"
        print(planet_name)
        print(index)
        
        planet_lon = (planet_lon + 180.00) % 360
        sign = int(planet_lon / 30) + 1
        if sign == 0:
            sign = 12
            
        index = (sign + 1 + 12 - asc) % 12
        if index == 0:
            index = 12
            
        planet_name = "Ketu"
        
    print(planet_name)
    print(index)