Using Sweph (Swiss Ephemeris) with Wasm (Emscripten)
Based on code written by @randogoth sublunar.almanac.
I extrapolated the sweph part with wasm and I created this web application that allows you to query the functions of the Swiss Ephemeris to generate ephemeris.
The application is ready to run.
The compiled files are:
To use the application run the index.html file using a local web server (localhost).astro.data (contains sweph files - virtual file system)
astro.js (browser readable file)
astro.wasm (binaries)
If you run the index.html file (file://) directly, browsers create a Cors error.
If you don't have a local web server you can use the emrun command (see below).
The most important change was the creation of a virtual file system.
Emscripten allows you to embed files that will be used within an application. Specifically, it creates a file with the ".data" extension which will contain all of the additional files.
To create a vfs you need to pass a parameter to the compiler (in the Makefile) indicating the directory containing the files, in our case "eph"
--preload-file ephThe compiler will create a ".data" file containing all the files in the folder.
From code just make sweph point to the "eph" directory
swe_set_ephe_path("eph");
Make sure Emscripten is installed and working 🙂. Here the documentation
From Command Line Interface (CLI) change to the sweph-wasm/lib/src directory.
To execute
make astro (compile sweph files and astro.c file)
make install (copy the files generated by the compiler to the right directory)
If you don't have a local web server (localhost) you can use the emrun command.
Just add --emrun to your compiler (Makefile), compile.
To run the application:
emrun index.html
The application loads these three files into the virtual file system:
seas_18.se1
semo_18.se1
sepl_18.se1
These are sweph's high-precision ephemeris files.
These files have a date range from the year 1800 to 2400.
If you want to widen the date range just add the others files in the sweph-wasm/lib/src/eph directory and compile.
The compiler will add all the files present in the directory and create the astro.data file.
Command:
make astro
make install
The astro.c file contains the logic to create the ephemeris. It interfaces with sweph and also contains functions that can be called from javascript.
A "web worker" is created from the browser which calls the "astro.js" file.
The web worker issues commands to functions in the astro.c file which returns a json string to the web worker. The return json is then processed by javascript.
The astro.js file is one of the files that is created by the compiler.
To enable data preloading, uncomment the line:
present in the index.html file