GeoTessJavaExamples  2.0
GeoTessModelGA.java
Go to the documentation of this file.
1 package gov.sandia.geotess.examples.customdata;
2 
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.Writer;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Scanner;
13 
14 import gov.sandia.geotess.GeoTessException;
15 import gov.sandia.geotess.GeoTessGrid;
16 import gov.sandia.geotess.GeoTessMetaData;
17 import gov.sandia.geotess.GeoTessModel;
18 import gov.sandia.geotess.GeoTessUtils;
19 
20 public class GeoTessModelGA extends GeoTessModel {
21 
22  /**
23  * A list of all the station names supported by this model
24  */
25  protected String[] stationNames;
26 
27  /**
28  * A list of all the phase names supported by this model.
29  */
31 
32  /**
33  * A list of all the attributes supported by this model.
34  * All of these attributes will have values in each PhaseData map and will
35  * be read from, and written to the output files.
36  */
38 
39  /**
40  * Map from index -> [Station Name, Phase Name, Attribute Name]
41  * Includes every combination of station - phase - attribute.
42  * Size equals nStations * nPhases * nAttributes.
43  */
44  private List<Triple> indexList;
45 
46  /**
47  * Map from station -> phase -> attribute -> attribute index
48  * Includes every combination of station - phase - attribute.
49  * Maximum index equals nStations * nPhases * nAttributes -1.
50  */
51  private Map<String, Map<SeismicPhase,Map<GeoAttributes, Integer>>> indexMap;
52 
53  /**
54  * Construct a new GeoTessModel object and populate it with information from
55  * the specified file.
56  *
57  * @param modelInputFile
58  * name of file containing the model.
59  * @param relativeGridPath
60  * the relative path from the directory where the model is stored
61  * to the directory where the grid is stored. Often, the model
62  * and grid are stored together in the same file in which case
63  * this parameter is ignored. Sometimes, however, the grid is
64  * stored in a separate file and only the name of the grid file
65  * (without path information) is stored in the model file. In
66  * this case, the code needs to know which directory to search
67  * for the grid file. The default is "" (empty string), which
68  * will cause the code to search for the grid file in the same
69  * directory in which the model file resides. Bottom line is that
70  * the default value is appropriate when the grid is stored in
71  * the same file as the model, or the model file is in the same
72  * directory as the model file.
73  * @throws IOException
74  */
75  public GeoTessModelGA(File modelInputFile, String relativeGridPath) throws IOException
76  {
77  super();
78  loadModel(modelInputFile, relativeGridPath);
79  }
80 
81  /**
82  * Construct a new GeoTessModel object and populate it with information from
83  * the specified file.
84  *
85  * <p>relativeGridPath is assumed to be "" (empty string), which is appropriate
86  * when the grid information is stored in the same file as the model or when
87  * the grid is stored in a separate file located in the same directory as the
88  * model file.
89  *
90  * @param modelInputFile
91  * name of file containing the model.
92  * @throws IOException
93  */
94  public GeoTessModelGA(File modelInputFile) throws IOException
95  {
96  super();
97  loadModel(modelInputFile, "");
98  }
99 
100  /**
101  * Construct a new GeoTessModel object and populate it with information from
102  * the specified file.
103  *
104  * @param modelInputFile
105  * name of file containing the model.
106  * @param relativeGridPath
107  * the relative path from the directory where the model is stored
108  * to the directory where the grid is stored. Often, the model
109  * and grid are stored together in the same file in which case
110  * this parameter is ignored. Sometimes, however, the grid is
111  * stored in a separate file and only the name of the grid file
112  * (without path information) is stored in the model file. In
113  * this case, the code needs to know which directory to search
114  * for the grid file. The default is "" (empty string), which
115  * will cause the code to search for the grid file in the same
116  * directory in which the model file resides. Bottom line is that
117  * the default value is appropriate when the grid is stored in
118  * the same file as the model, or the model file is in the same
119  * directory as the model file.
120  * @throws IOException
121  */
122  public GeoTessModelGA(String modelInputFile, String relativeGridPath) throws IOException
123  {
124  super();
125  loadModel(modelInputFile, relativeGridPath);
126  }
127 
128  /**
129  * Construct a new GeoTessModel object and populate it with information from
130  * the specified file.
131  *
132  * <p>relativeGridPath is assumed to be "" (empty string), which is appropriate
133  * when the grid information is stored in the same file as the model or when
134  * the grid is stored in a separate file located in the same directory as the
135  * model file.
136  *
137  * @param modelInputFile
138  * name of file containing the model.
139  * @throws IOException
140  */
141  public GeoTessModelGA(String modelInputFile) throws IOException
142  {
143  super();
144  loadModel(modelInputFile, "");
145  }
146 
147  /**
148  * Parameterized constructor, specifying the grid and metadata for the
149  * model. The grid is constructed and the data structures are initialized
150  * based on information supplied in metadata. The data structures are not
151  * populated with any information however (all Profiles are null). The
152  * application should populate the new model's Profiles after this
153  * constructor completes.
154  *
155  * <p>
156  * Before calling this constructor, the supplied MetaData object must be
157  * populated with required information by calling the following MetaData
158  * methods:
159  * <ul>
160  * <li>setDescription()
161  * <li>setLayerNames()
162  * <li>setAttributes()
163  * <li>setDataType()
164  * <li>setLayerTessIds() (only required if grid has more than one
165  * multi-level tessellation)
166  * </ul>
167  *
168  * @param gridFileName
169  * name of file from which to load the grid.
170  * @param metaData
171  * MetaData the new GeoTessModel instantiates a reference to the
172  * supplied metaData. No copy is made.
173  * @throws IOException
174  */
175  public GeoTessModelGA(String gridFileName, GeoTessMetaData metaData) throws IOException
176  {
177  super(gridFileName, metaData);
178  }
179 
180  /**
181  * Parameterized constructor, specifying the grid and metadata for the
182  * model. The grid is constructed and the data structures are initialized
183  * based on information supplied in metadata. The data structures are not
184  * populated with any information however (all Profiles are null). The
185  * application should populate the new model's Profiles after this
186  * constructor completes.
187  *
188  * <p>
189  * Before calling this constructor, the supplied MetaData object must be
190  * populated with required information by calling the following MetaData
191  * methods:
192  * <ul>
193  * <li>setDescription()
194  * <li>setLayerNames()
195  * <li>setAttributes()
196  * <li>setDataType()
197  * <li>setLayerTessIds() (only required if grid has more than one
198  * multi-level tessellation)
199  * <li>setSoftwareVersion()
200  * <li>setGenerationDate()
201  * </ul>
202  *
203  * @param grid
204  * a reference to the GeoTessGrid that will support this
205  * GeoTessModel.
206  * @param metaData
207  * MetaData the new GeoTessModel instantiates a reference to the
208  * supplied metaData. No copy is made.
209  * @throws GeoTessException
210  * if metadata is incomplete.
211  */
212  public GeoTessModelGA(GeoTessGrid grid, GeoTessMetaData metaData) throws GeoTessException, IOException
213  {
214  super(grid, metaData);
215  }
216 
217  /**
218  * Construct a new GAModel with all the structures from the supplied
219  * baseModel. The new GAModel will be built with references to the
220  * GeoTessMetaData, GeoTessGrid and all the Profiles in the baseModel.
221  * No copies are made. Changes to one will be reflected in the other.
222  * All of the extraData will be set to default values.
223  * @param baseModel
224  * @throws GeoTessException
225  */
226  public GeoTessModelGA(GeoTessModel baseModel) throws GeoTessException
227  {
228  super(baseModel.getGrid(), baseModel.getMetaData());
229  for (int i = 0; i < baseModel.getNVertices(); ++i)
230  for (int j=0; j<baseModel.getNLayers(); ++j)
231  setProfile(i,j,baseModel.getProfile(i, j));
232  }
233 
234  public String[] getStationNames() {
235  return stationNames;
236  }
237 
239  return phaseNames;
240  }
241 
243  return attributes;
244  }
245 
246  public List<Triple> getIndexList() {
247  return indexList;
248  }
249 
250  public int getAttributeIndex(String station, SeismicPhase phase, GeoAttributes attribute)
251  { return indexMap.get(station).get(phase).get(attribute); }
252 
253  /**
254  * Overridden IO method.
255  */
256  @Override
257  protected void loadModelBinary(DataInputStream input,
258  String inputDirectory, String relGridFilePath)
259  throws GeoTessException, IOException
260  {
261  // call super class to load model data from binary file.
262  super.loadModelBinary(input, inputDirectory, relGridFilePath);
263 
264  // it is good practice, but not required, to store the class
265  // name as the first thing added by the extending class.
266  String className = GeoTessUtils.readString(input);
267  if (!className.equals(this.getClass().getSimpleName()))
268  throw new IOException("Found class name "+className
269  +" but expecting "
270  +this.getClass().getSimpleName());
271 
272  // it is good practice, but not required, to store a format
273  // version number as the second thing added by the extending class.
274  // With this information, if the format changes in a future release
275  // it may be possible to make the class backward compatible.
276  int formatVersion = input.readInt();
277 
278  if (formatVersion != 1)
279  throw new IOException("GAModel Format version "+formatVersion+" is not supported.");
280 
281  // Read in list of station names supported by this model
282  int nStations = input.readInt();
283  String[] stations = new String[nStations];
284  for (int i=0; i<nStations; ++i)
285  stations[i] = GeoTessUtils.readString(input);
286 
287  // Read in list of phase names supported by this model
288  int nPhases = input.readInt();
289  SeismicPhase[] phases = new SeismicPhase[nPhases];
290  for (int i=0; i<nPhases; ++i)
291  phases[i] = SeismicPhase.valueOf(GeoTessUtils.readString(input));
292 
293  // Read in list of GeoAttributes supported by this model.
294  int nAttributes = input.readInt();
295  GeoAttributes[] attributes = new GeoAttributes[nAttributes];
296  for (int i=0; i<nAttributes; ++i)
297  attributes[i] = GeoAttributes.valueOf(GeoTessUtils.readString(input));
298 
299  setStationsPhasesAttributes(stations, phases, attributes);
300  }
301 
302  /**
303  * Overridden IO method.
304  */
305  @Override
306  protected void writeModelBinary(DataOutputStream output, String gridFileName)
307  throws IOException
308  {
309  // call super class to write standard model information to binary file.
310  super.writeModelBinary(output, gridFileName);
311 
312  // it is good practice, but not required, to store the class
313  // name as the first thing added by the extending class.
314  GeoTessUtils.writeString(output, this.getClass().getSimpleName());
315 
316  // it is good practice, but not required, to store a format
317  // version number as the second thing added by the extending class.
318  // With this information, if the format changes in a future release
319  // it may be possible to make the class backward compatible.
320  output.writeInt(1);
321 
322  // now output the extraData
323  output.writeInt(stationNames.length);
324  for (String station : stationNames)
325  GeoTessUtils.writeString(output, station);
326 
327  output.writeInt(phaseNames.length);
328  for (SeismicPhase phase : phaseNames)
329  GeoTessUtils.writeString(output, phase.toString());
330 
331  output.writeInt(attributes.length);
332  for (GeoAttributes a : attributes)
333  GeoTessUtils.writeString(output, a.toString());
334  }
335 
336  /**
337  * Overridden IO method.
338  */
339  @Override
340  protected void loadModelAscii(Scanner input, String inputDirectory,
341  String relGridFilePath) throws GeoTessException, IOException
342  {
343  super.loadModelAscii(input, inputDirectory, relGridFilePath);
344 
345  // it is good practice, but not required, to store the class
346  // name as the first thing added by the extending class.
347  String className = input.nextLine();
348  if (!className.equals(this.getClass().getSimpleName()))
349  throw new IOException("Found class name "+className
350  +" but expecting "
351  +this.getClass().getSimpleName());
352 
353  // it is good practice, but not required, to store a format
354  // version number as the second thing added by the extending class.
355  // With this information, if the format changes in a future release
356  // it may be possible to make the class backward compatible.
357  int formatVersion = input.nextInt();
358  input.nextLine();
359 
360  if (formatVersion != 1)
361  throw new IOException("GAModel Format version "+formatVersion+" is not supported.");
362 
363  // Read in list of station names supported by this model
364  int nStations = input.nextInt();
365  String[] stations = new String[nStations];
366  for (int i=0; i<nStations; ++i)
367  stations[i] = input.next();
368 
369  // Read in list of phase names supported by this model
370  int nPhases = input.nextInt();
371  SeismicPhase[] phases = new SeismicPhase[nPhases];
372  for (int i=0; i<nPhases; ++i)
373  phases[i] = SeismicPhase.valueOf(input.next());
374 
375  // Read in list of GeoAttribute names supported by this model
376  int nAttributes = input.nextInt();
377  GeoAttributes[] attributes = new GeoAttributes[nAttributes];
378  for (int i=0; i<nAttributes; ++i)
379  attributes[i] = GeoAttributes.valueOf(input.next());
380 
381  setStationsPhasesAttributes(stations, phases, attributes);
382  }
383 
385  String[] stations, SeismicPhase[] phases, GeoAttributes[] attributes)
386  {
387  this.stationNames = stations;
388  this.phaseNames = phases;
389  this.attributes = attributes;
390 
391  indexList = new ArrayList<>(stationNames.length*phaseNames.length*attributes.length);
392  indexMap = new HashMap<String, Map<SeismicPhase,Map<GeoAttributes,Integer>>>(stations.length);
393 
394  for (String s : stationNames)
395  {
396  Map<SeismicPhase, Map<GeoAttributes, Integer>> m2 =
397  new HashMap<SeismicPhase, Map<GeoAttributes,Integer>>(phases.length);
398  indexMap.put(s, m2);
399  for (SeismicPhase p : phaseNames)
400  {
401  Map<GeoAttributes, Integer> m3 = new HashMap<GeoAttributes, Integer>(attributes.length);
402  m2.put(p, m3);
403  for (GeoAttributes a : attributes)
404  {
405  m3.put(a, indexList.size());
406  indexList.add(new Triple(s, p, a));
407  }
408  }
409  }
410  }
411 
412  /**
413  * Overridden IO method.
414  */
415  @Override
416  protected void writeModelAscii(Writer output, String gridFileName)
417  throws IOException
418  {
419  // call super class to write standard model information to ascii file.
420  super.writeModelAscii(output, gridFileName);
421 
422  // it is good practice, but not required, to store the class
423  // name and a format version number as the first things added
424  // by the extending class.
425  // With this information, if the format changes in a future release
426  // it may be possible to make the class backward compatible.
427  output.write(String.format("%s%n%d%n", this.getClass().getSimpleName(), 1));
428 
429  // now output the extraData
430  output.write(stationNames.length);
431  for (String station : stationNames)
432  output.write(' '+station);
433  output.write("\n");
434 
435  output.write(phaseNames.length);
436  for (SeismicPhase phase : phaseNames)
437  output.write(' '+phase.toString());
438  output.write("\n");
439 
440  output.write(attributes.length);
441  for (GeoAttributes a : attributes)
442  output.write(' '+a.toString());
443  output.write("\n");
444  }
445 
446 }
gov.sandia.geotess.examples.customdata.GeoTessModelGA.indexList
List< Triple > indexList
Map from index -> [Station Name, Phase Name, Attribute Name] Includes every combination of station - ...
Definition: GeoTessModelGA.java:44
gov.sandia.geotess.examples.customdata.Triple
Definition: Triple.java:4
gov.sandia.geotess.examples.customdata.GeoTessModelGA.getAttributes
GeoAttributes[] getAttributes()
Definition: GeoTessModelGA.java:242
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(GeoTessGrid grid, GeoTessMetaData metaData)
Parameterized constructor, specifying the grid and metadata for the model.
Definition: GeoTessModelGA.java:212
gov.sandia.geotess.examples.customdata.GeoTessModelGA.attributes
GeoAttributes[] attributes
A list of all the attributes supported by this model.
Definition: GeoTessModelGA.java:37
gov.sandia.geotess.examples.customdata.GeoTessModelGA.writeModelBinary
void writeModelBinary(DataOutputStream output, String gridFileName)
Overridden IO method.
Definition: GeoTessModelGA.java:306
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(File modelInputFile, String relativeGridPath)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelGA.java:75
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(String gridFileName, GeoTessMetaData metaData)
Parameterized constructor, specifying the grid and metadata for the model.
Definition: GeoTessModelGA.java:175
gov
gov.sandia.geotess.examples.customdata.GeoTessModelGA.loadModelAscii
void loadModelAscii(Scanner input, String inputDirectory, String relGridFilePath)
Overridden IO method.
Definition: GeoTessModelGA.java:340
gov.sandia.geotess.examples.customdata.GeoTessModelGA
Definition: GeoTessModelGA.java:20
gov.sandia.geotess.examples.customdata.GeoTessModelGA.loadModelBinary
void loadModelBinary(DataInputStream input, String inputDirectory, String relGridFilePath)
Overridden IO method.
Definition: GeoTessModelGA.java:257
gov.sandia.geotess.examples.customdata.GeoTessModelGA.stationNames
String[] stationNames
A list of all the station names supported by this model.
Definition: GeoTessModelGA.java:25
gov.sandia.geotess.examples.customdata.GeoTessModelGA.indexMap
Map< String, Map< SeismicPhase, Map< GeoAttributes, Integer > > > indexMap
Map from station -> phase -> attribute -> attribute index Includes every combination of station - pha...
Definition: GeoTessModelGA.java:51
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(GeoTessModel baseModel)
Construct a new GAModel with all the structures from the supplied baseModel.
Definition: GeoTessModelGA.java:226
gov.sandia.geotess
gov.sandia.geotess.examples.customdata.GeoTessModelGA.getAttributeIndex
int getAttributeIndex(String station, SeismicPhase phase, GeoAttributes attribute)
Definition: GeoTessModelGA.java:250
gov.sandia.geotess.examples.customdata.GeoTessModelGA.phaseNames
SeismicPhase[] phaseNames
A list of all the phase names supported by this model.
Definition: GeoTessModelGA.java:30
gov.sandia.geotess.examples.customdata.GeoTessModelGA.writeModelAscii
void writeModelAscii(Writer output, String gridFileName)
Overridden IO method.
Definition: GeoTessModelGA.java:416
gov.sandia.geotess.examples.customdata.GeoTessModelGA.getPhaseNames
SeismicPhase[] getPhaseNames()
Definition: GeoTessModelGA.java:238
gov.sandia.geotess.examples.customdata.GeoTessModelGA.setStationsPhasesAttributes
void setStationsPhasesAttributes(String[] stations, SeismicPhase[] phases, GeoAttributes[] attributes)
Definition: GeoTessModelGA.java:384
gov.sandia.geotess.examples.customdata.GeoTessModelGA.getStationNames
String[] getStationNames()
Definition: GeoTessModelGA.java:234
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(File modelInputFile)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelGA.java:94
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(String modelInputFile, String relativeGridPath)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelGA.java:122
gov.sandia.geotess.examples.customdata.GeoTessModelGA.GeoTessModelGA
GeoTessModelGA(String modelInputFile)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelGA.java:141
gov.sandia.geotess.examples.customdata.GeoAttributes
Definition: GeoAttributes.java:3
gov.sandia.geotess.examples.customdata.SeismicPhase
Definition: SeismicPhase.java:3
gov.sandia.geotess.examples.customdata.GeoTessModelGA.getIndexList
List< Triple > getIndexList()
Definition: GeoTessModelGA.java:246
gov.sandia