Index: /trunk/ippToPsps/src/Dvo.c
===================================================================
--- /trunk/ippToPsps/src/Dvo.c	(revision 31265)
+++ /trunk/ippToPsps/src/Dvo.c	(revision 31265)
@@ -0,0 +1,220 @@
+/** @file Dvo.c
+ *
+ *  @ingroup ippToPsps
+ *
+ *  @author IfA
+ *  Copyright 2011 Institute for Astronomy, University of Hawaii
+ */
+
+#include "Dvo.h"
+
+/**
+  Destructor.
+  */
+static void destroy(Dvo* this) {
+
+    if (this == NULL) return;
+
+    this->logger->print(this->logger, MSG_DEBUG, "Dvo", "Destructor\n");
+
+    if (this->dvoConfig) dvoConfigFree(this->dvoConfig);
+    if (this->mysql) mysql_close(this->mysql);
+
+    free(this);
+}
+
+/**
+  Gets detections for this imageid
+  */
+static bool getDetections(Dvo* this, int32_t sourceId, int32_t imageId) {
+
+    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"); 
+    
+    char sql[500];
+    for (int i=0; i<numDetections; i++) {
+
+        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);
+
+        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");
+        }
+        printf("\n");
+    }
+
+    mysql_free_result(result);
+#endif
+    return true;
+}
+
+/**
+  Constructor.
+  */
+Dvo* new_Dvo(Logger* logger, const char* dvoDatabase) {
+
+    Dvo* this = (Dvo*)calloc(1, sizeof(Dvo));
+    this->logger = logger;
+    this->logger->print(this->logger, MSG_DEBUG, "Dvo", "Constructor\n");
+
+    const char *configFile = "jython/config.xml";
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Using config file here '%s'\n", configFile);
+    xmlDoc* doc = xmlReadFile(configFile, NULL, 0);
+
+    // database parameters to be retrieved from config file
+    char host[20];
+    char user[20];
+    char passwd[20];
+    char dbname[20];
+
+
+    if (doc == NULL) {
+
+        this->logger->print(this->logger, MSG_ERROR, "Dvo", "Could not load config file\n");
+    }
+    else {
+
+        xmlNode* root_element = xmlDocGetRootElement(doc);
+
+
+        /* We don't care what the top level element name is */
+        xmlNodePtr cur = root_element->xmlChildrenNode;
+        char name[50];
+        unsigned char* value;
+        while (cur != NULL) {
+
+            // get database stuff
+            if (strcmp((char*)cur->name, "localdatabase") == 0) {
+
+                xmlNodePtr dbParams = cur->xmlChildrenNode;
+
+                while (dbParams != NULL) {
+
+                    sprintf(name, "%s", dbParams->name);
+                    value = xmlNodeListGetString(doc, dbParams->xmlChildrenNode, 1);
+                    if (strcmp(name, "name") == 0) sprintf(dbname, "%s", value);
+                    else if (strcmp(name, "user") == 0) sprintf(user, "%s", value);
+                    else if (strcmp(name, "host") == 0) sprintf(host, "%s", value);
+                    else if (strcmp(name, "password") == 0) sprintf(passwd, "%s", value);
+
+                    dbParams = dbParams->next;
+                }
+            }
+
+            cur = cur->next;
+        }
+
+        xmlFreeDoc(doc);
+    }
+
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Using DVO database = '%s'\n", dvoDatabase);
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Using db name      = '%s'\n", dbname);
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Using db host      = '%s'\n", host);
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Using db user      = '%s'\n", user);
+    this->logger->print(this->logger, MSG_INFO, "Dvo", "Using db password  = '%s'\n", passwd);
+
+    int argc = 4;
+    char** argv;
+    argv = (char**)calloc(argc, sizeof(char *));
+    for (int i=0; i<argc; i++)
+        argv[i] = (char*)calloc(200, sizeof(char));
+
+    sprintf(argv[0], "nothing");
+    sprintf(argv[1], "-D");
+    sprintf(argv[2], "CATDIR");
+    sprintf(argv[3], dvoDatabase);
+
+    this->dvoConfig = dvoConfigRead(&argc, argv);
+
+    for (int i=0; i<argc; i++) free(argv[i]);
+    free(argv);
+
+    // method pointers
+    this->destroy = destroy;
+    this->getDetections = getDetections;
+
+    // set up MySQL connection
+    this->mysql = mysql_init(NULL);
+
+    if (!this->mysql)
+        this->logger->print(this->logger, MSG_ERROR, "Dvo", "mysql_init(), out of memory\n");
+    else {
+
+        if (!mysql_real_connect(this->mysql, host, user, passwd, dbname, 0, NULL, 0)) 
+            this->logger->print(this->logger, MSG_ERROR, "Dvo", "Failed to connect to database: %s\n", 
+                    mysql_error(this->mysql));
+
+        else
+            this->logger->print(this->logger, MSG_INFO, "Dvo", "Connected to %s@%s as user '%s'\n",
+                    dbname, host, user);
+
+    }
+
+    return this;
+}
+
+
+/**
+  Main
+  */
+int main(int argc, char **argv) {
+
+    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);
+
+    logger->destroy(logger);
+
+    return 0;
+}
+
Index: /trunk/ippToPsps/src/Dvo.h
===================================================================
--- /trunk/ippToPsps/src/Dvo.h	(revision 31265)
+++ /trunk/ippToPsps/src/Dvo.h	(revision 31265)
@@ -0,0 +1,47 @@
+/** @file Dvo.h
+ *
+ *  @ingroup ippToPsps
+ *
+ *  @author IfA
+ *  Copyright 2011 Institute for Astronomy, University of Hawaii
+ */
+
+#ifndef IPPTOPSPS_DVO_H
+#define IPPTOPSPS_DVO_H
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+#include <mysql/mysql.h>
+#include <mysql/mysql_com.h>
+
+#include "dvo_util.h"
+#include <psmodules.h>
+
+#include "Logger.h"
+
+/**
+
+  Class encapsulating the DVO database
+
+*/
+typedef struct Dvo {
+
+    // fields
+    Logger* logger;
+    dvoConfig* dvoConfig;
+    MYSQL* mysql;
+
+    // methods
+    bool (*getDetections)();
+
+    // destructor
+    void (*destroy)();
+
+} Dvo; 
+
+// constructor
+Dvo* new_Dvo(Logger* logger, const char* dvoDatabase);
+
+#endif // IPPTOPSPS_DVO_H
+
