[cig-commits] r15167 - in cs/spatialdata-0.1/trunk: libsrc/spatialdb modulesrc/spatialdb spatialdata/spatialdb tests/libtests/spatialdb tests/pytests/spatialdb

brad at geodynamics.org brad at geodynamics.org
Tue Jun 9 21:57:09 PDT 2009


Author: brad
Date: 2009-06-09 21:57:08 -0700 (Tue, 09 Jun 2009)
New Revision: 15167

Modified:
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/SimpleIO.cc
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.cc
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.hh
   cs/spatialdata-0.1/trunk/modulesrc/spatialdb/UniformDB.i
   cs/spatialdata-0.1/trunk/spatialdata/spatialdb/UniformDB.py
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestCompositeDB.cc
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestUniformDB.cc
   cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestCompositeDB.py
   cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestUniformDB.py
Log:
Added units to UniformDB. Fixed bug in time history.

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/SimpleIO.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/SimpleIO.cc	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/SimpleIO.cc	2009-06-10 04:57:08 UTC (rev 15167)
@@ -80,7 +80,6 @@
 { // convertToSI
   assert(0 != data);
 
-#if 1
   spatialdata::units::Parser parser;
 
   const int numValues = data->numValues();
@@ -98,7 +97,6 @@
     for (int iVal=0; iVal < numValues; ++iVal)
       values[iVal] *= scales[iVal];
   } // for
-#endif
 } // convertToSI
 
 

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc	2009-06-10 04:57:08 UTC (rev 15167)
@@ -70,6 +70,7 @@
   _npts = 0;
 } // close
 
+#include <iostream>
 // ----------------------------------------------------------------------
 // Query the database.
 int
@@ -86,7 +87,7 @@
 	--_ilower;
       } // while
     } else if (t > _time[_ilower+1]) {
-      const int imax = _npts-1;
+      const int imax = _npts-2;
       while(_ilower < imax) {
 	if (t <= _time[_ilower+1])
 	  break;
@@ -94,6 +95,7 @@
       } // while
     } // if/else
 
