试图计算日出......没有得到正确的答案

时间:2015-10-21 15:43:54

标签: java

这是我目前的代码:

Samperio (59.) Brosinski (68.) 0:2 beendet FSV Mainz 05Borussia Dortmund Coface Arena, 34.000 Zuschauer Schiedsrichter: Tobias Stieler aus Hamburg Reus (18., Aubameyang) Mkhitaryan (82., Aubameyang) Mkhitaryan (10.)

输入如下:(50,10,294,1,0)。最后2个可以忽略。

现在我基于以下页面: http://williams.best.vwh.net/sunrise_sunset_algorithm.htm

代码应该根据网站完成,但我没有接近所谓的结果。今天我应该得到7.5,但我得到的是9.358。

现在,那可能是因为有辐射/度的东西?我无法理解这一点,因为我一直试图将这些转换器(Pi / 180)插入到代码中,而没有任何可用的结果。

有谁能告诉我把它们放在哪里或指向正确的方向?我已经花了太多时间在这上面,现在我已经非常接近了。

2 个答案:

答案 0 :(得分:3)

我会在这里发布我的实施,以防人们需要它(从与你相同的来源移植)

https://gist.github.com/zhong-j-yu/2232343b14a5b5ef5b9d

    public class SunRiseSetAlgo
    {
        static double calcSunrise(int dayOfYear, double localOffset, double latitude, double longitude)
        {
            return calc(dayOfYear, localOffset, latitude, longitude, true);
        }
        static double calcSunset(int dayOfYear, double localOffset, double latitude, double longitude)
        {
            return calc(dayOfYear, localOffset, latitude, longitude, false);
        }

        // http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
        static double calc(int dayOfYear, double localOffset, double latitude, double longitude, boolean rise)
        {
            //1. first calculate the day of the year

    //        int N1 = floor(275 * month / 9.0);
    //        int N2 = floor((month + 9) / 12.0);
    //        int N3 = (1 + floor((year - 4 * floor(year / 4.0) + 2) / 3.0));
    //        int N = N1 - (N2 * N3) + day - 30;
            int N = dayOfYear;

            //2. convert the longitude to hour value and calculate an approximate time

            double lngHour = longitude / 15;
            double t = rise?
                N + (( 6 - lngHour) / 24) :
                N + ((18 - lngHour) / 24);

            //3. calculate the Sun's mean anomaly

            double M = (0.9856 * t) - 3.289;

            //4. calculate the Sun's true longitude

            double L = M + (1.916 * sin(M)) + (0.020 * sin(2 * M)) + 282.634;
            L = mod(L, 360);

            //5a. calculate the Sun's right ascension
            double RA = atan(0.91764 * tan(L));
            RA = mod(RA, 360);

            //5b. right ascension value needs to be in the same quadrant as L
            double Lquadrant  = (floor( L/90)) * 90;
            double RAquadrant = (floor(RA/90)) * 90;
            RA = RA + (Lquadrant - RAquadrant);

            //5c. right ascension value needs to be converted into hours
            RA = RA / 15;

            //6. calculate the Sun's declination
            double sinDec = 0.39782 * sin(L);
            double cosDec = cos(asin(sinDec));

            //7a. calculate the Sun's local hour angle
            double zenith = 90 + 50.0/60;
            double cosH = (cos(zenith) - (sinDec * sin(latitude))) / (cosDec * cos(latitude));

            if (cosH >  1)
              throw new Error("the sun never rises on this location (on the specified date");
            if (cosH < -1)
              throw new Error("the sun never sets on this location (on the specified date");

            //7b. finish calculating H and convert into hours
            double H = rise?
                360 - acos(cosH) :
                acos(cosH);
            H = H / 15;

            //8. calculate local mean time of rising/setting
            double T = H + RA - (0.06571 * t) - 6.622;

            //9. adjust back to UTC
            double UT = T - lngHour;

            //10. convert UT value to local time zone of latitude/longitude
            double localT = UT + localOffset;
            localT = mod(localT, 24);
            return localT;
        }

        static int floor(double d){ return (int)Math.floor(d); }

        static double sin(double degree)
        {
            return Math.sin(degree*Math.PI/180);
        }
        static double cos(double degree)
        {
            return Math.cos(degree*Math.PI/180);
        }
        static double tan(double degree)
        {
            return Math.tan(degree*Math.PI/180);
        }
        static double atan(double x)
        {
            return Math.atan(x) *180/Math.PI;
        }
        static double asin(double x)
        {
            return Math.asin(x) *180/Math.PI;
        }
        static double acos(double x)
        {
            return Math.acos(x) *180/Math.PI;
        }

        static double mod(double x, double lim)
        {
            return x - lim * floor(x/lim);
        }

    }

答案 1 :(得分:0)

Everone似乎链接到此http://williams.best.vwh.net/sunrise_sunset_algorithm.htm 它不再存在。为什么不尝试偶尔更新的内容,例如https://en.wikipedia.org/wiki/Sunrise_equation 然后,如果你愿意,你可以帮助编辑它以使其更好。