michael@0: /************************************************************************ michael@0: * Copyright (C) 1996-2012, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ************************************************************************ michael@0: * 2003-nov-07 srl Port from Java michael@0: */ michael@0: michael@0: #include "astro.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/calendar.h" michael@0: #include michael@0: #include michael@0: #include "unicode/putil.h" michael@0: #include "uhash.h" michael@0: #include "umutex.h" michael@0: #include "ucln_in.h" michael@0: #include "putilimp.h" michael@0: #include // for toString() michael@0: michael@0: #if defined (PI) michael@0: #undef PI michael@0: #endif michael@0: michael@0: #ifdef U_DEBUG_ASTRO michael@0: # include "uresimp.h" // for debugging michael@0: michael@0: static void debug_astro_loc(const char *f, int32_t l) michael@0: { michael@0: fprintf(stderr, "%s:%d: ", f, l); michael@0: } michael@0: michael@0: static void debug_astro_msg(const char *pat, ...) michael@0: { michael@0: va_list ap; michael@0: va_start(ap, pat); michael@0: vfprintf(stderr, pat, ap); michael@0: fflush(stderr); michael@0: } michael@0: #include "unicode/datefmt.h" michael@0: #include "unicode/ustring.h" michael@0: static const char * debug_astro_date(UDate d) { michael@0: static char gStrBuf[1024]; michael@0: static DateFormat *df = NULL; michael@0: if(df == NULL) { michael@0: df = DateFormat::createDateTimeInstance(DateFormat::MEDIUM, DateFormat::MEDIUM, Locale::getUS()); michael@0: df->adoptTimeZone(TimeZone::getGMT()->clone()); michael@0: } michael@0: UnicodeString str; michael@0: df->format(d,str); michael@0: u_austrncpy(gStrBuf,str.getTerminatedBuffer(),sizeof(gStrBuf)-1); michael@0: return gStrBuf; michael@0: } michael@0: michael@0: // must use double parens, i.e.: U_DEBUG_ASTRO_MSG(("four is: %d",4)); michael@0: #define U_DEBUG_ASTRO_MSG(x) {debug_astro_loc(__FILE__,__LINE__);debug_astro_msg x;} michael@0: #else michael@0: #define U_DEBUG_ASTRO_MSG(x) michael@0: #endif michael@0: michael@0: static inline UBool isINVALID(double d) { michael@0: return(uprv_isNaN(d)); michael@0: } michael@0: michael@0: static UMutex ccLock = U_MUTEX_INITIALIZER; michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool calendar_astro_cleanup(void) { michael@0: return TRUE; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * The number of standard hours in one sidereal day. michael@0: * Approximately 24.93. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define SIDEREAL_DAY (23.93446960027) michael@0: michael@0: /** michael@0: * The number of sidereal hours in one mean solar day. michael@0: * Approximately 24.07. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define SOLAR_DAY (24.065709816) michael@0: michael@0: /** michael@0: * The average number of solar days from one new moon to the next. This is the time michael@0: * it takes for the moon to return the same ecliptic longitude as the sun. michael@0: * It is longer than the sidereal month because the sun's longitude increases michael@0: * during the year due to the revolution of the earth around the sun. michael@0: * Approximately 29.53. michael@0: * michael@0: * @see #SIDEREAL_MONTH michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: const double CalendarAstronomer::SYNODIC_MONTH = 29.530588853; michael@0: michael@0: /** michael@0: * The average number of days it takes michael@0: * for the moon to return to the same ecliptic longitude relative to the michael@0: * stellar background. This is referred to as the sidereal month. michael@0: * It is shorter than the synodic month due to michael@0: * the revolution of the earth around the sun. michael@0: * Approximately 27.32. michael@0: * michael@0: * @see #SYNODIC_MONTH michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define SIDEREAL_MONTH 27.32166 michael@0: michael@0: /** michael@0: * The average number number of days between successive vernal equinoxes. michael@0: * Due to the precession of the earth's michael@0: * axis, this is not precisely the same as the sidereal year. michael@0: * Approximately 365.24 michael@0: * michael@0: * @see #SIDEREAL_YEAR michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define TROPICAL_YEAR 365.242191 michael@0: michael@0: /** michael@0: * The average number of days it takes michael@0: * for the sun to return to the same position against the fixed stellar michael@0: * background. This is the duration of one orbit of the earth about the sun michael@0: * as it would appear to an outside observer. michael@0: * Due to the precession of the earth's michael@0: * axis, this is not precisely the same as the tropical year. michael@0: * Approximately 365.25. michael@0: * michael@0: * @see #TROPICAL_YEAR michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define SIDEREAL_YEAR 365.25636 michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Time-related constants michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * The number of milliseconds in one second. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define SECOND_MS U_MILLIS_PER_SECOND michael@0: michael@0: /** michael@0: * The number of milliseconds in one minute. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define MINUTE_MS U_MILLIS_PER_MINUTE michael@0: michael@0: /** michael@0: * The number of milliseconds in one hour. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define HOUR_MS U_MILLIS_PER_HOUR michael@0: michael@0: /** michael@0: * The number of milliseconds in one day. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define DAY_MS U_MILLIS_PER_DAY michael@0: michael@0: /** michael@0: * The start of the julian day numbering scheme used by astronomers, which michael@0: * is 1/1/4713 BC (Julian), 12:00 GMT. This is given as the number of milliseconds michael@0: * since 1/1/1970 AD (Gregorian), a negative number. michael@0: * Note that julian day numbers and michael@0: * the Julian calendar are not the same thing. Also note that michael@0: * julian days start at noon, not midnight. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: #define JULIAN_EPOCH_MS -210866760000000.0 michael@0: michael@0: michael@0: /** michael@0: * Milliseconds value for 0.0 January 2000 AD. michael@0: */ michael@0: #define EPOCH_2000_MS 946598400000.0 michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Assorted private data used for conversions michael@0: //------------------------------------------------------------------------- michael@0: michael@0: // My own copies of these so compilers are more likely to optimize them away michael@0: const double CalendarAstronomer::PI = 3.14159265358979323846; michael@0: michael@0: #define CalendarAstronomer_PI2 (CalendarAstronomer::PI*2.0) michael@0: #define RAD_HOUR ( 12 / CalendarAstronomer::PI ) // radians -> hours michael@0: #define DEG_RAD ( CalendarAstronomer::PI / 180 ) // degrees -> radians michael@0: #define RAD_DEG ( 180 / CalendarAstronomer::PI ) // radians -> degrees michael@0: michael@0: /*** michael@0: * Given 'value', add or subtract 'range' until 0 <= 'value' < range. michael@0: * The modulus operator. michael@0: */ michael@0: inline static double normalize(double value, double range) { michael@0: return value - range * ClockMath::floorDivide(value, range); michael@0: } michael@0: michael@0: /** michael@0: * Normalize an angle so that it's in the range 0 - 2pi. michael@0: * For positive angles this is just (angle % 2pi), but the Java michael@0: * mod operator doesn't work that way for negative numbers.... michael@0: */ michael@0: inline static double norm2PI(double angle) { michael@0: return normalize(angle, CalendarAstronomer::PI * 2.0); michael@0: } michael@0: michael@0: /** michael@0: * Normalize an angle into the range -PI - PI michael@0: */ michael@0: inline static double normPI(double angle) { michael@0: return normalize(angle + CalendarAstronomer::PI, CalendarAstronomer::PI * 2.0) - CalendarAstronomer::PI; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Construct a new CalendarAstronomer object that is initialized to michael@0: * the current date and time. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::CalendarAstronomer(): michael@0: fTime(Calendar::getNow()), fLongitude(0.0), fLatitude(0.0), fGmtOffset(0.0), moonPosition(0,0), moonPositionSet(FALSE) { michael@0: clearCache(); michael@0: } michael@0: michael@0: /** michael@0: * Construct a new CalendarAstronomer object that is initialized to michael@0: * the specified date and time. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::CalendarAstronomer(UDate d): fTime(d), fLongitude(0.0), fLatitude(0.0), fGmtOffset(0.0), moonPosition(0,0), moonPositionSet(FALSE) { michael@0: clearCache(); michael@0: } michael@0: michael@0: /** michael@0: * Construct a new CalendarAstronomer object with the given michael@0: * latitude and longitude. The object's time is set to the current michael@0: * date and time. michael@0: *

