GeoTessJavaExamples  2.0
PopulateModel2D.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.Data;
39 import gov.sandia.geotess.GeoTessMetaData;
40 import gov.sandia.geotess.GeoTessModel;
41 import gov.sandia.geotess.GeoTessPosition;
42 import gov.sandia.gmp.util.globals.DataType;
43 import gov.sandia.gmp.util.globals.InterpolatorType;
44 import gov.sandia.gmp.util.numerical.vector.VectorGeo;
45 import gov.sandia.gmp.util.numerical.vector.VectorUnit;
46 
47 import java.io.File;
48 import java.util.Date;
49 
50 /**
51  * An example of how to generate a GeoTessModel and populate it with data.
52  * At every node in the 4 degree tessellation:
53  * <ul>
54  * <li>populate a new model with some simple data. The data consists of the
55  * distance in radians from every grid node to the geographic location of
56  * seismic station ANMO located at Latitude: 34.9462N Longitude: 106.4567W.
57  * <li>modify the data by converting it from radians to degrees.
58  * <li>interpolate a value from the grid and print the result to the screen.
59  * </ul>
60  * <p>
61  *
62  * @author sballar
63  *
64  */
65 public class PopulateModel2D
66 {
67  /**
68  * An example of how to generate a GeoTessModel and populate it with data.
69  * At every node in the 4 degree tessellation:
70  * <ul>
71  * <li>populate a new model with some simple data. The data consists of the
72  * distance in radians from every grid node to the geographic location of
73  * seismic station ANMO located at Latitude: 34.9462N Longitude: 106.4567W.
74  * <li>modify the data by converting it from radians to degrees.
75  * <li>interpolate a value from the grid and print the result to the screen.
76  * </ul>
77  * <p>
78  *
79  * @param args path to file geotess_grid_04000.geotess
80  */
81  public static void main(String[] args)
82  {
83  try
84  {
85  if (args.length == 0)
86  throw new Exception(
87  "\nMust specify a single command line argument specifying " +
88  "the path to the file geotess_grid_04000.geotess\n");
89 
90  System.out.println("Start simple example");
91  System.out.println();
92 
93  // Create a MetaData object in which we can specify information
94  // needed for model contruction.
95  GeoTessMetaData metaData = new GeoTessMetaData();
96 
97  // Specify a description of the model. This information is not
98  // processed in any way by GeoTess. It is carried around for
99  // information purposes.
100  metaData.setDescription(String
101  .format("Simple example of a GeoTess model,%n"
102  + "storing the distance from station ANMO %n"
103  + "near Albuquerque, New Mexico, USA%n"
104  + "Lat, lon = 34.9462, -106.4567 degrees.%n"
105  + "author: Sandy Ballard%n"
106  + "contact: sballar@sandia.gov%n"));
107 
108  // Specify a list of layer names. A model could have many layers,
109  // e.g., ("core", "mantle", "crust"), specified in order of
110  // increasing radius. This simple example has only one layer.
111  metaData.setLayerNames("surface");
112 
113  // Set layerID equal to the index of the one-and-only layer
114  // in this model.
115  int layerID = 0;
116 
117  // specify the names of the attributes and the units of the
118  // attributes in two String arrays. This model only includes
119  // one attribute.
120  // If this model had two attributes, they would be specified
121  // like this: setAttributes("Distance; Depth", "degrees; km");
122  metaData.setAttributes("Distance", "degrees");
123 
124  // specify the DataType for the data. All attributes, in all
125  // profiles, will have the same data type.
126  metaData.setDataType(DataType.FLOAT);
127 
128  // specify the name of the software that is going to generate
129  // the model. This gets stored in the model for future reference.
130  metaData.setModelSoftwareVersion("TestSimpleExample 1.0.0");
131 
132  // specify the date when the model was generated. This gets
133  // stored in the model for future reference.
134  metaData.setModelGenerationDate(new Date().toString());
135 
136  // specify the path to the file containing the grid to be used for
137  // this test. This information was passed in as a command line
138  // argument. Grids were included in the software delivery and
139  // are available from the GeoTess website.
140  String gridFile = new File(args[0]).getCanonicalPath();
141 
142  // call a GeoTessModel constructor to build the model. This will
143  // load the grid, and initialize all the data structures to null.
144  // To be useful, we will have to populate the data structures.
145  GeoTessModel model = new GeoTessModel(gridFile, metaData);
146 
147  // Each grid vertex will be assigned a single data value consisting
148  // of the epicentral distance in degrees from the location of the
149  // grid vertex to seismic station ANMO near Albuquerque, NM.
150  // Get unit vector representation of position of station ANMO.
151  double[] anmo = VectorGeo.getVectorDegrees(34.9462, -106.4567);
152 
153  // generate some data and store it in the model. The data consists
154  // of the angular distance in degrees from each vertex of the model
155  // grid to station ANMO near Albuquerque, NM, USA.
156  for (int vtx = 0; vtx < model.getGrid().getNVertices(); ++vtx)
157  {
158  // retrieve the unit vector corresponding to the i'th vertex of
159  // the grid.
160  double[] vertex = model.getGrid().getVertex(vtx);
161 
162  // compute the distance from the vertex to station ANMO.
163  float distance = (float) VectorUnit.angleDegrees(anmo, vertex);
164 
165  // Construct a new Data object that holds a single value of
166  // type float. Data.getData() can be called with multiple values
167  // (all of the same type), or an array of values. In this
168  // very simple example, there is only one value: distance.
169  Data data = Data.getDataFloat(distance);
170 
171  // associate the Data object with the specified vertex of the model.
172  // This instance of setProfile always creates a ProfileSurface object.
173  model.setProfile(vtx, data);
174  }
175 
176  // At this point, we have a fully functional GeoTessModel object
177  // that we can work with.
178 
179  // print a bunch of information about the model to the screen.
180  System.out.println(model.toString());
181 
182  // Obtain a GeoTessPosition object from the model. This object
183  // can be used to interpolate data from arbitrary points in the
184  // model. Specify which type of interpolation is to be used:
185  // linear or natural neighbor.
186  GeoTessPosition position = model
187  .getGeoTessPosition(InterpolatorType.LINEAR);
188 
189  // set the latitude and longitude of the GeoTessPosition object.
190  // This is the position on the Earth where we want to interpolate
191  // some data. This is the epicenter of the Sumatra-Andaman
192  // earthquake of 2004.
193  double lat = 3.316;
194  double lon = 95.854;
195  position.setTop(layerID, VectorGeo.getVectorDegrees(lat, lon));
196 
197  System.out.printf(
198  "Interpolation lat, lon = %7.3f deg, %7.3f deg%n%n",
199  VectorGeo.getLatDegrees(position.getVector()),
200  VectorGeo.getLonDegrees(position.getVector()));
201 
202  // retrieve the interpolated distance value at the most recent
203  // location specified in the GeoTessPostion object.
204  double distance = position.getValue(0);
205 
206  // Output the interpolated distance from the position specified in
207  // the GeoTessPosition object to station ANMO, in degrees.
208  System.out.printf("Interpolated distance from station ANMO = %1.3f degrees%n%n",
209  distance);
210 
211  // compute actual distance from ANMO to the position of interest.
212  double actualDistance = VectorUnit.angle(anmo,
213  position.getVector());
214 
215  System.out.printf("Actual distance from station ANMO = %1.3f degrees%n",
216  Math.toDegrees(actualDistance));
217 
218  System.out.println();
219 
220  // print out the index of the triangle in which point resides.
221  System.out.printf("Interpolated point resides in triangle index = %d%n%n",
222  position.getTriangle());
223 
224  // print out a table with the node indexes, node lat, node lon and
225  // interpolation coefficients for the nodes of the triangle that
226  // contains the point.
227  System.out.println(" Node Lat Lon Coeff");
228 
229  // get the indexes of the vertices that contribute to the
230  // interpolation.
231  int[] x = position.getVertices();
232 
233  // get the interpolation coefficients used in interpolation.
234  double[] coef = position.getHorizontalCoefficients();
235 
236  for (int j = 0; j < x.length; ++j)
237  {
238  int vtx = x[j];
239  System.out.printf(
240  "%6d %10.4f %10.4f %10.6f%n",
241  vtx,
242  VectorGeo.getLatDegrees(model.getGrid().getVertex(vtx)),
243  VectorGeo.getLonDegrees(model.getGrid().getVertex(vtx)),
244  coef[j]);
245  }
246  System.out.printf("%nSimple example completed successfully%n%n");
247 
248  }
249  catch (Exception ex)
250  {
251  ex.printStackTrace();
252  }
253 
254  }
255 
256 }
gov.sandia.geotess.examples.PopulateModel2D.main
static void main(String[] args)
An example of how to generate a GeoTessModel and populate it with data.
Definition: PopulateModel2D.java:81
gov
gov.sandia.geotess.examples.PopulateModel2D
An example of how to generate a GeoTessModel and populate it with data.
Definition: PopulateModel2D.java:66
gov.sandia.geotess
gov.sandia