37 #include "CPPGlobals.h"
39 #include "GeoTessGrid.h"
40 #include "GeoTessModel.h"
41 #include "GeoTessPosition.h"
42 #include "GeoTessModelUtils.h"
90 int main(
int argc,
char** argv)
98 const int& attributeIndex,
const vector<float>& attributeChanges);
99 GeoTessModel*
hitCount(GeoTessModel* inputModel,
const vector<double**>& rayPaths);
100 GeoTessModel*
refineModel(GeoTessModel* oldModel, GeoTessModel* hitCountModelRefined);
106 EarthShape ellipsoid(
"WGS84");
111 ellipsoid.getVectorDegrees(34.9462, -106.4567,
ANMO);
117 string gridFile =
"geotess_grid_04000.geotess";
119 if (!CPPUtils::fileExists(gridFile))
121 cout <<
"ERROR: grid file " << gridFile <<
" does not exist." << endl
122 <<
"This program must be run from directory GeoTessBuilderExamples/tomo2dTest" << endl;
131 vector<double**> rayPaths;
143 GeoTessPolygon* polygon =
new GeoTessPolygon(
ANMO, CPPUtils::toRadians(38), 100);
144 model->setActiveRegion(polygon);
165 vector<float> attributeChanges;
166 for (
int i=0; i<model->getNPoints(); ++i)
167 attributeChanges.push_back(0.01F);
188 GeoTessModel* hitCountModel =
hitCount(model, rayPaths);
191 hitCountModel->writeModel(
"hitcount_model_original.geotess");
212 GeoTessModel* hitCountModelRefined =
new GeoTessModel(
"hitcount_model_refined.geotess");
220 GeoTessModel* refinedModel =
refineModel(model, hitCountModelRefined);
222 refinedModel->writeModel(
"attenuation_model_refined.geotess");
226 delete hitCountModel;
228 delete hitCountModelRefined;
232 for (
int i=0; i<(int)rayPaths.size(); ++i)
233 CPPUtils::delete2DArray<double>(rayPaths[i]);
239 catch (
const GeoTessException& ex)
241 cout << endl << ex.emessage << endl;
246 cout << endl <<
"Unidentified error detected " << endl
247 << __FILE__ <<
" " << __LINE__ << endl;
266 printf(
"************************************************************\n");
268 printf(
"* startingModel()\n");
270 printf(
"************************************************************\n");
280 GeoTessMetaData* metaData =
new GeoTessMetaData();
285 metaData->setEarthShape(
"WGS84");
290 metaData->setDescription(
"GeoTessModel for example program Tomography2D");
293 metaData->setLayerNames(
"surface");
296 metaData->setAttributes(
"attenuation",
"1/km");
300 metaData->setDataType(GeoTessDataType::FLOAT);
304 metaData->setModelSoftwareVersion(
"Tomography2D");
308 metaData->setModelGenerationDate(CpuTimer::now());
313 GeoTessModel* model =
new GeoTessModel(gridFile, metaData);
315 float attenuation = 0.1F;
318 for (
int vtx = 0; vtx < model->getNVertices(); ++vtx)
320 model->setProfile(vtx, &attenuation, 1);
322 printf(
"%s\n", model->toString().c_str());
345 printf(
"************************************************************\n");
347 printf(
"* generateRayPaths()\n");
349 printf(
"************************************************************\n\n");
353 EarthShape ellipsoid(
"WGS84");
358 for (
int i = 0; i < nRays; ++i)
360 double** path = CPPUtils::new2DArray<double>(2,3);
361 rayPaths.push_back(path);
362 double*
event = path[0];
363 double* station = path[1];
365 station[0] =
ANMO[0];
366 station[1] =
ANMO[1];
367 station[2] =
ANMO[2];
371 GeoTessUtils::moveDistAz(station, CPPUtils::toRadians(i*5), CPPUtils::toRadians(i * 36), event);
377 printf(
" id event station distance azimuth\n");
378 for (
int i = 0; i < nRays; ++i)
380 double** rayPath = rayPaths[i];
381 double*
event = rayPath[0];
382 double* station = rayPath[1];
384 printf(
"%3d %s %s %10.4f %10.4f\n", i,
385 ellipsoid.getLatLonString(event).c_str(),
386 ellipsoid.getLatLonString(station).c_str(),
387 GeoTessUtils::angleDegrees(station, event),
388 GeoTessUtils::azimuthDegrees(station, event, NaN_DOUBLE)
418 printf(
"************************************************************\n");
420 printf(
"* rayTrace()\n");
422 printf(
"************************************************************\n\n");
430 double pointSpacing = CPPUtils::toRadians(0.1);
436 double earthRadius = -1;
439 const GeoTessInterpolatorType& interpType = GeoTessInterpolatorType::NATURAL_NEIGHBOR;
461 map<int, double> weights;
464 for (
int i = 0; i < (int)rayPaths.size(); ++i)
468 double** rayPath = rayPaths[i];
469 double*
event = rayPath[0];
470 double* station = rayPath[1];
480 double attenuation = model->getPathIntegral2D(attribute,
481 event, station, pointSpacing, earthRadius, interpType, &weights);
490 printf(
"----------------------------------------------------------------------------\n");
491 printf(
"ray station event distance azimuth attenuation\n");
492 printf(
"%3d %s %s %10.4f %10.4f %12.5f\n\n", i,
493 model->getEarthShape().getLatLonString(station).c_str(),
494 model->getEarthShape().getLatLonString(event).c_str(),
495 GeoTessUtils::angleDegrees(station, event),
496 GeoTessUtils::azimuthDegrees(station, event, NaN_DOUBLE),
500 printf(
"pointId weight | point lat, lon, distance and azimuth from station\n");
502 double sumWeights = 0;
504 if (weights.size() == 0)
505 printf(
"No weights because event-station distance = 0\n");
507 map<int, double>::iterator it;
508 for (it=weights.begin(); it != weights.end(); ++it)
510 int pointIndex = it->first;
511 double weight = it->second;
513 sumWeights += weight;
517 printf(
"%7d %9.2f | outside polygon\n", pointIndex, weight);
521 const double* gridNode = model->getPointMap()->getPointUnitVector(pointIndex);
523 printf(
"%7d %9.2f | %s %10.4f %10.4f\n",
525 model->getEarthShape().getLatLonString(gridNode).c_str(),
526 GeoTessUtils::angleDegrees(station, gridNode),
527 GeoTessUtils::azimuthDegrees(station, gridNode, NaN_DOUBLE));
532 printf(
"Sum of weights (length of ray path) = %10.4f km \n\n", sumWeights);
546 printf(
"************************************************************\n");
548 printf(
"* regularization()\n");
550 printf(
"************************************************************\n\n");
557 int level = model->getGrid().getLastLevel(tessId);
564 for (
int pointIndex = 0; pointIndex < model->getNPoints(); ++pointIndex)
570 int vertexId = model->getPointMap()->getVertexIndex(pointIndex);
573 const double* vertex = model->getGrid().getVertex(vertexId);
578 model->getGrid().getVertexNeighbors(tessId, level, vertexId, order, neighbors);
581 if (pointIndex % 100 == 0)
583 printf(
"--------------------------------------------------------\n");
584 printf(
"point %d, vertex %d, lat,lon %s:\n\n",
585 pointIndex, vertexId,
586 model->getEarthShape().getLatLonString(vertex).c_str());
588 printf(
"neighbor neighbor distance azimuth\n");
589 printf(
"vertexid pointid (deg) (deg)\n");
590 set<int>::iterator it;
591 for (it=neighbors.begin(); it != neighbors.end(); ++it)
593 int neighborVertexId = *it;
597 const double* neighborVertex = model->getGrid().getVertex(neighborVertexId);
600 int neighborPointId = model->getPointMap()->getPointIndex(
601 neighborVertexId, 0, 0);
603 printf(
"%8d %8d %8.2f %8.2f\n", neighborVertexId,
605 GeoTessUtils::angleDegrees(vertex, neighborVertex),
606 GeoTessUtils::azimuthDegrees(vertex, neighborVertex, NaN_DOUBLE));
622 const int& attributeIndex,
const vector<float>& attributeChanges)
624 for (
int pointIndex = 0; pointIndex < model->getNPoints(); ++pointIndex)
625 model->setValue(pointIndex, attributeIndex,
626 model->getValueFloat(pointIndex, attributeIndex)
627 + attributeChanges[pointIndex]);
639 GeoTessModel*
hitCount(GeoTessModel* inputModel,
const vector<double**>& rayPaths)
641 printf(
"************************************************************\n");
643 printf(
"* hitCount()\n");
645 printf(
"************************************************************\n");
649 GeoTessMetaData* metaData =
new GeoTessMetaData();
654 metaData->setEarthShape(
"WGS84");
657 metaData->setDescription(
"GeoTessModel of hit count for example program Tomography2D");
660 metaData->setLayerNames(
"surface");
663 metaData->setAttributes(
"HIT_COUNT",
"count");
666 metaData->setDataType(GeoTessDataType::INT);
670 metaData->setModelSoftwareVersion(
"Tomography2D hitCount()");
674 metaData->setModelGenerationDate(CpuTimer::now());
678 GeoTessModel* hitCountModel =
new GeoTessModel(&inputModel->getGrid(), metaData);
683 for (
int vertexId = 0; vertexId < hitCountModel->getNVertices(); ++vertexId)
684 hitCountModel->setProfile(vertexId, &count, 1);
688 hitCountModel->setActiveRegion(inputModel->getPointMap()->getPolygon());
691 double pointSpacing = CPPUtils::toRadians(0.1);
694 const GeoTessInterpolatorType* interpType = &GeoTessInterpolatorType::LINEAR;
697 int attributeIndex = 0;
702 map<int, double> weights;
705 for (
int i = 0; i < (int)rayPaths.size(); ++i)
709 double** rayPath = rayPaths[i];
710 double*
event = rayPath[0];
711 double* station = rayPath[1];
719 inputModel->getPathIntegral2D(-1, event, station, pointSpacing, -1., *interpType, &weights);
721 map<int, double>::iterator it;
722 for (it=weights.begin(); it != weights.end(); ++it)
724 int pointIndex = it->first;
727 int hitcount = hitCountModel->getValueInt(pointIndex, attributeIndex);
729 hitCountModel->setValue(pointIndex, attributeIndex, hitcount);
736 printf(
" point vertex lat lon distance hitcount\n");
737 for (
int pointIndex = 0; pointIndex < hitCountModel->getNPoints(); ++pointIndex)
738 if (hitCountModel->getValueInt(pointIndex, 0) > 0)
740 const double* u = hitCountModel->getPointMap()->getPointUnitVector( pointIndex);
742 printf(
"%8d %8d %s %9.2f %6d\n",
744 hitCountModel->getPointMap()->getVertexIndex(pointIndex),
745 hitCountModel->getPointMap()->getPointLatLonString(pointIndex).c_str(),
746 GeoTessUtils::angleDegrees(
ANMO, u),
747 hitCountModel->getValueInt(pointIndex, 0));
751 return hitCountModel;
769 GeoTessModel*
refineModel(GeoTessModel* oldModel, GeoTessModel* hitCountModelRefined)
771 printf(
"************************************************************\n");
773 printf(
"* refineModel()\n");
775 printf(
"************************************************************\n");
778 GeoTessGrid refinedGrid = hitCountModelRefined->getGrid();
782 GeoTessModel* newModel =
new GeoTessModel(
783 &hitCountModelRefined->getGrid(), &oldModel->getMetaData());
788 GeoTessPosition* pos = oldModel->getPosition(GeoTessInterpolatorType::LINEAR);
795 int attributeIndex = 0;
799 for (
int vtx = 0; vtx < newModel->getNVertices(); ++vtx)
804 const double* u = refinedGrid.getVertex(vtx);
809 int oldVtx = oldModel->getGrid().getVertexIndex(u);
813 attenuation = oldModel->getValueFloat(oldVtx, layerIndex, nodeIndex, attributeIndex);
817 pos->set(layerIndex, u, 6371.);
818 attenuation = (float)pos->getValue(0);
822 newModel->setProfile(vtx, &attenuation, 1);