[cig-commits] r15241 - cs/spatialdata-0.1/trunk/templates/spatialdb

brad at geodynamics.org brad at geodynamics.org
Sun Jun 14 15:35:39 PDT 2009


Author: brad
Date: 2009-06-14 15:35:38 -0700 (Sun, 14 Jun 2009)
New Revision: 15241

Added:
   cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.cc
   cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.hh
   cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.i
   cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.py
   cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdatacontrib.i
Removed:
   cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.cc
   cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.hh
   cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.icc
   cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.py
   cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdb.pyxe.src
Modified:
   cs/spatialdata-0.1/trunk/templates/spatialdb/Makefile.am
   cs/spatialdata-0.1/trunk/templates/spatialdb/__init__.py
   cs/spatialdata-0.1/trunk/templates/spatialdb/configure.ac
Log:
Working template for adding a simple material database extension module.

Deleted: cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.cc
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.cc	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.cc	2009-06-14 22:35:38 UTC (rev 15241)
@@ -1,144 +0,0 @@
-// -*- C++ -*-
-//
-// ======================================================================
-//
-//                           Brad T. Aagaard
-//                        U.S. Geological Survey
-//
-// {LicenseText}
-//
-// ======================================================================
-//
-
-#include <portinfo>
-
-#include "GeoGridVelModel.hh" // Implementation of class methods
-
-#include "spatialdata/geocoords/CSGeo.hh" // USES CSGeo
-#include "spatialdata/geocoords/Converter.hh" // USES Converter
-
-#include <math.h> // USES pow()
-#include <sstream> // USES std::ostringstream
-#include <stdexcept> // USES std::logic_error
-#include <string.h> // USES memcpy()
-#include <strings.h> // USES strcasecmp()
-#include <assert.h> // USES assert()
-
-// ----------------------------------------------------------------------
-// Constructor
-yourname::spatialdb::GeoGridVelModel::GeoGridVelModel(void) :
-  SpatialDB("My velocity model"),
-  _filename(""),
-  _csGeo(new spatialdata::geocoords::CSGeo),
-  _queryVals(0),
-  _querySize(0)
-{ // constructor
-  _csGeo->datumHoriz("WGS84");
-  _csGeo->datumVert("mean sea level");
-  _csGeo->ellipsoid("WGS84");
-  _csGeo->initialize();
-} // constructor
-
-// ----------------------------------------------------------------------
-// Destructor
-yourname::spatialdb::GeoGridVelModel::~GeoGridVelModel(void)
-{ // destructor
-  // Deallocate all data
-  delete _csGeo; _csGeo = 0;
-
-  delete[] _queryVals; _queryVals = 0;
-  _querySize = 0;
-
-} // destructor
-
-// ----------------------------------------------------------------------
-// Open the database and prepare for querying.
-void
-yourname::spatialdb::GeoGridVelModel::open(void)
-{ // open
-  // Read the velocity model and do any necessary pre-query stuff.
-} // open
-
-// ----------------------------------------------------------------------
-// Close the database.
-void
-yourname::spatialdb::GeoGridVelModel::close(void)
-{ // close
-  // Deallocate all data allocated in open() and query().
-} // close
-
-// ----------------------------------------------------------------------
-// Set values to be returned by queries.
-void
-yourname::spatialdb::GeoGridVelModel::queryVals(const char** names,
-					    const int numVals)
-{ // queryVals
-  if (0 == numVals) {
-    std::ostringstream msg;
-    msg
-      << "Number of values for query in spatial database " << label()
-      << "\n must be positive.\n";
-    throw std::runtime_error(msg.str());
-  } // if
-  assert(0 != names && 0 < numVals);
-  
-  _querySize = numVals;
-  delete[] _queryVals; _queryVals = new int[numVals];
-  for (int iVal=0; iVal < numVals; ++iVal) {
-    // Set the values to be returned in a query.
-    if (0 == strcasecmp(names[iVal], "vp"))
-      _queryVals[iVal] = QUERY_VP;
-    else if (0 == strcasecmp(names[iVal], "vs"))
-      _queryVals[iVal] = QUERY_VS;
-    else if (0 == strcasecmp(names[iVal], "density")) {
-      _queryVals[iVal] = QUERY_DENSITY;
-      std::ostringstream msg;
-      msg
-	<< "Could not find value " << names[iVal] << " in spatial database\n"
-	<< label() << ". Available values are:\n"
-	<< "vp, vs, density, topo-elev, basement-depth, moho-depth, vp-tag.";
-      throw std::runtime_error(msg.str());
-    } // else
-  } // for
-} // queryVals
-
-// ----------------------------------------------------------------------
-// Query the database.
-int
-yourname::spatialdb::GeoGridVelModel::query(double* vals,
-					const int numVals,
-					const double* coords,
-					const int numDims,
-					const spatialdata::geocoords::CoordSys* csQuery)
-{ // query
-  if (0 == _querySize) {
-    std::ostringstream msg;
-    msg
-      << "Values to be returned by spatial database " << label() << "\n"
-      << "have not been set. Please call queryVals() before query().\n";
-    throw std::runtime_error(msg.str());
-  } // if
-  else if (numVals != _querySize) {
-    std::ostringstream msg;
-    msg
-      << "Number of values to be returned by spatial database "
-      << label() << "\n"
-      << "(" << _querySize << ") does not match size of array provided ("
-      << numVals << ").\n";
-    throw std::runtime_error(msg.str());
-  } // if
-
-  // Convert coordinates to local coordinate system
-  memcpy(_xyzGeo, coords, numDims*sizeof(double));
-  spatialdata::geocoords::Converter::convert(_xyzGeo, 1, numDims, 
-					     _csGeo, csQuery);
-
-  int queryFlag = 0;
-  
-  // ADD QUERY STUFF HERE
-
-  return queryFlag;
-} // query
-
-
-// End of file

