Index: trunk/ippTools/src/guidetool.c
===================================================================
--- trunk/ippTools/src/guidetool.c	(revision 6981)
+++ trunk/ippTools/src/guidetool.c	(revision 6991)
@@ -9,8 +9,6 @@
 
 static bool pendingMode(pxConfig *config);
-static bool updateMode(pxConfig *config);
-static psArray *newFrameSearchPending(pxConfig *config);
-static p1PendingExpRow *newToP1PendingExp(newExpRow *newExp);
-static p2PendingExpRow *newToP2PendingExp(newExpRow *newExp);
+static bool defineMode(pxConfig *config);
+static p1PendingExpRow *rawScienceTop1PendingExp(rawScienceExpRow *raw);
 
 int main(int argc, char **argv)
@@ -24,6 +22,6 @@
             }
             break;
-        case PX_MODE_UPDATE:
-            if (!updateMode(config)) {
+        case PX_MODE_DEFINE:
+            if (!defineMode(config)) {
                 goto FAIL;
             }
@@ -44,176 +42,44 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    psArray *new = newFrameSearchPending(config);
-    if (!new) {
-        psError(PS_ERR_UNKNOWN, false, "no newFrames found");
+    psArray *p1PendingExp = p1PendingExpSelectRowObjects(config->dbh, NULL, 0);
+    if (!p1PendingExp) {
+        psError(PS_ERR_UNKNOWN, false, "no p1PendingExp found");
         return false;
     }
 
-    bool status = newFramePrint(stdout, config, new);
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false,"newFramePrint() failed");
-        return false;
+    psMetadata *output = psMetadataAlloc();
+
+    for (long i = 0; i < psArrayLength(p1PendingExp); i++) {
+        psMetadata *md = p1PendingExpMetadataFromObject(p1PendingExp->data[i]);
+        psMetadataAddMetadata(output, PS_LIST_TAIL, "p1PendingExp",
+            PS_META_DUPLICATE_OK, NULL, md);
+        psFree(md);
     }
+
+    psString str = psMetadataConfigFormat(output);
+    psFree(output);
+
+    fprintf(stdout, "%s\n", str);
+    psFree(str);
 
     return true;
 }
 
-static bool updateMode(pxConfig *config)
+static bool defineMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psArray *new = newFrameSearchPending(config);
-    if (!new) {
-        psError(PS_ERR_UNKNOWN, false, "no newFrames found");
-        return true;
-    }
-
-    // insert 'new' rawScience & detrendframes
-    if (new->n > 0) {
-        for (long i = 0; i < new->n; i++) {
-            newFrame *newFrame = new->data[i];
-            if (strcmp(newFrame->exposure->exp_type, "object") == 0) {
-                // it's a science frame
-                rawScienceFrame *rawScienceFrame = newToRawScienceFrame(newFrame);
-                if (!rawScienceFrameInsert(config, rawScienceFrame)) {
-                    // error
-                }
-            } else {
-                // it's a detrend frame
-                rawDetrendFrame *rawDetrendFrame = newToRawDetrendFrame(newFrame); 
-                if (!rawDetrendFrameInsert(config, rawDetrendFrame)) {
-                    // error
-                }
-            }
-            
-            /*
-            // lookup camera name in p0CameraConfig
-            psMetadata *where = psMetadataAlloc();
-            //bool status = false;
-            psMetadataAddStr(where, PS_LIST_TAIL, "camera", 0, NULL,
-                newFrame->exposure->camera);
-            psArray *configs = p0CameraConfigSelectRowObjects(config->dbh, where, MAX_ROWS);
-            psFree(where);
-            // if there is no match then we default to sending into into p2
-            if (!configs) {
-                // convert newFrame->exposure to p2PendingExp 
-                p2PendingExpRow *p2PendingExp = newToP2PendingExp(
-                    newFrame->exposure
-                );
-                p2PendingExpInsertObject(config->dbh, p2PendingExp);
-                psFree(p2PendingExp);
-                continue;
-            }
-
-            // only use the first match
-            int pX = ((p0CameraConfigRow *)configs->data[0])->phase;
-            switch (pX) {
-                case 1:
-                    // convert newFrame->exposure to p1PendingExp 
-                {
-                    p1PendingExpRow *p1PendingExp = newToP1PendingExp(
-                        newFrame->exposure
-                    );
-                    p1PendingExpInsertObject(config->dbh, p1PendingExp);
-                    psFree(p1PendingExp);
-                }
-                    break;
-                case 2:
-                    // convert newFrame->exposure to p2PendingExp 
-                {
-                    p2PendingExpRow *p2PendingExp = newToP2PendingExp(
-                        newFrame->exposure
-                    );
-                    p2PendingExpInsertObject(config->dbh, p2PendingExp);
-                    psFree(p2PendingExp);
-                }
-                    break;
-                default:
-                    // XXX add argv[0] to pxConfig
-                    psAbort("foo", "invalid phase (this should not happen)");
-            }
-
-            psFree(configs);
-            */
-        }
-        psFree(new);
-    }
 
     return true;
 }
 
