Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8344)
+++ trunk/ippTools/src/dettool.c	(revision 8349)
@@ -1654,4 +1654,86 @@
 static bool tonormalizeMode(pxConfig *config)
 {
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // select detRun.iteration
+    // select detProcessedImfile.det_id
+    // select detProcessedImfile.class_id
+    // by:
+    // find the current iteration bassed on det_id
+    // find all exp_ids in the current det_id/iteration from detInputExp
+    // find all rawImfiles in the current exp_ids
+    // compare to detProcessedImfiles by det_id/exp_id
+    // found how many imfile there are in each class_id
+    // and: 
+    // det_id is not in detStackedImfile;
+    // iteration is not in detStackedImfile;
+    // class_id is not in detStackedImfile;
+
+    psString query = psStringCopy(
+        " SELECT"
+        "    detRun.position,"
+        "    detRun.iteration,"
+        " FROM detRun"
+        " LEFT JOIN detInputExp"
+        "    ON detRun.position = detInputExp.det_id"
+        "    AND detRun.iteration = detInputExp.iteration"
+        " LEFT JOIN detStackedImfile"
+        "    ON detInputExp.det_id = detStackedImfile.det_id"
+        "    AND detInputExp.iteration = detStackedImfile.iteration"
+        " LEFT JOIN rawImfile"
+        "    ON detInputExp.exp_id = rawImfile.exp_id"
+        " WHERE"
+        "    detStackedImfile.det_id IS NOT NULL"
+        "    AND detStackedImfile.iteration IS NOT NULL"
+        "    AND detStackedImfile.class_id IS NOT NULL"
+        " GROUP BY"
+        "    detStackedImfile.class_id,"
+        "    rawImfile.class_id"
+        " HAVING"
+        "    COUNT(detStackedImfile.class_id) = COUNT(rawImfile.class_id)"
+        ); 
+
+    // XXX does it make sens to accept any search params?
+#if 0
+    if (config->where) {
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where);
+        psStringAppend(&query, " AND %s", whereClaus);
+        psFree(whereClaus);
+    }
+#endif
+
+    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) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
+        return false;
+    }
+
+    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, "rawDetrendImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
     return true;
 }
Index: trunk/ippTools/src/dettoolConfig.c
===================================================================
--- trunk/ippTools/src/dettoolConfig.c	(revision 8344)
+++ trunk/ippTools/src/dettoolConfig.c	(revision 8349)
@@ -171,4 +171,11 @@
     psMetadataAddStr(addstacArgs, PS_LIST_TAIL, "-recip",  0,
         "define recipe (required)", NULL);
+    psMetadataAddBool(addstacArgs, PS_LIST_TAIL, "-pleasenormalize",  0,
+        "dude, make me normal", false);
+
+    // -tonormalize
+    psMetadata *tonormalizeArgs = psMetadataAlloc();
+    psMetadataAddBool(tonormalizeArgs, PS_LIST_TAIL, "-simple",  0,
+        "use the simple output format", false);
     
     // -stacked
@@ -357,4 +364,5 @@
     PXTOOL_MODE("-processed",       DETTOOL_MODE_PROCESSED,     procArgs);
     PXTOOL_MODE("-addstacked",      DETTOOL_MODE_ADDSTACKED,    addstacArgs);
+    PXTOOL_MODE("-tonormalize",     DETTOOL_MODE_TONORMALIZE,   tonormalizeArgs);
     PXTOOL_MODE("-stacked",         DETTOOL_MODE_STACKED,       stacArgs);
     PXTOOL_MODE("-stackedframe",    DETTOOL_MODE_STACKEDFRAME,  stacframeArgs);
@@ -383,5 +391,5 @@
         printf("\nPan-STARRS Detrend Tool\n");
         printf("Usage: %s <mode> [<options>]\n\n", argv[0]);
-        printf(" <mode> : [-pending|-definebyexp|-definebyquery|-runs|-selectrun|-select|-raw|-addprocessed|-tostack|-processed|-addstacked|-stacked|-stackedframe|-addnormalized|-normalized|-normalizedframe|-addresidimfile|-residimfile|-addresidexp|-residexp|-declaremasterframe|-masterframe|-master|-rerun]\n\n");
+        printf(" <mode> : [-pending|-definebyexp|-definebyquery|-runs|-selectrun|-select|-raw|-addprocessed|-tostack|-processed|-addstacked|-tonormalize|-stacked|-stackedframe|-addnormalized|-normalized|-normalizedframe|-addresidimfile|-residimfile|-addresidexp|-residexp|-declaremasterframe|-masterframe|-master|-rerun]\n\n");
         fprintf (stdout, "-pending ");
         psArgumentHelp(pendingArgs);
@@ -431,4 +439,8 @@
         psFree(addstacArgs);
 
+        fprintf (stdout, "-tonormalize ");
+        psArgumentHelp(tonormalizeArgs);
+        psFree(tonormalizeArgs);
+
         fprintf (stdout, "-stacked ");
         psArgumentHelp(stacArgs);
@@ -498,4 +510,5 @@
     psFree(procArgs);
     psFree(addstacArgs);
+    psFree(tonormalizeArgs);
     psFree(stacArgs);
     psFree(stacframeArgs);
