|
| 1 | +package geoTools; |
| 2 | + |
| 3 | +/** |
| 4 | + * Calculates the time of sunrise or sunset |
| 5 | + * at a given longitude and latititude.<p> |
| 6 | + * |
| 7 | + * Formulas are taken from: {@link https://lexikon.astronomie.info/zeitgleichung/index.html} |
| 8 | + * |
| 9 | + * @author Berthold Fritz |
| 10 | + * |
| 11 | + */ |
| 12 | +public class SunriseSunset { |
| 13 | + private static final double sunsetDecIn_Minutes=-0.0145; // Dec of sun below local lat for sunset |
| 14 | + private static final double NOON=12; |
| 15 | + |
| 16 | + /** |
| 17 | + * Convert degrees to radians. |
| 18 | + * |
| 19 | + * @param grad |
| 20 | + * @return Given angle in degrees converted to radians. |
| 21 | + */ |
| 22 | + public static double toRad (double grad){ |
| 23 | + return grad*Math.PI/180; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Convert radians to degrees. |
| 28 | + * |
| 29 | + * @param rad |
| 30 | + * @return Given angle in radians converted to degrees. |
| 31 | + */ |
| 32 | + public static double toGrad (double rad){ |
| 33 | + return rad*180/Math.PI; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Time of sunrise at an observers location. |
| 38 | + * This is a convenient way to get a quick result whithout |
| 39 | + * calling all methods step by step. |
| 40 | + * |
| 41 | + * @param lng Longitude in degrees. |
| 42 | + * @param lat Latitude in degrees. |
| 43 | + * @param dayOfYear Day of year (1st of January is 1). |
| 44 | + * @param timeZone +/- relative to Greenwich Mean time (east is +, west is -) |
| 45 | + * @return Time of sunrise at the given location and timezone. |
| 46 | + */ |
| 47 | + public static double getSunriseTimeAtObserversLocationIn_h(double lng,double lat,int dayOfYear,int timeZone){ |
| 48 | + double latIn_rad=toRad(lat); |
| 49 | + double longIn_rad=toRad(lng); |
| 50 | + |
| 51 | + double declinationIn_rad=getSunDeclinationIn_Rad(dayOfYear); |
| 52 | + double deltaTimeIn_h=SunriseSunset.getDeltaTimeIn_h(declinationIn_rad,latIn_rad); |
| 53 | + double trueSunsetTimeIn_h=SunriseSunset.getTrueSunriseTimeIn_h(deltaTimeIn_h,dayOfYear); |
| 54 | + return getTimeAtLatitudeAndTimeZoneIn_h(trueSunsetTimeIn_h, longIn_rad, timeZone); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Time of sunset at an observers location. |
| 59 | + * This is a convenient way to get a quick result whithout |
| 60 | + * calling all methods step by step. |
| 61 | + * |
| 62 | + * @param lng Longitude in degrees. |
| 63 | + * @param lat Latitude in degrees. |
| 64 | + * @param dayOfYear Day of year (1st of January is 1). |
| 65 | + * @param timeZone +/- relative to Greenwich Mean time (east is +, west is -) |
| 66 | + * @return Time of sunset at the given location and timezone. |
| 67 | + */ |
| 68 | + public static double getSunsetTimeAtObserversLocationIn_h(double lng,double lat,int dayOfYear,int timeZone){ |
| 69 | + double latIn_rad=toRad(lat); |
| 70 | + double longIn_rad=toRad(lng); |
| 71 | + |
| 72 | + double declinationIn_rad=getSunDeclinationIn_Rad(dayOfYear); |
| 73 | + double deltaTimeIn_h=SunriseSunset.getDeltaTimeIn_h(declinationIn_rad,latIn_rad); |
| 74 | + double trueSunsetTimeIn_h=SunriseSunset.getTrueSunsetTimeIn_h(deltaTimeIn_h,dayOfYear); |
| 75 | + return getTimeAtLatitudeAndTimeZoneIn_h(trueSunsetTimeIn_h, longIn_rad, timeZone); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Declination. |
| 80 | + * |
| 81 | + * @param dayOfYear |
| 82 | + * @return Declination of sun in radians at a given day of year (1=1st of january). |
| 83 | + */ |
| 84 | + public static double getSunDeclinationIn_Rad(int dayOfYear){ |
| 85 | + return 0.4095*Math.sin(0.016906*(dayOfYear-80.086)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Difference between true local time and avarge local time (time equation). |
| 90 | + * |
| 91 | + * @param dayOfYear |
| 92 | + * @return Time difference in hours. |
| 93 | + */ |
| 94 | + public static double timeEquation(int dayOfYear){ |
| 95 | + return -0.171*Math.sin(0.0337*dayOfYear+0.465)-0.1299*Math.sin(0.01787*dayOfYear-0.168); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Time difference. |
| 100 | + * |
| 101 | + * @param decIn_Rad |
| 102 | + * @return Time differnece in hours until sun reaches a given latitude from |
| 103 | + * it's zenit. |
| 104 | + */ |
| 105 | + public static double getDeltaTimeIn_h(double decIn_Rad,double latitudeIn_rad){ |
| 106 | + return 12*Math.acos((Math.sin(sunsetDecIn_Minutes)-Math.sin(latitudeIn_rad)*Math.sin(decIn_Rad))/(Math.cos(0.9163)*Math.cos(decIn_Rad)))/Math.PI; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * True Sunrise time. |
| 111 | + * |
| 112 | + * @param deltaTimeIn_h |
| 113 | + * @return Time of sunrise in hours not compensated for average time at a given location |
| 114 | + * (time displayed on an observers watch). |
| 115 | + */ |
| 116 | + public static double getTrueSunriseTimeIn_h(double deltaTimeIn_h, int dayOfYear){ |
| 117 | + return NOON-deltaTimeIn_h-timeEquation(dayOfYear); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * True Sunset time. |
| 122 | + * |
| 123 | + * @param deltaTimeIn_h |
| 124 | + * @return Time of sunset in hours not compensated for average time at a given location |
| 125 | + * (time displayed on an observers watch). |
| 126 | + */ |
| 127 | + public static double getTrueSunsetTimeIn_h(double deltaTimeIn_h,int dayOfYear){ |
| 128 | + return NOON+deltaTimeIn_h-timeEquation(dayOfYear); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Time at the latitude and timezone of the observer. |
| 133 | + * |
| 134 | + * @param gmtTimeIn_h |
| 135 | + * @param latitude |
| 136 | + * @param timeZoneIn_DeltaToGMTin_h |
| 137 | + * @return Time at a given latitude and timezone. |
| 138 | + */ |
| 139 | + public static double getTimeAtLatitudeAndTimeZoneIn_h(double gmtTimeIn_h,double latitude,int timeZoneIn_DeltaToGMTin_h){ |
| 140 | + return gmtTimeIn_h-latitude/15+timeZoneIn_DeltaToGMTin_h; |
| 141 | + |
| 142 | + } |
| 143 | +} |
0 commit comments