Deleted: cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.hh
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.hh	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.hh	2009-06-14 22:35:38 UTC (rev 15241)
@@ -1,128 +0,0 @@
-// -*- C++ -*-
-//
-// ======================================================================
-//
-//                           Brad T. Aagaard
-//                        U.S. Geological Survey
-//
-// {LicenseText}
-//
-// ======================================================================
-//
-
-#if !defined(yourname_spatialdb_geogridvelmodel_hh)
-#define yourname_spatialdb_geogridvelmodel_hh
-
-#include "spatialdata/spatialdb/SpatialDB.hh" // ISA SpatialDB
-
-#include <string> // HASA std::string
-
-namespace yourname {
-  namespace spatialdb {
-    class GeoGridVelModel;
-
-    class TestGeoGridVelModel; // unit testing
-  } // spatialdb
-} // yourname
-
-namespace spatialdata {
-  namespace geocoords {
-    class CSGeo; // HASA CSGeo
-  } // geocoords
-} // spatialdata
-
-class yourname::spatialdb::GeoGridVelModel : spatialdata::spatialdb::SpatialDB
-{ // GeoGridVelModel
-  friend class TestGeoGridVelModel; // unit testing
- 
-// PUBLIC MEMBERS ///////////////////////////////////////////////////////
-public :
- 
-  /// Constructor
-  GeoGridVelModel(void);
- 
-  /// Destructor
-  ~GeoGridVelModel(void);
- 
-  /** Set filename of velocity model.
-   *
-   * @param name Filename of velocity model.
-   */
-  void filename(const char* name);
- 
-  /// Open the database and prepare for querying.
-  void open(void);
- 
-  /// Close the database.
-  void close(void);
- 
-  /** Set values to be returned by queries.
-   *
-   * @pre Must call open() before queryVals()
-   *
-   * @param names Names of values to be returned in queries
-   * @param numVals Number of values to be returned in queries
-   */
-  void queryVals(const char** names,
-		 const int numVals);
- 
-  /** Query the database.
-   *
-   * @note pVals should be preallocated to accommodate numVals values.
-   *
-   * @pre Must call open() before query()
-   *
-   * @param vals Array for computed values (output from query), must be
-   *   allocated BEFORE calling query().
-   * @param numVals Number of values expected (size of pVals array)
-   * @param coords Coordinates of point for query
-   * @param numDims Number of dimensions for coordinates
-   * @param pCSQuery Coordinate system of coordinates
-   *
-   * @returns 0 on success, 1 on failure (i.e., could not interpolate)
-   */
-  int query(double* vals,
-	    const int numVals,
-	    const double* coords,
-	    const int numDims,
-	    const spatialdata::geocoords::CoordSys* pCSQuery);
- 
- 
-// NOT IMPLEMENTED //////////////////////////////////////////////////////
-private :
- 
-  GeoGridVelModel(const GeoGridVelModel&); ///< Not implemented
-  const GeoGridVelModel& operator=(const GeoGridVelModel&); ///< Not implemented
- 
-// PRIVATE ENUMS ////////////////////////////////////////////////////////
-private :
- 
-  // Indices of values in database (used in queryVals to indicate
-  // which values to return in a query).
-  enum QueryValsEnum {
-    QUERY_VP=0, // vp
-    QUERY_VS=1, // vs
-    QUERY_DENSITY=2, // density
-  }; // ValsEnum
-
-// PRIVATE MEMBERS //////////////////////////////////////////////////////
-private :
-
-  double _xyzGeo[3];
-
-  // Add members for holding velocity model information.
-
-  std::string _filename;
-  spatialdata::geocoords::CSGeo* _csGeo;
-
-  int* _queryVals; ///< Indices of values to be returned in queries.
-  int _querySize; ///< Number of values requested to be returned in queries.
-
-}; // GeoGridVelModel
-
-#include "GeoGridVelModel.icc" // inline methods
-
-#endif // yourname_spatialdb_geogridvelmodel_hh
-
-
-// End of file 

Deleted: cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.icc
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.icc	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.icc	2009-06-14 22:35:38 UTC (rev 15241)
@@ -1,23 +0,0 @@
-// -*- C++ -*-
-//
-// ----------------------------------------------------------------------
-//
-//                           Brad T. Aagaard
-//                        U.S. Geological Survey
-//
-// <LicenseText>
-//
-// ----------------------------------------------------------------------
-//
-
-#if !defined(yourname_spatialdb_geogridvelmodel_hh)
-#error "GeoGridVelModel.icc must only be included from GeoGridVelModel.hh"
-#endif
-
-// Set filename of velocity model.
-void
-yourname::spatialdb::GeoGridVelModel::filename(const char* name) {
-  _filename = name;
-} // filename
-
-// End of file

