1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 protected static String packageName =/package-summary.html">ong> 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
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
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
93 logger.debug("couldn't load as resource: ", ex);
94 }
95
96
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
118
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
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
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
153
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 }