197 lines
7.1 KiB
JavaScript
197 lines
7.1 KiB
JavaScript
/*!
|
|
* satellite-js v5.0.0
|
|
* (c) 2013 Shashwat Kandadai and UCSC
|
|
* https://github.com/shashwatak/satellite-js
|
|
* License: MIT
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.days2mdhms = days2mdhms;
|
|
exports.invjday = invjday;
|
|
exports.jday = jday;
|
|
/* -----------------------------------------------------------------------------
|
|
*
|
|
* procedure days2mdhms
|
|
*
|
|
* this procedure converts the day of the year, days, to the equivalent month
|
|
* day, hour, minute and second.
|
|
*
|
|
* algorithm : set up array for the number of days per month
|
|
* find leap year - use 1900 because 2000 is a leap year
|
|
* loop through a temp value while the value is < the days
|
|
* perform int conversions to the correct day and month
|
|
* convert remainder into h m s using type conversions
|
|
*
|
|
* author : david vallado 719-573-2600 1 mar 2001
|
|
*
|
|
* inputs description range / units
|
|
* year - year 1900 .. 2100
|
|
* days - julian day of the year 0.0 .. 366.0
|
|
*
|
|
* outputs :
|
|
* mon - month 1 .. 12
|
|
* day - day 1 .. 28,29,30,31
|
|
* hr - hour 0 .. 23
|
|
* min - minute 0 .. 59
|
|
* sec - second 0.0 .. 59.999
|
|
*
|
|
* locals :
|
|
* dayofyr - day of year
|
|
* temp - temporary extended values
|
|
* inttemp - temporary int value
|
|
* i - index
|
|
* lmonth[12] - int array containing the number of days per month
|
|
*
|
|
* coupling :
|
|
* none.
|
|
* --------------------------------------------------------------------------- */
|
|
function days2mdhms(year, days) {
|
|
var lmonth = [31, year % 4 === 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
var dayofyr = Math.floor(days);
|
|
|
|
// ----------------- find month and day of month ----------------
|
|
var i = 1;
|
|
var inttemp = 0;
|
|
while (dayofyr > inttemp + lmonth[i - 1] && i < 12) {
|
|
inttemp += lmonth[i - 1];
|
|
i += 1;
|
|
}
|
|
var mon = i;
|
|
var day = dayofyr - inttemp;
|
|
|
|
// ----------------- find hours minutes and seconds -------------
|
|
var temp = (days - dayofyr) * 24.0;
|
|
var hr = Math.floor(temp);
|
|
temp = (temp - hr) * 60.0;
|
|
var minute = Math.floor(temp);
|
|
var sec = (temp - minute) * 60.0;
|
|
return {
|
|
mon: mon,
|
|
day: day,
|
|
hr: hr,
|
|
minute: minute,
|
|
sec: sec
|
|
};
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
*
|
|
* procedure jday
|
|
*
|
|
* this procedure finds the julian date given the year, month, day, and time.
|
|
* the julian date is defined by each elapsed day since noon, jan 1, 4713 bc.
|
|
*
|
|
* algorithm : calculate the answer in one step for efficiency
|
|
*
|
|
* author : david vallado 719-573-2600 1 mar 2001
|
|
*
|
|
* inputs description range / units
|
|
* year - year 1900 .. 2100
|
|
* mon - month 1 .. 12
|
|
* day - day 1 .. 28,29,30,31
|
|
* hr - universal time hour 0 .. 23
|
|
* min - universal time min 0 .. 59
|
|
* sec - universal time sec 0.0 .. 59.999
|
|
*
|
|
* outputs :
|
|
* jd - julian date days from 4713 bc
|
|
*
|
|
* locals :
|
|
* none.
|
|
*
|
|
* coupling :
|
|
* none.
|
|
*
|
|
* references :
|
|
* vallado 2007, 189, alg 14, ex 3-14
|
|
*
|
|
* --------------------------------------------------------------------------- */
|
|
function jdayInternal(year, mon, day, hr, minute, sec) {
|
|
var msec = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0;
|
|
return 367.0 * year - Math.floor(7 * (year + Math.floor((mon + 9) / 12.0)) * 0.25) + Math.floor(275 * mon / 9.0) + day + 1721013.5 + ((msec / 60000 + sec / 60.0 + minute) / 60.0 + hr) / 24.0 // ut in days
|
|
// # - 0.5*sgn(100.0*year + mon - 190002.5) + 0.5;
|
|
;
|
|
}
|
|
|
|
function jday(year, mon, day, hr, minute, sec, msec) {
|
|
if (year instanceof Date) {
|
|
var date = year;
|
|
return jdayInternal(date.getUTCFullYear(), date.getUTCMonth() + 1,
|
|
// Note, this function requires months in range 1-12.
|
|
date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
|
|
}
|
|
return jdayInternal(year, mon, day, hr, minute, sec, msec);
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
*
|
|
* procedure invjday
|
|
*
|
|
* this procedure finds the year, month, day, hour, minute and second
|
|
* given the julian date. tu can be ut1, tdt, tdb, etc.
|
|
*
|
|
* algorithm : set up starting values
|
|
* find leap year - use 1900 because 2000 is a leap year
|
|
* find the elapsed days through the year in a loop
|
|
* call routine to find each individual value
|
|
*
|
|
* author : david vallado 719-573-2600 1 mar 2001
|
|
*
|
|
* inputs description range / units
|
|
* jd - julian date days from 4713 bc
|
|
*
|
|
* outputs :
|
|
* year - year 1900 .. 2100
|
|
* mon - month 1 .. 12
|
|
* day - day 1 .. 28,29,30,31
|
|
* hr - hour 0 .. 23
|
|
* min - minute 0 .. 59
|
|
* sec - second 0.0 .. 59.999
|
|
*
|
|
* locals :
|
|
* days - day of year plus fractional
|
|
* portion of a day days
|
|
* tu - julian centuries from 0 h
|
|
* jan 0, 1900
|
|
* temp - temporary double values
|
|
* leapyrs - number of leap years from 1900
|
|
*
|
|
* coupling :
|
|
* days2mdhms - finds month, day, hour, minute and second given days and year
|
|
*
|
|
* references :
|
|
* vallado 2007, 208, alg 22, ex 3-13
|
|
* --------------------------------------------------------------------------- */
|
|
function invjday(jd, asArray) {
|
|
// --------------- find year and days of the year -
|
|
var temp = jd - 2415019.5;
|
|
var tu = temp / 365.25;
|
|
var year = 1900 + Math.floor(tu);
|
|
var leapyrs = Math.floor((year - 1901) * 0.25);
|
|
|
|
// optional nudge by 8.64x10-7 sec to get even outputs
|
|
var days = temp - ((year - 1900) * 365.0 + leapyrs) + 0.00000000001;
|
|
|
|
// ------------ check for case of beginning of a year -----------
|
|
if (days < 1.0) {
|
|
year -= 1;
|
|
leapyrs = Math.floor((year - 1901) * 0.25);
|
|
days = temp - ((year - 1900) * 365.0 + leapyrs);
|
|
}
|
|
|
|
// ----------------- find remaing data -------------------------
|
|
var mdhms = days2mdhms(year, days);
|
|
var mon = mdhms.mon,
|
|
day = mdhms.day,
|
|
hr = mdhms.hr,
|
|
minute = mdhms.minute;
|
|
var sec = mdhms.sec - 0.00000086400;
|
|
if (asArray) {
|
|
return [year, mon, day, hr, minute, Math.floor(sec)];
|
|
}
|
|
return new Date(Date.UTC(year, mon - 1, day, hr, minute, Math.floor(sec)));
|
|
} |