Skip to content

Commit 8d312fe

Browse files
committed
firefly-277: update moving target name resolution to new horizons api
1 parent 72b009f commit 8d312fe

File tree

2 files changed

+40
-177
lines changed

2 files changed

+40
-177
lines changed

src/firefly/java/edu/caltech/ipac/astro/net/HorizonsEphPairs.java

Lines changed: 39 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -3,237 +3,100 @@
33
*/
44
package edu.caltech.ipac.astro.net;
55

6+
import edu.caltech.ipac.firefly.messaging.JsonHelper;
67
import edu.caltech.ipac.util.AppProperties;
78
import edu.caltech.ipac.util.StringUtils;
89
import edu.caltech.ipac.util.download.FailedRequestException;
910
import edu.caltech.ipac.util.download.URLDownload;
11+
import org.json.simple.JSONArray;
12+
import org.json.simple.JSONObject;
1013

11-
import java.io.BufferedReader;
1214
import java.io.IOException;
1315
import java.io.Serializable;
14-
import java.io.StringReader;
1516
import java.net.URL;
1617
import java.net.URLEncoder;
1718
import java.util.ArrayList;
18-
import java.util.HashMap;
19-
import java.util.Iterator;
2019
import java.util.List;
21-
import java.util.Map;
2220

23-
/**
24-
* @author Booth Hartley
25-
* @version $Id: HorizonsEphPairs.java,v 1.12 2012/03/13 22:23:02 roby Exp $
26-
*/
2721
public class HorizonsEphPairs {
2822

2923
public static final String horizonsServer = AppProperties.getProperty("horizons.host", "https://ssd.jpl.nasa.gov");
30-
31-
private static final String PARSE_ERROR= "Error Parsing Name Resolver results";
32-
33-
private static final String NAME_IDENT = "Object Name";
34-
private static final String ID_IDENT = "Primary SPKID";
35-
private static final String DES_IDENT = "Primary designation";
36-
private static final String ALIAS_IDENT = "Aliases";
37-
38-
// private static final String CGI_CMD= "/cgi-bin/smb_spk.cgi";
39-
private static final String CGI_CMD = "/x/smb_spk.cgi";
40-
41-
24+
public static final String path= "/api/horizons_lookup.api";
4225

4326
public static HorizonsResults[] lowlevelGetEphInfo(String idOrName) throws FailedRequestException {
4427

4528
boolean isName = true;
46-
HorizonsResults retval[] = null;
4729
try {
4830
Integer.parseInt(idOrName);
4931
isName = false;
50-
} catch (NumberFormatException e) {
51-
}
32+
} catch (NumberFormatException ignore) { }
5233

5334
if (isName) {
5435
idOrName = StringUtils.crunch(idOrName);
55-
String s[] = idOrName.split(" ");
36+
String[] s = idOrName.split(" ");
5637
if (s.length >= 2) {
5738
try {
5839
Integer.parseInt(s[0]);
5940
s = idOrName.split("^[0-9]* "); // now get the rest
6041
if (inParens(s[1])) {
6142
idOrName = stripFirstLast(s[1]);
6243
} else {
63-
char cAry[] = s[1].toCharArray();
44+
char[] cAry = s[1].toCharArray();
6445
boolean hasDigit = false;
6546
for (int i = 0; (i < cAry.length && !hasDigit); i++) {
6647
hasDigit = Character.isDigit(cAry[i]);
6748
}
6849
if (!hasDigit) idOrName = s[1];
6950
}
70-
} catch (NumberFormatException e) {
51+
} catch (NumberFormatException ignore) {
7152
}
7253
}
7354
}
7455

75-
Map<String,String> data = new HashMap<>();
7656

7757
try {
78-
data.put("OPTION", "Look up");
79-
data.put("OBJECT",URLEncoder.encode(idOrName));
80-
81-
String urlStr = horizonsServer + CGI_CMD;
58+
String urlStr = horizonsServer + path + "?sstr="+ URLEncoder.encode(idOrName, "UTF-8");
8259
URL url = new URL(urlStr);
83-
String line;
84-
String result = URLDownload.getDataFromURL(url, data,null,null).getResultAsString();
85-
BufferedReader rd = new BufferedReader(new StringReader(result));
86-
87-
88-
List<String> list = new ArrayList<String>(12);
89-
boolean error = false;
90-
while ((line = rd.readLine()) != null) {
91-
list.add(line);
92-
if (line.indexOf("ERROR") > -1) error = true;
60+
String jsonStrResult = URLDownload.getDataFromURL(url, null,null,null).getResultAsString();
61+
JsonHelper json= JsonHelper.parse(jsonStrResult);
62+
63+
JSONArray rList= json.getValue(new JSONArray(), "result");
64+
List<HorizonsResults> horizonsResults= new ArrayList<>();
65+
for(Object r : rList) {
66+
String pdes= (String)((JSONObject)r).get("pdes");
67+
String name= (String)((JSONObject)r).get("name");
68+
String naifID= (String)((JSONObject)r).get("spkid");
69+
JSONArray aList = (JSONArray)((JSONObject)r).get("alias");
70+
String[] aStrAry= (String[])aList.toArray(new String[0]);
71+
horizonsResults.add(new HorizonsResults(name,naifID,pdes,aStrAry));
9372
}
94-
rd.close();
95-
96-
97-
if (error) {
98-
StringBuffer errStrBuff = new StringBuffer(100);
99-
errStrBuff.append("<html>");
100-
for (String s : list) errStrBuff.append(s);
101-
throw new FailedRequestException("<html>" +
102-
"<br><br><b>ERROR:</b><br><br>" +
103-
"No matches found for: " + idOrName);
104-
}
105-
106-
retval = buildHorizonsResults(list);
73+
return horizonsResults.toArray(new HorizonsResults[0]);
10774
} catch (IOException e) {
108-
throw new FailedRequestException("failed to retrieve ephermeris pairs",
109-
"IOException in getting ephermeris info", e);
75+
throw new FailedRequestException("failed to retrieve ephemeris pairs",
76+
"IOException in getting ephemeris info", e);
11077
}
111-
return retval;
11278
}
11379

11480

115-
private static String appendAll(List l) {
116-
StringBuffer buff = new StringBuffer(100);
117-
for (Iterator i = l.iterator(); i.hasNext(); ) {
118-
buff.append(i.next().toString());
119-
buff.append("\n");
120-
}
121-
return buff.toString();
122-
}
123-
124-
125-
private static HorizonsResults[] buildHorizonsResults(List<String> list)
126-
throws FailedRequestException {
127-
128-
String label;
129-
String sAry[];
130-
String name = null;
131-
String id = null;
132-
String des = null;
133-
String alias = null;
134-
String aliases[] = null;
135-
List retList = new ArrayList(4);
136-
for (String s : list) {
137-
if (s.length() == 0) {
138-
139-
if (name == null || id == null || des == null || aliases == null) {
140-
throw new FailedRequestException(
141-
PARSE_ERROR,
142-
"One of the fields was null.\n" +
143-
"Results from query:\n" +
144-
appendAll(list));
145-
} else {
146-
if (StringUtils.isEmpty(name)) {
147-
name = StringUtils.isEmpty(des) ? "No Name*" : des;
148-
}
149-
retList.add(new HorizonsResults(name, id, des, aliases));
150-
}
151-
name = null;
152-
id = null;
153-
des = null;
154-
alias = null;
155-
} else {
156-
sAry = s.split("=");
157-
if (sAry.length != 2) {
158-
throw new FailedRequestException(
159-
PARSE_ERROR,
160-
"Missing a value pair combination\n" +
161-
"Results from query:\n" +
162-
appendAll(list));
163-
}
164-
165-
label = StringUtils.crunch(sAry[0]);
166-
if (label.equals(NAME_IDENT)) {
167-
name = StringUtils.crunch(sAry[1]);
168-
if (StringUtils.isEmpty(name)) {
169-
name = "";
170-
} else if (inParens(name)) {
171-
name = stripFirstLast(name);
172-
}
173-
} else if (label.equals(ID_IDENT)) {
174-
id = StringUtils.crunch(sAry[1]);
175-
} else if (label.equals(DES_IDENT)) {
176-
des = StringUtils.crunch(sAry[1]);
177-
} else if (label.equals(ALIAS_IDENT)) {
178-
alias = StringUtils.crunch(sAry[1]);
179-
if (alias.length() == 0) {
180-
aliases = new String[0];
181-
} else {
182-
aliases = alias.split(",");
183-
for (int k = 0; k < aliases.length; k++) {
184-
aliases[k] = aliases[k].trim();
185-
}
186-
}
187-
} else {
188-
// if not one of the four ignore this line
189-
}
190-
}
191-
} // end loop
192-
if (retList.size() == 0) {
193-
throw new FailedRequestException(
194-
PARSE_ERROR,
195-
"No error indicated but no results\n" +
196-
"Results from query:\n" +
197-
appendAll(list));
198-
}
199-
200-
return (HorizonsResults[]) retList.toArray(new HorizonsResults[0]);
201-
}
202-
20381

20482
public static class HorizonsResults implements Serializable {
205-
private final String _name;
206-
private final String _naifID;
207-
private final String _primaryDes;
208-
private final String _aliases[];
209-
210-
public HorizonsResults(String name, String naifID, String primaryDes,
211-
String aliases[]) {
212-
_name = name;
213-
_naifID = naifID;
214-
_primaryDes = primaryDes;
215-
_aliases = aliases;
216-
}
217-
218-
public final String getName() {
219-
return _name;
220-
}
221-
222-
public final String getNaifID() {
223-
return _naifID;
83+
private final String name;
84+
private final String naifID;
85+
private final String primaryDes;
86+
private final String [] aliases;
87+
88+
public HorizonsResults(String name, String naifID, String primaryDes, String[] aliases) {
89+
this.name = name;
90+
this.naifID = naifID;
91+
this.primaryDes = primaryDes;
92+
this.aliases = aliases;
22493
}
22594

226-
public final String getPrimaryDes() {
227-
return _primaryDes;
228-
}
229-
230-
public final String[] getAliases() {
231-
return _aliases;
232-
}
233-
234-
public String toString() {
235-
return _name + " " + _naifID + " " + _primaryDes;
236-
}
95+
public final String getName() { return name; }
96+
public final String getNaifID() { return naifID; }
97+
public final String getPrimaryDes() { return primaryDes; }
98+
public final String[] getAliases() { return aliases; }
99+
public String toString() { return name + " " + naifID + " " + primaryDes; }
237100
}
238101

239102

src/firefly/java/edu/caltech/ipac/astro/net/TargetNetwork.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static HorizonsEphPairs.HorizonsResults[] getEphInfo(String nameOrId) thr
4343
res= (HorizonsEphPairs.HorizonsResults[])objCache.get(params);
4444
if (res==null) {
4545
res= HorizonsEphPairs.lowlevelGetEphInfo(nameOrId);
46-
objCache.put(params,res,TWO_MONTHS);
46+
if (res.length>0) objCache.put(params,res,TWO_MONTHS);
4747
}
4848
return res;
4949
}

0 commit comments

Comments
 (0)