Feedback

C# - Astronomie Bibliothek (Sonnenaufgang, Mondphasen)

Veröffentlicht von am 9/1/2009
(4 Bewertungen)
Eine kleine Bibliothek mit deren Hilfe man z.B
den Zeitpunkt des Sonnenaufganges oder des Sonnenunterganges ermitteln kann.
Weiters ist es möglich die Mondphasen eines Datums zu ermitteln.

Der Code wurde aus dem Windows7 sidebar gadget ''weather'' von JavaScript auf C# portiert.
#region Enums.cs

namespace AstronomyLib
{
    /// <summary>
    /// simplified moonphases
    /// </summary>
    public enum MoonPhases
    {
        /// <summary>
        /// Newmoon phase.
        /// </summary>
        newmoon = 0,
        /// <summary>
        /// Waxing crescent moon phase.
        /// </summary>
        waxingcrescent = 1,
        /// <summary>
        /// First quarter phase.
        /// </summary>
        firstquarter = 2,
        /// <summary>
        /// Waxing gibbous moon phase.
        /// </summary>
        waxinggibbous = 3,
        /// <summary>
        /// Fullmoon phase.
        /// </summary>
        fullmoon = 4,
        /// <summary>
        /// Waning gibbous moon phase.
        /// </summary>
        waninggibbous = 5,
        /// <summary>
        /// Last quarter phase.
        /// </summary>
        lastquarter = 6,
        /// <summary>
        /// Waning crescent moon phase.
        /// </summary>
        waningcrescent = 7
    }
}


#endregion

#region Moon.cs

using AstronomyLib;
namespace System.Astronomy
{
    /// <summary>
    /// Represents the simplified moonphases of a given date.
    /// </summary>
    public class Moon
    {
        /// <summary>
        /// The moonphase of the given date.
        /// </summary>
        public MoonPhases Phase;

        /// <summary>
        /// Initializes a new instance of the <see cref="Moon"/> class.
        /// </summary>
        /// <param name="year">The year.</param>
        /// <param name="day">The day.</param>
        /// <param name="hour">The hour.</param>
        public Moon(int year, int month, int day)
        {
            var P2 = 3.14159 * 2;
            var YY = year - (Int32)((12 - month) / 10);
            var MM = month + 9;
            if (MM >= 12) { MM = MM - 12; }
            var K1 = (Int32)(365.25 * (YY + 4712));
            var K2 = (Int32)(30.6 * MM + .5);
            var K3 = (Int32)((Int32)((YY / 100) + 49) * .75) - 38;
            var J = K1 + K2 + day + 59;
            if (J > 2299160) { J = J - K3; }
            var V = (J - 2451550.1) / 29.530588853;
            V = V - (Int32)(V);
            if (V < 0) { V = V + 1; }
            var AG = V * 29.53;
            if ((AG > 27.6849270496875) || (AG <= 1.8456618033125))
            {
                Phase = MoonPhases.newmoon;
            }
            else if ((AG > 1.8456618033125) && (AG <= 5.5369854099375))
            {
                Phase = MoonPhases.waxingcrescent;
            }
            else if ((AG > 5.5369854099375) && (AG <= 9.2283090165625))
            {
                Phase = MoonPhases.firstquarter;
            }
            else if ((AG > 9.2283090165625) && (AG <= 12.9196326231875))
            {
                Phase = MoonPhases.waxinggibbous;
            }
            else if ((AG > 12.9196326231875) && (AG <= 16.6109562298125))
            {
                Phase = MoonPhases.fullmoon;
            }
            else if ((AG > 16.6109562298125) && (AG <= 20.3022798364375))
            {
                Phase = MoonPhases.waninggibbous;
            }
            else if ((AG > 20.3022798364375) && (AG <= 23.9936034430625))
            {
                Phase = MoonPhases.lastquarter;
            }
            else if ((AG > 23.9936034430625) && (AG <= 27.6849270496875))
            {
                Phase = MoonPhases.waningcrescent;
            }
            else
            {
                Phase = MoonPhases.fullmoon;
            }
        }
    }
}


