View Javadoc

1   /*
2     The TauP Toolkit: Flexible Seismic Travel-Time and Raypath Utilities.
3     Copyright (C) 1998-2000 University of South Carolina
4   
5     This program is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public License
7     as published by the Free Software Foundation; either version 2
8     of the License, or (at your option) any later version.
9   
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  
19    The current version can be found at 
20    <A HREF="www.seis.sc.edu">http://www.seis.sc.edu</A>
21  
22    Bug reports and comments should be directed to 
23    H. Philip Crotwell, crotwell@seis.sc.edu or
24    Tom Owens, owens@seis.sc.edu
25  
26  */
27  
28  package edu.sc.seis.TauP;
29  
30  import java.applet.Applet;
31  import java.awt.BorderLayout;
32  import java.awt.Button;
33  import java.awt.CardLayout;
34  import java.awt.Choice;
35  import java.awt.GridBagConstraints;
36  import java.awt.GridBagLayout;
37  import java.awt.GridLayout;
38  import java.awt.Insets;
39  import java.awt.Label;
40  import java.awt.Panel;
41  import java.awt.TextArea;
42  import java.awt.TextField;
43  import java.awt.event.ActionEvent;
44  import java.awt.event.ActionListener;
45  import java.awt.event.ItemEvent;
46  import java.awt.event.ItemListener;
47  import java.io.IOException;
48  import java.io.InputStream;
49  import java.io.InvalidClassException;
50  import java.io.StreamTokenizer;
51  import java.io.StringWriter;
52  import java.lang.reflect.Constructor;
53  import java.lang.reflect.InvocationTargetException;
54  import java.net.URL;
55  import java.util.zip.ZipEntry;
56  import java.util.zip.ZipInputStream;
57  
58   
59  /*** Simple applet to run TauP tools. 
60   * 
61   *
62   * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
63  
64  
65  
66   * @author H. Philip Crotwell
67   */
68  public class TauPApplet extends Applet implements ActionListener, ItemListener {
69      Choice getModel;
70      Choice toolChoice;
71      TauP_Time tool;
72      TauModel tMod;
73      Panel inputPanel;
74      TextField modelField, depthField, distanceField, phasesField;
75      TextArea textArea;
76      PolarPlot plotArea;
77      CardLayout outputCards;
78      Panel outputPanel;
79      Button calculate;
80      Button textOrPlot;
81      Button clearTextArea;
82      String newline;
83      String modelName, phases = "";
84      double distance, depth;
85      StringWriter result = new StringWriter();
86  
87      protected void resetInputPanel() {
88  	String modelName = getModel.getSelectedItem();
89  
90  	inputPanel.removeAll();
91  
92  	//Add Components to the Applet. 
93  	Label l;
94  	l = new Label("Model Name", Label.CENTER);
95  	inputPanel.add(l);
96  	l = new Label("Source Depth", Label.CENTER);
97  	inputPanel.add(l);
98  	l = new Label("Distance", Label.CENTER);
99  	inputPanel.add(l);
100 	l = new Label("Phase List", Label.CENTER);
101 	inputPanel.add(l);
102 
103 	modelField = new TextField(modelName,5);
104 	modelField.setEditable(false);
105 	inputPanel.add(modelField);
106 	depthField = new TextField("0.0",5);
107 	inputPanel.add(depthField);
108 	distanceField = new TextField("10.0",5);
109 	inputPanel.add(distanceField);
110 	phasesField = new TextField("P,S,PKP,SKS",5);
111 	inputPanel.add(phasesField);
112 	inputPanel.validate();
113     }
114 
115     public void init() {
116 
117 	GridBagLayout gridbag = new GridBagLayout();
118 	setLayout(gridbag);
119 	GridBagConstraints constraints = new GridBagConstraints();
120 	constraints.gridwidth = GridBagConstraints.REMAINDER;
121 	constraints.weightx = 1.0;
122 	constraints.insets = new Insets(4,4,5,5);
123 
124 	Panel choicePanel = new Panel();
125 	toolChoice = new Choice();
126 	toolChoice.addItem("TauP_Time");
127 	toolChoice.addItem("TauP_Pierce");
128 	toolChoice.addItem("TauP_Path");
129 	toolChoice.addItem("TauP_Curve");
130 	toolChoice.select(0);
131 	toolChoice.addItemListener(this);
132 	choicePanel.add(new Label("Tool"));
133 	choicePanel.add(toolChoice);
134 	getModel = new Choice();
135 	choicePanel.add(new Label("Choose model."));
136 	choicePanel.add(getModel);
137 	getModel.addItemListener(this);
138 	gridbag.setConstraints(choicePanel, constraints);
139 	add(choicePanel);
140 
141 	inputPanel = new Panel();
142 	inputPanel.setLayout(new GridLayout(0,4));
143 	constraints.weighty = 0.0;
144 	gridbag.setConstraints(inputPanel, constraints);
145 	add(inputPanel);
146 	resetInputPanel();
147 
148 	calculate = new Button("Calculate");
149 	calculate.addActionListener(this);
150 	clearTextArea = new Button("Clear");
151 	clearTextArea.addActionListener(this);
152 
153 	textOrPlot = new Button("Plot");
154 	textOrPlot.addActionListener(this);
155 		
156 	constraints.weighty = 0.0;
157 	constraints.gridwidth = 1;
158 	add(clearTextArea);
159 	add(textOrPlot);
160 	constraints.gridwidth = GridBagConstraints.REMAINDER;
161 	gridbag.setConstraints(calculate, constraints);
162 	add(calculate);
163 
164 	outputPanel = new Panel();
165 	outputCards = new CardLayout();
166 	outputPanel.setLayout(outputCards);
167 		
168 	textArea = new TextArea();
169 	textArea.setEditable(false);
170 	outputPanel.add(textArea, "Text");
171 	outputCards.first(outputPanel);
172 
173 	Panel plotPanel = new Panel();
174 	plotPanel.setLayout(new BorderLayout());
175 		
176 	Panel buttonPanel = new Panel();
177 	buttonPanel.setLayout(new GridLayout(0,1));
178 	Button fullButton = new Button("Full");
179 	fullButton.addActionListener(this);
180 	buttonPanel.add(fullButton);
181 	Button halfButton = new Button("Half");
182 	halfButton.addActionListener(this);
183 	buttonPanel.add(halfButton);
184 	Button quarterButton = new Button("Quarter");
185 	quarterButton.addActionListener(this);
186 	buttonPanel.add(quarterButton);
187 	plotPanel.add(buttonPanel, BorderLayout.WEST);
188 		
189 	plotArea = new PolarPlot(plotPanel, 250);
190 	plotPanel.add(plotArea);
191 	outputPanel.add(plotPanel, "Plot");
192 
193 	constraints.weighty = 1.0;
194 	constraints.fill = GridBagConstraints.BOTH;
195 	gridbag.setConstraints(outputPanel, constraints);
196 	add(outputPanel);
197 
198 	findTauModel();
199 	if (getModel.getItemCount() > 0) {
200 	    getModel.select(0);
201 	    getModel.select("prem");
202 	}
203 
204 	validate();
205 		
206 	newline = System.getProperty("line.separator");
207     }
208 
209     public void loadTauModel(String modelName) throws IOException, InvalidClassException {
210 	try {
211 	    InputStream modelStream = getClass().getResourceAsStream("/StdModels/"+modelName+".taup");
212 
213 	    if (modelStream != null) {
214 		textArea.append("loading "+modelName+"...");
215 		tMod = TauModel.readModelFromStream(modelStream);
216 		this.modelName = modelName;
217 		modelField.setText(modelName);
218 		textArea.append("Got it.\n");
219 	    } else {
220 		textArea.append("Couldn't find model, InputStream is null.\n");
221 	    }
222 	} catch (ClassNotFoundException exptn) {
223 	    System.out.println("itemStateChanged: caught ClassNotFoundException:"+
224 			       exptn.getMessage());
225 	}
226     }
227 	
228     public void findTauModel() {
229 	try {
230 	    StreamTokenizer manifest;
231 	    InputStream manifestStream;
232 	    if (getParameter("ARCHIVE") != null) {
233 		URL jarURL = new URL(
234 				     getCodeBase()+getParameter("ARCHIVE"));
235 		ZipInputStream jarStream = new ZipInputStream(jarURL.openStream());
236 		ZipEntry jarEntry = jarStream.getNextEntry();
237 		while (jarEntry != null) {
238 		    if (jarEntry.getName().startsWith("StdModels") &&
239 			jarEntry.getName().endsWith(".taup")) {
240 			getModel.addItem(jarEntry.getName().substring(
241 								      jarEntry.getName().lastIndexOf('/')+1,
242 								      jarEntry.getName().length()-5));
243 		    }
244 		    textArea.append(jarEntry.getName()+"\n");
245 		    System.out.println("jar entry name is "+jarEntry.getName());
246 		    jarEntry = jarStream.getNextEntry();
247 		}
248 
249 		jarStream.close();
250 		textArea.append("Got models from jar.\n");
251 	    } else {
252 		// no manifest, so not a jar? try getting StdModels directory
253 		manifestStream = getClass().getResourceAsStream("/StdModels/");
254 		if (manifestStream != null) {
255 		    manifest = new StreamTokenizer( manifestStream);
256 		    manifest.ordinaryChars('.','.');
257 		    manifest.wordChars('.','.');
258 		    manifest.ordinaryChars('0','9');
259 		    manifest.wordChars('0','9');
260 
261 		    while (manifest.nextToken() != manifest.TT_EOF ) {
262 			if (manifest.ttype == StreamTokenizer.TT_WORD &&
263 			    manifest.sval.endsWith(".taup")) {
264 			    getModel.addItem(manifest.sval.substring(0, manifest.sval.length()-5));
265 			}
266 		    }
267 		    textArea.append("Got models from local directory.\n");
268 		} else {
269 		    textArea.append("Error: Couldn't find models.\n");
270 		}
271 		manifestStream.close();
272 	    }
273 
274 	} catch (IOException exptn) {
275 	    System.out.println("findTauModel: caught IOException:"+
276 			       exptn.getMessage()+"\ngetCodeBase="+getCodeBase()+
277 			       "\ngetParameter(archive)="+getParameter("archive"));
278 	} 
279     }
280 
281     public void loadTool(String toolName, TauModel newTauModel) {
282 	try {
283 	    textArea.append("tool change to "+toolChoice.getSelectedItem());
284 
285 				// create an instance of the tool class
286 	    Class toolClass = Class.forName("edu.sc.seis.TauP."+toolName);
287 	    if (tool == null || ! tool.getClass().equals(toolClass)) {
288 		// invoke the constructor with single arguement (TauModel)
289 		Class[] argClasses = { Class.forName("edu.sc.seis.TauP.TauModel") };
290 		Object[] argObjects = { newTauModel };
291 		Constructor toolConstructor = toolClass.getConstructor(argClasses);
292 		TauP_Time tempTool = (TauP_Time)toolConstructor.newInstance(argObjects);
293 		tool = tempTool;
294 		tMod = newTauModel;
295 		textArea.append(" successful.");
296 	    } else {
297 		textArea.append(" already done.");
298 	    }
299 
300 	} catch (ClassNotFoundException ex) {
301 	    textArea.append(" failed. ClassNotFoundException:\n"+ex.getMessage());
302 	    return;
303 	} catch (InstantiationException ex) {
304 	    textArea.append(" failed. InstantiationException:\n"+ex.getMessage());
305 	    return;
306 	} catch (IllegalAccessException ex) {
307 	    textArea.append(" failed. IllegalAccessException:\n"+ex.getMessage());
308 	    return;
309 	} catch (InvocationTargetException ex) {
310 	    textArea.append(" failed. InvocationTargetException:\n"+
311 			    ex.getTargetException().getMessage());
312 	    ex.printStackTrace();
313 	    ex.getTargetException().printStackTrace();
314 	    return;
315 	} catch (NoSuchMethodException ex) {
316 	    textArea.append(" failed. NoSuchMethodException:\n"+ex.getMessage());
317 	    return;
318 	} finally {
319 	    textArea.append("\n");
320 	}
321     }
322 
323     public void makePlotActive() {
324 	outputCards.show(outputPanel, "Plot");
325 	textOrPlot.setLabel("Text");
326     }
327 
328     public void makeTextActive() {
329 	outputCards.show(outputPanel, "Text");
330 	textOrPlot.setLabel("Plot");
331     }
332 
333     public void itemStateChanged(ItemEvent event) {
334 	try {
335 	    if (event.getItemSelectable() == getModel) {
336 		loadTauModel(getModel.getSelectedItem());
337 		if (tool == null) {
338 		    loadTool(toolChoice.getSelectedItem(), tMod);
339 		} else {
340 		    tool.setTauModel(tMod);
341 		}
342 		phases = phasesField.getText();
343 		tool.parsePhaseList(phases);
344 		tool.depthCorrect(depth);
345 	    } else if (event.getItemSelectable() == toolChoice) {
346 		if (tMod == null) {
347 		    loadTauModel(getModel.getSelectedItem());
348 		}
349 		loadTool(toolChoice.getSelectedItem(), tMod);
350 		phases = phasesField.getText();
351 		tool.parsePhaseList(phases);
352 		tool.depthCorrect(depth);
353 	    }
354 	} catch (TauModelException e) {
355 	    textArea.append("\n\ndepthCorrection failed, TauModelException: "+e.getMessage());
356 	} catch (IOException e) {
357 	    textArea.append("\n\ndepthCorrection failed, IOException: "+e.getMessage());
358 	}
359     }
360 
361     public void actionPerformed(ActionEvent e) {
362 	String target = e.getActionCommand();
363 		
364 	if (target == "Calculate") { 
365 	    String text;
366 
367 	    try {
368 
369 		// make sure we have loaded the model
370 		if (tMod == null) {
371 		    loadTauModel(getModel.getSelectedItem());
372 		    if (tool != null) {
373 			tool.setTauModel(tMod);
374 			phases = phasesField.getText();
375 			tool.parsePhaseList(phases);
376 			tool.recalcPhases();
377 		    }
378 		}
379 
380 		if (tool == null) {
381 		    loadTool(toolChoice.getSelectedItem(), tMod);
382 		    phases = phasesField.getText();
383 		    tool.parsePhaseList(phases);
384 		    tool.depthCorrect(depth);
385 		}
386 
387 		// update the phase list if it has changed
388 		if (phases != phasesField.getText()) {
389 		    phases = phasesField.getText();
390 		    tool.clearPhaseNames();
391 		    tool.parsePhaseList(phases);
392 		    tool.recalcPhases();
393 		}
394 
395 		// update depth. Note this only recomputes the TauModel if the depth has changed
396 		depth = Double.valueOf(depthField.getText()).doubleValue();
397 		tool.depthCorrect(depth);
398 
399 		// calculate the times for each phase at the given distance
400 		distance = Double.valueOf(distanceField.getText()).doubleValue();
401 		tool.calculate(distance);
402 
403 		if (textOrPlot.getLabel().equals("Plot")) {
404 		    // print it out
405 		    result = new StringWriter();
406 		    tool.printResult(result);
407 		    textArea.append(result.toString());
408 		    textArea.append("Done!\n");
409 		} else {
410 		    if (tool.getClass().getName().equals("edu.sc.seis.TauP.TauP_Time")) {
411 			// plotting doesn't make sense for times, so just print
412 			outputCards.show(outputPanel, "Text");
413 			textOrPlot.setLabel("Plot");
414 			validate();
415 			result = new StringWriter();
416 			tool.printResult(result);
417 			textArea.append(result.toString());
418 			textArea.append("Done!\n");
419 		    } else if (tool.getClass().getName().equals("edu.sc.seis.TauP.TauP_Pierce")) {
420 			double[] disconDepths = tool.getDisconDepths();
421 			double[] disconRadius = new double[disconDepths.length];
422 			for (int i=0; i< disconDepths.length; i++) {
423 			    disconRadius[i] = tool.getTauModel().getRadiusOfEarth()-disconDepths[i];
424 			}
425 			plotArea.setCircles(disconRadius);
426 			Arrival[] arrivals = tool.getArrivals();
427 			plotArea.clearSegments();
428 			for (int i=0; i< arrivals.length; i++) {
429 			    if (arrivals[i].pierce != null) {
430 				plotArea.appendSegment(arrivals[i].pierce);
431 			    }
432 			}
433 			plotArea.repaint();
434 		    } else if (tool.getClass().getName().equals("edu.sc.seis.TauP.TauP_Path")) {
435 			double[] disconDepths = tool.getDisconDepths();
436 			double[] disconRadius = new double[disconDepths.length];
437 			for (int i=0; i< disconDepths.length; i++) {
438 			    disconRadius[i] = tool.getTauModel().getRadiusOfEarth()-disconDepths[i];
439 			}
440 			plotArea.setCircles(disconRadius);
441 			Arrival[] arrivals = tool.getArrivals();
442 			plotArea.clearSegments();
443 			for (int i=0; i< arrivals.length; i++) {
444 			    if (arrivals[i].path != null) {
445 				plotArea.appendSegment(arrivals[i].path);
446 			    }
447 			}
448 			plotArea.repaint();
449 		    } else if (tool.getClass().getName().equals("edu.sc.seis.TauP.TauP_Curve")) {
450 
451 		    }
452 					
453 		}
454 
455 	    } catch(TauModelException exptn) {
456 		System.out.println("actionPerformed: caught TauModelException:"+exptn.getMessage());
457 		exptn.printStackTrace();
458 	    } catch(IOException exptn) {
459 		System.out.println("actionPerformed: caught IOException:"+exptn.getMessage());
460 		exptn.printStackTrace();
461 	    }
462 
463 	} else if (target == "depthField") {
464 	    try {
465 		// update depth. Note this only recomputes the TauModel if the depth has changed
466 		depth = Double.valueOf(depthField.getText()).doubleValue();
467 		tool.depthCorrect(depth);
468 	    } catch(TauModelException exptn) {
469 		System.out.println("actionPerformed: caught TauModelException:"+exptn.getMessage());
470 		exptn.printStackTrace();
471 	    }
472 			
473 	}  else if (target == "Clear") {
474 	    textArea.setText("");
475 	} else if (target == "Plot") {
476 	    makePlotActive();
477 	} else if (target == "Text") {
478 	    makeTextActive();
479 	} else if (target == "Full") {
480 	    plotArea.setDisplayMode(PolarPlot.FULL);
481 	} else if (target == "Half") {
482 	    plotArea.setDisplayMode(PolarPlot.HALF);
483 	} else if (target == "Quarter") {
484 	    plotArea.setDisplayMode(PolarPlot.QUARTER);
485 	}
486 		
487     }
488 }