Index: /trunk/ippTools/src/pztool.c
===================================================================
--- /trunk/ippTools/src/pztool.c	(revision 9966)
+++ /trunk/ippTools/src/pztool.c	(revision 9967)
@@ -32,5 +32,5 @@
 static bool pendingMode(pxConfig *config);
 static bool copydoneMode(pxConfig *config);
-static bool summitExpPrint(FILE *stream, summitExpRow *summitExp);
+
 static psArray *pztoolPendingExp(pxConfig *config);
 static psArray *pztoolPendingImfiles(pxConfig *config);
@@ -78,40 +78,58 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    psArray *summit = summitExpSelectRowObjects(config->dbh,
-        config->where, MAX_ROWS);
-    if (!summit) {
-        psError(PS_ERR_UNKNOWN, false, "no summitExp rows found"); \
-        return false;
-    }
-
-    psArray *new = newExpSelectRowObjects(config->dbh,
-        config->where, MAX_ROWS);
-    if (!new) {
-        psError(PS_ERR_UNKNOWN, false, "no newExp rows found"); \
-    }
-
-    if (new) {
-        for (long i = 0; i < summit->n; i++) {
-            summitExpRow *summitExp = summit->data[i];
-            for (long j = 0; j < summit->n; j++) {
-                newExpRow *newExp = new->data[j];
-                if (strcmp(summitExp->exp_id, newExp->exp_id) == 0) {
-                    psArrayRemoveData(summit, summitExp);
-                    // dec the counter as the array just got shorter
-                    //and we don't want to skip elemnts
-                    i--;
-                    break;
-                }
-            }
-        }
-        psFree(new);
-    }
-
-    for (long i = 0; i < summit->n; i++) {
-        if (!summitExpPrint(stdout, summit->data[i])) {
-            psError(PS_ERR_UNKNOWN, false,"summitExpPrint() failed");
+    psString query = psStringCopy(
+        "SELECT"
+        "    summitExp.*"
+        " FROM summitExp"
+        " LEFT JOIN newExp"
+        "   USING(exp_id, camera, telescope)"
+        " WHERE"
+        "   newExp.exp_id is NULL"
+        "   AND newExp.camera IS NULL"
+        "   AND newExp.telescope IS NULL"
+    );
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "newExp");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psError(PS_ERR_UNKNOWN, false, "no newExp rows found");
+        psFree(output);
+        return true;
+    }
+
+    bool simple = false;
+    {
+        bool status = false;
+        simple = psMetadataLookupBool(&status, config->args, "-simple");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
             return false;
         }
     }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "newExp", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
 
     return true;
@@ -201,20 +219,4 @@
 
     return false;
-}
-
-static bool summitExpPrint(FILE *stream, summitExpRow *summitExp)
-{
-    PS_ASSERT_PTR_NON_NULL(stream, false);
-    PS_ASSERT_PTR_NON_NULL(summitExp, false);
-
-    fprintf(stream, "%s %s %s %s %s\n",
-        summitExp->exp_id,
-        summitExp->camera,
-        summitExp->telescope,
-        summitExp->exp_type,
-        summitExp->uri
-    );
-
-    return true;
 }
 
Index: /trunk/ippTools/src/pztoolConfig.c
===================================================================
--- /trunk/ippTools/src/pztoolConfig.c	(revision 9966)
+++ /trunk/ippTools/src/pztoolConfig.c	(revision 9967)
@@ -42,34 +42,36 @@
     // -seen
     psMetadata *seenArgs = psMetadataAlloc();
-    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_tag",  0,
-        "define exposure ID", NULL); 
-    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-inst",  0,
-        "define camera ID", NULL); 
-    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-telescope",  0,
-        "define telescope ID", NULL); 
-    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_type",  0,
-        "define exposure type", NULL); 
+    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_tag", 0,
+            "define exposure ID", NULL); 
+    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-inst", 0,
+            "define camera ID", NULL); 
+    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-telescope", 0,
+            "define telescope ID", NULL); 
+    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_type", 0,
+            "define exposure type", NULL); 
+    psMetadataAddBool(seenArgs, PS_LIST_TAIL, "-simple", 0,
+            "use the simple output format", false);
     
     // -pending
     psMetadata *pendingArgs = psMetadataAlloc();
