Science With the Virtual Observatory |
The stubbed portion of ConeCaller.java that we need
to add to is in the main() method; it looks like this:
if (query != null) {
// search the registry for ConeSearch services
}
We can follow a bit of the pattern we used in
FindConeSearch.java's main() to query the
Registry. So to start, we can insert into the above if-block the
following:
SimpleResource[] cs = FindConeSearch.keywordSearch(query);
if (cs == null || cs.length == 0) {
System.err.println("No matching cone search services found");
}
else {
System.out.println("==================================");
for(int i=0; i < cs.length; i++) {
// print out the title, URL, & number of records
// returned by the cone search
}
}
The for loop steps through the matched service
descriptions from which we need to extract the baseURL. We need to
remember to fix any messy URLs, we can insert as the first step within
the loop, the fix:
String baseURL =
FindConeSearch.correctBaseURL(cs[i].getServiceURL());
Next we'll print out some some info from the resource description:
System.out.println("Title: " + cs[i].getTitle());
System.out.println("Address: " + baseURL);
As for submitting a Cone Search and counting the results, let's put that into a separate function.
int nr = countHits(ra,dec,sr, baseURL);
System.out.println("Matching catalog records found: " +
nr);
System.out.println("==================================");
Our first implementation of countHits() might look
pretty simple, borrowing from the readVot() function:
public static int countHits(double ra, double dec, double sr,
String coneUrl)
throws Exception
{
URL cone = new URL(coneUrl + "RA="+ra+"&DEC="+dec+"&SR="+sr);
VOTWrap.VOTable vot = VOTWrap.createVOTable(cone.openStream());
VOTWrap.TableData tdata =
vot.getResource(0).getTable(0).getTableData();
return tdata.getTRCount();
}
However, there are lots of things that can go wrong, so we better
try to catch some of the errors with a try-catch
enclosure:
public static int countHits(double ra, double dec, double sr,
String coneUrl)
throws Exception
{
try {
URL cone = new URL(coneUrl + "RA="+ra+"&DEC="+dec+"&SR="+sr);
VOTWrap.VOTable vot = VOTWrap.createVOTable(cone.openStream());
VOTWrap.TableData tdata =
vot.getResource(0).getTable(0).getTableData();
return tdata.getTRCount();
}
catch ( ) {
}
}
We'll catch three possible errors. First, despite our effort to clean up the URL, it may still be erroneous:
catch (MalformedURLException ex) {
System.err.println("Problem with URL: " + coneUrl);
return 0;
}
Next, the server may be down...
catch (ConnectException ex) {
System.err.println("Service not responding.");
return 0;
}
Finally, we'll catch any other possible problems:
catch (Exception ex) {
System.err.println("Unknown service error.");
return 0;
}
The most common reason for this last error condition is due to the
VOTable not having a TABLE in it (causing a
NullPointerException). Instead the VOTable only contains
an error message. There is a standard for encoding errors, so in
principle we could (and should) pull this out and display
it. Unfortunately, very few Cone Search implementations follow the
standard correctly.
Our implementation is mostly complete. To get our
countHits() function to compile not that we have put in
the catching of exceptions, we need to remove the throws
clause in the signiture:
public static int countHits(double ra, double dec, double sr,
String coneUrl)
{
try {
URL cone = new URL(coneUrl + "RA="+ra+"&DEC="+dec+"&SR="+sr);
VOTWrap.VOTable vot = VOTWrap.createVOTable(cone.openStream());
VOTWrap.TableData tdata =
vot.getResource(0).getTable(0).getTableData();
return tdata.getTRCount();
}
catch (MalformedURLException ex) {
System.err.println("Problem with URL: " + coneUrl);
return 0;
}
catch (ConnectException ex) {
System.err.println("Service not responding.");
return 0;
}
catch (Exception ex) {
System.err.println("Unknown service error.");
return 0;
}
}
Ahh. Don't you feel safer now?
The NVO Summer School is made possible through the support of the National Science Foundation and the National Aeronautics and Space Administration.
![]() |