Deleted: cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.py
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.py	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/GeoGridVelModel.py	2009-06-14 22:35:38 UTC (rev 15241)
@@ -1,107 +0,0 @@
-#!/usr/bin/env python
-#
-# ----------------------------------------------------------------------
-#
-#                           Brad T. Aagaard
-#                        U.S. Geological Survey
-#
-# <LicenseText>
-#
-# ----------------------------------------------------------------------
-#
-
-# 1. Rename the GeoGridVelModel class to something appropriate.
-# 2. Adjust the Pyre properties and facilities for user parameters.
-# 3. Update the constructor, __init__().
-# 4. Update initialize().
-# 5. Update _configure().
-
-## @brief Python manager for spatial database interface to a new velocity
-## model.
-##
-## Factory: spatial_database
-
-from spatialdata.spatialdb.SpatialDB import SpatialDB
-
-# GeoGridVelModel class
-class GeoGridVelModel(SpatialDB):
-  """
-  Python manager for spatial database to a new velocity model.
-
-  Factory: spatial_database
-  """
-
-  # INVENTORY //////////////////////////////////////////////////////////
-
-  class Inventory(SpatialDB.Inventory):
-    """
-    Python object for managing GeoGridVelModel facilities and properties.
-    """
-
-    ## @class Inventory
-    ## Python object for managing GeoGridVelModel facilities and properties.
-    ##
-    ## \b Properties
-    ## @li \b filename Filename for seismic velocity model.
-    ##
-    ## \b Facilities
-    ## @li none
-
-    import pyre.inventory
-
-    filename = pyre.inventory.str("filename", default="model.txt")
-    filename.meta['tip'] = "Filename for seismic velocity model."
-
-
-  # PUBLIC METHODS /////////////////////////////////////////////////////
-
-  # Change this name.
-  def __init__(self, name="geogridvelmodel"):
-    """
-    Constructor.
-    """
-    SpatialDB.__init__(self, name) # Call parent function
-
-    # Create handle to corresponding C++ object
-    import spatialdb as bindings
-    self.cppHandle = bindings.GeoGridVelModel()
-    self.cppHandle.label = "Geographic gridded velocity model"
-    return
-
-
-  def initialize(self):
-    """
-    Initialize database.
-    """
-    SpatialDB.initialize(self) # Call parent function
-
-    # Transfer Pyre properties/facilities to C++
-    self.cppHandle.filename(self.filename)
-    return
-  
-
-  # PRIVATE METHODS ////////////////////////////////////////////////////
-
-  def _configure(self):
-    """
-    Set members based on inventory.
-    """
-    SpatialDB._configure(self) # Call parent function.
-    
-    # Transfer inventory to object
-    self.filename = self.inventory.filename
-    return
-
-
-# FACTORIES ////////////////////////////////////////////////////////////
-
-# Factory used when setting GeoGridVelModel to a Pyre
-# 'spatial_database' facility.
-def spatial_database():
-  """
-  Factory associated with GeoGridVelModel.
-  """
-  return GeoGridVelModel()
-
-
-# End of file 

Modified: cs/spatialdata-0.1/trunk/templates/spatialdb/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/Makefile.am	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/Makefile.am	2009-06-14 22:35:38 UTC (rev 15241)
@@ -14,50 +14,57 @@
 
 
 # LIBRARY --------------------------------------------------------------
-lib_LTLIBRARIES = libyournamespatialdb.la
+lib_LTLIBRARIES = libspatialdatacontrib.la
 
-libyournamespatialdb_la_SOURCES = \
-	GeoGridVelModel.cc
+libspatialdatacontrib_la_SOURCES = \
+	UniformVelModel.cc
 
-libyournamespatialdb_la_LDFLAGS = $(AM_LDFLAGS)
+libspatialdatacontrib_la_LDFLAGS = $(AM_LDFLAGS)
 
-libyournamespatialdb_la_LIBADD = \
+libspatialdatacontrib_la_LIBADD = \
 	-lspatialdata \
 	-lproj
+if NO_UNDEFINED
+libspatialdatacontrib_la_LIBADD += \
+	$(PYTHON_BLDLIBRARY) $(PYTHON_LIBS) $(PYTHON_SYSLIBS)
+endif
 
+AM_CPPFLAGS = $(PYTHON_EGG_CPPFLAGS) -I$(PYTHON_INCDIR) 
+
 INCLUDES = 
 
 # MODULE ---------------------------------------------------------------
 
-pkgpyexec_LTLIBRARIES = spatialdbmodule.la
+pkgpyexec_LTLIBRARIES = _spatialdatacontribmodule.la
 
-spatialdbmodule_la_LDFLAGS = -module -avoid-version \
+pkgpyexec_PYTHON = spatialdatacontrib.py
+
+swig_sources = \
+	spatialdatacontrib.i \
+	UniformVelModel.i
+
+swig_generated = \
+	spatialdatacontrib_wrap.cxx \
+	spatialdatacontrib.py
+
+_spatialdatacontribmodule_la_LDFLAGS = -module -avoid-version \
 	$(AM_LDFLAGS) $(PYTHON_LA_LDFLAGS)
 
-dist_spatialdbmodule_la_SOURCES = spatialdb.pyxe.src
-nodist_spatialdbmodule_la_SOURCES = \
-	yourname.spatialdb.pyxe \
-	yourname.spatialdb.c yourname.spatialdb_embed.cpp yourname.spatialdb_embed.h
+dist__spatialdatacontribmodule_la_SOURCES = $(swig_sources) $(swig_generated)
 
-spatialdbmodule_la_LIBADD = libyournamespatialdb.la
+_spatialdatacontribmodule_la_LIBADD = libspatialdatacontrib.la
 if NO_UNDEFINED
