gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master da5064ca 2/2: Library (txt.h): precision of 0


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master da5064ca 2/2: Library (txt.h): precision of 0 in floats, prints rounded integer
Date: Mon, 24 Oct 2022 12:44:23 -0400 (EDT)

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

    Library (txt.h): precision of 0 in floats, prints rounded integer
    
    Until now, when a floating point column (32-bit or 64-bit) was printed with
    a precision of 0 (for example using '--txtf64precision'), the option would
    be ignored! This was happening because in the text library, we were
    assuming that the precision is always larger than 0 (so when it was given a
    value of zero, it would use the default value instead).
    
    With this commit, 0 is no longer the invalid value, but we are properly
    using the 'GAL_BLANK_INT' value as an invalid value and this fixed the
    problem.
    
    This bug was reported by Sepideh Eskandarlou.
    
    This fixes bug #63266.
---
 NEWS                         | 12 ++++++++----
 bin/script/sort-by-night.in  |  2 +-
 bin/table/args.h             |  4 ++--
 bin/table/main.h             |  4 ++--
 bin/table/table.c            |  6 ++++--
 bin/table/ui.c               |  2 ++
 doc/announce-acknowledge.txt |  2 +-
 doc/gnuastro.texi            |  4 ++++
 lib/data.c                   |  4 +++-
 lib/tableintern.c            | 23 ++++++++++++++---------
 lib/txt.c                    | 19 +++++++++++--------
 11 files changed, 52 insertions(+), 30 deletions(-)

diff --git a/NEWS b/NEWS
index f551c559..6e8bb953 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,10 @@ See the end of the file for license conditions.
 ** Changed features
 
 ** Bugs fixed
+  bug #63266: Table ignores a value of 0 given to '--txtf32precision' or
+              '--txtf32precision=0' (happens when floating point columns
+              need to be rounded to integers). Reported by Sepideh
+              Eskandarlou.
 
 
 
@@ -133,9 +137,9 @@ See the end of the file for license conditions.
     --txtf32format (or '-f'): Format of 32-bit floating point columns. This
       can be either 'fixed' (for fixed-point notation) or 'exp' (for
       exponential/scientific notation).
-    --txtf32precision (or '-d'): number of digits following the
+    --txtf32precision (or '-p'): number of digits following the
       decimal-point of 32-bit floating point columns.
-    --txtf64format (or '-p'): Format of 64-bit floating point columns. This
+    --txtf64format (or '-d'): Format of 64-bit floating point columns. This
       can be either 'fixed' (for fixed-point notation) or 'exp' (for
       exponential/scientific notation).
     --txtf32precision (or '-B'): number of digits following the
@@ -167,8 +171,8 @@ See the end of the file for license conditions.
       input pixel's area in the output pixel. When the output pixel scale
       is similar to the input, the Moiré pattern can cause varying
       artificial smoothing of the noise level. See the newly added "Moiré
-      pattern" section of the book for more on its basics and how to reduce
-      it in your outputs.
+      pattern and its correction" section of the book for more on its
+      basics and how to reduce it in your outputs.
 
   astscript-fits-view:
   --ds9colorbarmulti: show a separate color-bar for each image in DS9. By
