@sffjunkie/astral
Version:
calculations for the position of the sun and the moon
250 lines (248 loc) • 8.03 kB
JavaScript
;
// Copyright 2009-2020, Simon Kennedy, sffjunkie+code@gmail.com
Object.defineProperty(exports, "__esModule", { value: true });
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var luxon_1 = require("luxon");
var geocoder = require("./geocoder");
exports.geocoder = geocoder;
var sun = require("./sun");
exports.sun = sun;
var moon = require("./moon");
exports.moon = moon;
var location_1 = require("./location");
exports.Location = location_1.Location;
var error_1 = require("./error");
/**
* Returns the current time in the specified time zone
* @param timezone - The timezone to use
*/
function now(timezone) {
if (timezone === void 0) { timezone = 'utc'; }
var time = luxon_1.DateTime.utc();
if (timezone !== 'utc') {
time = time.setZone(timezone);
}
return time;
}
exports.now = now;
/**
* Returns the current date in the specified time zone
* @param timezone - The timezone to use
*/
function today(timezone) {
if (timezone === void 0) { timezone = 'utc'; }
var dt = now(timezone);
dt.set({ hour: 0, minute: 0, second: 0 });
return dt;
}
exports.today = today;
/**
* Converts as string of the form `degrees°minutes'seconds"[N|S|E|W]`, or a number encoded as a string, to a number
*
* N and E return positive values
* S and W return negative values
*
* @param dms - string to convert
* @param limit - Limit the value between ± `limit` (if provided)
* @throws [[ValueError]] if `dms` is a string that can not be converted.
*/
function dmsToNumber(dms, limit) {
if (dms === undefined) {
console.log('2');
}
var res;
res = Number(dms);
if (isNaN(res)) {
var re = /(?<deg>\d{1,3})[°]?((?<min>\d{1,2})[′'])?((?<sec>\d{1,2})[″\"])?(?<dir>[NSEW])?/i;
var m = dms.toString().match(re);
if (m === null) {
throw new error_1.ValueError("Unable to convert " + dms + " to a float");
}
var deg = m.groups['deg'] || '0.0';
var min_ = m.groups['min'] || '0.0';
var sec = m.groups['sec'] || '0.0';
var dir_ = m.groups['dir'] || 'E';
res = parseFloat(deg);
if (min_)
res += parseFloat(min_) / 60;
if (sec)
res += parseFloat(sec) / 3600;
dir_ = dir_.toUpperCase();
if (dir_ === 'S' || dir_ === 'W') {
res = -res;
}
}
if (limit) {
if (res > limit)
res = limit;
else if (res < -limit)
res = -limit;
}
return res;
}
exports.dmsToNumber = dmsToNumber;
/**
* The depression angle in degrees for the dawn/dusk calculations
*/
var Depression;
(function (Depression) {
Depression[Depression["CIVIL"] = 6] = "CIVIL";
Depression[Depression["NAUTICAL"] = 12] = "NAUTICAL";
Depression[Depression["ASTRONOMICAL"] = 18] = "ASTRONOMICAL";
})(Depression || (Depression = {}));
exports.Depression = Depression;
/**
* Direction of the sun either RISING or SETTING
*/
var SunDirection;
(function (SunDirection) {
SunDirection[SunDirection["RISING"] = 1] = "RISING";
SunDirection[SunDirection["SETTING"] = -1] = "SETTING";
})(SunDirection || (SunDirection = {}));
exports.SunDirection = SunDirection;
/**
* Defines the location of an observer on Earth.
* Latitude and longitude can be set either as a float or as a string.
* For strings they must be of the form
*
* degrees°minutes'seconds"[N|S|E|W] e.g. 51°31'N
*
* `minutes’` & `seconds”` are optional.
*
* Elevations are optional and are either
*
* <ul>
* <li>A float that is the elevation in metres above a location, if the nearest
* obscuring feature is the horizon</li>
* <li>or a tuple of the elevation in metres and the distance in metres to the
nearest obscuring feature.</li>
* </ul>
*/
var Observer = /** @class */ (function () {
/**
* Constructor
* @param latitude - Latitude - Northern latitudes should be positive
* @param longitude - Longitude - Eastern longitudes should be positive
* @param elevation - Elevation and/or distance to nearest obscuring feature
in metres above/below the location.
*/
function Observer(latitude, longitude, elevation) {
if (latitude) {
this.latitude = dmsToNumber(latitude, 90.0);
}
else {
this.latitude = 51.4733;
}
if (longitude) {
this.longitude = dmsToNumber(longitude, 180.0);
}
else {
this.longitude = -0.0008333;
}
if (elevation) {
this.elevation = elevation;
}
else {
this.elevation = 0.0;
}
}
Observer.fromObject = function (obj) {
var n = new Observer();
if ('latitude' in obj)
n.latitude = dmsToNumber(obj['latitude'], 90.0);
if ('longitude' in obj)
n.longitude = dmsToNumber(obj['longitude'], 90.0);
if ('elevation' in obj)
n.elevation = obj['elevation'];
return n;
};
return Observer;
}());
exports.Observer = Observer;
var LocationInfo = /** @class */ (function () {
/**
* Constructor
* @param name - Location name (can be any string)
* @param region - Region location is in (can be any string)
* @param timezone - The location's time zone
* @param latitude - Latitude - Northern latitudes should be positive
* @param longitude - Longitude - Eastern longitudes should be positive
*/
function LocationInfo(name, region, timezone, latitude, longitude) {
this.name = name;
this.region = region;
this.timezone = timezone;
if (name) {
this.name = name;
}
else {
this.name = 'Greenwich';
}
if (region) {
this.region = region;
}
else {
this.region = 'England';
}
if (timezone) {
this.timezone = timezone;
}
else {
this.timezone = 'Europe/London';
}
if (latitude) {
this.latitude = dmsToNumber(latitude, 90.0);
}
else {
this.latitude = 51.4733;
}
if (longitude) {
this.longitude = dmsToNumber(longitude, 180.0);
}
else {
this.longitude = -0.0008333;
}
}
LocationInfo.fromObject = function (obj) {
var n = new LocationInfo();
if ('name' in obj)
n.name = obj['name'];
if ('region' in obj)
n.region = obj['region'];
if ('timezone' in obj)
n.timezone = obj['timezone'];
if ('latitude' in obj)
n.latitude = dmsToNumber(obj['latitude'], 90.0);
if ('longitude' in obj)
n.longitude = dmsToNumber(obj['longitude'], 180.0);
return n;
};
Object.defineProperty(LocationInfo.prototype, "observer", {
/** Get an observer instance for this location */
get: function () {
return new Observer(this.latitude, this.longitude, 0.0);
},
enumerable: true,
configurable: true
});
Object.defineProperty(LocationInfo.prototype, "timezone_group", {
/** Get the timezone group for this location */
get: function () {
return this.timezone.split('/')[0];
},
enumerable: true,
configurable: true
});
return LocationInfo;
}());
exports.LocationInfo = LocationInfo;