Index: /trunk/ippTools/src/pxtools.c
===================================================================
--- /trunk/ippTools/src/pxtools.c	(revision 28108)
+++ /trunk/ippTools/src/pxtools.c	(revision 28109)
@@ -48,68 +48,66 @@
 }
 
-psString pxMergeCodeVersions(psString version1, psString version2) {
+psString pxMergeCodeVersions(psString version1, psString version2)
+{
+    if (!version1 && !version2) {
+        return NULL;
+    }
+
+    if (!version1) {
+        return psStringCopy(version2);
+    }
+    if (!version2) {
+        return psStringCopy(version1);
+    }
+
+    bool mod1 = false, mod2 = false;    // Modified versions?
+    if (strchr(version1, 'M')) {
+        psStringSubstitute(&version1, "M", "");
+        mod1 = true;
+    }
+    if (strchr(version2, 'M')) {
+        psStringSubstitute(&version2, "M", "");
+        mod2 = true;
+    }
+
+    int num1 = strtol(version1, NULL, 10);
+    int num2 = strtol(version2, NULL, 10);
+    int numO = PS_MAX(num1, num2);
+
     psString out = NULL;
-
-    bool mod1 = false;
-    bool mod2 = false;
-
-    psS32 num1;
-    psS32 num2;
-    psS32 numO;
-
-    if (!version1) {
-        psStringAppend(&out, "%s", version2);
-        return(out);
-    }
-    if (!version2) {
-        psStringAppend(&out, "%s", version1);
-        return(out);
-    }
-
-    if (strchr(version1,'M')) {
-        psStringSubstitute(&version1,"M","");
-        mod1 = true;
-    }
-    if (strchr(version2,'M')) {
-        psStringSubstitute(&version2,"M","");
-        mod2 = true;
-    }
-
-    num1 = strtol(version1,NULL,10);
-    num2 = strtol(version2,NULL,10);
-
-    if (num1 >= num2) {
-        numO = num1;
-    }
-    else {
-        numO = num2;
-    }
-
-    psStringAppend(&out,"%" PRId32,numO);
+    psStringAppend(&out, "%" PRId32, numO);
     if (mod1 || mod2) {
-        psStringAppend(&out,"M");
-    }
-    return(out);
+        psStringAppend(&out, "M");
+    }
+    return out;
 }
 
 bool pxCoalesceRunStatus(pxConfig *config, const psString dbQFile, psS64 stage_id, psString *software_ver,
                          psS64 *maskfrac_npix, psF32 *maskfrac_static, psF32 *maskfrac_dynamic,
-                         psF32 *maskfrac_magic, psF32 *maskfrac_advisory) {
+                         psF32 *maskfrac_magic, psF32 *maskfrac_advisory)
+{
     psString query = pxDataGet(dbQFile);
-/*   psString text_id = NULL; */
-/*   psStringAppend(&text_id," %" PRId64,stage_id); */
-/*   psStringSubstitute(&query,text_id,"@STAGE_ID@"); */
-/*   psFree(text_id); */
+    if (!query) {
+        psError(psErrorCodeLast(), false, "Unable to read query");
+        return false;
+    }
     if (!p_psDBRunQueryF(config->dbh, query, stage_id)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+        psError(psErrorCodeLast(), false, "database error");
         psFree(query);
-        return(false);
+        return false;
     }
     psFree(query);
+
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return(false);
-    }
+        psError(psErrorCodeLast(), false, "database error");
+        return false;
+    }
+
+    *maskfrac_npix = 0;
+    *maskfrac_static = 0.0;
+    *maskfrac_dynamic = 0.0;
+    *maskfrac_magic = 0.0;
+    *maskfrac_advisory = 0.0;
 
     for (long i = 0; i < psArrayLength(output); i++) {
@@ -121,72 +119,88 @@
         psF32 this_magic = psMetadataLookupF32(NULL, row, "maskfrac_magic");
         psF32 this_advisory = psMetadataLookupF32(NULL, row, "maskfrac_advisory");
+
+        psTrace("pxtools", 3, "Mask stats: %d %f %f %f %f\n",
+                this_npix, this_static, this_dynamic, this_magic, this_advisory);
+
+        if (this_npix == 0 || this_npix == PS_MAX_S32) {
+            continue;
+        }
+        if (!isfinite(this_static) || !isfinite(this_dynamic) ||
+            !isfinite(this_magic) || !isfinite(this_advisory)) {
+            continue;
+        }
+
         psString this_version = psMetadataLookupStr(NULL, row, "software_ver");
-
-        psTrace("pxtools", 3, "Mask stats: %d %f %f %f %f %s\n",
-                this_npix, this_static, this_dynamic, this_magic, this_advisory, this_version);
-
         *software_ver = pxMergeCodeVersions(*software_ver,this_version);
-/*     printf("%ld : %d %f %f %f %f <-> %ld %f %f %f %f\n",i,this_npix,this_static,this_dynamic,this_magic,this_advisory, */
-/*         *maskfrac_npix,*maskfrac_static,*maskfrac_dynamic,*maskfrac_magic,*maskfrac_advisory); */
-        if (this_npix > 0) {
-            *maskfrac_static = ((*maskfrac_static * *maskfrac_npix) + (this_npix * this_static)) / (this_npix + *maskfrac_npix);
-            *maskfrac_dynamic = ((*maskfrac_dynamic * *maskfrac_npix) + (this_npix * this_dynamic)) / (this_npix + *maskfrac_npix);
-            *maskfrac_magic = ((*maskfrac_magic * *maskfrac_npix) + (this_npix * this_magic)) / (this_npix + *maskfrac_npix);
-            *maskfrac_advisory = ((*maskfrac_advisory * *maskfrac_npix) + (this_npix * this_advisory)) / (this_npix + *maskfrac_npix);
-            *maskfrac_npix += this_npix;
-        }
+
+        *maskfrac_npix += this_npix;
+        *maskfrac_static += this_npix * this_static;
+        *maskfrac_dynamic += this_npix * this_dynamic;
+        *maskfrac_magic += this_npix * this_magic;
+        *maskfrac_advisory += this_npix * this_advisory;
     }
     psFree(output);
