Index: trunk/pstamp/src/pstampparse.c
===================================================================
--- trunk/pstamp/src/pstampparse.c	(revision 17456)
+++ trunk/pstamp/src/pstampparse.c	(revision 18243)
@@ -72,4 +72,6 @@
         options->mode = parseMode(argv[argnum]);
         psArgumentRemove(argnum, &argc, argv);
+    } else {
+        options->mode = PSP_MODE_LIST_JOB;
     }
 
@@ -82,4 +84,9 @@
         options->req_id = atol(argv[argnum]);
         psArgumentRemove(argnum, &argc, argv);
+    }
+
+    if ((options->mode == PSP_MODE_QUEUE_JOB) && (options->req_id <= 0)) {
+        psError(PS_ERR_UNKNOWN, true, "need request id in order to queue jobs\n");
+        return false;
     }
 
@@ -147,5 +154,5 @@
     // XXX: need to select the database name based on the project name specified in the input
 
-    options->config->database = pmConfigDB(options->config);
+    options->config->database = psMemIncrRefCounter(pmConfigDB(options->config));
 
     if (!options->config->database) {
@@ -215,5 +222,5 @@
     //
     //
-    // we return an array containing strins for the values
+    // we return an array containing strings for the values
 
     if (!p_psDBRunQuery(options->config->database, query, target, key1, key2)) {
@@ -264,6 +271,6 @@
 
     psStringAppend(&query, "SELECT %%s from %s WHERE %s = %%s", table, id_type);
-    // append class_id unless it is set to the special value "all"
-    if (class_id && strcmp("all", class_id)) {
+    // append class_id unless it is set to the special value "null"
+    if (class_id && strcmp("null", class_id)) {
         psStringAppend(&query, "%s", " and class_id = '%s'" );
     }
@@ -559,39 +566,43 @@
     bool status = false;;
     psString roiString = NULL;
-    psString ra = psMetadataLookupStr(&status, options->md, "RA");
-
-    if (ra != NULL) {
-        psString dec =  psMetadataLookupStr(&status, options->md, "DEC");
-        if (dec == NULL) {
-            psError(PS_ERR_UNKNOWN, true, "bad request file: RA specified without DEC");
-            return NULL;
-        }
-        psStringAppend(&roiString, "-skycenter %s %s", ra, dec);
+
+    psU32 coord_mask = psMetadataLookupU32(&status, options->md, "COORD_MASK");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "bad request file: COORD_MASK not specified");
+        return NULL;
+    }
+
+    psString x = psMetadataLookupStr(&status, options->md, "CENTER_X");
+    if (x == NULL) {
+        psError(PS_ERR_UNKNOWN, true, "bad request file: CENTER_X not specified");
+        return NULL;
+    }
+    psString y = psMetadataLookupStr(&status, options->md, "CENTER_Y");
+    if (y == NULL) {
+        psError(PS_ERR_UNKNOWN, true, "bad request file: CENTER_Y not specified");
+        return NULL;
+    }
+
+    if (coord_mask & PSTAMP_CENTER_IN_PIXELS) {
+        psStringAppend(&roiString, "-pixcenter %s %s", x, y);
     } else {
-        psString x = psMetadataLookupStr(&status, options->md, "CENTER_X");
-        if (x == NULL) {
-            psError(PS_ERR_UNKNOWN, true, "bad request file: center not specified");
-            return NULL;
-        }
-        psString y = psMetadataLookupStr(&status, options->md, "CENTER_Y");
-        psStringAppend(&roiString, "-pixcenter %s %s", x, y);
-    }
-
-    psString d_ra = psMetadataLookupStr(&status, options->md, "D_RA");
-    if (d_ra != NULL) {
-        psString d_dec =  psMetadataLookupStr(&status, options->md, "D_DEC");
-        if (d_dec == NULL) {
-            psError(PS_ERR_UNKNOWN, true, "bad request file: D_RA specified without D_DEC");
-            return NULL;
-        }
-        psStringAppend(&roiString, " -arcrange %s %s", d_ra, d_dec);
+        psStringAppend(&roiString, "-skycenter %s %s", x, y);
+    }
+
+    psString w = psMetadataLookupStr(&status, options->md, "WIDTH");
+    if (w == NULL) {
+        psError(PS_ERR_UNKNOWN, true, "bad request file: WIDTH not specified");
+        return NULL;
+    }
+    psString h = psMetadataLookupStr(&status, options->md, "HEIGHT");
+    if (h == NULL) {
+        psError(PS_ERR_UNKNOWN, true, "bad request file: HEIGHT not specified");
+        return NULL;
+    }
+
+    if (coord_mask & PSTAMP_RANGE_IN_PIXELS) {
+        psStringAppend(&roiString, " -pixrange %s %s", w, h);
     } else {
-        psString w = psMetadataLookupStr(&status, options->md, "WIDTH");
-        if (w == NULL) {
-            psError(PS_ERR_UNKNOWN, true, "bad request file: range not specified");
-            return NULL;
-        }
-        psString h = psMetadataLookupStr(&status, options->md, "HEIGHT");
-        psStringAppend(&roiString, " -pixrange %s %s", w, h);
+        psStringAppend(&roiString, " -arcrange %s %s", w, h);
     }
 
@@ -636,5 +647,5 @@
 
 static bool
-queueJobs (pspOptions *options, psString job_type, psArray *uris, psString user_tag, psString commandArgs)
+queueJobs (pspOptions *options, psString job_type, psArray *uris, psString stamp_name, psString commandArgs)
 {
     int numURIs = psArrayLength(uris);
@@ -643,11 +654,11 @@
     if (numURIs == 1) {
         // use the user tag as the outputBase
-        status = queueOneJob(options, job_type, uris->data[0], user_tag, commandArgs);
+        status = queueOneJob(options, job_type, uris->data[0], stamp_name, commandArgs);
     } else {
         for (int i = 0; i< numURIs; i++) {
             psString outputBase = NULL;
 
-            // append an integer to the user_tag to get a uniqueu outputBase
-            psStringAppend(&outputBase, "%s_%d", user_tag, i);
+            // append an integer to the stamp_name to get a uniqueu outputBase
+            psStringAppend(&outputBase, "%s_%d", stamp_name, i);
 
             status = queueOneJob(options, job_type, uris->data[i], outputBase, commandArgs);
@@ -664,8 +675,8 @@
 
 static bool
-turnOffResultsFile(pspOptions *options)
+turnOnResultsFile(pspOptions *options)
 {
     // turn off the creation of a results file for this request
-    char * query = "UPDATE pstampRequest SET resultsFile = 0 WHERE req_id = %" PRId64;
+    char * query = "UPDATE pstampRequest SET resultsFile = 1 WHERE req_id = %" PRId64;
     return p_psDBRunQuery(options->config->database, query, options->req_id);
 }
@@ -678,8 +689,10 @@
     psString    img_type = psMetadataLookupStr(NULL, options->md, "IMG_TYPE");
 
+#ifdef notdef
     if (! turnOffResultsFile(options)) {
         fprintf(stderr, "failed to update resultsFile for request %" PRId64 "\n", options->req_id);
         return false;
     }
+#endif
 
     if (options->outputDirectory == NULL) {
@@ -716,32 +729,16 @@
 parseRequestTable(pspOptions *options)
 {
-    bool status;
+    bool status = false;
+
     psString job_type = psMetadataLookupStr(&status, options->md, "JOB_TYPE");
-
     if (job_type == NULL) {
-        // XXX: compatiabilty hack change to error at some point
-        job_type = "stamp";
-    }
-
-    psString cmd_mode = psMetadataLookupStr(&status, options->md, "CMD_MODE");
-
-    // if cmd_mode is set on the command line ignore the value in the request file
-    if (options->mode == PSP_MODE_UNKNOWN) {
-        if (cmd_mode != NULL) {
-            options->mode = parseMode(cmd_mode);
-        } else {
-            // default to listing the parameters of the job's that the request would queue
-            options->mode = PSP_MODE_LIST_JOB;
-        }
-    }
-
-    if ((options->mode == PSP_MODE_QUEUE_JOB) && (options->req_id <= 0)) {
-        fprintf(stderr, "need request id in order to queue jobs\n");
-        return false;
-    }
+        psError(PS_ERR_UNKNOWN, true, "bad request file: JOB_TYPE not specified");
+        return false;
+    }
+
+    bool isStampJob = (strcmp(job_type, "stamp") == 0);
 
     // how are we to select the images ?
     psString req_type = psMetadataLookupStr(&status, options->md, "REQ_TYPE");
-
     if ( !req_type) {
         psError(PS_ERR_UNKNOWN, true, "request file has no REQ_TYPE");
@@ -750,4 +747,8 @@
 
     psString roiString = parseROI(options);
+    if (isStampJob && !roiString) {
+        psError(PS_ERR_UNKNOWN, false, "need valid ROI for stamp request");
+        return false;
+    }
 
     psArray *uris = NULL;
@@ -776,5 +777,5 @@
 
     if (uris == NULL) {
-        // may need a finer grained status code: did something go wrong with the system
+        // XXX we need a finer grained status code: did something go wrong with the system
         // or are there simply no images matching the request
         fprintf(stderr, "No matching images found.\n");
@@ -784,4 +785,5 @@
     int numURIs = psArrayLength(uris);
     if (options->mode == PSP_MODE_LIST_URI) {
+        // Just list the images that would have jobs queued for them
         for (int i=0; i<numURIs; i++) {
             fprintf(stdout, "%s\n", (psString) uris->data[i]);
@@ -790,19 +792,21 @@
         psString commandArgs = roiString;
         psString class_id = psMetadataLookupStr(&status, options->md, "CLASS_ID");
-        if (class_id) {
+        // We don't check here whether class_id is required. We leave that up to
+        // ppstamp
+        if (class_id && strcmp(class_id, "null")) {
             // TODO: the ppstamp argument needs to change to class_id
             // perhaps allow chip as a synonym
             psStringAppend(&commandArgs, " -chip %s", class_id);
         }
-        psString user_tag = psMetadataLookupStr(&status, options->md, "USER_TAG");
+        psString stamp_name = psMetadataLookupStr(&status, options->md, "STAMP_NAME");
 
         if (options->mode == PSP_MODE_LIST_JOB) {
             if (numURIs == 1) {
                 printf("%s %s %s %s\n", job_type, (psString) uris->data[0],
-                    user_tag ? user_tag : "", commandArgs ? commandArgs : "") ;
+                    stamp_name ? stamp_name : "", commandArgs ? commandArgs : "") ;
             } else {
-                // add an integer to the user_tag
+                // add an integer to the stamp_name
                 for (int i = 0; i < numURIs; i++) {
-                    printf("%s %s %s_%d %s\n", job_type, (psString) uris->data[i], user_tag, i,
+                    printf("%s %s %s_%d %s\n", job_type, (psString) uris->data[i], stamp_name, i,
                         commandArgs ? commandArgs :"");
                 }
@@ -812,19 +816,28 @@
             if (!strcmp(job_type, "stamp")) {
                 // Postage stamp job
-                if (user_tag == NULL) {
-                    psError(PS_ERR_UNKNOWN, true, "no user tag in request file");
+                if (stamp_name == NULL) {
+                    psError(PS_ERR_UNKNOWN, true, "no STAMP_NAME in request file");
                     return false;
                 }
 
-                if (!queueJobs(options, job_type, uris, user_tag, commandArgs)) {
+                if (!queueJobs(options, job_type, uris, stamp_name, commandArgs)) {
+                    return false;
+                }
+
+                // Note: This is a DB operation
+                if (!turnOnResultsFile(options)) {
+                    psError(PS_ERR_UNKNOWN, true, "failed to update resultsFile");
+
+                    // TODO: should we unqueue the jobs, set state of request to false??
+                    // Maybe get rid of the column and just use job_type to trigger creation of
+                    // results file. so what if the gui doesn't need it?
                     return false;
                 }
             } else {
-
                 return queueGetImageJob(options, uris, req_type);
             }
 
         } else {
-            // this error is actually caught by parseMode but we might as well check
+            // this error is actually caught by parseMode
             fprintf(stderr, "unknown command mode found: %d\n", options->mode);
             exit(1);
@@ -848,12 +861,15 @@
 
     if (!options) {
+        psErrorStackPrint(stderr, "unable to parse arguments");
         return 1;
     }
 
     if (!readRequestTable(options)) {
+        psErrorStackPrint(stderr, "unable to read request table");
         return 1;
     }
 
     if (!setupDB(options)) {
+        psErrorStackPrint(stderr, "unable to set up data base connection");
         return 1;
     }
@@ -864,5 +880,5 @@
 
     if (!parseRequestTable(options)) {
-        // psErrorStackPrint(stderr, "unable to parse request table");
+        psErrorStackPrint(stderr, "unable to parse request table");
         return 1;
     }
