123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #include "timelib.h"
- #include <stdio.h>
- #include <math.h>
- #define days_since_2000_Jan_0(y,m,d) \
- (367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
- #ifndef PI
- # define PI 3.1415926535897932384
- #endif
- #define RADEG ( 180.0 / PI )
- #define DEGRAD ( PI / 180.0 )
- #define sind(x) sin((x)*DEGRAD)
- #define cosd(x) cos((x)*DEGRAD)
- #define tand(x) tan((x)*DEGRAD)
- #define atand(x) (RADEG*atan(x))
- #define asind(x) (RADEG*asin(x))
- #define acosd(x) (RADEG*acos(x))
- #define atan2d(y,x) (RADEG*atan2(y,x))
- #include "astro.h"
- #define INV360 (1.0 / 360.0)
- static double astro_revolution(double x)
- {
- return (x - 360.0 * floor(x * INV360));
- }
- static double astro_rev180( double x )
- {
- return (x - 360.0 * floor(x * INV360 + 0.5));
- }
- static double astro_GMST0(double d)
- {
- double sidtim0;
-
-
-
-
-
- sidtim0 = astro_revolution((180.0 + 356.0470 + 282.9404) + (0.9856002585 + 4.70935E-5) * d);
- return sidtim0;
- }
- static void astro_sunpos(double d, double *lon, double *r)
- {
- double M,
- w,
-
- e,
- E,
- x, y,
- v;
-
- M = astro_revolution(356.0470 + 0.9856002585 * d);
- w = 282.9404 + 4.70935E-5 * d;
- e = 0.016709 - 1.151E-9 * d;
-
- E = M + e * RADEG * sind(M) * (1.0 + e * cosd(M));
- x = cosd(E) - e;
- y = sqrt(1.0 - e*e) * sind(E);
- *r = sqrt(x*x + y*y);
- v = atan2d(y, x);
- *lon = v + w;
- if (*lon >= 360.0) {
- *lon -= 360.0;
- }
- }
- static void astro_sun_RA_dec(double d, double *RA, double *dec, double *r)
- {
- double lon, obl_ecl, x, y, z;
-
- astro_sunpos(d, &lon, r);
-
- x = *r * cosd(lon);
- y = *r * sind(lon);
-
- obl_ecl = 23.4393 - 3.563E-7 * d;
-
- z = y * sind(obl_ecl);
- y = y * cosd(obl_ecl);
-
- *RA = atan2d(y, x);
- *dec = atan2d(z, sqrt(x*x + y*y));
- }
- int timelib_astro_rise_set_altitude(timelib_time *t_loc, double lon, double lat, double altit, int upper_limb, double *h_rise, double *h_set, timelib_sll *ts_rise, timelib_sll *ts_set, timelib_sll *ts_transit)
- {
- double d,
- sr,
- sRA,
- sdec,
- sradius,
- t,
- tsouth,
- sidtime;
- timelib_time *t_utc;
- timelib_sll timestamp, old_sse;
- int rc = 0;
-
- old_sse = t_loc->sse;
- t_loc->h = 12;
- t_loc->i = t_loc->s = 0;
- timelib_update_ts(t_loc, NULL);
-
- t_utc = timelib_time_ctor();
- t_utc->y = t_loc->y;
- t_utc->m = t_loc->m;
- t_utc->d = t_loc->d;
- t_utc->h = t_utc->i = t_utc->s = 0;
- timelib_update_ts(t_utc, NULL);
-
- timestamp = t_utc->sse;
- d = timelib_ts_to_j2000(timestamp) + 2 - lon/360.0;
-
- sidtime = astro_revolution(astro_GMST0(d) + 180.0 + lon);
-
- astro_sun_RA_dec( d, &sRA, &sdec, &sr );
-
- tsouth = 12.0 - astro_rev180(sidtime - sRA) / 15.0;
-
- sradius = 0.2666 / sr;
-
- if (upper_limb) {
- altit -= sradius;
- }
-
-
- {
- double cost;
- cost = (sind(altit) - sind(lat) * sind(sdec)) / (cosd(lat) * cosd(sdec));
- *ts_transit = t_utc->sse + (tsouth * 3600);
- if (cost >= 1.0) {
- rc = -1;
- t = 0.0;
- *ts_rise = *ts_set = t_utc->sse + (tsouth * 3600);
- } else if (cost <= -1.0) {
- rc = +1;
- t = 12.0;
- *ts_rise = t_loc->sse - (12 * 3600);
- *ts_set = t_loc->sse + (12 * 3600);
- } else {
- t = acosd(cost) / 15.0;
-
- *ts_rise = ((tsouth - t) * 3600) + t_utc->sse;
- *ts_set = ((tsouth + t) * 3600) + t_utc->sse;
- *h_rise = (tsouth - t);
- *h_set = (tsouth + t);
- }
- }
-
- timelib_time_dtor(t_utc);
- t_loc->sse = old_sse;
- return rc;
- }
- double timelib_ts_to_julianday(timelib_sll ts)
- {
- double tmp;
- tmp = (double) ts;
- tmp /= (double) 86400;
- tmp += (double) 2440587.5;
- return tmp;
- }
- double timelib_ts_to_j2000(timelib_sll ts)
- {
- return timelib_ts_to_julianday(ts) - 2451545;
- }
|