+    assert(_ilower < _npts-1);
     if (t >= _time[_ilower] && t <= _time[_ilower+1]) {
       const double tL = _time[_ilower];
       const double tU = _time[_ilower+1];

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.cc	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.cc	2009-06-10 04:57:08 UTC (rev 15167)
@@ -15,6 +15,9 @@
 #include "SpatialDB.hh" // ISA SpatialDB object
 #include "UniformDB.hh" // Implementation of class methods
 
+#include "spatialdata/units/Parser.hh" // USES Parser
+
+#include <vector> // USES std::vector
 #include <stdexcept> // USES std::runtime_error
 
 #include <sstream> // USES std::ostringsgream
@@ -59,25 +62,36 @@
 // Set values in database.
 void
 spatialdata::spatialdb::UniformDB::setData(const char* const* names,
+					   const char* const* units,
 					   const double* values,
 					   const int numValues)
 { // setData
-  assert( (0 < numValues && 0 != names && 0 != values) ||
-	  (0 == numValues && 0 == names && 0 == values) );
+  assert( (0 < numValues && 0 != names && 0 != units && 0 != values) ||
+	  (0 == numValues && 0 == names && 0 == units && 0 == values) );
 
   // clear out old data
   delete[] _names; _names = 0;
   delete[] _values; _values = 0;
   _numValues = numValues;
 
+  spatialdata::units::Parser parser;
+
   if (0 < numValues) {
     _names = new std::string[numValues];
     for (int i=0; i < numValues; ++i)
       _names[i] = names[i];
 
+    std::vector<double> scales(numValues);
+    for (int i=0; i < numValues; ++i) {
+      if (strcasecmp(units[i], "none") != 0)
+	scales[i] = parser.parse(units[i]);
+      else
+	scales[i] = 1.0;
+    } // for
+
     _values = new double[numValues];
     for (int i=0; i < numValues; ++i)
-      _values[i] = values[i];
+      _values[i] = values[i]*scales[i];
   } // if
 } // setData
 

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.hh
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.hh	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/UniformDB.hh	2009-06-10 04:57:08 UTC (rev 15167)
@@ -45,10 +45,12 @@
   /** Set values in database.
    *
    * @param names Array of names of values in database.
+   * @param units Array of units for values in database.
    * @param values Array of values in database.
    * @param numValues Number of values in database.
    */
   void setData(const char* const* names,
+	       const char* const* units,
 	       const double* values,
 	       const int numValues);
 

Modified: cs/spatialdata-0.1/trunk/modulesrc/spatialdb/UniformDB.i
===================================================================
--- cs/spatialdata-0.1/trunk/modulesrc/spatialdb/UniformDB.i	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/modulesrc/spatialdb/UniformDB.i	2009-06-10 04:57:08 UTC (rev 15167)
@@ -39,20 +39,26 @@
       /** Set values in database.
        *
        * @param names Array of names of values in database.
+       * @param units Array of units of values in database.
        * @param values Array of values in database.
        * @param numValues Number of values in database.
        */
       %apply(const char* const* string_list) {
 	(const char* const* names)
 	  };
+      %apply(const char* const* string_list) {
+	(const char* const* units)
+	  };
       %apply(double* IN_ARRAY1, int DIM1) {
 	(const double* values,
 	 const int numValues)
 	  };
       void setData(const char* const* names,
+		   const char* const* names,
 		   const double* values,
 		   const int numValues);
       %clear(const char* const* names);
+      %clear(const char* const* units);
       %clear(const double* values, const int numValues);
       
       /// Open the database and prepare for querying.

Modified: cs/spatialdata-0.1/trunk/spatialdata/spatialdb/UniformDB.py
===================================================================
--- cs/spatialdata-0.1/trunk/spatialdata/spatialdb/UniformDB.py	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/spatialdata/spatialdb/UniformDB.py	2009-06-10 04:57:08 UTC (rev 15167)
@@ -39,6 +39,7 @@
     ##
     ## \b Properties
     ## @li \b values Names of values in spatial database.
+    ## @li \b units Units of values in spatial database.
     ## @li \b data Values in spatial database.
     ##
     ## \b Facilities
@@ -49,6 +50,9 @@
     values = pyre.inventory.list("values", default=[])
     values.meta['tip'] = "Names of values in spatial database."
 
+    units = pyre.inventory.list("units", default=[])
+    units.meta['tip'] = "Units of values in spatial database."
+
     data = pyre.inventory.list("data", default=[])
     data.meta['tip'] = "Values in spatial database."
 
@@ -72,7 +76,7 @@
     SpatialDBObj._configure(self)
     self._validateParameters(self.inventory)
     data = map(float, self.inventory.data)
-    self.setData(self.inventory.values, data)
+    self.setData(self.inventory.values, self.inventory.units, data)
     return
 
   

Modified: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestCompositeDB.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestCompositeDB.cc	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestCompositeDB.cc	2009-06-10 04:57:08 UTC (rev 15167)
@@ -63,8 +63,9 @@
   UniformDB dbU;
   const int numValuesU = 3;
   const char* namesU[] = { "one", "two", "three" };
+  const char* unitsU[] = { "none", "none", "none" };
   const double valuesU[] = { 1.1, 2.2, 3.3 };
-  dbU.setData(namesU, valuesU, numValuesU);
+  dbU.setData(namesU, unitsU, valuesU, numValuesU);
 
   const int numNamesA = 2;
   const char* namesA[] = { "three", "one" };
@@ -94,8 +95,9 @@
   UniformDB dbU;
   const int numValuesU = 3;
   const char* namesU[] = { "one", "two", "three" };
+  const char* unitsU[] = { "none", "none", "none" };
   const double valuesU[] = { 1.1, 2.2, 3.3 };
-  dbU.setData(namesU, valuesU, numValuesU);
+  dbU.setData(namesU, unitsU, valuesU, numValuesU);
 
   const int numNamesB = 2;
   const char* namesB[] = { "three", "one" };
@@ -126,8 +128,9 @@
   { // initialize db A
     const int numValuesA = 3;
     const char* namesA[] = { "one", "two", "three" };
+    const char* unitsA[] = { "none", "none", "none" };
     const double valuesA[] = { 1.1, 2.2, 3.3 };
-    dbA.setData(namesA, valuesA, numValuesA);
+    dbA.setData(namesA, unitsA, valuesA, numValuesA);
   } // initialize db A
     
   const int numNamesA = 2;
@@ -138,8 +141,9 @@
   { // initialize db B
     const int numValuesB = 2;
     const char* namesB[] = { "four", "five" };
+    const char* unitsB[] = { "none", "none" };
     const double valuesB[] = { 4.4, 5.5 };
-    dbB.setData(namesB, valuesB, numValuesB);
+    dbB.setData(namesB, unitsB, valuesB, numValuesB);
   } // initialize db B
 
   const int numNamesB = 1;
@@ -185,8 +189,9 @@
   { // initialize db A
     const int numValuesA = 3;
     const char* namesA[] = { "one", "two", "three" };
+    const char* unitsA[] = { "none", "none", "none" };
     const double valuesA[] = { 1.1, 2.2, 3.3 };
-    dbA.setData(namesA, valuesA, numValuesA);
+    dbA.setData(namesA, unitsA, valuesA, numValuesA);
   } // initialize db A
 
   const int numNamesA = 2;
@@ -197,8 +202,9 @@
   { // initialize db B
     const int numValuesB = 2;
     const char* namesB[] = { "four", "five" };
+    const char* unitsB[] = { "none", "none" };
     const double valuesB[] = { 4.4, 5.5 };
-    dbB.setData(namesB, valuesB, numValuesB);
+    dbB.setData(namesB, unitsB, valuesB, numValuesB);
   } // initialize db B
 
   const int numNamesB = 1;

Modified: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestUniformDB.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestUniformDB.cc	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestUniformDB.cc	2009-06-10 04:57:08 UTC (rev 15167)
@@ -60,17 +60,19 @@
   UniformDB db;
 
   const int numValues = 3;
-  const char* names[] = { "one", "two", "three" };
-  const double values[] = { 1.1, 2.2, 3.3 };
+  const char* names[numValues] = { "one", "two", "three" };
+  const char* units[numValues] = { "m", "km", "cm" };
+  const double values[numValues] = { 1.1, 2.2, 3.3 };
+  const double valuesE[numValues] = { 1.1, 2.2e+3, 3.3e-2 };
 
-  db.setData(names, values, numValues);
+  db.setData(names, units, values, numValues);
 
   CPPUNIT_ASSERT_EQUAL(numValues, db._numValues);
   for (int i=0; i < numValues; ++i)
     CPPUNIT_ASSERT_EQUAL(std::string(names[i]), db._names[i]);
 
   for (int i=0; i < numValues; ++i)
-    CPPUNIT_ASSERT_EQUAL(values[i], db._values[i]);
+    CPPUNIT_ASSERT_EQUAL(valuesE[i], db._values[i]);
 } // testSetData
 
 // ----------------------------------------------------------------------
