Skip to content

Commit ae773fa

Browse files
committed
Initial commit
0 parents  commit ae773fa

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Dependency directories
2+
node_modules/

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2011-2017 GitHub Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Astro Calculator
2+
3+
This npm package performs astronomical calculations and conversions. If you are attempting to write an app which has to do with astronomy, this package can be of great help as you don't have to search for formulae or methods to perform a certain astronomical calculation or conversion.
4+
5+
## Installing
6+
7+
```bash
8+
npm install astro-calculator
9+
```
10+
11+
## Getting started
12+
13+
This package provides following functionalities (all parameters are of type `number`):
14+
15+
- `getDaysFromJ2000`: calculates number of days from 2000-01-01 at noon until now
16+
- `parameters`: year, month, day, hours, minutes
17+
- `returns`: number
18+
- `getLocalSiderialTime`: calculates local siderial time
19+
- `parameters`: year, month, day, hours, minutes, LON
20+
- `returns`: number
21+
- `getHourAngle`: calculates hour angle
22+
- `parameters`: localSiderialTime, RA
23+
- `returns`: number
24+
- `getAltAz`: calculates altitude and azimuth
25+
- `parameters`: RA, DEC, LAT, LON, year, month, day, hours, minutes (RA - hours decimal; DEC, LAT, LON - degrees decimal)
26+
- `returns`: array of numbers - [Alt, Az] as degrees decimal
27+
- `isLeapYear`: determines if year is leap or not
28+
- `parameters`: year
29+
- `returns`: boolean
30+
- `convertToHours`: converts to hours
31+
- `parameters`: hours, minutes, seconds
32+
- `returns`: number
33+
- `convertToDegreesDecimal`: converts to degrees decimal
34+
- `parameters`: degrees, minutes, seconds
35+
- `returns`: number
36+
- `convertRAToDegreesDecimal`: converts RA to degrees decimmal
37+
- `parameters`: RA
38+
- `returns`: number
39+
- `convertDegreesToRadians`: converts degrees to radians
40+
- `parameters`: degrees
41+
- `returns`: number
42+
- `convertRadiansToDegrees`: converts radians to degrees
43+
- `parameters`: radians
44+
- `returns`: number
45+
- `convertDegreesDecimalToDegreesMinutesSeconds`: converts degrees decimal to degrees, minutes and seconds
46+
- `parameters`: degrees
47+
- `returns`: array of numbers - [deg, min, sec]
48+
49+
## Usage
50+
51+
Just import the package and use its functions with appropriate parameters, like so:
52+
53+
```javascript
54+
const ac = require("../astro-calculator");
55+
56+
let getDaysFromJ2000 = ac.getDaysFromJ2000(2022, 9, 16, 18, 32);
57+
let getLocalSiderialTime = ac.getLocalSiderialTime(2022, 9, 16, 18, 32, 16.03);
58+
let getHourAngle = ac.getHourAngle(8549.15, 6.45);
59+
let getAltAz = ac.getAltAz(6.45, 29, 45.81, 15.97, 2022, 9, 16, 17, 01);
60+
let isLeapYear = ac.isLeapYear(2022);
61+
let convertToHours = ac.convertToHours(18, 30, 21);
62+
let convertToDegreesDecimal = ac.convertToDegreesDecimal(123, 29, 33);
63+
let convertRAToDegreesDecimal = ac.convertRAToDegreesDecimal(6.45);
64+
let convertDegreesToRadians = ac.convertDegreesToRadians(36.87);
65+
let convertRadiansToDegrees = ac.convertRadiansToDegrees(0.6435);
66+
let convertDegreesDecimalToDegreesMinutesSeconds = ac.convertDegreesDecimalToDegreesMinutesSeconds(123.38);
67+
68+
console.log("getDaysFromJ2000: " + getDaysFromJ2000);
69+
console.log("getLocalSiderialTime: " + getLocalSiderialTime);
70+
console.log("getHourAngle: " + getHourAngle);
71+
console.log("getAltAz: " + getAltAz);
72+
console.log("isLeapYear: " + isLeapYear);
73+
console.log("convertToHours: " + convertToHours);
74+
console.log("convertToDegreesDecimal: " + convertToDegreesDecimal);
75+
console.log("convertRAToDegreesDecimal: " + convertRAToDegreesDecimal);
76+
console.log("convertDegreesToRadians: " + convertDegreesToRadians);
77+
console.log("convertRadiansToDegrees: " + convertRadiansToDegrees);
78+
console.log("convertDegreesDecimalToDegreesMinutesSeconds: " + convertDegreesDecimalToDegreesMinutesSeconds);
79+
```
80+
81+
and results look like this in the console:
82+
83+
```
84+
getDaysFromJ2000: 8294.272222222222
85+
getLocalSiderialTime: 8569.714533016668
86+
getHourAngle: 8542.699999999999
87+
getAltAz: -14.650875603437063,351.0514900218268
88+
isLeapYear: false
89+
convertToHours: 18.50583333333333
90+
convertToDegreesDecimal: 123.4925
91+
convertRAToDegreesDecimal: 96.75
92+
convertDegreesToRadians: 0.6435028952103092
93+
convertRadiansToDegrees: 36.86983411666847
94+
convertDegreesDecimalToDegreesMinutesSeconds: 123,22,48
95+
```
96+
97+
**Note**: When asked for time (hour and minues) in parameters of functions for example (`getAltAz`), remember to provide UTC time, not your local current time. This, of course, doesn't apply to the function `convertToHours` which will convert to hours any value of hours, minutes and seconds you provide it to convert - that's another story because it has nothing to do with calculating siderial time, hour angle or alt/az.