-static psArray *newFrameSearchPending(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    psArray *new = newFrameSearch(config);
-    if (!new) {
-        psError(PS_ERR_UNKNOWN, false, "no newFrames found");
-        return false;
-    }
-
-    psArray *raw = rawScienceFrameSearch(config);
-    if (!raw) {
-        psError(PS_ERR_UNKNOWN, false, "no rawScienceFrames found");
-    //    return false;
-    }
-
-    psArray *rawDetrend = rawDetrendFrameSearch(config);
-    if (!rawDetrend) {
-        psError(PS_ERR_UNKNOWN, false, "no rawDetrendFrames found");
-    //    return false;
-    }
-
-    // ignore duplicate raw frames
-    if (raw) {
-        for (long i = 0; i < new->n; i++) {
-            newFrame *newFrame = new->data[i];
-            for (long j = 0; j < raw->n; j++) {
-                rawScienceFrame *rawScienceFrame = raw->data[j];
-                if (strcmp(newFrame->exposure->exp_id,
-                           rawScienceFrame->exposure->exp_id) == 0) {
-                    psArrayRemove(new, newFrame);
-                    // dec the counter as the array just got shorter
-                    //and we don't want to skip elemnts
-                    i--;
-                    break;
-                }
-            }
-        }
-        psFree(raw);
-    }
-
-    // ignore duplicate rawDetrend frames
-    if (rawDetrend) {
-        for (long i = 0; i < new->n; i++) {
-            newFrame *newFrame = new->data[i];
-            for (long j = 0; j < rawDetrend->n; j++) {
-                rawDetrendFrame *rawDetrendFrame = rawDetrend->data[j];
-                if (strcmp(newFrame->exposure->exp_id,
-                           rawDetrendFrame->exposure->exp_id) == 0) {
-                    psArrayRemove(new, newFrame);
-                    // dec the counter as the array just got shorter
-                    //and we don't want to skip elemnts
-                    i--;
-                    break;
-                }
-            }
-        }
-        psFree(rawDetrend);
-    }
-
-    return new;
-}
-
-
-static p1PendingExpRow *newToP1PendingExp(newExpRow *newExp)
+static p1PendingExpRow *rawScienceTop1PendingExp(rawScienceExpRow *raw)
 {
     return p1PendingExpRowAlloc(
-        newExp->exp_id,
-        newExp->camera,
-        newExp->telescope,
-        newExp->exp_type,
+        raw->exp_id,
+        raw->camera,
+        raw->telescope,
+        raw->exp_type,
 //        newExp->class,
-        newExp->imfiles,
+        raw->imfiles,
         "my filter",
         "my stats",
@@ -222,19 +88,2 @@
     );
 }
-
-static p2PendingExpRow *newToP2PendingExp(newExpRow *newExp)
-{
-    return p2PendingExpRowAlloc(
-        newExp->exp_id,
-        newExp->camera,
-        newExp->telescope,
-        newExp->exp_type,
-//        newExp->class,
-        newExp->imfiles,
-        "my filter", 
-        "my stats",
-        "my recipe",
-        0xff, // XXX calc version number
-        0xff // XXX calc version number
-    );
-}