-spatialdbmodule_la_LIBADD += \
+_spatialdatacontribmodule_la_LIBADD += \
 	$(PYTHON_BLDLIBRARY) $(PYTHON_LIBS) $(PYTHON_SYSLIBS)
 endif
 
-INCLUDES += -I$(PYTHON_INCDIR)
+INCLUDES += -I$(NUMPY_INCDIR) -I$(PYTHON_INCDIR)
 
-yourname.spatialdb.pyx yourname.spatialdb_embed.cpp yourname.spatialdb_embed.h: yourname.spatialdb.pyxe
-	pyrexembed yourname.spatialdb.pyxe
-yourname.spatialdb.pyxe: $(srcdir)/spatialdb.pyxe.src
-	cp $(srcdir)/spatialdb.pyxe.src $@
-yourname.spatialdb_embed.cpp: yourname.spatialdb_embed.h
-yourname.spatialdb_embed.h: yourname.spatialdb.pyx
+$(srcdir)/spatialdatacontrib_wrap.cxx $(srcdir)/spatialdatacontrib.py: $(swig_sources)
+	$(SWIG) -Wall -c++ -python $(CPPFLAGS) $<
 
-.pyx.c:
-	pyrexc $(PYXFLAGS) $<
 
-CLEANFILES = yourname.spatialdb.pyx yourname.spatialdb.pyxe yourname.spatialdb.c *_embed.*
+MAINTAINERCLEANFILES = $(swig_generated)
 
 
 
@@ -65,7 +72,7 @@
 
 nobase_pkgpyexec_PYTHON = \
 	__init__.py \
-	GeoGridVelModel.py
+	UniformVelModel.py
 
 
 # End of file 