-    return(true);
-}
-
-bool pxSetRunSoftware(pxConfig *config, const psString tableName, const psString stage_id_name, const psS64 stage_id,
-                      psString software_ver) {
+
+    if (*maskfrac_npix > 0) {
+        *maskfrac_static /= *maskfrac_npix;
+        *maskfrac_dynamic /= *maskfrac_npix;
+        *maskfrac_magic /= *maskfrac_npix;
+        *maskfrac_advisory /= *maskfrac_npix;
+    }
+
+    return true;
+}
+
+bool pxSetRunSoftware(pxConfig *config, const psString tableName, const psString stage_id_name,
+                      const psS64 stage_id, psString software_ver)
+{
     char *query = "UPDATE %s SET software_ver = '%s' WHERE %s = %" PRId64;
-/*   printf(query,tableName,software_ver,stage_id_name,stage_id); */
-    if (!p_psDBRunQueryF(config->dbh,query,tableName,software_ver,stage_id_name,stage_id)) {
-        psError(PS_ERR_UNKNOWN, false,
-                "failed to set software version for %s %" PRId64,stage_id_name,stage_id);
-        return(false);
-    }
-
-    return(true);
-}
-
-bool pxSetRunMaskfrac(pxConfig *config, const psString tableName, const psString stage_id_name, const psS64 stage_id,
-                      psS64 maskfrac_npix, psF32 maskfrac_static, psF32 maskfrac_dynamic,
-                      psF32 maskfrac_magic, psF32 maskfrac_advisory) {
-    char *query = "UPDATE %s SET maskfrac_npix = %f, maskfrac_static = %f, maskfrac_dynamic = %f, maskfrac_magic = %f, maskfrac_advisory = %f WHERE %s = %" PRId64;
-    if (!p_psDBRunQueryF(config->dbh,query,tableName,(float) maskfrac_npix,maskfrac_static,
-                         maskfrac_dynamic, maskfrac_magic,maskfrac_advisory,stage_id_name,stage_id)) {
-        psError(PS_ERR_UNKNOWN, false,
+    if (!p_psDBRunQueryF(config->dbh, query, tableName, software_ver, stage_id_name, stage_id)) {
+        psError(psErrorCodeLast(), false,
+                "failed to set software version for %s %" PRId64, stage_id_name, stage_id);
+        return false;
+    }
+
+    return true;
+}
+
+bool pxSetRunMaskfrac(pxConfig *config, const psString tableName, const psString stage_id_name,
+                      const psS64 stage_id, psS64 maskfrac_npix, psF32 maskfrac_static,
+                      psF32 maskfrac_dynamic, psF32 maskfrac_magic, psF32 maskfrac_advisory)
+{
+    char *query = "UPDATE %s SET maskfrac_npix = %f, maskfrac_static = %f, maskfrac_dynamic = %f, "
+        "maskfrac_magic = %f, maskfrac_advisory = %f WHERE %s = %" PRId64;
+    if (!p_psDBRunQueryF(config->dbh, query, tableName, (float)maskfrac_npix, maskfrac_static,
+                         maskfrac_dynamic, maskfrac_magic, maskfrac_advisory, stage_id_name, stage_id)) {
+        psError(psErrorCodeLast(), false,
                 "failed to set maskfrac stats for %s %" PRId64,stage_id_name,stage_id);
-        return(false);
-    }
-
-
-
-    return(true);
-}
-
-bool pxCamSetRunMaskfrac(pxConfig *config, const psString tableName, const psString stage_id_name, const psS64 stage_id,
+        return false;
+    }
+
+
+
+    return true;
+}
+
+bool pxCamSetRunMaskfrac(pxConfig *config, const psString tableName,
+                         const psString stage_id_name, const psS64 stage_id,
                          psS64 maskfrac_ref_npix, psF32 maskfrac_ref_static, psF32 maskfrac_ref_dynamic,
                          psF32 maskfrac_ref_magic, psF32 maskfrac_ref_advisory,
                          psS64 maskfrac_max_npix, psF32 maskfrac_max_static, psF32 maskfrac_max_dynamic,
                          psF32 maskfrac_max_magic, psF32 maskfrac_max_advisory) {
-  char *query = "UPDATE %s SET maskfrac_ref_npix = %f, maskfrac_ref_static = %f, maskfrac_ref_dynamic = %f, maskfrac_ref_magic = %f, maskfrac_ref_advisory = %f, maskfrac_max_npix = %f, maskfrac_max_static = %f, maskfrac_max_dynamic = %f, maskfrac_max_magic = %f, maskfrac_max_advisory = %f WHERE %s = %" PRId64;
-  if (!p_psDBRunQueryF(config->dbh,query,tableName,(float) maskfrac_ref_npix,maskfrac_ref_static,
-                       maskfrac_ref_dynamic, maskfrac_ref_magic,maskfrac_ref_advisory,
-                       (float) maskfrac_max_npix,maskfrac_max_static,
-                       maskfrac_max_dynamic, maskfrac_max_magic,maskfrac_max_advisory,
-                       stage_id_name,stage_id)) {
-    psError(PS_ERR_UNKNOWN, false,
-            "failed to set maskfrac stats for %s %" PRId64,stage_id_name,stage_id);
-    return(false);
+  char *query = "UPDATE %s SET maskfrac_ref_npix = %f, maskfrac_ref_static = %f, maskfrac_ref_dynamic = %f, "
+      "maskfrac_ref_magic = %f, maskfrac_ref_advisory = %f, maskfrac_max_npix = %f, maskfrac_max_static = %f, "
+      "maskfrac_max_dynamic = %f, maskfrac_max_magic = %f, maskfrac_max_advisory = %f WHERE %s = %" PRId64;
+  if (!p_psDBRunQueryF(config->dbh, query, tableName, (float)maskfrac_ref_npix, maskfrac_ref_static,
+                       maskfrac_ref_dynamic,  maskfrac_ref_magic, maskfrac_ref_advisory,
+                       (float)maskfrac_max_npix, maskfrac_max_static,
+                       maskfrac_max_dynamic,  maskfrac_max_magic, maskfrac_max_advisory,
+                       stage_id_name, stage_id)) {
+      psError(psErrorCodeLast(), false,
+              "failed to set maskfrac stats for %s %" PRId64,stage_id_name,stage_id);
+      return false;
   }
 
-
-
-  return(true);
+  return true;
 }
 
@@ -225,5 +239,5 @@
     psMetadataItem *item = psMetadataLookup(config->args, name);
     if (!item) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for %s", name);
+        psError(psErrorCodeLast(), false, "failed to lookup value for %s", name);
         return false;
     }
@@ -242,5 +256,5 @@
             item->comment = psStringCopy (op);
             if (!psMetadataAddItem(where, item, PS_LIST_TAIL, PS_META_DUPLICATE_OK)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item %s", field);
+                psError(psErrorCodeLast(), false, "failed to add item %s", field);
                 psFree(where);
                 return false;
@@ -334,5 +348,5 @@
 
     if (!p_psDBRunQueryF(config->dbh, *pQuery, joinHook)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+        psError(psErrorCodeLast(), false, "database error");
         return false;
     }
@@ -346,5 +360,5 @@
 
     if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+        psError(psErrorCodeLast(), false, "database error");
         return false;
     }
@@ -352,14 +366,14 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+        psError(psErrorCodeLast(), false, "database error");
         return false;
     }
     if (!psArrayLength(output)) {
         psFree(output);
-        psError(PS_ERR_UNKNOWN, true, "no rows in dbversion");
+        psError(PXTOOLS_ERR_PROG, true, "no rows in dbversion");
         return false;
     }
     if (psArrayLength(output) > 1) {
-        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found in dbversion: %ld",
+        psError(PXTOOLS_ERR_PROG, true, "unexpected number of rows found in dbversion: %ld",
                 psArrayLength(output));
         return false;
@@ -376,10 +390,10 @@
     psArray *array = NULL;
     if (!pxLookupVersion(config, &array)) {
-        psError(PS_ERR_UNKNOWN, false, "pxLookupVersion failed");
+        psError(psErrorCodeLast(), false, "pxLookupVersion failed");
         return NULL;
     }
     psMetadata *md = array->data[0];
     if (!md) {
-        psError(PS_ERR_UNKNOWN, true, "output of pxLookupVersion is null");
+        psError(PXTOOLS_ERR_PROG, true, "output of pxLookupVersion is null");
         return NULL;
     }
@@ -397,9 +411,9 @@
     psArray *array = NULL;
     if (!pxLookupVersion(config, &array) || !array) {
-        psError(PS_ERR_UNKNOWN, false, "pxLookupVersion failed");
+        psError(psErrorCodeLast(), false, "pxLookupVersion failed");
         return false;
     }
     if (!ippdbPrintMetadatas(file, array, "dbversion", true)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psError(psErrorCodeLast(), false, "failed to print array");
         psFree(array);
         return false;
@@ -417,5 +431,5 @@
     psMetadataItem *multi_item =  psMetadataLookup(input, "dbversion");
     if (!multi_item || (multi_item->type != PS_DATA_METADATA_MULTI)) {
-        psError(PS_ERR_UNKNOWN, true, "dbversion multi not found in input");
+        psError(PXTOOLS_ERR_PROG, true, "dbversion multi not found in input");
         return false;
     }
@@ -423,5 +437,5 @@
     psMetadataItem *dbversion = psListGet(multi_item->data.list, 0);
     if (!dbversion) {
-        psError(PS_ERR_UNKNOWN, true, "dbversion not found in input");
+        psError(PXTOOLS_ERR_PROG, true, "dbversion not found in input");
         return false;
     }
@@ -432,5 +446,5 @@
         psString schema_version = pxGetDBVersion(config);
         if (!schema_version) {
-            psError(PS_ERR_UNKNOWN, false, "pxGetDBVersion failed");
+            psError(psErrorCodeLast(), false, "pxGetDBVersion failed");
             return false;
         }
@@ -438,9 +452,9 @@
         psString import_version = psMetadataLookupStr(NULL, md, "schema_version");
         if (import_version && strcmp(import_version, schema_version)) {
-            psError(PS_ERR_UNKNOWN, true, "input file schema_version: %s does not match data base: %s",
+            psError(PXTOOLS_ERR_PROG, true, "input file schema_version: %s does not match data base: %s",
                     import_version, schema_version);
             return false;
         } else if (!import_version) {
-            psError(PS_ERR_UNKNOWN, true, "input file schema_version is NULL");
+            psError(PXTOOLS_ERR_PROG, true, "input file schema_version is NULL");
             return false;
         } else {
@@ -448,8 +462,8 @@
         }
     } else {
-        psError(PS_ERR_UNKNOWN, true, "Unexpected config dump format");
-        return false;
-    }
-
-    return true;
-}
+        psError(PXTOOLS_ERR_PROG, true, "Unexpected config dump format");
+        return false;
+    }
+
+    return true;
+}
