gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 1802d363 1/2: Table: new $_all to repeat arith


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 1802d363 1/2: Table: new $_all to repeat arithmetic operation on all columns
Date: Thu, 6 Jul 2023 12:13:02 -0400 (EDT)

branch: master
commit 1802d3633ace2e1a54adf4f84c0e1bf42bce00aa
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Table: new $_all to repeat arithmetic operation on all columns
    
    Until now, if it was necessary to repeat the same operation on all the
    columns of a table, users had to repeat the "-c'arith ...'" option many
    times. When the number of columns are many, this was annoying (especially
    since managing commenting of the single-quotes in a shell loop to later use
    in the 'asttable' command is not easy!).
    
    With this commit, a new '$_all' has been defined in Table's column
    arithmetic. When an Arithmetic expression contains this string, that
    expression will be repeated for all the columns in the table.
    
    This was added after a discussion with Sepideh Eskandarlou.
---
 NEWS           | 12 +++++---
 bin/table/ui.c | 93 +++++++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 81 insertions(+), 24 deletions(-)

diff --git a/NEWS b/NEWS
index c88ad2ba..1b4acc92 100644
--- a/NEWS
+++ b/NEWS
@@ -39,10 +39,14 @@ See the end of the file for license conditions.
     - pool-mean: Similar to 'pool-min' but using mean.
     - pool-median: Similar to 'pool-min' but using median.
 
-  astscript-zeropoint:
-  --mksrc: use a custom Makefile for estimating the zeropoint, not the
-    default installed Makefile. This is primarily intended for debugging or
-    developing this script, not for normal usage.
+  Table:
+  - '$_all' in column arithmetic: when an arithmetic expression contains
+    this string, it will be repeated independently for all the columns of
+    the input table.
+
+  astscript-zeropoint: --mksrc: use a custom Makefile for estimating the
+  zeropoint, not the default installed Makefile. This is primarily intended
+  for debugging or developing this script, not for normal usage.
 
   Library:
   -gal_pool_min: min-pooling function, see 'pool-min' above.
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 09c7b07d..2a2b9ef6 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -190,7 +190,8 @@ parse_opt(int key, char *arg, struct argp_state *state)
          'arg' will be an empty string! We don't want to account for such
          cases (and give a clear error that no input has been given). */
       if(p->filename)
-        argp_error(state, "only one argument (input file) should be given");
+        argp_error(state, "only one argument (input file) should be given, "
+                   "extra argument is: '%s'", arg);
       else
         if(arg[0]!='\0') p->filename=arg;
       break;
@@ -654,6 +655,38 @@ ui_print_info_exit(struct tableparams *p)
 
 
 
+static void
+ui_columns_prepare_arith(struct tableparams *p, gal_data_t *colinfo,
+                         gal_list_str_t **colstoread, size_t *totcalled,
+                         size_t numcols, char *str)
+{
+  struct column_pack *node;
+
+  /* If this is the first arithmetic operation and the user has
+     already asked for some columns, we'll need to put all
+     previously requested simply-printed columns into an 'colpack'
+     structure, then add this arithmetic operation's 'colpack'. */
+  if(p->colpack==NULL && *colstoread)
+    {
+      /* Allocate an empty structure and set the necessary
+         pointers. */
+      node=ui_colpack_add_new_to_end(&p->colpack);
+      node->start=0;
+      node->numsimple=gal_list_str_number(*colstoread);
+      *totcalled=node->numsimple;
+    }
+
+  /* Add a new column pack for this arithmetic operation, then read
+     all the tokens (while specifying which columns it needs). */
+  node=ui_colpack_add_new_to_end(&p->colpack);
+  arithmetic_init(p, &node->arith, colstoread, totcalled,
+                  str+ARITHMETIC_CALL_LENGTH, colinfo, numcols);
+}
+
+
+
+
+
 /* The columns can be given as comma-separated values to one option or
    multiple calls to the column option. Here, we'll check if the input list
    has comma-separated values. If they do then the list will be updated to
@@ -661,12 +694,13 @@ ui_print_info_exit(struct tableparams *p)
 static void
 ui_columns_prepare(struct tableparams *p, gal_list_str_t *lines)
 {
-  int tableformat;
+  char *tstr;
   gal_data_t *colinfo=NULL;
+  int tableformat, arithallind=0;
   struct column_pack *node, *last;
   gal_list_str_t *tmp, *colstoread=NULL;
   size_t i, totcalled=0, numcols, numrows, numsimple;
-  char *str, countstr[11]; /* an un-signed 32-bit integer takes 10 chars */
+  char *c, *str, countstr[11]; /* an un-signed 32-bit integer takes 10 chars */
 
   /* Go over the list of requested columns from the main input. */
   for(tmp=p->columns;tmp!=NULL;tmp=tmp->next)
@@ -687,25 +721,44 @@ ui_columns_prepare(struct tableparams *p, gal_list_str_t 
*lines)
             colinfo=gal_table_info(p->filename, p->cp.hdu, lines,
                                    &numcols, &numrows, &tableformat);
 
-          /* If this is the first arithmetic operation and the user has
-             already asked for some columns, we'll need to put all
-             previously requested simply-printed columns into an 'colpack'
-             structure, then add this arithmetic operation's 'colpack'. */
-          if(p->colpack==NULL && colstoread)
+          /* Check if '$_all' is in the string. */
+          for(c=str; *c!='\0'; ++c)
+            if(strncmp(c, "$_all", 5)==0)
+              { arithallind=c-str; break; }
+
+          /* When '$_all' is in the string, we need to repeat this option
+             for every column. Otherwise, just add this option once.*/
+          if(arithallind)
             {
-              /* Allocate an empty structure and set the necessary
-                 pointers. */
-              node=ui_colpack_add_new_to_end(&p->colpack);
-              node->start=0;
-              node->numsimple=gal_list_str_number(colstoread);
-              totcalled=node->numsimple;
+              /* Because we want to use the same bytes as '$_all' in the
+                 input string, we have four characters to write over '_all'
+                 with the number of each column. However, 'sprintf' already
+                 puts a '\0' on the last character (which we later replace
+                 with a ' '), so we only have three characters to use for
+                 column numbers. Therefore, to use this feature the input
+                 table can have a maximum of 999 columns (the FITS standard
+                 only accepts 999 columns; so it is very rare for people to
+                 need more! but we can add it if necessary). */
+              if(numcols>999)
+                error(EXIT_FAILURE, 0, "the '$_all' feature is currently "
+                      "only implemented for tables with fewer than 999 "
+                      "columns. Please contact us at %s to add more columns",
+                      PACKAGE_BUGREPORT);
+
+              /* Repeat the arithmetic command for each column. */
+              for(i=1;i<=numcols;++i)
+                {
+                  gal_checkset_allocate_copy(str, &tstr);
+                  sprintf(tstr+arithallind+1, "%-3zu", i);
+                  tstr[arithallind+4]=' ';
+                  ui_columns_prepare_arith(p, colinfo, &colstoread, &totcalled,
+                                           numcols, tstr);
+                  free(tstr);
+                }
             }
-
-          /* Add a new column pack for this arithmetic operation, then read
-             all the tokens (while specifying which columns it needs). */
-          node=ui_colpack_add_new_to_end(&p->colpack);
-          arithmetic_init(p, &node->arith, &colstoread, &totcalled,
-                          str+ARITHMETIC_CALL_LENGTH, colinfo, numcols);
+          else
+            ui_columns_prepare_arith(p, colinfo, &colstoread, &totcalled,
+                                     numcols, str);
         }
 
       /* This is a simple column (no change in values). */



reply via email to

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