2 * sunsolver.cxx - given a location on earth and a time of day/date,
3 * find the number of seconds to various sun positions.
5 * Written by Curtis Olson, started September 2003.
7 * Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <simgear/math/SGMath.hxx>
34 #include <simgear/ephemeris/ephemeris.hxx>
35 #include <simgear/timing/sg_time.hxx>
37 #include <Main/globals.hxx>
40 #include "sunsolver.hxx"
43 static const time_t day_secs = 86400;
44 static const time_t half_day_secs = day_secs / 2;
45 static const time_t step_secs = 60;
47 /* given a particular time expressed in side real time at prime
48 * meridian (GST), compute position on the earth (lat, lon) such that
49 * sun is directly overhead. (lat, lon are reported in radians */
51 void fgSunPositionGST(double gst, double *lon, double *lat) {
52 /* time_t ssue; seconds since unix epoch */
53 /* double *lat; (return) latitude */
54 /* double *lon; (return) longitude */
59 double beta = globals->get_ephem()->get_sun()->getLat();
60 double r = globals->get_ephem()->get_sun()->getDistance();
61 double xs = globals->get_ephem()->get_sun()->getxs();
62 double ys = globals->get_ephem()->get_sun()->getys();
63 double ye = globals->get_ephem()->get_sun()->getye();
64 double ze = globals->get_ephem()->get_sun()->getze();
65 alpha = atan2(ys - tan(beta)*ze/ys, xs);
66 delta = asin(sin(beta)*ye/ys + cos(beta)*ze);
68 tmp = alpha - (SGD_2PI/24)*gst;
71 while (tmp < -SGD_PI);
72 } else if (tmp > SGD_PI) {
74 while (tmp < -SGD_PI);
81 static double sun_angle( const SGTime &t, const SGVec3d& world_up,
82 double lon_rad, double lat_rad ) {
83 SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" );
84 SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t.getGst() );
86 double sun_lon, sun_gd_lat;
87 fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
88 SGVec3d sunpos = SGVec3d::fromGeod(SGGeod::fromRad(sun_lon, sun_gd_lat));
90 SG_LOG( SG_EVENT, SG_DEBUG, " t.cur_time = " << t.get_cur_time() );
91 SG_LOG( SG_EVENT, SG_DEBUG,
92 " Sun Geodetic lat = " << sun_gd_lat );
94 // calculate the sun's relative angle to local up
95 SGVec3f nup = normalize(toVec3f(world_up));
96 SGVec3f nsun = normalize(toVec3f(sunpos));
97 // cout << "nup = " << nup[0] << "," << nup[1] << ","
99 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
100 // << nsun[2] << endl;
102 double sun_angle = acos( dot( nup, nsun ) );
103 double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
104 while ( sun_angle_deg < -180 ) { sun_angle += 360; }
105 SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
108 return sun_angle_deg;
113 * Given the current unix time in seconds, calculate seconds to the
114 * specified sun angle (relative to straight up.) Also specify if we
115 * want the angle while the sun is ascending or descending. For
116 * instance noon is when the sun angle is 0 (or the closest it can
117 * get.) Dusk is when the sun angle is 90 and descending. Dawn is
118 * when the sun angle is 90 and ascending.
120 time_t fgTimeSecondsUntilSunAngle( time_t cur_time,
123 double target_angle_deg,
126 // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
127 // << lat_rad * SG_RADIANS_TO_DEGREES << endl;
128 SGVec3d world_up = SGVec3d::fromGeod(SGGeod::fromRad(lon_rad, lat_rad));
129 SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
131 double best_diff = 180.0;
132 double last_angle = -99999.0;
133 time_t best_time = cur_time;
135 for ( time_t secs = cur_time - half_day_secs;
136 secs < cur_time + half_day_secs;
139 t.update( lon_rad, lat_rad, secs, 0 );
140 double angle_deg = sun_angle( t, world_up, lon_rad, lat_rad );
141 double diff = fabs( angle_deg - target_angle_deg );
142 if ( diff < best_diff ) {
143 if ( last_angle <= 180.0 && ascending
144 && ( last_angle > angle_deg ) ) {
145 // cout << "best angle = " << angle << " offset = "
146 // << secs - cur_time << endl;
149 } else if ( last_angle <= 180.0 && !ascending
150 && ( last_angle < angle_deg ) ) {
151 // cout << "best angle = " << angle << " offset = "
152 // << secs - cur_time << endl;
158 last_angle = angle_deg;
161 return best_time - cur_time;