Added: cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.cc
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.cc	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.cc	2009-06-14 22:35:38 UTC (rev 15241)
@@ -0,0 +1,264 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+#include <portinfo> // machine specific info generated by configure
+
+#include "UniformVelModel.hh" // Implementation of class methods
+
+#include "spatialdata/geocoords/CSGeoProj.hh" // USES CSGeoProj
+#include "spatialdata/geocoords/Projector.hh" // USES Projector
+#include "spatialdata/geocoords/Converter.hh" // USES Converter
+
+#include <strings.h> // USES strcasecmp()
+#include <cassert> // USES assert()
+#include <sstream> // USES std::ostringstream
+#include <stdexcept> // USES std::runtime_error, std::logic_error
+
+// ----------------------------------------------------------------------
+// Constructor
+contrib::spatialdb::UniformVelModel::UniformVelModel(void) :
+  spatialdata::spatialdb::SpatialDB("Velocity model with uniform properties"),
+  _vp(0),
+  _vs(0),
+  _density(0),
+  _cs(0),
+  _queryVals(0),
+  _querySize(0)
+{ // constructor
+} // constructor
+
+// ----------------------------------------------------------------------
+// Destructor
+contrib::spatialdb::UniformVelModel::~UniformVelModel(void)
+{ // destructor
+  // Deallocate all data
+  delete _cs; _cs = 0;
+
+  delete[] _queryVals; _queryVals = 0;
+  _querySize = 0;
+} // destructor
+
+// ----------------------------------------------------------------------
+// Open the database and prepare for querying.
+void
+contrib::spatialdb::UniformVelModel::open(void)
+{ // open
+  // Setup the velocity model and do any necessary pre-query stuff.
+
+  // Make sure the physical properties have been set.
+  if (0 == _vs) {
+    std::ostringstream msg;
+    msg << "S wave speed has not been set for spatial database "
+	<< label() << ".";
+    throw std::runtime_error(msg.str());
+  } // if
+  if (0 == _vp) {
+    std::ostringstream msg;
+    msg << "P wave speed has not been set for spatial database "
+	<< label() << ".";
+    throw std::runtime_error(msg.str());
+  } // if
+  if (0 == _density) {
+    std::ostringstream msg;
+    msg << "Density has not been set for spatial database "
+	<< label() << ".";
+    throw std::runtime_error(msg.str());
+  } // if
+
+  // Set up geographic projected coordinate system. In this case we
+  // use UTM coordinates is zone 10 with the NAD27 horizontal datum
+  // and mean sea level for the vertical datum.
+  spatialdata::geocoords::Projector proj;
+  proj.projection("utm");
+  proj.units("m");
+  proj.projOptions("+zone=10");
+
+  // Create a new coordinate system object (first deleting the old one
+  // if it exists).
+  delete _cs; _cs = new spatialdata::geocoords::CSGeoProj;
+
+  // Set parameters for the coordinate system.
+  _cs->datumHoriz("NAD27");
+  _cs->datumVert("mean sea level");
+  _cs->ellipsoid("clrk66");
+  _cs->projector(proj);
+  _cs->initialize();
+} // open
+
+// ----------------------------------------------------------------------
+// Close the database.
+void
+contrib::spatialdb::UniformVelModel::close(void)
+{ // close
+  // Deallocate all data allocated in open() and query().
+
+  // Deallocate coordinate system object and set to null so that
+  // subsequent calls to delete won't try to dellocate memory that has
+  // already been deallocated.
+  delete _cs; _cs = 0;
+} // close
+
+// ----------------------------------------------------------------------
+// Set values to be returned by queries.
+void
+contrib::spatialdb::UniformVelModel::queryVals(const char* const* names,
+					       const int numVals)
+{ // queryVals
+  // Check function arguments.
+  if (0 >= numVals) {
+    std::ostringstream msg;
+    msg << "Number of values for query in spatial database " << label()
+	<< "\n must be positive.\n";
+    throw std::runtime_error(msg.str());
+  } // if
+  assert(0 != names && 0 < numVals);
+  
+  // Store the query size.
+  _querySize = numVals;
+
+  // Allocate the array to store the flags for the queries (first
+  // dellocating the array if necessary).
+  delete[] _queryVals; _queryVals = (numVals > 0) ? new int[numVals] : 0;
+
+  // Set the values in the array of values to include in queries.
+  for (int iVal=0; iVal < numVals; ++iVal) {
+    // Set the values to be returned in a query.
+    if (0 == strcasecmp(names[iVal], "vp"))
+      _queryVals[iVal] = QUERY_VP;
+    else if (0 == strcasecmp(names[iVal], "vs"))
+      _queryVals[iVal] = QUERY_VS;
+    else if (0 == strcasecmp(names[iVal], "density"))
+      _queryVals[iVal] = QUERY_DENSITY;
+    else {
+      std::ostringstream msg;
+      msg << "Could not find value " << names[iVal] << " in spatial database\n"
+	  << label() << ". Available values are:\n"
+	  << "vp, vs, and density.";
+      throw std::runtime_error(msg.str());
+    } // else
+  } // for
+} // queryVals
+
+// ----------------------------------------------------------------------
+// Query the database.
+int
+contrib::spatialdb::UniformVelModel::query(double* vals,
+					   const int numVals,
+					   const double* coords,
+					   const int numDims,
+					   const spatialdata::geocoords::CoordSys* csQuery)
+{ // query
+  // Make sure we are setup to perform a query and check to make sure
+  // that the number of values expected to be returned in the query
+  // (as specified in the arguments) matches the size of queries
+  // specified in the more recent call to queryVals().
+  if (0 == _querySize) {
+    std::ostringstream msg;
+    msg << "Values to be returned by spatial database " << label() << "\n"
+	<< "have not been set. Please call queryVals() before query().\n";
+    throw std::runtime_error(msg.str());
+  } // if
+  if (numVals != _querySize) {
+    std::ostringstream msg;
+    msg << "Number of values to be returned by spatial database "
+	<< label() << "\n"
+	<< "(" << _querySize << ") does not match size of array provided ("
+	<< numVals << ").\n";
+    throw std::runtime_error(msg.str());
+  } // if
+  const int spaceDim = csQuery->spaceDim();
+  if (2 != spaceDim || 3 != spaceDim) {
+    std::ostringstream msg;
+    msg << "Spatial dimension of coordinate system for query location must have "
+	<< "2 or 3 spatial dimensions while querying uniform velocity model "
+	<< "database " << label() << ".\n";
+    throw std::runtime_error(msg.str());
+  } // if
+    
+  // Convert coordinates to local coordinate system. Not useful for
+  // this trivial seismic velocity model but will be necessary in most
+  // cases.
+  _xyz[0] = 0.0;
+  _xyz[1] = 0.0;
+  _xyz[2] = 0.0;
+  for (int i=0; i < spaceDim; ++i)
+    _xyz[i] = coords[i];
+  spatialdata::geocoords::Converter::convert(_xyz, 1, spaceDim, _cs, csQuery);
+
+  // Perform query operation
+  int queryFlag = 0;
+  for (int i=0; i < _querySize; ++i) {
+    switch (_queryVals[i]) {
+    case QUERY_VP:
+      vals[i] = _vp;
+      break;
+    case QUERY_VS:
+      vals[i] = _vs;
+      break;
+    case QUERY_DENSITY:
+      vals[i] = _density;
+      break;
+    default :
+      throw std::logic_error("Unknown query value.");
+    } // switch
+  } // for
+  
+  return queryFlag;
+} // query
+
+// ----------------------------------------------------------------------
+// Set the P wave speed.
+void
+contrib::spatialdb::UniformVelModel::vp(const double value)
+{ // vp
+  if (value <= 0.0) {
+    std::ostringstream msg;
+    msg << "P wave speed must be positive for uniform velocity model "
+	<< "database " << label() << ".\n";
+    throw std::runtime_error(msg.str());
+  } // if
+
+  _vp = value;
+} // vp
+
+// ----------------------------------------------------------------------
+// Set the S wave speed.
+void
+contrib::spatialdb::UniformVelModel::vs(const double value)
+{ // vs
+  if (value <= 0.0) {
+    std::ostringstream msg;
+    msg << "S wave speed must be positive for uniform velocity model "
+	<< "database " << label() << ".\n";
+    throw std::runtime_error(msg.str());
+  } // if
+
+  _vs = value;
+} // vs
+
+// ----------------------------------------------------------------------
+// Set the density.
+void
+contrib::spatialdb::UniformVelModel::density(const double value)
+{ // density
+  if (value <= 0.0) {
+    std::ostringstream msg;
+    msg << "Densiy must be positive for uniform velocity model database "
+	<< label() << ".\n";
+    throw std::runtime_error(msg.str());
+  } // if
+
+  _density = value;
+} // density
+ 
+
+// End of file