app.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
function getDaysFromJ2000(year, month, day, hours, minutes) {
2+
3+
let daysFromJ2000 = 0;
4+
5+
const daysToBeginningOfMonth = [ [1, 0, 0], [2, 31, 31], [3, 59, 60], [4, 90, 91], [5, 120, 121], [6, 151, 152], [7, 181, 182], [8, 212, 213], [9, 243, 244], [10, 273, 274], [11, 304, 305], [12, 334, 335] ];
6+
const daysSinceJ2000ToBeginningOfYear = [ [1998, -731.5], [1999, -366.5], [2000, -1.5], [2001, 364.5], [2002, 729.5], [2003, 1094.5], [2004, 1459.5], [2005, 1825.5], [2006, 2190.5], [2007, 2555.5], [2008, 2920.5], [2009, 3286.5], [2010, 3651.5], [2011, 4016.5], [2012, 4381.5], [2013, 4747.5], [2014, 5112.5], [2015, 5477.5], [2016, 5842.5], [2017, 6208.5], [2018, 6573.5], [2019, 6938.5], [2020, 7303.5], [2021, 7669.5], [2022, 8034.5], [2023, 8399.5], [2024, 8764.5], [2025, 9130.5], [2026, 9495.5], [2027, 9860.5], [2028, 10225.5] ];
7+
8+
// Calculate fraction of the day
9+
let fractionOfDay = (hours + minutes / 60) / 24;
10+
11+
// Find number of days to the beginning of month
12+
let numberOfDaysToBeginningOfMonth = 0;
13+
14+
daysToBeginningOfMonth.forEach(element => {
15+
if (element[0] === month) {
16+
if (this.isLeapYear(year)) {
17+
numberOfDaysToBeginningOfMonth = element[2];
18+
} else {
19+
numberOfDaysToBeginningOfMonth = element[1];
20+
}
21+
}
22+
})
23+
24+
// Find days since J2000.0 to the beginning of the year
25+
let numberOfDaysSinceJ2000ToBeginningOfYear = 0;
26+
27+
daysSinceJ2000ToBeginningOfYear.forEach(element => {
28+
if (element[0] === year) {
29+
numberOfDaysSinceJ2000ToBeginningOfYear = element[1];
30+
}
31+
})
32+
33+
// Add these calculations to get daysFromJ2000
34+
daysFromJ2000 = fractionOfDay + numberOfDaysToBeginningOfMonth + day + numberOfDaysSinceJ2000ToBeginningOfYear;
35+
36+
return daysFromJ2000;
37+
38+
}
39+
40+
function getLocalSiderialTime(year, month, day, hours, minutes, LON) {
41+
42+
let localSiderialTime = 0;
43+
44+
// Calculate fraction of the day
45+
let UT = (hours + minutes / 60);
46+
47+
// Calculate days from J2000
48+
let daysFromJ2000 = this.getDaysFromJ2000(year, month, day, hours, minutes);
49+
50+
// Calculate local siderial time, add 360 if negative
51+
localSiderialTime = (100.46 + 0.985647 * daysFromJ2000 + LON + 15 * UT) < 0 ? ((100.46 + 0.985647 * daysFromJ2000 + LON + 15 * UT) + 360) : (100.46 + 0.985647 * daysFromJ2000 + LON + 15 * UT);
52+
53+
return localSiderialTime;
54+
55+
}
56+
57+
function getHourAngle(localSiderialTime, RA) {
58+
return (localSiderialTime - RA) < 0 ? localSiderialTime - RA + 360 : localSiderialTime - RA;
59+
}
60+
61+
function getAltAz(RA, DEC, LAT, LON, year, month, day, hours, minutes) {
62+
63+
// RA - hours decimal
64+
// DEC, LAT, LON - degrees decimal
65+
// Result - degrees decimal (array of two numbers, altitude and azimuth)
66+
67+
let AltAz = [];
68+
69+
let HA = this.getHourAngle(this.getLocalSiderialTime(year, month, day, hours, minutes, LON), this.convertRAToDegreesDecimal(RA));
70+
71+
let sinDEC = Math.sin(this.convertDegreesToRadians(DEC));
72+
let sinLAT = Math.sin(this.convertDegreesToRadians(LAT));
73+
let sinHA = Math.sin(this.convertDegreesToRadians(HA));
74+
let cosDEC = Math.cos(this.convertDegreesToRadians(DEC));
75+
let cosLAT = Math.cos(this.convertDegreesToRadians(LAT));
76+
let cosHA = Math.cos(this.convertDegreesToRadians(HA));
77+
78+
let sinALT = sinDEC * sinLAT + cosDEC * cosLAT * cosHA;
79+
let ALT = this.convertRadiansToDegrees(Math.asin(sinALT));
80+
81+
let cosALT = Math.cos(this.convertDegreesToRadians(ALT));
82+
let cosA = (sinDEC - sinALT * sinLAT) / (cosALT * cosLAT);
83+
let A = this.convertRadiansToDegrees(Math.acos(cosA));
84+
85+
let AZ = (sinHA > 0 ? 360 - A : A);
86+
87+
AltAz = [ALT, AZ];
88+
89+
return AltAz;
90+
}
91+
92+
function isLeapYear(year) {
93+
return (year % 400) ? ((year % 100) ? ((year % 4) ? false : true) : false) : true;
94+
}
95+
96+
function convertToHours(hours, minutes, seconds) {
97+
return hours + minutes/60 + seconds/3600;
98+
}
99+
100+
function convertToDegreesDecimal(degrees, minutes, seconds) {
101+
return degrees < 0 ? -(Math.abs(degrees) + minutes/60 + seconds/3600) : degrees + minutes/60 + seconds/3600;
102+
}
103+
104+
function convertRAToDegreesDecimal (RA) {
105+
return RA * 15;
106+
}
107+
108+
function convertDegreesToRadians(degrees) {
109+
return degrees * Math.PI / 180;
110+
}
111+
112+
function convertRadiansToDegrees(radians) {
113+
return radians / (Math.PI / 180);
114+
}
115+
116+
function convertDegreesDecimalToDegreesMinutesSeconds(degrees) {
117+
118+
let deg = parseInt(degrees.toString());
119+
let fraction = Math.abs(degrees - deg);
120+
let min = parseInt((fraction * 60).toString());
121+
let sec = Math.round(fraction * 3600 - min * 60);
122+
123+
return [deg, min, sec];
124+
}
125+
126+
module.exports = {
127+
getDaysFromJ2000,
128+
getLocalSiderialTime,
129+
getHourAngle,
130+
getAltAz,
131+
isLeapYear,
132+
convertToHours,
133+
convertToDegreesDecimal,
134+
convertRAToDegreesDecimal,
135+
convertDegreesToRadians,
136+
convertRadiansToDegrees,
137+
convertDegreesDecimalToDegreesMinutesSeconds
138+
}

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "astro-calculator",
3+
"version": "1.0.0",
4+
"description": "Performs astronomical calculations and conversions",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"astronomy",
11+
"calculation",
12+
"conversion",
13+
"converter",
14+
"coordinate",
15+
"formulae",
16+
"astro",
17+
"right ascension",
18+
"declination",
19+
"degree",
20+
"altitude",
21+
"azimuth"
22+
],
23+
"bugs": {
24+
"url": "https://github.com/sdrazen/astro-calculator/issues",
25+
"email": "sdrazen@yahoo.com"
26+
},
27+
"publishConfig": {
28+
"access": "public"
29+
},
30+
"repository": {
31+
"type": "git",
32+
"url": "https://github.com/sdrazen/astro-calculator"
33+
},
34+
"author": "Dražen Škrinjarić",
35+
"license": "MIT",
36+
"dependencies": {
37+
}
38+
}

0 commit comments

Comments
 (0)