2 * This file is part of Live Wallpaper (livewp)
4 * Copyright (C) 2010 Vlad Vasiliev
5 * Copyright (C) 2010 Tanya Makova
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This software is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this software; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 /*******************************************************************************/
25 #include "livewp-astro.h"
27 double ut(int hour, int min, int zone)
29 return (hour - zone + (double)min/60);
32 double jd(int year, int month, int day, double uu)
34 return (367*year)-floor((floor((month+9)/12)+year)*7/4)+floor(275*month/9)+day-730531.5+(uu/24);
37 double altitude(double lon, double lat, int year, int month, int day, int hour, int min, int zone)
39 double uu, jj, T, k, M, Lo, DL, L, eps, delta, RA, GMST, LMST, H, eqt, alt;
40 uu = ut(hour, min, zone);
41 jj = jd(year, month, day, uu);
44 M = 357.5291 + 35999.0503 * T - 0.0001559 * T * T - 0.00000045 * T * T * T;
46 Lo = 280.46645 + 36000.76983 * T + 0.0003032 * T * T;
48 DL = (1.9146-0.004817*T-0.000014*T*T)*sin(k*M)+(0.019993-0.000101*T)*sin(k*2*M)+0.00029*sin(k*3*M);
51 delta=(1/k)*asin(sin(L*k)*sin(eps*k));
52 RA=(1/k)*atan2(cos(eps*k)*sin(L*k),cos(L*k));
53 RA=fmod((RA+360), 360);
54 GMST=280.46061837+360.98564736629*jj+0.000387933*T*T-T*T*T/38710000;
55 GMST=fmod((GMST+360), 360);
59 alt=(1/k)*asin(sin(lat*k)*sin(delta*k)+cos(lat*k)*cos(delta*k)*cos(H*k));
63 double azimuth(double lon, double lat, int year, int month, int day, int hour, int min, int zone)
65 double uu, jj, T, k, M, Lo, DL, L, eps, delta, RA, GMST, LMST, H, eqt, azm;
66 uu = ut(hour, min, zone);
67 jj = jd(year, month, day, uu);
70 M = 357.5291 + 35999.0503 * T - 0.0001559 * T * T - 0.00000045 * T * T * T;
72 Lo = 280.46645 + 36000.76983 * T + 0.0003032 * T * T;
74 DL = (1.9146-0.004817*T-0.000014*T*T)*sin(k*M)+(0.019993-0.000101*T)*sin(k*2*M)+0.00029*sin(k*3*M);
77 delta=(1/k)*asin(sin(L*k)*sin(eps*k));
78 RA=(1/k)*atan2(cos(eps*k)*sin(L*k),cos(L*k));
79 RA=fmod((RA+360), 360);
80 GMST=280.46061837+360.98564736629*jj+0.000387933*T*T-T*T*T/38710000;
81 GMST=fmod((GMST+360), 360);
85 azm=(1/k)*atan2(-sin(H*k),cos(lat*k)*tan(delta*k)-sin(lat*k)*cos(H*k));
86 azm=fmod((azm+360), 360);
89 double moon_phase(int year, int month, int day)
92 double agepart, ge, jd, moonday;
94 moonday = 29.530588853;
95 if (month <= 2) lg = 0;
97 if ((fmod(year, 4) == 0) && (!(fmod(year, 100) == 0) && (fmod(year, 400) != 0)))
103 floor((year - 1) / 4) +
104 (-floor((year - 1) / 100)) +
105 floor((year - 1) / 400) +
106 floor((((367 * month) - 362) / 12) + lg + day);
108 agepart = (jd - 2451550.1) / moonday;
109 agepart = agepart - floor(agepart);
110 if (agepart < 0) agepart + 1;
112 ageday = agepart * moonday;
114 moonfill = 1.0 - ((moonday/2 - ageday) / (moonday/2));
115 fprintf(stderr, "agepart = %f, ageday = %f, moonfill = %f\n", agepart, ageday, moonfill);
121 int main(int argc, char * argv)
123 int year, month, day, hour, min, zone;
124 double alt, azm, lon, lat, moonpart;
134 //moonpart = moon_phase1(year, month, day);
135 //fprintf(stderr, "moon path = %f\n", moonpart);
136 alt = altitude(lon, lat, year, month, day, hour, min, zone);
137 azm = azimuth(lon, lat, year, month, day, hour, min, zone);
138 fprintf(stderr, "alt=%5.6f azm=%5.6f\n", alt, azm);