mirror of
https://github.com/cosinekitty/astronomy.git
synced 2025-12-23 23:58:15 -05:00
Instead of declaring all the "body" parameters in the TypeScript/JavaScript code to be strings, I created a string-valued enumerated type called Body. The same string values can still be passed in from JavaScript code, or callers can use syntax like Astronomy.Body.Moon. This improves the type checking inside the TypeScript source, plus it adds better documentation for each of the parameters. In the generated Markdown documentation, the user can click on the Body type and see all the supported bodies. The other three supported languages (C, C#, Python) already use enumerated types for bodies, so this brings the JavaScript version more in sync with them.
62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
/*
|
|
riseset.js - by Don Cross - 2019-06-15
|
|
|
|
Example Node.js program for Astronomy Engine:
|
|
https://github.com/cosinekitty/astronomy
|
|
|
|
This program calculates the time of the next
|
|
sunrise, sunset, moonrise, and moonset.
|
|
|
|
To execute, run the command:
|
|
node riseset latitude longitude [date]
|
|
*/
|
|
|
|
const Astronomy = require('./astronomy.js');
|
|
|
|
function DisplayEvent(name, evt) {
|
|
let text = evt ? evt.date.toISOString() : '';
|
|
console.log(name.padEnd(8) + ' : ' + text);
|
|
}
|
|
|
|
function ParseNumber(text, name) {
|
|
const x = Number(text);
|
|
if (!Number.isFinite(x)) {
|
|
console.error(`ERROR: Not a valid numeric value for ${name}: "${text}"`);
|
|
process.exit(1);
|
|
}
|
|
return x;
|
|
}
|
|
|
|
function ParseDate(text) {
|
|
const d = new Date(text);
|
|
if (!Number.isFinite(d.getTime())) {
|
|
console.error(`ERROR: Not a valid date: "${text}"`);
|
|
process.exit(1);
|
|
}
|
|
return d;
|
|
}
|
|
|
|
function Demo() {
|
|
if (process.argv.length === 4 || process.argv.length === 5) {
|
|
const latitude = ParseNumber(process.argv[2]);
|
|
const longitude = ParseNumber(process.argv[3]);
|
|
const observer = new Astronomy.Observer(latitude, longitude, 0);
|
|
const date = (process.argv.length === 5) ? ParseDate(process.argv[4]) : new Date();
|
|
let sunrise = Astronomy.SearchRiseSet(Astronomy.Body.Sun, observer, +1, date, 300);
|
|
let sunset = Astronomy.SearchRiseSet(Astronomy.Body.Sun, observer, -1, date, 300);
|
|
let moonrise = Astronomy.SearchRiseSet(Astronomy.Body.Moon, observer, +1, date, 300);
|
|
let moonset = Astronomy.SearchRiseSet(Astronomy.Body.Moon, observer, -1, date, 300);
|
|
console.log('search : ' + date.toISOString());
|
|
DisplayEvent('sunrise', sunrise);
|
|
DisplayEvent('sunset', sunset);
|
|
DisplayEvent('moonrise', moonrise);
|
|
DisplayEvent('moonset', moonset);
|
|
process.exit(0);
|
|
} else {
|
|
console.log('USAGE: node riseset.js latitude longitude [date]');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
Demo();
|