Class SunTimesCalculator

java.lang.Object
com.kosherjava.zmanim.util.AstronomicalCalculator
com.kosherjava.zmanim.util.SunTimesCalculator
All Implemented Interfaces:
Cloneable

public class SunTimesCalculator
extends AstronomicalCalculator
Implementation of sunrise and sunset methods to calculate astronomical times. This calculator uses the Java algorithm written by Kevin Boone that is based on the US Naval Observatory'sAlmanac for Computer algorithm ( Amazon, Barnes & Noble) and is used with his permission. Added to Kevin's code is adjustment of the zenith to account for elevation.
Author:
© Eliyahu Hershfeld 2004 - 2020, © Kevin Boone 2000
  • Field Summary

    Fields
    Modifier and Type Field Description
    private static double DEG_PER_HOUR
    The number of degrees of longitude that corresponds to one hour time difference.
  • Constructor Summary

    Constructors
    Constructor Description
    SunTimesCalculator()  
  • Method Summary

    Modifier and Type Method Description
    private static double acosDeg​(double x)  
    private static double asinDeg​(double x)  
    private static double cosDeg​(double deg)
    Calculate cosine of the angle in degrees
    private static double getApproxTimeDays​(int dayOfYear, double hoursFromMeridian, boolean isSunrise)
    Calculate the approximate time of sunset or sunrise in days since midnight Jan 1st, assuming 6am and 6pm events.
    String getCalculatorName()
    Returns the name of the algorithm.
    private static double getCosLocalHourAngle​(double sunTrueLongitude, double latitude, double zenith)
    Calculate the cosine of the Sun's local hour angle
    private static double getHoursFromMeridian​(double longitude)
    Get time difference between location's longitude and the Meridian, in hours.
    private static double getLocalMeanTime​(double localHour, double sunRightAscensionHours, double approxTimeDays)
    Calculate local mean time of rising or setting.
    private static double getMeanAnomaly​(int dayOfYear, double longitude, boolean isSunrise)
    Calculate the Sun's mean anomaly in degrees, at sunrise or sunset, given the longitude in degrees
    private static double getSunRightAscensionHours​(double sunTrueLongitude)
    Calculates the Sun's right ascension in hours.
    private static double getSunTrueLongitude​(double sunMeanAnomaly)  
    private static double getTimeUTC​(Calendar calendar, GeoLocation geoLocation, double zenith, boolean isSunrise)
    Get sunrise or sunset time in UTC, according to flag.
    double getUTCSunrise​(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation)
    A method that calculates UTC sunrise as well as any time based on an angle above or below sunrise.
    double getUTCSunset​(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation)
    A method that calculates UTC sunset as well as any time based on an angle above or below sunset.
    private static double sinDeg​(double deg)  
    private static double tanDeg​(double deg)  

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEG_PER_HOUR

      private static final double DEG_PER_HOUR
      The number of degrees of longitude that corresponds to one hour time difference.
      See Also:
      Constant Field Values
  • Constructor Details

  • Method Details

    • getCalculatorName

      Description copied from class: AstronomicalCalculator
      Returns the name of the algorithm.
      Specified by:
      getCalculatorName in class AstronomicalCalculator
      Returns:
      the descriptive name of the algorithm.
      See Also:
      AstronomicalCalculator.getCalculatorName()
    • getUTCSunrise

      public double getUTCSunrise​(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation)
      Description copied from class: AstronomicalCalculator
      A method that calculates UTC sunrise as well as any time based on an angle above or below sunrise. This abstract method is implemented by the classes that extend this class.
      Specified by:
      getUTCSunrise in class AstronomicalCalculator
      Parameters:
      calendar - Used to calculate day of year.
      geoLocation - The location information used for astronomical calculating sun times.
      zenith - the azimuth below the vertical zenith of 90 degrees. for sunrise typically the zenith used for the calculation uses geometric zenith of 90° and adjusts this slightly to account for solar refraction and the sun's radius. Another example would be AstronomicalCalendar.getBeginNauticalTwilight() that passes AstronomicalCalendar.NAUTICAL_ZENITH to this method.
      adjustForElevation - Should the time be adjusted for elevation
      Returns:
      The UTC time of sunrise in 24 hour format. 5:45:00 AM will return 5.75.0. If an error was encountered in the calculation (expected behavior for some locations such as near the poles, Double.NaN will be returned.
      See Also:
      AstronomicalCalculator.getUTCSunrise(Calendar, GeoLocation, double, boolean)
    • getUTCSunset

      public double getUTCSunset​(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation)
      Description copied from class: AstronomicalCalculator
      A method that calculates UTC sunset as well as any time based on an angle above or below sunset. This abstract method is implemented by the classes that extend this class.
      Specified by:
      getUTCSunset in class AstronomicalCalculator
      Parameters:
      calendar - Used to calculate day of year.
      geoLocation - The location information used for astronomical calculating sun times.
      zenith - the azimuth below the vertical zenith of 90°. For sunset typically the zenith used for the calculation uses geometric zenith of 90° and adjusts this slightly to account for solar refraction and the sun's radius. Another example would be AstronomicalCalendar.getEndNauticalTwilight() that passes AstronomicalCalendar.NAUTICAL_ZENITH to this method.
      adjustForElevation - Should the time be adjusted for elevation
      Returns:
      The UTC time of sunset in 24 hour format. 5:45:00 AM will return 5.75.0. If an error was encountered in the calculation (expected behavior for some locations such as near the poles, Double.NaN will be returned.
      See Also:
      AstronomicalCalculator.getUTCSunset(Calendar, GeoLocation, double, boolean)
    • sinDeg

      private static double sinDeg​(double deg)
      Parameters:
      deg - the degrees
      Returns:
      sin of the angle in degrees
    • acosDeg

      private static double acosDeg​(double x)
      Parameters:
      x - angle
      Returns:
      acos of the angle in degrees
    • asinDeg

      private static double asinDeg​(double x)
      Parameters:
      x - angle
      Returns:
      asin of the angle in degrees
    • tanDeg

      private static double tanDeg​(double deg)
      Parameters:
      deg - degrees
      Returns:
      tan of the angle in degrees
    • cosDeg

      private static double cosDeg​(double deg)
      Calculate cosine of the angle in degrees
      Parameters:
      deg - degrees
      Returns:
      cosine of the angle in degrees
    • getHoursFromMeridian

      private static double getHoursFromMeridian​(double longitude)
      Get time difference between location's longitude and the Meridian, in hours.
      Parameters:
      longitude - the longitude
      Returns:
      time difference between the location's longitude and the Meridian, in hours. West of Meridian has a negative time difference
    • getApproxTimeDays

      private static double getApproxTimeDays​(int dayOfYear, double hoursFromMeridian, boolean isSunrise)
      Calculate the approximate time of sunset or sunrise in days since midnight Jan 1st, assuming 6am and 6pm events. We need this figure to derive the Sun's mean anomaly.
      Parameters:
      dayOfYear - the day of year
      hoursFromMeridian - hours from the meridian
      isSunrise - true for sunrise and false for sunset
      Returns:
      the approximate time of sunset or sunrise in days since midnight Jan 1st, assuming 6am and 6pm events. We need this figure to derive the Sun's mean anomaly.
    • getMeanAnomaly

      private static double getMeanAnomaly​(int dayOfYear, double longitude, boolean isSunrise)
      Calculate the Sun's mean anomaly in degrees, at sunrise or sunset, given the longitude in degrees
      Parameters:
      dayOfYear - the day of the year
      longitude - longitude
      isSunrise - true for sunrise and false for sunset
      Returns:
      the Sun's mean anomaly in degrees
    • getSunTrueLongitude

      private static double getSunTrueLongitude​(double sunMeanAnomaly)
      Parameters:
      sunMeanAnomaly - the Sun's mean anomaly in degrees
      Returns:
      the Sun's true longitude in degrees. The result is an angle >= 0 and <= 360.
    • getSunRightAscensionHours

      private static double getSunRightAscensionHours​(double sunTrueLongitude)
      Calculates the Sun's right ascension in hours.
      Parameters:
      sunTrueLongitude - the Sun's true longitude in degrees > 0 and < 360.
      Returns:
      the Sun's right ascension in hours in angles > 0 and < 360.
    • getCosLocalHourAngle

      private static double getCosLocalHourAngle​(double sunTrueLongitude, double latitude, double zenith)
      Calculate the cosine of the Sun's local hour angle
      Parameters:
      sunTrueLongitude - the sun's true longitude
      latitude - the latitude
      zenith - the zenith
      Returns:
      the cosine of the Sun's local hour angle
    • getLocalMeanTime

      private static double getLocalMeanTime​(double localHour, double sunRightAscensionHours, double approxTimeDays)
      Calculate local mean time of rising or setting. By 'local' is meant the exact time at the location, assuming that there were no time zone. That is, the time difference between the location and the Meridian depended entirely on the longitude. We can't do anything with this time directly; we must convert it to UTC and then to a local time.
      Parameters:
      localHour - the local hour
      sunRightAscensionHours - the sun's right ascention in hours
      approxTimeDays - approximate time days
      Returns:
      the fractional number of hours since midnight as a double
    • getTimeUTC

      private static double getTimeUTC​(Calendar calendar, GeoLocation geoLocation, double zenith, boolean isSunrise)
      Get sunrise or sunset time in UTC, according to flag. This time is returned as a double and is not adjusted for time-zone.
      Parameters:
      calendar - the Calendar object to extract the day of year for calculation
      geoLocation - the GeoLocation object that contains the latitude and longitude
      zenith - Sun's zenith, in degrees
      isSunrise - True for sunrise and false for sunset.
      Returns:
      the time as a double. If an error was encountered in the calculation (expected behavior for some locations such as near the poles, Double.NaN will be returned.