intl/icu/source/i18n/astro.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /************************************************************************
michael@0 2 * Copyright (C) 1996-2012, International Business Machines Corporation
michael@0 3 * and others. All Rights Reserved.
michael@0 4 ************************************************************************
michael@0 5 * 2003-nov-07 srl Port from Java
michael@0 6 */
michael@0 7
michael@0 8 #include "astro.h"
michael@0 9
michael@0 10 #if !UCONFIG_NO_FORMATTING
michael@0 11
michael@0 12 #include "unicode/calendar.h"
michael@0 13 #include <math.h>
michael@0 14 #include <float.h>
michael@0 15 #include "unicode/putil.h"
michael@0 16 #include "uhash.h"
michael@0 17 #include "umutex.h"
michael@0 18 #include "ucln_in.h"
michael@0 19 #include "putilimp.h"
michael@0 20 #include <stdio.h> // for toString()
michael@0 21
michael@0 22 #if defined (PI)
michael@0 23 #undef PI
michael@0 24 #endif
michael@0 25
michael@0 26 #ifdef U_DEBUG_ASTRO
michael@0 27 # include "uresimp.h" // for debugging
michael@0 28
michael@0 29 static void debug_astro_loc(const char *f, int32_t l)
michael@0 30 {
michael@0 31 fprintf(stderr, "%s:%d: ", f, l);
michael@0 32 }
michael@0 33
michael@0 34 static void debug_astro_msg(const char *pat, ...)
michael@0 35 {
michael@0 36 va_list ap;
michael@0 37 va_start(ap, pat);
michael@0 38 vfprintf(stderr, pat, ap);
michael@0 39 fflush(stderr);
michael@0 40 }
michael@0 41 #include "unicode/datefmt.h"
michael@0 42 #include "unicode/ustring.h"
michael@0 43 static const char * debug_astro_date(UDate d) {
michael@0 44 static char gStrBuf[1024];
michael@0 45 static DateFormat *df = NULL;
michael@0 46 if(df == NULL) {
michael@0 47 df = DateFormat::createDateTimeInstance(DateFormat::MEDIUM, DateFormat::MEDIUM, Locale::getUS());
michael@0 48 df->adoptTimeZone(TimeZone::getGMT()->clone());
michael@0 49 }
michael@0 50 UnicodeString str;
michael@0 51 df->format(d,str);
michael@0 52 u_austrncpy(gStrBuf,str.getTerminatedBuffer(),sizeof(gStrBuf)-1);
michael@0 53 return gStrBuf;
michael@0 54 }
michael@0 55
michael@0 56 // must use double parens, i.e.: U_DEBUG_ASTRO_MSG(("four is: %d",4));
michael@0 57 #define U_DEBUG_ASTRO_MSG(x) {debug_astro_loc(__FILE__,__LINE__);debug_astro_msg x;}
michael@0 58 #else
michael@0 59 #define U_DEBUG_ASTRO_MSG(x)
michael@0 60 #endif
michael@0 61
michael@0 62 static inline UBool isINVALID(double d) {
michael@0 63 return(uprv_isNaN(d));
michael@0 64 }
michael@0 65
michael@0 66 static UMutex ccLock = U_MUTEX_INITIALIZER;
michael@0 67
michael@0 68 U_CDECL_BEGIN
michael@0 69 static UBool calendar_astro_cleanup(void) {
michael@0 70 return TRUE;
michael@0 71 }
michael@0 72 U_CDECL_END
michael@0 73
michael@0 74 U_NAMESPACE_BEGIN
michael@0 75
michael@0 76 /**
michael@0 77 * The number of standard hours in one sidereal day.
michael@0 78 * Approximately 24.93.
michael@0 79 * @internal
michael@0 80 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 81 */
michael@0 82 #define SIDEREAL_DAY (23.93446960027)
michael@0 83
michael@0 84 /**
michael@0 85 * The number of sidereal hours in one mean solar day.
michael@0 86 * Approximately 24.07.
michael@0 87 * @internal
michael@0 88 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 89 */
michael@0 90 #define SOLAR_DAY (24.065709816)
michael@0 91
michael@0 92 /**
michael@0 93 * The average number of solar days from one new moon to the next. This is the time
michael@0 94 * it takes for the moon to return the same ecliptic longitude as the sun.
michael@0 95 * It is longer than the sidereal month because the sun's longitude increases
michael@0 96 * during the year due to the revolution of the earth around the sun.
michael@0 97 * Approximately 29.53.
michael@0 98 *
michael@0 99 * @see #SIDEREAL_MONTH
michael@0 100 * @internal
michael@0 101 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 102 */
michael@0 103 const double CalendarAstronomer::SYNODIC_MONTH = 29.530588853;
michael@0 104
michael@0 105 /**
michael@0 106 * The average number of days it takes
michael@0 107 * for the moon to return to the same ecliptic longitude relative to the
michael@0 108 * stellar background. This is referred to as the sidereal month.
michael@0 109 * It is shorter than the synodic month due to
michael@0 110 * the revolution of the earth around the sun.
michael@0 111 * Approximately 27.32.
michael@0 112 *
michael@0 113 * @see #SYNODIC_MONTH
michael@0 114 * @internal
michael@0 115 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 116 */
michael@0 117 #define SIDEREAL_MONTH 27.32166
michael@0 118
michael@0 119 /**
michael@0 120 * The average number number of days between successive vernal equinoxes.
michael@0 121 * Due to the precession of the earth's
michael@0 122 * axis, this is not precisely the same as the sidereal year.
michael@0 123 * Approximately 365.24
michael@0 124 *
michael@0 125 * @see #SIDEREAL_YEAR
michael@0 126 * @internal
michael@0 127 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 128 */
michael@0 129 #define TROPICAL_YEAR 365.242191
michael@0 130
michael@0 131 /**
michael@0 132 * The average number of days it takes
michael@0 133 * for the sun to return to the same position against the fixed stellar
michael@0 134 * background. This is the duration of one orbit of the earth about the sun
michael@0 135 * as it would appear to an outside observer.
michael@0 136 * Due to the precession of the earth's
michael@0 137 * axis, this is not precisely the same as the tropical year.
michael@0 138 * Approximately 365.25.
michael@0 139 *
michael@0 140 * @see #TROPICAL_YEAR
michael@0 141 * @internal
michael@0 142 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 143 */
michael@0 144 #define SIDEREAL_YEAR 365.25636
michael@0 145
michael@0 146 //-------------------------------------------------------------------------
michael@0 147 // Time-related constants
michael@0 148 //-------------------------------------------------------------------------
michael@0 149
michael@0 150 /**
michael@0 151 * The number of milliseconds in one second.
michael@0 152 * @internal
michael@0 153 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 154 */
michael@0 155 #define SECOND_MS U_MILLIS_PER_SECOND
michael@0 156
michael@0 157 /**
michael@0 158 * The number of milliseconds in one minute.
michael@0 159 * @internal
michael@0 160 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 161 */
michael@0 162 #define MINUTE_MS U_MILLIS_PER_MINUTE
michael@0 163
michael@0 164 /**
michael@0 165 * The number of milliseconds in one hour.
michael@0 166 * @internal
michael@0 167 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 168 */
michael@0 169 #define HOUR_MS U_MILLIS_PER_HOUR
michael@0 170
michael@0 171 /**
michael@0 172 * The number of milliseconds in one day.
michael@0 173 * @internal
michael@0 174 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 175 */
michael@0 176 #define DAY_MS U_MILLIS_PER_DAY
michael@0 177
michael@0 178 /**
michael@0 179 * The start of the julian day numbering scheme used by astronomers, which
michael@0 180 * is 1/1/4713 BC (Julian), 12:00 GMT. This is given as the number of milliseconds
michael@0 181 * since 1/1/1970 AD (Gregorian), a negative number.
michael@0 182 * Note that julian day numbers and
michael@0 183 * the Julian calendar are <em>not</em> the same thing. Also note that
michael@0 184 * julian days start at <em>noon</em>, not midnight.
michael@0 185 * @internal
michael@0 186 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 187 */
michael@0 188 #define JULIAN_EPOCH_MS -210866760000000.0
michael@0 189
michael@0 190
michael@0 191 /**
michael@0 192 * Milliseconds value for 0.0 January 2000 AD.
michael@0 193 */
michael@0 194 #define EPOCH_2000_MS 946598400000.0
michael@0 195
michael@0 196 //-------------------------------------------------------------------------
michael@0 197 // Assorted private data used for conversions
michael@0 198 //-------------------------------------------------------------------------
michael@0 199
michael@0 200 // My own copies of these so compilers are more likely to optimize them away
michael@0 201 const double CalendarAstronomer::PI = 3.14159265358979323846;
michael@0 202
michael@0 203 #define CalendarAstronomer_PI2 (CalendarAstronomer::PI*2.0)
michael@0 204 #define RAD_HOUR ( 12 / CalendarAstronomer::PI ) // radians -> hours
michael@0 205 #define DEG_RAD ( CalendarAstronomer::PI / 180 ) // degrees -> radians
michael@0 206 #define RAD_DEG ( 180 / CalendarAstronomer::PI ) // radians -> degrees
michael@0 207
michael@0 208 /***
michael@0 209 * Given 'value', add or subtract 'range' until 0 <= 'value' < range.
michael@0 210 * The modulus operator.
michael@0 211 */
michael@0 212 inline static double normalize(double value, double range) {
michael@0 213 return value - range * ClockMath::floorDivide(value, range);
michael@0 214 }
michael@0 215
michael@0 216 /**
michael@0 217 * Normalize an angle so that it's in the range 0 - 2pi.
michael@0 218 * For positive angles this is just (angle % 2pi), but the Java
michael@0 219 * mod operator doesn't work that way for negative numbers....
michael@0 220 */
michael@0 221 inline static double norm2PI(double angle) {
michael@0 222 return normalize(angle, CalendarAstronomer::PI * 2.0);
michael@0 223 }
michael@0 224
michael@0 225 /**
michael@0 226 * Normalize an angle into the range -PI - PI
michael@0 227 */
michael@0 228 inline static double normPI(double angle) {
michael@0 229 return normalize(angle + CalendarAstronomer::PI, CalendarAstronomer::PI * 2.0) - CalendarAstronomer::PI;
michael@0 230 }
michael@0 231
michael@0 232 //-------------------------------------------------------------------------
michael@0 233 // Constructors
michael@0 234 //-------------------------------------------------------------------------
michael@0 235
michael@0 236 /**
michael@0 237 * Construct a new <code>CalendarAstronomer</code> object that is initialized to
michael@0 238 * the current date and time.
michael@0 239 * @internal
michael@0 240 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 241 */
michael@0 242 CalendarAstronomer::CalendarAstronomer():
michael@0 243 fTime(Calendar::getNow()), fLongitude(0.0), fLatitude(0.0), fGmtOffset(0.0), moonPosition(0,0), moonPositionSet(FALSE) {
michael@0 244 clearCache();
michael@0 245 }
michael@0 246
michael@0 247 /**
michael@0 248 * Construct a new <code>CalendarAstronomer</code> object that is initialized to
michael@0 249 * the specified date and time.
michael@0 250 * @internal
michael@0 251 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 252 */
michael@0 253 CalendarAstronomer::CalendarAstronomer(UDate d): fTime(d), fLongitude(0.0), fLatitude(0.0), fGmtOffset(0.0), moonPosition(0,0), moonPositionSet(FALSE) {
michael@0 254 clearCache();
michael@0 255 }
michael@0 256
michael@0 257 /**
michael@0 258 * Construct a new <code>CalendarAstronomer</code> object with the given
michael@0 259 * latitude and longitude. The object's time is set to the current
michael@0 260 * date and time.
michael@0 261 * <p>
michael@0 262 * @param longitude The desired longitude, in <em>degrees</em> east of
michael@0 263 * the Greenwich meridian.
michael@0 264 *
michael@0 265 * @param latitude The desired latitude, in <em>degrees</em>. Positive
michael@0 266 * values signify North, negative South.
michael@0 267 *
michael@0 268 * @see java.util.Date#getTime()
michael@0 269 * @internal
michael@0 270 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 271 */
michael@0 272 CalendarAstronomer::CalendarAstronomer(double longitude, double latitude) :
michael@0 273 fTime(Calendar::getNow()), moonPosition(0,0), moonPositionSet(FALSE) {
michael@0 274 fLongitude = normPI(longitude * (double)DEG_RAD);
michael@0 275 fLatitude = normPI(latitude * (double)DEG_RAD);
michael@0 276 fGmtOffset = (double)(fLongitude * 24. * (double)HOUR_MS / (double)CalendarAstronomer_PI2);
michael@0 277 clearCache();
michael@0 278 }
michael@0 279
michael@0 280 CalendarAstronomer::~CalendarAstronomer()
michael@0 281 {
michael@0 282 }
michael@0 283
michael@0 284 //-------------------------------------------------------------------------
michael@0 285 // Time and date getters and setters
michael@0 286 //-------------------------------------------------------------------------
michael@0 287
michael@0 288 /**
michael@0 289 * Set the current date and time of this <code>CalendarAstronomer</code> object. All
michael@0 290 * astronomical calculations are performed based on this time setting.
michael@0 291 *
michael@0 292 * @param aTime the date and time, expressed as the number of milliseconds since
michael@0 293 * 1/1/1970 0:00 GMT (Gregorian).
michael@0 294 *
michael@0 295 * @see #setDate
michael@0 296 * @see #getTime
michael@0 297 * @internal
michael@0 298 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 299 */
michael@0 300 void CalendarAstronomer::setTime(UDate aTime) {
michael@0 301 fTime = aTime;
michael@0 302 U_DEBUG_ASTRO_MSG(("setTime(%.1lf, %sL)\n", aTime, debug_astro_date(aTime+fGmtOffset)));
michael@0 303 clearCache();
michael@0 304 }
michael@0 305
michael@0 306 /**
michael@0 307 * Set the current date and time of this <code>CalendarAstronomer</code> object. All
michael@0 308 * astronomical calculations are performed based on this time setting.
michael@0 309 *
michael@0 310 * @param jdn the desired time, expressed as a "julian day number",
michael@0 311 * which is the number of elapsed days since
michael@0 312 * 1/1/4713 BC (Julian), 12:00 GMT. Note that julian day
michael@0 313 * numbers start at <em>noon</em>. To get the jdn for
michael@0 314 * the corresponding midnight, subtract 0.5.
michael@0 315 *
michael@0 316 * @see #getJulianDay
michael@0 317 * @see #JULIAN_EPOCH_MS
michael@0 318 * @internal
michael@0 319 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 320 */
michael@0 321 void CalendarAstronomer::setJulianDay(double jdn) {
michael@0 322 fTime = (double)(jdn * DAY_MS) + JULIAN_EPOCH_MS;
michael@0 323 clearCache();
michael@0 324 julianDay = jdn;
michael@0 325 }
michael@0 326
michael@0 327 /**
michael@0 328 * Get the current time of this <code>CalendarAstronomer</code> object,
michael@0 329 * represented as the number of milliseconds since
michael@0 330 * 1/1/1970 AD 0:00 GMT (Gregorian).
michael@0 331 *
michael@0 332 * @see #setTime
michael@0 333 * @see #getDate
michael@0 334 * @internal
michael@0 335 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 336 */
michael@0 337 UDate CalendarAstronomer::getTime() {
michael@0 338 return fTime;
michael@0 339 }
michael@0 340
michael@0 341 /**
michael@0 342 * Get the current time of this <code>CalendarAstronomer</code> object,
michael@0 343 * expressed as a "julian day number", which is the number of elapsed
michael@0 344 * days since 1/1/4713 BC (Julian), 12:00 GMT.
michael@0 345 *
michael@0 346 * @see #setJulianDay
michael@0 347 * @see #JULIAN_EPOCH_MS
michael@0 348 * @internal
michael@0 349 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 350 */
michael@0 351 double CalendarAstronomer::getJulianDay() {
michael@0 352 if (isINVALID(julianDay)) {
michael@0 353 julianDay = (fTime - (double)JULIAN_EPOCH_MS) / (double)DAY_MS;
michael@0 354 }
michael@0 355 return julianDay;
michael@0 356 }
michael@0 357
michael@0 358 /**
michael@0 359 * Return this object's time expressed in julian centuries:
michael@0 360 * the number of centuries after 1/1/1900 AD, 12:00 GMT
michael@0 361 *
michael@0 362 * @see #getJulianDay
michael@0 363 * @internal
michael@0 364 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 365 */
michael@0 366 double CalendarAstronomer::getJulianCentury() {
michael@0 367 if (isINVALID(julianCentury)) {
michael@0 368 julianCentury = (getJulianDay() - 2415020.0) / 36525.0;
michael@0 369 }
michael@0 370 return julianCentury;
michael@0 371 }
michael@0 372
michael@0 373 /**
michael@0 374 * Returns the current Greenwich sidereal time, measured in hours
michael@0 375 * @internal
michael@0 376 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 377 */
michael@0 378 double CalendarAstronomer::getGreenwichSidereal() {
michael@0 379 if (isINVALID(siderealTime)) {
michael@0 380 // See page 86 of "Practial Astronomy with your Calculator",
michael@0 381 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 382
michael@0 383 double UT = normalize(fTime/(double)HOUR_MS, 24.);
michael@0 384
michael@0 385 siderealTime = normalize(getSiderealOffset() + UT*1.002737909, 24.);
michael@0 386 }
michael@0 387 return siderealTime;
michael@0 388 }
michael@0 389
michael@0 390 double CalendarAstronomer::getSiderealOffset() {
michael@0 391 if (isINVALID(siderealT0)) {
michael@0 392 double JD = uprv_floor(getJulianDay() - 0.5) + 0.5;
michael@0 393 double S = JD - 2451545.0;
michael@0 394 double T = S / 36525.0;
michael@0 395 siderealT0 = normalize(6.697374558 + 2400.051336*T + 0.000025862*T*T, 24);
michael@0 396 }
michael@0 397 return siderealT0;
michael@0 398 }
michael@0 399
michael@0 400 /**
michael@0 401 * Returns the current local sidereal time, measured in hours
michael@0 402 * @internal
michael@0 403 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 404 */
michael@0 405 double CalendarAstronomer::getLocalSidereal() {
michael@0 406 return normalize(getGreenwichSidereal() + (fGmtOffset/(double)HOUR_MS), 24.);
michael@0 407 }
michael@0 408
michael@0 409 /**
michael@0 410 * Converts local sidereal time to Universal Time.
michael@0 411 *
michael@0 412 * @param lst The Local Sidereal Time, in hours since sidereal midnight
michael@0 413 * on this object's current date.
michael@0 414 *
michael@0 415 * @return The corresponding Universal Time, in milliseconds since
michael@0 416 * 1 Jan 1970, GMT.
michael@0 417 */
michael@0 418 double CalendarAstronomer::lstToUT(double lst) {
michael@0 419 // Convert to local mean time
michael@0 420 double lt = normalize((lst - getSiderealOffset()) * 0.9972695663, 24);
michael@0 421
michael@0 422 // Then find local midnight on this day
michael@0 423 double base = (DAY_MS * ClockMath::floorDivide(fTime + fGmtOffset,(double)DAY_MS)) - fGmtOffset;
michael@0 424
michael@0 425 //out(" lt =" + lt + " hours");
michael@0 426 //out(" base=" + new Date(base));
michael@0 427
michael@0 428 return base + (long)(lt * HOUR_MS);
michael@0 429 }
michael@0 430
michael@0 431
michael@0 432 //-------------------------------------------------------------------------
michael@0 433 // Coordinate transformations, all based on the current time of this object
michael@0 434 //-------------------------------------------------------------------------
michael@0 435
michael@0 436 /**
michael@0 437 * Convert from ecliptic to equatorial coordinates.
michael@0 438 *
michael@0 439 * @param ecliptic A point in the sky in ecliptic coordinates.
michael@0 440 * @return The corresponding point in equatorial coordinates.
michael@0 441 * @internal
michael@0 442 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 443 */
michael@0 444 CalendarAstronomer::Equatorial& CalendarAstronomer::eclipticToEquatorial(CalendarAstronomer::Equatorial& result, const CalendarAstronomer::Ecliptic& ecliptic)
michael@0 445 {
michael@0 446 return eclipticToEquatorial(result, ecliptic.longitude, ecliptic.latitude);
michael@0 447 }
michael@0 448
michael@0 449 /**
michael@0 450 * Convert from ecliptic to equatorial coordinates.
michael@0 451 *
michael@0 452 * @param eclipLong The ecliptic longitude
michael@0 453 * @param eclipLat The ecliptic latitude
michael@0 454 *
michael@0 455 * @return The corresponding point in equatorial coordinates.
michael@0 456 * @internal
michael@0 457 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 458 */
michael@0 459 CalendarAstronomer::Equatorial& CalendarAstronomer::eclipticToEquatorial(CalendarAstronomer::Equatorial& result, double eclipLong, double eclipLat)
michael@0 460 {
michael@0 461 // See page 42 of "Practial Astronomy with your Calculator",
michael@0 462 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 463
michael@0 464 double obliq = eclipticObliquity();
michael@0 465 double sinE = ::sin(obliq);
michael@0 466 double cosE = cos(obliq);
michael@0 467
michael@0 468 double sinL = ::sin(eclipLong);
michael@0 469 double cosL = cos(eclipLong);
michael@0 470
michael@0 471 double sinB = ::sin(eclipLat);
michael@0 472 double cosB = cos(eclipLat);
michael@0 473 double tanB = tan(eclipLat);
michael@0 474
michael@0 475 result.set(atan2(sinL*cosE - tanB*sinE, cosL),
michael@0 476 asin(sinB*cosE + cosB*sinE*sinL) );
michael@0 477 return result;
michael@0 478 }
michael@0 479
michael@0 480 /**
michael@0 481 * Convert from ecliptic longitude to equatorial coordinates.
michael@0 482 *
michael@0 483 * @param eclipLong The ecliptic longitude
michael@0 484 *
michael@0 485 * @return The corresponding point in equatorial coordinates.
michael@0 486 * @internal
michael@0 487 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 488 */
michael@0 489 CalendarAstronomer::Equatorial& CalendarAstronomer::eclipticToEquatorial(CalendarAstronomer::Equatorial& result, double eclipLong)
michael@0 490 {
michael@0 491 return eclipticToEquatorial(result, eclipLong, 0); // TODO: optimize
michael@0 492 }
michael@0 493
michael@0 494 /**
michael@0 495 * @internal
michael@0 496 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 497 */
michael@0 498 CalendarAstronomer::Horizon& CalendarAstronomer::eclipticToHorizon(CalendarAstronomer::Horizon& result, double eclipLong)
michael@0 499 {
michael@0 500 Equatorial equatorial;
michael@0 501 eclipticToEquatorial(equatorial, eclipLong);
michael@0 502
michael@0 503 double H = getLocalSidereal()*CalendarAstronomer::PI/12 - equatorial.ascension; // Hour-angle
michael@0 504
michael@0 505 double sinH = ::sin(H);
michael@0 506 double cosH = cos(H);
michael@0 507 double sinD = ::sin(equatorial.declination);
michael@0 508 double cosD = cos(equatorial.declination);
michael@0 509 double sinL = ::sin(fLatitude);
michael@0 510 double cosL = cos(fLatitude);
michael@0 511
michael@0 512 double altitude = asin(sinD*sinL + cosD*cosL*cosH);
michael@0 513 double azimuth = atan2(-cosD*cosL*sinH, sinD - sinL * ::sin(altitude));
michael@0 514
michael@0 515 result.set(azimuth, altitude);
michael@0 516 return result;
michael@0 517 }
michael@0 518
michael@0 519
michael@0 520 //-------------------------------------------------------------------------
michael@0 521 // The Sun
michael@0 522 //-------------------------------------------------------------------------
michael@0 523
michael@0 524 //
michael@0 525 // Parameters of the Sun's orbit as of the epoch Jan 0.0 1990
michael@0 526 // Angles are in radians (after multiplying by CalendarAstronomer::PI/180)
michael@0 527 //
michael@0 528 #define JD_EPOCH 2447891.5 // Julian day of epoch
michael@0 529
michael@0 530 #define SUN_ETA_G (279.403303 * CalendarAstronomer::PI/180) // Ecliptic longitude at epoch
michael@0 531 #define SUN_OMEGA_G (282.768422 * CalendarAstronomer::PI/180) // Ecliptic longitude of perigee
michael@0 532 #define SUN_E 0.016713 // Eccentricity of orbit
michael@0 533 //double sunR0 1.495585e8 // Semi-major axis in KM
michael@0 534 //double sunTheta0 (0.533128 * CalendarAstronomer::PI/180) // Angular diameter at R0
michael@0 535
michael@0 536 // The following three methods, which compute the sun parameters
michael@0 537 // given above for an arbitrary epoch (whatever time the object is
michael@0 538 // set to), make only a small difference as compared to using the
michael@0 539 // above constants. E.g., Sunset times might differ by ~12
michael@0 540 // seconds. Furthermore, the eta-g computation is befuddled by
michael@0 541 // Duffet-Smith's incorrect coefficients (p.86). I've corrected
michael@0 542 // the first-order coefficient but the others may be off too - no
michael@0 543 // way of knowing without consulting another source.
michael@0 544
michael@0 545 // /**
michael@0 546 // * Return the sun's ecliptic longitude at perigee for the current time.
michael@0 547 // * See Duffett-Smith, p. 86.
michael@0 548 // * @return radians
michael@0 549 // */
michael@0 550 // private double getSunOmegaG() {
michael@0 551 // double T = getJulianCentury();
michael@0 552 // return (281.2208444 + (1.719175 + 0.000452778*T)*T) * DEG_RAD;
michael@0 553 // }
michael@0 554
michael@0 555 // /**
michael@0 556 // * Return the sun's ecliptic longitude for the current time.
michael@0 557 // * See Duffett-Smith, p. 86.
michael@0 558 // * @return radians
michael@0 559 // */
michael@0 560 // private double getSunEtaG() {
michael@0 561 // double T = getJulianCentury();
michael@0 562 // //return (279.6966778 + (36000.76892 + 0.0003025*T)*T) * DEG_RAD;
michael@0 563 // //
michael@0 564 // // The above line is from Duffett-Smith, and yields manifestly wrong
michael@0 565 // // results. The below constant is derived empirically to match the
michael@0 566 // // constant he gives for the 1990 EPOCH.
michael@0 567 // //
michael@0 568 // return (279.6966778 + (-0.3262541582718024 + 0.0003025*T)*T) * DEG_RAD;
michael@0 569 // }
michael@0 570
michael@0 571 // /**
michael@0 572 // * Return the sun's eccentricity of orbit for the current time.
michael@0 573 // * See Duffett-Smith, p. 86.
michael@0 574 // * @return double
michael@0 575 // */
michael@0 576 // private double getSunE() {
michael@0 577 // double T = getJulianCentury();
michael@0 578 // return 0.01675104 - (0.0000418 + 0.000000126*T)*T;
michael@0 579 // }
michael@0 580
michael@0 581 /**
michael@0 582 * Find the "true anomaly" (longitude) of an object from
michael@0 583 * its mean anomaly and the eccentricity of its orbit. This uses
michael@0 584 * an iterative solution to Kepler's equation.
michael@0 585 *
michael@0 586 * @param meanAnomaly The object's longitude calculated as if it were in
michael@0 587 * a regular, circular orbit, measured in radians
michael@0 588 * from the point of perigee.
michael@0 589 *
michael@0 590 * @param eccentricity The eccentricity of the orbit
michael@0 591 *
michael@0 592 * @return The true anomaly (longitude) measured in radians
michael@0 593 */
michael@0 594 static double trueAnomaly(double meanAnomaly, double eccentricity)
michael@0 595 {
michael@0 596 // First, solve Kepler's equation iteratively
michael@0 597 // Duffett-Smith, p.90
michael@0 598 double delta;
michael@0 599 double E = meanAnomaly;
michael@0 600 do {
michael@0 601 delta = E - eccentricity * ::sin(E) - meanAnomaly;
michael@0 602 E = E - delta / (1 - eccentricity * ::cos(E));
michael@0 603 }
michael@0 604 while (uprv_fabs(delta) > 1e-5); // epsilon = 1e-5 rad
michael@0 605
michael@0 606 return 2.0 * ::atan( ::tan(E/2) * ::sqrt( (1+eccentricity)
michael@0 607 /(1-eccentricity) ) );
michael@0 608 }
michael@0 609
michael@0 610 /**
michael@0 611 * The longitude of the sun at the time specified by this object.
michael@0 612 * The longitude is measured in radians along the ecliptic
michael@0 613 * from the "first point of Aries," the point at which the ecliptic
michael@0 614 * crosses the earth's equatorial plane at the vernal equinox.
michael@0 615 * <p>
michael@0 616 * Currently, this method uses an approximation of the two-body Kepler's
michael@0 617 * equation for the earth and the sun. It does not take into account the
michael@0 618 * perturbations caused by the other planets, the moon, etc.
michael@0 619 * @internal
michael@0 620 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 621 */
michael@0 622 double CalendarAstronomer::getSunLongitude()
michael@0 623 {
michael@0 624 // See page 86 of "Practial Astronomy with your Calculator",
michael@0 625 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 626
michael@0 627 if (isINVALID(sunLongitude)) {
michael@0 628 getSunLongitude(getJulianDay(), sunLongitude, meanAnomalySun);
michael@0 629 }
michael@0 630 return sunLongitude;
michael@0 631 }
michael@0 632
michael@0 633 /**
michael@0 634 * TODO Make this public when the entire class is package-private.
michael@0 635 */
michael@0 636 /*public*/ void CalendarAstronomer::getSunLongitude(double jDay, double &longitude, double &meanAnomaly)
michael@0 637 {
michael@0 638 // See page 86 of "Practial Astronomy with your Calculator",
michael@0 639 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 640
michael@0 641 double day = jDay - JD_EPOCH; // Days since epoch
michael@0 642
michael@0 643 // Find the angular distance the sun in a fictitious
michael@0 644 // circular orbit has travelled since the epoch.
michael@0 645 double epochAngle = norm2PI(CalendarAstronomer_PI2/TROPICAL_YEAR*day);
michael@0 646
michael@0 647 // The epoch wasn't at the sun's perigee; find the angular distance
michael@0 648 // since perigee, which is called the "mean anomaly"
michael@0 649 meanAnomaly = norm2PI(epochAngle + SUN_ETA_G - SUN_OMEGA_G);
michael@0 650
michael@0 651 // Now find the "true anomaly", e.g. the real solar longitude
michael@0 652 // by solving Kepler's equation for an elliptical orbit
michael@0 653 // NOTE: The 3rd ed. of the book lists omega_g and eta_g in different
michael@0 654 // equations; omega_g is to be correct.
michael@0 655 longitude = norm2PI(trueAnomaly(meanAnomaly, SUN_E) + SUN_OMEGA_G);
michael@0 656 }
michael@0 657
michael@0 658 /**
michael@0 659 * The position of the sun at this object's current date and time,
michael@0 660 * in equatorial coordinates.
michael@0 661 * @internal
michael@0 662 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 663 */
michael@0 664 CalendarAstronomer::Equatorial& CalendarAstronomer::getSunPosition(CalendarAstronomer::Equatorial& result) {
michael@0 665 return eclipticToEquatorial(result, getSunLongitude(), 0);
michael@0 666 }
michael@0 667
michael@0 668
michael@0 669 /**
michael@0 670 * Constant representing the vernal equinox.
michael@0 671 * For use with {@link #getSunTime getSunTime}.
michael@0 672 * Note: In this case, "vernal" refers to the northern hemisphere's seasons.
michael@0 673 * @internal
michael@0 674 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 675 */
michael@0 676 /*double CalendarAstronomer::VERNAL_EQUINOX() {
michael@0 677 return 0;
michael@0 678 }*/
michael@0 679
michael@0 680 /**
michael@0 681 * Constant representing the summer solstice.
michael@0 682 * For use with {@link #getSunTime getSunTime}.
michael@0 683 * Note: In this case, "summer" refers to the northern hemisphere's seasons.
michael@0 684 * @internal
michael@0 685 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 686 */
michael@0 687 double CalendarAstronomer::SUMMER_SOLSTICE() {
michael@0 688 return (CalendarAstronomer::PI/2);
michael@0 689 }
michael@0 690
michael@0 691 /**
michael@0 692 * Constant representing the autumnal equinox.
michael@0 693 * For use with {@link #getSunTime getSunTime}.
michael@0 694 * Note: In this case, "autumn" refers to the northern hemisphere's seasons.
michael@0 695 * @internal
michael@0 696 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 697 */
michael@0 698 /*double CalendarAstronomer::AUTUMN_EQUINOX() {
michael@0 699 return (CalendarAstronomer::PI);
michael@0 700 }*/
michael@0 701
michael@0 702 /**
michael@0 703 * Constant representing the winter solstice.
michael@0 704 * For use with {@link #getSunTime getSunTime}.
michael@0 705 * Note: In this case, "winter" refers to the northern hemisphere's seasons.
michael@0 706 * @internal
michael@0 707 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 708 */
michael@0 709 double CalendarAstronomer::WINTER_SOLSTICE() {
michael@0 710 return ((CalendarAstronomer::PI*3)/2);
michael@0 711 }
michael@0 712
michael@0 713 CalendarAstronomer::AngleFunc::~AngleFunc() {}
michael@0 714
michael@0 715 /**
michael@0 716 * Find the next time at which the sun's ecliptic longitude will have
michael@0 717 * the desired value.
michael@0 718 * @internal
michael@0 719 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 720 */
michael@0 721 class SunTimeAngleFunc : public CalendarAstronomer::AngleFunc {
michael@0 722 public:
michael@0 723 virtual ~SunTimeAngleFunc();
michael@0 724 virtual double eval(CalendarAstronomer& a) { return a.getSunLongitude(); }
michael@0 725 };
michael@0 726
michael@0 727 SunTimeAngleFunc::~SunTimeAngleFunc() {}
michael@0 728
michael@0 729 UDate CalendarAstronomer::getSunTime(double desired, UBool next)
michael@0 730 {
michael@0 731 SunTimeAngleFunc func;
michael@0 732 return timeOfAngle( func,
michael@0 733 desired,
michael@0 734 TROPICAL_YEAR,
michael@0 735 MINUTE_MS,
michael@0 736 next);
michael@0 737 }
michael@0 738
michael@0 739 CalendarAstronomer::CoordFunc::~CoordFunc() {}
michael@0 740
michael@0 741 class RiseSetCoordFunc : public CalendarAstronomer::CoordFunc {
michael@0 742 public:
michael@0 743 virtual ~RiseSetCoordFunc();
michael@0 744 virtual void eval(CalendarAstronomer::Equatorial& result, CalendarAstronomer&a) { a.getSunPosition(result); }
michael@0 745 };
michael@0 746
michael@0 747 RiseSetCoordFunc::~RiseSetCoordFunc() {}
michael@0 748
michael@0 749 UDate CalendarAstronomer::getSunRiseSet(UBool rise)
michael@0 750 {
michael@0 751 UDate t0 = fTime;
michael@0 752
michael@0 753 // Make a rough guess: 6am or 6pm local time on the current day
michael@0 754 double noon = ClockMath::floorDivide(fTime + fGmtOffset, (double)DAY_MS)*DAY_MS - fGmtOffset + (12*HOUR_MS);
michael@0 755
michael@0 756 U_DEBUG_ASTRO_MSG(("Noon=%.2lf, %sL, gmtoff %.2lf\n", noon, debug_astro_date(noon+fGmtOffset), fGmtOffset));
michael@0 757 setTime(noon + ((rise ? -6 : 6) * HOUR_MS));
michael@0 758 U_DEBUG_ASTRO_MSG(("added %.2lf ms as a guess,\n", ((rise ? -6. : 6.) * HOUR_MS)));
michael@0 759
michael@0 760 RiseSetCoordFunc func;
michael@0 761 double t = riseOrSet(func,
michael@0 762 rise,
michael@0 763 .533 * DEG_RAD, // Angular Diameter
michael@0 764 34. /60.0 * DEG_RAD, // Refraction correction
michael@0 765 MINUTE_MS / 12.); // Desired accuracy
michael@0 766
michael@0 767 setTime(t0);
michael@0 768 return t;
michael@0 769 }
michael@0 770
michael@0 771 // Commented out - currently unused. ICU 2.6, Alan
michael@0 772 // //-------------------------------------------------------------------------
michael@0 773 // // Alternate Sun Rise/Set
michael@0 774 // // See Duffett-Smith p.93
michael@0 775 // //-------------------------------------------------------------------------
michael@0 776 //
michael@0 777 // // This yields worse results (as compared to USNO data) than getSunRiseSet().
michael@0 778 // /**
michael@0 779 // * TODO Make this when the entire class is package-private.
michael@0 780 // */
michael@0 781 // /*public*/ long getSunRiseSet2(boolean rise) {
michael@0 782 // // 1. Calculate coordinates of the sun's center for midnight
michael@0 783 // double jd = uprv_floor(getJulianDay() - 0.5) + 0.5;
michael@0 784 // double[] sl = getSunLongitude(jd);// double lambda1 = sl[0];
michael@0 785 // Equatorial pos1 = eclipticToEquatorial(lambda1, 0);
michael@0 786 //
michael@0 787 // // 2. Add ... to lambda to get position 24 hours later
michael@0 788 // double lambda2 = lambda1 + 0.985647*DEG_RAD;
michael@0 789 // Equatorial pos2 = eclipticToEquatorial(lambda2, 0);
michael@0 790 //
michael@0 791 // // 3. Calculate LSTs of rising and setting for these two positions
michael@0 792 // double tanL = ::tan(fLatitude);
michael@0 793 // double H = ::acos(-tanL * ::tan(pos1.declination));
michael@0 794 // double lst1r = (CalendarAstronomer_PI2 + pos1.ascension - H) * 24 / CalendarAstronomer_PI2;
michael@0 795 // double lst1s = (pos1.ascension + H) * 24 / CalendarAstronomer_PI2;
michael@0 796 // H = ::acos(-tanL * ::tan(pos2.declination));
michael@0 797 // double lst2r = (CalendarAstronomer_PI2-H + pos2.ascension ) * 24 / CalendarAstronomer_PI2;
michael@0 798 // double lst2s = (H + pos2.ascension ) * 24 / CalendarAstronomer_PI2;
michael@0 799 // if (lst1r > 24) lst1r -= 24;
michael@0 800 // if (lst1s > 24) lst1s -= 24;
michael@0 801 // if (lst2r > 24) lst2r -= 24;
michael@0 802 // if (lst2s > 24) lst2s -= 24;
michael@0 803 //
michael@0 804 // // 4. Convert LSTs to GSTs. If GST1 > GST2, add 24 to GST2.
michael@0 805 // double gst1r = lstToGst(lst1r);
michael@0 806 // double gst1s = lstToGst(lst1s);
michael@0 807 // double gst2r = lstToGst(lst2r);
michael@0 808 // double gst2s = lstToGst(lst2s);
michael@0 809 // if (gst1r > gst2r) gst2r += 24;
michael@0 810 // if (gst1s > gst2s) gst2s += 24;
michael@0 811 //
michael@0 812 // // 5. Calculate GST at 0h UT of this date
michael@0 813 // double t00 = utToGst(0);
michael@0 814 //
michael@0 815 // // 6. Calculate GST at 0h on the observer's longitude
michael@0 816 // double offset = ::round(fLongitude*12/PI); // p.95 step 6; he _rounds_ to nearest 15 deg.
michael@0 817 // double t00p = t00 - offset*1.002737909;
michael@0 818 // if (t00p < 0) t00p += 24; // do NOT normalize
michael@0 819 //
michael@0 820 // // 7. Adjust
michael@0 821 // if (gst1r < t00p) {
michael@0 822 // gst1r += 24;
michael@0 823 // gst2r += 24;
michael@0 824 // }
michael@0 825 // if (gst1s < t00p) {
michael@0 826 // gst1s += 24;
michael@0 827 // gst2s += 24;
michael@0 828 // }
michael@0 829 //
michael@0 830 // // 8.
michael@0 831 // double gstr = (24.07*gst1r-t00*(gst2r-gst1r))/(24.07+gst1r-gst2r);
michael@0 832 // double gsts = (24.07*gst1s-t00*(gst2s-gst1s))/(24.07+gst1s-gst2s);
michael@0 833 //
michael@0 834 // // 9. Correct for parallax, refraction, and sun's diameter
michael@0 835 // double dec = (pos1.declination + pos2.declination) / 2;
michael@0 836 // double psi = ::acos(sin(fLatitude) / cos(dec));
michael@0 837 // double x = 0.830725 * DEG_RAD; // parallax+refraction+diameter
michael@0 838 // double y = ::asin(sin(x) / ::sin(psi)) * RAD_DEG;
michael@0 839 // double delta_t = 240 * y / cos(dec) / 3600; // hours
michael@0 840 //
michael@0 841 // // 10. Add correction to GSTs, subtract from GSTr
michael@0 842 // gstr -= delta_t;
michael@0 843 // gsts += delta_t;
michael@0 844 //
michael@0 845 // // 11. Convert GST to UT and then to local civil time
michael@0 846 // double ut = gstToUt(rise ? gstr : gsts);
michael@0 847 // //System.out.println((rise?"rise=":"set=") + ut + ", delta_t=" + delta_t);
michael@0 848 // long midnight = DAY_MS * (time / DAY_MS); // Find UT midnight on this day
michael@0 849 // return midnight + (long) (ut * 3600000);
michael@0 850 // }
michael@0 851
michael@0 852 // Commented out - currently unused. ICU 2.6, Alan
michael@0 853 // /**
michael@0 854 // * Convert local sidereal time to Greenwich sidereal time.
michael@0 855 // * Section 15. Duffett-Smith p.21
michael@0 856 // * @param lst in hours (0..24)
michael@0 857 // * @return GST in hours (0..24)
michael@0 858 // */
michael@0 859 // double lstToGst(double lst) {
michael@0 860 // double delta = fLongitude * 24 / CalendarAstronomer_PI2;
michael@0 861 // return normalize(lst - delta, 24);
michael@0 862 // }
michael@0 863
michael@0 864 // Commented out - currently unused. ICU 2.6, Alan
michael@0 865 // /**
michael@0 866 // * Convert UT to GST on this date.
michael@0 867 // * Section 12. Duffett-Smith p.17
michael@0 868 // * @param ut in hours
michael@0 869 // * @return GST in hours
michael@0 870 // */
michael@0 871 // double utToGst(double ut) {
michael@0 872 // return normalize(getT0() + ut*1.002737909, 24);
michael@0 873 // }
michael@0 874
michael@0 875 // Commented out - currently unused. ICU 2.6, Alan
michael@0 876 // /**
michael@0 877 // * Convert GST to UT on this date.
michael@0 878 // * Section 13. Duffett-Smith p.18
michael@0 879 // * @param gst in hours
michael@0 880 // * @return UT in hours
michael@0 881 // */
michael@0 882 // double gstToUt(double gst) {
michael@0 883 // return normalize(gst - getT0(), 24) * 0.9972695663;
michael@0 884 // }
michael@0 885
michael@0 886 // Commented out - currently unused. ICU 2.6, Alan
michael@0 887 // double getT0() {
michael@0 888 // // Common computation for UT <=> GST
michael@0 889 //
michael@0 890 // // Find JD for 0h UT
michael@0 891 // double jd = uprv_floor(getJulianDay() - 0.5) + 0.5;
michael@0 892 //
michael@0 893 // double s = jd - 2451545.0;
michael@0 894 // double t = s / 36525.0;
michael@0 895 // double t0 = 6.697374558 + (2400.051336 + 0.000025862*t)*t;
michael@0 896 // return t0;
michael@0 897 // }
michael@0 898
michael@0 899 // Commented out - currently unused. ICU 2.6, Alan
michael@0 900 // //-------------------------------------------------------------------------
michael@0 901 // // Alternate Sun Rise/Set
michael@0 902 // // See sci.astro FAQ
michael@0 903 // // http://www.faqs.org/faqs/astronomy/faq/part3/section-5.html
michael@0 904 // //-------------------------------------------------------------------------
michael@0 905 //
michael@0 906 // // Note: This method appears to produce inferior accuracy as
michael@0 907 // // compared to getSunRiseSet().
michael@0 908 //
michael@0 909 // /**
michael@0 910 // * TODO Make this when the entire class is package-private.
michael@0 911 // */
michael@0 912 // /*public*/ long getSunRiseSet3(boolean rise) {
michael@0 913 //
michael@0 914 // // Compute day number for 0.0 Jan 2000 epoch
michael@0 915 // double d = (double)(time - EPOCH_2000_MS) / DAY_MS;
michael@0 916 //
michael@0 917 // // Now compute the Local Sidereal Time, LST:
michael@0 918 // //
michael@0 919 // double LST = 98.9818 + 0.985647352 * d + /*UT*15 + long*/
michael@0 920 // fLongitude*RAD_DEG;
michael@0 921 // //
michael@0 922 // // (east long. positive). Note that LST is here expressed in degrees,
michael@0 923 // // where 15 degrees corresponds to one hour. Since LST really is an angle,
michael@0 924 // // it's convenient to use one unit---degrees---throughout.
michael@0 925 //
michael@0 926 // // COMPUTING THE SUN'S POSITION
michael@0 927 // // ----------------------------
michael@0 928 // //
michael@0 929 // // To be able to compute the Sun's rise/set times, you need to be able to
michael@0 930 // // compute the Sun's position at any time. First compute the "day
michael@0 931 // // number" d as outlined above, for the desired moment. Next compute:
michael@0 932 // //
michael@0 933 // double oblecl = 23.4393 - 3.563E-7 * d;
michael@0 934 // //
michael@0 935 // double w = 282.9404 + 4.70935E-5 * d;
michael@0 936 // double M = 356.0470 + 0.9856002585 * d;
michael@0 937 // double e = 0.016709 - 1.151E-9 * d;
michael@0 938 // //
michael@0 939 // // This is the obliquity of the ecliptic, plus some of the elements of
michael@0 940 // // the Sun's apparent orbit (i.e., really the Earth's orbit): w =
michael@0 941 // // argument of perihelion, M = mean anomaly, e = eccentricity.
michael@0 942 // // Semi-major axis is here assumed to be exactly 1.0 (while not strictly
michael@0 943 // // true, this is still an accurate approximation). Next compute E, the
michael@0 944 // // eccentric anomaly:
michael@0 945 // //
michael@0 946 // double E = M + e*(180/PI) * ::sin(M*DEG_RAD) * ( 1.0 + e*cos(M*DEG_RAD) );
michael@0 947 // //
michael@0 948 // // where E and M are in degrees. This is it---no further iterations are
michael@0 949 // // needed because we know e has a sufficiently small value. Next compute
michael@0 950 // // the true anomaly, v, and the distance, r:
michael@0 951 // //
michael@0 952 // /* r * cos(v) = */ double A = cos(E*DEG_RAD) - e;
michael@0 953 // /* r * ::sin(v) = */ double B = ::sqrt(1 - e*e) * ::sin(E*DEG_RAD);
michael@0 954 // //
michael@0 955 // // and
michael@0 956 // //
michael@0 957 // // r = sqrt( A*A + B*B )
michael@0 958 // double v = ::atan2( B, A )*RAD_DEG;
michael@0 959 // //
michael@0 960 // // The Sun's true longitude, slon, can now be computed:
michael@0 961 // //
michael@0 962 // double slon = v + w;
michael@0 963 // //
michael@0 964 // // Since the Sun is always at the ecliptic (or at least very very close to
michael@0 965 // // it), we can use simplified formulae to convert slon (the Sun's ecliptic
michael@0 966 // // longitude) to sRA and sDec (the Sun's RA and Dec):
michael@0 967 // //
michael@0 968 // // ::sin(slon) * cos(oblecl)
michael@0 969 // // tan(sRA) = -------------------------
michael@0 970 // // cos(slon)
michael@0 971 // //
michael@0 972 // // ::sin(sDec) = ::sin(oblecl) * ::sin(slon)
michael@0 973 // //
michael@0 974 // // As was the case when computing az, the Azimuth, if possible use an
michael@0 975 // // atan2() function to compute sRA.
michael@0 976 //
michael@0 977 // double sRA = ::atan2(sin(slon*DEG_RAD) * cos(oblecl*DEG_RAD), cos(slon*DEG_RAD))*RAD_DEG;
michael@0 978 //
michael@0 979 // double sin_sDec = ::sin(oblecl*DEG_RAD) * ::sin(slon*DEG_RAD);
michael@0 980 // double sDec = ::asin(sin_sDec)*RAD_DEG;
michael@0 981 //
michael@0 982 // // COMPUTING RISE AND SET TIMES
michael@0 983 // // ----------------------------
michael@0 984 // //
michael@0 985 // // To compute when an object rises or sets, you must compute when it
michael@0 986 // // passes the meridian and the HA of rise/set. Then the rise time is
michael@0 987 // // the meridian time minus HA for rise/set, and the set time is the
michael@0 988 // // meridian time plus the HA for rise/set.
michael@0 989 // //
michael@0 990 // // To find the meridian time, compute the Local Sidereal Time at 0h local
michael@0 991 // // time (or 0h UT if you prefer to work in UT) as outlined above---name
michael@0 992 // // that quantity LST0. The Meridian Time, MT, will now be:
michael@0 993 // //
michael@0 994 // // MT = RA - LST0
michael@0 995 // double MT = normalize(sRA - LST, 360);
michael@0 996 // //
michael@0 997 // // where "RA" is the object's Right Ascension (in degrees!). If negative,
michael@0 998 // // add 360 deg to MT. If the object is the Sun, leave the time as it is,
michael@0 999 // // but if it's stellar, multiply MT by 365.2422/366.2422, to convert from
michael@0 1000 // // sidereal to solar time. Now, compute HA for rise/set, name that
michael@0 1001 // // quantity HA0:
michael@0 1002 // //
michael@0 1003 // // ::sin(h0) - ::sin(lat) * ::sin(Dec)
michael@0 1004 // // cos(HA0) = ---------------------------------
michael@0 1005 // // cos(lat) * cos(Dec)
michael@0 1006 // //
michael@0 1007 // // where h0 is the altitude selected to represent rise/set. For a purely
michael@0 1008 // // mathematical horizon, set h0 = 0 and simplify to:
michael@0 1009 // //
michael@0 1010 // // cos(HA0) = - tan(lat) * tan(Dec)
michael@0 1011 // //
michael@0 1012 // // If you want to account for refraction on the atmosphere, set h0 = -35/60
michael@0 1013 // // degrees (-35 arc minutes), and if you want to compute the rise/set times
michael@0 1014 // // for the Sun's upper limb, set h0 = -50/60 (-50 arc minutes).
michael@0 1015 // //
michael@0 1016 // double h0 = -50/60 * DEG_RAD;
michael@0 1017 //
michael@0 1018 // double HA0 = ::acos(
michael@0 1019 // (sin(h0) - ::sin(fLatitude) * sin_sDec) /
michael@0 1020 // (cos(fLatitude) * cos(sDec*DEG_RAD)))*RAD_DEG;
michael@0 1021 //
michael@0 1022 // // When HA0 has been computed, leave it as it is for the Sun but multiply
michael@0 1023 // // by 365.2422/366.2422 for stellar objects, to convert from sidereal to
michael@0 1024 // // solar time. Finally compute:
michael@0 1025 // //
michael@0 1026 // // Rise time = MT - HA0
michael@0 1027 // // Set time = MT + HA0
michael@0 1028 // //
michael@0 1029 // // convert the times from degrees to hours by dividing by 15.
michael@0 1030 // //
michael@0 1031 // // If you'd like to check that your calculations are accurate or just
michael@0 1032 // // need a quick result, check the USNO's Sun or Moon Rise/Set Table,
michael@0 1033 // // <URL:http://aa.usno.navy.mil/AA/data/docs/RS_OneYear.html>.
michael@0 1034 //
michael@0 1035 // double result = MT + (rise ? -HA0 : HA0); // in degrees
michael@0 1036 //
michael@0 1037 // // Find UT midnight on this day
michael@0 1038 // long midnight = DAY_MS * (time / DAY_MS);
michael@0 1039 //
michael@0 1040 // return midnight + (long) (result * 3600000 / 15);
michael@0 1041 // }
michael@0 1042
michael@0 1043 //-------------------------------------------------------------------------
michael@0 1044 // The Moon
michael@0 1045 //-------------------------------------------------------------------------
michael@0 1046
michael@0 1047 #define moonL0 (318.351648 * CalendarAstronomer::PI/180 ) // Mean long. at epoch
michael@0 1048 #define moonP0 ( 36.340410 * CalendarAstronomer::PI/180 ) // Mean long. of perigee
michael@0 1049 #define moonN0 ( 318.510107 * CalendarAstronomer::PI/180 ) // Mean long. of node
michael@0 1050 #define moonI ( 5.145366 * CalendarAstronomer::PI/180 ) // Inclination of orbit
michael@0 1051 #define moonE ( 0.054900 ) // Eccentricity of orbit
michael@0 1052
michael@0 1053 // These aren't used right now
michael@0 1054 #define moonA ( 3.84401e5 ) // semi-major axis (km)
michael@0 1055 #define moonT0 ( 0.5181 * CalendarAstronomer::PI/180 ) // Angular size at distance A
michael@0 1056 #define moonPi ( 0.9507 * CalendarAstronomer::PI/180 ) // Parallax at distance A
michael@0 1057
michael@0 1058 /**
michael@0 1059 * The position of the moon at the time set on this
michael@0 1060 * object, in equatorial coordinates.
michael@0 1061 * @internal
michael@0 1062 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1063 */
michael@0 1064 const CalendarAstronomer::Equatorial& CalendarAstronomer::getMoonPosition()
michael@0 1065 {
michael@0 1066 //
michael@0 1067 // See page 142 of "Practial Astronomy with your Calculator",
michael@0 1068 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 1069 //
michael@0 1070 if (moonPositionSet == FALSE) {
michael@0 1071 // Calculate the solar longitude. Has the side effect of
michael@0 1072 // filling in "meanAnomalySun" as well.
michael@0 1073 getSunLongitude();
michael@0 1074
michael@0 1075 //
michael@0 1076 // Find the # of days since the epoch of our orbital parameters.
michael@0 1077 // TODO: Convert the time of day portion into ephemeris time
michael@0 1078 //
michael@0 1079 double day = getJulianDay() - JD_EPOCH; // Days since epoch
michael@0 1080
michael@0 1081 // Calculate the mean longitude and anomaly of the moon, based on
michael@0 1082 // a circular orbit. Similar to the corresponding solar calculation.
michael@0 1083 double meanLongitude = norm2PI(13.1763966*PI/180*day + moonL0);
michael@0 1084 meanAnomalyMoon = norm2PI(meanLongitude - 0.1114041*PI/180 * day - moonP0);
michael@0 1085
michael@0 1086 //
michael@0 1087 // Calculate the following corrections:
michael@0 1088 // Evection: the sun's gravity affects the moon's eccentricity
michael@0 1089 // Annual Eqn: variation in the effect due to earth-sun distance
michael@0 1090 // A3: correction factor (for ???)
michael@0 1091 //
michael@0 1092 double evection = 1.2739*PI/180 * ::sin(2 * (meanLongitude - sunLongitude)
michael@0 1093 - meanAnomalyMoon);
michael@0 1094 double annual = 0.1858*PI/180 * ::sin(meanAnomalySun);
michael@0 1095 double a3 = 0.3700*PI/180 * ::sin(meanAnomalySun);
michael@0 1096
michael@0 1097 meanAnomalyMoon += evection - annual - a3;
michael@0 1098
michael@0 1099 //
michael@0 1100 // More correction factors:
michael@0 1101 // center equation of the center correction
michael@0 1102 // a4 yet another error correction (???)
michael@0 1103 //
michael@0 1104 // TODO: Skip the equation of the center correction and solve Kepler's eqn?
michael@0 1105 //
michael@0 1106 double center = 6.2886*PI/180 * ::sin(meanAnomalyMoon);
michael@0 1107 double a4 = 0.2140*PI/180 * ::sin(2 * meanAnomalyMoon);
michael@0 1108
michael@0 1109 // Now find the moon's corrected longitude
michael@0 1110 moonLongitude = meanLongitude + evection + center - annual + a4;
michael@0 1111
michael@0 1112 //
michael@0 1113 // And finally, find the variation, caused by the fact that the sun's
michael@0 1114 // gravitational pull on the moon varies depending on which side of
michael@0 1115 // the earth the moon is on
michael@0 1116 //
michael@0 1117 double variation = 0.6583*CalendarAstronomer::PI/180 * ::sin(2*(moonLongitude - sunLongitude));
michael@0 1118
michael@0 1119 moonLongitude += variation;
michael@0 1120
michael@0 1121 //
michael@0 1122 // What we've calculated so far is the moon's longitude in the plane
michael@0 1123 // of its own orbit. Now map to the ecliptic to get the latitude
michael@0 1124 // and longitude. First we need to find the longitude of the ascending
michael@0 1125 // node, the position on the ecliptic where it is crossed by the moon's
michael@0 1126 // orbit as it crosses from the southern to the northern hemisphere.
michael@0 1127 //
michael@0 1128 double nodeLongitude = norm2PI(moonN0 - 0.0529539*PI/180 * day);
michael@0 1129
michael@0 1130 nodeLongitude -= 0.16*PI/180 * ::sin(meanAnomalySun);
michael@0 1131
michael@0 1132 double y = ::sin(moonLongitude - nodeLongitude);
michael@0 1133 double x = cos(moonLongitude - nodeLongitude);
michael@0 1134
michael@0 1135 moonEclipLong = ::atan2(y*cos(moonI), x) + nodeLongitude;
michael@0 1136 double moonEclipLat = ::asin(y * ::sin(moonI));
michael@0 1137
michael@0 1138 eclipticToEquatorial(moonPosition, moonEclipLong, moonEclipLat);
michael@0 1139 moonPositionSet = TRUE;
michael@0 1140 }
michael@0 1141 return moonPosition;
michael@0 1142 }
michael@0 1143
michael@0 1144 /**
michael@0 1145 * The "age" of the moon at the time specified in this object.
michael@0 1146 * This is really the angle between the
michael@0 1147 * current ecliptic longitudes of the sun and the moon,
michael@0 1148 * measured in radians.
michael@0 1149 *
michael@0 1150 * @see #getMoonPhase
michael@0 1151 * @internal
michael@0 1152 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1153 */
michael@0 1154 double CalendarAstronomer::getMoonAge() {
michael@0 1155 // See page 147 of "Practial Astronomy with your Calculator",
michael@0 1156 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 1157 //
michael@0 1158 // Force the moon's position to be calculated. We're going to use
michael@0 1159 // some the intermediate results cached during that calculation.
michael@0 1160 //
michael@0 1161 getMoonPosition();
michael@0 1162
michael@0 1163 return norm2PI(moonEclipLong - sunLongitude);
michael@0 1164 }
michael@0 1165
michael@0 1166 /**
michael@0 1167 * Calculate the phase of the moon at the time set in this object.
michael@0 1168 * The returned phase is a <code>double</code> in the range
michael@0 1169 * <code>0 <= phase < 1</code>, interpreted as follows:
michael@0 1170 * <ul>
michael@0 1171 * <li>0.00: New moon
michael@0 1172 * <li>0.25: First quarter
michael@0 1173 * <li>0.50: Full moon
michael@0 1174 * <li>0.75: Last quarter
michael@0 1175 * </ul>
michael@0 1176 *
michael@0 1177 * @see #getMoonAge
michael@0 1178 * @internal
michael@0 1179 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1180 */
michael@0 1181 double CalendarAstronomer::getMoonPhase() {
michael@0 1182 // See page 147 of "Practial Astronomy with your Calculator",
michael@0 1183 // by Peter Duffet-Smith, for details on the algorithm.
michael@0 1184 return 0.5 * (1 - cos(getMoonAge()));
michael@0 1185 }
michael@0 1186
michael@0 1187 /**
michael@0 1188 * Constant representing a new moon.
michael@0 1189 * For use with {@link #getMoonTime getMoonTime}
michael@0 1190 * @internal
michael@0 1191 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1192 */
michael@0 1193 const CalendarAstronomer::MoonAge CalendarAstronomer::NEW_MOON() {
michael@0 1194 return CalendarAstronomer::MoonAge(0);
michael@0 1195 }
michael@0 1196
michael@0 1197 /**
michael@0 1198 * Constant representing the moon's first quarter.
michael@0 1199 * For use with {@link #getMoonTime getMoonTime}
michael@0 1200 * @internal
michael@0 1201 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1202 */
michael@0 1203 /*const CalendarAstronomer::MoonAge CalendarAstronomer::FIRST_QUARTER() {
michael@0 1204 return CalendarAstronomer::MoonAge(CalendarAstronomer::PI/2);
michael@0 1205 }*/
michael@0 1206
michael@0 1207 /**
michael@0 1208 * Constant representing a full moon.
michael@0 1209 * For use with {@link #getMoonTime getMoonTime}
michael@0 1210 * @internal
michael@0 1211 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1212 */
michael@0 1213 const CalendarAstronomer::MoonAge CalendarAstronomer::FULL_MOON() {
michael@0 1214 return CalendarAstronomer::MoonAge(CalendarAstronomer::PI);
michael@0 1215 }
michael@0 1216 /**
michael@0 1217 * Constant representing the moon's last quarter.
michael@0 1218 * For use with {@link #getMoonTime getMoonTime}
michael@0 1219 * @internal
michael@0 1220 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1221 */
michael@0 1222
michael@0 1223 class MoonTimeAngleFunc : public CalendarAstronomer::AngleFunc {
michael@0 1224 public:
michael@0 1225 virtual ~MoonTimeAngleFunc();
michael@0 1226 virtual double eval(CalendarAstronomer&a) { return a.getMoonAge(); }
michael@0 1227 };
michael@0 1228
michael@0 1229 MoonTimeAngleFunc::~MoonTimeAngleFunc() {}
michael@0 1230
michael@0 1231 /*const CalendarAstronomer::MoonAge CalendarAstronomer::LAST_QUARTER() {
michael@0 1232 return CalendarAstronomer::MoonAge((CalendarAstronomer::PI*3)/2);
michael@0 1233 }*/
michael@0 1234
michael@0 1235 /**
michael@0 1236 * Find the next or previous time at which the Moon's ecliptic
michael@0 1237 * longitude will have the desired value.
michael@0 1238 * <p>
michael@0 1239 * @param desired The desired longitude.
michael@0 1240 * @param next <tt>true</tt> if the next occurrance of the phase
michael@0 1241 * is desired, <tt>false</tt> for the previous occurrance.
michael@0 1242 * @internal
michael@0 1243 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1244 */
michael@0 1245 UDate CalendarAstronomer::getMoonTime(double desired, UBool next)
michael@0 1246 {
michael@0 1247 MoonTimeAngleFunc func;
michael@0 1248 return timeOfAngle( func,
michael@0 1249 desired,
michael@0 1250 SYNODIC_MONTH,
michael@0 1251 MINUTE_MS,
michael@0 1252 next);
michael@0 1253 }
michael@0 1254
michael@0 1255 /**
michael@0 1256 * Find the next or previous time at which the moon will be in the
michael@0 1257 * desired phase.
michael@0 1258 * <p>
michael@0 1259 * @param desired The desired phase of the moon.
michael@0 1260 * @param next <tt>true</tt> if the next occurrance of the phase
michael@0 1261 * is desired, <tt>false</tt> for the previous occurrance.
michael@0 1262 * @internal
michael@0 1263 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1264 */
michael@0 1265 UDate CalendarAstronomer::getMoonTime(const CalendarAstronomer::MoonAge& desired, UBool next) {
michael@0 1266 return getMoonTime(desired.value, next);
michael@0 1267 }
michael@0 1268
michael@0 1269 class MoonRiseSetCoordFunc : public CalendarAstronomer::CoordFunc {
michael@0 1270 public:
michael@0 1271 virtual ~MoonRiseSetCoordFunc();
michael@0 1272 virtual void eval(CalendarAstronomer::Equatorial& result, CalendarAstronomer&a) { result = a.getMoonPosition(); }
michael@0 1273 };
michael@0 1274
michael@0 1275 MoonRiseSetCoordFunc::~MoonRiseSetCoordFunc() {}
michael@0 1276
michael@0 1277 /**
michael@0 1278 * Returns the time (GMT) of sunrise or sunset on the local date to which
michael@0 1279 * this calendar is currently set.
michael@0 1280 * @internal
michael@0 1281 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1282 */
michael@0 1283 UDate CalendarAstronomer::getMoonRiseSet(UBool rise)
michael@0 1284 {
michael@0 1285 MoonRiseSetCoordFunc func;
michael@0 1286 return riseOrSet(func,
michael@0 1287 rise,
michael@0 1288 .533 * DEG_RAD, // Angular Diameter
michael@0 1289 34 /60.0 * DEG_RAD, // Refraction correction
michael@0 1290 MINUTE_MS); // Desired accuracy
michael@0 1291 }
michael@0 1292
michael@0 1293 //-------------------------------------------------------------------------
michael@0 1294 // Interpolation methods for finding the time at which a given event occurs
michael@0 1295 //-------------------------------------------------------------------------
michael@0 1296
michael@0 1297 UDate CalendarAstronomer::timeOfAngle(AngleFunc& func, double desired,
michael@0 1298 double periodDays, double epsilon, UBool next)
michael@0 1299 {
michael@0 1300 // Find the value of the function at the current time
michael@0 1301 double lastAngle = func.eval(*this);
michael@0 1302
michael@0 1303 // Find out how far we are from the desired angle
michael@0 1304 double deltaAngle = norm2PI(desired - lastAngle) ;
michael@0 1305
michael@0 1306 // Using the average period, estimate the next (or previous) time at
michael@0 1307 // which the desired angle occurs.
michael@0 1308 double deltaT = (deltaAngle + (next ? 0.0 : - CalendarAstronomer_PI2 )) * (periodDays*DAY_MS) / CalendarAstronomer_PI2;
michael@0 1309
michael@0 1310 double lastDeltaT = deltaT; // Liu
michael@0 1311 UDate startTime = fTime; // Liu
michael@0 1312
michael@0 1313 setTime(fTime + uprv_ceil(deltaT));
michael@0 1314
michael@0 1315 // Now iterate until we get the error below epsilon. Throughout
michael@0 1316 // this loop we use normPI to get values in the range -Pi to Pi,
michael@0 1317 // since we're using them as correction factors rather than absolute angles.
michael@0 1318 do {
michael@0 1319 // Evaluate the function at the time we've estimated
michael@0 1320 double angle = func.eval(*this);
michael@0 1321
michael@0 1322 // Find the # of milliseconds per radian at this point on the curve
michael@0 1323 double factor = uprv_fabs(deltaT / normPI(angle-lastAngle));
michael@0 1324
michael@0 1325 // Correct the time estimate based on how far off the angle is
michael@0 1326 deltaT = normPI(desired - angle) * factor;
michael@0 1327
michael@0 1328 // HACK:
michael@0 1329 //
michael@0 1330 // If abs(deltaT) begins to diverge we need to quit this loop.
michael@0 1331 // This only appears to happen when attempting to locate, for
michael@0 1332 // example, a new moon on the day of the new moon. E.g.:
michael@0 1333 //
michael@0 1334 // This result is correct:
michael@0 1335 // newMoon(7508(Mon Jul 23 00:00:00 CST 1990,false))=
michael@0 1336 // Sun Jul 22 10:57:41 CST 1990
michael@0 1337 //
michael@0 1338 // But attempting to make the same call a day earlier causes deltaT
michael@0 1339 // to diverge:
michael@0 1340 // CalendarAstronomer.timeOfAngle() diverging: 1.348508727575625E9 ->
michael@0 1341 // 1.3649828540224032E9
michael@0 1342 // newMoon(7507(Sun Jul 22 00:00:00 CST 1990,false))=
michael@0 1343 // Sun Jul 08 13:56:15 CST 1990
michael@0 1344 //
michael@0 1345 // As a temporary solution, we catch this specific condition and
michael@0 1346 // adjust our start time by one eighth period days (either forward
michael@0 1347 // or backward) and try again.
michael@0 1348 // Liu 11/9/00
michael@0 1349 if (uprv_fabs(deltaT) > uprv_fabs(lastDeltaT)) {
michael@0 1350 double delta = uprv_ceil (periodDays * DAY_MS / 8.0);
michael@0 1351 setTime(startTime + (next ? delta : -delta));
michael@0 1352 return timeOfAngle(func, desired, periodDays, epsilon, next);
michael@0 1353 }
michael@0 1354
michael@0 1355 lastDeltaT = deltaT;
michael@0 1356 lastAngle = angle;
michael@0 1357
michael@0 1358 setTime(fTime + uprv_ceil(deltaT));
michael@0 1359 }
michael@0 1360 while (uprv_fabs(deltaT) > epsilon);
michael@0 1361
michael@0 1362 return fTime;
michael@0 1363 }
michael@0 1364
michael@0 1365 UDate CalendarAstronomer::riseOrSet(CoordFunc& func, UBool rise,
michael@0 1366 double diameter, double refraction,
michael@0 1367 double epsilon)
michael@0 1368 {
michael@0 1369 Equatorial pos;
michael@0 1370 double tanL = ::tan(fLatitude);
michael@0 1371 double deltaT = 0;
michael@0 1372 int32_t count = 0;
michael@0 1373
michael@0 1374 //
michael@0 1375 // Calculate the object's position at the current time, then use that
michael@0 1376 // position to calculate the time of rising or setting. The position
michael@0 1377 // will be different at that time, so iterate until the error is allowable.
michael@0 1378 //
michael@0 1379 U_DEBUG_ASTRO_MSG(("setup rise=%s, dia=%.3lf, ref=%.3lf, eps=%.3lf\n",
michael@0 1380 rise?"T":"F", diameter, refraction, epsilon));
michael@0 1381 do {
michael@0 1382 // See "Practical Astronomy With Your Calculator, section 33.
michael@0 1383 func.eval(pos, *this);
michael@0 1384 double angle = ::acos(-tanL * ::tan(pos.declination));
michael@0 1385 double lst = ((rise ? CalendarAstronomer_PI2-angle : angle) + pos.ascension ) * 24 / CalendarAstronomer_PI2;
michael@0 1386
michael@0 1387 // Convert from LST to Universal Time.
michael@0 1388 UDate newTime = lstToUT( lst );
michael@0 1389
michael@0 1390 deltaT = newTime - fTime;
michael@0 1391 setTime(newTime);
michael@0 1392 U_DEBUG_ASTRO_MSG(("%d] dT=%.3lf, angle=%.3lf, lst=%.3lf, A=%.3lf/D=%.3lf\n",
michael@0 1393 count, deltaT, angle, lst, pos.ascension, pos.declination));
michael@0 1394 }
michael@0 1395 while (++ count < 5 && uprv_fabs(deltaT) > epsilon);
michael@0 1396
michael@0 1397 // Calculate the correction due to refraction and the object's angular diameter
michael@0 1398 double cosD = ::cos(pos.declination);
michael@0 1399 double psi = ::acos(sin(fLatitude) / cosD);
michael@0 1400 double x = diameter / 2 + refraction;
michael@0 1401 double y = ::asin(sin(x) / ::sin(psi));
michael@0 1402 long delta = (long)((240 * y * RAD_DEG / cosD)*SECOND_MS);
michael@0 1403
michael@0 1404 return fTime + (rise ? -delta : delta);
michael@0 1405 }
michael@0 1406 /**
michael@0 1407 * Return the obliquity of the ecliptic (the angle between the ecliptic
michael@0 1408 * and the earth's equator) at the current time. This varies due to
michael@0 1409 * the precession of the earth's axis.
michael@0 1410 *
michael@0 1411 * @return the obliquity of the ecliptic relative to the equator,
michael@0 1412 * measured in radians.
michael@0 1413 */
michael@0 1414 double CalendarAstronomer::eclipticObliquity() {
michael@0 1415 if (isINVALID(eclipObliquity)) {
michael@0 1416 const double epoch = 2451545.0; // 2000 AD, January 1.5
michael@0 1417
michael@0 1418 double T = (getJulianDay() - epoch) / 36525;
michael@0 1419
michael@0 1420 eclipObliquity = 23.439292
michael@0 1421 - 46.815/3600 * T
michael@0 1422 - 0.0006/3600 * T*T
michael@0 1423 + 0.00181/3600 * T*T*T;
michael@0 1424
michael@0 1425 eclipObliquity *= DEG_RAD;
michael@0 1426 }
michael@0 1427 return eclipObliquity;
michael@0 1428 }
michael@0 1429
michael@0 1430
michael@0 1431 //-------------------------------------------------------------------------
michael@0 1432 // Private data
michael@0 1433 //-------------------------------------------------------------------------
michael@0 1434 void CalendarAstronomer::clearCache() {
michael@0 1435 const double INVALID = uprv_getNaN();
michael@0 1436
michael@0 1437 julianDay = INVALID;
michael@0 1438 julianCentury = INVALID;
michael@0 1439 sunLongitude = INVALID;
michael@0 1440 meanAnomalySun = INVALID;
michael@0 1441 moonLongitude = INVALID;
michael@0 1442 moonEclipLong = INVALID;
michael@0 1443 meanAnomalyMoon = INVALID;
michael@0 1444 eclipObliquity = INVALID;
michael@0 1445 siderealTime = INVALID;
michael@0 1446 siderealT0 = INVALID;
michael@0 1447 moonPositionSet = FALSE;
michael@0 1448 }
michael@0 1449
michael@0 1450 //private static void out(String s) {
michael@0 1451 // System.out.println(s);
michael@0 1452 //}
michael@0 1453
michael@0 1454 //private static String deg(double rad) {
michael@0 1455 // return Double.toString(rad * RAD_DEG);
michael@0 1456 //}
michael@0 1457
michael@0 1458 //private static String hours(long ms) {
michael@0 1459 // return Double.toString((double)ms / HOUR_MS) + " hours";
michael@0 1460 //}
michael@0 1461
michael@0 1462 /**
michael@0 1463 * @internal
michael@0 1464 * @deprecated ICU 2.4. This class may be removed or modified.
michael@0 1465 */
michael@0 1466 /*UDate CalendarAstronomer::local(UDate localMillis) {
michael@0 1467 // TODO - srl ?
michael@0 1468 TimeZone *tz = TimeZone::createDefault();
michael@0 1469 int32_t rawOffset;
michael@0 1470 int32_t dstOffset;
michael@0 1471 UErrorCode status = U_ZERO_ERROR;
michael@0 1472 tz->getOffset(localMillis, TRUE, rawOffset, dstOffset, status);
michael@0 1473 delete tz;
michael@0 1474 return localMillis - rawOffset;
michael@0 1475 }*/
michael@0 1476
michael@0 1477 // Debugging functions
michael@0 1478 UnicodeString CalendarAstronomer::Ecliptic::toString() const
michael@0 1479 {
michael@0 1480 #ifdef U_DEBUG_ASTRO
michael@0 1481 char tmp[800];
michael@0 1482 sprintf(tmp, "[%.5f,%.5f]", longitude*RAD_DEG, latitude*RAD_DEG);
michael@0 1483 return UnicodeString(tmp, "");
michael@0 1484 #else
michael@0 1485 return UnicodeString();
michael@0 1486 #endif
michael@0 1487 }
michael@0 1488
michael@0 1489 UnicodeString CalendarAstronomer::Equatorial::toString() const
michael@0 1490 {
michael@0 1491 #ifdef U_DEBUG_ASTRO
michael@0 1492 char tmp[400];
michael@0 1493 sprintf(tmp, "%f,%f",
michael@0 1494 (ascension*RAD_DEG), (declination*RAD_DEG));
michael@0 1495 return UnicodeString(tmp, "");
michael@0 1496 #else
michael@0 1497 return UnicodeString();
michael@0 1498 #endif
michael@0 1499 }
michael@0 1500
michael@0 1501 UnicodeString CalendarAstronomer::Horizon::toString() const
michael@0 1502 {
michael@0 1503 #ifdef U_DEBUG_ASTRO
michael@0 1504 char tmp[800];
michael@0 1505 sprintf(tmp, "[%.5f,%.5f]", altitude*RAD_DEG, azimuth*RAD_DEG);
michael@0 1506 return UnicodeString(tmp, "");
michael@0 1507 #else
michael@0 1508 return UnicodeString();
michael@0 1509 #endif
michael@0 1510 }
michael@0 1511
michael@0 1512
michael@0 1513 // static private String radToHms(double angle) {
michael@0 1514 // int hrs = (int) (angle*RAD_HOUR);
michael@0 1515 // int min = (int)((angle*RAD_HOUR - hrs) * 60);
michael@0 1516 // int sec = (int)((angle*RAD_HOUR - hrs - min/60.0) * 3600);
michael@0 1517
michael@0 1518 // return Integer.toString(hrs) + "h" + min + "m" + sec + "s";
michael@0 1519 // }
michael@0 1520
michael@0 1521 // static private String radToDms(double angle) {
michael@0 1522 // int deg = (int) (angle*RAD_DEG);
michael@0 1523 // int min = (int)((angle*RAD_DEG - deg) * 60);
michael@0 1524 // int sec = (int)((angle*RAD_DEG - deg - min/60.0) * 3600);
michael@0 1525
michael@0 1526 // return Integer.toString(deg) + "\u00b0" + min + "'" + sec + "\"";
michael@0 1527 // }
michael@0 1528
michael@0 1529 // =============== Calendar Cache ================
michael@0 1530
michael@0 1531 void CalendarCache::createCache(CalendarCache** cache, UErrorCode& status) {
michael@0 1532 ucln_i18n_registerCleanup(UCLN_I18N_ASTRO_CALENDAR, calendar_astro_cleanup);
michael@0 1533 if(cache == NULL) {
michael@0 1534 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1535 } else {
michael@0 1536 *cache = new CalendarCache(32, status);
michael@0 1537 if(U_FAILURE(status)) {
michael@0 1538 delete *cache;
michael@0 1539 *cache = NULL;
michael@0 1540 }
michael@0 1541 }
michael@0 1542 }
michael@0 1543
michael@0 1544 int32_t CalendarCache::get(CalendarCache** cache, int32_t key, UErrorCode &status) {
michael@0 1545 int32_t res;
michael@0 1546
michael@0 1547 if(U_FAILURE(status)) {
michael@0 1548 return 0;
michael@0 1549 }
michael@0 1550 umtx_lock(&ccLock);
michael@0 1551
michael@0 1552 if(*cache == NULL) {
michael@0 1553 createCache(cache, status);
michael@0 1554 if(U_FAILURE(status)) {
michael@0 1555 umtx_unlock(&ccLock);
michael@0 1556 return 0;
michael@0 1557 }
michael@0 1558 }
michael@0 1559
michael@0 1560 res = uhash_igeti((*cache)->fTable, key);
michael@0 1561 U_DEBUG_ASTRO_MSG(("%p: GET: [%d] == %d\n", (*cache)->fTable, key, res));
michael@0 1562
michael@0 1563 umtx_unlock(&ccLock);
michael@0 1564 return res;
michael@0 1565 }
michael@0 1566
michael@0 1567 void CalendarCache::put(CalendarCache** cache, int32_t key, int32_t value, UErrorCode &status) {
michael@0 1568 if(U_FAILURE(status)) {
michael@0 1569 return;
michael@0 1570 }
michael@0 1571 umtx_lock(&ccLock);
michael@0 1572
michael@0 1573 if(*cache == NULL) {
michael@0 1574 createCache(cache, status);
michael@0 1575 if(U_FAILURE(status)) {
michael@0 1576 umtx_unlock(&ccLock);
michael@0 1577 return;
michael@0 1578 }
michael@0 1579 }
michael@0 1580
michael@0 1581 uhash_iputi((*cache)->fTable, key, value, &status);
michael@0 1582 U_DEBUG_ASTRO_MSG(("%p: PUT: [%d] := %d\n", (*cache)->fTable, key, value));
michael@0 1583
michael@0 1584 umtx_unlock(&ccLock);
michael@0 1585 }
michael@0 1586
michael@0 1587 CalendarCache::CalendarCache(int32_t size, UErrorCode &status) {
michael@0 1588 fTable = uhash_openSize(uhash_hashLong, uhash_compareLong, NULL, size, &status);
michael@0 1589 U_DEBUG_ASTRO_MSG(("%p: Opening.\n", fTable));
michael@0 1590 }
michael@0 1591
michael@0 1592 CalendarCache::~CalendarCache() {
michael@0 1593 if(fTable != NULL) {
michael@0 1594 U_DEBUG_ASTRO_MSG(("%p: Closing.\n", fTable));
michael@0 1595 uhash_close(fTable);
michael@0 1596 }
michael@0 1597 }
michael@0 1598
michael@0 1599 U_NAMESPACE_END
michael@0 1600
michael@0 1601 #endif // !UCONFIG_NO_FORMATTING

mercurial