Index: /trunk/pstamp/src/pstampdump.c
===================================================================
--- /trunk/pstamp/src/pstampdump.c	(revision 25155)
+++ /trunk/pstamp/src/pstampdump.c	(revision 25156)
@@ -1,7 +1,7 @@
-// pstampdump  - read a fits table describing a postage stamp request or response file and
+// pstampdump  - read a fits file  containing a postage stamp request or response table and
 //               print the contents to stdout
 //
-//              Actually should work for any kind of fits table, but I'm not sure what will
-//              happen for binary data.
+//              Actually this should work fine for dumping the rows of any of any kind of fits table
+//              The -header option is pstamp table specific
 
 #include <pslib.h>
@@ -9,22 +9,35 @@
 #include <string.h>
 
-static psArray *readRequestTable(psString fileName)
+static bool readFitsFile(psString fileName, psMetadata **pHeader, psArray **pTable)
 {
-    psFits *fitsFile = psFitsOpen(fileName, "r");
-    if (fitsFile == NULL) {
+    psFits *fits = psFitsOpen(fileName, "r");
+    if (fits == NULL) {
         psError(PS_ERR_IO, false, "failed to open %s for output", fileName);
-        return NULL;
+        return false;
+    }
+    if (!psFitsMoveExtNum(fits, 1, true)) {
+        psError(PS_ERR_IO, false, "failed to move to first extension from %s", fileName);
+        return false;
     }
 
-    psArray *array = psFitsReadTable(fitsFile);
+    if (pHeader) {
+        *pHeader = psFitsReadHeader(NULL, fits);
+        if (!*pHeader) {
+            psError(PS_ERR_IO, false, "failed to header from %s", fileName);
+            return false;
+        }
+    }
 
-    psFitsClose(fitsFile);
+    if (pTable) {
+        *pTable = psFitsReadTable(fits);
+        if (*pTable == NULL) {
+            psError(PS_ERR_IO, false, "psFitsReadTable failed for %s", fileName);
+            return false;
+        }
+    }
 
-    if (array == NULL) {
-        psError(PS_ERR_IO, false, "psFitsReadTable failed for %s", fileName);
-        return NULL;
-    } 
+    psFitsClose(fits);
 
-    return array;
+    return true;
 }
 
@@ -32,9 +45,28 @@
 usage(char *program_name)
 {
-    fprintf(stderr, "usage: %s filename\n", program_name);
+    fprintf(stderr, "usage: %s [-header] [-simple] filename\n", program_name);
     exit(1);
 }
+
 int main(int argc, char *argv[])
 {
+    bool dumpHeader = false;
+    bool simple = false;
+    bool dumpTable = true;
+    int argnum;
+    if ((argnum = psArgumentGet(argc, argv, "-header"))) {
+        dumpHeader = true;
+        psArgumentRemove(argnum, &argc, argv);
+    }
+    if ((argnum = psArgumentGet(argc, argv, "-simple"))) {
+        simple = true;
+        psArgumentRemove(argnum, &argc, argv);
+    }
+    if ((argnum = psArgumentGet(argc, argv, "-headeronly"))) {
+        dumpTable = false;
+        dumpHeader = true;
+        psArgumentRemove(argnum, &argc, argv);
+    }
+
     if (argc != 2) {
         usage(argv[0]);
@@ -43,29 +75,85 @@
     psString fileName = argv[1];
 
-    psArray *array = readRequestTable(fileName);
-    if (array == NULL) {
-        psErrorStackPrint(stderr, "failed to read fits table: %s\n", fileName);
+    psMetadata *header;
+    psArray *array;
+    if (!readFitsFile(fileName, dumpHeader ? &header : NULL, dumpTable ? &array : NULL)) {
+        psErrorStackPrint(stderr, "failed to process fits table from: %s\n", fileName);
+        return 1;
+    }
+    if (dumpHeader) {
+        psString extname = psMetadataLookupStr(NULL, header, "EXTNAME");
+        if (!extname) {
+            psErrorStackPrint(stderr, "failed to find EXTNAME in fits header of: %s\n", fileName);
+            return 1;
+        }
+        psString req_name = psMetadataLookupStr(NULL, header, "REQ_NAME");
+        if (!req_name) {
+            psErrorStackPrint(stderr, "failed to find REQ_NAME in fits header of: %s\n", fileName);
+            return 1;
+        }
+        if (!strcmp(extname, "PS1_PS_REQUEST")) {
+            psString extver = psMetadataLookupStr(NULL, header, "EXTVER");
+            if (!extver) {
+                psErrorStackPrint(stderr, "failed to find EXTVER in fits header of: %s\n", fileName);
+                return 1;
+            }
+            printf("%s %s %s\n", extname, extver, req_name);
+        } else if (!strcmp(extname, "PS1_PS_RESULTS")) {
+            psS64 req_id = psMetadataLookupS64(NULL, header, "REQ_ID");
+            printf("%s %s %" PRId64 "\n", extname, req_name, req_id);
+        } else {
+            psErrorStackPrint(stderr, "do not recognize extname: %s in %s\n", extname, fileName);
+            return 1;
+        }
+    }
+    if (!dumpTable) {
+        // done
+        return 0;
+    }
+
+    if (!psArrayLength(array)) {
+        fprintf(stderr, "%s contains an empty table\n", fileName);
         return 1;
     }
 
-    if (!psArrayLength(array)) {
-        fprintf(stderr, "%s is an empty table\n", fileName);
-        exit(1);
-    }
-
-//    printf("FITS_TABLE METADATA\n");
     for (int i=0; i<psArrayLength(array); i++) {
-        printf("ROW_%d METADATA\n", i);
-        // psMetadataPrint(stderr, array->data[i], 0);
-        // psMetadataConfigWrite(array->data[i], "-");
         psString str = psMetadataConfigFormat(array->data[i]);
         if (!str) {
-            psErrorStackPrint(stderr, "Can't write to STDOUT\n");
-            exit(PS_EXIT_SYS_ERROR);
+            psErrorStackPrint(stderr, "failed to format metadata item\n");
+            return (PS_EXIT_SYS_ERROR);
         }
-        printf("%s", str);
-        printf("END\n");
+        if (!simple) {
+            printf("ROW_%d METADATA\n", i);
+            printf("%s", str);
+            printf("END\n");
+        } else {
+            // simple output format space separated values one line per row
+            char *p = str;
+            char *pnl;
+            while ((pnl = strchr(p, '\n'))) {
+                // terminate the string for this line
+                *pnl = 0;
+                bool blank = (p == pnl);
+                if (blank) {
+                    p = pnl + 1;
+                    continue;
+                }
+                // split line into space separated tokens
+                char *name = strtok(p, " ");
+                char *type = strtok(NULL, " ");
+                char *val = strtok(NULL, " ");
+
+                // avoid unused variables warning/error
+                (void) name; (void) type;
+
+                if (val) {
+                    printf("%s ", val);
+                }
+                // next line
+                p = pnl + 1;
+            }
+            printf("\n");
+        }
     }
-//   printf("END\n");
 
     return 0;
