Zmanim API 1.1 Beta 2 Released

Note: This post is about an old beta release. The current version of the KosherJava Zmanim API is available on GitHub and from the Zmanim Download page.

The second beta release of the Zmanim API 1.1 is now available on the download page. Changes in this release include additional code refactoring, building on the refactoring done in the last few releases. As in the previous release, included is the zmanimAstronomical-1.1_beta_2.jar, a release that only includes the AstronomicalCalendar class and supporting classes. As part of the changes, an effort was put in to make the code simpler to port to other languages. This mostly involved moving formatting code out of the main classes. The only interface changes were the addition of a few new methods to the GeoLocation class. It is likely that the next release will remove the empty GeoLocation constructor that can lead to inadvertent errors. This removal might break existing code. I hope to post details about these changes in the near future.

Zmanim Bug Report from the Land of the Midnight Sun

I was recently contacted by Jan Terje Johansen, a developer at Datek Wireless AS in Norway, with an interesting bug report. Datek uses the Zmanim API (the AstronomicalCalendar base class) to allow their clients (the power company and stadiums) to remotely (via a web interface) control streetlights and stadium lights throughout Norway using a wireless lighting control system that they developed. The zmanim code is used to allow setting the lighting times based on an offset of sunrise/sunset. For the technically curious, they are controlled primarily through GPRS, with SMS as fallback. A version under development uses ZigBee. The bug encountered was that for Tromsoe (Tromsø), Norway, as well as other areas within the Arctic Circle that experience the midnight sun, from May 13 to May 17 (the date of last rise of the season in Tromsoe), the Zmanim API produced correct sunrise/sunset times, but the date component was a day off (the API returned all times as Java Dates at the time; version 3.0 of the API moved to the java.time API of the JSR-310 Date and Time API introduced in Java 8). For non-automated systems, the date component is not important, but in their case it would cause the lights to go on/off on the wrong day. Jan provided a suggested patch that worked well. The actual fix I used was slightly different because I took advantage of the time spent on fixing the bug to refactor and simplify the code. This change as well as a few other changes are part of the Zmanim 1.1 beta release that will likely be released as a final release in a few days. Jan mentioned that:

“IMHO your API is easily the best and most accurate Open Source sunset/sunrise API out there”. He continued: “Officially (according to the Norwegian Meteorology Institute), the midnight sunset/sunrise is from May 20 to July 22 in Tromsoe, ie. the complete sun is above the horizon 24 hours. Parts of sun is visible 24 hours a day in Tromsoe from May 18th to July 25th. This is the same as in your calculations.” … “I have tested your calculations against other official midnight sunrise/sunset (part of sun) dates in Northern-Norway (North Cape, Hammerfest, Longyearbyen (78,049762N -15,458252E [sic; Longyearbyen is at about 78.22, 15.65])) and they are spot on.”

In response to my question regarding his testing of the NOAACalculator versus the USNO-based SunTimesCalculator, he had an interesting and very practical answer:

“We prefer to use USNO calculator as it is more in tune with the sunrise/sunset times printed in most newspapers. You see, our experience is that most users don’t look at the sun to determine sunrise/sunset but read the times in the newspaper. If our times don’t correspond to the printed ones, something is wrong with our system in their mind.”

Zmanim API 1.1 Release Candidate Available

Note: This post is about an old release candidate. The RC1 files are no longer available; the links below point to the final 1.1 release (see the Zmanim API 1.1 Released post). The current version of the KosherJava Zmanim API is available on GitHub and from the Zmanim Download page.

A release candidate (RC1) of the Zmanim API 1.1 is now available on the download page. Changes in this release include a slight cleanup of the recently released NOAACalculator code (it does not change calculated times), as well as fixes to the date (but not time) of calculations for locations near the Arctic Circle. This date fix builds on the February release of the API to fix an Arctic Circle issue and a similar issue encountered when trying to generate zmanim for locations outside the local time zone. Also included in this release is the zmanimAstronomical-1.1.jar, a release that only includes the AstronomicalCalendar class and supporting classes. There was also some code refactoring to make the code easier to maintain. A detailed post will follow (hopefully within the next week or so).

Fix to NOAA Sunrise/Sunset Algorithm

Update: The NOAACalculator introduced in this post has since become the Zmanim API’s default calculator. The current version also includes the SPACalculator and MeeusCalculator, alongside the USNO-based SunTimesCalculator.

