IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 25156


Ignore:
Timestamp:
Aug 20, 2009, 11:58:04 AM (17 years ago)
Author:
bills
Message:

added option to print rows in simple format and options to print
certain values from the header

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pstamp/src/pstampdump.c

    r21404 r25156  
    1 // pstampdump  - read a fits table describing a postage stamp request or response file and
     1// pstampdump  - read a fits file  containing a postage stamp request or response table and
    22//               print the contents to stdout
    33//
    4 //              Actually should work for any kind of fits table, but I'm not sure what will
    5 //              happen for binary data.
     4//              Actually this should work fine for dumping the rows of any of any kind of fits table
     5//              The -header option is pstamp table specific
    66
    77#include <pslib.h>
     
    99#include <string.h>
    1010
    11 static psArray *readRequestTable(psString fileName)
     11static bool readFitsFile(psString fileName, psMetadata **pHeader, psArray **pTable)
    1212{
    13     psFits *fitsFile = psFitsOpen(fileName, "r");
    14     if (fitsFile == NULL) {
     13    psFits *fits = psFitsOpen(fileName, "r");
     14    if (fits == NULL) {
    1515        psError(PS_ERR_IO, false, "failed to open %s for output", fileName);
    16         return NULL;
     16        return false;
     17    }
     18    if (!psFitsMoveExtNum(fits, 1, true)) {
     19        psError(PS_ERR_IO, false, "failed to move to first extension from %s", fileName);
     20        return false;
    1721    }
    1822
    19     psArray *array = psFitsReadTable(fitsFile);
     23    if (pHeader) {
     24        *pHeader = psFitsReadHeader(NULL, fits);
     25        if (!*pHeader) {
     26            psError(PS_ERR_IO, false, "failed to header from %s", fileName);
     27            return false;
     28        }
     29    }
    2030
    21     psFitsClose(fitsFile);
     31    if (pTable) {
     32        *pTable = psFitsReadTable(fits);
     33        if (*pTable == NULL) {
     34            psError(PS_ERR_IO, false, "psFitsReadTable failed for %s", fileName);
     35            return false;
     36        }
     37    }
    2238
    23     if (array == NULL) {
    24         psError(PS_ERR_IO, false, "psFitsReadTable failed for %s", fileName);
    25         return NULL;
    26     }
     39    psFitsClose(fits);
    2740
    28     return array;
     41    return true;
    2942}
    3043
     
    3245usage(char *program_name)
    3346{
    34     fprintf(stderr, "usage: %s filename\n", program_name);
     47    fprintf(stderr, "usage: %s [-header] [-simple] filename\n", program_name);
    3548    exit(1);
    3649}
     50
    3751int main(int argc, char *argv[])
    3852{
     53    bool dumpHeader = false;
     54    bool simple = false;
     55    bool dumpTable = true;
     56    int argnum;
     57    if ((argnum = psArgumentGet(argc, argv, "-header"))) {
     58        dumpHeader = true;
     59        psArgumentRemove(argnum, &argc, argv);
     60    }
     61    if ((argnum = psArgumentGet(argc, argv, "-simple"))) {
     62        simple = true;
     63        psArgumentRemove(argnum, &argc, argv);
     64    }
     65    if ((argnum = psArgumentGet(argc, argv, "-headeronly"))) {
     66        dumpTable = false;
     67        dumpHeader = true;
     68        psArgumentRemove(argnum, &argc, argv);
     69    }
     70
    3971    if (argc != 2) {
    4072        usage(argv[0]);
     
    4375    psString fileName = argv[1];
    4476
    45     psArray *array = readRequestTable(fileName);
    46     if (array == NULL) {
    47         psErrorStackPrint(stderr, "failed to read fits table: %s\n", fileName);
     77    psMetadata *header;
     78    psArray *array;
     79    if (!readFitsFile(fileName, dumpHeader ? &header : NULL, dumpTable ? &array : NULL)) {
     80        psErrorStackPrint(stderr, "failed to process fits table from: %s\n", fileName);
     81        return 1;
     82    }
     83    if (dumpHeader) {
     84        psString extname = psMetadataLookupStr(NULL, header, "EXTNAME");
     85        if (!extname) {
     86            psErrorStackPrint(stderr, "failed to find EXTNAME in fits header of: %s\n", fileName);
     87            return 1;
     88        }
     89        psString req_name = psMetadataLookupStr(NULL, header, "REQ_NAME");
     90        if (!req_name) {
     91            psErrorStackPrint(stderr, "failed to find REQ_NAME in fits header of: %s\n", fileName);
     92            return 1;
     93        }
     94        if (!strcmp(extname, "PS1_PS_REQUEST")) {
     95            psString extver = psMetadataLookupStr(NULL, header, "EXTVER");
     96            if (!extver) {
     97                psErrorStackPrint(stderr, "failed to find EXTVER in fits header of: %s\n", fileName);
     98                return 1;
     99            }
     100            printf("%s %s %s\n", extname, extver, req_name);
     101        } else if (!strcmp(extname, "PS1_PS_RESULTS")) {
     102            psS64 req_id = psMetadataLookupS64(NULL, header, "REQ_ID");
     103            printf("%s %s %" PRId64 "\n", extname, req_name, req_id);
     104        } else {
     105            psErrorStackPrint(stderr, "do not recognize extname: %s in %s\n", extname, fileName);
     106            return 1;
     107        }
     108    }
     109    if (!dumpTable) {
     110        // done
     111        return 0;
     112    }
     113
     114    if (!psArrayLength(array)) {
     115        fprintf(stderr, "%s contains an empty table\n", fileName);
    48116        return 1;
    49117    }
    50118
    51     if (!psArrayLength(array)) {
    52         fprintf(stderr, "%s is an empty table\n", fileName);
    53         exit(1);
    54     }
    55 
    56 //    printf("FITS_TABLE METADATA\n");
    57119    for (int i=0; i<psArrayLength(array); i++) {
    58         printf("ROW_%d METADATA\n", i);
    59         // psMetadataPrint(stderr, array->data[i], 0);
    60         // psMetadataConfigWrite(array->data[i], "-");
    61120        psString str = psMetadataConfigFormat(array->data[i]);
    62121        if (!str) {
    63             psErrorStackPrint(stderr, "Can't write to STDOUT\n");
    64             exit(PS_EXIT_SYS_ERROR);
     122            psErrorStackPrint(stderr, "failed to format metadata item\n");
     123            return (PS_EXIT_SYS_ERROR);
    65124        }
    66         printf("%s", str);
    67         printf("END\n");
     125        if (!simple) {
     126            printf("ROW_%d METADATA\n", i);
     127            printf("%s", str);
     128            printf("END\n");
     129        } else {
     130            // simple output format space separated values one line per row
     131            char *p = str;
     132            char *pnl;
     133            while ((pnl = strchr(p, '\n'))) {
     134                // terminate the string for this line
     135                *pnl = 0;
     136                bool blank = (p == pnl);
     137                if (blank) {
     138                    p = pnl + 1;
     139                    continue;
     140                }
     141                // split line into space separated tokens
     142                char *name = strtok(p, " ");
     143                char *type = strtok(NULL, " ");
     144                char *val = strtok(NULL, " ");
     145
     146                // avoid unused variables warning/error
     147                (void) name; (void) type;
     148
     149                if (val) {
     150                    printf("%s ", val);
     151                }
     152                // next line
     153                p = pnl + 1;
     154            }
     155            printf("\n");
     156        }
    68157    }
    69 //   printf("END\n");
    70158
    71159    return 0;
Note: See TracChangeset for help on using the changeset viewer.