File: MoonRise.c

package info (click to toggle)
gkrellmoon 0.6-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 276 kB
  • ctags: 149
  • sloc: ansic: 1,231; makefile: 53
file content (187 lines) | stat: -rw-r--r-- 3,714 bytes parent folder | download | duplicates (6)
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
/* (C) Mike Henderson <mghenderson@lanl.gov>. 
 *
 *  I've converted to glib types, removed unused variables, and piped the whole
 *  thing through indent.
 *
 *  josh buhl <jbuhl@users.sourceforge.net> 
 */

#include "Moon.h"
#include "MoonRise.h"

void
UTTohhmm(gdouble UT, gint * h, gint * m)
{
	if (UT < 0.0) {
		*h = -1.0;
		*m = -1.0;
	} else {
		*h = (gint) UT;
		*m = (gint) ((UT - (gdouble) (*h)) * 60.0 + 0.5);
		if (*m == 60) {
			/* 
			 *  If it was 23:60 this should become 24:00
			 *  I prefer this designation to 00:00. So dont reset h to 0 when it goes above 24.
			 */
			*h += 1;
			*m = 0;
		}
	}

}

static gint
Interp(gdouble ym, gdouble y0, gdouble yp, gdouble * xe, gdouble * ye,
       gdouble * z1, gdouble * z2, gint * nz)
{

	gdouble a, b, c, d, dx;

	*nz = 0;
	a = 0.5 * (ym + yp) - y0;
	b = 0.5 * (yp - ym);
	c = y0;
	*xe = -b / (2.0 * a);
	*ye = (a * (*xe) + b) * (*xe) + c;
	d = b * b - 4.0 * a * c;

	if (d >= 0) {
		dx = 0.5 * sqrt(d) / fabs(a);
		*z1 = *xe - dx;
		*z2 = *xe + dx;
		if (fabs(*z1) <= 1.0)
			*nz += 1;
		if (fabs(*z2) <= 1.0)
			*nz += 1;
		if (*z1 < -1.0)
			*z1 = *z2;
	}

	return (0);
}


static gdouble SinH(CTrans * c, gdouble UT)
{

	gdouble TU, TU2, TU3, LambdaMoon, BetaMoon, R, AGE, frac(), jd();
	gdouble RA_Moon, DEC_Moon, gmst, lmst, Tau, epsilon, Moon();
	gdouble angle2pi();

	TU = (jd(c->year, c->month, c->day, UT) - 2451545.0) / 36525.0;

	/* this is more accurate, but expensive. */
	TU2 = TU * TU;
	TU3 = TU2 * TU;
	Moon(TU, &LambdaMoon, &BetaMoon, &R, &AGE);
	LambdaMoon *= RadPerDeg;
	BetaMoon *= RadPerDeg;
	epsilon =
	    (23.43929167 - 0.013004166 * TU - 1.6666667e-7 * TU2 -
	     5.0277777778e-7 * TU3) * RadPerDeg;
	RA_Moon =
	    angle2pi(atan2
		     (sin(LambdaMoon) * cos(epsilon) -
		      tan(BetaMoon) * sin(epsilon), cos(LambdaMoon)));
	DEC_Moon =
	    asin(sin(BetaMoon) * cos(epsilon) +
		 cos(BetaMoon) * sin(epsilon) * sin(LambdaMoon));


	/* This is less accurate, but computationally less expensive */
/*      MiniMoon(TU, &RA_Moon, &DEC_Moon); */
/*      RA_Moon *= 15.0*RadPerDeg; */
/*      DEC_Moon *= RadPerDeg; */

	/*
	 *  Compute Greenwich Mean Sidereal Time (gmst)
	 */
	UT = 24.0 * frac(UT / 24.0);
	/* this is for the ephemeris meridian??? 
	   gmst = 6.697374558 + 1.0027379093*UT + (8640184.812866+(0.093104-6.2e-6*TU)*TU)*TU/3600.0;
	 */
	gmst =
	    UT + 6.697374558 + (8640184.812866 +
				(0.093104 -
				 6.2e-6 * TU) * TU) * TU / 3600.0;
	lmst = 24.0 * frac((gmst - c->Glon / 15.0) / 24.0);

	Tau = 15.0 * lmst * RadPerDeg - RA_Moon;

	return (c->SinGlat * sin(DEC_Moon) +
		c->CosGlat * cos(DEC_Moon) * cos(Tau));
}


void
MoonRise(CTrans * c, gdouble * UTRise, gdouble * UTSet)
{

	gdouble UT, ym, y0, yp, SinH0;
	gdouble xe, ye, z1, z2;
	gint Rise, Set, nz, TimeZone;

	SinH0 = sin(8.0 / 60.0 * RadPerDeg);

	/* report rise and set times in LST */
	TimeZone = c->UT - c->LST;

	UT = 1.0 + TimeZone;
	*UTRise = -999.0;
	*UTSet = -999.0;
	Rise = Set = 0;

	ym = SinH(c, UT - 1.0) - SinH0;

	while ((UT <= 24.0 + TimeZone)) {

		y0 = SinH(c, UT) - SinH0;
		yp = SinH(c, UT + 1.0) - SinH0;

		Interp(ym, y0, yp, &xe, &ye, &z1, &z2, &nz);

		switch (nz) {

		case 0:
			break;
		case 1:
			if (ym < 0.0) {
				*UTRise = UT + z1;
				Rise = 1;
			} else {
				*UTSet = UT + z1;
				Set = 1;
			}
			break;
		case 2:
			if (ye < 0.0) {
				*UTRise = UT + z2;
				*UTSet = UT + z1;
			} else {
				*UTRise = UT + z1;
				*UTSet = UT + z2;
			}
			Rise = 1;
			Set = 1;
			break;
		}
		ym = yp;
		UT += 2.0;

	}

	if (Rise) {
		*UTRise -= TimeZone;
		*UTRise = hour24(*UTRise);
	} else {
		*UTRise = -999.0;
	}

	if (Set) {
		*UTSet -= TimeZone;
		*UTSet = hour24(*UTSet);
	} else {
		*UTSet = -999.0;
	}

}