gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 9ec7556 039/125: Table's output file type set


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 9ec7556 039/125: Table's output file type set common sense
Date: Sun, 23 Apr 2017 22:36:32 -0400 (EDT)

branch: master
commit 9ec7556f3a4a807e72851db28d83cd1b2540a7c0
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Table's output file type set common sense
    
    Previously, when the (old) `--outtabletype' option was given, the output
    was forced to be a file (automatic output would be triggered when no output
    filename was given). But this is not too natural, because you might have a
    special FITS type as a preference, and thus put it in your configuration
    file. But then, you could never pipe your outputs to other programs (show
    the table on stdout).
    
    So now, we have a `--tabletype' option, with the same acceptable values as
    before, but now it won't be used to set a filename. In short, Table won't
    be using automatic output: when no output type is given, the table will be
    printed in stdout. The value to `--tabletype' is only relevant for table
    formats that can't directly be determined from the filename.
    
    This option will later be moved as a common option, for use by all the
    programs that print tables as output. As a preparation, the function that
    determines the recognized internal macro for the table type from a
    string. The same will happen for the `searchin' parameter, so the old
    `gal_table_searchin_from_str' was also renamed to
    `gal_table_string_to_searchin' to fit the same naming style as the new
    `gal_table_string_to_type'.
    
    Also, `strtol' saves values as zero when they couldn't be read. This would
    cause problems when setting widths. So now, in plain text printing, when
    the width is smaller and equal to zero, the default widths will be evoked,
    not when it is just smaller than zero.
---
 bin/table/args.h        |   8 ++--
 bin/table/asttable.conf |   1 +
 bin/table/main.h        |   6 +--
 bin/table/table.c       |   2 +-
 bin/table/ui.c          |  89 +++++++++--------------------------
 doc/gnuastro.texi       |  32 +++++++++----
 lib/gnuastro/table.h    |   8 ++--
 lib/gnuastro/txt.h      |   4 +-
 lib/table.c             | 123 +++++++++++++++++++++++++++++++++---------------
 lib/txt.c               |  22 ++++-----
 10 files changed, 154 insertions(+), 141 deletions(-)

diff --git a/bin/table/args.h b/bin/table/args.h
index 3030a04..79bce89 100644
--- a/bin/table/args.h
+++ b/bin/table/args.h
@@ -126,11 +126,11 @@ static struct argp_option options[] =
       2
     },
     {
-      "outtabletype",
+      "tabletype",
       't',
       "STR",
       0,
-      "Output type: `txt', `fits-ascii', `fits-binary'.",
+      "Output table type: `fits-ascii', `fits-binary'.",
       2
     },
 
@@ -206,8 +206,8 @@ parse_opt(int key, char *arg, struct argp_state *state)
 
     /* Output: */
     case 't':
-      gal_checkset_allocate_copy(arg, &p->up.outtabletype);
-      p->up.outtabletypeset=1;
+      gal_checkset_allocate_copy(arg, &p->up.tabletype);
+      p->up.tabletypeset=1;
       break;
 
 
diff --git a/bin/table/asttable.conf b/bin/table/asttable.conf
index cd4fa1f..16d4116 100644
--- a/bin/table/asttable.conf
+++ b/bin/table/asttable.conf
@@ -23,3 +23,4 @@
  ignorecase       0
 
 # Output:
+ tabletype        fits-binary
\ No newline at end of file
diff --git a/bin/table/main.h b/bin/table/main.h
index 9939d62..4866551 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -40,14 +40,14 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 struct uiparams
 {
   char              *filename;
-  char          *outtabletype;
+  char             *tabletype;
   gal_data_t      *allcolinfo;
   char              *searchin;
 
   /* If values are set: */
   int          informationset;
   int           ignorecaseset;
-  int         outtabletypeset;
+  int            tabletypeset;
   int             searchinset;
 };
 
@@ -66,7 +66,7 @@ struct tableparams
   struct gal_linkedlist_stll *columns; /* List of given columns.        */
 
   /* Output: */
-  int            outtabletype;  /* Type of output table (FITS, txt).    */
+  int               tabletype;  /* Type of output table (FITS, txt).    */
   gal_data_t           *table;  /* Linked list of output table columns. */
 
   /* Operating modes */
