gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 16441b6: Printing column information in standa


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 16441b6: Printing column information in standard output when requested
Date: Fri, 22 Jun 2018 17:24:05 -0400 (EDT)

branch: master
commit 16441b6d0c4fd7c5cde429d6e1f801125f9d84df
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Printing column information in standard output when requested
    
    Until now it was implicitly assumed that when printing to standard output,
    the column values need to be piped into another program for further
    processing (there was an option to have a text table output). However, some
    times we just want to inspect the contents of the columns on the
    command-line and not necessarily pipe it into another program. In such
    cases, the column information/meta-data can be very helpful.
    
    The library's `gal_txt_write' function now accepts a new `colinfoinstdout'
    argument for such cases. Ideally, it is possible to see if the program's
    output is being piped or simply printed. In this case, we could use such a
    check and print the column meta-data or not. But I haven't yet fully learnt
    how to implement piping features within a C program with Argp.
    
    So until we are able to implement an internal check of piping, the Table
    program has a new option (`--colinfoinstdout'). When called (and the output
    is to be printed in the standard output), it will print the column
    meta-data to the standard output.
    
    This was suggested by Leindert Boogaard.
---
 NEWS                         |  7 +++++
 bin/convertt/convertt.c      |  2 +-
 bin/fits/fits.c              |  2 +-
 bin/match/match.c            |  6 ++--
 bin/mkcatalog/mkcatalog.c    |  4 +--
 bin/mkcatalog/upperlimit.c   |  2 +-
 bin/noisechisel/threshold.c  |  2 +-
 bin/segment/clumps.c         |  2 +-
 bin/segment/segment.c        |  2 +-
 bin/statistics/statistics.c  |  2 +-
 bin/statistics/ui.c          |  2 +-
 bin/table/args.h             | 20 ++++++++++++-
 bin/table/main.h             |  3 +-
 bin/table/table.c            |  3 +-
 bin/table/ui.c               |  6 ++++
 bin/table/ui.h               |  7 +++--
 configure.ac                 |  2 +-
 doc/announce-acknowledge.txt |  1 +
 doc/gnuastro.texi            | 68 +++++++++++++++++++++++++++++---------------
 lib/gnuastro/table.h         |  3 +-
 lib/gnuastro/txt.h           |  3 +-
 lib/table.c                  |  9 +++---
 lib/txt.c                    | 67 +++++++++++++++++++------------------------
 23 files changed, 139 insertions(+), 86 deletions(-)

diff --git a/NEWS b/NEWS
index 0158d35..e77364d 100644
--- a/NEWS
+++ b/NEWS
@@ -5,10 +5,17 @@ GNU Astronomy Utilities NEWS                          -*- 
outline -*-
 
 ** New features
 
