- merged offline functionality

- added emscripten makefile
This commit is contained in:
Sublunar Space 2019-01-02 16:27:37 +01:00
parent 63260b2dee
commit 08a6d31d32
9 changed files with 137424 additions and 53 deletions

View file

@ -137,14 +137,14 @@ SL.Calendar = (function() {
* @param {float} lon - decimal degrees longitude of place on Earth
*/
function make(days, ts, lat, lon ) {
if ( days > 60 ) {
if ( days > 60 && $("body").attr("use") == "online" ) {
alert("Sorry, the calculation is limited to 60 days at the moment! Calculating the first 60 out of your desired "+days+" days!");
days = 60;
}
$('#caption .stoke').html('calculating ephemeris...please wait');
$("#grid").empty();
$('#download').remove();
if ( $("body").attr("use") == "offline" ) $("#grid").css({"visibility":"hidden"});
if ( $("body").attr("use") == "download" ) $("#grid").css({"visibility":"hidden"});
Filter.load();
// check if negative UTC offset will be before the beginning of UTC day and if yes, go back one day.
if ( moment.unix(ts).startOf('day') > moment.unix(ts).add( $("#offset").val(), 'hours') ) {
@ -153,19 +153,26 @@ SL.Calendar = (function() {
var files = document.getElementById('fileInput').files;
// check if JSON file is loaded. If not, calculate...
if (files.length <= 0) {
var url = "pmom.php?days="+days+"&ts="+ts+"&lat="+lat+"&lon="+lon;
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(json) {
Hours.moments = json.query;
$('#caption .stoke').html('generating planetary hours...please wait');
for (var i = 0, len = Hours.moments.length; i < len; i++) {
var everyday = setTimeout( Hour.make, 10, Hours.moments[i], i, Hours.moments.length, everyday);
NProgress.set(i/Hours.moments.length);
// checks if usecase is offline, then uses WebAssembly
if ( $("body").attr("use") != "online" ) {
var astrologer = new Worker('js/sweph.js');
astrologer.postMessage([days, ts, lat, lon]);
astrologer.onmessage = function(response) {
Hours.moments = response.data;
maker();
}
});
// checks if usecase is online, then uses server based calculation
} else {
var url = "pmom.php?days="+days+"&ts="+ts+"&lat="+lat+"&lon="+lon;
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(json) {
Hours.moments = json.query;
maker();
});
}
} else {
// display calendar data from JSON file
var fr = new FileReader();
@ -181,16 +188,20 @@ SL.Calendar = (function() {
return false;
}
Hours.moments = result.query;
$('#caption .stoke').html('generating planetary hours...please wait');
if ( Hours.moments.length > 0 ) {
maker();
}
fr.readAsText(files.item(0));
}
}
function maker() {
if ( Hours.moments.length > 0 ) {
$('#caption .stoke').html('generating planetary hours...please wait');
for (var i = 0, len = Hours.moments.length; i < len; i++) {
var everyday = setTimeout( Hour.make, 10, Hours.moments[i], i, Hours.moments.length, everyday);
NProgress.set(i/Hours.moments.length);
}
}
}
fr.readAsText(files.item(0));
}
}
return {
@ -247,11 +258,11 @@ SL.Calendar = (function() {
}
SL.Calendar.resetFileInput();
$('.planetaryhour').on('click', SL.Calendar.hourInfo);
$('#calendar').isotope('destroy');
if ( $('#calendar').data('isotope') ) $('#calendar').isotope('destroy');
NProgress.done();
clearTimeout(timeout);
$('#caption .stoke').empty();
if ( $("body").attr("use") == "offline" )
if ( $("body").attr("use") == "download" )
SL.Calendar.download( $("#grid").html(), fname, "skip", "html", "text/html; charset=utf-8");
}
}

5418
js/pmom.js Normal file

File diff suppressed because it is too large Load diff

BIN
js/pmom.wasm Normal file

Binary file not shown.

131818
js/pmom.wast Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

26
js/sweph.js Normal file
View file

@ -0,0 +1,26 @@
self.Module = {
locateFile: function (s) {
return s;
},
// Add this function
onRuntimeInitialized: function() {
var query = get();
postMessage(query);
}
};
self.importScripts("pmom.js");
self.data = {};
// to pass data from the main JS file
self.onmessage = function(messageEvent) {
self.data = messageEvent.data; // save the data
}
// gets executed when everything is ready.
self.get = function() {
var calc = self.Module.ccall('get', 'string', ['number', 'number', 'number', 'number'], [self.data[0], self.data[1], self.data[2], self.data[3]]);
var result = JSON.parse(calc);
return result.query;
}

60
lib/sweph/JSMakefile Normal file
View file

@ -0,0 +1,60 @@
# $Header$
# this Makefile creates a SwissEph library and a swetest sample on 64-bit
# Redhat Enterprise Linux RHEL 6.
# The mode marked as 'Linux' should also work with the GNU C compiler
# gcc on other systems.
# If you modify this makefile for another compiler, please
# let us know. We would like to add as many variations as possible.
# If you get warnings and error messages from your compiler, please
# let us know. We like to fix the source code so that it compiles
# free of warnings.
# send email to the Swiss Ephemeris mailing list.
#
CFLAGS = -g -Wall -fPIC -O2 -DVARIANT=2 # for Linux and other gcc systems
OP=$(CFLAGS)
CC=emcc #for Linux
# compilation rule for general cases
.o :
$(CC) $(OP) -o $@ $? -lm
.c.o:
$(CC) -c $(OP) $<
SWEOBJ = swedate.o swehouse.o swejpl.o swemmoon.o swemplan.o swepcalc.o sweph.o\
swepdate.o swephlib.o swecl.o swehel.o
pmom: pmom.o libswe.a
$(CC) $(OP) -o pmom.js pmom.o -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -L. -lswe -lm -ldl
install:
mv pmom.js ../../js/; mv pmom.wasm ../../js/; mv pmom.wast ../../js/
# create an archive and a dynamic link libary fro SwissEph
# a user of this library will inlcude swephexp.h and link with -lswe
libswe.a: $(SWEOBJ)
ar r libswe.a $(SWEOBJ)
libswe.so: $(SWEOBJ)
$(CC) -shared -o libswe.so $(SWEOBJ)
clean:
rm -f *.o *.wasm *.wast *.js *.html pmom libswe*; rm ../../js/pmom.*
###
swecl.o: swejpl.h sweodef.h swephexp.h swedll.h sweph.h swephlib.h
sweclips.o: sweodef.h swephexp.h swedll.h
swedate.o: swephexp.h sweodef.h swedll.h
swehel.o: swephexp.h sweodef.h swedll.h
swehouse.o: swephexp.h sweodef.h swedll.h swephlib.h swehouse.h
swejpl.o: swephexp.h sweodef.h swedll.h sweph.h swejpl.h
swemini.o: swephexp.h sweodef.h swedll.h
swemmoon.o: swephexp.h sweodef.h swedll.h sweph.h swephlib.h
swemplan.o: swephexp.h sweodef.h swedll.h sweph.h swephlib.h swemptab.h
swepcalc.o: swepcalc.h swephexp.h sweodef.h swedll.h
sweph.o: swejpl.h sweodef.h swephexp.h swedll.h sweph.h swephlib.h
swephlib.o: swephexp.h sweodef.h swedll.h sweph.h swephlib.h
swetest.o: swephexp.h sweodef.h swedll.h

View file

@ -13,7 +13,7 @@
# send email to the Swiss Ephemeris mailing list.
#
CFLAGS = -g -Wall -fPIC -O2 # for Linux and other gcc systems
CFLAGS = -g -Wall -fPIC -O2 -DVARIANT=1 # for Linux and other gcc systems
OP=$(CFLAGS)
CC=cc #for Linux

View file

@ -3,12 +3,23 @@
* License MIT: http://www.opensource.org/licenses/MIT
*/
#define ONLINE 1
#define OFFLINE 2
#ifndef VARIANT
#error "VARIANT needs to be set"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "swephexp.h"
#if VARIANT == OFFLINE
#include <emscripten.h>
#endif
/**
* converts Julian Day to UNIX epoch
*/
@ -254,34 +265,19 @@ int * solar_moment(double lat, double lon, unsigned long unixtime, unsigned long
/**
* calculates JSON Ephemeris Object
*/
int main(int argc,char* argv[]) // days, timestamp, latitude, longitude
const char * pmom(int days, long epoch, double latitude, double longitude, int buflen) // days, timestamp, latitude, longitude
{
double latitude = 0.0;
double longitude = 0.0;
unsigned long epoch = 0, queryday;
unsigned long queryday;
struct tm *tmp;
time_t timestamp = 0;
int32 year, month, day, hour, minute, second, j, h, m, interval, start, days = 1, hm;
int32 year, month, day, hour, minute, second, j, h, m, interval, start, hm;
double hours;
unsigned long solar[9] = {0};
double lunar[3] = {0.0}, planet[16] = {0.0};
char* Buffer = malloc(buflen);
int length = 0;
// read and assign commandline arguments
if ( argc > 1 ) {
days = atof(argv[1]);
}
if ( argc > 3 ) {
latitude = atof(argv[3]);
longitude = atof(argv[4]);
}
if ( argc > 2 ) {
sscanf(argv[2], "%lu", &epoch);
timestamp = epoch;
}
else {
time(&timestamp);
epoch = (int) timestamp;
}
timestamp = epoch;
// take timestamp, convert it to year, month, day, decimal hour
tmp = gmtime(&timestamp);
@ -296,7 +292,7 @@ int main(int argc,char* argv[]) // days, timestamp, latitude, longitude
// start of Almanac
queryday = time_to_epoch(year, month, day, 0, 0, 0);
j = 0;
printf("{ \"query\":[");
length += snprintf(Buffer+length, buflen-length, "{ \"query\":[");
// calculate all hours for all days
while ( j <= days ) {
@ -316,18 +312,65 @@ int main(int argc,char* argv[]) // days, timestamp, latitude, longitude
planetary_moment(latitude, longitude, hm, lunar, planet);
printf(
length += snprintf(Buffer+length, buflen-length,
"{ \"ts\": %d, \"lunar\": { \"day\": %d, \"angle\": %f, \"phase\": %f }, \"planetary\": { \"day\": { \"no\": %d, \"start\": %d, \"end\": %d }, \"night\": { \"start\": %d, \"end\": %d }, \"hour\": { \"no\": %d, \"start\": %d, \"end\": %d, \"length\": { \"day\": %d, \"night\": %d } } }, \"ephemeris\": { \"sun\": { \"deg\": %f, \"speed\": %f }, \"moon\": { \"deg\": %f, \"speed\": %f }, \"mercury\": { \"deg\": %f, \"speed\": %f }, \"venus\": { \"deg\": %f, \"speed\": %f }, \"mars\": { \"deg\": %f, \"speed\": %f }, \"jupiter\": { \"deg\": %f, \"speed\": %f }, \"saturn\": { \"deg\": %f, \"speed\": %f }, \"asc\": { \"deg\": %f }, \"mc\": { \"deg\": %f } } }",
hm, (int) lunar[0], lunar[1], lunar[2], solar[7], solar[1], solar[2]-1, solar[2], solar[3]-1, h, hm, hm+interval-1, solar[5], solar[6], planet[0], planet[7], planet[1], planet[8], planet[2], planet[9], planet[3], planet[10], planet[4], planet[11], planet[5], planet[12], planet[6], planet[13], planet[14], planet[15]
);
if(j == days && h == 23) printf("");
else printf(", ");
if(j == days && h == 23) length += snprintf(Buffer+length, buflen-length, "");
else length += snprintf(Buffer+length, buflen-length, ", ");
h++;
}
queryday = solar[3];
j++;
};
printf("]}");
length += snprintf(Buffer+length, buflen-length, "]}");
return Buffer;
}
#if VARIANT == ONLINE
int main(int argc,char* argv[]) // days, timestamp, latitude, longitude
{
double latitude = 0.0;
double longitude = 0.0;
unsigned long epoch = 0;
time_t timestamp = 0;
int32 days = 1, buflen;
// read and assign commandline arguments
if ( argc > 1 ) {
days = atof(argv[1]);
}
if ( argc > 3 ) {
latitude = atof(argv[3]);
longitude = atof(argv[4]);
}
if ( argc > 2 ) {
sscanf(argv[2], "%lu", &epoch);
timestamp = epoch;
}
else {
time(&timestamp);
epoch = (int) timestamp;
}
timestamp = epoch;
buflen = days * 100000;
printf(pmom( days, epoch, latitude, longitude, buflen));
}
#endif
#if VARIANT == OFFLINE
EMSCRIPTEN_KEEPALIVE
const char * get(int days, long epoch, double latitude, double longitude) // days, timestamp, latitude, longitude
{
time_t timestamp = 0;
int32 buflen;
timestamp = epoch;
buflen = days * 100000;
return pmom( days, epoch, latitude, longitude, buflen);
}
#endif