]> git.mxchange.org Git - flightgear.git/blob - src/Time/sunsolver.cxx
Revert "Create TimeManager subsystem" - not quite ready for prime-time yet!
[flightgear.git] / src / Time / sunsolver.cxx
1 /*
2  * sunsolver.cxx - given a location on earth and a time of day/date,
3  *                 find the number of seconds to various sun positions.
4  *
5  * Written by Curtis Olson, started September 2003.
6  *
7  * Copyright (C) 2003  Curtis L. Olson  - http://www.flightgear.org/~curt
8  *
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.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #      include <config.h>
28 #endif
29
30 #include <cmath>
31 #include <ctime>
32 #include <cassert>
33
34 #include <simgear/math/SGMath.hxx>
35 #include <simgear/timing/sg_time.hxx>
36
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39
40 #include "tmp.hxx"
41 #include "sunsolver.hxx"
42
43
44 static const time_t day_secs = 86400;
45 static const time_t half_day_secs = day_secs / 2;
46 static const time_t step_secs = 60;
47
48 /* given a particular time expressed in side real time at prime
49  * meridian (GST), compute position on the earth (lat, lon) such that
50  * sun is directly overhead.  (lat, lon are reported in radians */
51
52 void fgSunPositionGST(double gst, double *lon, double *lat) {
53     /* time_t  ssue;           seconds since unix epoch */
54     /* double *lat;            (return) latitude        */
55     /* double *lon;            (return) longitude       */
56
57     double alpha, delta;
58     double tmp;
59
60     SGPropertyNode* sun = fgGetNode("/ephemeris/sun");
61     assert(sun);
62     double beta = sun->getDoubleValue("lat-deg");
63     // double r = globals->get_ephem()->get_sun()->getDistance();
64     double xs = sun->getDoubleValue("xs");
65     double ys = sun->getDoubleValue("ys");
66     double ye = sun->getDoubleValue("ye");
67     double ze = sun->getDoubleValue("ze");
68     alpha = atan2(ys - tan(beta)*ze/ys, xs);
69     delta = asin(sin(beta)*ye/ys + cos(beta)*ze);
70
71     tmp = alpha - (SGD_2PI/24)*gst;
72     if (tmp < -SGD_PI) {
73         do tmp += SGD_2PI;
74         while (tmp < -SGD_PI);
75     } else if (tmp > SGD_PI) {
76         do tmp -= SGD_2PI;
77         while (tmp < -SGD_PI);
78     }
79
80     *lon = tmp;
81     *lat = delta;
82 }
83
84 static double sun_angle( const SGTime &t, const SGVec3d& world_up,
85                          double lon_rad, double lat_rad ) {
86     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
87     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t.getGst() );
88
89     double sun_lon, sun_gd_lat;
90     fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
91     SGVec3d sunpos = SGVec3d::fromGeod(SGGeod::fromRad(sun_lon, sun_gd_lat));
92
93     SG_LOG( SG_EVENT, SG_DEBUG, "    t.cur_time = " << t.get_cur_time() );
94     SG_LOG( SG_EVENT, SG_DEBUG, 
95             "    Sun Geodetic lat = " << sun_gd_lat );
96
97     // calculate the sun's relative angle to local up
98     SGVec3f nup = normalize(toVec3f(world_up));
99     SGVec3f nsun = normalize(toVec3f(sunpos));
100     // cout << "nup = " << nup[0] << "," << nup[1] << "," 
101     //      << nup[2] << endl;
102     // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
103     //      << nsun[2] << endl;
104
105     double sun_angle = acos( dot( nup, nsun ) );
106     double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
107     while ( sun_angle_deg < -180 ) { sun_angle += 360; }
108     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
109             << sun_angle_deg );
110
111     return sun_angle_deg;
112 }
113
114
115 /**
116  * Given the current unix time in seconds, calculate seconds to the
117  * specified sun angle (relative to straight up.)  Also specify if we
118  * want the angle while the sun is ascending or descending.  For
119  * instance noon is when the sun angle is 0 (or the closest it can
120  * get.)  Dusk is when the sun angle is 90 and descending.  Dawn is
121  * when the sun angle is 90 and ascending.
122  */
123 time_t fgTimeSecondsUntilSunAngle( time_t cur_time,
124                                    double lon_rad,
125                                    double lat_rad,
126                                    double target_angle_deg,
127                                    bool ascending )
128 {
129     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
130     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
131     SGVec3d world_up = SGVec3d::fromGeod(SGGeod::fromRad(lon_rad, lat_rad));
132     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
133
134     double best_diff = 180.0;
135     double last_angle = -99999.0;
136     time_t best_time = cur_time;
137
138     for ( time_t secs = cur_time - half_day_secs;
139           secs < cur_time + half_day_secs;
140           secs += step_secs )
141     {
142         t.update( lon_rad, lat_rad, secs, 0 );
143         double angle_deg = sun_angle( t, world_up, lon_rad, lat_rad );
144         double diff = fabs( angle_deg - target_angle_deg );
145         if ( diff < best_diff ) {
146             if ( last_angle <= 180.0 && ascending
147                  && ( last_angle > angle_deg ) ) {
148                 // cout << "best angle = " << angle << " offset = "
149                 //      << secs - cur_time << endl;
150                 best_diff = diff;
151                 best_time = secs;
152             } else if ( last_angle <= 180.0 && !ascending
153                         && ( last_angle < angle_deg ) ) {
154                 // cout << "best angle = " << angle << " offset = "
155                 //      << secs - cur_time << endl;
156                 best_diff = diff;
157                 best_time = secs;
158             }
159         }
160
161         last_angle = angle_deg;
162     }
163
164     return best_time - cur_time;
165 }