The Zmanim API was developed from the ground up as an API which allows for easy plugging in of different algorithms. The Zmanim API ships with three “Calculator” implementations. Two calculators implement the US Naval Observatory’s algorithm, the SunTimesCalculator and the ZmanimCalculator (no longer available). Both produce identical zmanim using slightly different code and are included for comparison. There is also the JSuntimeCalculator (no longer available – see the information on the NOAACalculator below), an implementation of the NOAA algorithm by Jonathan Stott. I was recently contacted by Eliezer Bulka, who wanted to know why sunrise/sunset times generated by the NOAA algorithm were about 2 minutes off from the sunrise/sunset times generated by the NOAA JavaScript implementation that is the source of the JSuntimeCalculator. Comparing apples to apples required modifying the NOAA JavaScript to allow entry of decimal latitude/longitude and changing the output to display seconds. No change was made to the algorithm itself. I then ported the JavaScript directly to Java. This involved nothing more than slight syntax changes between the languages. Once this was done, I noticed that the sunrise/sunset output from the Java port exactly matched the output of the NOAA JavaScript. Analysis of Jonathan’s code showed (or at least my interpretation of it did) that there were two areas that caused the difference. One is that he used a slightly different method of computing the Julian date, a key part of the algorithm. His change includes the time of day as part of the calculation. The net result of this change is that solar time generated using his algorithm varies based on the time of day the calculation is run, something that is incorrect. This means that there can be a discrepancy of up to one calendar day. If the user calculated sunrise at 11:59 PM, sunrise would be calculated for the following day even if the user attempted to calculate it for today. In addition, the other calculations do not match the output of the matching NOAA code. I have deprecated the JSuntimeCalculator and in its place added the NOAACalculator that was the result of the direct port of the NOAA code, shoehorned into the Zmanim API Calculator interface. I ran some tests to compare the maximum and minimum discrepancy between the two implementations, and calculations for Lakewood, NJ (latitude 40.0828, longitude -74.2094) show a discrepancy of between 1 minute 34 seconds and 1 minute 37 seconds for sunrise and sunset across an entire year of calculations. I also compared the USNO algorithm to the new NOAA implementation and ended up with a maximum deviation of less than 30 seconds, where they had previously been about 1.5 minutes apart. While I do believe that the Julian date calculation is a bug, I do not know that this is the case for the rest of the calculation, but it is clear that it does not match the NOAA implementation that it was based on, and I recommend that you download the latest version that has the new NOAACalculator that fixes this issue. In addition to this fix, an additional patch will be released later this week that will address issues with calculations within the Arctic Circle. Stay tuned for the next post.

Technical Information about the Bearing to Yerushalayim Map

Vintage Map with CompassI have been asked a number of questions recently about the directional accuracy of the Bearing to Yerushalayim and Zmanim Map calculations. The rhumb line calculations are very straightforward, and I will not spend much time on them in this article. As a side note, for those interested in the subject, I would like to mention that Rabbi Gavriel Goetz recently published a very comprehensive pamphlet, Gevuras Moishe, on the subject, and it is well worth reading.

My original calculations and implementation of the great circle route (geodesic line) used simple spherical trigonometry to calculate the initial bearing (and distance) based on a sphere using an authalic mean radius of 6,371 km. These calculations were very similar to the method used in Rabbi Yehuda Herskowitz’s article in Yeshurun volume III page 586. This model of the earth is more than accurate enough for these calculations (you would need a very accurate “nose alignment” and the total lack of shuckling to even get within a degree of being correct). The Google Maps API, Yahoo Maps and Microsoft’s Live Maps, for simplicity’s sake, all use very similar calculations, as mentioned in the cfis blog quoting Morten Nielsen:

Google Maps / Virtual Earth / Yahoo Maps(?) all use a spherical datum based on WGS84. That is, it has the same center, orientation and scale as WGS84, but has no flattening. The radius of the sphere is the same as the semi-major axis of WGS84 (6378137 meters).

Using a sphere, all the above-mentioned mapping APIs use the spherical law of cosines formula for the calculations, which yields an identical result to the more complex haversine formula.

Curious how accurate these calculations really are, being based on a perfect sphere and not the oblate ellipsoid/spheroid that the earth actually is, I started looking into the subject a little more. The current standard reference ellipsoid model of the earth is the WGS84 model. The geodesist/mathematician Thaddeus Vincenty published the Vincenty formulae for this type of calculation, which are said to be accurate to about one-half millimeter, more than adequate for our calculation. Chris Veness implemented this formula in JavaScript and released it under the LGPL, making it very simple to use. I slightly modified his work to allow easier interaction with the Google Maps API. The implementation that I used when I finally published the map on December 30, 2007, uses the Vincenty formula with the WGS84 ellipsoid (the Vincenty formula can easily be used with different ellipsoid models). The table below compares the initial bearing and distance results of the different calculations for a Lakewood, NJ (latitude: 40.095965°, longitude: -74.22213°) to Har Habayis (latitude: 31.77805°, longitude: 35.235149°) example.

Calculation MethodInitial BearingDistance
6,371 km (authalic mean radius) sphere using spherical trigonometry53.86555°9224.67442 km
Google Maps API 6,378.137 km (WGS84 equatorial radius) sphere using spherical trigonometryN/A9235.00819 km
Vincenty formula using the WGS84 ellipsoid53.81786°9244.61686 km
rhumb line95.37152°9891.16074 km
Note: The table shows the original 2008 JavaScript results. Running the same calculations with version 3.1.0 of the KosherJava Zmanim API gives a Vincenty initial bearing of 53.81787° and distance of 9244.61720 km, and a rhumb line distance of 9891.16103 km. The distances differ by less than a meter, a result of small differences between the JavaScript and Java code.