diff --git a/bin/table/table.c b/bin/table/table.c
index 222e8d5..bae4f1e 100644
--- a/bin/table/table.c
+++ b/bin/table/table.c
@@ -44,6 +44,6 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 void
 table(struct tableparams *p)
 {
-  gal_table_write(p->table, NULL, p->outtabletype, p->cp.output,
+  gal_table_write(p->table, NULL, p->tabletype, p->cp.output,
                   p->cp.dontdelete);
 }
diff --git a/bin/table/ui.c b/bin/table/ui.c
index bdde217..3357c56 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -133,11 +133,11 @@ readconfig(char *filename, struct tableparams *p)
       else if(strcmp(name, "output")==0)
         gal_checkset_allocate_copy_set(value, &cp->output, &cp->outputset);
 
-      else if (strcmp(name, "outtabletype")==0)
+      else if (strcmp(name, "tabletype")==0)
         {
-          if(p->up.outtabletypeset) continue;
-          gal_checkset_allocate_copy_set(value, &p->up.outtabletype,
-                                         &p->up.outtabletypeset);
+          if(p->up.tabletypeset) continue;
+          gal_checkset_allocate_copy_set(value, &p->up.tabletype,
+                                         &p->up.tabletypeset);
         }
 
 
@@ -192,8 +192,8 @@ printvalues(FILE *fp, struct tableparams *p)
 
 
   fprintf(fp, "\n# Output:\n");
-  if(up->outtabletypeset)
-    fprintf(fp, CONF_SHOWFMT"%s\n", "outtabletype", p->up.outtabletype);
+  if(up->tabletypeset)
+    fprintf(fp, CONF_SHOWFMT"%s\n", "tabletype", p->up.tabletype);
 
 
   /* For the operating mode, first put the macro to print the common
@@ -227,6 +227,12 @@ checkifset(struct tableparams *p)
   if(up->searchinset==0)
     GAL_CONFIGFILES_REPORT_NOTSET("searchin");
 
+  /* If the output is a FITS file, but tabletype is not set, then report
+     this option as missing. */
+  if( p->cp.output && gal_fits_name_is_fits(p->cp.output)
+      && up->tabletypeset==0 )
+    GAL_CONFIGFILES_REPORT_NOTSET("tabletype");
+
   GAL_CONFIGFILES_END_OF_NOTSET_REPORT;
 }
 
@@ -255,80 +261,27 @@ checkifset(struct tableparams *p)
 void
 sanitycheck(struct tableparams *p)
 {
-  char *suffix=NULL;
   struct uiparams *up=&p->up;
 
-
   /* Set the searchin integer value. */
-  p->searchin=gal_table_searchin_from_str(p->up.searchin);
+  p->searchin=gal_table_string_to_searchin(p->up.searchin);
 
 
   /* If the outtabletype option was given, then convert it to an easiy
      usable integer. Note we cannot do this in the output filename check
      below, since it is also necessary when there is an output file.*/
-  if(up->outtabletypeset)
-    {
-      if( !strcmp(up->outtabletype, "txt") )
-        p->outtabletype=GAL_TABLE_TYPE_TXT;
-      else if( !strcmp(up->outtabletype, "fits-ascii") )
-        p->outtabletype=GAL_TABLE_TYPE_AFITS;
-      else if( !strcmp(up->outtabletype, "fits-binary") )
-        p->outtabletype=GAL_TABLE_TYPE_BFITS;
-      else
-        error(EXIT_FAILURE, 0, "the value to the `--outtabletype' "
-              "option on the command line or `fitstabletype' variable in "
-              "any of the configuration files must be either `txt', "
-              "`fits-ascii' or `fits-binary'. You have given `%s'",
-              up->outtabletype);
-    }
+  if(up->tabletypeset)
+    p->tabletype=gal_table_string_to_type(up->tabletype);
 
 
-  /* Set the output name if it wasn't given. */
-  if(p->cp.output==NULL)
-    {
-      /* Only set the output filename automatically if the output type is
-         given. A `NULL' output filename will indicate that the table
-         should be printed in STDOUT.*/
-      if(up->outtabletypeset)
-        {
-          /* Set the filename based on the type of table desired. For the
-             time being, there is only txt and FITS table formats, but we
-             might add other formats in the future, so the structure below
-             is defined to account for those future types.
-
-             Note that `p->outtabletype' is set above internally, so we
-             don't need a `default' case here.*/
-          switch(p->outtabletype)
-            {
-            case GAL_TABLE_TYPE_TXT:
-              suffix="_table.txt";
-              break;
-
-            case GAL_TABLE_TYPE_AFITS:
-            case GAL_TABLE_TYPE_BFITS:
-              suffix="_table.fits";
-              break;
-            }
-
-          /* Set the output name */
-          if(suffix)
-            gal_checkset_automatic_output(p->up.filename, suffix,
-                                          p->cp.removedirinfo,
-                                          p->cp.dontdelete, &p->cp.output);
-        }
-    }
   /* If the output name was set and is a FITS file, make sure that the type
      of the table is not a `txt'. */
-  else
-    {
-      if( gal_fits_name_is_fits(p->cp.output)
-          && ( p->outtabletype !=GAL_TABLE_TYPE_AFITS
-               && p->outtabletype !=GAL_TABLE_TYPE_BFITS ) )
-        error(EXIT_FAILURE, 0, "desired output table is a FITS file, but "
-              "`outtabletype' is not %s. Please set it to "
-              "`fits-ascii', or `fits-binary'",
-              up->outtabletypeset ? "a FITS table type" : "set");
-    }
+  if( p->cp.output && gal_fits_name_is_fits(p->cp.output)
+      && ( p->tabletype !=GAL_TABLE_TYPE_AFITS
+           && p->tabletype !=GAL_TABLE_TYPE_BFITS ) )
+        error(EXIT_FAILURE, 0, "desired output file `%s' is a FITS file, "
+              "but `tabletype' is set to `%s'. Please set it to "
+              "`fits-ascii', or `fits-binary'", p->cp.output, up->tabletype);
 }
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index e274fd5..32f4e13 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -6432,16 +6432,28 @@ single precision floating point numbers.
 (@option{=INT}) The number of digits to print after the floating point for
 double precision floating point numbers.
 
