gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 0f19b1bc 1/2: Table: when printing column info


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0f19b1bc 1/2: Table: when printing column info, also shows vector columns
Date: Sat, 31 Dec 2022 22:31:14 -0500 (EST)

branch: master
commit 0f19b1bcdc5ef57002df7b0e71c9bcaca4a53ea5
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Table: when printing column info, also shows vector columns
    
    Until now, Table (actually the 'gal_table_print_info' function in the
    library) would not show if the given column is a vector (multi-value) or
    not.
    
    With this commit, when the column is a vector, a '[N]' is printed after the
    column's type. The 'N' shows how many values there are in that vector.
---
 NEWS              |  3 +++
 doc/gnuastro.texi | 38 +++++++++++++++++---------------------
 lib/table.c       | 40 +++++++++++++++++++++++++++++++++-------
 3 files changed, 53 insertions(+), 28 deletions(-)

diff --git a/NEWS b/NEWS
index 52dc2fcc..467bfba6 100644
--- a/NEWS
+++ b/NEWS
@@ -90,6 +90,9 @@ See the end of the file for license conditions.
    --outliernumngb: see description of same option in NoiseChisel.
 
    Table:
+   --information: also identifies vector columns (a column with more than
+     one value), by placing a '[N]' after the type. Where 'N' is the number
+     of elements within the vector.
    --txteasy: (or '-Y') when output is a plain-text file or just gets
      printed on standard output (terminal), all floating point columns are
      printed in fixed point notation (as in '123.456') instead of the
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index e95c2297..8ee376a8 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -14729,6 +14729,8 @@ If you want to preserve the sexagesimal contents of a 
column, you should store t
 @itemx --information
 Only print the column information in the specified table on the command-line 
and exit.
 Each column's information (number, name, units, data type, and comments) will 
be printed as a row on the command-line.
+If the column is a multi-value (vector) a @code{[N]} is printed after the 
type, where @code{N} is the number of elements within that vector.
+
 Note that the FITS standard only requires the data type (see @ref{Numeric data 
types}), and in plain text tables, no meta-data/information is mandatory.
 Gnuastro has its own convention in the comments of a plain text table to store 
and transfer this information as described in @ref{Gnuastro text table format}.
 
@@ -33453,30 +33455,24 @@ Convert the input identifier (one of the 
@code{GAL_TABLE_DISPLAY_FMT_FIXED}; for
 @end deftypefun
 
 @deftypefun {gal_data_t *} gal_table_info (char @code{*filename}, char 
@code{*hdu}, gal_list_str_t @code{*lines}, size_t @code{*numcols}, size_t 
@code{*numrows}, int @code{*tableformat})
-Store the information of each column of a table into an array of data
-structures with @code{numcols} datasets (one data structure for each
-column). The number of rows is stored in @code{numrows}. The format of the
-table (e.g., ASCII text file, or FITS binary or ASCII table) will be put in
-@code{tableformat} (macros defined above). If the @code{filename} is not a
-FITS file, then @code{hdu} will not be used (can be @code{NULL}).
-
-The input must be either a file (specified by @code{filename}) or a list of
-strings (@code{lines}). @code{lines} is a list of strings with each node
-representing one line (including the new-line character), see @ref{List of
-strings}. It will mostly be the output of @code{gal_txt_stdin_read}, which
-is used to read the program's input as separate lines from the standard
-input (see @ref{Text files}). Note that @code{filename} and @code{lines}
-are mutually exclusive and one of them must be @code{NULL}.
-
-In the output datasets, only the meta-data strings (column name, units and
-comments), will be allocated and set. This function is just for column
-information (meta-data), not column contents.
+
+Store the information of each column of a table into an array of data 
structures with @code{numcols} datasets (one data structure for each column).
+The number of rows is stored in @code{numrows}.
+The format of the table (e.g., ASCII text file, or FITS binary or ASCII table) 
will be put in @code{tableformat} (macros defined above).
+If the @code{filename} is not a FITS file, then @code{hdu} will not be used 
(can be @code{NULL}).
+
+The input must be either a file (specified by @code{filename}) or a list of 
strings (@code{lines}).
+@code{lines} is a list of strings with each node representing one line 
(including the new-line character), see @ref{List of strings}.
+It will mostly be the output of @code{gal_txt_stdin_read}, which is used to 
read the program's input as separate lines from the standard input (see 
@ref{Text files}).
+Note that @code{filename} and @code{lines} are mutually exclusive and one of 
them must be @code{NULL}.
+
+In the output datasets, only the meta-data strings (column name, units and 
comments), will be allocated and set.
+This function is just for column information (meta-data), not column contents.
 @end deftypefun
 
 @deftypefun void gal_table_print_info (gal_data_t @code{*allcols}, size_t 
@code{numcols}, size_t @code{numrows})
-This program will print the column information for all the columns (output
-of @code{gal_table_info}). The output is in the same format as this command
-with Gnuastro Table program (see @ref{Table}):
+Print the column information for all the columns (output of 
@code{gal_table_info}) to standard output.
+The output is in the same format as this command with Gnuastro Table program 
(see @ref{Invoking asttable}):
 @example
 $ asttable --info table.fits
 @end example
