GeoTessCPPExamples  2.0
PopulateModel2D.cc
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 #include "CPPUtils.h"
37 #include "CpuTimer.h"
38 #include "GeoTessGrid.h"
39 #include "GeoTessModel.h"
40 #include "GeoTessPosition.h"
41 
42 using namespace geotess;
43 
44 /**
45  * This file contains an example application that illustrate how to
46  * populate a 2D model with data. It loads a existing GeoTessGrid object from
47  * a file, populates it with information only on the surface of the
48  * model, writes the model to a file, reloads the model from file
49  * and interrogates it for basic information.
50  * <p>
51  * The program takes one command line argument which specifies the
52  * full path to the file GeoTessModels/crust20.geotess
53  * that was delivered with the GeoTess package.
54  */
55 int main(int argc, char** argv)
56 {
57  try
58  {
59  if(argc < 2)
60  {
61  cout << "Must supply a single command line argument specifying path to the GeoTessModels directory" << endl;
62  return -1;
63  }
64 
65  string path = argv[1];
66 
67  // specify the path to the file containing the grid to be use for this test.
68  string gridFile = CPPUtils::insertPathSeparator(path, "geotess_grid_04000.geotess");
69 
70  CpuTimer tmr;
71 
72  cout << "Start very simple example ..." << endl << endl;
73 
74  GeoTessMetaData *metaData = new GeoTessMetaData();
75 
76  // specify the ellipsoid that is to be used when interacting with this model. This call is
77  // really unnecessary here because WGS84 is the default, but other options are available.
78  metaData->setEarthShape("WGS84");
79 
80  string s = "Simple example of a GeoTess model\n";
81  s += "Storing the distance from station ANMO \n";
82  s += "near Albuquerque, New Mexico, USA\n";
83  s += "Lat, lon = 34.9462, -106.4567 degrees.\n";
84  s += "author: Sandy Ballard\n";
85  s += "contact: sballar@sandia.gov\n";
86  s += "November, 2011\n";
87 
88  metaData->setDescription(s);
89 
90  // Specify the name of the layer that this model supports.
91  // Since this is going to be a 2D model, only specify one layer name.
92  metaData->setLayerNames("Surface");
93 
94  // specify the names of the attributes and the units of the attributes in
95  // two String arrays. This model only includes one attribute.
96  metaData->setAttributes("Distance", "degrees");
97 
98  // specify the GeoTessDataType for the data. All attributes, in all profiles, will
99  // have the same data type.
100  metaData->setDataType(GeoTessDataType::FLOAT);
101 
102  // specify the name of the software that is going to generate
103  // the model. This gets stored in the model for future reference.
104  metaData->setModelSoftwareVersion("GeoTessCPPExamples.populateModel2D 1.0.0");
105 
106  // specify the date when the model was generated. This gets
107  // stored in the model for future reference.
108  metaData->setModelGenerationDate(CpuTimer::now());
109 
110  // call a GeoTessModel constructor to build the model. This will build the
111  // grid, and initialize all the data structures to null. To be useful, we
112  // will have to populate the data structures.
113  GeoTessModel* model = new GeoTessModel(gridFile, metaData);
114 
115  cout << endl << model->toString() << endl << endl;
116 
117  // retrieve a reference to the EarthShape that should be used when interacting with
118  // this model. This object will be used to convert between geographic and geocentric
119  // coordinates, and between depth and radius.
120  EarthShape& ellipsoid = model->getEarthShape();
121 
122  // get unit vector representation of position of station ANMO.
123  double anmo[3];
124  ellipsoid.getVectorDegrees(34.9462, -106.4567, anmo);
125 
126  // generate some data and store it in the model. The data consists of
127  // the angular distance in degrees from each vertex of the model grid to
128  // station ANMO near Albuquerque, NM, USA.
129 
130  // loop over all the vertices of the model
131  for (int vertex = 0; vertex < model->getNVertices(); ++vertex)
132  {
133  // retrieve the unit vector corresponding to the i'th vertex of the grid.
134  const double* unit_vector = model->getGrid().getVertex(vertex);
135 
136  // compute the distance from the vertex to station ANMO in degrees.
137  float value = (float) GeoTessUtils::angleDegrees(anmo, unit_vector);
138 
139  // set the model value associated with this vertex to the specified
140  // value. Note that 'value' in this case is a single value,
141  // but if this model supported multiple attributes on each vertex,
142  // then value could be an array of floats (or any other type).
143  model->setProfile(vertex, &value, 1);
144  }
145 
146  // at this point, the model is fully populated and ready for use.
147 
148  // Let's write the model out to a file, delete it, reload it from the
149  // same file and test it.
150 
151  // specify the name of the file to which to write the model.
152  string modelFileName = CPPUtils::insertPathSeparator(path, "populateModel2D.geotess");
153 
154  // write the model to a file. The first string is the name of the file
155  // that is to receive the output data. The second string is the relative path
156  // from where the data file is going to reside back to the existing
157  // GeoTessGrid file. By specifying '*', the grid information will be
158  // stored in the same file with the data
159 
160  //gridFile = "*";
161  model->writeModel(modelFileName);
162 
163  // we are done with this model, set it to null.
164  delete model;
165  model = NULL;
166 
167  // construct a new model by reading the old one back into memory.
168  model = new GeoTessModel(modelFileName);
169 
170  // print a bunch of information about the model to the screen.
171  cout << model->toString() << endl;
172 
173  // Instantiate a GeoTessPosition object, giving it a reference to the model.
174  // Specify which type of interpolation is to be used: linear or natural neighbor.
175  //GeoTessPosition* position = GeoTessPosition::getGeoTessPosition(model,
176  // GeoTessInterpolatorType::NATURAL_NEIGHBOR);
177  GeoTessPosition* position = model->getPosition(GeoTessInterpolatorType::LINEAR);
178 
179  // set the latitude and longitude of the GeoTessPosition object. This is
180  // the position on the Earth where we want to interpolate some data.
181  // This is the epicenter of the Sumatra-Andaman earthquake of 2004.
182  double lat = 3.316;
183  double lon = 95.854;
184  int layerID = 0;
185  double v[3];
186  ellipsoid.getVectorDegrees(lat, lon, v);
187 
188  position->setTop(layerID, v);
189 
190  char ss[300];
191  sprintf(ss, "Interpolation lat, lon, depth = %7.3f deg, %7.3f deg",
192  ellipsoid.getLatDegrees(position->getVector()),
193  ellipsoid.getLonDegrees(position->getVector()));
194  cout << ss << endl << endl;
195 
196  // retrieve the interpolated distance value at the most recent location specified
197  // in the GeoTessPostion object.
198  double distance = position->getValue(0);
199 
200  // Output the interpolated distance from the position specified in the GeoTessPosition
201  // object to station ANMO, in degrees.
202  sprintf(ss, "Interpolated distance from station ANMO = %1.3f degrees", distance);
203  //CPPUtils::toDegrees(distance));
204  cout << ss << endl << endl;
205 
206  // compute actual distance from ANMO to the position of interest.
207  double actualDistance = GeoTessUtils::angleDegrees(anmo, position->getVector());
208 
209  sprintf(ss, "Actual distance from station ANMO = %1.3f degrees", actualDistance);
210  cout << ss << endl << endl;
211 
212  // print out the index of the triangle in which point resides.
213  sprintf(ss, "Interpolated point resides in triangle index = %d", position->getTriangle());
214  cout << ss << endl;
215 
216  // print out a table with the node indexes, node lat, node lon and
217  // interpolation coefficients for the nodes of the triangle that
218  // contains the point.
219  cout << " Node Lat Lon Coeff" << endl;
220 
221  // get the indexes of the vertices that contribute to the interpolation.
222  const vector<int>& x = position->getVertices();
223 
224  // get the interpolation coefficients used in interpolation.
225  const vector<double>& coef = position->getHorizontalCoefficients();
226 
227  const GeoTessGrid& gridnew = model->getGrid();
228  for (int j = 0; j < (int) x.size(); ++j)
229  {
230  sprintf(ss, "%6d %10.4f %10.4f %10.6f", x[j],
231  ellipsoid.getLatDegrees(gridnew.getVertex(x[j])),
232  ellipsoid.getLonDegrees(gridnew.getVertex(x[j])), coef[j]);
233  cout << ss << endl;
234  }
235 
236  delete position;
237  delete model;
238 
239  cout << "Cpu Time (msec): " << tmr.cpuTime() << endl;
240  cout << endl << "End of populateModel2D" << endl << endl;
241  }
242  catch (const GeoTessException& ex)
243  {
244  cout << endl << ex.emessage << endl;
245  return 1;
246  }
247  catch (...)
248  {
249  cout << endl << "Unidentified error detected " << endl
250  << __FILE__ << " " << __LINE__ << endl;
251  return 2;
252  }
253  return 0;
254 }
geotess
Definition: AK135Model.h:55
main
int main(int argc, char **argv)
Definition: PopulateModel2D.cc:55