address@hidden --outtabletype
-(@option{=STR}) The output table type (for example plain text, FITS ascii,
-or FITS binary). If this option is not called on the command-line or any of
-the configuration files and no output filename is given with the
address@hidden option (see @ref{Common options}), then the columns that
-are read will be printed in the standard output (on the command-line).
-However, if it is called, it can only have one of these values:
address@hidden, @option{fits-ascii}, @option{fits-binary} for the current
-types of recognized output table types. If the output is a FITS file, this
-option is necessary because there are two types of FITS tables.
address@hidden -t
address@hidden --tabletype
+(@option{=STR}) The output file's table type when a filename is given to
+the @option{--output} option (see @ref{Common options}). This is ofcourse
+only relevant when the type of the table cannot be deduced from its
+filename. For example, if a name ending in @file{.fits} was given to
address@hidden, then Gnuastro knows you want a FITS table. But there are
+two types of FITS tables: FITS ASCII, and FITS binary. Thus, with this
+option, the program is able to identify which type you want. The currently
+recognized values to this option are:
+
address@hidden @command
address@hidden txt
+A plain text table with space characters between the columns. Setting
address@hidden to this value is acceptable, but irrelevant. Because a
+plain text table only currently has one format and is the default when the
+output filename wasn't recognized.
address@hidden fits-ascii
+A FITS ASCII table.
address@hidden fits-binary
+A FITS binary table.
address@hidden table
 
 
 @end table
diff --git a/lib/gnuastro/table.h b/lib/gnuastro/table.h
index 069481e..f374d83 100644
--- a/lib/gnuastro/table.h
+++ b/lib/gnuastro/table.h
@@ -117,14 +117,16 @@ enum gal_table_diplay_formats
 
 
 /* Functions */
+int
+gal_table_string_to_type(char *string);
+
+int
+gal_table_string_to_searchin(char *string);
 
 gal_data_t *
 gal_table_info(char *filename, char *hdu, size_t *numcols, size_t *numrows,
                  int *tabletype);
 
-int
-gal_table_searchin_from_str(char *searchin_str);
-
 gal_data_t *
 gal_table_read(char *filename, char *hdu, struct gal_linkedlist_stll *cols,
                int searchin, int ignorecase, int minmapsize);
diff --git a/lib/gnuastro/txt.h b/lib/gnuastro/txt.h
index a1f791f..e387201 100644
--- a/lib/gnuastro/txt.h
+++ b/lib/gnuastro/txt.h
@@ -74,8 +74,8 @@ gal_txt_table_read(char *filename, size_t numrows, gal_data_t 
*colinfo,
                    struct gal_linkedlist_sll *indexll, int minmapsize);
 
 void
