GeoTessCExamples  2.0
PopulateModel2D.c
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 "CpuTimerC.h"
37 #include "GeoTessMetaDataC.h"
38 #include "GeoTessModelC.h"
39 #include "GeoTessUtilsC.h"
40 #include "GeoTessGridC.h"
41 #include "InterpolatorTypeC.h"
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include "DataTypeC.h"
46 #include "ErrorHandler.h"
47 
48 #define len 512
49 
50 static const double RAD_TO_DEG = 180./3.1415926535897932384626;
51 static const double DEG_TO_RAD = 3.1415926535897932384626/180.;
52 
53 
54 void errorCheck();
55 
56 int main(int argc, char** argv)
57 {
58  double start;
59  int i,j, layerID, tmp;
60  GeoTessMetaDataC* metaData;
61  GeoTessModelC* model;
62  GeoTessGridC* grid, *gridnew;
63  char s[len];
64  double anmo[3];
65  GeoTessPositionC* position;
66  double lat, lon, distance, actualDistance;
67  float value;
68  double v[3];
69  char ss[300];
70  int* x;
71  int nx;
72  double* coef;
73  char* strTmp;
74  char modelFileName[1000];
75  char* gridFile;
76 
77  printf("%s", "Start PopulateModel2D ...\n");
78 
79  gridFile = NULL;
80  if(argc != 2){
81  printf("%s\n","Must provide a single command line argument specifying path to the file geotess_grid_04000.geotess which resides in the GeoTessModels directory");
82  return -1;
83  }else{
84  gridFile = argv[1];
85  }
86 
87  start = cpu_getCurrCPUTime();
88 
89  // construct a GeoTessMetaDataC object, which is basically a wrapper
90  // around a C++ GeoTessMetaData object.
91  metaData = geometadata_create();
92 
93  // specify the ellipsoid to use for converting between geographic and geocentric
94  // coordinates and between radius and depth. This is really not necessary here since
95  // WGS84 is the default, but other options are available.
96  geometadata_setEarthShape(metaData, "WGS84");
97 
98  s[0] = '\0';
99  strncat(s, "Simple example of a GeoTess model\n", len);
100  strncat(s, "Storing the distance from station ANMO \n", len);
101  strncat(s, "near Albuquerque, New Mexico, USA\n", len);
102  strncat(s, "Lat, lon = 34.9462, -106.4567 degrees.\n", len);
103  strncat(s, "author: Sandy Ballard\n", len);
104  strncat(s, "contact: sballar@sandia.gov\n", len);
105  strncat(s, "November, 2011\n", len);
106  geometadata_setDescription(metaData, s);
107 
108  // Specify a list of layer names. A 3D model could have many layers,
109  // e.g., ("core", "mantle", "crust"), specified in order of increasing
110  // radius. 2D models may only have one layer, but the name of the layer
111  // must be specified.
112  geometadata_setLayerNames1(metaData, (char*)"Surface");
113 
114  // specify the names of the attributes and the units of the attributes in
115  // two Strings. This model only includes one attribute. If this model were
116  // to have more than one attribute, the attribute names would be specified
117  // in one string, separated by semicolons, and the units would be specified
118  // in another string, also separated by semicolons. For unitless attributes,
119  // just specify each unit as a single space character.
120  geometadata_setAttributes1(metaData, "Distance", "degrees");
121 
122  // specify the DataType for the data. All attributes, at all nodes, will
123  // have the same data type.
124  geometadata_setDataType1(metaData, FLOAT);
125 
126  errorCheck();
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  // This can really help to find the code that generated a model after a long time.
131  geometadata_setModelSoftwareVersion(metaData, (char*)"GeoTessCExamples.PopulateModel2D 1.0.0");
132 
133  // specify the date when the model was generated. This gets
134  // stored in the model for future reference.
135  strTmp = cpu_now();
136  geometadata_setModelGenerationDate(metaData, strTmp);
137  free(strTmp);
138  strTmp = NULL;
139 
140  // call a GeoTessModel constructor to build the model. This will build the
141  // grid, and initialize all the data structures to null. To be useful, we
142  // will have to populate the data structures.
143  model = geomodel_create3(gridFile, metaData);
144 
145  // Delete the GeoTessMetaDataC object, which is a wrapper around a C++ GeoTessMetaData
146  // object. Ownership of the underlying C++ GeoTessMetaData object has been transfered
147  // to the newly created GeoTessModel object, which will delete the C++ GeoTessMetaData
148  // object when it is done with it.
149  geometadata_destroy(metaData);
150 
151  // retrieve a reference to the ellipsoid that is stored in the model. This ellipsoid
152  // will be used to convert between geographic and geocentric coordinates and between radius and depth.
153  EarthShapeC* ellipsoid = geomodel_getEarthShape(model);
154 
155  errorCheck();
156 
157  // print the model toString() function to the screen
158  strTmp = geomodel_toString(model);
159  printf("\n%s\n\n", strTmp);
160  free(strTmp);
161  strTmp = NULL;
162 
163  // get unit vector representation of position of station ANMO.
164  earthshape_getVectorDegrees(ellipsoid, 34.9462, -106.4567, anmo);
165 
166  // Get a GeoTessGridC wrapper around a C++ GeoTessGrid object.
167  // The referenced C++ GeoTessGrid object is owned by the GeoTessModel
168  // and we are simply getting a reference to it.
169  grid = geomodel_getGrid(model);
170 
171  // generate some data and store it in the model. The data consists of
172  // the angular distance in degrees from each vertex of the model grid to
173  // station ANMO near Albuquerque, NM, USA.
174  for (i = 0; i < geogrid_getNVertices(grid); ++i)
175  {
176  // retrieve the unit vector corresponding to the i'th vertex of the grid.
177  double* vertex = geogrid_getVertex(grid, i);
178 
179  // compute the distance from the vertex to station ANMO.
180  value = (float) geoutils_angleDegrees(anmo, vertex);
181 
182  geomodel_setProfSurfFloats(model, i, &value, 1);
183 
184  }
185 
186  // free the memory allocated for the C GeoTessGridC wrapper without deleting the
187  // C++ GeoTessGrid object that is inside the wrapper. The C++ GeoTessGrid object
188  // is 'owned' by the GeoTessModel object, which will manage its memory.
189  geogrid_destroy(grid);
190 
191  // Now let's write the model out to a file, delete it, reload it from the
192  // same file and test it.
193 
194  // specify the name of the file to which to write the model.
195  strncat(modelFileName, "populate_model_2d.geotess", len);
196 
197  // write the model to an ascii file. The first string is the name of the file
198  // that is to receive the output data. The second string is the relative path
199  // from where the data file is going to reside back to the existing
200  // GeoTessGeometry file. By specifying '*', the grid information will be
201  // stored in the same file with the data
202 
203  geomodel_writeModelParts(model, modelFileName, "*");
204 
205  // we are done with this model, free it.
206  geomodel_destroy(model);
207 
208  printf ("Read model %s\n", modelFileName);
209 
210  // construct a new model by reading the old one back into memory.
211  model = geomodel_create(modelFileName);
212 
213  errorCheck();
214 
215  // print a bunch of information about the model to the screen.
216  strTmp = geomodel_toString(model);
217  printf("%s\n", strTmp);
218  free(strTmp);
219  strTmp = NULL;
220 
221  // Instantiate a GeoTessPosition object. Specify which type of interpolation is to
222  // be used: linear or natural neighbor.
223  // We are getting a GeoTessPositionC wrapper around a C++ GeoTessPosition object.
224  // The C++ GeoTessModel from which we obtain the C++ GeoTessPosition object does
225  // not retain a reference to it; we are assuming ownership of the object and
226  // must delete it when we delete the GeoTessPositionC wrapper.
227  position = geomodel_getPosition2(model, LINEAR);
228 
229  // set the latitude and longitude of the GeoTessPosition object. This is
230  // the position on the Earth where we want to interpolate some data.
231  // This is the epicenter of the Sumatra-Andaman earthquake of 2004.
232  lat = 3.316;
233  lon = 95.854;
234  layerID = 0;
235  earthshape_getVectorDegrees(ellipsoid, lat, lon, v);
236  geoposition_setTop1(position, layerID, v);
237 
238  sprintf(ss, "Interpolation lat, lon, depth = %7.3f deg, %7.3f deg",
239  earthshape_getLatDegrees(ellipsoid, geoposition_getVector(position)),
240  earthshape_getLonDegrees(ellipsoid, geoposition_getVector(position)));
241  printf("%s\n", ss);
242  // retrieve the interpolated distance value at the most recent location specified
243  // in the GeoTessPostion object.
244  distance = geoposition_getValue(position, 0);
245 
246  // Output the interpolated distance from the position specified in the GeoTessPosition
247  // object to station ANMO, in degrees.
248  sprintf(ss, "Interpolated distance from station ANMO = %1.3f degrees", distance);
249  //CPPUtils::toDegrees(distance));
250  printf("%s\n", ss);
251  // compute actual distance from ANMO to the position of interest.
252  actualDistance = geoutils_angleDegrees(anmo, geoposition_getVector(position));
253 
254  sprintf(ss, "Actual distance from station ANMO = %1.3f degrees", actualDistance);
255  printf("%s\n", ss);
256  // print out the index of the triangle in which point resides.
257  sprintf(ss, "Interpolated point resides in triangle index = %d", geoposition_getTriangle(position));
258  printf("%s\n", ss);
259 
260  // print out a table with the node indexes, node lat, node lon and
261  // interpolation coefficients for the nodes of the triangle that
262  // contains the point.
263  printf("%s\n", "Vertex Lat Lon Coeff");
264  // get the indexes of the vertices that contribute to the interpolation.
265  x = geoposition_getVertices(position);
266  nx = geoposition_getNVertices(position);
267 
268  // get the interpolation coefficients used in interpolation.
269  geoposition_getHorizontalCoefficients(position, &coef, &tmp);
270  gridnew = geomodel_getGrid(model);
271  for (j = 0; j < nx; ++j)
272  {
273  sprintf(ss, "%6d %10.4f %10.4f %10.6f", x[j],
274  earthshape_getLatDegrees(ellipsoid, geogrid_getVertex(gridnew, x[j])),
275  earthshape_getLonDegrees(ellipsoid, geogrid_getVertex(gridnew, x[j])), coef[j]);
276  printf("%s\n", ss);
277  }
278  free(x);
279  free(coef);
280 
281  earthshape_destroy(ellipsoid);
282 
283  // free the C GeoTessPositionC wrapper around the C++ GeoTessPosition object.
284  // The underlying C++ GeoTessPosition object will be deleted as well.
285  geoposition_destroy(position);
286 
287  // free the C GeoTessGridC wrapper around the C++ GeoTessGrid object
288  // without freeing the underlying GeoTessGrid. It will be freed by
289  // the GeoTessModel when it gets deleted (next).
290  geogrid_destroy(gridnew);
291 
292  // free the C GeoTessModelC wrapper around the C++ GeoTessModel object.
293  // The underlying C++ GeoTessModel object will be deleted as well.
294  geomodel_destroy(model);
295 
296  printf("Cpu Time (msec): %f\n", cpu_getCurrCPUTime() - start);
297  printf("%s", "End of PopulateModel2D\n\n");
298 
299  return 0;
300 }
301 
303 {
304  // print out error messages from the message stack,
305  // most recent error first.
306  while (error_exists())
307  {
308  fprintf(stderr, "%s\n", error_getMessage());
309 
310  // if there were any error messages to start with,
311  // and the last error message has been printed,
312  // then exit.
313  if (!error_exists())
314  exit(-1);
315  }
316 
317 }
errorCheck
void errorCheck()
Definition: PopulateModel2D.c:302
RAD_TO_DEG
static const double RAD_TO_DEG
Definition: PopulateModel2D.c:50
DEG_TO_RAD
static const double DEG_TO_RAD
Definition: PopulateModel2D.c:51
len
#define len
Definition: PopulateModel2D.c:48
main
int main(int argc, char **argv)
Definition: PopulateModel2D.c:56