Added: cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.hh
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.hh	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.hh	2009-06-14 22:35:38 UTC (rev 15241)
@@ -0,0 +1,151 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+/** This is the C++ header file that describes the UniformVelModel
+ *  object. It includes the functions and data structures associated
+ *  with the object.
+ */
+
+#if !defined(contrib_uniformvelmodel_hh)
+#define contrib_uniformvelmodel_hh
+
+#include "spatialdata/spatialdb/SpatialDB.hh" // ISA SpatialDB
+
+namespace contrib {
+  namespace spatialdb {
+    class UniformVelModel;
+  } // spatialdb
+} // contrib
+
+class contrib::spatialdb::UniformVelModel : public spatialdata::spatialdb::SpatialDB
+{ // UniformVelModel
+  friend class TestUniformVelModel; // unit testing
+ 
+// PUBLIC METHODS ///////////////////////////////////////////////////////
+public :
+ 
+  /// Constructor
+  UniformVelModel(void);
+ 
+  /// Destructor
+  ~UniformVelModel(void);
+
+// PUBLIC METHODS ///////////////////////////////////////////////////////
+public :
+ 
+  // Functions required to satisfy the SpatialDB interface.
+ 
+  /// Open the database and prepare for querying.
+  void open(void);
+ 
+  /// Close the database.
+  void close(void);
+ 
+  /** Set values to be returned by queries. Queries may not want all
+   * of the values available.
+   *
+   * @pre Must call open() before queryVals()
+   *
+   * @param names Names of values to be returned in queries
+   * @param numVals Number of values to be returned in queries
+   */
+  void queryVals(const char* const* names,
+		 const int numVals);
+ 
+  /** Query the database.
+   *
+   * @note pVals should be preallocated to accommodate numVals values.
+   *
+   * @pre Must call open() before query()
+   *
+   * @param vals Array for computed values (output from query), must be
+   *   allocated BEFORE calling query().
+   * @param numVals Number of values expected (size of pVals array)
+   * @param coords Coordinates of point for query
+   * @param numDims Number of dimensions for coordinates
+   * @param csQuery Coordinate system of coordinates
+   *
+   * @returns 0 on success, 1 on failure (e.g., query location outside
+   * region associated with the velocity model).
+   */
+  int query(double* vals,
+	    const int numVals,
+	    const double* coords,
+	    const int numDims,
+	    const spatialdata::geocoords::CoordSys* csQuery);
+ 
+// PUBLIC METHODS ///////////////////////////////////////////////////////
+public :
+
+  // Functions specific to this type of spatial database.
+
+  /** Set the P wave speed.
+   *
+   * @param value P wave speed in m/s.
+   */
+  void vp(const double value);
+
+  /** Set the S wave speed.
+   *
+   * @param value S wave speed in m/s.
+   */
+  void vs(const double value);
+
+  /** Set the density.
+   *
+   * @param value Density in kg/m**3.
+   */
+  void density(const double value);
+ 
+// PRIVATE ENUMS ////////////////////////////////////////////////////////
+private :
+ 
+  // Indices of values in database (used in queryVals to indicate
+  // which values to return in a query).
+  enum QueryValsEnum {
+    QUERY_VP=0, // vp
+    QUERY_VS=1, // vs
+    QUERY_DENSITY=2, // density
+  }; // ValsEnum
+
+// PRIVATE MEMBERS //////////////////////////////////////////////////////
+private :
+
+  // Physical property information (hardwired in this case). Replace
+  // with data structures for holding the physical property
+  // information.
+
+  double _vp; ///< P wave speed
+  double _vs; ///< S wave speed
+  double _density; ///< Density
+  
+  /// Coordinate system for velocity model. When querying the velocity
+  /// model for physical properties, the coordinates of the query are
+  /// transformed into this coordinate system.
+  spatialdata::geocoords::CSGeoProj* _cs;
+  double _xyz[3]; ///< Array used in conversion of coordinates.
+
+  int* _queryVals; ///< Indices of values to be returned in queries.
+  int _querySize; ///< Number of values requested to be returned in queries.
+
+// NOT IMPLEMENTED //////////////////////////////////////////////////////
+private :
+ 
+  UniformVelModel(const UniformVelModel&); ///< Not implemented
+  const UniformVelModel& operator=(const UniformVelModel&); ///< Not implemented
+ 
+}; // UniformVelModel
+
+#endif // contrib_uniformvelmodel_hh
+
+
+// End of file 