#endregion

#region SunsetSunrise.cs


// Astronomy Namespace
namespace System.Astronomy
{
    /// <summary>
    /// A class that provides sun informations of a given day on a given location
    /// represented by latitude and longtitude.
    /// </summary>
    public class Sun
    {
        #region ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="Sun"/> class.
        /// </summary>
        /// <param name="Latitude">The latitude.</param>
        /// <param name="Longtitude">The longtitude.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        public Sun(double Latitude, double Longtitude, int year, int month, int day)
        {
            var PI = Math.PI;
            var DR = PI / 180;
            var RD = 1 / DR;
            var B5 = Latitude;
            var L5 = Longtitude;
            var H = 0;    // timezone UTC
            var Now = DateTime.Now;
            var M = month;
            var D = day;
            B5 = DR * B5;
            var N = (Int32)(275 * M / 9) - 2 * (Int32)((M + 9) / 12) + D - 30;
            var L0 = 4.8771 + .0172 * (N + .5 - L5 / 360);
            var C = .03342 * Math.Sin(L0 + 1.345);
            var C2 = RD * (Math.Atan(Math.Tan(L0 + C)) - Math.Atan(.9175 * Math.Tan(L0 + C)) - C);
            var SD = .3978 * Math.Sin(L0 + C);
            var CD = Math.Sqrt(1 - SD * SD);
            var SC = (SD * Math.Sin(B5) + .0145) / (Math.Cos(B5) * CD);

            if (Math.Abs(SC) <= 1)
            {
                // calculate sunrise 
                var C3 = RD * Math.Atan(SC / Math.Sqrt(1 - SC * SC));
                var R1 = 6 - H - (L5 + C2 + C3) / 15;
                var HR = (Int32)(R1);
                var MR = (Int32)((R1 - HR) * 60);
                Sunrise = new DateTime(year, month, day, HR, MR, 0);
                // calculate sunset
                var S1 = 18 - H - (L5 + C2 - C3) / 15;
                var HS = (Int32)(S1);
                var MS = (Int32)((S1 - HS) * 60);
                Sunset = new DateTime(year, month, day, HS, MS, 0);
            }
            else
            {
                if (SC > 1)
                {
                    // sun is up all day ...
                    // Set Sunset to be in the future ...
                    Sunset = new DateTime(Now.Year + 1, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second);
                    // Set Sunrise to be in the past ...
                    Sunrise = new DateTime(Now.Year - 1, Now.Month, Now.Day, Now.Hour, Now.Minute - 1, Now.Second);
                }
                if (SC < -1)
                {
                    // sun is down all day ...
                    // Set Sunrise and Sunset to be in the future ...
                    Sunrise = new DateTime(Now.Year + 1, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second);
                    Sunset = new DateTime(Now.Year + 1, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second);
                }
            }
        }

        #endregion
    
        #region fields

        /// <summary>
        /// DateTime representation of the sunrise-timestamp of a given day on a given location.
        /// </summary>
        public DateTime Sunrise;

        /// <summary>
        /// DateTime representation of the sunset-timestamp of a given day on a given location.
        /// </summary>
        public DateTime Sunset;


        #endregion
    }
}


#endregion

2 Kommentare zum Snippet

Rainer Hilmer schrieb am 9/1/2009:
Hoi das ist interessant. Vielen Dank dafür. :-)
receiverbox schrieb am 1/5/2017:
Ganz nett, allerdings scheint wo der Wurm drinnen zu sein. Die Zeitzonen lassen sich nicht anpassen, hier ist H mit 0 auf UTC festgesetzt und wenn ich außerhalb von Europa bin, also zum Beispiel für den 5.1.2017 Sonnenauf- und -untergang von Peking berechne, liefert das Programm Fehler anstatt von Uhrzeiten...
 

Logge dich ein, um hier zu kommentieren!