-gal_txt_write(gal_data_t *cols, char *comment, char *filename,
-              int dontdelete);
+gal_txt_table_write(gal_data_t *cols, char *comment, char *filename,
+                    int dontdelete);
 
 
 
diff --git a/lib/table.c b/lib/table.c
index 9d3e38c..d12cdaa 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -40,6 +40,84 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 
 
+/************************************************************************/
+/***************               Table types                ***************/
+/************************************************************************/
+/* Return the type of desired table based on a standard string. */
+int
+gal_table_string_to_type(char *string)
+{
+  if(string)
+    {
+      if( !strcmp(string, "txt") )
+        return GAL_TABLE_TYPE_TXT;
+
+      else if( !strcmp(string, "fits-ascii") )
+        return GAL_TABLE_TYPE_AFITS;
+
+      else if( !strcmp(string, "fits-binary") )
+        return GAL_TABLE_TYPE_BFITS;
+
+      else
+        error(EXIT_FAILURE, 0, "`%s' couldn't be interpretted as a valid "
+              "table type. The known types are `txt', `fits-ascii', and "
+              "`fits-binary'", string);
+    }
+
+  /* If string was not NULL, this function won't reach here. */
+  return -1;
+}
+
+
+
+
+
+/* In programs, the `searchin' variable is much more easier to type in as a
+   description than an integer (which is what `gal_table_read_cols'
+   needs). This function will check the string value and give the
+   corresponding integer value.*/
+int
+gal_table_string_to_searchin(char *string)
+{
+  if(strcmp(string, "name")==0)
+    return GAL_TABLE_SEARCH_NAME;
+
+  else if(strcmp(string, "unit")==0)
+    return GAL_TABLE_SEARCH_UNIT;
+
+  else if(strcmp(string, "comment")==0)
+    return GAL_TABLE_SEARCH_COMMENT;
+
+  else
+    error(EXIT_FAILURE, 0, "`--searchin' only recognizes the values "
+          "`name', `unit', and `comment', you have asked for `%s'",
+          string);
+
+  /* Report an error control reaches here. */
+  error(EXIT_FAILURE, 0, "A bug! please contact us at %s so we can address "
+        "the problem. For some reason control has reached the end of "
+        "`gal_table_searchin_from_str'", PACKAGE_BUGREPORT);
+  return -1;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
 /************************************************************************/
 /***************         Information about a table        ***************/
@@ -96,34 +174,6 @@ gal_table_info(char *filename, char *hdu, size_t *numcols, 
size_t *numrows,
 /************************************************************************/
 /***************               Read a table               ***************/
 /************************************************************************/
-/* In programs, the `searchin' variable is much more easier to type in as a
-   description than an integer (which is what `gal_table_read_cols'
-   needs). This function will check the string value and give the
-   corresponding integer value.*/
-int
-gal_table_searchin_from_str(char *searchin_str)
-{
-  if(strcmp(searchin_str, "name")==0)
-    return GAL_TABLE_SEARCH_NAME;
-  else if(strcmp(searchin_str, "unit")==0)
-    return GAL_TABLE_SEARCH_UNIT;
-  else if(strcmp(searchin_str, "comment")==0)
-    return GAL_TABLE_SEARCH_COMMENT;
-  else
-    error(EXIT_FAILURE, 0, "`--searchin' only recognizes the values "
-          "`name', `unit', and `comment', you have asked for `%s'",
-          searchin_str);
-
-  /* Report an error control reaches here. */
-  error(EXIT_FAILURE, 0, "A bug! please contact us at %s so we can address "
-        "the problem. For some reason control has reached the end of "
-        "`gal_table_searchin_from_str'", PACKAGE_BUGREPORT);
-  return -1;
-}
-
-
-
-
 
 /* Function to print regular expression error. This is taken from the GNU C
    library manual, with small modifications to fit out style, */
@@ -456,17 +506,12 @@ gal_table_write(gal_data_t *cols, char *comments, int 
tabletype,
      used. When the filename is empty, a text table must be printed on the
      standard output (on the command-line). */
   if(filename)
-    switch(tabletype)
-      {
-      case GAL_TABLE_TYPE_TXT:
-        gal_txt_write(cols, comments, filename, dontdelete);
-        break;
-
-      case GAL_TABLE_TYPE_AFITS:
-      case GAL_TABLE_TYPE_BFITS:
+    {
+      if(gal_fits_name_is_fits(filename))
         gal_fits_table_write(cols, comments, tabletype, filename, dontdelete);
-        break;
-      }
+      else
+        gal_txt_table_write(cols, comments, filename, dontdelete);
+    }
   else
-    gal_txt_write(cols, comments, filename, dontdelete);
+    gal_txt_table_write(cols, comments, filename, dontdelete);
 }
diff --git a/lib/txt.c b/lib/txt.c
index 4e9ac1f..c29dfbf 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -914,12 +914,12 @@ make_fmts_for_printf(gal_data_t *cols, size_t numcols, 
int leftadjust,
           if(col->type==GAL_DATA_TYPE_ULONG)
             {
               lng="l";
-              width=( col->disp_width<0 ? GAL_TABLE_DEF_LINT_WIDTH
+              width=( col->disp_width<=0 ? GAL_TABLE_DEF_LINT_WIDTH
                       : col->disp_width );
             }
-          else width=( col->disp_width<0 ? GAL_TABLE_DEF_INT_WIDTH
+          else width=( col->disp_width<=0 ? GAL_TABLE_DEF_INT_WIDTH
                        : col->disp_width );
-          precision=( col->disp_precision<0 ? GAL_TABLE_DEF_INT_PRECISION
+          precision=( col->disp_precision<=0 ? GAL_TABLE_DEF_INT_PRECISION
                       : col->disp_precision );
           break;
 
@@ -930,9 +930,9 @@ make_fmts_for_printf(gal_data_t *cols, size_t numcols, int 
leftadjust,
         case GAL_DATA_TYPE_SHORT:
         case GAL_DATA_TYPE_INT:
           fmt="d";
-          width=( col->disp_width<0 ? GAL_TABLE_DEF_INT_WIDTH
+          width=( col->disp_width<=0 ? GAL_TABLE_DEF_INT_WIDTH
                   : col->disp_width );
-          precision=( col->disp_precision<0 ? GAL_TABLE_DEF_INT_PRECISION
+          precision=( col->disp_precision<=0 ? GAL_TABLE_DEF_INT_PRECISION
                       : col->disp_precision );
           break;
 
@@ -942,9 +942,9 @@ make_fmts_for_printf(gal_data_t *cols, size_t numcols, int 
leftadjust,
         case GAL_DATA_TYPE_LONGLONG:
           fmt="d";
           lng = col->type==GAL_DATA_TYPE_LONG ? "l" : "ll";
-          width=( col->disp_width<0 ? GAL_TABLE_DEF_LINT_WIDTH
+          width=( col->disp_width<=0 ? GAL_TABLE_DEF_LINT_WIDTH
                   : col->disp_width );
-          precision=( col->disp_precision<0 ? GAL_TABLE_DEF_INT_PRECISION
+          precision=( col->disp_precision<=0 ? GAL_TABLE_DEF_INT_PRECISION
                       : col->disp_precision );
           break;
 
@@ -964,7 +964,7 @@ make_fmts_for_printf(gal_data_t *cols, size_t numcols, int 
leftadjust,
                       ? GAL_TABLE_DEF_FLT_WIDTH
                       : GAL_TABLE_DEF_DBL_WIDTH )
                   : col->disp_width );
-          precision=( col->disp_precision<0 ? GAL_TABLE_DEF_FLT_PRECISION
+          precision=( col->disp_precision<=0 ? GAL_TABLE_DEF_FLT_PRECISION
                       : col->disp_precision );
           break;
 
@@ -1042,7 +1042,7 @@ make_fmts_for_printf(gal_data_t *cols, size_t numcols, 
int leftadjust,
          the final length of the overall format statement. The space in the
          end of `fmts[i*2]' is to ensure that the columns don't merge, even
          if the printed string is larger than the expected width. */
-      if(precision<0)
+      if(precision<=0)
         *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%%s%d.%d%s%s ",
                             leftadjust ? "-" : "", width, precision,
                             lng, fmt);
@@ -1078,8 +1078,8 @@ make_fmts_for_printf(gal_data_t *cols, size_t numcols, 
int leftadjust,
 
 
 void
-gal_txt_write(gal_data_t *cols, char *comment, char *filename,
-              int dontdelete)
+gal_txt_table_write(gal_data_t *cols, char *comment, char *filename,
+                    int dontdelete)
 {
   FILE *fp;
   gal_data_t *col;



reply via email to

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