michael@0: * @param longitude The desired longitude, in degrees east of michael@0: * the Greenwich meridian. michael@0: * michael@0: * @param latitude The desired latitude, in degrees. Positive michael@0: * values signify North, negative South. michael@0: * michael@0: * @see java.util.Date#getTime() michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::CalendarAstronomer(double longitude, double latitude) : michael@0: fTime(Calendar::getNow()), moonPosition(0,0), moonPositionSet(FALSE) { michael@0: fLongitude = normPI(longitude * (double)DEG_RAD); michael@0: fLatitude = normPI(latitude * (double)DEG_RAD); michael@0: fGmtOffset = (double)(fLongitude * 24. * (double)HOUR_MS / (double)CalendarAstronomer_PI2); michael@0: clearCache(); michael@0: } michael@0: michael@0: CalendarAstronomer::~CalendarAstronomer() michael@0: { michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Time and date getters and setters michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Set the current date and time of this CalendarAstronomer object. All michael@0: * astronomical calculations are performed based on this time setting. michael@0: * michael@0: * @param aTime the date and time, expressed as the number of milliseconds since michael@0: * 1/1/1970 0:00 GMT (Gregorian). michael@0: * michael@0: * @see #setDate michael@0: * @see #getTime michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: void CalendarAstronomer::setTime(UDate aTime) { michael@0: fTime = aTime; michael@0: U_DEBUG_ASTRO_MSG(("setTime(%.1lf, %sL)\n", aTime, debug_astro_date(aTime+fGmtOffset))); michael@0: clearCache(); michael@0: } michael@0: michael@0: /** michael@0: * Set the current date and time of this CalendarAstronomer object. All michael@0: * astronomical calculations are performed based on this time setting. michael@0: * michael@0: * @param jdn the desired time, expressed as a "julian day number", michael@0: * which is the number of elapsed days since michael@0: * 1/1/4713 BC (Julian), 12:00 GMT. Note that julian day michael@0: * numbers start at noon. To get the jdn for michael@0: * the corresponding midnight, subtract 0.5. michael@0: * michael@0: * @see #getJulianDay michael@0: * @see #JULIAN_EPOCH_MS michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: void CalendarAstronomer::setJulianDay(double jdn) { michael@0: fTime = (double)(jdn * DAY_MS) + JULIAN_EPOCH_MS; michael@0: clearCache(); michael@0: julianDay = jdn; michael@0: } michael@0: michael@0: /** michael@0: * Get the current time of this CalendarAstronomer object, michael@0: * represented as the number of milliseconds since michael@0: * 1/1/1970 AD 0:00 GMT (Gregorian). michael@0: * michael@0: * @see #setTime michael@0: * @see #getDate michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: UDate CalendarAstronomer::getTime() { michael@0: return fTime; michael@0: } michael@0: michael@0: /** michael@0: * Get the current time of this CalendarAstronomer object, michael@0: * expressed as a "julian day number", which is the number of elapsed michael@0: * days since 1/1/4713 BC (Julian), 12:00 GMT. michael@0: * michael@0: * @see #setJulianDay michael@0: * @see #JULIAN_EPOCH_MS michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getJulianDay() { michael@0: if (isINVALID(julianDay)) { michael@0: julianDay = (fTime - (double)JULIAN_EPOCH_MS) / (double)DAY_MS; michael@0: } michael@0: return julianDay; michael@0: } michael@0: michael@0: /** michael@0: * Return this object's time expressed in julian centuries: michael@0: * the number of centuries after 1/1/1900 AD, 12:00 GMT michael@0: * michael@0: * @see #getJulianDay michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getJulianCentury() { michael@0: if (isINVALID(julianCentury)) { michael@0: julianCentury = (getJulianDay() - 2415020.0) / 36525.0; michael@0: } michael@0: return julianCentury; michael@0: } michael@0: michael@0: /** michael@0: * Returns the current Greenwich sidereal time, measured in hours michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getGreenwichSidereal() { michael@0: if (isINVALID(siderealTime)) { michael@0: // See page 86 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: michael@0: double UT = normalize(fTime/(double)HOUR_MS, 24.); michael@0: michael@0: siderealTime = normalize(getSiderealOffset() + UT*1.002737909, 24.); michael@0: } michael@0: return siderealTime; michael@0: } michael@0: michael@0: double CalendarAstronomer::getSiderealOffset() { michael@0: if (isINVALID(siderealT0)) { michael@0: double JD = uprv_floor(getJulianDay() - 0.5) + 0.5; michael@0: double S = JD - 2451545.0; michael@0: double T = S / 36525.0; michael@0: siderealT0 = normalize(6.697374558 + 2400.051336*T + 0.000025862*T*T, 24); michael@0: } michael@0: return siderealT0; michael@0: } michael@0: michael@0: /** michael@0: * Returns the current local sidereal time, measured in hours michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getLocalSidereal() { michael@0: return normalize(getGreenwichSidereal() + (fGmtOffset/(double)HOUR_MS), 24.); michael@0: } michael@0: michael@0: /** michael@0: * Converts local sidereal time to Universal Time. michael@0: * michael@0: * @param lst The Local Sidereal Time, in hours since sidereal midnight michael@0: * on this object's current date. michael@0: * michael@0: * @return The corresponding Universal Time, in milliseconds since michael@0: * 1 Jan 1970, GMT. michael@0: */ michael@0: double CalendarAstronomer::lstToUT(double lst) { michael@0: // Convert to local mean time michael@0: double lt = normalize((lst - getSiderealOffset()) * 0.9972695663, 24); michael@0: michael@0: // Then find local midnight on this day michael@0: double base = (DAY_MS * ClockMath::floorDivide(fTime + fGmtOffset,(double)DAY_MS)) - fGmtOffset; michael@0: michael@0: //out(" lt =" + lt + " hours"); michael@0: //out(" base=" + new Date(base)); michael@0: michael@0: return base + (long)(lt * HOUR_MS); michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Coordinate transformations, all based on the current time of this object michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Convert from ecliptic to equatorial coordinates. michael@0: * michael@0: * @param ecliptic A point in the sky in ecliptic coordinates. michael@0: * @return The corresponding point in equatorial coordinates. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::Equatorial& CalendarAstronomer::eclipticToEquatorial(CalendarAstronomer::Equatorial& result, const CalendarAstronomer::Ecliptic& ecliptic) michael@0: { michael@0: return eclipticToEquatorial(result, ecliptic.longitude, ecliptic.latitude); michael@0: } michael@0: michael@0: /** michael@0: * Convert from ecliptic to equatorial coordinates. michael@0: * michael@0: * @param eclipLong The ecliptic longitude michael@0: * @param eclipLat The ecliptic latitude michael@0: * michael@0: * @return The corresponding point in equatorial coordinates. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::Equatorial& CalendarAstronomer::eclipticToEquatorial(CalendarAstronomer::Equatorial& result, double eclipLong, double eclipLat) michael@0: { michael@0: // See page 42 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: michael@0: double obliq = eclipticObliquity(); michael@0: double sinE = ::sin(obliq); michael@0: double cosE = cos(obliq); michael@0: michael@0: double sinL = ::sin(eclipLong); michael@0: double cosL = cos(eclipLong); michael@0: michael@0: double sinB = ::sin(eclipLat); michael@0: double cosB = cos(eclipLat); michael@0: double tanB = tan(eclipLat); michael@0: michael@0: result.set(atan2(sinL*cosE - tanB*sinE, cosL), michael@0: asin(sinB*cosE + cosB*sinE*sinL) ); michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Convert from ecliptic longitude to equatorial coordinates. michael@0: * michael@0: * @param eclipLong The ecliptic longitude michael@0: * michael@0: * @return The corresponding point in equatorial coordinates. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::Equatorial& CalendarAstronomer::eclipticToEquatorial(CalendarAstronomer::Equatorial& result, double eclipLong) michael@0: { michael@0: return eclipticToEquatorial(result, eclipLong, 0); // TODO: optimize michael@0: } michael@0: michael@0: /** michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::Horizon& CalendarAstronomer::eclipticToHorizon(CalendarAstronomer::Horizon& result, double eclipLong) michael@0: { michael@0: Equatorial equatorial; michael@0: eclipticToEquatorial(equatorial, eclipLong); michael@0: michael@0: double H = getLocalSidereal()*CalendarAstronomer::PI/12 - equatorial.ascension; // Hour-angle michael@0: michael@0: double sinH = ::sin(H); michael@0: double cosH = cos(H); michael@0: double sinD = ::sin(equatorial.declination); michael@0: double cosD = cos(equatorial.declination); michael@0: double sinL = ::sin(fLatitude); michael@0: double cosL = cos(fLatitude); michael@0: michael@0: double altitude = asin(sinD*sinL + cosD*cosL*cosH); michael@0: double azimuth = atan2(-cosD*cosL*sinH, sinD - sinL * ::sin(altitude)); michael@0: michael@0: result.set(azimuth, altitude); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // The Sun michael@0: //------------------------------------------------------------------------- michael@0: michael@0: // michael@0: // Parameters of the Sun's orbit as of the epoch Jan 0.0 1990 michael@0: // Angles are in radians (after multiplying by CalendarAstronomer::PI/180) michael@0: // michael@0: #define JD_EPOCH 2447891.5 // Julian day of epoch michael@0: michael@0: #define SUN_ETA_G (279.403303 * CalendarAstronomer::PI/180) // Ecliptic longitude at epoch michael@0: #define SUN_OMEGA_G (282.768422 * CalendarAstronomer::PI/180) // Ecliptic longitude of perigee michael@0: #define SUN_E 0.016713 // Eccentricity of orbit michael@0: //double sunR0 1.495585e8 // Semi-major axis in KM michael@0: //double sunTheta0 (0.533128 * CalendarAstronomer::PI/180) // Angular diameter at R0 michael@0: michael@0: // The following three methods, which compute the sun parameters michael@0: // given above for an arbitrary epoch (whatever time the object is michael@0: // set to), make only a small difference as compared to using the michael@0: // above constants. E.g., Sunset times might differ by ~12 michael@0: // seconds. Furthermore, the eta-g computation is befuddled by michael@0: // Duffet-Smith's incorrect coefficients (p.86). I've corrected michael@0: // the first-order coefficient but the others may be off too - no michael@0: // way of knowing without consulting another source. michael@0: michael@0: // /** michael@0: // * Return the sun's ecliptic longitude at perigee for the current time. michael@0: // * See Duffett-Smith, p. 86. michael@0: // * @return radians michael@0: // */ michael@0: // private double getSunOmegaG() { michael@0: // double T = getJulianCentury(); michael@0: // return (281.2208444 + (1.719175 + 0.000452778*T)*T) * DEG_RAD; michael@0: // } michael@0: michael@0: // /** michael@0: // * Return the sun's ecliptic longitude for the current time. michael@0: // * See Duffett-Smith, p. 86. michael@0: // * @return radians michael@0: // */ michael@0: // private double getSunEtaG() { michael@0: // double T = getJulianCentury(); michael@0: // //return (279.6966778 + (36000.76892 + 0.0003025*T)*T) * DEG_RAD; michael@0: // // michael@0: // // The above line is from Duffett-Smith, and yields manifestly wrong michael@0: // // results. The below constant is derived empirically to match the michael@0: // // constant he gives for the 1990 EPOCH. michael@0: // // michael@0: // return (279.6966778 + (-0.3262541582718024 + 0.0003025*T)*T) * DEG_RAD; michael@0: // } michael@0: michael@0: // /** michael@0: // * Return the sun's eccentricity of orbit for the current time. michael@0: // * See Duffett-Smith, p. 86. michael@0: // * @return double michael@0: // */ michael@0: // private double getSunE() { michael@0: // double T = getJulianCentury(); michael@0: // return 0.01675104 - (0.0000418 + 0.000000126*T)*T; michael@0: // } michael@0: michael@0: /** michael@0: * Find the "true anomaly" (longitude) of an object from michael@0: * its mean anomaly and the eccentricity of its orbit. This uses michael@0: * an iterative solution to Kepler's equation. michael@0: * michael@0: * @param meanAnomaly The object's longitude calculated as if it were in michael@0: * a regular, circular orbit, measured in radians michael@0: * from the point of perigee. michael@0: * michael@0: * @param eccentricity The eccentricity of the orbit michael@0: * michael@0: * @return The true anomaly (longitude) measured in radians michael@0: */ michael@0: static double trueAnomaly(double meanAnomaly, double eccentricity) michael@0: { michael@0: // First, solve Kepler's equation iteratively michael@0: // Duffett-Smith, p.90 michael@0: double delta; michael@0: double E = meanAnomaly; michael@0: do { michael@0: delta = E - eccentricity * ::sin(E) - meanAnomaly; michael@0: E = E - delta / (1 - eccentricity * ::cos(E)); michael@0: } michael@0: while (uprv_fabs(delta) > 1e-5); // epsilon = 1e-5 rad michael@0: michael@0: return 2.0 * ::atan( ::tan(E/2) * ::sqrt( (1+eccentricity) michael@0: /(1-eccentricity) ) ); michael@0: } michael@0: michael@0: /** michael@0: * The longitude of the sun at the time specified by this object. michael@0: * The longitude is measured in radians along the ecliptic michael@0: * from the "first point of Aries," the point at which the ecliptic michael@0: * crosses the earth's equatorial plane at the vernal equinox. michael@0: *