diff --git a/lib/table.c b/lib/table.c
index fe579877..ae5e035a 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -154,9 +154,9 @@ gal_table_info(char *filename, char *hdu, gal_list_str_t 
*lines,
 void
 gal_table_print_info(gal_data_t *allcols, size_t numcols, size_t numrows)
 {
-  size_t i;
-  int Nw=3, nw=4, uw=5, tw=4;   /* Initial width from label's width */
-  char *name, *unit, *comment;
+  size_t i, mms;
+  int Nw=3, nw=4, uw=5, tw=4, twt;/* Initial width from label's width */
+  char *typestr, *name, *unit, *comment;
 
   /* If there aren't any columns, there is no need to print anything. */
   if(numcols==0) return;
@@ -167,13 +167,23 @@ gal_table_print_info(gal_data_t *allcols, size_t numcols, 
size_t numrows)
   Nw=log10(numcols)+1;
   for(i=0;i<numcols;++i)
     {
+      /* Length of name and unit columns. */
       if(allcols[i].name && strlen(allcols[i].name)>nw)
         nw=strlen(allcols[i].name);
       if(allcols[i].unit && strlen(allcols[i].unit)>uw)
         uw=strlen(allcols[i].unit);
-      if(allcols[i].type
-         && strlen(gal_type_name(allcols[i].type, 1))>tw)
-        tw=strlen(gal_type_name(allcols[i].type, 1));
+
+      /* For the type, we need to account for vector columns. For strings,
+         their length is already accounted for there, so this doesn't
+         involve them. Recall that within 'gal_fits_tab_info', we put the
+         repeat counter into the 'minmapsize' element of the
+         'gal_data_t'. If the repeat of a numerical column is more than 1,
+         then we need to add a '[N]' after the type name later. */
+      mms=allcols[i].minmapsize;
+      twt=strlen(gal_type_name(allcols[i].type, 1));
+      if(allcols[i].type!=GAL_TYPE_STRING && mms>1)
+        twt += (int)(log10(mms))+1+2; /* 1 for the log, 2 for '[]'. */
+      if(allcols[i].type && twt>tw) tw=twt;
     }
 
   /* We want one column space between the columns for readability, not the
@@ -191,15 +201,31 @@ gal_table_print_info(gal_data_t *allcols, size_t numcols, 
size_t numrows)
   /* For each column, print the information, then free them. */
   for(i=0;i<numcols;++i)
     {
+      /* To help in reading. */
       name    = allcols[i].name;       /* Just defined for easier     */
       unit    = allcols[i].unit;       /* readability. The compiler   */
       comment = allcols[i].comment;    /* optimizer will remove them. */
+
+      /* Set the vector size (if relevant). */
+      mms=allcols[i].minmapsize;
+      if(allcols[i].type!=GAL_TYPE_STRING && mms>1)
+        {
+          if( asprintf(&typestr, "%s[%zu]",
+                       gal_type_name(allcols[i].type, 1), mms)<0 )
+            error(EXIT_FAILURE, 0, "%s: 'astprintf' allocation", __func__);
+        }
+      else
+        gal_checkset_allocate_copy(gal_type_name(allcols[i].type, 1),
+                                   &typestr);
+
+      /* Print the actual info. */
       printf("%-*zu%-*s%-*s%-*s%s\n", Nw, i+1,
              nw, name ? name : GAL_BLANK_STRING ,
              uw, unit ? unit : GAL_BLANK_STRING ,
              tw,
-             allcols[i].type ? gal_type_name(allcols[i].type, 1) : "--",
+             allcols[i].type ? typestr : "--",
              comment ? comment : GAL_BLANK_STRING);
+      free(typestr);
     }
 
   /* Print the number of rows. */



reply via email to

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