2003-09-15 22:55:39 +00:00
|
|
|
/*
|
|
|
|
* sunsolver.cxx - given a location on earth and a time of day/date,
|
|
|
|
* find the number of seconds to various sun positions.
|
|
|
|
*
|
|
|
|
* Written by Curtis Olson, started September 2003.
|
|
|
|
*
|
2004-11-19 22:10:41 +00:00
|
|
|
* Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt
|
2003-09-15 22:55:39 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-09-15 22:55:39 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2005-11-12 14:40:17 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2008-07-25 18:38:29 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <ctime>
|
2003-09-15 22:55:39 +00:00
|
|
|
|
2007-01-30 20:14:25 +00:00
|
|
|
#include <simgear/math/SGMath.hxx>
|
2005-11-11 14:46:15 +00:00
|
|
|
#include <simgear/ephemeris/ephemeris.hxx>
|
2003-09-15 22:55:39 +00:00
|
|
|
#include <simgear/timing/sg_time.hxx>
|
|
|
|
|
|
|
|
#include <Main/globals.hxx>
|
|
|
|
|
2005-11-11 14:46:15 +00:00
|
|
|
#include "tmp.hxx"
|
2003-09-15 22:55:39 +00:00
|
|
|
#include "sunsolver.hxx"
|
|
|
|
|
|
|
|
|
2003-09-16 22:34:22 +00:00
|
|
|
static const time_t day_secs = 86400;
|
|
|
|
static const time_t half_day_secs = day_secs / 2;
|
|
|
|
static const time_t step_secs = 60;
|
2003-09-15 22:55:39 +00:00
|
|
|
|
2005-11-11 14:46:15 +00:00
|
|
|
/* given a particular time expressed in side real time at prime
|
|
|
|
* meridian (GST), compute position on the earth (lat, lon) such that
|
|
|
|
* sun is directly overhead. (lat, lon are reported in radians */
|
|
|
|
|
|
|
|
void fgSunPositionGST(double gst, double *lon, double *lat) {
|
|
|
|
/* time_t ssue; seconds since unix epoch */
|
|
|
|
/* double *lat; (return) latitude */
|
|
|
|
/* double *lon; (return) longitude */
|
|
|
|
|
|
|
|
double alpha, delta;
|
|
|
|
double tmp;
|
|
|
|
|
|
|
|
double beta = globals->get_ephem()->get_sun()->getLat();
|
|
|
|
double r = globals->get_ephem()->get_sun()->getDistance();
|
|
|
|
double xs = globals->get_ephem()->get_sun()->getxs();
|
|
|
|
double ys = globals->get_ephem()->get_sun()->getys();
|
|
|
|
double ye = globals->get_ephem()->get_sun()->getye();
|
|
|
|
double ze = globals->get_ephem()->get_sun()->getze();
|
|
|
|
alpha = atan2(ys - tan(beta)*ze/ys, xs);
|
|
|
|
delta = asin(sin(beta)*ye/ys + cos(beta)*ze);
|
|
|
|
|
|
|
|
tmp = alpha - (SGD_2PI/24)*gst;
|
|
|
|
if (tmp < -SGD_PI) {
|
|
|
|
do tmp += SGD_2PI;
|
|
|
|
while (tmp < -SGD_PI);
|
|
|
|
} else if (tmp > SGD_PI) {
|
|
|
|
do tmp -= SGD_2PI;
|
|
|
|
while (tmp < -SGD_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
*lon = tmp;
|
|
|
|
*lat = delta;
|
|
|
|
}
|
|
|
|
|
2007-01-30 20:14:25 +00:00
|
|
|
static double sun_angle( const SGTime &t, const SGVec3d& world_up,
|
2003-09-15 22:55:39 +00:00
|
|
|
double lon_rad, double lat_rad ) {
|
|
|
|
SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" );
|
|
|
|
SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t.getGst() );
|
|
|
|
|
2003-12-19 02:42:32 +00:00
|
|
|
double sun_lon, sun_gd_lat;
|
2003-09-15 22:55:39 +00:00
|
|
|
fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
|
2007-01-30 20:14:25 +00:00
|
|
|
SGVec3d sunpos = SGVec3d::fromGeod(SGGeod::fromRad(sun_lon, sun_gd_lat));
|
2003-09-15 22:55:39 +00:00
|
|
|
|
|
|
|
SG_LOG( SG_EVENT, SG_DEBUG, " t.cur_time = " << t.get_cur_time() );
|
|
|
|
SG_LOG( SG_EVENT, SG_DEBUG,
|
2003-12-19 02:42:32 +00:00
|
|
|
" Sun Geodetic lat = " << sun_gd_lat );
|
2003-09-15 22:55:39 +00:00
|
|
|
|
|
|
|
// calculate the sun's relative angle to local up
|
2007-01-30 20:14:25 +00:00
|
|
|
SGVec3f nup = normalize(toVec3f(world_up));
|
|
|
|
SGVec3f nsun = normalize(toVec3f(sunpos));
|
2003-09-15 22:55:39 +00:00
|
|
|
// cout << "nup = " << nup[0] << "," << nup[1] << ","
|
|
|
|
// << nup[2] << endl;
|
|
|
|
// cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
|
|
|
|
// << nsun[2] << endl;
|
|
|
|
|
2007-01-30 20:14:25 +00:00
|
|
|
double sun_angle = acos( dot( nup, nsun ) );
|
2003-09-15 22:55:39 +00:00
|
|
|
double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
|
|
|
|
while ( sun_angle_deg < -180 ) { sun_angle += 360; }
|
|
|
|
SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
|
|
|
|
<< sun_angle_deg );
|
|
|
|
|
|
|
|
return sun_angle_deg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-09-17 15:49:15 +00:00
|
|
|
* Given the current unix time in seconds, calculate seconds to the
|
|
|
|
* specified sun angle (relative to straight up.) Also specify if we
|
|
|
|
* want the angle while the sun is ascending or descending. For
|
|
|
|
* instance noon is when the sun angle is 0 (or the closest it can
|
|
|
|
* get.) Dusk is when the sun angle is 90 and descending. Dawn is
|
|
|
|
* when the sun angle is 90 and ascending.
|
2003-09-15 22:55:39 +00:00
|
|
|
*/
|
2003-09-17 15:49:15 +00:00
|
|
|
time_t fgTimeSecondsUntilSunAngle( time_t cur_time,
|
2003-09-15 22:55:39 +00:00
|
|
|
double lon_rad,
|
2003-09-17 15:49:15 +00:00
|
|
|
double lat_rad,
|
|
|
|
double target_angle_deg,
|
|
|
|
bool ascending )
|
2003-09-15 22:55:39 +00:00
|
|
|
{
|
|
|
|
// cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
|
|
|
|
// << lat_rad * SG_RADIANS_TO_DEGREES << endl;
|
2007-01-30 20:14:25 +00:00
|
|
|
SGVec3d world_up = SGVec3d::fromGeod(SGGeod::fromRad(lon_rad, lat_rad));
|
2003-09-15 22:55:39 +00:00
|
|
|
SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
|
|
|
|
|
2003-09-17 15:49:15 +00:00
|
|
|
double best_diff = 180.0;
|
2003-09-15 22:55:39 +00:00
|
|
|
double last_angle = -99999.0;
|
|
|
|
time_t best_time = cur_time;
|
|
|
|
|
2003-09-16 22:34:22 +00:00
|
|
|
for ( time_t secs = cur_time - half_day_secs;
|
|
|
|
secs < cur_time + half_day_secs;
|
|
|
|
secs += step_secs )
|
|
|
|
{
|
2003-09-15 22:55:39 +00:00
|
|
|
t.update( lon_rad, lat_rad, secs, 0 );
|
2003-09-17 15:49:15 +00:00
|
|
|
double angle_deg = sun_angle( t, world_up, lon_rad, lat_rad );
|
|
|
|
double diff = fabs( angle_deg - target_angle_deg );
|
2003-09-15 22:55:39 +00:00
|
|
|
if ( diff < best_diff ) {
|
2003-09-17 15:49:15 +00:00
|
|
|
if ( last_angle <= 180.0 && ascending
|
|
|
|
&& ( last_angle > angle_deg ) ) {
|
2003-09-15 22:55:39 +00:00
|
|
|
// cout << "best angle = " << angle << " offset = "
|
|
|
|
// << secs - cur_time << endl;
|
|
|
|
best_diff = diff;
|
|
|
|
best_time = secs;
|
2003-09-17 15:49:15 +00:00
|
|
|
} else if ( last_angle <= 180.0 && !ascending
|
|
|
|
&& ( last_angle < angle_deg ) ) {
|
2003-09-15 22:55:39 +00:00
|
|
|
// cout << "best angle = " << angle << " offset = "
|
|
|
|
// << secs - cur_time << endl;
|
|
|
|
best_diff = diff;
|
|
|
|
best_time = secs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-17 15:49:15 +00:00
|
|
|
last_angle = angle_deg;
|
2003-09-15 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return best_time - cur_time;
|
|
|
|
}
|