michael@0: * Currently, this method uses an approximation of the two-body Kepler's michael@0: * equation for the earth and the sun. It does not take into account the michael@0: * perturbations caused by the other planets, the moon, etc. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getSunLongitude() michael@0: { michael@0: // See page 86 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: michael@0: if (isINVALID(sunLongitude)) { michael@0: getSunLongitude(getJulianDay(), sunLongitude, meanAnomalySun); michael@0: } michael@0: return sunLongitude; michael@0: } michael@0: michael@0: /** michael@0: * TODO Make this public when the entire class is package-private. michael@0: */ michael@0: /*public*/ void CalendarAstronomer::getSunLongitude(double jDay, double &longitude, double &meanAnomaly) michael@0: { michael@0: // See page 86 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: michael@0: double day = jDay - JD_EPOCH; // Days since epoch michael@0: michael@0: // Find the angular distance the sun in a fictitious michael@0: // circular orbit has travelled since the epoch. michael@0: double epochAngle = norm2PI(CalendarAstronomer_PI2/TROPICAL_YEAR*day); michael@0: michael@0: // The epoch wasn't at the sun's perigee; find the angular distance michael@0: // since perigee, which is called the "mean anomaly" michael@0: meanAnomaly = norm2PI(epochAngle + SUN_ETA_G - SUN_OMEGA_G); michael@0: michael@0: // Now find the "true anomaly", e.g. the real solar longitude michael@0: // by solving Kepler's equation for an elliptical orbit michael@0: // NOTE: The 3rd ed. of the book lists omega_g and eta_g in different michael@0: // equations; omega_g is to be correct. michael@0: longitude = norm2PI(trueAnomaly(meanAnomaly, SUN_E) + SUN_OMEGA_G); michael@0: } michael@0: michael@0: /** michael@0: * The position of the sun at this object's current date and time, michael@0: * in equatorial coordinates. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: CalendarAstronomer::Equatorial& CalendarAstronomer::getSunPosition(CalendarAstronomer::Equatorial& result) { michael@0: return eclipticToEquatorial(result, getSunLongitude(), 0); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Constant representing the vernal equinox. michael@0: * For use with {@link #getSunTime getSunTime}. michael@0: * Note: In this case, "vernal" refers to the northern hemisphere's seasons. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: /*double CalendarAstronomer::VERNAL_EQUINOX() { michael@0: return 0; michael@0: }*/ michael@0: michael@0: /** michael@0: * Constant representing the summer solstice. michael@0: * For use with {@link #getSunTime getSunTime}. michael@0: * Note: In this case, "summer" refers to the northern hemisphere's seasons. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::SUMMER_SOLSTICE() { michael@0: return (CalendarAstronomer::PI/2); michael@0: } michael@0: michael@0: /** michael@0: * Constant representing the autumnal equinox. michael@0: * For use with {@link #getSunTime getSunTime}. michael@0: * Note: In this case, "autumn" refers to the northern hemisphere's seasons. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: /*double CalendarAstronomer::AUTUMN_EQUINOX() { michael@0: return (CalendarAstronomer::PI); michael@0: }*/ michael@0: michael@0: /** michael@0: * Constant representing the winter solstice. michael@0: * For use with {@link #getSunTime getSunTime}. michael@0: * Note: In this case, "winter" refers to the northern hemisphere's seasons. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::WINTER_SOLSTICE() { michael@0: return ((CalendarAstronomer::PI*3)/2); michael@0: } michael@0: michael@0: CalendarAstronomer::AngleFunc::~AngleFunc() {} michael@0: michael@0: /** michael@0: * Find the next time at which the sun's ecliptic longitude will have michael@0: * the desired value. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: class SunTimeAngleFunc : public CalendarAstronomer::AngleFunc { michael@0: public: michael@0: virtual ~SunTimeAngleFunc(); michael@0: virtual double eval(CalendarAstronomer& a) { return a.getSunLongitude(); } michael@0: }; michael@0: michael@0: SunTimeAngleFunc::~SunTimeAngleFunc() {} michael@0: michael@0: UDate CalendarAstronomer::getSunTime(double desired, UBool next) michael@0: { michael@0: SunTimeAngleFunc func; michael@0: return timeOfAngle( func, michael@0: desired, michael@0: TROPICAL_YEAR, michael@0: MINUTE_MS, michael@0: next); michael@0: } michael@0: michael@0: CalendarAstronomer::CoordFunc::~CoordFunc() {} michael@0: michael@0: class RiseSetCoordFunc : public CalendarAstronomer::CoordFunc { michael@0: public: michael@0: virtual ~RiseSetCoordFunc(); michael@0: virtual void eval(CalendarAstronomer::Equatorial& result, CalendarAstronomer&a) { a.getSunPosition(result); } michael@0: }; michael@0: michael@0: RiseSetCoordFunc::~RiseSetCoordFunc() {} michael@0: michael@0: UDate CalendarAstronomer::getSunRiseSet(UBool rise) michael@0: { michael@0: UDate t0 = fTime; michael@0: michael@0: // Make a rough guess: 6am or 6pm local time on the current day michael@0: double noon = ClockMath::floorDivide(fTime + fGmtOffset, (double)DAY_MS)*DAY_MS - fGmtOffset + (12*HOUR_MS); michael@0: michael@0: U_DEBUG_ASTRO_MSG(("Noon=%.2lf, %sL, gmtoff %.2lf\n", noon, debug_astro_date(noon+fGmtOffset), fGmtOffset)); michael@0: setTime(noon + ((rise ? -6 : 6) * HOUR_MS)); michael@0: U_DEBUG_ASTRO_MSG(("added %.2lf ms as a guess,\n", ((rise ? -6. : 6.) * HOUR_MS))); michael@0: michael@0: RiseSetCoordFunc func; michael@0: double t = riseOrSet(func, michael@0: rise, michael@0: .533 * DEG_RAD, // Angular Diameter michael@0: 34. /60.0 * DEG_RAD, // Refraction correction michael@0: MINUTE_MS / 12.); // Desired accuracy michael@0: michael@0: setTime(t0); michael@0: return t; michael@0: } michael@0: michael@0: // Commented out - currently unused. ICU 2.6, Alan michael@0: // //------------------------------------------------------------------------- michael@0: // // Alternate Sun Rise/Set michael@0: // // See Duffett-Smith p.93 michael@0: // //------------------------------------------------------------------------- michael@0: // michael@0: // // This yields worse results (as compared to USNO data) than getSunRiseSet(). michael@0: // /** michael@0: // * TODO Make this when the entire class is package-private. michael@0: // */ michael@0: // /*public*/ long getSunRiseSet2(boolean rise) { michael@0: // // 1. Calculate coordinates of the sun's center for midnight michael@0: // double jd = uprv_floor(getJulianDay() - 0.5) + 0.5; michael@0: // double[] sl = getSunLongitude(jd);// double lambda1 = sl[0]; michael@0: // Equatorial pos1 = eclipticToEquatorial(lambda1, 0); michael@0: // michael@0: // // 2. Add ... to lambda to get position 24 hours later michael@0: // double lambda2 = lambda1 + 0.985647*DEG_RAD; michael@0: // Equatorial pos2 = eclipticToEquatorial(lambda2, 0); michael@0: // michael@0: // // 3. Calculate LSTs of rising and setting for these two positions michael@0: // double tanL = ::tan(fLatitude); michael@0: // double H = ::acos(-tanL * ::tan(pos1.declination)); michael@0: // double lst1r = (CalendarAstronomer_PI2 + pos1.ascension - H) * 24 / CalendarAstronomer_PI2; michael@0: // double lst1s = (pos1.ascension + H) * 24 / CalendarAstronomer_PI2; michael@0: // H = ::acos(-tanL * ::tan(pos2.declination)); michael@0: // double lst2r = (CalendarAstronomer_PI2-H + pos2.ascension ) * 24 / CalendarAstronomer_PI2; michael@0: // double lst2s = (H + pos2.ascension ) * 24 / CalendarAstronomer_PI2; michael@0: // if (lst1r > 24) lst1r -= 24; michael@0: // if (lst1s > 24) lst1s -= 24; michael@0: // if (lst2r > 24) lst2r -= 24; michael@0: // if (lst2s > 24) lst2s -= 24; michael@0: // michael@0: // // 4. Convert LSTs to GSTs. If GST1 > GST2, add 24 to GST2. michael@0: // double gst1r = lstToGst(lst1r); michael@0: // double gst1s = lstToGst(lst1s); michael@0: // double gst2r = lstToGst(lst2r); michael@0: // double gst2s = lstToGst(lst2s); michael@0: // if (gst1r > gst2r) gst2r += 24; michael@0: // if (gst1s > gst2s) gst2s += 24; michael@0: // michael@0: // // 5. Calculate GST at 0h UT of this date michael@0: // double t00 = utToGst(0); michael@0: // michael@0: // // 6. Calculate GST at 0h on the observer's longitude michael@0: // double offset = ::round(fLongitude*12/PI); // p.95 step 6; he _rounds_ to nearest 15 deg. michael@0: // double t00p = t00 - offset*1.002737909; michael@0: // if (t00p < 0) t00p += 24; // do NOT normalize michael@0: // michael@0: // // 7. Adjust michael@0: // if (gst1r < t00p) { michael@0: // gst1r += 24; michael@0: // gst2r += 24; michael@0: // } michael@0: // if (gst1s < t00p) { michael@0: // gst1s += 24; michael@0: // gst2s += 24; michael@0: // } michael@0: // michael@0: // // 8. michael@0: // double gstr = (24.07*gst1r-t00*(gst2r-gst1r))/(24.07+gst1r-gst2r); michael@0: // double gsts = (24.07*gst1s-t00*(gst2s-gst1s))/(24.07+gst1s-gst2s); michael@0: // michael@0: // // 9. Correct for parallax, refraction, and sun's diameter michael@0: // double dec = (pos1.declination + pos2.declination) / 2; michael@0: // double psi = ::acos(sin(fLatitude) / cos(dec)); michael@0: // double x = 0.830725 * DEG_RAD; // parallax+refraction+diameter michael@0: // double y = ::asin(sin(x) / ::sin(psi)) * RAD_DEG; michael@0: // double delta_t = 240 * y / cos(dec) / 3600; // hours michael@0: // michael@0: // // 10. Add correction to GSTs, subtract from GSTr michael@0: // gstr -= delta_t; michael@0: // gsts += delta_t; michael@0: // michael@0: // // 11. Convert GST to UT and then to local civil time michael@0: // double ut = gstToUt(rise ? gstr : gsts); michael@0: // //System.out.println((rise?"rise=":"set=") + ut + ", delta_t=" + delta_t); michael@0: // long midnight = DAY_MS * (time / DAY_MS); // Find UT midnight on this day michael@0: // return midnight + (long) (ut * 3600000); michael@0: // } michael@0: michael@0: // Commented out - currently unused. ICU 2.6, Alan michael@0: // /** michael@0: // * Convert local sidereal time to Greenwich sidereal time. michael@0: // * Section 15. Duffett-Smith p.21 michael@0: // * @param lst in hours (0..24) michael@0: // * @return GST in hours (0..24) michael@0: // */ michael@0: // double lstToGst(double lst) { michael@0: // double delta = fLongitude * 24 / CalendarAstronomer_PI2; michael@0: // return normalize(lst - delta, 24); michael@0: // } michael@0: michael@0: // Commented out - currently unused. ICU 2.6, Alan michael@0: // /** michael@0: // * Convert UT to GST on this date. michael@0: // * Section 12. Duffett-Smith p.17 michael@0: // * @param ut in hours michael@0: // * @return GST in hours michael@0: // */ michael@0: // double utToGst(double ut) { michael@0: // return normalize(getT0() + ut*1.002737909, 24); michael@0: // } michael@0: michael@0: // Commented out - currently unused. ICU 2.6, Alan michael@0: // /** michael@0: // * Convert GST to UT on this date. michael@0: // * Section 13. Duffett-Smith p.18 michael@0: // * @param gst in hours michael@0: // * @return UT in hours michael@0: // */ michael@0: // double gstToUt(double gst) { michael@0: // return normalize(gst - getT0(), 24) * 0.9972695663; michael@0: // } michael@0: michael@0: // Commented out - currently unused. ICU 2.6, Alan michael@0: // double getT0() { michael@0: // // Common computation for UT <=> GST michael@0: // michael@0: // // Find JD for 0h UT michael@0: // double jd = uprv_floor(getJulianDay() - 0.5) + 0.5; michael@0: // michael@0: // double s = jd - 2451545.0; michael@0: // double t = s / 36525.0; michael@0: // double t0 = 6.697374558 + (2400.051336 + 0.000025862*t)*t; michael@0: // return t0; michael@0: // } michael@0: michael@0: // Commented out - currently unused. ICU 2.6, Alan michael@0: // //------------------------------------------------------------------------- michael@0: // // Alternate Sun Rise/Set michael@0: // // See sci.astro FAQ michael@0: // // http://www.faqs.org/faqs/astronomy/faq/part3/section-5.html michael@0: // //------------------------------------------------------------------------- michael@0: // michael@0: // // Note: This method appears to produce inferior accuracy as michael@0: // // compared to getSunRiseSet(). michael@0: // michael@0: // /** michael@0: // * TODO Make this when the entire class is package-private. michael@0: // */ michael@0: // /*public*/ long getSunRiseSet3(boolean rise) { michael@0: // michael@0: // // Compute day number for 0.0 Jan 2000 epoch michael@0: // double d = (double)(time - EPOCH_2000_MS) / DAY_MS; michael@0: // michael@0: // // Now compute the Local Sidereal Time, LST: michael@0: // // michael@0: // double LST = 98.9818 + 0.985647352 * d + /*UT*15 + long*/ michael@0: // fLongitude*RAD_DEG; michael@0: // // michael@0: // // (east long. positive). Note that LST is here expressed in degrees, michael@0: // // where 15 degrees corresponds to one hour. Since LST really is an angle, michael@0: // // it's convenient to use one unit---degrees---throughout. michael@0: // michael@0: // // COMPUTING THE SUN'S POSITION michael@0: // // ---------------------------- michael@0: // // michael@0: // // To be able to compute the Sun's rise/set times, you need to be able to michael@0: // // compute the Sun's position at any time. First compute the "day michael@0: // // number" d as outlined above, for the desired moment. Next compute: michael@0: // // michael@0: // double oblecl = 23.4393 - 3.563E-7 * d; michael@0: // // michael@0: // double w = 282.9404 + 4.70935E-5 * d; michael@0: // double M = 356.0470 + 0.9856002585 * d; michael@0: // double e = 0.016709 - 1.151E-9 * d; michael@0: // // michael@0: // // This is the obliquity of the ecliptic, plus some of the elements of michael@0: // // the Sun's apparent orbit (i.e., really the Earth's orbit): w = michael@0: // // argument of perihelion, M = mean anomaly, e = eccentricity. michael@0: // // Semi-major axis is here assumed to be exactly 1.0 (while not strictly michael@0: // // true, this is still an accurate approximation). Next compute E, the michael@0: // // eccentric anomaly: michael@0: // // michael@0: // double E = M + e*(180/PI) * ::sin(M*DEG_RAD) * ( 1.0 + e*cos(M*DEG_RAD) ); michael@0: // // michael@0: // // where E and M are in degrees. This is it---no further iterations are michael@0: // // needed because we know e has a sufficiently small value. Next compute michael@0: // // the true anomaly, v, and the distance, r: michael@0: // // michael@0: // /* r * cos(v) = */ double A = cos(E*DEG_RAD) - e; michael@0: // /* r * ::sin(v) = */ double B = ::sqrt(1 - e*e) * ::sin(E*DEG_RAD); michael@0: // // michael@0: // // and michael@0: // // michael@0: // // r = sqrt( A*A + B*B ) michael@0: // double v = ::atan2( B, A )*RAD_DEG; michael@0: // // michael@0: // // The Sun's true longitude, slon, can now be computed: michael@0: // // michael@0: // double slon = v + w; michael@0: // // michael@0: // // Since the Sun is always at the ecliptic (or at least very very close to michael@0: // // it), we can use simplified formulae to convert slon (the Sun's ecliptic michael@0: // // longitude) to sRA and sDec (the Sun's RA and Dec): michael@0: // // michael@0: // // ::sin(slon) * cos(oblecl) michael@0: // // tan(sRA) = ------------------------- michael@0: // // cos(slon) michael@0: // // michael@0: // // ::sin(sDec) = ::sin(oblecl) * ::sin(slon) michael@0: // // michael@0: // // As was the case when computing az, the Azimuth, if possible use an michael@0: // // atan2() function to compute sRA. michael@0: // michael@0: // double sRA = ::atan2(sin(slon*DEG_RAD) * cos(oblecl*DEG_RAD), cos(slon*DEG_RAD))*RAD_DEG; michael@0: // michael@0: // double sin_sDec = ::sin(oblecl*DEG_RAD) * ::sin(slon*DEG_RAD); michael@0: // double sDec = ::asin(sin_sDec)*RAD_DEG; michael@0: // michael@0: // // COMPUTING RISE AND SET TIMES michael@0: // // ---------------------------- michael@0: // // michael@0: // // To compute when an object rises or sets, you must compute when it michael@0: // // passes the meridian and the HA of rise/set. Then the rise time is michael@0: // // the meridian time minus HA for rise/set, and the set time is the michael@0: // // meridian time plus the HA for rise/set. michael@0: // // michael@0: // // To find the meridian time, compute the Local Sidereal Time at 0h local michael@0: // // time (or 0h UT if you prefer to work in UT) as outlined above---name michael@0: // // that quantity LST0. The Meridian Time, MT, will now be: michael@0: // // michael@0: // // MT = RA - LST0 michael@0: // double MT = normalize(sRA - LST, 360); michael@0: // // michael@0: // // where "RA" is the object's Right Ascension (in degrees!). If negative, michael@0: // // add 360 deg to MT. If the object is the Sun, leave the time as it is, michael@0: // // but if it's stellar, multiply MT by 365.2422/366.2422, to convert from michael@0: // // sidereal to solar time. Now, compute HA for rise/set, name that michael@0: // // quantity HA0: michael@0: // // michael@0: // // ::sin(h0) - ::sin(lat) * ::sin(Dec) michael@0: // // cos(HA0) = --------------------------------- michael@0: // // cos(lat) * cos(Dec) michael@0: // // michael@0: // // where h0 is the altitude selected to represent rise/set. For a purely michael@0: // // mathematical horizon, set h0 = 0 and simplify to: michael@0: // // michael@0: // // cos(HA0) = - tan(lat) * tan(Dec) michael@0: // // michael@0: // // If you want to account for refraction on the atmosphere, set h0 = -35/60 michael@0: // // degrees (-35 arc minutes), and if you want to compute the rise/set times michael@0: // // for the Sun's upper limb, set h0 = -50/60 (-50 arc minutes). michael@0: // // michael@0: // double h0 = -50/60 * DEG_RAD; michael@0: // michael@0: // double HA0 = ::acos( michael@0: // (sin(h0) - ::sin(fLatitude) * sin_sDec) / michael@0: // (cos(fLatitude) * cos(sDec*DEG_RAD)))*RAD_DEG; michael@0: // michael@0: // // When HA0 has been computed, leave it as it is for the Sun but multiply michael@0: // // by 365.2422/366.2422 for stellar objects, to convert from sidereal to michael@0: // // solar time. Finally compute: michael@0: // // michael@0: // // Rise time = MT - HA0 michael@0: // // Set time = MT + HA0 michael@0: // // michael@0: // // convert the times from degrees to hours by dividing by 15. michael@0: // // michael@0: // // If you'd like to check that your calculations are accurate or just michael@0: // // need a quick result, check the USNO's Sun or Moon Rise/Set Table, michael@0: // // . michael@0: // michael@0: // double result = MT + (rise ? -HA0 : HA0); // in degrees michael@0: // michael@0: // // Find UT midnight on this day michael@0: // long midnight = DAY_MS * (time / DAY_MS); michael@0: // michael@0: // return midnight + (long) (result * 3600000 / 15); michael@0: // } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // The Moon michael@0: //------------------------------------------------------------------------- michael@0: michael@0: #define moonL0 (318.351648 * CalendarAstronomer::PI/180 ) // Mean long. at epoch michael@0: #define moonP0 ( 36.340410 * CalendarAstronomer::PI/180 ) // Mean long. of perigee michael@0: #define moonN0 ( 318.510107 * CalendarAstronomer::PI/180 ) // Mean long. of node michael@0: #define moonI ( 5.145366 * CalendarAstronomer::PI/180 ) // Inclination of orbit michael@0: #define moonE ( 0.054900 ) // Eccentricity of orbit michael@0: michael@0: // These aren't used right now michael@0: #define moonA ( 3.84401e5 ) // semi-major axis (km) michael@0: #define moonT0 ( 0.5181 * CalendarAstronomer::PI/180 ) // Angular size at distance A michael@0: #define moonPi ( 0.9507 * CalendarAstronomer::PI/180 ) // Parallax at distance A michael@0: michael@0: /** michael@0: * The position of the moon at the time set on this michael@0: * object, in equatorial coordinates. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: const CalendarAstronomer::Equatorial& CalendarAstronomer::getMoonPosition() michael@0: { michael@0: // michael@0: // See page 142 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: // michael@0: if (moonPositionSet == FALSE) { michael@0: // Calculate the solar longitude. Has the side effect of michael@0: // filling in "meanAnomalySun" as well. michael@0: getSunLongitude(); michael@0: michael@0: // michael@0: // Find the # of days since the epoch of our orbital parameters. michael@0: // TODO: Convert the time of day portion into ephemeris time michael@0: // michael@0: double day = getJulianDay() - JD_EPOCH; // Days since epoch michael@0: michael@0: // Calculate the mean longitude and anomaly of the moon, based on michael@0: // a circular orbit. Similar to the corresponding solar calculation. michael@0: double meanLongitude = norm2PI(13.1763966*PI/180*day + moonL0); michael@0: meanAnomalyMoon = norm2PI(meanLongitude - 0.1114041*PI/180 * day - moonP0); michael@0: michael@0: // michael@0: // Calculate the following corrections: michael@0: // Evection: the sun's gravity affects the moon's eccentricity michael@0: // Annual Eqn: variation in the effect due to earth-sun distance michael@0: // A3: correction factor (for ???) michael@0: // michael@0: double evection = 1.2739*PI/180 * ::sin(2 * (meanLongitude - sunLongitude) michael@0: - meanAnomalyMoon); michael@0: double annual = 0.1858*PI/180 * ::sin(meanAnomalySun); michael@0: double a3 = 0.3700*PI/180 * ::sin(meanAnomalySun); michael@0: michael@0: meanAnomalyMoon += evection - annual - a3; michael@0: michael@0: // michael@0: // More correction factors: michael@0: // center equation of the center correction michael@0: // a4 yet another error correction (???) michael@0: // michael@0: // TODO: Skip the equation of the center correction and solve Kepler's eqn? michael@0: // michael@0: double center = 6.2886*PI/180 * ::sin(meanAnomalyMoon); michael@0: double a4 = 0.2140*PI/180 * ::sin(2 * meanAnomalyMoon); michael@0: michael@0: // Now find the moon's corrected longitude michael@0: moonLongitude = meanLongitude + evection + center - annual + a4; michael@0: michael@0: // michael@0: // And finally, find the variation, caused by the fact that the sun's michael@0: // gravitational pull on the moon varies depending on which side of michael@0: // the earth the moon is on michael@0: // michael@0: double variation = 0.6583*CalendarAstronomer::PI/180 * ::sin(2*(moonLongitude - sunLongitude)); michael@0: michael@0: moonLongitude += variation; michael@0: michael@0: // michael@0: // What we've calculated so far is the moon's longitude in the plane michael@0: // of its own orbit. Now map to the ecliptic to get the latitude michael@0: // and longitude. First we need to find the longitude of the ascending michael@0: // node, the position on the ecliptic where it is crossed by the moon's michael@0: // orbit as it crosses from the southern to the northern hemisphere. michael@0: // michael@0: double nodeLongitude = norm2PI(moonN0 - 0.0529539*PI/180 * day); michael@0: michael@0: nodeLongitude -= 0.16*PI/180 * ::sin(meanAnomalySun); michael@0: michael@0: double y = ::sin(moonLongitude - nodeLongitude); michael@0: double x = cos(moonLongitude - nodeLongitude); michael@0: michael@0: moonEclipLong = ::atan2(y*cos(moonI), x) + nodeLongitude; michael@0: double moonEclipLat = ::asin(y * ::sin(moonI)); michael@0: michael@0: eclipticToEquatorial(moonPosition, moonEclipLong, moonEclipLat); michael@0: moonPositionSet = TRUE; michael@0: } michael@0: return moonPosition; michael@0: } michael@0: michael@0: /** michael@0: * The "age" of the moon at the time specified in this object. michael@0: * This is really the angle between the michael@0: * current ecliptic longitudes of the sun and the moon, michael@0: * measured in radians. michael@0: * michael@0: * @see #getMoonPhase michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getMoonAge() { michael@0: // See page 147 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: // michael@0: // Force the moon's position to be calculated. We're going to use michael@0: // some the intermediate results cached during that calculation. michael@0: // michael@0: getMoonPosition(); michael@0: michael@0: return norm2PI(moonEclipLong - sunLongitude); michael@0: } michael@0: michael@0: /** michael@0: * Calculate the phase of the moon at the time set in this object. michael@0: * The returned phase is a double in the range michael@0: * 0 <= phase < 1, interpreted as follows: michael@0: *

    michael@0: *
  • 0.00: New moon michael@0: *
  • 0.25: First quarter michael@0: *
  • 0.50: Full moon michael@0: *
  • 0.75: Last quarter michael@0: *