-    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-exp_tag",  0,
-        "define exposure ID", NULL); 
-    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-inst",  0,
-        "define camera ID", NULL); 
-    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-telescope",  0,
-        "define telescope ID", NULL); 
-    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-exp_type",  0,
-        "define exposure type", NULL); 
+    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-exp_tag", 0,
+            "define exposure ID", NULL); 
+    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-inst", 0,
+            "define camera ID", NULL); 
+    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-telescope", 0,
+            "define telescope ID", NULL); 
+    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-exp_type", 0,
+            "define exposure type", NULL); 
 
     // -copydone
     psMetadata *copydoneArgs = psMetadataAlloc();
-    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-exp_tag",  0,
-        "define exposure ID", NULL); 
-    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class",  0,
-        "define class", NULL);
-    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class_id",  0,
-        "define class_id", NULL);
-    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-suri",  0,
-        "define storage uri", NULL);
+    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-exp_tag", 0,
+            "define exposure ID", NULL); 
+    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class", 0,
+            "define class", NULL);
+    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class_id", 0,
+            "define class_id", NULL);
+    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-suri", 0,
+            "define storage uri", NULL);
 
 #define PXTOOL_MODE(option, modeval, argset) \
@@ -80,10 +82,17 @@
         if (config->mode) { \
             psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed"); \
+            goto FAIL; \
         } \
         config->mode = modeval; \
         config->args = psMemIncrRefCounter(argset); \
     } \
+    if (!psMetadataAddMetadata(argSets, PS_LIST_TAIL, option, 0, NULL, argset)) {;\
+        psError(PS_ERR_UNKNOWN, false, "failed to add argset for %s", option); \
+    } \
+    psFree(argset); \
 }
 
+    psMetadata *argSets = psMetadataAlloc();
+    // find which mode we're running under
     PXTOOL_MODE("-seen",            PZTOOL_MODE_SEEN,         seenArgs);
     PXTOOL_MODE("-pending",         PZTOOL_MODE_PENDING,      pendingArgs);
@@ -100,26 +109,25 @@
 
     if (argErr) {
-        printf("\nPan-STARRS Phase Z Search Tool\n");
+        printf("\nPan-STARRS Detrend Tool\n");
         printf("Usage: %s <mode> [<options>]\n\n", argv[0]);
-        printf(" <mode> : -seen | -pending | -copydone\n\n");
+        printf(" <mode> :\n\n"); 
 
-        fprintf (stdout, "-seen ");
-        psArgumentHelp(seenArgs);
-        psFree(seenArgs);
+        psMetadataIterator *iter = psMetadataIteratorAlloc(argSets, 0, NULL);
+        psMetadataItem *item = NULL;
+        while ((item = psMetadataGetAndIncrement(iter))) {
+            if (!item->type == PS_DATA_METADATA) {
+                psAbort(argv[0], "all options must be specified as a metadata");
+            }
 
-        fprintf (stdout, "-pending ");
-        psArgumentHelp(pendingArgs);
-        psFree(pendingArgs);
+            fprintf(stdout, "%s ", item->name);
+            psArgumentHelp(item->data.md);
+        }
+        psFree(iter);
 
-        fprintf (stdout, "-copydone ");
-        psArgumentHelp(copydoneArgs);
-        psFree(copydoneArgs);
-
+        psFree(argSets);
         goto FAIL;
     }
 
-    psFree(seenArgs);
-    psArgumentHelp(pendingArgs);
-    psFree(copydoneArgs);
+    psFree(argSets);
 
     // setup search criterion
