// Julian Day calculation
const jd = swe.julday(2023, 6, 15, 12.5);
// UTC to Julian Day
const result = swe.utc_to_jd(2023, 6, 15, 12, 30, 0, swe.SE_GREG_CAL);
// Julian Day to calendar date
const date = swe.revjul(jd, swe.SE_GREG_CAL);
// Sidereal time
const sidTime = swe.sidtime(jd);
// Delta T
const deltaT = swe.deltat(jd);
// Basic calculation
const pos = swe.calc_ut(jd, swe.SE_SUN, swe.SEFLG_SWIEPH);
// Returns: [longitude, latitude, distance, speed]
// Detailed calculation
const detailed = swe.calc(jd, swe.SE_MOON, swe.SEFLG_SWIEPH);
// Returns: {longitude, latitude, distance, longitudeSpeed, latitudeSpeed, distanceSpeed}
async function birthChart(year, month, day, hour, minute, timezone) {
const swe = new SwissEph();
await swe.initSwissEph();
const utcHour = hour + minute / 60 - timezone;
const jd = swe.julday(year, month, day, utcHour);
const planets = [swe.SE_SUN, swe.SE_MOON, swe.SE_MERCURY, swe.SE_VENUS,
swe.SE_MARS, swe.SE_JUPITER, swe.SE_SATURN];
const chart = {};
for (const planet of planets) {
const pos = swe.calc_ut(jd, planet, swe.SEFLG_SWIEPH);
chart[swe.get_planet_name(planet)] = pos[0];
}
swe.close();
return chart;
}
async function currentPositions() {
const swe = new SwissEph();
await swe.initSwissEph();
const now = new Date();
const jd = swe.julday(
now.getUTCFullYear(),
now.getUTCMonth() + 1,
now.getUTCDate(),
now.getUTCHours() + now.getUTCMinutes() / 60
);
const sunPos = swe.calc_ut(jd, swe.SE_SUN, swe.SEFLG_SWIEPH);
const moonPos = swe.calc_ut(jd, swe.SE_MOON, swe.SEFLG_SWIEPH);
swe.close();
return {
sun: sunPos[0],
moon: moonPos[0],
julianDay: jd
};
}
async function comparePositions(year, month, day, hour) {
const swe = new SwissEph();
await swe.initSwissEph();
const jd = swe.julday(year, month, day, hour);
// Tropical
const tropical = swe.calc_ut(jd, swe.SE_SUN, swe.SEFLG_SWIEPH);
// Sidereal (Lahiri)
swe.set_sid_mode(swe.SE_SIDM_LAHIRI, 0, 0);
const sidereal = swe.calc_ut(jd, swe.SE_SUN, swe.SEFLG_SWIEPH | swe.SEFLG_SIDEREAL);
swe.close();
return {
tropical: tropical[0],
sidereal: sidereal[0],
difference: tropical[0] - sidereal[0]
};
}
// Normalize degrees (0-360)
const normalized = swe.degnorm(370); // Returns 10
// Split degrees into D°M'S"
const split = swe.split_deg(123.456, swe.SE_SPLIT_DEG_ROUND_SEC);
// Returns: {degree: 123, min: 27, second: 24, sign: 4}
// Day of week (0=Monday, 6=Sunday)
const dayOfWeek = swe.day_of_week(jd);
// Planet name
const name = swe.get_planet_name(swe.SE_SUN); // Returns "Sun"
// Version info
const version = swe.version();
async function safeCalculation() {
let swe = null;
try {
swe = new SwissEph();
await swe.initSwissEph();
// Your calculations here
const jd = swe.julday(2023, 6, 15, 12);
const result = swe.calc_ut(jd, swe.SE_SUN, swe.SEFLG_SWIEPH);
return { success: true, data: result };
} catch (error) {
return { success: false, error: error.message };
} finally {
if (swe) swe.close();
}
}
your-project/
├── src/
│ └── swisseph.js # Main library
├── wasm/
│ ├── swisseph.js # WASM module
│ └── swisseph.wasm # WASM binary
└── your-app.js # Your application