Skip to content

Commit

Permalink
Merge pull request #32 from knyazhe1/rise_trans_true_hor
Browse files Browse the repository at this point in the history
Add rise_trans_true_hor method + other fixes
  • Loading branch information
mivion committed Jan 13, 2016
2 parents fd30a47 + 971d53d commit 3f14c8a
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/eclipses.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function MoonEclipseWhenGlob(jET) {
glob.flag = result.rflag;
});
var loc = MoonEclipseWhenLoc(glob.et - 1);
glob.local = Math.abs(glob.et - loc.et) < 1;
glob.loc = Math.abs(glob.et - loc.et) < 1;
if(glob.loc) {
glob.et = loc.et;
glob.flag += loc.flag;
Expand Down
201 changes: 171 additions & 30 deletions examples/trans.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,179 @@
var assert = require ('assert');
var fs = require ('fs');
var swisseph = require ('..');
var swisseph = require('..');
var assert = require('assert');

// Test date
var date = {year: 2012, month: 1, day: 1, hour: 0};
console.log ('Test date:', date);
// Moscow, Russian Federation
var longitude = 37.61556;
var latitude = 55.75222;
var altitude = 156; // meters above sea
var tzone = 3; // GMT+3

var flag = swisseph.SEFLG_SPEED | swisseph.SEFLG_MOSEPH;
var pressure = 1000; // in hPa
var temperature = 10; // in Celsius degree
var angle = 10; // in degree above horizont

var iflag = swisseph.SEFLG_SPEED | swisseph.SEFLG_MOSEPH | swisseph.SEFLG_TOPOCTR;

// path to ephemeris data
swisseph.swe_set_ephe_path (__dirname + '/../ephe');
swisseph.swe_set_topo(longitude, latitude, altitude);

// Julian day
swisseph.swe_julday (date.year, date.month, date.day, date.hour, swisseph.SE_GREG_CAL, function (julday_ut) {
assert.equal (julday_ut, 2455927.5);
console.log ('Julian UT day for date:', julday_ut);

var long = 64.22;
var lat = 173.22;
var height = 0;
var atpress = 146;
var attemp = 26;

// Sun rise
swisseph.swe_rise_trans (julday_ut, swisseph.SE_SUN, '', flag, swisseph.SE_CALC_RISE,
long, lat, height, atpress, attemp, function callback (result) {
assert (!result.error, result.error);
console.log (result.transitTime);
})
var year = {
start: new Date(2016, 0, 1),
end: new Date(2016, 11, 31, 23, 59, 59, 999)
};

var msPerDay = 1000 * 60 * 60 * 24;

function JulianDate(jsDate) {
var jET;
with(jsDate)
swisseph.swe_utc_time_zone (
getFullYear(),
getMonth() + 1,
getDate(),
getHours(),
getMinutes(),
getSeconds() + getMilliseconds() / 1000,
tzone,
function (result) {jET = result;}
);
with(jET)
swisseph.swe_utc_to_jd (
year,
month,
day,
hour,
minute,
second,
swisseph.SE_GREG_CAL,
function (result) {
assert (!result.error, result.error);
jET = result.julianDayET;
});
return jET;
};

function LocalDate(jET) {
swisseph.swe_jdet_to_utc (
jET,
swisseph.SE_GREG_CAL,
function (result) {jET = result;}
);
with(jET)
swisseph.swe_utc_time_zone (
year,
month,
day,
hour,
minute,
second,
-tzone,
function (result) {jET = result;}
);
var intS = Math.floor(jET.second);
var mS = Math.round(1000 * (jET.second - intS));
with(jET)
var jsDate = new Date(year, month - 1, day, hour, minute, intS, mS);
return jsDate;
};

// Moon rise
swisseph.swe_rise_trans (julday_ut, swisseph.SE_MOON, '', flag, swisseph.SE_CALC_RISE,
long, lat, height, atpress, attemp, function (result) {
function BodyName(Body) {
if(typeof(Body) === 'string')
return Body;
var name;
swisseph.swe_get_planet_name(Body, function(result) {
assert (!result.error, result.error);
console.log (result.transitTime);
})
name = result.name;
});
return name;
};

function TransName(Trans) {
var names = [
'rise', // swisseph.SE_CALC_RISE
'set', // swisseph.SE_CALC_SET
'upper meridian', // swisseph.SE_CALC_MTRANSIT
'lower meridian' // swisseph.SE_CALC_ITRANSIT
];
return names[Math.log2(Trans)] + ' transit';
};

function RiseTrans(jET, Planet, Star, TransKind, HorzAngle) {
var et;
if(HorzAngle == undefined) {
swisseph.swe_rise_trans(jET,
Planet,
Star,
iflag,
TransKind,
longitude,
latitude,
altitude,
pressure,
temperature,
function (result) {
assert (!result.error, result.error);
et = result.transitTime
});
return et;
};

swisseph.swe_rise_trans_true_hor(jET,
Planet,
Star,
iflag,
TransKind,
longitude,
latitude,
altitude,
pressure,
temperature,
HorzAngle,
function (result) {
assert (!result.error, result.error);
et = result.transitTime
});
return et;
};

//--- main --- //
console.log('NOTE: This example using Moshier ephemeris!')
console.log('Some stars and bodies maybe not rise or not set under horizont!');
var dh, dt;
var jd = JulianDate(new Date());

var bodyList = [
swisseph.SE_SUN,
swisseph.SE_MOON,
'Sirius',
'Algol'
];

bodyList.forEach(function(b) {
[swisseph.SE_CALC_RISE, swisseph.SE_CALC_MTRANSIT].forEach(function(trans) {
var et = RiseTrans(jd,
(typeof(b) === 'string') ? 0 : b,
(typeof(b) === 'string') ? b : '',
trans);
if(et === -2)
dh = 'never going to happen'
else
dh = new Date(LocalDate(et));

et = RiseTrans(jd,
(typeof(b) === 'string') ? 0 : b,
(typeof(b) === 'string') ? b : '',
trans,
angle);
if(et === -2)
dt = 'never going to happen'
else
dt = new Date(LocalDate(et));

console.log({
body: BodyName(b),
kind: TransName(trans),
horz: dh,
angl: dt
});
});
});
87 changes: 83 additions & 4 deletions src/eclipse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace v8;
* int32 swe_gauquelin_sector(double t_ut, int32 ipl, char *starname, int32 iflag, int32 imeth, double *geopos (in[3]), double atpress, double attemp, double *dgsect (out[1]), char *serr (out[AS_MAXCH]))
* =>
* swe_gauquelin_sector(double t_ut, int32 ipl, char *starname, int32 iflag, int32 imeth, double longitude, double latitude, double height, double atpress, double attemp[, function callback (result)]) = {
* name: string,
* name: string, //star
* gauquelinSector: double,
* error: string
* }
Expand Down Expand Up @@ -380,7 +380,7 @@ NAN_METHOD(node_swe_sol_eclipse_when_loc) {
* =>
* swe_lun_occult_when_loc(double tjd_start, int32 ipl, char *starname, int32 ifl, double longitude, double latitude, double height, int32 backward[, function callback (result)]) = {
* rflag: long,
* star: string,
* name: string, // star
* maximum: double, // tret [0]
* first: double, // tret [1]
* second: double, // tret [2]
Expand Down Expand Up @@ -542,7 +542,7 @@ NAN_METHOD(node_swe_sol_eclipse_when_glob) {
* =>
* swe_lun_occult_when_glob(double tjd_start, int32 ipl, char *starname, int32 ifl, int32 ifltype, int32 backward[, function callback (result)]) = {
* rflag: long,
* name: string,
* name: string, // star
* maximum: double, // tret [0]
* noon: double, // tret [1]
* begin: double, // tret [2]
Expand Down Expand Up @@ -1184,6 +1184,7 @@ NAN_METHOD(node_swe_azalt_rev) {
* int32 swe_rise_trans(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double *geopos (in[3]), double atpress, double attemp, double *tret (out[1]), char *serr (out[AS_MAXCH]))
* =>
* swe_rise_trans(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double longitude, double latitude, double height, double atpress, double attemp[, function callback (result)]) = {
* name: string, // star
* transitTime: double, // tret [0]
* error: string
* }
Expand Down Expand Up @@ -1235,10 +1236,88 @@ NAN_METHOD(node_swe_rise_trans) {
);

Local <Object> result = Nan::New<Object> ();

// http://www.astro.com/swisseph/swephprg.htm#_Toc433200782
if (rflag == -1) { // if an error occurred (usually an ephemeris problem)
result->Set (Nan::New<String> ("error").ToLocalChecked(), Nan::New<String> (serr).ToLocalChecked());
} else {
if (rflag == -2) { // if a rising or setting event was not found because the object is circumpolar
tret = -2;
};
result->Set (Nan::New<String> ("name").ToLocalChecked(), Nan::New<String> (star).ToLocalChecked());
result->Set (Nan::New<String> ("transitTime").ToLocalChecked(), Nan::New<Number> (tret));
};

if (rflag < 0) {
HandleCallback (info, result);
info.GetReturnValue().Set (result);
};

/**
* int32 swe_rise_trans_true_hor(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double *geopos (in[3]), double atpress, double attemp, double horhgt, double *tret (out[1]), char *serr (out[AS_MAXCH]))
* =>
* swe_rise_trans_true_hor(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double longitude, double latitude, double height, double atpress, double horhgt, double attemp[, function callback (result)]) = {
* name: string, // star
* transitTime: double, // tret [0]
* error: string
* }
*/
NAN_METHOD(node_swe_rise_trans_true_hor) {
Nan::HandleScope scope;

if (info.Length () < 11) {
Nan::ThrowTypeError ("Wrong number of arguments");
};

if (
!info [0]->IsNumber () ||
!info [1]->IsNumber () ||
!info [2]->IsString () ||
!info [3]->IsNumber () ||
!info [4]->IsNumber () ||
!info [5]->IsNumber () ||
!info [6]->IsNumber () ||
!info [7]->IsNumber () ||
!info [8]->IsNumber () ||
!info [9]->IsNumber () ||
!info [10]->IsNumber ()
) {
Nan::ThrowTypeError ("Wrong type of arguments");
};

double geopos [10] = {0};
double tret;
char star [AS_MAXCH];
char serr [AS_MAXCH];
long rflag;

::strcpy (star, * String::Utf8Value (info [2]->ToString ()));

geopos [0] = info [5]->NumberValue ();
geopos [1] = info [6]->NumberValue ();
geopos [2] = info [7]->NumberValue ();

rflag = ::swe_rise_trans_true_hor (
info [0]->NumberValue (),
(int)info [1]->NumberValue (),
star,
(int)info [3]->NumberValue (),
(int)info [4]->NumberValue (),
geopos,
info [8]->NumberValue (),
info [9]->NumberValue (),
info [10]->NumberValue (),
&tret, serr
);

Local <Object> result = Nan::New<Object> ();

// http://www.astro.com/swisseph/swephprg.htm#_Toc433200782
if (rflag == -1) { // if an error occurred (usually an ephemeris problem)
result->Set (Nan::New<String> ("error").ToLocalChecked(), Nan::New<String> (serr).ToLocalChecked());
} else {
if (rflag == -2) { // if a rising or setting event was not found because the object is circumpolar
tret = -2;
};
result->Set (Nan::New<String> ("name").ToLocalChecked(), Nan::New<String> (star).ToLocalChecked());
result->Set (Nan::New<String> ("transitTime").ToLocalChecked(), Nan::New<Number> (tret));
};
Expand Down
18 changes: 15 additions & 3 deletions src/eclipse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace v8;
* int32 swe_gauquelin_sector(double t_ut, int32 ipl, char *starname, int32 iflag, int32 imeth, double *geopos, double atpress, double attemp, double *dgsect, char *serr)
* =>
* swe_gauquelin_sector(double t_ut, int32 ipl, char *starname, int32 iflag, int32 imeth, double longitude, double latitude, double height, double atpress, double attemp[, function callback (result)]) = {
* name: string,
* name: string, //star
* gauquelinSector: double,
* error: string
* }
Expand Down Expand Up @@ -112,7 +112,7 @@ NAN_METHOD(node_swe_sol_eclipse_when_loc);
* =>
* swe_lun_occult_when_loc(double tjd_start, int32 ipl, char *starname, int32 ifl, double longitude, double latitude, double height, int32 backward[, function callback (result)]) = {
* rflag: long,
* star: string,
* name: string, // star
* maximum: double, // tret [0]
* first: double, // tret [1]
* second: double, // tret [2]
Expand Down Expand Up @@ -157,7 +157,7 @@ NAN_METHOD(node_swe_sol_eclipse_when_glob);
* =>
* swe_lun_occult_when_glob(double tjd_start, int32 ipl, char *starname, int32 ifl, int32 ifltype, int32 backward[, function callback (result)]) = {
* rflag: long,
* name: string,
* name: string, // star
* maximum: double, // tret [0]
* noon: double, // tret [1]
* begin: double, // tret [2]
Expand Down Expand Up @@ -316,12 +316,24 @@ NAN_METHOD(node_swe_azalt_rev);
* int32 swe_rise_trans(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double *geopos, double atpress, double attemp, double *tret, char *serr)
* =>
* swe_rise_trans(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double longitude, double latitude, double height, double atpress, double attemp[, function callback (result)]) = {
* name: string, // star
* transitTime: double, // tret [0]
* error: string
* }
*/
NAN_METHOD(node_swe_rise_trans);

/**
* int32 swe_rise_trans_true_hor(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double *geopos (in[3]), double atpress, double attemp, double horhgt, double *tret (out[1]), char *serr (out[AS_MAXCH]))
* =>
* swe_rise_trans_true_hor(double tjd_ut, int32 ipl, char *starname, int32 epheflag, int32 rsmi, double longitude, double latitude, double height, double atpress, double horhgt, double attemp[, function callback (result)]) = {
* name: string, // star
* transitTime: double, // tret [0]
* error: string
* }
*/
NAN_METHOD(node_swe_rise_trans_true_hor);

/**
* int32 swe_nod_aps(double tjd_et, int32 ipl, int32 iflag, int32 method, double *xnasc, double *xndsc, double *xperi, double *xaphe, char *serr)
* =>
Expand Down
Loading

0 comments on commit 3f14c8a

Please sign in to comment.