michael@0: * michael@0: * @see #getMoonAge michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: double CalendarAstronomer::getMoonPhase() { michael@0: // See page 147 of "Practial Astronomy with your Calculator", michael@0: // by Peter Duffet-Smith, for details on the algorithm. michael@0: return 0.5 * (1 - cos(getMoonAge())); michael@0: } michael@0: michael@0: /** michael@0: * Constant representing a new moon. michael@0: * For use with {@link #getMoonTime getMoonTime} michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: const CalendarAstronomer::MoonAge CalendarAstronomer::NEW_MOON() { michael@0: return CalendarAstronomer::MoonAge(0); michael@0: } michael@0: michael@0: /** michael@0: * Constant representing the moon's first quarter. michael@0: * For use with {@link #getMoonTime getMoonTime} michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: /*const CalendarAstronomer::MoonAge CalendarAstronomer::FIRST_QUARTER() { michael@0: return CalendarAstronomer::MoonAge(CalendarAstronomer::PI/2); michael@0: }*/ michael@0: michael@0: /** michael@0: * Constant representing a full moon. michael@0: * For use with {@link #getMoonTime getMoonTime} michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: const CalendarAstronomer::MoonAge CalendarAstronomer::FULL_MOON() { michael@0: return CalendarAstronomer::MoonAge(CalendarAstronomer::PI); michael@0: } michael@0: /** michael@0: * Constant representing the moon's last quarter. michael@0: * For use with {@link #getMoonTime getMoonTime} michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: michael@0: class MoonTimeAngleFunc : public CalendarAstronomer::AngleFunc { michael@0: public: michael@0: virtual ~MoonTimeAngleFunc(); michael@0: virtual double eval(CalendarAstronomer&a) { return a.getMoonAge(); } michael@0: }; michael@0: michael@0: MoonTimeAngleFunc::~MoonTimeAngleFunc() {} michael@0: michael@0: /*const CalendarAstronomer::MoonAge CalendarAstronomer::LAST_QUARTER() { michael@0: return CalendarAstronomer::MoonAge((CalendarAstronomer::PI*3)/2); michael@0: }*/ michael@0: michael@0: /** michael@0: * Find the next or previous time at which the Moon's ecliptic michael@0: * longitude will have the desired value. michael@0: *

