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