00001 package cedar.jetweb.xml; 00002 00003 //import java.io.*; 00004 00005 import org.jdom.Element; 00006 import org.jdom.Document; 00007 import org.jdom.JDOMException; 00008 import org.jdom.input.SAXBuilder; 00009 00010 import java.util.List; 00011 import java.util.Iterator; 00012 import java.util.StringTokenizer; 00013 import java.util.Vector; 00014 import java.util.Collection; 00015 00016 import cedar.jetweb.model.plots.DataPoint; 00017 import cedar.jetweb.model.plots.DataPlot; 00018 import cedar.jetweb.model.plots.PredictedPlot; 00019 import cedar.jetweb.JetWebException; 00020 00021 00030 public class JetWebPlotMLReader extends JetWebXMLReader implements JetWebPlotReader{ 00031 00032 private String filename; 00033 private Element soloPlot; 00034 private String title; 00035 private String xlabel; 00036 private String ylabel; 00037 private boolean isLogarithmic = false; 00038 private Vector<DataPoint> dataPoints = new Vector<DataPoint>(); 00039 00044 public static void main(String args[]){ 00045 try { 00046 JetWebPlotMLReader tester = new JetWebPlotMLReader(args[0]); 00047 System.out.println("test title " + tester.getTitle()); 00048 System.out.println("x label " + tester.getXLabel()); 00049 System.out.println("y label " + tester.getYLabel()); 00050 Collection<DataPoint> points=tester.getDataPoints(); 00051 int i=0; 00052 for (DataPoint point : points) { 00053 i++; 00054 System.out.println("point"+i+ point.toString()); 00055 } 00056 } catch (Throwable t) { 00057 System.out.println("Error reading XML"); 00058 } 00059 } 00063 public JetWebPlotMLReader(String filename) throws JetWebXMLException { 00064 super(filename); 00065 populateSoloPlot(); 00066 } 00070 private void populateSoloPlot(){ 00071 Document doc = getDocument(); 00072 if (soloPlot==null){ 00073 soloPlot = doc.getRootElement().getChild("plot"); 00074 } 00075 if (soloPlot==null){ 00076 System.out.println("Error reading plotfile - no plot"); 00077 } else { 00078 //extract plot title 00079 title=soloPlot.getChild("title").getChild("label").getAttributeValue("text"); 00080 //extract axes titles 00081 List<Element> axesList=soloPlot.getChild("dataArea").getChildren("axis"); 00082 Iterator<Element> axes = axesList.iterator(); 00083 while (axes.hasNext()){ 00084 Element axis = axes.next(); 00085 String axisLabel=axis.getChild("label").getAttributeValue("text"); 00086 String axisType=axis.getAttributeValue("location"); 00087 if (axisType.equals("x0")){ 00088 xlabel=axisLabel; 00089 } else { 00090 ylabel=axisLabel; 00091 String isLog = axis.getAttributeValue("logarithmic"); 00092 if(isLog.compareTo("true")==0) { 00093 isLogarithmic = true; 00094 }else { 00095 isLogarithmic = false; 00096 } 00097 } 00098 } 00099 //populate a vector of data points for the plot 00100 populateDataPoints(); 00101 00102 } 00103 } 00115 private void populateDataPoints(){ 00116 List<Element> pointsList = soloPlot.getChild("dataArea").getChildren("data1d"); 00117 Iterator<Element> points = pointsList.iterator(); 00118 try { 00119 while (points.hasNext()){ 00120 Element point = points.next(); 00121 Element axisAttr = point.getChild("binnedDataAxisAttributes"); 00122 double numberOfBins = Double.parseDouble(axisAttr.getAttributeValue("numberOfBins")); 00123 double min = Double.parseDouble(axisAttr.getAttributeValue("min")); 00124 double max = Double.parseDouble(axisAttr.getAttributeValue("max")); 00125 double binwidth = (max - min) / numberOfBins; 00126 00127 //have to decode this string into points and errors 00128 String[][] hdata = decodeData(point.getChildTextTrim 00129 ("bins1d")); 00130 for (int i=0;i<hdata[0].length;i++){ 00131 00132 DataPoint pointA = new DataPoint(); 00133 pointA.setX(binwidth*i + 0.5*binwidth + min); 00134 pointA.setY(Double.parseDouble(hdata[0][i])); 00135 pointA.setYUp(Double.parseDouble(hdata[1][i])); 00136 pointA.setYDown(pointA.getYDown()); 00137 pointA.setBinWidth(binwidth); 00138 /* 00139 double[] pointA = new double[6]; 00140 pointA[DataPoint.X]=binwidth*i + binwidth/2.0 +min; 00141 pointA[DataPoint.Y]=Double.parseDouble(hdata[0][i]); 00142 pointA[DataPoint.YUP]=Double.parseDouble(hdata[1][i]); 00143 pointA[DataPoint.YDOWN]=pointA[2]; 00144 pointA[DataPoint.BINWIDTH]=binwidth; 00145 00146 dataPoints.add(new DataPoint(pointA)); 00147 */ 00148 dataPoints.add(pointA); 00149 } 00150 } 00151 } catch (Exception e){ 00152 System.out.println("Exception in populate data points " + e); 00153 e.printStackTrace(System.out); 00154 } 00155 } 00164 private String[][] decodeData(String data){ 00165 00166 StringTokenizer tokLines = new StringTokenizer(data,"\n"); 00167 String[][] pointsA = new String[3][tokLines.countTokens()]; 00168 int i = 0; 00169 while (tokLines.hasMoreTokens()){ 00170 StringTokenizer tokCommas = new StringTokenizer(tokLines.nextToken(),","); 00171 pointsA[0][i]=tokCommas.nextToken(); 00172 pointsA[1][i]=tokCommas.nextToken(); 00173 if (tokCommas.countTokens()==3){ 00174 pointsA[2][i]=tokCommas.nextToken(); 00175 } 00176 i++; 00177 } 00178 return pointsA; 00179 } 00181 public String getTitle(){ 00182 return title; 00183 } 00185 public String getXLabel(){ 00186 return xlabel; 00187 } 00189 public String getYLabel(){ 00190 return ylabel; 00191 } 00201 private Collection<DataPoint> getDataPoints(){ 00202 return dataPoints; 00203 } 00204 public void fillPlot(DataPlot plot) throws JetWebException { 00205 //System.out.println("fillplot called. title= " + title); 00206 plot.setTitle(title); 00207 plot.setXLabel(xlabel); 00208 plot.setYLabel(ylabel); 00209 plot.setLogarithmic(isLogarithmic); 00210 plot.setDataPoints(dataPoints); 00211 if (plot instanceof PredictedPlot){ 00212 PredictedPlot.adjustErrors((PredictedPlot)plot); 00213 } 00214 plot.setHasData(true); 00215 } 00216 00217 public void setDirectory(String dir) throws JetWebException { 00218 throw new JetWebException("JetWebPlotMLReader","setDirectory not implemented"); 00219 } 00220 00221 } 00222 00223 00224 00225 00226 00227
Generated Wed Jan 17 09:14:27 GMT 2007