michael@0: * @param desired The desired longitude. michael@0: * @param next true if the next occurrance of the phase michael@0: * is desired, false for the previous occurrance. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: UDate CalendarAstronomer::getMoonTime(double desired, UBool next) michael@0: { michael@0: MoonTimeAngleFunc func; michael@0: return timeOfAngle( func, michael@0: desired, michael@0: SYNODIC_MONTH, michael@0: MINUTE_MS, michael@0: next); michael@0: } michael@0: michael@0: /** michael@0: * Find the next or previous time at which the moon will be in the michael@0: * desired phase. michael@0: *

michael@0: * @param desired The desired phase of the moon. michael@0: * @param next true if the next occurrance of the phase michael@0: * is desired, false for the previous occurrance. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: UDate CalendarAstronomer::getMoonTime(const CalendarAstronomer::MoonAge& desired, UBool next) { michael@0: return getMoonTime(desired.value, next); michael@0: } michael@0: michael@0: class MoonRiseSetCoordFunc : public CalendarAstronomer::CoordFunc { michael@0: public: michael@0: virtual ~MoonRiseSetCoordFunc(); michael@0: virtual void eval(CalendarAstronomer::Equatorial& result, CalendarAstronomer&a) { result = a.getMoonPosition(); } michael@0: }; michael@0: michael@0: MoonRiseSetCoordFunc::~MoonRiseSetCoordFunc() {} michael@0: michael@0: /** michael@0: * Returns the time (GMT) of sunrise or sunset on the local date to which michael@0: * this calendar is currently set. michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: UDate CalendarAstronomer::getMoonRiseSet(UBool rise) michael@0: { michael@0: MoonRiseSetCoordFunc func; michael@0: return riseOrSet(func, michael@0: rise, michael@0: .533 * DEG_RAD, // Angular Diameter michael@0: 34 /60.0 * DEG_RAD, // Refraction correction michael@0: MINUTE_MS); // Desired accuracy michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Interpolation methods for finding the time at which a given event occurs michael@0: //------------------------------------------------------------------------- michael@0: michael@0: UDate CalendarAstronomer::timeOfAngle(AngleFunc& func, double desired, michael@0: double periodDays, double epsilon, UBool next) michael@0: { michael@0: // Find the value of the function at the current time michael@0: double lastAngle = func.eval(*this); michael@0: michael@0: // Find out how far we are from the desired angle michael@0: double deltaAngle = norm2PI(desired - lastAngle) ; michael@0: michael@0: // Using the average period, estimate the next (or previous) time at michael@0: // which the desired angle occurs. michael@0: double deltaT = (deltaAngle + (next ? 0.0 : - CalendarAstronomer_PI2 )) * (periodDays*DAY_MS) / CalendarAstronomer_PI2; michael@0: michael@0: double lastDeltaT = deltaT; // Liu michael@0: UDate startTime = fTime; // Liu michael@0: michael@0: setTime(fTime + uprv_ceil(deltaT)); michael@0: michael@0: // Now iterate until we get the error below epsilon. Throughout michael@0: // this loop we use normPI to get values in the range -Pi to Pi, michael@0: // since we're using them as correction factors rather than absolute angles. michael@0: do { michael@0: // Evaluate the function at the time we've estimated michael@0: double angle = func.eval(*this); michael@0: michael@0: // Find the # of milliseconds per radian at this point on the curve michael@0: double factor = uprv_fabs(deltaT / normPI(angle-lastAngle)); michael@0: michael@0: // Correct the time estimate based on how far off the angle is michael@0: deltaT = normPI(desired - angle) * factor; michael@0: michael@0: // HACK: michael@0: // michael@0: // If abs(deltaT) begins to diverge we need to quit this loop. michael@0: // This only appears to happen when attempting to locate, for michael@0: // example, a new moon on the day of the new moon. E.g.: michael@0: // michael@0: // This result is correct: michael@0: // newMoon(7508(Mon Jul 23 00:00:00 CST 1990,false))= michael@0: // Sun Jul 22 10:57:41 CST 1990 michael@0: // michael@0: // But attempting to make the same call a day earlier causes deltaT michael@0: // to diverge: michael@0: // CalendarAstronomer.timeOfAngle() diverging: 1.348508727575625E9 -> michael@0: // 1.3649828540224032E9 michael@0: // newMoon(7507(Sun Jul 22 00:00:00 CST 1990,false))= michael@0: // Sun Jul 08 13:56:15 CST 1990 michael@0: // michael@0: // As a temporary solution, we catch this specific condition and michael@0: // adjust our start time by one eighth period days (either forward michael@0: // or backward) and try again. michael@0: // Liu 11/9/00 michael@0: if (uprv_fabs(deltaT) > uprv_fabs(lastDeltaT)) { michael@0: double delta = uprv_ceil (periodDays * DAY_MS / 8.0); michael@0: setTime(startTime + (next ? delta : -delta)); michael@0: return timeOfAngle(func, desired, periodDays, epsilon, next); michael@0: } michael@0: michael@0: lastDeltaT = deltaT; michael@0: lastAngle = angle; michael@0: michael@0: setTime(fTime + uprv_ceil(deltaT)); michael@0: } michael@0: while (uprv_fabs(deltaT) > epsilon); michael@0: michael@0: return fTime; michael@0: } michael@0: michael@0: UDate CalendarAstronomer::riseOrSet(CoordFunc& func, UBool rise, michael@0: double diameter, double refraction, michael@0: double epsilon) michael@0: { michael@0: Equatorial pos; michael@0: double tanL = ::tan(fLatitude); michael@0: double deltaT = 0; michael@0: int32_t count = 0; michael@0: michael@0: // michael@0: // Calculate the object's position at the current time, then use that michael@0: // position to calculate the time of rising or setting. The position michael@0: // will be different at that time, so iterate until the error is allowable. michael@0: // michael@0: U_DEBUG_ASTRO_MSG(("setup rise=%s, dia=%.3lf, ref=%.3lf, eps=%.3lf\n", michael@0: rise?"T":"F", diameter, refraction, epsilon)); michael@0: do { michael@0: // See "Practical Astronomy With Your Calculator, section 33. michael@0: func.eval(pos, *this); michael@0: double angle = ::acos(-tanL * ::tan(pos.declination)); michael@0: double lst = ((rise ? CalendarAstronomer_PI2-angle : angle) + pos.ascension ) * 24 / CalendarAstronomer_PI2; michael@0: michael@0: // Convert from LST to Universal Time. michael@0: UDate newTime = lstToUT( lst ); michael@0: michael@0: deltaT = newTime - fTime; michael@0: setTime(newTime); michael@0: U_DEBUG_ASTRO_MSG(("%d] dT=%.3lf, angle=%.3lf, lst=%.3lf, A=%.3lf/D=%.3lf\n", michael@0: count, deltaT, angle, lst, pos.ascension, pos.declination)); michael@0: } michael@0: while (++ count < 5 && uprv_fabs(deltaT) > epsilon); michael@0: michael@0: // Calculate the correction due to refraction and the object's angular diameter michael@0: double cosD = ::cos(pos.declination); michael@0: double psi = ::acos(sin(fLatitude) / cosD); michael@0: double x = diameter / 2 + refraction; michael@0: double y = ::asin(sin(x) / ::sin(psi)); michael@0: long delta = (long)((240 * y * RAD_DEG / cosD)*SECOND_MS); michael@0: michael@0: return fTime + (rise ? -delta : delta); michael@0: } michael@0: /** michael@0: * Return the obliquity of the ecliptic (the angle between the ecliptic michael@0: * and the earth's equator) at the current time. This varies due to michael@0: * the precession of the earth's axis. michael@0: * michael@0: * @return the obliquity of the ecliptic relative to the equator, michael@0: * measured in radians. michael@0: */ michael@0: double CalendarAstronomer::eclipticObliquity() { michael@0: if (isINVALID(eclipObliquity)) { michael@0: const double epoch = 2451545.0; // 2000 AD, January 1.5 michael@0: michael@0: double T = (getJulianDay() - epoch) / 36525; michael@0: michael@0: eclipObliquity = 23.439292 michael@0: - 46.815/3600 * T michael@0: - 0.0006/3600 * T*T michael@0: + 0.00181/3600 * T*T*T; michael@0: michael@0: eclipObliquity *= DEG_RAD; michael@0: } michael@0: return eclipObliquity; michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Private data michael@0: //------------------------------------------------------------------------- michael@0: void CalendarAstronomer::clearCache() { michael@0: const double INVALID = uprv_getNaN(); michael@0: michael@0: julianDay = INVALID; michael@0: julianCentury = INVALID; michael@0: sunLongitude = INVALID; michael@0: meanAnomalySun = INVALID; michael@0: moonLongitude = INVALID; michael@0: moonEclipLong = INVALID; michael@0: meanAnomalyMoon = INVALID; michael@0: eclipObliquity = INVALID; michael@0: siderealTime = INVALID; michael@0: siderealT0 = INVALID; michael@0: moonPositionSet = FALSE; michael@0: } michael@0: michael@0: //private static void out(String s) { michael@0: // System.out.println(s); michael@0: //} michael@0: michael@0: //private static String deg(double rad) { michael@0: // return Double.toString(rad * RAD_DEG); michael@0: //} michael@0: michael@0: //private static String hours(long ms) { michael@0: // return Double.toString((double)ms / HOUR_MS) + " hours"; michael@0: //} michael@0: michael@0: /** michael@0: * @internal michael@0: * @deprecated ICU 2.4. This class may be removed or modified. michael@0: */ michael@0: /*UDate CalendarAstronomer::local(UDate localMillis) { michael@0: // TODO - srl ? michael@0: TimeZone *tz = TimeZone::createDefault(); michael@0: int32_t rawOffset; michael@0: int32_t dstOffset; michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: tz->getOffset(localMillis, TRUE, rawOffset, dstOffset, status); michael@0: delete tz; michael@0: return localMillis - rawOffset; michael@0: }*/ michael@0: michael@0: // Debugging functions michael@0: UnicodeString CalendarAstronomer::Ecliptic::toString() const michael@0: { michael@0: #ifdef U_DEBUG_ASTRO michael@0: char tmp[800]; michael@0: sprintf(tmp, "[%.5f,%.5f]", longitude*RAD_DEG, latitude*RAD_DEG); michael@0: return UnicodeString(tmp, ""); michael@0: #else michael@0: return UnicodeString(); michael@0: #endif michael@0: } michael@0: michael@0: UnicodeString CalendarAstronomer::Equatorial::toString() const michael@0: { michael@0: #ifdef U_DEBUG_ASTRO michael@0: char tmp[400]; michael@0: sprintf(tmp, "%f,%f", michael@0: (ascension*RAD_DEG), (declination*RAD_DEG)); michael@0: return UnicodeString(tmp, ""); michael@0: #else michael@0: return UnicodeString(); michael@0: #endif michael@0: } michael@0: michael@0: UnicodeString CalendarAstronomer::Horizon::toString() const michael@0: { michael@0: #ifdef U_DEBUG_ASTRO michael@0: char tmp[800]; michael@0: sprintf(tmp, "[%.5f,%.5f]", altitude*RAD_DEG, azimuth*RAD_DEG); michael@0: return UnicodeString(tmp, ""); michael@0: #else michael@0: return UnicodeString(); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: // static private String radToHms(double angle) { michael@0: // int hrs = (int) (angle*RAD_HOUR); michael@0: // int min = (int)((angle*RAD_HOUR - hrs) * 60); michael@0: // int sec = (int)((angle*RAD_HOUR - hrs - min/60.0) * 3600); michael@0: michael@0: // return Integer.toString(hrs) + "h" + min + "m" + sec + "s"; michael@0: // } michael@0: michael@0: // static private String radToDms(double angle) { michael@0: // int deg = (int) (angle*RAD_DEG); michael@0: // int min = (int)((angle*RAD_DEG - deg) * 60); michael@0: // int sec = (int)((angle*RAD_DEG - deg - min/60.0) * 3600); michael@0: michael@0: // return Integer.toString(deg) + "\u00b0" + min + "'" + sec + "\""; michael@0: // } michael@0: michael@0: // =============== Calendar Cache ================ michael@0: michael@0: void CalendarCache::createCache(CalendarCache** cache, UErrorCode& status) { michael@0: ucln_i18n_registerCleanup(UCLN_I18N_ASTRO_CALENDAR, calendar_astro_cleanup); michael@0: if(cache == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: *cache = new CalendarCache(32, status); michael@0: if(U_FAILURE(status)) { michael@0: delete *cache; michael@0: *cache = NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: int32_t CalendarCache::get(CalendarCache** cache, int32_t key, UErrorCode &status) { michael@0: int32_t res; michael@0: michael@0: if(U_FAILURE(status)) { michael@0: return 0; michael@0: } michael@0: umtx_lock(&ccLock); michael@0: michael@0: if(*cache == NULL) { michael@0: createCache(cache, status); michael@0: if(U_FAILURE(status)) { michael@0: umtx_unlock(&ccLock); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: res = uhash_igeti((*cache)->fTable, key); michael@0: U_DEBUG_ASTRO_MSG(("%p: GET: [%d] == %d\n", (*cache)->fTable, key, res)); michael@0: michael@0: umtx_unlock(&ccLock); michael@0: return res; michael@0: } michael@0: michael@0: void CalendarCache::put(CalendarCache** cache, int32_t key, int32_t value, UErrorCode &status) { michael@0: if(U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: umtx_lock(&ccLock); michael@0: michael@0: if(*cache == NULL) { michael@0: createCache(cache, status); michael@0: if(U_FAILURE(status)) { michael@0: umtx_unlock(&ccLock); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: uhash_iputi((*cache)->fTable, key, value, &status); michael@0: U_DEBUG_ASTRO_MSG(("%p: PUT: [%d] := %d\n", (*cache)->fTable, key, value)); michael@0: michael@0: umtx_unlock(&ccLock); michael@0: } michael@0: michael@0: CalendarCache::CalendarCache(int32_t size, UErrorCode &status) { michael@0: fTable = uhash_openSize(uhash_hashLong, uhash_compareLong, NULL, size, &status); michael@0: U_DEBUG_ASTRO_MSG(("%p: Opening.\n", fTable)); michael@0: } michael@0: michael@0: CalendarCache::~CalendarCache() { michael@0: if(fTable != NULL) { michael@0: U_DEBUG_ASTRO_MSG(("%p: Closing.\n", fTable)); michael@0: uhash_close(fTable); michael@0: } michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // !UCONFIG_NO_FORMATTING