Skip to content

Commit 63403fd

Browse files
authored
More code simlification and refactoring of NOAACalculator
No impact to any public interfaces.
1 parent 524e848 commit 63403fd

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

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

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ 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 = getSunRiseSetUTC(getJulianDay(calendar), geoLocation.getLatitude(), -geoLocation.getLongitude(),
68+
double sunrise = getSunRiseSetUTC(calendar, geoLocation.getLatitude(), -geoLocation.getLongitude(),
6969
adjustedZenith, SolarEvent.SUNRISE);
7070
sunrise = sunrise / 60;
7171
return sunrise > 0 ? sunrise % 24 : sunrise % 24 + 24; // ensure that the time is >= 0 and < 24
@@ -77,7 +77,7 @@ public double getUTCSunrise(Calendar calendar, GeoLocation geoLocation, double z
7777
public double getUTCSunset(Calendar calendar, GeoLocation geoLocation, double zenith, boolean adjustForElevation) {
7878
double elevation = adjustForElevation ? geoLocation.getElevation() : 0;
7979
double adjustedZenith = adjustZenith(zenith, elevation);
80-
double sunset = getSunRiseSetUTC(getJulianDay(calendar), geoLocation.getLatitude(), -geoLocation.getLongitude(),
80+
double sunset = getSunRiseSetUTC(calendar, geoLocation.getLatitude(), -geoLocation.getLongitude(),
8181
adjustedZenith, SolarEvent.SUNSET);
8282
sunset = sunset / 60;
8383
return sunset > 0 ? sunset % 24 : sunset % 24 + 24; // ensure that the time is >= 0 and < 24
@@ -116,19 +116,6 @@ private static double getJulianCenturiesFromJulianDay(double julianDay) {
116116
return (julianDay - JULIAN_DAY_JAN_1_2000) / JULIAN_DAYS_PER_CENTURY;
117117
}
118118

119-
/**
120-
* Convert centuries since <a href="https://en.wikipedia.org/wiki/Epoch_(astronomy)#J2000">J2000.0</a> to
121-
* <a href="https://en.wikipedia.org/wiki/Julian_day">Julian day</a>.
122-
*
123-
* @param julianCenturies
124-
* the number of Julian centuries since <a href=
125-
* "https://en.wikipedia.org/wiki/Epoch_(astronomy)#J2000">J2000.0</a>.
126-
* @return the Julian Day corresponding to the Julian centuries passed in
127-
*/
128-
private static double getJulianDayFromJulianCenturies(double julianCenturies) {
129-
return julianCenturies * JULIAN_DAYS_PER_CENTURY + JULIAN_DAY_JAN_1_2000;
130-
}
131-
132119
/**
133120
* Returns the Geometric <a href="https://en.wikipedia.org/wiki/Mean_longitude">Mean Longitude</a> of the Sun.
134121
*
@@ -143,31 +130,31 @@ private static double getSunGeometricMeanLongitude(double julianCenturies) {
143130
}
144131

145132
/**
146-
* Returns the Geometric <a href="https://en.wikipedia.org/wiki/Mean_anomaly">Mean Anomaly</a> of the Sun.
133+
* Returns the Geometric <a href="https://en.wikipedia.org/wiki/Mean_anomaly">Mean Anomaly</a> of the Sun in degrees.
147134
*
148135
* @param julianCenturies
149136
* the number of Julian centuries since <a href=
150137
* "https://en.wikipedia.org/wiki/Epoch_(astronomy)#J2000">J2000.0</a>.
151138
* @return the Geometric Mean Anomaly of the Sun in degrees
152139
*/
153140
private static double getSunGeometricMeanAnomaly(double julianCenturies) {
154-
return 357.52911 + julianCenturies * (35999.05029 - 0.0001537 * julianCenturies); // in degrees
141+
return 357.52911 + julianCenturies * (35999.05029 - 0.0001537 * julianCenturies);
155142
}
156143

157144
/**
158-
* Return the <a href="https://en.wikipedia.org/wiki/Eccentricity_%28orbit%29">eccentricity of earth's orbit</a>.
145+
* Return the unitless <a href="https://en.wikipedia.org/wiki/Eccentricity_%28orbit%29">eccentricity of earth's orbit</a>.
159146
*
160147
* @param julianCenturies
161148
* the number of Julian centuries since <a href=
162149
* "https://en.wikipedia.org/wiki/Epoch_(astronomy)#J2000">J2000.0</a>.
163150
* @return the unitless eccentricity
164151
*/
165152
private static double getEarthOrbitEccentricity(double julianCenturies) {
166-
return 0.016708634 - julianCenturies * (0.000042037 + 0.0000001267 * julianCenturies); // unitless
153+
return 0.016708634 - julianCenturies * (0.000042037 + 0.0000001267 * julianCenturies);
167154
}
168155

169156
/**
170-
* Returns the <a href="https://en.wikipedia.org/wiki/Equation_of_the_center">equation of center</a> for the sun.
157+
* Returns the <a href="https://en.wikipedia.org/wiki/Equation_of_the_center">equation of center</a> for the sun in degrees.
171158
*
172159
* @param julianCenturies
173160
* the number of Julian centuries since <a href=
@@ -181,7 +168,7 @@ private static double getSunEquationOfCenter(double julianCenturies) {
181168
double sin2m = Math.sin(mrad + mrad);
182169
double sin3m = Math.sin(mrad + mrad + mrad);
183170
return sinm * (1.914602 - julianCenturies * (0.004817 + 0.000014 * julianCenturies)) + sin2m
184-
* (0.019993 - 0.000101 * julianCenturies) + sin3m * 0.000289; // in degrees
171+
* (0.019993 - 0.000101 * julianCenturies) + sin3m * 0.000289;
185172
}
186173

187174
/**
@@ -195,7 +182,7 @@ private static double getSunEquationOfCenter(double julianCenturies) {
195182
private static double getSunTrueLongitude(double julianCenturies) {
196183
double sunLongitude = getSunGeometricMeanLongitude(julianCenturies);
197184
double center = getSunEquationOfCenter(julianCenturies);
198-
return sunLongitude + center; // in degrees
185+
return sunLongitude + center;
199186
}
200187

201188
// /**
@@ -209,7 +196,7 @@ private static double getSunTrueLongitude(double julianCenturies) {
209196
// double meanAnomaly = getSunGeometricMeanAnomaly(julianCenturies);
210197
// double equationOfCenter = getSunEquationOfCenter(julianCenturies);
211198
//
212-
// return meanAnomaly + equationOfCenter; // in degrees
199+
// return meanAnomaly + equationOfCenter;
213200
// }
214201

215202
/**
@@ -224,7 +211,7 @@ private static double getSunApparentLongitude(double julianCenturies) {
224211
double sunTrueLongitude = getSunTrueLongitude(julianCenturies);
225212
double omega = 125.04 - 1934.136 * julianCenturies;
226213
double lambda = sunTrueLongitude - 0.00569 - 0.00478 * Math.sin(Math.toRadians(omega));
227-
return lambda; // in degrees
214+
return lambda;
228215
}
229216

230217
/**
@@ -238,7 +225,7 @@ private static double getSunApparentLongitude(double julianCenturies) {
238225
private static double getMeanObliquityOfEcliptic(double julianCenturies) {
239226
double seconds = 21.448 - julianCenturies
240227
* (46.8150 + julianCenturies * (0.00059 - julianCenturies * (0.001813)));
241-
return 23.0 + (26.0 + (seconds / 60.0)) / 60.0; // in degrees
228+
return 23.0 + (26.0 + (seconds / 60.0)) / 60.0;
242229
}
243230

244231
/**
@@ -253,7 +240,7 @@ private static double getMeanObliquityOfEcliptic(double julianCenturies) {
253240
private static double getObliquityCorrection(double julianCenturies) {
254241
double obliquityOfEcliptic = getMeanObliquityOfEcliptic(julianCenturies);
255242
double omega = 125.04 - 1934.136 * julianCenturies;
256-
return obliquityOfEcliptic + 0.00256 * Math.cos(Math.toRadians(omega)); // in degrees
243+
return obliquityOfEcliptic + 0.00256 * Math.cos(Math.toRadians(omega));
257244
}
258245

259246
/**
@@ -270,7 +257,7 @@ private static double getSunDeclination(double julianCenturies) {
270257
double lambda = getSunApparentLongitude(julianCenturies);
271258
double sint = Math.sin(Math.toRadians(obliquityCorrection)) * Math.sin(Math.toRadians(lambda));
272259
double theta = Math.toDegrees(Math.asin(sint));
273-
return theta; // in degrees
260+
return theta;
274261
}
275262

276263
/**
@@ -296,7 +283,7 @@ private static double getEquationOfTime(double julianCenturies) {
296283
double sin2m = Math.sin(2.0 * Math.toRadians(geomMeanAnomalySun));
297284
double equationOfTime = y * sin2l0 - 2.0 * eccentricityEarthOrbit * sinm + 4.0 * eccentricityEarthOrbit * y
298285
* sinm * cos2l0 - 0.5 * y * y * sin4l0 - 1.25 * eccentricityEarthOrbit * eccentricityEarthOrbit * sin2m;
299-
return Math.toDegrees(equationOfTime) * 4.0; // in minutes of time
286+
return Math.toDegrees(equationOfTime) * 4.0;
300287
}
301288

302289
/**
@@ -403,20 +390,19 @@ public static double getSolarAzimuth(Calendar calendar, double latitude, double
403390
* @return the time in minutes from zero UTC
404391
*/
405392
public double getUTCNoon(Calendar calendar, GeoLocation geoLocation) {
406-
double julianDay = getJulianDay(calendar);
407-
double julianCenturies = getJulianCenturiesFromJulianDay(julianDay);
408-
double noon = getSolarNoonUTC(julianCenturies, -geoLocation.getLongitude());
393+
double noon = getSolarNoonUTC(getJulianDay(calendar), -geoLocation.getLongitude());
409394
noon = noon / 60;
410395
return noon > 0 ? noon % 24 : noon % 24 + 24; // ensure that the time is >= 0 and < 24
411396
}
412397

413398
/**
414399
* Return the <a href="https://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
415-
* of of <a href="http://en.wikipedia.org/wiki/Noon#Solar_noon">solar noon</a> for the given day at the given location
400+
* of <a href="http://en.wikipedia.org/wiki/Noon#Solar_noon">solar noon</a> for the given day at the given location
416401
* on earth.
402+
* @todo Refactor to possibly use the getSunRiseSetUTC (to be renamed) and remove the need for this method.
417403
*
418-
* @param julianCenturies
419-
* the number of Julian centuries since <a href=
404+
* @param julianDay
405+
* the Julian day since <a href=
420406
* "https://en.wikipedia.org/wiki/Epoch_(astronomy)#J2000">J2000.0</a>.
421407
* @param longitude
422408
* the longitude of observer in degrees
@@ -426,24 +412,26 @@ public double getUTCNoon(Calendar calendar, GeoLocation geoLocation) {
426412
* @see com.kosherjava.zmanim.util.AstronomicalCalculator#getUTCNoon(Calendar, GeoLocation)
427413
* @see #getUTCNoon(Calendar, GeoLocation)
428414
*/
429-
private static double getSolarNoonUTC(double julianCenturies, double longitude) {
430-
// Only 1 pass for approximate solar noon to calculate equation of time
431-
double tnoon = getJulianCenturiesFromJulianDay(getJulianDayFromJulianCenturies(julianCenturies) + longitude
432-
/ 360.0);
415+
private static double getSolarNoonUTC(double julianDay, double longitude) {
416+
// First pass for approximate solar noon to calculate equation of time
417+
double tnoon = getJulianCenturiesFromJulianDay(julianDay + longitude / 360.0);
433418
double equationOfTime = getEquationOfTime(tnoon);
434419
double solNoonUTC = 720 + (longitude * 4) - equationOfTime; // minutes
435-
double newt = getJulianCenturiesFromJulianDay(getJulianDayFromJulianCenturies(julianCenturies) - 0.5
436-
+ solNoonUTC / 1440.0);
420+
421+
// second pass
422+
double newt = getJulianCenturiesFromJulianDay(julianDay - 0.5 + solNoonUTC / 1440.0);
437423
equationOfTime = getEquationOfTime(newt);
438-
return 720 + (longitude * 4) - equationOfTime; // minutes
424+
return 720 + (longitude * 4) - equationOfTime;
439425
}
440426

441427
/**
442428
* Return the <a href="https://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
443-
* of sunrise or sunset for the given day at the given location on earth.
429+
* of sunrise or sunset in minutes for the given day at the given location on earth.
430+
* @todo support solar noon and possibly increase the number of passes in the Arctic areas.
431+
* This would remove the need for getSolarNoonUTC.
444432
*
445-
* @param julianDay
446-
* the Julian day
433+
* @param calendar
434+
* the calendar
447435
* @param latitude
448436
* the latitude of observer in degrees
449437
* @param longitude
@@ -454,16 +442,16 @@ private static double getSolarNoonUTC(double julianCenturies, double longitude)
454442
* Is the calculation for sunrise or sunset
455443
* @return the time in minutes from zero Universal Coordinated Time (UTC)
456444
*/
457-
private static double getSunRiseSetUTC(double julianDay, double latitude, double longitude, double zenith,
445+
private static double getSunRiseSetUTC(Calendar calendar, double latitude, double longitude, double zenith,
458446
SolarEvent solarEvent) {
459-
double julianCenturies = getJulianCenturiesFromJulianDay(julianDay);
447+
double julianDay = getJulianDay(calendar);
460448

461449
// Find the time of solar noon at the location, and use that declination.
462450
// This is better than start of the Julian day
463-
double noonmin = getSolarNoonUTC(julianCenturies, longitude);
451+
double noonmin = getSolarNoonUTC(julianDay, longitude);
464452
double tnoon = getJulianCenturiesFromJulianDay(julianDay + noonmin / 1440.0);
465453

466-
// First calculates sunrise and approx length of day
454+
// First calculates sunrise and approximate length of day
467455
double equationOfTime = getEquationOfTime(tnoon);
468456
double solarDeclination = getSunDeclination(tnoon);
469457
double hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, solarEvent);
@@ -472,14 +460,14 @@ private static double getSunRiseSetUTC(double julianDay, double latitude, double
472460
double timeUTC = 720 + timeDiff - equationOfTime;
473461

474462
// Second pass includes fractional Julian Day in gamma calc
475-
double newt = getJulianCenturiesFromJulianDay(getJulianDayFromJulianCenturies(julianCenturies) + timeUTC
476-
/ 1440.0);
463+
double newt = getJulianCenturiesFromJulianDay(julianDay + timeUTC / 1440.0);
477464
equationOfTime = getEquationOfTime(newt);
465+
478466
solarDeclination = getSunDeclination(newt);
479467
hourAngle = getSunHourAngle(latitude, solarDeclination, zenith, solarEvent);
480468
delta = longitude - Math.toDegrees(hourAngle);
481469
timeDiff = 4 * delta;
482-
timeUTC = 720 + timeDiff - equationOfTime; // in minutes
470+
timeUTC = 720 + timeDiff - equationOfTime;
483471
return timeUTC;
484472
}
485473
}

0 commit comments

Comments
 (0)