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.io.IOException;
31  import java.util.Properties;
32  
33  /***
34    * TauP_Create - Re-implementation of the seismic travel time calculation
35    * method described in "The Computation of Seismic Travel Times"
36    * by Buland and Chapman, BSSA vol. 73, No. 5, October 1983, pp 1271-1302.
37    * This creates the SlownessModel and tau branches and saves them for later
38    * use.
39    *
40    * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
41  
42  
43  
44    * @author H. Philip Crotwell
45    */
46  public class TauP_Create {
47  
48      public transient boolean verbose = false;
49  
50      String modelFilename = "iasp91.tvel";
51  
52      protected String velFileType = "tvel";
53  
54      String directory = ".";
55  
56      SlownessModel sMod;
57  
58      VelocityModel vMod;
59  
60      TauModel tMod;
61  
62      boolean DEBUG = false;
63  
64      protected boolean GUI = false;
65  
66      protected Properties toolProps;
67  
68  
69      /* constructor */
70  
71      public TauP_Create() {
72          Alert.setGUI(GUI);
73  	try {
74  	    toolProps = PropertyLoader.load();
75  	} catch (Exception e) {
76  	    Alert.warning("Unable to load properties, using defaults.",
77  			  e.getMessage());
78  	    toolProps = new Properties();
79  	}
80      }
81  
82      /* Accessor methods */
83  
84  
85      public void setDirectory(String directory) {
86  	this.directory = directory;
87      }
88  
89      public String getDirectory() {
90  	return directory;
91      }
92  
93      public void setModelFilename(String modelFilename) {
94  	this.modelFilename = modelFilename;
95      }
96  
97      public String getModelFilename() {
98  	return modelFilename;
99      }
100 
101     public void setDEBUG(boolean DEBUG) {
102 	this.DEBUG = DEBUG;
103     }
104 
105     public boolean getDEBUG() {
106 	return DEBUG;
107     }
108 
109     public void setVelocityModel(VelocityModel vMod) {
110 	this.vMod = vMod;
111     }
112 
113     public VelocityModel getVelocityModel() {
114 	return vMod;
115     }
116 
117     public void setSlownessModel(SlownessModel sMod) {
118 	this.sMod = sMod;
119     }
120 
121     public SlownessModel getSlownessModel() {
122 	return sMod;
123     }
124 
125     public void setTauModel(TauModel tMod) {
126 	this.tMod = tMod;
127     }
128 
129     public TauModel getTauModel() {
130 	return tMod;
131     }
132 
133     public void printUsage() {
134 	String className = this.getClass().getName();
135 	className =
136 	    className.substring(className.lastIndexOf('.')+1,className.length());
137  
138 	System.out.println("Usage: "+className.toLowerCase()+" [arguments]");
139 	System.out.println("  or, for purists, java "+this.getClass().getName()+
140 			   " [arguments]");
141 
142 	System.out.println("\nArguments are:");
143 	System.out.println("\n   To specify the velocity model:");
144 	System.out.println("-nd modelfile       -- \"named discontinuities\" velocity file");
145 	System.out.println("-tvel modelfile     -- \".tvel\" velocity file, ala ttimes\n");
146 	System.out.println(
147 			   "-debug              -- enable debugging output\n"+
148 			   "-verbose            -- enable verbose output\n"+
149 			   "-version            -- print the version\n"+
150 			   "-help               -- print this out, but you already know that!\n\n");
151     }
152 
153     /* parses the command line args for TauP_Create. */
154     protected String[] parseCmdLineArgs(String[] args) {
155 	int i=0;
156 	String[] noComprendoArgs = new String[args.length];
157 	int numNoComprendoArgs = 0;
158  
159 	while (i<args.length) {
160 	    if (args[i].equalsIgnoreCase("-help")) {
161 		printUsage();
162 		noComprendoArgs[numNoComprendoArgs++] = args[i];
163 		return noComprendoArgs;
164 	    } else if (args[i].equalsIgnoreCase("-version")) {
165 		System.out.println(Version.getVersion());
166 		noComprendoArgs[numNoComprendoArgs++] = args[i];
167 		return noComprendoArgs;
168 	    } else if (args[i].equalsIgnoreCase("-debug")) {
169 		verbose = true;
170 		DEBUG = true;
171 	    } else if (args[i].equalsIgnoreCase("-verbose")) {
172 		verbose = true;
173 	    } else if (args[i].equalsIgnoreCase("-gui")) {
174 		GUI = true;
175 	    } else if (i<args.length-1 && args[i].equalsIgnoreCase("-nd")) {
176 		velFileType = "nd";
177 		parseFileName(args[i+1]);
178 		i++;
179 	    } else if (i<args.length-1 && args[i].equalsIgnoreCase("-tvel")) {
180 		velFileType = "tvel";
181 		parseFileName(args[i+1]);
182 		i++;
183 	    } else if (args[i].startsWith("GB.")) {
184 		velFileType = "nd";
185 		parseFileName(args[i]);
186 	    } else if (args[i].endsWith(".nd")) {
187 		velFileType = "nd";
188 		parseFileName(args[i]);
189 	    } else if (args[i].endsWith(".tvel")) {
190 		velFileType = "tvel";
191 		parseFileName(args[i]);
192 	    } else {
193 		/* I don't know how to interpret this argument, so pass it back */
194 		noComprendoArgs[numNoComprendoArgs++] = args[i];
195 	    }
196 	    i++;
197 	}
198 	if (numNoComprendoArgs > 0) {
199 	    String[] temp = new String[numNoComprendoArgs];
200 	    System.arraycopy(noComprendoArgs,0,temp,0,numNoComprendoArgs);
201 	    return temp;
202 	} else {
203 	    return new String[0];
204 	}
205     }
206 
207     /*** Allows TauP_Create to run as an application. Creates an instance 
208      *  of TauP_Create and calls tauPCreate.init() and 
209      *  tauPCreate.start(). */
210     public static void main(String[] args) throws SlownessModelException, TauModelException {
211 	System.out.println("TauP_Create starting...");
212 	TauP_Create tauPCreate = new TauP_Create();
213 
214 	String[] noComprendoArgs = tauPCreate.parseCmdLineArgs(args);
215 	if (noComprendoArgs.length > 0) {
216 	    for (int i=0;i<noComprendoArgs.length;i++) {
217 		if (noComprendoArgs[i].equals("-help") ||
218 		    noComprendoArgs[i].equals("-version")) {
219 		    System.exit(0);
220 		}
221 	    }
222 	    System.out.println("I don't understand the following arguments, continuing:");
223 	    for (int i=0;i<noComprendoArgs.length;i++) {
224 		System.out.print(noComprendoArgs[i]+" ");
225 	    }
226 	    System.out.println();
227 	    noComprendoArgs = null;
228 	}
229 
230 	try {
231 	    tauPCreate.init();
232 	    tauPCreate.start();
233 	    System.out.println("Done!");
234 	} catch (IOException e) {
235 	    System.out.println("Tried to read!\n Caught IOException "
236 			       + e.getMessage()+"\nCheck that the file exists and is readable.");
237 	} catch (VelocityModelException e) {
238 	    System.out.println("Caught VelocityModelException "
239 			       + e.getMessage()+"\nCheck your velocity model.");
240 	}
241     }
242 
243     public void parseFileName(String modelFilename) {
244 	int j = modelFilename.lastIndexOf(
245 					  System.getProperty("file.separator"));
246 	this.modelFilename = modelFilename.substring(j+1);
247 	if (j==-1) {
248 	    directory = ".";
249 	} else {
250 	    directory = modelFilename.substring(0,j);
251 	}
252     }
253 
254     public void init() throws IOException, VelocityModelException {
255 	String file_sep = System.getProperty("file.separator");
256 
257 		
258 	vMod = new VelocityModel();
259 	// Read the velocity model file.
260 	vMod.setFileType(velFileType);
261        if (verbose) System.out.println("filename ="+
262 				       directory+file_sep+modelFilename);
263 
264        vMod.readVelocityFile(directory+file_sep+modelFilename);
265        if (verbose) {
266 	   System.out.println("Done reading velocity model.");
267 	   System.out.println("Radius of model "+vMod.getModelName()+" is "+
268 			      vMod.getRadiusOfEarth());
269        }
270 
271 	if (DEBUG) System.out.println(vMod);
272     }
273 
274     public void start() throws SlownessModelException, TauModelException {
275 	try {
276 	    long currTime;
277 	    long prevTime = System.currentTimeMillis();
278 	    String file_sep = System.getProperty("file.separator");
279 
280 	    if (vMod.getSpherical()) {
281 		sMod = new SphericalSModel();
282 	    } else {
283 		System.out.println("Flat slowness model not yet implemented."+
284 				   "\n   Using spherical slowness model.");
285 		sMod = new SphericalSModel();
286 	    }
287 
288 	    sMod.DEBUG = DEBUG;
289 				// set sMod defaults from properties
290 	    sMod.setMinDeltaP(Double.valueOf(toolProps.getProperty(
291 								   "taup.create.minDeltaP", "0.1")).doubleValue());
292 	    sMod.setMaxDeltaP(Double.valueOf(toolProps.getProperty(
293 								   "taup.create.maxDeltaP", "11.0")).doubleValue());
294 	    sMod.setMaxDepthInterval(Double.valueOf(toolProps.getProperty(
295 									  "taup.create.maxDepthInterval", "115.0")).doubleValue());
296 	    sMod.setMaxRangeInterval(Double.valueOf(toolProps.getProperty(
297 									  "taup.create.maxRangeInterval", "1.75")).doubleValue());
298 	    sMod.setMaxInterpError(Double.valueOf(toolProps.getProperty(
299 									"taup.create.maxInterpError", "0.05")).doubleValue());
300 	    sMod.setAllowInnerCoreS(Boolean.valueOf(toolProps.getProperty(
301 									  "taup.create.allowInnerCoreS", "true")).booleanValue());
302 
303 	    if (verbose) {
304 		System.out.println("Parameters are:");
305 		System.out.println("taup.create.minDeltaP = "
306 				   +sMod.getMinDeltaP()+" sec / radian");
307 		System.out.println("taup.create.maxDeltaP = "
308 				   +sMod.getMaxDeltaP()+" sec / radian");
309 		System.out.println("taup.create.maxDepthInterval = "
310 				   +sMod.getMaxDepthInterval()+" kilometers");
311 		System.out.println("taup.create.maxRangeInterval = "
312 				   +sMod.getMaxRangeInterval()+" degrees");
313 		System.out.println("taup.create.maxInterpError = "
314 				   +sMod.getMaxInterpError()+" seconds");
315 		System.out.println("taup.create.allowInnerCoreS = "
316 				   +sMod.isAllowInnerCoreS());
317 	    }
318 
319 	    sMod.createSample(vMod); // Create slowness sampling.
320 
321 	    currTime = System.currentTimeMillis();
322 	    if (verbose) {
323 		System.out.println("Slow model time="+(currTime-prevTime)+" "
324 				   +sMod.getNumLayers(true)+" P layers,"
325 				   +sMod.getNumLayers(false)+" S layers");
326 	    }
327 	    prevTime = currTime;
328 
329 	    if (DEBUG) System.out.println(sMod);
330 
331 	    tMod = new TauModel();
332 	    tMod.DEBUG = DEBUG;
333             // Creates tau model from slownesses
334 	    tMod.calcTauIncFrom(sMod);
335 
336 	    currTime = System.currentTimeMillis();
337 	    if (verbose) {
338 		System.out.println("T model time="+(currTime-prevTime));
339 	    }
340 	    prevTime = currTime;
341 
342 	    if (DEBUG) System.out.println("Done calculating Tau branches.");
343 	    if (DEBUG) tMod.print();
344 
345 	    String outFile;
346 	    if (directory.equals(".")) {
347 		outFile = directory+file_sep+ vMod.getModelName()+".taup";
348 	    } else {
349 		outFile = vMod.getModelName()+".taup";
350 	    }
351 
352 	    tMod.writeModel(outFile);
353 
354 	    if (verbose) {
355 		System.out.println("Done Saving "+outFile);
356 	    }
357 
358 	} catch (IOException e) {
359 	    System.out.println("Tried to write!\n Caught IOException "
360 			       + e.getMessage()
361 			       +"\nDo you have write permission in this directory?");
362 	} catch (VelocityModelException e) {
363 	    System.out.println("Caught VelocityModelException "
364 			       + e.getMessage());
365 	} finally {
366 	    if (verbose) {
367 		System.out.println("Done!");
368 	    }
369 	}
370     }
371 }