Calculating the Bearing/Direction to Har Habayis Using the Zmanim API

Vintage Map with CompassAn earlier “Bearing to Yerushalayim and Zmanim Map” post demonstrated the use of JavaScript to render the bearing to Har Habayis on a Google Map. A more detailed follow-up post “Technical Information about the Bearing to Yerushalayim Map” dealt with detailed technical information on these calculations. The main Bearing to Yerushalayim and Zmanim Map page usually has the most up to date information on the subject. What was not detailed in previously published posts and pages was that most of the calculations available via JavaScript are now in the core Zmanim API. Available since the July, 2008 beta 2 release of version 1.1 is the ability to bearings/directions using both the great circle and rhumb line methods in Java. The GeoLocation Object was modified to calculate the great circle bearings (both initial and final), and rhumb line bearing from any GeoLocation Object to another. In addition, distance calculation between the two points using both of these line types is supported. What was not ported from the JavaScript version was the less accurate Haversine formula, or the simpler spherical law of cosines algorithms that yield identical results. Instead, the Zmanim API uses the far more accurate Vincenty formulae using the WGS84 geoid model of the earth. Published by the geodesist/mathematician Thaddeus Vincenty, it is said to be accurate to about one-half millimeter, more than adequate for our calculation. The code in the API is a Java port of the previously published, slightly modified version of Chris Veness’s JavaScript implementation . Below is a simple Java example of generating bearing and distances.

/**
 * This program demonstrates how to calculate bearing to Yerushalayim
 * using the kosherjava.com Zmanim API. Both the great circle and
 * rhumb line method are shown
 * To compile, ensure that the Zmanim Jar is in your classpath.
 */
import com.kosherjava.zmanim.util.GeoLocation;
import java.util.TimeZone;

public class BearingToYerushalayim{
	public static void main(String [] args) {
		GeoLocation lakewood = new GeoLocation("Lakewood, NJ", 40.09596, -74.22213, 0, TimeZone.getTimeZone("America/New_York"));
		GeoLocation harHabayis = new GeoLocation("Har Habayis", 31.77805, 35.235149, 0, TimeZone.getTimeZone("Asia/Jerusalem"));

		double greatCircleInitialBearing = lakewood.getGeodesicInitialBearing(harHabayis);
		double greatCircleDistance = lakewood.getGeodesicDistance(harHabayis);

		double rhumbLineBearing = lakewood.getRhumbLineBearing(harHabayis);
		double rhumbLineDistance = lakewood.getRhumbLineDistance(harHabayis);

		System.out.println("Great circle initial bearing: " + greatCircleInitialBearing + " degrees ");
		System.out.println("Great circle distance: " + greatCircleDistance / 1000 + " km");

		System.out.println("Rhumb line bearing: " + rhumbLineBearing + " degrees");
		System.out.println("Rhumb line distance: " + rhumbLineDistance / 1000 + " km");

	}
}

ZmanimCLI (Command Line Interface)

Java Logo SepiaMoshe Wagner who wrote the Zmanim GUI notified me in August that that he created a command line interface for zmanim using my Zmanim API. The technical approach of using reflection was similar to the way I used reflection in the Zmanim Clock Applet, but he took it to new heights. Sample use of accessing zmanim using his CLI interface is:

moshe@debian:~/Desktop$ java -jar ZmanimCLI.jar sunrise
6:10:28
moshe@debian:~/Desktop$ java -jar ZmanimCLI.jar --date 2010/08/12 tzais72
20:38:15
moshe@debian:~/Desktop$ java -jar ZmanimCLI.jar
Usage: ZmanimCLI [options] [Time]

Options:
       -d      --date <yyyy/mm/dd>             Set date. (Year first!)
       -lat    --latitude <latitude>           Set location's latitude
       -lon    --longitude <longitude>         Set location's longitude
       -e      --elevation <elevation>         Set location's
elevation; Positive only
       -tz     --timezone <timezone>           Set location's TimeZone

Help:
       -h      --help                          Show this help
       -stl    --time-list                     Show common available
times to display
       -ftl    --full-time-list                Show all available
times to display
       -tzl    --timezone-list                 Show available timezones

Example:
       ZmanimCLI --latitude 31.7780 --longitude 35.235149 --elevation
600 --timezone Israel Sunrise
       Will show the sunrise time today in Jerusalem

While your first reaction may be that it is interesting in a theoretical geeky way, but has no practical value, I will quote Moshe’s explanation as to why it is useful:

Why is this useful? Well, first of all it was a nice experiment. But mainly, you can now use Zmanim (although externally), via any language you want, no longer being tied to Java.

