/*--------------------------------------------------------------------------- File name : dtfits.c Author : N. Devillard Created on : July 1999 Description : FITS table dump *--------------------------------------------------------------------------*/ /* $Id: dtfits.c,v 1.10 2002/03/04 12:46:46 yjung Exp $ $Author: yjung $ $Date: 2002/03/04 12:46:46 $ $Revision: 1.10 $ */ /*--------------------------------------------------------------------------- Includes ---------------------------------------------------------------------------*/ #include #include #include "qfits.h" /*--------------------------------------------------------------------------- Private declarations ---------------------------------------------------------------------------*/ static int dump_extension(qfits_table * tdesc, FILE * out, char separator, int data_only, int use_zero_scale) ; static void qfits_dump(char *, char *, int, char); static void usage(char *pname) ; static char prog_desc[] = "FITS table dump" ; /*---------------------------------------------------------------------------- Main code ---------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { char name_i[FILENAMESZ] ; char name_o[FILENAMESZ] ; int i ; int data_only ; char separator ; data_only = 0 ; separator = '|' ; if (argc<2) { usage(argv[0]); } i=1 ; while (i=argc) { fprintf(stderr, "option -s needs an argument\n"); return -1 ; } i++ ; separator = argv[i][0] ; } else { break ; } i++ ; } if ((argc-i)<1) { fprintf(stderr, "missing input file name\n"); return -1 ; } strcpy(name_i, argv[i]); i++ ; if ((argc-i)<1) { name_o[0] = 0 ; } else { strcpy(name_o, argv[i]); } qfits_dump(name_i, name_o, data_only, separator); return 0 ; } /* This function only gives the usage for the program */ static void usage(char *pname) { printf("%s -- %s\n", pname, prog_desc); printf( "use : %s [options] [out]\n" "options are:\n" "\t-d to dump data only (no headers)\n" "\t-s to output data with separator \n" "\n", pname); exit(0) ; } static void qfits_dump( char * name_i, char * name_o, int data_only, char separator) { qfits_table * tdesc ; FILE * out ; int xtnum ; int n_ext ; /* Set where to send the output */ if (name_o[0]==(char)0) { out = stdout ; } else { if ((out = fopen(name_o, "w"))==NULL) { fprintf(stderr, "cannot create output file [%s]\n", name_o); return ; } } if (!data_only) { fprintf(out, "#\n"); fprintf(out, "# file %s\n", name_i); } /* Query number of extensions in the file */ n_ext = qfits_query_n_ext(name_i); if (!data_only) { fprintf(out, "# extensions %d\n", n_ext); } /* If no extension, bail out */ if (n_ext<1) { if (out!=stdout) fclose(out) ; return ; } /* Loop over all extensions */ for (xtnum=1 ; xtnum<=n_ext ; xtnum++) { if (!data_only) { fprintf(out, "# --------------------------------------------\n"); fprintf(out, "# XTENSION %d\n", xtnum); } if ((tdesc = qfits_table_open(name_i, xtnum)) == NULL) { printf("cannot open table [%s]:[%d]\n", name_i, xtnum); if (out!=stdout) fclose(out); return ; } dump_extension(tdesc, out, separator, data_only, 1) ; qfits_table_close(tdesc); } fclose(out) ; return ; } static int dump_extension( qfits_table * tdesc, FILE * out, char separator, int data_only, int use_zero_scale) { qfits_col * col ; char * stw ; int * sizes ; char format[512] ; int i, j ; if (!data_only) { fprintf(out, "# Number of columns %d\n", tdesc->nc); fprintf(out, "#\n"); } /* Get the sizes of the columns */ sizes = calloc(tdesc->nc, sizeof(int)) ; for (i=0 ; inc ; i++) { for (j=0 ; jnr ; j++) { if ((stw = qfits_table_field_to_string(tdesc, i, j, use_zero_scale)) == NULL) { sizes[i] = 0 ; break ; } else { if (sizes[i] < (int)strlen(stw)) { sizes[i] = (int)strlen(stw) ; } } } } /* Print out the column names */ if (!data_only) { for (i=0 ; inc ; i++) { col = tdesc->col + i ; if (sizes[i] != 0) { if (sizes[i] < strlen(col->tlabel)) sizes[i] = (int)strlen(col->tlabel) ; sprintf(format, "%%%ds", sizes[i]) ; fprintf(out, format, col->tlabel); if (i!=(tdesc->nc-1)) printf("%c", separator); } } fprintf(out, "\n"); } /* Dump all columns, line by line */ col = tdesc->col ; for (j=0 ; jnr ; j++) { for (i=0 ; inc ; i++) { col = tdesc->col + i ; if ((stw = qfits_table_field_to_string(tdesc, i, j, use_zero_scale)) != NULL) { sprintf(format, "%%%ds", sizes[i]) ; fprintf(out, format, stw); if (i!=(tdesc->nc-1)) printf("%c", separator); } } fprintf(out, "\n"); } free(sizes) ; return 0 ; }