+  Table:
+   - `--colinfoinstdout': column information when writing to standard output.
+
 ** Removed features
 
 ** Changed features
 
+  Library:
+    - gal_txt_write: new `colinfoinstdout' argument.
+    - gal_table_write: new `colinfoinstdout' argument.
+
 ** Bug fixes
 
   bug #54057: Building failure due to not finding gsl_interp_steffen.
diff --git a/bin/convertt/convertt.c b/bin/convertt/convertt.c
index 7ed7ebf..be79d20 100644
--- a/bin/convertt/convertt.c
+++ b/bin/convertt/convertt.c
@@ -336,7 +336,7 @@ convertt(struct converttparams *p)
     /* Plain text: only one channel is acceptable. */
     case OUT_FORMAT_TXT:
       gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
-      gal_txt_write(p->chll, NULL, p->cp.output);
+      gal_txt_write(p->chll, NULL, p->cp.output, 0);
       break;
 
     /* JPEG: */
diff --git a/bin/fits/fits.c b/bin/fits/fits.c
index 0381d72..320ec50 100644
--- a/bin/fits/fits.c
+++ b/bin/fits/fits.c
@@ -235,7 +235,7 @@ fits_print_extension_info(struct fitsparams *p)
       printf(" Column 4: Size of data in HDU.\n");
       printf("-----\n");
     }
-  gal_table_write(cols, NULL, GAL_TABLE_FORMAT_TXT, NULL, NULL);
+  gal_table_write(cols, NULL, GAL_TABLE_FORMAT_TXT, NULL, NULL, 0);
   gal_list_data_free(cols);
 }
 
diff --git a/bin/match/match.c b/bin/match/match.c
index bfbbf21..bd659c3 100644
--- a/bin/match/match.c
+++ b/bin/match/match.c
@@ -104,7 +104,7 @@ match_catalog_read_write_all(struct matchparams *p, size_t 
*permutation,
   else
     {
       /* Write the catalog to a file. */
-      gal_table_write(cat, NULL, p->cp.tableformat, outname, extname);
+      gal_table_write(cat, NULL, p->cp.tableformat, outname, extname, 0);
 
       /* Correct arrays and sizes (when `notmatched' was called). The
          `array' element has to be corrected for later freeing.
@@ -169,7 +169,7 @@ match_catalog_write_one(struct matchparams *p, gal_data_t 
*a, gal_data_t *b,
 
   /* Reverse the table and write it out. */
   gal_list_data_reverse(&cat);
-  gal_table_write(cat, NULL, p->cp.tableformat, p->cp.output, "MATCHED");
+  gal_table_write(cat, NULL, p->cp.tableformat, p->cp.output, "MATCHED", 0);
 }
 
 
@@ -255,7 +255,7 @@ match_catalog(struct matchparams *p)
 
           /* Write them into the table. */
           gal_table_write(mcols, NULL, p->cp.tableformat, p->logname,
-                          "LOG_INFO");
+                          "LOG_INFO", 0);
 
           /* Set the comment pointer to NULL: they weren't allocated. */
           mcols->comment=NULL;
diff --git a/bin/mkcatalog/mkcatalog.c b/bin/mkcatalog/mkcatalog.c
index afbc5e4..724c430 100644
--- a/bin/mkcatalog/mkcatalog.c
+++ b/bin/mkcatalog/mkcatalog.c
@@ -611,7 +611,7 @@ mkcatalog_write_outputs(struct mkcatalogparams *p)
      write the objects catalog and free the comments. */
   gal_list_str_reverse(&comments);
   gal_table_write(p->objectcols, comments, p->cp.tableformat, p->objectsout,
-                  "OBJECTS");
+                  "OBJECTS", 0);
   gal_list_str_free(comments, 1);
 
 
@@ -630,7 +630,7 @@ mkcatalog_write_outputs(struct mkcatalogparams *p)
          write the objects catalog and free the comments. */
       gal_list_str_reverse(&comments);
       gal_table_write(p->clumpcols, comments, p->cp.tableformat, p->clumpsout,
-                      "CLUMPS");
+                      "CLUMPS", 0);
       gal_list_str_free(comments, 1);
     }
 
diff --git a/bin/mkcatalog/upperlimit.c b/bin/mkcatalog/upperlimit.c
index 58a3bbe..406a66e 100644
--- a/bin/mkcatalog/upperlimit.c
+++ b/bin/mkcatalog/upperlimit.c
@@ -418,7 +418,7 @@ upperlimit_write_check(struct mkcatalogparams *p, 
gal_list_sizet_t *check_x,
   y->next=s;
   gal_list_str_reverse(&comments);
   gal_table_write(x, comments, p->cp.tableformat, p->upcheckout,
-                  "UPPERLIMIT_CHECK");
+                  "UPPERLIMIT_CHECK", 0);
 
   /* Inform the user. */
   if(!p->cp.quiet)
diff --git a/bin/noisechisel/threshold.c b/bin/noisechisel/threshold.c
index a3c9d46..9fb44c6 100644
--- a/bin/noisechisel/threshold.c
+++ b/bin/noisechisel/threshold.c
@@ -226,7 +226,7 @@ threshold_write_sn_table(struct noisechiselparams *p, 
gal_data_t *insn,
 
   /* write the table. */
   gal_checkset_writable_remove(filename, 0, 1);
-  gal_table_write(cols, comments, p->cp.tableformat, filename, "SN");
+  gal_table_write(cols, comments, p->cp.tableformat, filename, "SN", 0);
 
 
   /* Clean up (if necessary). */
diff --git a/bin/segment/clumps.c b/bin/segment/clumps.c
index 2c9a338..a2f7235 100644
--- a/bin/segment/clumps.c
+++ b/bin/segment/clumps.c
@@ -532,7 +532,7 @@ clumps_write_sn_table(struct segmentparams *p, gal_data_t 
*insn,
 
   /* write the table. */
   gal_checkset_writable_remove(filename, 0, 1);
-  gal_table_write(cols, comments, p->cp.tableformat, filename, "SN");
+  gal_table_write(cols, comments, p->cp.tableformat, filename, "SN", 0);
 
 
   /* Clean up (if necessary). */
diff --git a/bin/segment/segment.c b/bin/segment/segment.c
index a0dbd6a..189f7d0 100644
--- a/bin/segment/segment.c
+++ b/bin/segment/segment.c
@@ -797,7 +797,7 @@ segment_save_sn_table(struct clumps_params *clprm)
   objind->next=clumpinobj;
   gal_checkset_writable_remove(p->clumpsn_d_name, 0, 1);
   gal_table_write(objind, comments, p->cp.tableformat, p->clumpsn_d_name,
-                  "CLUMPS_SN");
+                  "CLUMPS_SN", 0);
 
 
   /* Clean up. */
diff --git a/bin/statistics/statistics.c b/bin/statistics/statistics.c
index 3ecdf48..5cd1c63 100644
--- a/bin/statistics/statistics.c
+++ b/bin/statistics/statistics.c
@@ -588,7 +588,7 @@ write_output_table(struct statisticsparams *p, gal_data_t 
*table,
 
   /* Write the table. */
   gal_checkset_writable_remove(output, 0, p->cp.dontdelete);
-  gal_table_write(table, comments, p->cp.tableformat, output, "TABLE");
+  gal_table_write(table, comments, p->cp.tableformat, output, "TABLE", 0);
 
 
   /* Let the user know, if we aren't in quiet mode. */
diff --git a/bin/statistics/ui.c b/bin/statistics/ui.c
index 3819c11..edf2e85 100644
--- a/bin/statistics/ui.c
+++ b/bin/statistics/ui.c
@@ -834,7 +834,7 @@ ui_preparations(struct statisticsparams *p)
               gal_checkset_writable_remove(tl->tilecheckname, 0,
                                            cp->dontdelete);
               gal_table_write(check, NULL, cp->tableformat, tl->tilecheckname,
-                              "TABLE");
+                              "TABLE", 0);
             }
           gal_data_free(check);
         }
diff --git a/bin/table/args.h b/bin/table/args.h
index f67bcb5..0123f24 100644
--- a/bin/table/args.h
+++ b/bin/table/args.h
@@ -49,21 +49,39 @@ struct argp_option program_options[] =
 
 
 
+    /* Output. */
     {
       "information",
       UI_KEY_INFORMATION,
       0,
       0,
       "Only print table and column information.",
-      GAL_OPTIONS_GROUP_OPERATING_MODE,
+      GAL_OPTIONS_GROUP_OUTPUT,
       &p->information,
       GAL_OPTIONS_NO_ARG_TYPE,
       GAL_OPTIONS_RANGE_0_OR_1,
       GAL_OPTIONS_NOT_MANDATORY,
       GAL_OPTIONS_NOT_SET
     },
+    {
+      "colinfoinstdout",
+      UI_KEY_COLINFOINSTDOUT,
+      0,
+      0,
+      "Column info/metadata when printing to stdout.",
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->colinfoinstdout,
+      GAL_OPTIONS_NO_ARG_TYPE,
+      GAL_OPTIONS_RANGE_0_OR_1,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
+    },
+
+
+
 
 
+    /* End. */
     {0}
   };
 
diff --git a/bin/table/main.h b/bin/table/main.h
index bbd8288..89b885e 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -46,7 +46,8 @@ struct tableparams
   struct gal_options_common_params cp; /* Common parameters.            */
   char              *filename;  /* Input filename.                      */
   gal_list_str_t     *columns;  /* List of given columns.               */
-  uint8_t         information;  /* ==1, only print FITS information.    */
+  uint8_t         information;  /* ==1: only print FITS information.    */
+  uint8_t     colinfoinstdout;  /* ==1: print column metadata in CL.    */
 
   /* Output: */
   gal_data_t           *table;  /* Linked list of output table columns. */
diff --git a/bin/table/table.c b/bin/table/table.c
index 2411467..b0afbf8 100644
--- a/bin/table/table.c
+++ b/bin/table/table.c
@@ -47,5 +47,6 @@ void
 table(struct tableparams *p)
 {
   gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
-  gal_table_write(p->table, NULL, p->cp.tableformat, p->cp.output, "TABLE");
+  gal_table_write(p->table, NULL, p->cp.tableformat, p->cp.output,
+                  "TABLE", p->colinfoinstdout);
 }
diff --git a/bin/table/ui.c b/bin/table/ui.c
index a401692..6a77393 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -120,11 +120,17 @@ ui_initialize_options(struct tableparams *p,
       /* Select individually. */
       switch(cp->coptions[i].key)
         {
+        /* Mandatory options. */
         case GAL_OPTIONS_KEY_SEARCHIN:
         case GAL_OPTIONS_KEY_MINMAPSIZE:
         case GAL_OPTIONS_KEY_TABLEFORMAT:
           cp->coptions[i].mandatory=GAL_OPTIONS_MANDATORY;
           break;
+
+        /* Options to ignore. */
+        case GAL_OPTIONS_KEY_TYPE:
+          cp->coptions[i].flags=OPTION_HIDDEN;
+          break;
         }
 
       /* Select by group. */
diff --git a/bin/table/ui.h b/bin/table/ui.h
index 1402ee3..c7482e5 100644
--- a/bin/table/ui.h
+++ b/bin/table/ui.h
@@ -32,14 +32,15 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 /* Available letters for short options:
 
-   a b d e f g j k l m n p r s t u v w x y z
+   a b d e f g j k l m n p r t u v w x y z
    A B C E G H J L O Q R W X Y
 */
 enum option_keys_enum
 {
   /* With short-option version. */
-  UI_KEY_COLUMN      = 'c',
-  UI_KEY_INFORMATION = 'i',
+  UI_KEY_COLUMN          = 'c',
+  UI_KEY_INFORMATION     = 'i',
+  UI_KEY_COLINFOINSTDOUT = 's',
 
   /* Only with long version (start with a value 1000, the rest will be set
      automatically). */
diff --git a/configure.ac b/configure.ac
index 10b854b..dcb470f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -50,7 +50,7 @@ AC_CONFIG_MACRO_DIRS([bootstrapped/m4])
 
 # Library version, see the GNU Libtool manual ("Library interface versions"
 # section for the exact definition of each) for
-GAL_CURRENT=4
+GAL_CURRENT=5
 GAL_REVISION=0
 GAL_AGE=0
 GAL_LT_VERSION="${GAL_CURRENT}:${GAL_REVISION}:${GAL_AGE}"
diff --git a/doc/announce-acknowledge.txt b/doc/announce-acknowledge.txt
index 2382d74..6364f5a 100644
--- a/doc/announce-acknowledge.txt
+++ b/doc/announce-acknowledge.txt
@@ -1,4 +1,5 @@
 Alphabetically ordered list to acknowledge in the next release.
 
+Leindert Boogaard
 Takashi Ichikawa
 Alan Lefor
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 3f15b00..524b92d 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -23821,21 +23821,34 @@ this line is ignored.
 @end itemize
 @end deftypefun
 
address@hidden void gal_table_write (gal_data_t @code{*cols}, gal_list_str_t 
@code{*comments}, int @code{tableformat}, char @code{*filename}, char 
@code{*extname})
-Write the @code{cols} list of datasets into a table in @code{filename} (see
address@hidden of gal_data_t}). The format of the table can be determined with
address@hidden that accepts the macros defined above. If
address@hidden is not @code{NULL}, then the list of comments will also be
-printed into the output table. When the output table is a plain text file,
-each node's string will be printed after a @code{#} (so it can be
-considered as a comment) and in FITS table they will follow a
address@hidden keyword. If @file{filename} is a FITS file, the table
-extension that will be written will have the name @code{extname}.
-
-If a file named @file{filename} already exists, the operation depends on
-the type of output. When @file{filename} is a FITS file, the table will be
-added as a new extension after all existing ones. If @file{filename} is a
-plain text file, this function will abort with an error.
address@hidden void gal_table_write (gal_data_t @code{*cols}, gal_list_str_t 
@code{*comments}, int @code{tableformat}, char @code{*filename}, char 
@code{*extname}, uint8_t @code{colinfoinstdout})
+Write @code{cols} (a list of datasets, see @ref{List of gal_data_t}) into a
+table stored in @code{filename}. The format of the table can be determined
+with @code{tableformat} that accepts the macros defined above. When
address@hidden, the column information will be printed on the
+standard output (command-line).
+
+If @code{comments!=NULL}, the list of comments (see @ref{List of strings})
+will also be printed into the output table. When the output table is a
+plain text file, every node of @code{comments} will be printed after a
address@hidden (so it can be considered as a comment) and in FITS table they 
will
+follow a @code{COMMENT} keyword.
+
+If a file named @code{filename} already exists, the operation depends on
+the type of output. When @code{filename} is a FITS file, the table will be
+added as a new extension after all existing extensions. If @code{filename}
+is a plain text file, this function will abort with an error.
+
+If @code{filename} is a FITS file, the table extension will have the name
address@hidden
+
+When @code{colinfoinstdout!=0} and @code{filename==NULL} (columns are
+printed in the standard output), the dataset metadata will also printed in
+the standard output. When printing to the standard output, the column
+information can be piped into another program for futher processing and
+thus the meta-data (lines starting with a @code{#}) must be ignored. In
+such cases, you only print the column values by passing @code{0} to
address@hidden
 @end deftypefun
 
 @deftypefun void gal_table_write_log (gal_data_t @code{*logll}, char 
@code{*program_string}, time_t @code{*rawtime}, gal_list_str_t 
@code{*comments}, char @code{*filename}, int @code{quiet})
@@ -24522,7 +24535,7 @@ the dataset. If the necessary space for the image is 
larger than
 see the description under the same name in @ref{Generic data container}.
 @end deftypefun
 
address@hidden void gal_txt_write (gal_data_t @code{*cols}, gal_list_str_t 
@code{*comment}, char @code{*filename})
address@hidden void gal_txt_write (gal_data_t @code{*cols}, gal_list_str_t 
@code{*comment}, char @code{*filename}, uint8_t @code{colinfoinstdout})
 Write @code{cols} in a plain text file @code{filename}. @code{cols} may
 have one or two dimensions which determines the output:
 
@@ -24539,16 +24552,25 @@ table.
 be written.
 @end table
 
-If @code{filename} already exists this function will abort with an error
-and will not write over the existing file. Please make sure if the file
-exists or not and take the appropriate action before calling this
-function. If @code{comments!=NULL}, a @code{#} will be put at the start of
-each node of the list of strings and will be written in the file before the
-column meta-data in @code{filename} (see @ref{List of strings}).
-
 This is a low-level function for tables. It is recommended to use
 @code{gal_table_write} for generic writing of tables in a variety of
 formats, see @ref{Table input output}.
+
+If @code{filename} already exists this function will abort with an error
+and will not write over the existing file. Before calling this function
+make sure if the file exists or not. If @code{comments!=NULL}, a @code{#}
+will be put at the start of each node of the list of strings and will be
+written in the file before the column meta-data in @code{filename} (see
address@hidden of strings}).
+
+When @code{filename==NULL}, the column information will be printed on the
+standard output (command-line). When @code{colinfoinstdout!=0} and
address@hidden (columns are printed in the standard output), the
+dataset metadata will also printed in the standard output. When printing to
+the standard output, the column information can be piped into another
+program for futher processing and thus the meta-data (lines starting with a
address@hidden) must be ignored. In such cases, you only print the column values
+by passing @code{0} to @code{colinfoinstdout}.
 @end deftypefun
 
 
diff --git a/lib/gnuastro/table.h b/lib/gnuastro/table.h
index 456380e..b92a340 100644
--- a/lib/gnuastro/table.h
+++ b/lib/gnuastro/table.h
@@ -152,7 +152,8 @@ gal_table_comments_add_intro(gal_list_str_t **comments,
 
 void
 gal_table_write(gal_data_t *cols, gal_list_str_t *comments,
-                int tableformat, char *filename, char *extname);
+                int tableformat, char *filename, char *extname,
+                uint8_t colinfoinstdout);
 
 void
 gal_table_write_log(gal_data_t *logll, char *program_string,
diff --git a/lib/gnuastro/txt.h b/lib/gnuastro/txt.h
index d6ec0be..e26e7f5 100644
--- a/lib/gnuastro/txt.h
+++ b/lib/gnuastro/txt.h
@@ -89,7 +89,8 @@ gal_data_t *
 gal_txt_image_read(char *filename, size_t minmapsize);
 
 void
-gal_txt_write(gal_data_t *input, gal_list_str_t *comment, char *filename);
+gal_txt_write(gal_data_t *input, gal_list_str_t *comment, char *filename,
+              uint8_t colinfoinstdout);
 
 
 
diff --git a/lib/table.c b/lib/table.c
index ec174e9..84bfdd5 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -521,7 +521,8 @@ gal_table_comments_add_intro(gal_list_str_t **comments, 
char *program_string,
    specified by `tableformat'. */
 void
 gal_table_write(gal_data_t *cols, gal_list_str_t *comments,
-                int tableformat, char *filename, char *extname)
+                int tableformat, char *filename, char *extname,
+                uint8_t colinfoinstdout)
 {
   /* If a filename was given, then the tableformat is relevant and must be
      used. When the filename is empty, a text table must be printed on the
@@ -531,11 +532,11 @@ gal_table_write(gal_data_t *cols, gal_list_str_t 
*comments,
       if(gal_fits_name_is_fits(filename))
         gal_fits_tab_write(cols, comments, tableformat, filename, extname);
       else
-        gal_txt_write(cols, comments, filename);
+        gal_txt_write(cols, comments, filename, colinfoinstdout);
     }
   else
     /* Write to standard output. */
-    gal_txt_write(cols, comments, filename);
+    gal_txt_write(cols, comments, filename, colinfoinstdout);
 }
 
 
