Molto molto tempo fa dovevo calcolare la posizione del sole per cercare di calcolare se fosse nel campo visivo delle telecamere in modo da poter evitare abbagliamenti. Non ricordo proprio la sorgente del mio codice, ma se e’ incomprensibile e’ perche’ ho solo cercato di implementare le formule che ho trovato in rete senza capirle.
Paolo
usage: sunposition
prints sun azimuth and elevation at current lat/long and time hh:mm
All angles in degrees.
# include <stdio.h>
# include <math.h>
# define TPI (M_PI * 2.0)
# define RADS (M_PI / 180.0)
# define DEGS (180.0 / M_PI)
# define INT(a) ((double) ((int) (a)))
double range(double a)
{
if (a < 0.0) {
while (a < 0.0) {
a += 2.0 * M_PI;
}
return a;
} else {
while (a > 2.0 * M_PI) {
a -= 2.0 * M_PI;
}
return a;
}
}
void sunposition(int yy, int mm, int dd, int hh, int mi,
double lat, double lon, double *alt, double *az)
{ double d, l, g, lambda, s;
d = 367.0 * (double) yy -
INT(7.0 * ((double) yy + INT(((double) mm + 9.0) / 12.0)) / 4.0) +
INT(275.0 * (double) mm / 9) + (double) dd +
((double) hh + (double) mi / 60.0 ) / 24.0 - 730531.5;
/* mean longitude of the Sun */
l = range(280.461 * RADS + .9856474 * RADS * d);
/* mean anomaly of the Sun */
g = range(357.528 * RADS + .9856003 * RADS * d);
/* Ecliptic longitude of the Sun */
s = l + 1.915 * RADS * sin(g) + .02 * RADS * sin(2 * g);
lambda = fmod(s, 360.0);
{
double C32, C33, C25, C34, C41, C39, C40, D42;
C33 = (84381.448 - 46.815 * d / 36525) / 3600.0 * RADS;
C32 = lambda;
C25 = fmod(280.46061837 + 360.98564736629 * d + lon, 360.0) * RADS;
C34 = atan2(sin(C32) * cos(C33) - tan(0.0) * sin(C33), cos(C32) );
C41 = asin(sin(0) * cos(C33) + cos(0) * sin(C33) * sin(C32));
C39 = lat * RADS;
C40 = fmod(C25 - C34, 2.0 * M_PI);
D42 = sin(C41) * sin(C39) + cos(C41) * cos(C39) * cos(C40);
*alt = asin(D42);
*az = acos(((sin(C41) - sin(C39) * D42) / (cos(C39) * cos(*alt))));
}
}
main(int argc, char *argv[])
{ int yy, mm, dd, hh, mi;
double lat, lon, alt, az;
sscanf(argv[1], "%d", &yy);
sscanf(argv[2], "%d", &mm);
sscanf(argv[3], "%d", &dd);
sscanf(argv[4], "%d", &hh);
sscanf(argv[5], "%d", &mi);
sscanf(argv[6], "%lf", &lat);
sscanf(argv[7], "%lf", &lon);
sunposition(yy, mm, dd, hh, mi, lat, lon, &alt, &az);
printf("Alt : %f\n", alt * DEGS);
printf("Az : %f\n", az * DEGS);
}