mivion / swisseph

Swiss Ephemeris binding for node.js
GNU General Public License v2.0
202 stars 71 forks source link

On how the library is to be used #73

Open gkostov opened 3 years ago

gkostov commented 3 years ago

Hey guys,

All examples refer to functions from the Swiss Ephemeris library API (like swe_calc_ut()) but in the main file of this library /lib/swisseph.js there is that calc() function for which I can't find any documentation. Is that function meant to be used at all? It seems to produce results that are very close to the examples like /examples/planets.js but are not exactly equal. I'm still trying to figure out how to properly use the library so any help would be appreciated.

Thanks

krisztianb commented 3 years ago

Hi. I've just discovered this library myself and was asking myself the same question.

But first I ran into several problems installing it:

Then installing the package finally worked. But to get back to your question:

When you install the library it executes a C++ build which generates this file: myProject\node_modules\swisseph\build\Releases\wisseph.node. That is the file which is imported in the index.js file you are talking about. It is actually a C++ binary which Node.js can load. You can find more about this here.

The index.js file imports the Swiss Ephemeris library into an object and adds some constants to it. Then that object is exported. That object is what we import in our project by doing require("swisseph"). By importing the object you can access all the Swiss Ephemeris functions.

But not just some constants are added. Also this strange calc function is added. I don't know why it is there.

Executing the code from the README:

var swisseph = require("swisseph");
var date = { year: 2015, month: 1, day: 1, hour: 0 };
var julday = swisseph.swe_julday(date.year, date.month, date.day, date.hour, swisseph.SE_GREG_CAL);
console.log(julday);

Gives me: 2457023.5

krisztianb commented 3 years ago

I think I now understand what the "calc" function does. It looks like it takes a data object (called options) and extends it with missing/converted data. So to answer your question: You don't have to use it. To me it looks like a test function, because it calls "swe_close" at the end which releases all resources (open files and allocated memory) used by the Swiss Ephemeris DLL.

Example (TypeScript code):

import swisseph from "swisseph";

var ret = swisseph.calc(
    {
        date: {
            gregorian: { terrestrial: { year: 2021, month: 9, day: 17, hour: 21 } },
        },
        body: {
            id: 1,
            position: {
                longitude: 47,
                latitude: 16,
            },
        },
        observer: {
            geographic: {
                longitude: 47,
                latitude: 16,
                height: 2,
            },
        },
    },
    (result) => {
        console.log(result);
    }
);

console.log(ret);

Output:

{
  date: {
    gregorian: {
      terrestrial: { terrestrial: { year: 2021, month: 9, day: 17, hour: 21 } },
      universal: { terrestrial: { year: 2021, month: 9, day: 17, hour: 20 } },
      delta: 69.81315104729428
    },
    julian: {
      terrestrial: 2459475.375,
      delta: 0.000808022581565906,
      universal: 2459475.3741919775
    }
  },
  body: {
    id: 1,
    position: {
      longitude: { decimalDegree: 47 };
      latitude: { decimalDegree: 16 };
      distance: 0.0025105828529191744,
      longitudeSpeed: 9.71724408884711,
      latitudeSpeed: 2.283500123212434,
      distanceSpeed: 0.00016263597412378983,
      rflag: 33028
    },
    name: 'Moon'
  },
  observer: {
    geographic: { longitude: 47, latitude: 16, height: 2 },
    ephemeris: 'moshier'
  }
}