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