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.File;
31  import java.io.FileNotFoundException;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.InvalidClassException;
35  import java.io.OptionalDataException;
36  import java.io.StreamCorruptedException;
37  import java.util.zip.ZipEntry;
38  import java.util.zip.ZipFile;
39  
40  /*** static class that loads a tau model, after searching for it. It can
41   * be extended to change the search mechanism.
42   *
43   * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
44  
45  
46  
47   * @author H. Philip Crotwell
48   *
49   */
50  public class TauModelLoader {
51  
52      protectedong> static String packageName = "/edu/sc/seis/TauP/StdModels";
53  
54      /*** Reads the velocity model, slowness model, and tau model from
55       * a file saved using Java's Serializable interface.
56       */
57      public static TauModel load(String modelName, String searchPath) throws FileNotFoundException,
58          ClassNotFoundException,
59          InvalidClassException,
60          IOException,
61          StreamCorruptedException,
62          OptionalDataException {
63          String filename;
64  
65          /* Append ".taup" to modelname if it isn't already there. */
66          if (modelName.endsWith(".taup")) {
67              filename = modelName;
68          } else {
69              filename = modelName + ".taup";
70          }
71  
72          String classPath = System.getProperty("java.class.path");
73          String taupPath = searchPath;
74  
75          int offset = 0;
76          int pathSepIndex;
77          String pathEntry;
78          File jarFile;
79          File modelFile;
80  
81          /* First we try to find the model in the distributed taup.jar file.*/
82          try {
83              Class c=null;
84  
85              c = Class.forName("edu.sc.seis.TauP.TauModelLoader");
86  
87              InputStream in = c.getResourceAsStream(packageName+"/"+filename);
88              if (in != null) {
89                  return TauModel.readModelFromStream(in);
90              }
91          } catch (Exception ex) {
92              // couldn't get as a resource, so keep going
93              logger.debug("couldn't load as resource: ", ex);
94          }
95  
96          /* couldn't find as a resource, try in classpath. */
97          while (offset < classPath.length()) {
98              pathSepIndex = classPath.indexOf(File.pathSeparatorChar, offset);
99              if (pathSepIndex != -1) {
100                 pathEntry = classPath.substring(offset,pathSepIndex);
101                 offset = pathSepIndex+1;
102             } else {
103                 pathEntry = classPath.substring(offset);
104                 offset = classPath.length();
105             }
106             jarFile = new File(pathEntry);
107             if (jarFile.exists() && jarFile.isFile() &&
108                 jarFile.getName().equals("taup.jar") && jarFile.canRead()) {
109                 ZipFile zippy = new ZipFile(jarFile);
110                 ZipEntry zipEntry = zippy.getEntry("StdModels/"+filename);
111                 if (zipEntry != null) {
112                     return TauModel.readModelFromStream(zippy.getInputStream( zipEntry));
113                 }
114             }
115         }
116 
117         /*  It isn't in the taup.jar so we try to find it within the paths
118          *  specified in the taup.model.path property. */
119         offset = 0;
120         if (taupPath != null) {
121             while (offset < taupPath.length()) {
122                 pathSepIndex = taupPath.indexOf(File.pathSeparatorChar, offset);
123                 if (pathSepIndex != -1) {
124                     pathEntry = taupPath.substring(offset,pathSepIndex);
125                     offset = pathSepIndex+1;
126                 } else {
127                     pathEntry = taupPath.substring(offset);
128                     offset = taupPath.length();
129                 }
130 
131                 /* Check each jar file. */
132                 if (pathEntry.endsWith(".jar") || pathEntry.endsWith(".zip")) {
133                     jarFile = new File(pathEntry);
134                     if (jarFile.exists() && jarFile.isFile() && jarFile.canRead()) {
135                         ZipFile zippy = new ZipFile(jarFile);
136                         ZipEntry zipEntry = zippy.getEntry("Models/"+filename);
137                         if (zipEntry != null) {
138                             return TauModel.readModelFromStream(zippy.getInputStream( zipEntry));
139                         }
140                     }
141                 } else {
142                     /* Check for regular files. */
143                     modelFile = new File(pathEntry+"/"+filename);
144                     if (modelFile.exists() && modelFile.isFile() &&
145                         modelFile.canRead()) {
146                         return TauModel.readModel(modelFile.getCanonicalPath());
147                     }
148                 }
149             }
150         }
151 
152         /* Couldn't find it in the taup.model.path either, look in the current
153          * directory. */
154         modelFile = new File(filename);
155         if (modelFile.exists()&& modelFile.isFile()&&modelFile.canRead()) {
156             return TauModel.readModel(modelFile.getCanonicalPath());
157         } else {
158             throw new FileNotFoundException(
159                 "Can't find any saved models for "+modelName);
160         }
161 
162 
163     }
164 
165 
166     private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(TauModelLoader.class);
167 
168 }