Esempio n. 1
0
        public static SolarInfo ForDate(double latitude, double longitude, DateTime date)
        {
            var info = new SolarInfo();
            info.Date = date = date.Date;

            var year = date.Year;
            var month = date.Month;
            var day = date.Day;
            if ((latitude >= -90) && (latitude < -89))
            {
                //alert("All latitudes between 89 and 90 S\n will be set to -89");
                //latLongForm["latDeg"].value = -89;
                latitude = -89;
            }

            if ((latitude <= 90) && (latitude > 89))
            {
                //alert("All latitudes between 89 and 90 N\n will be set to 89");
                //latLongForm["latDeg"].value = 89;
                latitude = 89;
            }

            //***** Calculate the time of sunrise
            var JD = calcJD(year, month, day);
            //var dow = calcDayOfWeek(JD);
            var doy = calcDayOfYear(month, day, isLeapYear(year));
            var T = calcTimeJulianCent(JD);

            // var alpha = calcSunRtAscension(T);
            var solarDec = calcSunDeclination(T);
            var eqTime = calcEquationOfTime(T); // (in minutes)

            // Calculate sunrise for this date
            // if no sunrise is found, set flag nosunrise
            var nosunrise = false;

            var riseTimeGMT = calcSunriseUTC(JD, latitude, longitude);
            nosunrise = !isNumber(riseTimeGMT);

            // Calculate sunset for this date
            // if no sunset is found, set flag nosunset
            var nosunset = false;
            var setTimeGMT = calcSunsetUTC(JD, latitude, longitude);
            if (!isNumber(setTimeGMT))
            {
                nosunset = true;
            }

            if (!nosunrise)	// Sunrise was found
            {
                info.Sunrise = date.Date.AddMinutes(riseTimeGMT);
            }

            if (!nosunset)	// Sunset was found
            {
                info.Sunset = date.Date.AddMinutes(setTimeGMT);
            }

            // Calculate solar noon for this date
            var solNoonGMT = calcSolNoonUTC(T, longitude);

            if (!(nosunset || nosunrise))
            {
                // info.Noon = TimeSpan.FromMinutes(solNoonGMT);
                info.Noon = TimeSpan.FromTicks((long)(solNoonGMT * TimeSpan.TicksPerMinute));
            }

            var tsnoon = calcTimeJulianCent(calcJDFromJulianCent(T) - 0.5 + solNoonGMT / 1440.0);
            eqTime = calcEquationOfTime(tsnoon);
            solarDec = calcSunDeclination(tsnoon);

            // info.EquationOfTime = TimeSpan.FromMinutes(eqTime);
            info.EquationOfTime = TimeSpan.FromTicks((long)(eqTime * TimeSpan.TicksPerMinute));
            info.SolarDeclination = solarDec;

            // report special cases of no sunrise
            if (nosunrise)
            {
                // if Northern hemisphere and spring or summer, OR
                // if Southern hemisphere and fall or winter, use
                // previous sunrise and next sunset

                if (((latitude > 66.4) && (doy > 79) && (doy < 267)) ||
                ((latitude < -66.4) && ((doy < 83) || (doy > 263))))
                {
                    var newjd = findRecentSunrise(JD, latitude, longitude);
                    var newtime = calcSunriseUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd -= 1.0;
                    }

                    info.Sunrise = ConvertToDate(newtime, newjd);
                }

                // if Northern hemisphere and fall or winter, OR
                // if Southern hemisphere and spring or summer, use
                // next sunrise and previous sunset

                else if (((latitude > 66.4) && ((doy < 83) || (doy > 263))) ||
                ((latitude < -66.4) && (doy > 79) && (doy < 267)))
                {
                    var newjd = findNextSunrise(JD, latitude, longitude);
                    var newtime = calcSunriseUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd -= 1.0;
                    }

                    info.Sunrise = ConvertToDate(newtime, newjd);
                }
                else
                {
                    Microsoft.SPOT.Debug.Assert(false, "Cannot Find Sunrise!");
                }

                // alert("Last Sunrise was on day " + findRecentSunrise(JD, latitude, longitude));
                // alert("Next Sunrise will be on day " + findNextSunrise(JD, latitude, longitude));
            }

            if (nosunset)
            {
                // if Northern hemisphere and spring or summer, OR
                // if Southern hemisphere and fall or winter, use
                // previous sunrise and next sunset

                if (((latitude > 66.4) && (doy > 79) && (doy < 267)) ||
                ((latitude < -66.4) && ((doy < 83) || (doy > 263))))
                {
                    var newjd = findNextSunset(JD, latitude, longitude);
                    var newtime = calcSunsetUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd -= 1.0;
                    }

                    info.Sunset = ConvertToDate(newtime, newjd);
                }

                // if Northern hemisphere and fall or winter, OR
                // if Southern hemisphere and spring or summer, use
                // next sunrise and last sunset

                else if (((latitude > 66.4) && ((doy < 83) || (doy > 263))) ||
                ((latitude < -66.4) && (doy > 79) && (doy < 267)))
                {
                    var newjd = findRecentSunset(JD, latitude, longitude);
                    var newtime = calcSunsetUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd -= 1.0;
                    }

                    info.Sunset = ConvertToDate(newtime, newjd);
                }
                else
                {
                    Microsoft.SPOT.Debug.Assert(false, "Cannot Find Sunset!");
                }

            }

            return info;
        }
        public static SolarInfo ForDate(double latitude, double longitude, DateTime date)
        {
            SolarInfo info = new SolarInfo();

            info.Date = date.Date;


            int year  = date.Year;
            int month = date.Month;
            int day   = date.Day;

            if ((latitude >= -90) && (latitude < -89))
            {
                //alert("All latitudes between 89 and 90 S\n will be set to -89");
                //latLongForm["latDeg"].value = -89;
                latitude = -89;
            }

            if ((latitude <= 90) && (latitude > 89))
            {
                //alert("All latitudes between 89 and 90 N\n will be set to 89");
                //latLongForm["latDeg"].value = 89;
                latitude = 89;
            }

            //***** Calculate the time of sunrise
            double JD = calcJD(year, month, day);

            info.JD = JD;
            //double dow = calcDayOfWeek(JD);
            int    doy = calcDayOfYear(month, day, isLeapYear(year));
            double T   = calcTimeJulianCent(JD);

            // double alpha = calcSunRtAscension(T);
            double solarDec = calcSunDeclination(T);
            double eqTime   = calcEquationOfTime(T); // (in minutes)



            // Calculate sunrise for this date
            // if no sunrise is found, set flag nosunrise
            bool nosunrise = false;

            double riseTimeGMT = calcSunriseUTC(JD, latitude, longitude);

            nosunrise = !isNumber(riseTimeGMT);

            // Calculate sunset for this date
            // if no sunset is found, set flag nosunset
            bool   nosunset   = false;
            double setTimeGMT = calcSunsetUTC(JD, latitude, longitude);

            if (!isNumber(setTimeGMT))
            {
                nosunset = true;
            }

            if (!nosunrise) // Sunrise was found
            {
                info.Sunrise = date.Date.AddMinutes(riseTimeGMT);
            }

            if (!nosunset) // Sunset was found
            {
                info.Sunset = date.Date.AddMinutes(setTimeGMT);
            }

            // Calculate solar noon for this date
            double solNoonGMT = calcSolNoonUTC(T, longitude);

            if (!(nosunset || nosunrise))
            {
                info.Noon = TimeSpan.FromMinutes(solNoonGMT);
            }


            double tsnoon = calcTimeJulianCent(calcJDFromJulianCent(T) - 0.5 + solNoonGMT / 1440.0);

            eqTime   = calcEquationOfTime(tsnoon);
            solarDec = calcSunDeclination(tsnoon);

            info.EquationOfTime   = TimeSpan.FromMinutes(eqTime);
            info.SolarDeclination = solarDec;

            // report special cases of no sunrise
            if (nosunrise)
            {
                // if Northern hemisphere and spring or summer, OR
                // if Southern hemisphere and fall or winter, use
                // previous sunrise and next sunset

                if (((latitude > 66.4) && (doy > 79) && (doy < 267)) ||
                    ((latitude < -66.4) && ((doy < 83) || (doy > 263))))
                {
                    double newjd   = findRecentSunrise(JD, latitude, longitude);
                    double newtime = calcSunriseUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd   += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd   -= 1.0;
                    }

                    info.Sunrise = ConvertToDate(newtime, newjd);
                }

                // if Northern hemisphere and fall or winter, OR
                // if Southern hemisphere and spring or summer, use
                // next sunrise and previous sunset

                else if (((latitude > 66.4) && ((doy < 83) || (doy > 263))) ||
                         ((latitude < -66.4) && (doy > 79) && (doy < 267)))
                {
                    double newjd   = findNextSunrise(JD, latitude, longitude);
                    double newtime = calcSunriseUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd   += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd   -= 1.0;
                    }

                    info.Sunrise = ConvertToDate(newtime, newjd);
                }
                else
                {
                    Debug.Fail("Cannot Find Sunrise!");
                }

                // alert("Last Sunrise was on day " + findRecentSunrise(JD, latitude, longitude));
                // alert("Next Sunrise will be on day " + findNextSunrise(JD, latitude, longitude));
            }

            if (nosunset)
            {
                // if Northern hemisphere and spring or summer, OR
                // if Southern hemisphere and fall or winter, use
                // previous sunrise and next sunset

                if (((latitude > 66.4) && (doy > 79) && (doy < 267)) ||
                    ((latitude < -66.4) && ((doy < 83) || (doy > 263))))
                {
                    double newjd   = findNextSunset(JD, latitude, longitude);
                    double newtime = calcSunsetUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd   += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd   -= 1.0;
                    }

                    info.Sunset = ConvertToDate(newtime, newjd);
                }

                // if Northern hemisphere and fall or winter, OR
                // if Southern hemisphere and spring or summer, use
                // next sunrise and last sunset

                else if (((latitude > 66.4) && ((doy < 83) || (doy > 263))) ||
                         ((latitude < -66.4) && (doy > 79) && (doy < 267)))
                {
                    double newjd   = findRecentSunset(JD, latitude, longitude);
                    double newtime = calcSunsetUTC(newjd, latitude, longitude);

                    if (newtime > 1440)
                    {
                        newtime -= 1440;
                        newjd   += 1.0;
                    }
                    if (newtime < 0)
                    {
                        newtime += 1440;
                        newjd   -= 1.0;
                    }

                    info.Sunset = ConvertToDate(newtime, newjd);
                }
                else
                {
                    Debug.Fail("Cannot Find Sunset!");
                }
            }

            return(info);
        }