Months later, Moshe actually put this to practical use in his C++ based Luach project. This Luach (similar to the known Kaluach) uses the Qt framework. utilizing libhdate for the date stuff (something not offered by the Zmanim API, and the topic of a future Zmanim API FAQ), displaying zmanim using the Zmanim API via CLI for the zmanim calculations. While you would expect such an approach to be slow, using the Luach seemed almost instantaneous. I will post more about his Luach program (recently reviewed at KosherDev.com) at some point in the future.

FAQ: Where is the Zmanim API Main Method?

KosherJava Zmanim API FAQ

Question:

Where is the main method?

Answer:

This is a more technical variant of the “How do I install the Zmanim API Program?”, but coming from someone who already knows that it is a Java program that can’t be installed, but assumes that it can be run. The main method is the entry point to a Java program. Since this is a library/API and not a program, it does not have a main method. The code to generate zmanim is spelled out in the How to Use the Zmanim API page. Below is a full example of a very simple zmanim program that outputs sunrise, sof zman krias shema and sunset for the current day in Lakewood, NJ. Please ensure that the Zmanim jar (download) is in your classpath.

/**
 * This program is a simple demonstration of the kosherjava.com Zmanim API.
 * To compile, ensure that the Zmanim Jar is in your classpath.
 */
import com.kosherjava.zmanim.*;
import com.kosherjava.zmanim.util.*;
import java.util.TimeZone;
public class SimpleZmanim{
	public static void main(String [] args) {
		String locationName = "Lakewood, NJ";
		double latitude = 40.096; //latitude of Lakewood, NJ
		double longitude = -74.222; //longitude of Lakewood, NJ
		double elevation = 0; //optional elevation
		//use a Valid Olson Database timezone listed in java.util.TimeZone.getAvailableIDs()
		TimeZone timeZone = TimeZone.getTimeZone("America/New_York");
		//create the location object
		GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, timeZone);
		//create the ZmanimCalendar
		ZmanimCalendar zc = new ZmanimCalendar(location);
		//optionally set the internal calendar. If not set it will default to the current date
		//zc.getCalendar().set(1969, Calendar.FEBRUARY, 8);
		System.out.println("Today's Zmanim for " + locationName);
		System.out.println("Sunrise: " + zc.getSunrise()); //output sunrise
		System.out.println("Sof Zman Shema GRA: " + zc.getSofZmanShmaGRA()); //output Sof Zman Shema GRA
		System.out.println("Sunset: " + zc.getSunset()); //output sunset
	}
}

The following would compile and execute this code (sample from a DOS prompt in Windows).

C:\path\to\code>javac SimpleZmanim.java

C:\path\to\code>java SimpleZmanim

Today's Zmanim for Lakewood, NJ
Sunrise: Thu Nov 05 06:30:27 EST 2009
Sof Zman Shema GRA: Thu Nov 05 09:05:21 EST 2009
Sunset: Thu Nov 05 16:50:02 EST 2009

Please see the Zmanim API documentation for a more complete view of the API.

Zmanim Calendar Generator Can Now Output Fewer Zmanim

Sunrise Calendar>After numerous requests, the Zmanim Calendar Generator can now output a less extensive list of zmanim. While the “full” calendar option (the default) generates an Excel spreadsheet with 108 columns of zmanim, the “standard” output generates a spreadsheet with 15 columns containing the most commonly used zmanim. The exact list of what constitutes commonly used zmanim is likely to be tweaked over time. The spreadsheet was initially designed for developers as a sample of the various zmanim available in the API. Developers can compare the output of their zmanim using the API, or a ported version of the API to the spreadsheet for accuracy. Based on feedback, many people use it to generate shul calendars and wanted a less daunting list of zmanim.

Zmanim API 1.1 Released


The Zmanim API 1.1 was released early this morning. Information about what changed in this release can be seen in previous posts about various beta and patch releases. A last-minute change involved the removal of the misheyakir calculations commonly used by the Syrian community. The removal was due to the various different minhagim used, and Chacham Yosef Harari-Raful not endorsing any specific one, nor does he include any in his calendar. The API is flexible enough to be used for any calculation wanted by the various Syrian shuls even without “native” support for a built in “Ateret Torah” misheyakir. Some missing JavaDocs were also added.

I would like to again thank Rabbi Rachamim Ashkenazi the publisher of a zmanim calendar for the Syrian Community, and Victor Grazi for his input, testing and technical expertise used for adding the new “Ateret Torah” zmanim.

The main download is the Zmanim 1.1 release zip file that includes source files and JavaDoc documentation. Also available for download (included in the above zip file) is the main zmanim-1.1.jar and the new zmanimAstronomical-1.1.jar that only includes the AstronomicalCalendar and supporting classes. Additional detail on the downloads can be seen on the Zmanim Download page.