@@ -81,14 +83,15 @@
   UniformDB db;
 
   const int numValues = 3;
-  const char* names[] = { "one", "two", "three" };
-  const double values[] = { 1.1, 2.2, 3.3 };
+  const char* names[numValues] = { "one", "two", "three" };
+  const char* units[numValues] = { "none", "none", "none" };
+  const double values[numValues] = { 1.1, 2.2, 3.3 };
 
   const int querySize = 2;
-  const char* queryNames[] = { "three", "two" };
-  const int queryVals[] = { 2, 1 };
+  const char* queryNames[querySize] = { "three", "two" };
+  const int queryVals[querySize] = { 2, 1 };
 
-  db.setData(names, values, numValues);
+  db.setData(names, units, values, numValues);
   db.queryVals(queryNames, querySize);
 
   CPPUNIT_ASSERT_EQUAL(querySize, db._querySize);
@@ -104,20 +107,21 @@
   UniformDB db;
 
   const int numValues = 3;
-  const char* names[] = { "one", "two", "three" };
-  const double values[] = { 1.1, 2.2, 3.3 };
+  const char* names[numValues] = { "one", "two", "three" };
+  const char* units[numValues] = { "none", "none", "none" };
+  const double values[numValues] = { 1.1, 2.2, 3.3 };
 
   const int querySize = 2;
-  const char* queryNames[] = { "three", "two" };
-  const int queryVals[] = { 2, 1 };
+  const char* queryNames[querySize] = { "three", "two" };
+  const int queryVals[querySize] = { 2, 1 };
 
-  db.setData(names, values, numValues);
+  db.setData(names, units, values, numValues);
   db.queryVals(queryNames, querySize);
 
   const int spaceDim = 2;
   spatialdata::geocoords::CSCart cs;
   cs.setSpaceDim(spaceDim);
-  const double coords[] = { 2.3, 5.6 };
+  const double coords[spaceDim] = { 2.3, 5.6 };
   double data[querySize];
 
   db.query(data, querySize, coords, spaceDim, &cs);

Modified: cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestCompositeDB.py
===================================================================
--- cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestCompositeDB.py	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestCompositeDB.py	2009-06-10 04:57:08 UTC (rev 15167)
@@ -22,12 +22,14 @@
     dbA = UniformDB()
     dbA.inventory.label = "db A"
     dbA.inventory.values = ["one", "two", "three"]
+    dbA.inventory.units = ["none", "none", "none"]
     dbA.inventory.data = [1.1, 2.2, 3.3]
     dbA._configure()
     
     dbB = UniformDB()
     dbB.inventory.label = "db B"
     dbB.inventory.values = ["two", "three", "four", "five" ]
+    dbB.inventory.units = ["none", "none", "none", "none" ]
     dbB.inventory.data = [2.1, 3.1, 4.1, 5.1]
     dbB._configure()
 

Modified: cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestUniformDB.py
===================================================================
--- cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestUniformDB.py	2009-06-10 04:11:35 UTC (rev 15166)
+++ cs/spatialdata-0.1/trunk/tests/pytests/spatialdb/TestUniformDB.py	2009-06-10 04:57:08 UTC (rev 15167)
@@ -22,6 +22,7 @@
     db = UniformDB()
     db.inventory.label = "test"
     db.inventory.values = ["one", "two", "three"]
+    db.inventory.units = ["m", "km", "cm"]
     db.inventory.data = [1.1, 2.2, 3.3]
     db._configure()
     self._db = db
@@ -34,8 +35,8 @@
     from spatialdata.geocoords.CSCart import CSCart
     cs = CSCart()
     queryVals = ["three", "one"]
-    dataE = numpy.array([[3.3, 1.1],
-                         [3.3, 1.1]], numpy.float64)
+    dataE = numpy.array([[3.3e-2, 1.1],
+                         [3.3e-2, 1.1]], numpy.float64)
     errE = [0, 0]
     
     db = self._db



More information about the CIG-COMMITS mailing list