/// <summary> /// Calculates sun position for a given date and latitude/longitude. /// </summary> /// <param name="date"></param> /// <param name="lat"></param> /// <param name="lng"></param> /// <returns></returns> public static SunPosition GetSunPosition(DateTimeOffset date, double lat, double lng) { var lw = Constants.Rad * -lng; var phi = Constants.Rad * lat; var d = date.ToDays(); var sunCoords = Sun.GetEquatorialCoords(d); var h = Position.GetSiderealTime(d, lw) - sunCoords.RightAscension; var azimuth = Position.GetAzimuth(h, phi, sunCoords.Declination); var altitude = Position.GetAltitude(h, phi, sunCoords.Declination); return(new SunPosition(azimuth, altitude)); }
/// <summary> /// Calculates illumination parameters of the moon. /// Location is not needed because percentage will be the same for both Northern and Southern hemisphere. /// Based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and /// Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998. /// </summary> /// <param name="date"></param> /// <returns></returns> public static MoonIllumination GetMoonIllumination(DateTime date) { var d = date.ToDays(); const int sdist = 149598000; // distance from Earth to Sun in km var sunCoords = Sun.GetEquatorialCoords(d); var moonCoords = Moon.GetGeocentricCoords(d); var phi = Math.Acos(Math.Sin(sunCoords.Declination) * Math.Sin(moonCoords.Declination) + Math.Cos(sunCoords.Declination) * Math.Cos(moonCoords.Declination) * Math.Cos(sunCoords.RightAscension - moonCoords.RightAscension)); var inc = Math.Atan2(sdist * Math.Sin(phi), moonCoords.Distance - sdist * Math.Cos(phi)); var angle = Math.Atan2( Math.Cos(sunCoords.Declination) * Math.Sin(sunCoords.RightAscension - moonCoords.RightAscension), Math.Sin(sunCoords.Declination) * Math.Cos(moonCoords.Declination) - Math.Cos(sunCoords.Declination) * Math.Sin(moonCoords.Declination) * Math.Cos(sunCoords.RightAscension - moonCoords.RightAscension)); var fraction = (1 + Math.Cos(inc)) / 2; var phase = 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI; return(new MoonIllumination(fraction, phase, angle)); }