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
|
// Encoding: UTF-8
#include "calendar.h"
#include <cstdio>
#include <ctime>
#include <iomanip>
/*以下为"calendar.h规定之量*/
double day = 86400;
double delta = 1e-11;
/*计算需要用的类的实例化*/
Julian julian;
parameter p;
void radToDMS(const char *message, int lowerBound, int upperBound,
double radians) {
// Convert radians to degrees
double degrees = radians * 180.0 / M_PI;
while (degrees < lowerBound)
degrees += (upperBound - lowerBound);
while (degrees >= upperBound)
degrees -= (upperBound - lowerBound);
// Calculate degrees, minutes, and seconds
int deg = static_cast<int>(degrees);
double min_sec = (degrees - deg) * 60.0;
int min = static_cast<int>(min_sec);
double sec = (min_sec - min) * 60.0;
// Output the result
printf("%s: %d°%d'%06.3f''\n", message, deg, min, sec);
}
int main(int argc, char *argv[]) {
Date date;
if (argc != 2) {
printf("Input the time you want to calculate in <YYYY-MM-DD,HH:MM:SS> "
"format: ");
scanf("%d-%d-%d,%d:%d:%d", &date.tm_year, &date.tm_mon, &date.tm_mday,
&date.tm_hour, &date.tm_min, &date.tm_sec);
} else {
sscanf(argv[1], "%d-%d-%d,%d:%d:%d", &date.tm_year, &date.tm_mon,
&date.tm_mday, &date.tm_hour, &date.tm_min, &date.tm_sec);
}
date.tm_year -= 1900;
date.tm_mon -= 1;
date.tm_isdst = -1;
time_t time = mktime(&date);
double t = julian.getJulianKiloYear(time);
pair<double, double> sun = p.sun_longitude(t);
radToDMS("Sun longitude: ", 0, 360, sun.first);
radToDMS("Sun latitude: ", -90, 90, sun.second);
double alpha, delta; // 太阳赤经赤纬
double lambda = sun.first, beta = sun.second; // 太阳黄经黄纬
// 黄赤交角epsilon,23°26'21.448''转换为弧度
double epsilon = (23.0 + 26.0 / 60.0 + 21.448 / 3600.0) * M_PI / 180.0;
delta =
asin(sin(epsilon) * sin(lambda) * cos(beta) + cos(epsilon) * sin(beta));
alpha = atan2(
(cos(epsilon) * sin(lambda) * cos(beta) - sin(epsilon) * sin(beta)),
(cos(epsilon) * cos(beta)));
radToDMS("Sun right ascension: ", 0, 360, alpha);
radToDMS("Sun declination: ", -90, 90, delta);
// 时角
double H = p.getHourAngle(t, LONGITUDE, LATITUDE, alpha);
double A, h; //方位角和高度角
h = asin(sin(LATITUDE) * sin(delta) + cos(LATITUDE) * cos(delta) * cos(H));
A = atan2(-cos(delta) * sin(H),
cos(LATITUDE) * sin(delta) - sin(LATITUDE) * cos(delta) * cos(H));
A += M_PI;
// 转换为角度制输出
radToDMS("Hour angle: ", 0, 360, H);
radToDMS("Azimuth: ", 0, 360, A);
radToDMS("Elevation: ", 0, 90, h);
return 0;
}
|