Added: cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.i
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.i	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.i	2009-06-14 22:35:38 UTC (rev 15241)
@@ -0,0 +1,113 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+// SWIG interface to C++ UniformVelModel object.
+
+/* This is nearly identical to the C++ UniformVelModel header
+ * file. There are a few important differences required by SWIG:
+ *
+ * (1) Instead of forward declaring the UniformVelModel database, we
+ * embed the class definition within the namespace declarations.
+ *
+ * (2) We only include public members and methods, because this is an
+ * interface file.
+ */
+
+namespace contrib {
+  namespace spatialdb {
+    
+    class UniformVelModel : public spatialdata::spatialdb::SpatialDB
+    { // UniformVelModel
+ 
+      // PUBLIC MEMBERS ///////////////////////////////////////////////////
+    public :
+ 
+      /// Constructor
+      UniformVelModel(void);
+      
+      /// Destructor
+      ~UniformVelModel(void);
+      
+      // PUBLIC METHODS ///////////////////////////////////////////////////
+    public :
+      
+      // Functions required to satisfy the SpatialDB interface.
+      
+      /// Open the database and prepare for querying.
+      void open(void);
+      
+      /// Close the database.
+      void close(void);
+      
+      /** Set values to be returned by queries. Queries may not want all
+       * of the values available.
+       *
+       * @pre Must call open() before queryVals()
+       *
+       * @param names Names of values to be returned in queries
+       * @param numVals Number of values to be returned in queries
+       */
+      void queryVals(const char* const* names,
+		     const int numVals);
+      
+      /** Query the database.
+       *
+       * @note pVals should be preallocated to accommodate numVals values.
+       *
+       * @pre Must call open() before query()
+       *
+       * @param vals Array for computed values (output from query), must be
+       *   allocated BEFORE calling query().
+       * @param numVals Number of values expected (size of pVals array)
+       * @param coords Coordinates of point for query
+       * @param numDims Number of dimensions for coordinates
+       * @param csQuery Coordinate system of coordinates
+       *
+       * @returns 0 on success, 1 on failure (e.g., query location outside
+       * region associated with the velocity model).
+       */
+      int query(double* vals,
+		const int numVals,
+		const double* coords,
+		const int numDims,
+		const spatialdata::geocoords::CoordSys* csQuery);
+      
+      // PUBLIC METHODS ///////////////////////////////////////////////////
+    public :
+      
+      // Functions specific to this type of spatial database.
+      
+      /** Set the P wave speed.
+       *
+       * @param value P wave speed in m/s.
+       */
+      void vp(const double value);
+      
+      /** Set the S wave speed.
+       *
+       * @param value S wave speed in m/s.
+       */
+      void vs(const double value);
+      
+      /** Set the density.
+       *
+       * @param value Density in kg/m**3.
+       */
+      void density(const double value);
+      
+    }; // UniformVelModel
+
+  } // spatialdb
+} // contrib
+
+
+// End of file

Added: cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.py
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.py	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/UniformVelModel.py	2009-06-14 22:35:38 UTC (rev 15241)
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+#
+# ----------------------------------------------------------------------
+#
+#                           Brad T. Aagaard
+#                        U.S. Geological Survey
+#
+# <LicenseText>
+#
+# ----------------------------------------------------------------------
+#
+
+## @brief Python object associated with the C++ UniformVelModel
+## object. This objects provides a Pyre interface to the C++ object. 
+##
+## Factory: spatial_database
+
+from spatialdata.spatialdb.SpatialDBObj import SpatialDBObj
+from spatialdatacontrib import UniformVelModel as ModuleUniformVelModel
+
+# UniformVelModel class
+class UniformVelModel(SpatialDBObj, ModuleUniformVelModel):
+  """
+  Python object associated with the C++ UniformVelModel object. This
+  objects provides a Pyre interface to the C++ object.  Python manager
+  for spatial database to a new velocity model.
+
+  Factory: spatial_database
+  """
+
+  # INVENTORY //////////////////////////////////////////////////////////
+
+  class Inventory(SpatialDBObj.Inventory):
+    """
+    Python object for managing UniformVelModel facilities and properties.
+    """
+
+    ## @class Inventory
+    ## Python object for managing UniformVelModel facilities and properties.
+    ##
+    ## \b Properties
+    ## @li \b vs S wave speed.
+    ## @li \b vp P wave speed.
+    ## @li \b density Density.
+    ##
+    ## \b Facilities
+    ## @li none
+
+    import pyre.inventory
+
+    from pyre.units.time import s
+    from pyre.units.length import km,m
+    from pyre.units.mass import kg
+
+    vs = pyre.inventory.dimensional("vs", default=2.6*km/s)
+    vs.meta['tip'] = "S wave speed."
+
+    vp = pyre.inventory.dimensional("vp", default=4.5*km/s)
+    vp.meta['tip'] = "P wave speed."
+
+    density = pyre.inventory.dimensional("density", default=2.5e+3*kg/m**3)
+    density.meta['tip'] = "Density."
+
+
+  # PUBLIC METHODS /////////////////////////////////////////////////////
+
+  def __init__(self, name="uniformvelmodel"):
+    """
+    Constructor.
+    """
+    SpatialDBObj.__init__(self, name)
+    return
+
+
+  # PRIVATE METHODS ////////////////////////////////////////////////////
+
+  def _configure(self):
+    """
+    Set members based on inventory.
+    """
+    SpatialDBObj._configure(self) # Call parent function.
+    
+    # Transfer inventory to C++ object
+    ModuleUniformVelModel.vs(self, self.inventory.vs.value)
+    ModuleUniformVelModel.vp(self, self.inventory.vp.value)
+    ModuleUniformVelModel.density(self, self.inventory.density.value)
+    return
+
+
+  def _createModuleObj(self):
+    """
+    Create handle to C++ object.
+    """
+    ModuleUniformVelModel.__init__(self)
+    return
+
+
+# FACTORIES ////////////////////////////////////////////////////////////
+
+# Factory used when setting UniformVelModel to a Pyre 'spatial_database' facility.
+def spatial_database():
+  """
+  Factory associated with UniformVelModel.
+  """
+  return UniformVelModel()
+
+
+# End of file 

Modified: cs/spatialdata-0.1/trunk/templates/spatialdb/__init__.py
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/__init__.py	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/__init__.py	2009-06-14 22:35:38 UTC (rev 15241)
@@ -12,9 +12,9 @@
 
 ## @file __init__.py
 ##
-## @brief Python top-level SpatialData module initialization
+## @brief Python module initialization
 