@@ -553,7 +554,7 @@ gal_table_write_log(gal_data_t *logll, char *program_string,
   gal_table_comments_add_intro(&comments, program_string, rawtime);
 
   /* Write the log file to disk */
-  gal_table_write(logll, comments, GAL_TABLE_FORMAT_TXT, filename, "LOG");
+  gal_table_write(logll, comments, GAL_TABLE_FORMAT_TXT, filename, "LOG", 0);
 
   /* In verbose mode, print the information. */
   if(!quiet)
diff --git a/lib/txt.c b/lib/txt.c
index 097be54..a8655e0 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -1141,36 +1141,14 @@ txt_print_value(FILE *fp, void *array, int type, size_t 
ind, char *fmt)
 
 
 
-static FILE *
-txt_open_file_write_info(gal_data_t *datall, char **fmts,
-                         gal_list_str_t *comment, char *filename)
+static void
+txt_write_metadata(FILE *fp, gal_data_t *datall, char **fmts)
 {
-  FILE *fp;
   gal_data_t *data;
   char *tmp, *nstr;
   size_t i, j, num=0;
-  gal_list_str_t *strt;
   int nlen, nw=0, uw=0, tw=0, bw=0;
 
-  /* Make sure the file doesn't already eixist. */
-  if( gal_checkset_check_file_return(filename) )
-    error(EXIT_FAILURE, 0, "%s: %s already exists. For safety, this "
-          "function will not over-write an existing file. Please delete "
-          "it before calling this function", __func__, filename);
-
-  /* Open the output file. */
-  errno=0;
-  fp=fopen(filename, "w");
-  if(fp==NULL)
-    error(EXIT_FAILURE, errno, "%s: couldn't be open to write text "
-          "table by %s", filename, __func__);
-
-
-  /* Write the comments if there were any. */
-  for(strt=comment; strt!=NULL; strt=strt->next)
-    fprintf(fp, "# %s\n", strt->v);
-
-
   /* Get the maximum width for each information field. */
   for(data=datall;data!=NULL;data=data->next)
     {
@@ -1187,14 +1165,7 @@ txt_open_file_write_info(gal_data_t *datall, char **fmts,
     }
 
 
-  /* Write the column information if the output is a file. When the
-     output is directed to standard output (the command-line), it is
-     most probably intended for piping into another program (for
-     example AWK for further processing, or sort, or anything) so the
-     user already has the column information and is probably going to
-     change them, so they are just a nuisance.
-
-     When there are more than 9 columns, we don't want to have cases
+  /* When there are more than 9 columns, we don't want to have cases
      like `# Column 1 :' (note the space between `1' and `:', this
      space won't exist for the 2 digit colum numbers).
 
@@ -1231,7 +1202,6 @@ txt_open_file_write_info(gal_data_t *datall, char **fmts,
 
   /* Clean up and return. */
   free(nstr);
-  return fp;
 }
 
 
@@ -1239,10 +1209,12 @@ txt_open_file_write_info(gal_data_t *datall, char 
**fmts,
 
 
 void
-gal_txt_write(gal_data_t *input, gal_list_str_t *comment, char *filename)
+gal_txt_write(gal_data_t *input, gal_list_str_t *comment, char *filename,
+              uint8_t colinfoinstdout)
 {
   FILE *fp;
   char **fmts;
+  gal_list_str_t *strt;
   size_t i, j, num=0, fmtlen;
   gal_data_t *data, *next2d=NULL;
 
@@ -1290,10 +1262,31 @@ gal_txt_write(gal_data_t *input, gal_list_str_t 
*comment, char *filename)
 
   /* Set the output FILE pointer: if it isn't NULL, its an actual file,
      otherwise, its the standard output. */
-  fp = ( filename
-         ? txt_open_file_write_info(input, fmts, comment, filename)
-         : stdout );
+  if(filename)
+    {
+      /* Make sure the file doesn't already exist. */
+      if( gal_checkset_check_file_return(filename) )
+        error(EXIT_FAILURE, 0, "%s: %s already exists. For safety, this "
+              "function will not over-write an existing file. Please delete "
+              "it before calling this function", __func__, filename);
+
+      /* Open the output file. */
+      errno=0;
+      fp=fopen(filename, "w");
+      if(fp==NULL)
+        error(EXIT_FAILURE, errno, "%s: couldn't be open to write text "
+              "table by %s", filename, __func__);
+
+      /* Write the comments if there were any. */
+      for(strt=comment; strt!=NULL; strt=strt->next)
+        fprintf(fp, "# %s\n", strt->v);
+    }
+  else
+    fp=stdout;
 
+  /* Write the meta-data if necessary. */
+  if(filename ? 1 : colinfoinstdout)
+    txt_write_metadata(fp, input, fmts);
 
   /* Print the dataset */
   switch(input->ndim)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]