GeoTessJavaExamples  2.0
TestCrust20.java
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 package gov.sandia.geotess.examples;
37 
38 import gov.sandia.geotess.GeoTessModel;
39 import gov.sandia.geotess.GeoTessPosition;
40 import gov.sandia.geotess.Profile;
41 import gov.sandia.geotess.ProfileType;
42 import gov.sandia.gmp.util.globals.InterpolatorType;
43 import gov.sandia.gmp.util.globals.OptimizationType;
44 import gov.sandia.gmp.util.numerical.polygon.GreatCircle;
45 import gov.sandia.gmp.util.numerical.vector.VectorGeo;
46 
47 import java.io.File;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.HashMap;
51 
52 /**
53  * Example application that loads a GeoTessModel that contains a
54  * representation of the Crust 2.0 model of Bassin, Laske and Masters (2000).
55  * The example prints out meta data about the model and then interpolates
56  * model values at a point in Tibet.
57  *
58  * Bassin, C., Laske, G. and Masters, G.,
59  * The Current Limits of Resolution for Surface Wave
60  * Tomography in North America, EOS Trans AGU, 81, F897, 2000.
61  * http://igppweb.ucsd.edu/~gabi/crust2.html
62  *
63  * @author sballar
64  *
65  */
66 public class TestCrust20
67 {
68  /**
69  * @param args
70  */
71  public static void main(String[] args)
72  {
73  try
74  {
75  if (args.length == 0)
76  throw new Exception(
77  "\nMust specify a single command line argument specifying " +
78  "the path to the file crust20.geotess\n");
79 
80  // specify the location of the GeoTess Crust 2.0 models.
81  File inputFile = new File(args[0]);
82 
83  System.out.printf("Loading model from file %s%n%n",
84  inputFile.getCanonicalPath());
85 
86  // instantiate a model and load the model from file
87  GeoTessModel model = new GeoTessModel(inputFile, OptimizationType.SPEED);
88 
89  // print out summary information about the model.
90  System.out.println(model);
91 
92  System.out.printf("\n");
93  System.out.printf("=============================================================================\n");
94  System.out.printf("\n");
95  System.out.printf("Interpolate Data\n");
96  System.out.printf("\n");
97  System.out.printf("=============================================================================\n\n");
98 
99  // instantiate a GeoTessPosition object which will manage the
100  // geographic position of an interpolation point, the interpolation
101  // coefficients, etc.
102  GeoTessPosition position = model.getGeoTessPosition(InterpolatorType.LINEAR);
103 
104  // set the position in the GeoTessPosition object to
105  // latitude = 30N, longitude = 90E, and radius equal to the
106  // top of layer 0.
107  position.setTop(0, VectorGeo.getVectorDegrees(30., 90.));
108 
109  // regurgitate the position
110  System.out.printf("Interpolated model properties at lat, lon = %1.4f, %1.4f:%n%n",
111  VectorGeo.getLatDegrees(position.getVector()),
112  VectorGeo.getLonDegrees(position.getVector()));
113 
114  // print a table with values interpolated from the model at the
115  // specified position
116  System.out.println("Layer Depth Thick vP vS density");
117  for (int layer = model.getMetaData().getNLayers() - 1; layer >= 0; --layer)
118  {
119  // change the radius of the position object to the radius of the
120  // top of specified layer.
121  position.setTop(layer);
122 
123  System.out.printf("%3d %10.4f %10.4f %10.4f %10.4f %10.4f%n",
124  layer,
125  position.getDepth(),
126  position.getLayerThickness(),
127  position.getValue(0),
128  position.getValue(1),
129  position.getValue(2));
130  }
131 
132  System.out.println();
133 
134  // print out the index of the triangle in which the point resides.
135  System.out.printf( "Interpolated point resides in triangle index = %d%n%n",
136  position.getTriangle());
137 
138  // print out a table with information about the 3 nodes at the
139  // corners of the triangle that contains the interpolation point.
140  // The information output is:
141  // the index of the node,
142  // node latitude in degrees,
143  // node longitude in degrees,
144  // interpolation coefficient, and
145  // distance in degrees from interpolation point.
146 
147  System.out.println(position.toString());
148 
149 
150  System.out.println("Call position.getWeights()");
151  HashMap<Integer, Double> weights = new HashMap<Integer, Double>();
152  position.getWeights(weights, 1.0);
153 
154  System.out.printf("geoposition_getWeights() returned weights for %d point indices:\n\n", weights.size());
155  System.out.printf("pointIndex weight\n");
156  double sumWeights = 0;
157  for (Integer pointIndex : weights.keySet())
158  {
159  double weight = weights.get(pointIndex);
160 
161  System.out.printf("%10d %10.6f\n", pointIndex, weight);
162  sumWeights += weight;
163  }
164  System.out.printf("\nSum of the weights is %1.6f\n", sumWeights);
165 
166  System.out.printf("\n");
167  System.out.printf("=============================================================================\n");
168  System.out.printf("\n");
169  System.out.printf("Query Model Data\n");
170  System.out.printf("\n");
171  System.out.printf("=============================================================================\n\n");
172 
173  // now we will extract some information about model values stored
174  // on grid nodes in the model. These are not interpolated values.
175 
176  // consider just one vertex. Vertex 57 is located in Tibet
177  int vertexId = 57;
178 
179  double[] u = model.getGrid().getVertex(vertexId);
180 
181  double earthRadius = VectorGeo.getEarthRadius(u);
182 
183  System.out.printf("Vertex=%d lat = %1.4f lon = %1.4f ellipsoid_radius = %1.3f\n\n", vertexId,
184  VectorGeo.getLatDegrees(u),
185  VectorGeo.getLonDegrees(u),
186  earthRadius);
187 
188  // write out the first header line which includes the names of the attributes
189  System.out.printf(" layer profile depth");
190  for (int attribute=0; attribute < model.getNAttributes(); ++attribute)
191  System.out.printf(" %8s", model.getMetaData().getAttributeName(attribute));
192  System.out.printf("\n");
193 
194  // print out second header line which includes attribute units
195  System.out.printf("layer name type (km) ");
196  for (int attribute=0; attribute < model.getNAttributes(); ++attribute)
197  System.out.printf(" %8s", model.getMetaData().getAttributeUnit(attribute));
198  System.out.printf("\n");
199 
200  System.out.printf("---------------------------------------------------------------------------\n");
201 
202  // loop over the layers in reverse order (shallowest to deepest)
203  for (int layer = model.getNLayers() - 1; layer >= 0; --layer)
204  {
205  // get the name of this layer
206  String layerName = model.getMetaData().getLayerName(layer);
207 
208  // get the Profile object that spans the current layer at the current vertex.
209  Profile profile = model.getProfile(vertexId, layer);
210 
211  // get the profile type: THIN, CONSTANT, NPOINT, etc.
212  ProfileType pType = profile.getType();
213 
214  // loop over every node in this layer in reverse order (shallowest to deepest).
215  // The upper limit can be either geoprofile_getNRadii(profile) or
216  // geoprofile_getNData(profile). The resulting table will differ depending
217  // on which is chosen.
218  for (int node=profile.getNRadii()-1; node >= 0; --node)
219  {
220  // get the radius of the current node in km.
221  double radius = profile.getRadius(node);
222 
223  // print layer id, layer name, profile type and radius
224  System.out.printf("%3d %-16s %-16s %8.3f", layer, layerName, pType, earthRadius-radius);
225 
226  // loop over all the attributes (vp, vs, density, etc) and print out values.
227  for (int attribute=0; attribute<model.getNAttributes(); ++attribute)
228  {
229  double value = profile.getValue(attribute, node);
230  System.out.printf(" %8.3f", value);
231  }
232  System.out.printf("\n");
233  }
234  System.out.printf("\n");
235  }
236 
237  System.out.printf("\n");
238  System.out.printf("=============================================================================\n");
239  System.out.printf("\n");
240  System.out.printf("Get Weights for a GreatCircle Raypath\n");
241  System.out.printf("\n");
242  System.out.printf("=============================================================================\n\n");
243 
244  // define two unit vectors, pointA and pointB.
245  // A is located at 0N, 0E and B is located at 10N, 10E.
246  double[] pointA = new double[3];
247  double[] pointB = new double[3];
248 
249  VectorGeo.getVectorDegrees(0., 0., pointA);
250  VectorGeo.getVectorDegrees(10., 10., pointB);
251 
252  GreatCircle greatCircle = new GreatCircle(pointA, pointB);
253 
254  ArrayList<double[]> rayPath = greatCircle.getPoints(31, false);
255 
256  // radii will the radius of each of the points along the rayPath.
257  double[] radii = new double[rayPath.size()];
258  Arrays.fill(radii, 6371.);
259 
260  // get the weights for the specified rayPaths.
261  model.getWeights(rayPath, radii, null,
262  InterpolatorType.LINEAR, InterpolatorType.LINEAR, weights);
263 
264  System.out.printf("model.getWeights() returned weights for %d point indices:\n\n",
265  weights.size());
266 
267  // initialize the sum of the weights to zero. The sum of the weights
268  // should equal the length of rayPath measured in km.
269  sumWeights=0;
270 
271  // loop over all the points in the model that were touched by rayPath
272  // and print out the pointIndex and the associated weight.
273  // Also sum up the weights.
274  System.out.printf("pointIndex weight\n");
275  for (Integer pointIndex : weights.keySet())
276  {
277  System.out.printf("%10d %10.6f\n", pointIndex, weights.get(pointIndex));
278  sumWeights += weights.get(pointIndex);
279  }
280 
281  System.out.printf("\n");
282  System.out.printf("sumWeights = %1.6f km\n", sumWeights);
283  System.out.printf("distance * 6371 = %1.6f km\n\n", greatCircle.getDistance() * 6371.);
284 
285  System.out.println("Done.");
286 
287  }
288  catch (Exception e)
289  {
290  e.printStackTrace();
291  }
292  }
293 
294 }
gov.sandia.geotess.examples.TestCrust20.main
static void main(String[] args)
Definition: TestCrust20.java:71
gov
gov.sandia.geotess
gov.sandia.geotess.examples.TestCrust20
Example application that loads a GeoTessModel that contains a representation of the Crust 2....
Definition: TestCrust20.java:67
gov.sandia