Skip to content

Commit 1cdd73e

Browse files
committed
Create swe functions for handling topocentric and sidereal cases
1 parent eb82d6c commit 1cdd73e

File tree

1 file changed

+95
-18
lines changed

1 file changed

+95
-18
lines changed

flatlib/ephem/swe.py

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@
6262
const.AY_GALCENTER_5SAG: 17
6363
}
6464

65+
# SWE flags for computations
66+
"""
67+
Note: following flags are defined in libswe/swephexp.h
68+
69+
#define SEFLG_SWIEPH 2 /* use SWISSEPH ephemeris */
70+
#define SEFLG_SPEED 256 /* high precision speed */
71+
#define SEFLG_TOPOCTR (32*1024) /* topocentric position */
72+
#define SEFLG_SIDEREAL (64*1024) /* sidereal position */
73+
"""
74+
75+
SEFLG_SWIEPH = 2
76+
SEFLG_SPEED = 256
77+
SEFLG_TOPOCTR = 32 * 1024
78+
SEFLG_SIDEREAL = 64 * 1024
79+
6580

6681
# ==== Internal functions ==== #
6782

@@ -203,44 +218,37 @@ def get_ayanamsa(jd, mode):
203218
return swisseph.get_ayanamsa_ut(jd)
204219

205220

206-
# === Topocentric and sidereal objects == #
221+
# === Sidereal and topocentric functions == #
207222

208223
def swe_object(obj, jd, lat=None, lon=None, alt=None, mode=None):
209224
"""
210225
Returns an object from the swiss ephemeris.
211-
- If lat/lon/alt values are set, it returns topocentric positions
212-
- If sidmode is set, it returns sidereal positions
213-
214-
Note: flags are defined in the libswe/swephexp.h file
215-
216-
#define SEFLG_SWIEPH 2 /* use SWISSEPH ephemeris */
217-
#define SEFLG_SPEED 256 /* high precision speed */
218-
#define SEFLG_TOPOCTR (32*1024) /* topocentric position */
219-
#define SEFLG_SIDEREAL (64*1024) /* sidereal position */
226+
- If lat/lon/alt values are set, it returns the topocentric positions
227+
- If mode is set, returns sidereal positions for the given mode
220228
221229
:param obj: the object
222-
:param jd: the julian date as real number
230+
:param jd: the julian date
223231
:param lat: the latitude in degrees
224232
:param lon: the longitude in degrees
225233
:param alt: the altitude above msl in meters
226234
:param mode: the ayanamsa
227-
:return: swiss ephem object
235+
:return: swiss ephem object dict
228236
"""
229237
swe_obj = SWE_OBJECTS[obj]
230-
flags = 2 + 256
238+
flags = SEFLG_SWIEPH + SEFLG_SPEED
231239

232-
# Topocentric positions
240+
# Use topocentric positions
233241
if lat and lon and alt:
234242
swisseph.set_topo(lat, lon, alt)
235-
flags += (32 * 1024)
243+
flags += SEFLG_TOPOCTR
236244

237-
# Sidereal zodiac
245+
# Use sidereal zodiac
238246
if mode:
239247
eph_mode = SWE_AYANAMSAS[mode]
240248
swisseph.set_sid_mode(eph_mode, 0, 0)
241-
flags += (64*1024)
249+
flags += SEFLG_SIDEREAL
242250

243-
# Compute and return position
251+
# Compute and return positions
244252
swelist = swisseph.calc_ut(jd, swe_obj, flags)
245253
return {
246254
'id': obj,
@@ -249,3 +257,72 @@ def swe_object(obj, jd, lat=None, lon=None, alt=None, mode=None):
249257
'lonspeed': swelist[3],
250258
'latspeed': swelist[4]
251259
}
260+
261+
262+
def swe_houses_lon(jd, lat, lon, hsys, mode=None):
263+
"""
264+
Returns the longitudes of houses and angles cusps.
265+
- If mode is set, returns sidereal positions for the given mode
266+
267+
:param jd: the julian date
268+
:param lat: the latitude in degrees
269+
:param lon: the longitude in degrees
270+
:param hsys: the house system
271+
:param mode: the ayanamsa
272+
:return: list of houses and angles longitudes
273+
"""
274+
swe_hsys = SWE_HOUSESYS[hsys]
275+
flags = SEFLG_SWIEPH + SEFLG_SPEED
276+
277+
# Use sidereal zodiac
278+
if mode:
279+
eph_mode = SWE_AYANAMSAS[mode]
280+
swisseph.set_sid_mode(eph_mode, 0, 0)
281+
flags = SEFLG_SIDEREAL
282+
283+
# Compute house cusps and angles
284+
cusps, ascmc = swisseph.houses_ex(jd, lat, lon, swe_hsys, flags)
285+
angles = [
286+
ascmc[0],
287+
ascmc[1],
288+
angle.norm(ascmc[0] + 180),
289+
angle.norm(ascmc[1] + 180)
290+
]
291+
292+
return cusps, angles
293+
294+
295+
def swe_houses(jd, lat, lon, hsys, mode=None):
296+
"""
297+
Returns the houses and angles.
298+
- If mode is set, returns sidereal positions for the given mode
299+
300+
:param jd: the julian date
301+
:param lat: the latitude in degrees
302+
:param lon: the longitude in degrees
303+
:param hsys: the house system
304+
:param mode: the ayanamsa
305+
:return: list of houses and angles
306+
"""
307+
# Compute house cusps and angles
308+
cusps, ascmc = swe_houses_lon(jd, lat, lon, hsys, mode)
309+
310+
# Compute house sizes
311+
cusps += (cusps[0], )
312+
houses = [
313+
{
314+
'id': const.LIST_HOUSES[i],
315+
'lon': cusps[i],
316+
'size': angle.distance(cusps[i], cusps[i + 1])
317+
} for i in range(12)
318+
]
319+
320+
# Create angles
321+
angles = [
322+
{'id': const.ASC, 'lon': ascmc[0]},
323+
{'id': const.MC, 'lon': ascmc[1]},
324+
{'id': const.DESC, 'lon': angle.norm(ascmc[0] + 180)},
325+
{'id': const.IC, 'lon': angle.norm(ascmc[1] + 180)}
326+
]
327+
328+
return houses, angles

0 commit comments

Comments
 (0)