diff --git a/bin/script/sort-by-night.in b/bin/script/sort-by-night.in
index d9ff8620..0141c355 100644
--- a/bin/script/sort-by-night.in
+++ b/bin/script/sort-by-night.in
@@ -275,7 +275,7 @@ list=$(astfits --keyvalue=$key --hdu=$hdu $inputs 
--colinfoinstdout \
                                      sec int32 86400 % '$hour' 3600  x lt \
                                      day 1 - \
                                    where' \
-                          --colmetadata=ARITH_8,NIGHT,counter,"Observing 
night." \
+                          --colmetadata=3,NIGHT,counter,"Observing night." \
                           --colinfoinstdout --stdintimeout=$stdintime \
                | asttable --sort=UNIXSEC --colinfoinstdout \
                           --stdintimeout=$stdintime)
diff --git a/bin/table/args.h b/bin/table/args.h
index ea7e4709..f1c1b3e8 100644
--- a/bin/table/args.h
+++ b/bin/table/args.h
@@ -228,7 +228,7 @@ struct argp_option program_options[] =
       "Text output float32 precision.",
       GAL_OPTIONS_GROUP_OUTPUT,
       &p->txtf32precision,
-      GAL_TYPE_SIZE_T,
+      GAL_TYPE_INT,
       GAL_OPTIONS_RANGE_GE_0,
       GAL_OPTIONS_NOT_MANDATORY,
       GAL_OPTIONS_NOT_SET
@@ -241,7 +241,7 @@ struct argp_option program_options[] =
       "Text output float32 precision.",
       GAL_OPTIONS_GROUP_OUTPUT,
       &p->txtf64precision,
-      GAL_TYPE_SIZE_T,
+      GAL_TYPE_INT,
       GAL_OPTIONS_RANGE_GE_0,
       GAL_OPTIONS_NOT_MANDATORY,
       GAL_OPTIONS_NOT_SET
diff --git a/bin/table/main.h b/bin/table/main.h
index f7e0def8..c2a02826 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -124,8 +124,8 @@ struct tableparams
   gal_data_t     *colmetadata;  /* Set column metadata.                 */
   char          *txtf32fmtstr;  /* Floating point formats (exp, flt).   */
   char          *txtf64fmtstr;  /* Floating point formats (exp, flt).   */
-  size_t      txtf32precision;  /* Precision of float32 in text.        */
-  size_t      txtf64precision;  /* Precision of float32 in text.        */
+  int         txtf32precision;  /* Precision of float32 in text.        */
+  int         txtf64precision;  /* Precision of float32 in text.        */
 
   /* Internal. */
   struct column_pack *outcols;  /* Output column packages.              */
diff --git a/bin/table/table.c b/bin/table/table.c
index c109f023..62824aae 100644
--- a/bin/table/table.c
+++ b/bin/table/table.c
@@ -1201,11 +1201,13 @@ table_txt_formats(struct tableparams *p)
         {
         case GAL_TYPE_FLOAT32:
           if(p->txtf32format)    tmp->disp_fmt=p->txtf32format;
-          if(p->txtf32precision) tmp->disp_precision=p->txtf32precision;
+          if(p->txtf32precision!=GAL_BLANK_INT)
+            tmp->disp_precision=p->txtf32precision;
           break;
         case GAL_TYPE_FLOAT64:
           if(p->txtf64format)    tmp->disp_fmt=p->txtf64format;
-          if(p->txtf64precision) tmp->disp_precision=p->txtf64precision;
+          if(p->txtf64precision!=GAL_BLANK_INT)
+            tmp->disp_precision=p->txtf64precision;
           break;
         }
     }
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 0606c5a5..783afba7 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -120,6 +120,8 @@ ui_initialize_options(struct tableparams *p,
   cp->coptions           = gal_commonopts_options;
 
   /* Program-specific initialization. */
+  p->txtf32precision     = GAL_BLANK_INT;
+  p->txtf64precision     = GAL_BLANK_INT;
   p->head                = GAL_BLANK_SIZE_T;
   p->tail                = GAL_BLANK_SIZE_T;
 
diff --git a/doc/announce-acknowledge.txt b/doc/announce-acknowledge.txt
index 0f0c5b8e..b246ebef 100644
--- a/doc/announce-acknowledge.txt
+++ b/doc/announce-acknowledge.txt
@@ -1,6 +1,6 @@
 Alphabetically ordered list to acknowledge in the next release.
 
-
+Sepideh Eskandarlou
 
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 135f471b..aa32e93e 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -14867,6 +14867,8 @@ Exponential notation (for example 
@code{1.2345689012e+06})
 @item  -p STR
 @itemx --txtf32precision=INT
 Number of digits after the decimal point (precision) for columns with a 32-bit 
floating point datatype.
+This can take any positive integer (including 0).
+When given a value of zero, the floating point number will be rounded to the 
nearest integer.
 
 @item  -d STR
 @itemx --txtf64format=STR
@@ -14883,6 +14885,8 @@ Exponential notation (for example 
@code{1.2345689012e+06})
 @item  -B STR
 @itemx --txtf64precision=INT
 Number of digits after the decimal point (precision) for columns with a 64-bit 
floating point datatype.
+This can take any positive integer (including 0).
+When given a value of zero, the floating point number will be rounded to the 
nearest integer.
 @end table
 
 
diff --git a/lib/data.c b/lib/data.c
index caa997a7..a720be26 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -123,6 +123,7 @@ gal_data_initialize(gal_data_t *data, void *array, uint8_t 
type,
      the default values are used if/when printing.*/
   data->flag       = 0;
   data->status     = 0;
+  data->disp_width = -1;
   data->next       = NULL;
   data->ndim       = ndim;
   data->type       = type;
@@ -130,10 +131,11 @@ gal_data_initialize(gal_data_t *data, void *array, 
uint8_t type,
   data->mmapname   = NULL;
   data->quietmmap  = quietmmap;
   data->minmapsize = minmapsize;
+  data->disp_precision=GAL_BLANK_INT;
+  data->disp_fmt=GAL_TABLE_DISPLAY_FMT_INVALID;
   gal_checkset_allocate_copy(name, &data->name);
   gal_checkset_allocate_copy(unit, &data->unit);
   gal_checkset_allocate_copy(comment, &data->comment);
-  data->disp_fmt=data->disp_width=data->disp_precision=-1;
 
 
   /* Copy the WCS structure. */
diff --git a/lib/tableintern.c b/lib/tableintern.c
index 6b37fda5..5ff56fe8 100644
--- a/lib/tableintern.c
+++ b/lib/tableintern.c
@@ -239,7 +239,7 @@ gal_tableintern_col_print_info(gal_data_t *col, int 
tableformat,
 {
   size_t j;
   char **strarr;
-  int maxstrlen, width=0, precision=0;
+  int maxstrlen, width=0, precision=GAL_BLANK_INT;
 
 
   /* First do a sanity check, so we can safly stop checking in the steps
@@ -318,7 +318,8 @@ gal_tableintern_col_print_info(gal_data_t *col, int 
tableformat,
         }
       else width=( col->disp_width<=0 ? GAL_TABLE_DEF_WIDTH_INT
                     : col->disp_width );
-      precision=( col->disp_precision<=0 ? GAL_TABLE_DEF_PRECISION_INT
+      precision=( col->disp_precision==GAL_BLANK_INT
+                  ? GAL_TABLE_DEF_PRECISION_INT
                   : col->disp_precision );
       break;
 
@@ -331,8 +332,9 @@ gal_tableintern_col_print_info(gal_data_t *col, int 
tableformat,
       fmt[0] = tableformat==GAL_TABLE_FORMAT_TXT ? 'd' : 'I';
       width = ( col->disp_width<=0 ? GAL_TABLE_DEF_WIDTH_INT
                 : col->disp_width );
-      precision = ( col->disp_precision<=0 ? GAL_TABLE_DEF_PRECISION_INT
-                    : col->disp_precision );
+      precision=( col->disp_precision==GAL_BLANK_INT
+                  ? GAL_TABLE_DEF_PRECISION_INT
+                  : col->disp_precision );
       break;
 
 
@@ -343,7 +345,8 @@ gal_tableintern_col_print_info(gal_data_t *col, int 
tableformat,
       fmt[0] = tableformat==GAL_TABLE_FORMAT_TXT ? 'd' : 'I';
       width=( col->disp_width<=0 ? GAL_TABLE_DEF_WIDTH_LINT
               : col->disp_width );
-      precision=( col->disp_precision<=0 ? GAL_TABLE_DEF_PRECISION_INT
+      precision=( col->disp_precision==GAL_BLANK_INT
+                  ? GAL_TABLE_DEF_PRECISION_INT
                   : col->disp_precision );
       break;
 
@@ -375,14 +378,16 @@ gal_tableintern_col_print_info(gal_data_t *col, int 
tableformat,
         case GAL_TYPE_FLOAT32:
           width     = ( col->disp_width<=0
                         ? GAL_TABLE_DEF_WIDTH_FLT : col->disp_width );
-          precision = ( col->disp_precision<=0
-                        ? GAL_TABLE_DEF_PRECISION_FLT : col->disp_precision );
+          precision = ( col->disp_precision==GAL_BLANK_INT
+                        ? GAL_TABLE_DEF_PRECISION_FLT
+                        : col->disp_precision );
           break;
         case GAL_TYPE_FLOAT64:
           width     = ( col->disp_width<=0
                         ? GAL_TABLE_DEF_WIDTH_DBL : col->disp_width );
-          precision = ( col->disp_precision<=0
-                        ? GAL_TABLE_DEF_PRECISION_DBL : col->disp_precision );
+          precision = ( col->disp_precision==GAL_BLANK_INT
+                        ? GAL_TABLE_DEF_PRECISION_DBL
+                        : col->disp_precision );
           break;
         }
 
diff --git a/lib/txt.c b/lib/txt.c
index 94e14c7c..e21e27ca 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -1425,6 +1425,7 @@ make_fmts_for_printf(gal_data_t *datall, int leftadjust, 
size_t *len)
                                 ? gal_blank_as_string(data->type, 0)
                                 : NULL );
 
+
       /* Fill in the printing paramters. */
       gal_tableintern_col_print_info(data, GAL_TABLE_FORMAT_TXT, fmt, lng);
 
@@ -1442,24 +1443,25 @@ make_fmts_for_printf(gal_data_t *datall, int 
leftadjust, size_t *len)
          if the printed string is larger than the expected width. */
       if(data->next)
         {
-          if(data->disp_precision > 0)
-            *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%%s%d.%d%s%s ",
-                                leftadjust ? "-" : "", data->disp_width,
-                                data->disp_precision, lng, fmt);
-          else
+          if(data->disp_precision == GAL_BLANK_INT)
             *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%%s%d%s%s ",
                                 leftadjust ? "-" : "", data->disp_width,
                                 lng, fmt);
+          else
+            *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%%s%d.%d%s%s ",
+                                leftadjust ? "-" : "", data->disp_width,
+                                data->disp_precision, lng, fmt);
         }
       else /* Last column: no empty characters (no width or adjustment). */
         {
-          if(data->disp_precision > 0)
+          if(data->disp_precision == GAL_BLANK_INT)
+            *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%%s%s", lng, fmt);
+          else
             *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%.%d%s%s",
                                 data->disp_precision, lng, fmt);
-          else
-            *len += 1 + sprintf(fmts[i*FMTS_COLS], "%%%s%s", lng, fmt);
         }
 
+
       /* Set the string for the Gnuastro type. For strings, we also need to
          write the maximum number of characters.*/
       if(data->type==GAL_TYPE_STRING)
@@ -1468,6 +1470,7 @@ make_fmts_for_printf(gal_data_t *datall, int leftadjust, 
size_t *len)
       else
         strcpy(fmts[i*FMTS_COLS+1], gal_type_name(data->type, 0));
 
+
       /* Increment the column counter. */
       ++i;
     }



reply via email to

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