Index: trunk/ippToPsps/src/Dvo.c
===================================================================
--- trunk/ippToPsps/src/Dvo.c	(revision 31265)
+++ trunk/ippToPsps/src/Dvo.c	(revision 31928)
@@ -27,71 +27,87 @@
   Gets detections for this imageid
   */
-static bool getDetections(Dvo* this, int32_t sourceId, int32_t imageId) {
+static bool getDetections(Dvo* this) {
 
     SkyList* skyList = NULL;
     Image* image = NULL;
-    this->logger->print(this->logger, MSG_INFO, "Dvo", "Getting skylist from DVO database...\n"); 
-    skyList = dvoSkyListByExternID(this->dvoConfig, sourceId, imageId, &image);
-    if (!skyList) {
-
-        this->logger->print(this->logger, MSG_ERROR, 
-                "Dvo", "Could not find skylist for sourceId=%d and image ID = %d\n", 
-                sourceId, imageId);
-
-        return false;
-    }
-    this->logger->print(this->logger, MSG_INFO, "Dvo", "...done\n"); 
-
-    // now get detections
-    this->logger->print(this->logger, MSG_INFO, "Dvo", "Getting detections...\n"); 
-    dvoDetection* dvoDetections = NULL;
-    int32_t maxDetectionId = -1;
-    int32_t numDetections = 
-        dvoGetDetections(skyList, image->imageID, &dvoDetections, &maxDetectionId);
-
-    this->logger->print(this->logger, MSG_INFO, "Dvo", 
-            "Found %d detections with a max detection ID of %d\n", 
-            numDetections, maxDetectionId); 
-
-    if (dvoDetections) dvoFree(dvoDetections);
-    SkyListFree(skyList);
-
-    this->logger->print(this->logger, MSG_INFO, "Dvo", "Deleting everything from dvo table\n"); 
-    mysql_query(this->mysql, "DELETE FROM dvo");
-
-    this->logger->print(this->logger, MSG_INFO, "Dvo", "Inserting IDs into dvo table...\n"); 
-    
+
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Deleting everything from the dvoDetection table\n"); 
+    mysql_query(this->mysql, "DELETE FROM dvoDetection");
+
+    mysql_query(this->mysql, "SELECT sourceID, imageID FROM dvoMeta");
+    MYSQL_RES* result = mysql_store_result(this->mysql);
+
+    // loop through all souceID/imageIDs
+    MYSQL_ROW row;
     char sql[500];
-    for (int i=0; i<numDetections; i++) {
+    int sourceID, imageID;
+    while ((row = mysql_fetch_row(result))) {
+
+        sourceID = atoi(row[0]);
+        imageID = atoi(row[1]);
+
+        this->logger->print(this->logger, MSG_INFO, "Dvo", "---------------------------------------------------------------------\n");
+        this->logger->print(this->logger, MSG_INFO, "Dvo", "Getting skylist from DVO database for source ID = %d and image ID = %d\n",
+                sourceID, imageID); 
+        skyList = dvoSkyListByExternID(this->dvoConfig, sourceID, imageID, &image);
+        if (!skyList) {
+
+            this->logger->print(this->logger, MSG_ERROR, 
+                    "Dvo", "Could not find skylist for sourceId=%d and image ID = %d\n", 
+                    sourceID, imageID);
+
+            continue;
+        }
 
         sprintf(sql, 
-                "INSERT INTO dvo (ippDetectID, ippObjID, objID) VALUES (%u, %lu, %lu)",
-                dvoDetections[i].meas.detID,
-                (uint64_t)dvoDetections[i].ave.catID*1000000000 + (uint64_t)dvoDetections[i].ave.objID,
-                dvoDetections[i].ave.extID);
-
+                "UPDATE dvoMeta SET flags = %d, photcode = %d WHERE sourceID = %d AND imageID = %d",
+                image->flags,
+                image->photcode,
+                sourceID,
+                imageID);
         mysql_query(this->mysql, sql); 
-    }
-    this->logger->print(this->logger, MSG_INFO, "Dvo", "...done\n"); 
-
-#if 0
-    mysql_query(this->mysql, "SELECT * FROM Filter");
-    MYSQL_RES* result = mysql_store_result(this->mysql);
-
-    int num_fields = mysql_num_fields(result);
-
-    MYSQL_ROW row;
-
-    while ((row = mysql_fetch_row(result))) {
-
-        for(int i = 0; i < num_fields; i++) {
-
-            printf("%s ", row[i] ? row[i] : "NULL");
+
+        // now get detections
+        this->logger->print(this->logger, MSG_INFO, "Dvo", "Getting detections...\n"); 
+        dvoDetection* dvoDetections = NULL;
+        int32_t maxDetectionId = -1;
+        int32_t numDetections = 
+            dvoGetDetections(skyList, image->imageID, &dvoDetections, &maxDetectionId);
+
+        this->logger->print(this->logger, MSG_INFO, "Dvo", 
+                "Found %d detections with a max detection ID of %d\n", 
+                numDetections, maxDetectionId); 
+
+
+        this->logger->print(this->logger, MSG_INFO, "Dvo", "Inserting IDs into dvoDetection table...\n"); 
+
+        uint32_t zeroDvoCount = 0;
+        for (int i=0; i<maxDetectionId+1; i++) {
+
+            if (dvoDetections[i].ave.extID == 0) {
+                zeroDvoCount++;
+            }
+            else {
+                sprintf(sql, 
+                        "INSERT INTO dvoDetection (sourceID, imageID, ippDetectID, detectID, ippObjID, objID, flags) VALUES (%d, %d, %u, %lu, %lu, %lu, %u)",
+                        sourceID,   // sourceID
+                        imageID,    // imageID
+                        dvoDetections[i].meas.detID,   // ippDetectID
+                        dvoDetections[i].meas.extID,   // detectID
+                        (uint64_t)dvoDetections[i].ave.catID*1000000000 + (uint64_t)dvoDetections[i].ave.objID, // ippObjID
+                        dvoDetections[i].ave.extID, // objID
+                        dvoDetections[i].meas.dbFlags); // flags
+
+                mysql_query(this->mysql, sql); 
+            }
         }
-        printf("\n");
+
+        if (zeroDvoCount) this->logger->print(this->logger, MSG_ERROR, "Dvo", "Found %d 0 object IDs\n", zeroDvoCount);
+        if (dvoDetections) dvoFree(dvoDetections);
+        SkyListFree(skyList);
     }
 
     mysql_free_result(result);
-#endif
+
     return true;
 }
@@ -106,5 +122,5 @@
     this->logger->print(this->logger, MSG_DEBUG, "Dvo", "Constructor\n");
 
-    const char *configFile = "jython/config.xml";
+    const char *configFile = "./config.xml"; // TODO hardcoded path to config
     this->logger->print(this->logger, MSG_INFO, "Dvo", "Using config file here '%s'\n", configFile);
     xmlDoc* doc = xmlReadFile(configFile, NULL, 0);
@@ -202,5 +218,4 @@
 }
 
-
 /**
   Main
@@ -210,7 +225,16 @@
     Logger* logger = new_Logger(NULL, true);
 
-    Dvo * dvo = new_Dvo(logger, "/data/ipp004.0/gpc1/catdirs/ThreePi.V1");
-    dvo->getDetections(dvo, 33, 8345290);
-    dvo->destroy(dvo);
+    if (argc < 2) {
+
+        logger->print(logger, MSG_ERROR, "Dvo", "Incorrect arguments\n");
+        logger->print(logger, MSG_INFO, "Dvo", "Usage: ./dvo <pathToDvo>\n");
+    }
+    else {
+        //Dvo * dvo = new_Dvo(logger, "/data/ipp004.0/gpc1/catdirs/ThreePi.V1");
+        Dvo * dvo = new_Dvo(logger, argv[1]);
+        //dvo->getDetections(dvo, 33, 8345290);
+        dvo->getDetections(dvo);
+        dvo->destroy(dvo);
+    }
 
     logger->destroy(logger);
