Skip to content

Commit 524e848

Browse files
authored
Refactor NOAACalculator removjng more more code duplication
Should make it simpler to read, maintain and port. Hopefully more to come.
1 parent 759caf7 commit 524e848

File tree

1 file changed

+15
-55
lines changed

1 file changed

+15
-55
lines changed

src/main/java/com/kosherjava/zmanim/util/NOAACalculator.java

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ public String getCalculatorName() {
6565
public double getUTCSunrise(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation) {
6666
double elevation = adjustForElevation ? geoLocation.getElevation() : 0;
6767
double adjustedZenith = adjustZenith(zenith, elevation);
68-
double sunrise = getSunriseUTC(getJulianDay(calendar), geoLocation.getLatitude(), -geoLocation.getLongitude(),
69-
adjustedZenith);
68+
double sunrise = getSunRiseSetUTC(getJulianDay(calendar), geoLocation.getLatitude(), -geoLocation.getLongitude(),
69+
adjustedZenith, SolarEvent.SUNRISE);
7070
sunrise = sunrise / 60;
71-
7271
return sunrise > 0 ? sunrise % 24 : sunrise % 24 + 24; // ensure that the time is >= 0 and < 24
7372
}
7473

@@ -78,8 +77,8 @@ public double getUTCSunrise(Calendar calendar, GeoLocation geoLocation, double z
7877
public double getUTCSunset(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation) {
7978
double elevation = adjustForElevation ? geoLocation.getElevation() : 0;
8079
double adjustedZenith = adjustZenith(zenith, elevation);
81-
double sunset = getSunsetUTC(getJulianDay(calendar), geoLocation.getLatitude(), -geoLocation.getLongitude(),
82-
adjustedZenith);
80+
double sunset = getSunRiseSetUTC(getJulianDay(calendar), geoLocation.getLatitude(), -geoLocation.getLongitude(),
81+
adjustedZenith, SolarEvent.SUNSET);
8382
sunset = sunset / 60;
8483
return sunset > 0 ? sunset % 24 : sunset % 24 + 24; // ensure that the time is >= 0 and < 24
8584
}
@@ -386,48 +385,6 @@ public static double getSolarAzimuth(Calendar calendar, double latitude, double
386385

387386
}
388387

389-
/**
390-
* Return the <a href="https://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
391-
* of sunrise for the given day at the given location on earth.
392-
*
393-
* @param julianDay
394-
* the Julian day
395-
* @param latitude
396-
* the latitude of observer in degrees
397-
* @param longitude
398-
* the longitude of observer in degrees
399-
* @param zenith
400-
* the zenith
401-
* @return the time in minutes from zero UTC
402-
*/
403-
private static double getSunriseUTC(double julianDay, double latitude, double longitude, double zenith) {
404-
double julianCenturies = getJulianCenturiesFromJulianDay(julianDay);
405-
406-
// Find the time of solar noon at the location, and use that declination. This is better than start of the
407-
// Julian day
408-
double noonmin = getSolarNoonUTC(julianCenturies, longitude);
409-
double tnoon = getJulianCenturiesFromJulianDay(julianDay + noonmin / 1440.0);
410-
411-
// First pass to approximate sunrise (using solar noon)
412-
double equationOfTime = getEquationOfTime(tnoon);
413-
double solarDeclination = getSunDeclination(tnoon);
414-
double hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, SolarEvent.SUNRISE);
415-
double delta = longitude - Math.toDegrees(hourAngle);
416-
double timeDiff = 4 * delta; // in minutes of time
417-
double timeUTC = 720 + timeDiff - equationOfTime; // in minutes
418-
419-
// Second pass includes fractional Julian Day in gamma calc
420-
double newt = getJulianCenturiesFromJulianDay(getJulianDayFromJulianCenturies(julianCenturies) + timeUTC
421-
/ 1440.0);
422-
equationOfTime = getEquationOfTime(newt);
423-
solarDeclination = getSunDeclination(newt);
424-
hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, SolarEvent.SUNRISE);
425-
delta = longitude - Math.toDegrees(hourAngle);
426-
timeDiff = 4 * delta;
427-
timeUTC = 720 + timeDiff - equationOfTime; // in minutes
428-
return timeUTC;
429-
}
430-
431388
/**
432389
* Return the <a href="https://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
433390
* of <a href="https://en.wikipedia.org/wiki/Noon#Solar_noon">solar noon</a> for the given day at the given location
@@ -470,7 +427,7 @@ public double getUTCNoon(Calendar calendar, GeoLocation geoLocation) {
470427
* @see #getUTCNoon(Calendar, GeoLocation)
471428
*/
472429
private static double getSolarNoonUTC(double julianCenturies, double longitude) {
473-
// First pass uses approximate solar noon to calculate equation of time
430+
// Only 1 pass for approximate solar noon to calculate equation of time
474431
double tnoon = getJulianCenturiesFromJulianDay(getJulianDayFromJulianCenturies(julianCenturies) + longitude
475432
/ 360.0);
476433
double equationOfTime = getEquationOfTime(tnoon);
@@ -480,10 +437,10 @@ private static double getSolarNoonUTC(double julianCenturies, double longitude)
480437
equationOfTime = getEquationOfTime(newt);
481438
return 720 + (longitude * 4) - equationOfTime; // minutes
482439
}
483-
440+
484441
/**
485442
* Return the <a href="https://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
486-
* of sunset for the given day at the given location on earth.
443+
* of sunrise or sunset for the given day at the given location on earth.
487444
*
488445
* @param julianDay
489446
* the Julian day
@@ -493,20 +450,23 @@ private static double getSolarNoonUTC(double julianCenturies, double longitude)
493450
* longitude of observer in degrees
494451
* @param zenith
495452
* zenith
453+
* @param solarEvent
454+
* Is the calculation for sunrise or sunset
496455
* @return the time in minutes from zero Universal Coordinated Time (UTC)
497456
*/
498-
private static double getSunsetUTC(double julianDay, double latitude, double longitude, double zenith) {
457+
private static double getSunRiseSetUTC(double julianDay, double latitude, double longitude, double zenith,
458+
SolarEvent solarEvent) {
499459
double julianCenturies = getJulianCenturiesFromJulianDay(julianDay);
500460

501-
// Find the time of solar noon at the location, and use that declination. This is better than start of the
502-
// Julian day
461+
// Find the time of solar noon at the location, and use that declination.
462+
// This is better than start of the Julian day
503463
double noonmin = getSolarNoonUTC(julianCenturies, longitude);
504464
double tnoon = getJulianCenturiesFromJulianDay(julianDay + noonmin / 1440.0);
505465

506466
// First calculates sunrise and approx length of day
507467
double equationOfTime = getEquationOfTime(tnoon);
508468
double solarDeclination = getSunDeclination(tnoon);
509-
double hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, SolarEvent.SUNSET);
469+
double hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, solarEvent);
510470
double delta = longitude - Math.toDegrees(hourAngle);
511471
double timeDiff = 4 * delta;
512472
double timeUTC = 720 + timeDiff - equationOfTime;
@@ -516,7 +476,7 @@ private static double getSunsetUTC(double julianDay, double latitude, double lon
516476
/ 1440.0);
517477
equationOfTime = getEquationOfTime(newt);
518478
solarDeclination = getSunDeclination(newt);
519-
hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, SolarEvent.SUNSET);
479+
hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, solarEvent);
520480
delta = longitude - Math.toDegrees(hourAngle);
521481
timeDiff = 4 * delta;
522482
timeUTC = 720 + timeDiff - equationOfTime; // in minutes

0 commit comments

Comments
 (0)