-
Notifications
You must be signed in to change notification settings - Fork 16
/
Astro.Coord.js
279 lines (246 loc) · 7.83 KB
/
Astro.Coord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) 2016 Fabio Soldati, www.peakfinder.org
// License MIT: http://www.opensource.org/licenses/MIT
/**
* Represents a geographical point in ecliptic coordinates with latitude and longitude coordinates
* and an optional height.
* @constructor
* @param {number} lat - The latitude of the location in radians.
* @param {number} lng - The longitude of the location in radians. Remark: geographic longitudes are measured positively westwards!
* @param {?number} h - The height above the sea level.
*/
A.EclCoord = function (lat, lng, h) {
if (isNaN(lat) || isNaN(lng)) {
throw new Error('Invalid EclCoord object: (' + lat + ', ' + lng + ')');
}
this.lat = lat;
this.lng = lng;
if (h !== undefined) {
this.h = h;
}
};
A.EclCoord.prototype = {
/**
* Returns a pretty printed string in the WGS84 format.
* @return {String} Pretty printed string.
*/
toWgs84String: function () { // (Number) -> String
return A.Math.formatNum(this.lat * 180 / Math.PI) + ', ' + A.Math.formatNum(-this.lng * 180 / Math.PI);
}
};
/**
* Create a new EclCoord object from the given wgs coordinates.
*
* @param {number} lat - The latitude of the location degrees.
* @param {number} lng - The longitude of the location degrees.
* @param {?number} h - The height above the sea level.
*/
A.EclCoord.fromWgs84 = function(wgs84lat, wgs84lng, h) {
return new A.EclCoord(wgs84lat * Math.PI / 180, -wgs84lng * Math.PI / 180, h);
};
/**
* Represents a geographical point in equatorial coordinates with right ascension and declination.
* @constructor
* @param {number} ra - The right ascension in radians.
* @param {number} lng - The declination in radians.
*/
A.EqCoord = function (ra, dec) { // (Number, Number, Number)
if (isNaN(ra) || isNaN(dec)) {
throw new Error('Invalid EqCoord object: (' + ra + ', ' + dec + ')');
}
this.ra = ra;
this.dec = dec;
};
A.EqCoord.prototype = {
/**
* Returns a pretty printed string in degrees.
* @return {String} Pretty printed string.
*/
toString: function () { // (Number) -> String
return "ra:" + A.Math.formatNum(this.ra * 180 / Math.PI) + ', dec:' + A.Math.formatNum(this.dec * 180 / Math.PI);
}
};
/**
* Represents a geographical point in horizontal coordinates with azimuth and altitude.
* @constructor
* @param {number} az - The azimuth in radians.
* @param {number} alt - The altitude in radians.
*/
A.HzCoord = function (az, alt) { // (Number, Number, Number)
if (isNaN(az) || isNaN(alt)) {
throw new Error('Invalid HzCoord object: (' + az + ', ' + alt + ')');
}
this.az = az;
this.alt = alt;
};
A.HzCoord.prototype = {
/**
* Returns a pretty printed string in degrees.
* @return {String} Pretty printed string.
*/
toString: function () { // (Number) -> String
return "azi:" + A.Math.formatNum(this.az * 180 / Math.PI) + ', alt:' + A.Math.formatNum(this.alt * 180 / Math.PI);
}
};
/**
* A.Coord includes some static functions for coordinate transformations
* @module A.Coord
*/
A.Coord = {
/**
* DMSToDeg converts from parsed sexagesimal angle components to decimal degrees.
*
* @function dmsToDeg
* @static
*
* @param {boolean} neg - set to true if negative
* @param {number} d - degrees
* @param {number} m - minutes
* @param {number} s - seconds
* @return {number} decimal degrees
*/
dmsToDeg: function(neg, d, m, s) {
s = (((d*60+m)*60) + s) / 3600;
if (neg) {
return -s;
}
return s;
},
/**
* Returns radian value from an angle.
*
* @function calcAngle
* @static
*
* @param {boolean} neg - set to true if negative
* @param {number} d - degrees
* @param {number} m - minutes
* @param {number} s - seconds
* @return {number} degrees in radians
*/
calcAngle: function(neg, d, m, s) {
return A.Coord.dmsToDeg(neg, d, m, s) * Math.PI / 180;
},
/**
* Returns a radian value from hour, minute, and second components. <br>
*
* Negative values are not supported, and NewRA wraps values larger than 24
* to the range [0,24) hours.
*
* @function calcRA
* @static
*
* @param {number} h - hours
* @param {number} m - minutes
* @param {number} s - seconds
* @return {number} degrees in radians
*/
calcRA: function(h, m, s) {
var r = A.Coord.dmsToDeg(false, h, m, s) % 24;
return r * 15 * Math.PI / 180;
},
/**
* Returns a pretty formatted HMS string from the given seconds
*
* @function secondsToHMSStr
* @static
* @param {number} sec - seconds
* @return {string} formatted string
*/
secondsToHMSStr: function(sec) {
var days = Math.floor(sec / 86400);
sec = A.Math.pMod(sec, 86400);
var hours = Math.floor(sec/3600) % 24;
var minutes = Math.floor(sec/60) % 60;
var seconds = Math.floor(sec % 60);
return (days !== 0 ? days + "d " : "") + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
},
/**
* Returns a pretty formatted HM string from the given seconds
*
* @function secondsToHMStr
* @static
* @param {number} sec - seconds
* @return {string} formatted string
*/
secondsToHMStr: function(sec) {
var days = Math.floor(sec / 86400);
sec = A.Math.pMod(sec, 86400);
var hours = Math.floor(sec/3600) % 24;
var minutes = Math.floor(sec/60) % 60;
return (days !== 0 ? days + "d " : "") + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;
},
/**
* EqToEcl converts equatorial coordinates to ecliptic coordinates.
* @function eqToEcl
* @static
*
* @param {EqCoord} eqcoord - equatorial coordinates, in radians
* @param {number} epsilon - obliquity of the ecliptic
*
* @return {EclCoord} ecliptic coordinates of observer on Earth
*/
eqToEcl: function(eqcoord, epsilon) {
var sra = Math.sin(eqcoord.ra);
var cra = Math.cos(eqcoord.ra);
var sdec = Math.sin(eqcoord.dec);
var cdec = Math.cos(eqcoord.dec);
var sepsilon = Math.sin(epsilon);
var cepsilon = Math.cos(epsilon);
return new A.EclCoord(
Math.atan2(sra * cepsilon + (sdec / cdec) * sepsilon, cra), // (13.1) p. 93
Math.asin(sdec * cepsilon - cdec * sepsilon * sra) // (13.2) p. 93
);
},
/**
* EclToEq converts ecliptic coordinates to equatorial coordinates.
* @function eclToEq
* @static
*
* @param {EclCoord} latlng - ecliptic coordinates of observer on Earth
* @param {number} epsilon - obliquity of the ecliptic
*
* @return {EqCoord} equatorial coordinates, in radians
*/
eclToEq: function(eclCoord, epsilon) {
var slat = Math.sin(eclCoord.lat);
var clat = Math.cos(eclCoord.lat);
var slng = Math.sin(eclCoord.lng);
var clng = Math.cos(eclCoord.lng);
var sepsilon = Math.sin(epsilon);
var cepsilon = Math.cos(epsilon);
var ra = Math.atan2(slat * cepsilon - (slng / clng) * sepsilon, clat); // (13.3) p. 93
if (ra < 0) {
ra += 2 * Math.PI;
}
return new A.EqCoord(
ra,
Math.asin(slng * cepsilon + clng * sepsilon * slat) // (13.4) p. 93
);
},
/**
* EqToHz computes Horizontal coordinates from equatorial coordinates. <br>
* Sidereal time must be consistent with the equatorial coordinates.
* If coordinates are apparent, sidereal time must be apparent as well.
*
* @function eqToHz
* @static
*
* @param {EqCoord} eqcoord - equatorial coordinates, in radians
* @param {EclCoord} latlng - ecliptic coordinates of observer on Earth
* @param {number} st - sidereal time at Greenwich at time of observation in radians.
* @return {HzCoord} horizontal coordinates, in radians
*/
eqToHz: function(eqcoord, eclCoord, strad) {
var H = strad - eclCoord.lng - eqcoord.ra;
var sH = Math.sin(H);
var cH = Math.cos(H);
var slat = Math.sin(eclCoord.lat);
var clat = Math.cos(eclCoord.lat);
var sdec = Math.sin(eqcoord.dec);
var cdec = Math.cos(eqcoord.dec);
return new A.HzCoord(
Math.atan2(sH, cH * slat - (sdec / cdec) * clat), // (13.5) p. 93
Math.asin(slat * sdec + clat * cdec * cH) // (13.6) p. 93
);
}
};