timotejroiko / sweph

The definitive Swiss Ephemeris bindings for Node.js
Other
93 stars 15 forks source link

A simple example how to build an Astrological Chart #4

Closed nodeperson closed 2 years ago

nodeperson commented 2 years ago

Hello! It would be great if you could give us a little simple example of how we - newbies - can use your library to build a little simple astrological chart. Anyway, thanks for your efforts!

timotejroiko commented 2 years ago

Hi nodeperson, sweph is a simple wrapper for the swiss ephemeris library created by astro.com / astrodienst, so in order to learn how to use it, you should refer to their official documentations found here:

https://www.astro.com/swisseph/swisseph.htm
https://www.astro.com/swisseph/swephprg.htm

From the above documentation, the steps would be like this:

  1. you need to figure out a person's birth time in UTC / GMT-0 and their birth location's latitude and longitude (this part is not covered by the swiss ephemeris, you must obtain them yourself either manually or using third party libraries and services)
  2. you need to download the necessary ephemeris files for the planets you wish to calculate from here: https://www.astro.com/ftp/swisseph/ephe/ and use the set_ephe_path function to point the library to the correct folder (example the file sepl_18.se1 is valid for all major planets from year 1800 to 2400)
  3. use the function utc_to_jd to convert the birth time into a "julian day" number. this function gives you two numbers, one for UT and one for ET
  4. use one of the calc functions once for each planet you wish to calculate
  5. use one of the houses functions with the UT number to calculate the houses
  6. decide how to display the results (the swiss ephemeris only covers raw data, it does not include any kind of graphics or display, you need to do that part yourself)

example in code using this library:

const { set_ephe_path, utc_to_jd, calc, houses } = require("sweph");
set_ephe_path("./ephemeris"); // folder where the downloaded ephemeris files are

// get JD values from a UTC/GMT-0 date
const JD = utc_to_jd(2005, 11, 15, 13, 45, 0, 1) // year, month, day, hour, minute, second, calendar system (1 = gregorian calendar, valid for all dates after year 1582)
const JDET = JD[0]; // for use in calc functions
const JDUT = JD[1]; // for use in calc_ut functions and houses functions

const planets = [0,1,2,3,4,5,6,7,8,9] // planet IDs, 0 = sun, 1 = moon, 2 = mercury, etc...
for(const planetID of planets) {
    // calculate each planet individually
    const calculated = calc(JDET, planetID, 2); // third parameter contains the calculation options, 2 = default options
    console.log(`logging planet ${planetID}`, calculated);
}

// coordinates must be in decimal degrees
const latitude = 53.2352; // approx 53º10N
const longitude = -145.235; // approx 145º10W
const result = houses(JDUT, latitude, longitude, "P"); // P = placidus house system
console.log("logging houses:", result);

The results from the calc and houses functions return values in "celestial longitude" by default. To convert those into astrological signs, you must divide it in 12 parts of 30 degrees. For example if a planet's longitude is 124.2352 that means, from 0-30 is aries, from 30-60 is taurus, from 60-90 is gemini, from 90-120 is cancer, from 120-150 is leo, that planet is at 4.2353 degrees of leo.

Thats basically the extent of what i can help you with on here, you must figure out timezones, coordinates and graphics by yourself, i can only help you with understanding the technicalities of the raw ephemeris data here.

To create something like a graphical chart, you will need a drawing system like canvas or svg and knowledge of trigonometry to perform the necessary graphical calculations, for example drawing a circle and then drawing a point at X degrees of the circle, etc...

krisztianb commented 2 years ago

You might also want to take a look at this: https://github.com/milanpredic/astrochart instead of reinventing the wheel.

nodeperson commented 2 years ago

Excellent! Thank you!

zoultrex commented 1 month ago

You might also want to take a look at this: https://github.com/milanpredic/astrochart instead of reinventing the wheel.

Do you know of any other libraries like this one that are simple to use just for astrological charts, the one you pointed out won't install for me