-__all__ = ['GeoGridVelModel',
+__all__ = ['UniformVelModel',
            ]
 
 

Modified: cs/spatialdata-0.1/trunk/templates/spatialdb/configure.ac
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/configure.ac	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/configure.ac	2009-06-14 22:35:38 UTC (rev 15241)
@@ -11,9 +11,10 @@
 #
 
 AC_PREREQ(2.59)
-AC_INIT([yourname], [0.0.1], [youremail at here])
+AC_INIT([spatialdatacontrib], [0.0.1], [cig-short at geodynamics.org])
 AC_CONFIG_HEADER([portinfo])
 AC_CONFIG_AUX_DIR([./aux-config])
+#AC_CONFIG_MACRO_DIR([./m4])
 AM_INIT_AUTOMAKE([foreign])
 
 # ----------------------------------------------------------------------
@@ -43,26 +44,11 @@
 AM_PATH_PYTHON([2.3])
 CIT_PYTHON_SYSCONFIG
 
-# Check for Python modules and packages.
-#CIT_PYTHON_EGG_SETUP
+# SWIG
+CIT_NUMPY_PYTHON_MODULE
+CIT_NUMPY_INCDIR
+AC_PROG_SWIG(1.3.33)
 
-# PYREX/PYREXEMBED
-AC_CHECK_PROG([have_pyrex],
-  [pyrexc],
-  [yes],
-  [no])
-if test "$have_pyrex" = "no"; then
-  AC_MSG_ERROR([Pyrex not found in current path.])
-fi
-AC_CHECK_PROG([have_pyrexembed],
-  [pyrexembed],
-  [yes],
-  [no])
-if test "$have_pyrexembed" = "no"; then
-  AC_MSG_ERROR([Pyrexembed not found in current path.])
-fi
-AC_ARG_VAR(PYXFLAGS, [Pyrex flags])
-
 # CPPUNIT
 if test "$enable_testing" = "yes" ; then
   AC_LANG(C++)
@@ -95,7 +81,11 @@
   [AC_MSG_RESULT(no)
    AC_MSG_ERROR([Spatialdata library not found; try LDFLAGS="-L<Spatialdata lib dir>"])
 ])
+AC_CHECK_HEADER([spatialdata/spatialdb/swig/SpatialDBObj.i], [], [
+  AC_MSG_ERROR([SpatialDB SWIG interface file not found; try CPPFLAGS="-I<Spatialdata include dir>"])
+])
 
+
 # ENDIANNESS
 AC_C_BIGENDIAN
 

Added: cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdatacontrib.i
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdatacontrib.i	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdatacontrib.i	2009-06-14 22:35:38 UTC (rev 15241)
@@ -0,0 +1,51 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+// Define the spatialdatacontrib SWIG interface module.
+
+// Set module name
+%module spatialdatacontrib
+
+// Header files for module C++ code.
+%{
+#include "UniformVelModel.hh"
+%}
+
+// Convert standard C++ exceptions to Python exceptions.
+%include "exception.i"
+%exception {
+  try {
+    $action
+  } catch (const std::exception& err) {
+    SWIG_exception(SWIG_RuntimeError, err.what());
+  } // try/catch
+} // exception
+
+%include "typemaps.i"
+%include "spatialdata/swig/chararray.i"
+
+// Numpy interface stuff
+%{
+#define SWIG_FILE_WITH_INIT
+%}
+%include "spatialdata/swig/numpy.i"
+%init %{
+import_array();
+%}
+
+
+// Interface files.
+%include "spatialdata/spatialdb/swig/SpatialDBObj.i"
+%include "UniformVelModel.i"
+
+
+// End of file

Deleted: cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdb.pyxe.src
===================================================================
--- cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdb.pyxe.src	2009-06-14 18:09:56 UTC (rev 15240)
+++ cs/spatialdata-0.1/trunk/templates/spatialdb/spatialdb.pyxe.src	2009-06-14 22:35:38 UTC (rev 15241)
@@ -1,58 +0,0 @@
-#!/usr/bin/env python
-#
-# ======================================================================
-#
-#                           Brad T. Aagaard
-#                        U.S. Geological Survey
-#
-# {LicenseText}
-#
-# ======================================================================
-#
-
-#header{
-#include "spatialdata/spatialdb/SpatialDB.hh"
-#include "GeoGridVelModel.hh"
-
-#include "spatialdata/geocoords/CoordSys.hh"
-
-#include <stdexcept>
-#include <Python.h>
-#}header
-
-# ----------------------------------------------------------------------
-from spatialdata.spatialdb.spatialdb cimport SpatialDB
-
-
-cdef class GeoGridVelModel(SpatialDB):
-
-  def __init__(self):
-    """
-    Constructor.
-    """
-    # create shim for constructor
-    #embed{ void* GeoGridVelModel_constructor()
-      return (void*)(new yourname::spatialdb::GeoGridVelModel);
-    #}embed
-
-    SpatialDB.__init__(self)
-    self.thisptr = GeoGridVelModel_constructor()
-    self.handle = self._createHandle()
-    return
-
-
-  def filename(self, name):
-    """
-    Set the filename for velocity model data file.
-    """
-    # create shim for method 'filename'
-    #embed{ void GeoGridVelModel_filename(void* pObj, char* name)
-    assert(0 != pObj);
-    ((yourname::spatialdb::GeoGridVelModel*) pObj)->filename(name);
-    #}embed
-
-    GeoGridVelModel_filename(self.thisptr, name)
-    return
-
-
-# End of file 



More information about the CIG-COMMITS mailing list