gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master d53cdb43: Arithmetic: single number written to


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master d53cdb43: Arithmetic: single number written to file when --output is called
Date: Sat, 11 Jun 2022 12:29:37 -0400 (EDT)

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

    Arithmetic: single number written to file when --output is called
    
    Until now, when Arithmetic's final output was a single number and the user
    had given an output file name with '--output', Arithmetic would ignore the
    request and not make any output file: it would continue printing the value
    on the standard output. To write the output in a file, the only solution
    was to use the '--onedasimage' option. But in that case, there was no way
    to write it as a table (as with all other 1D outputs).
    
    With this commit, a new internal variable has been added to Arithmetic that
    will let it know if the user actually called '--output' or not. With this
    variable, Arithmetic can behave as expected: when '--output' isn't given,
    it will print a single-element output on standard output. Otherwise, it
    will respect the user's request and write the output in a file.
    
    This bug was found by Raul Infante-Sainz.
    
    This fixes bug #62597.
---
 NEWS                        |  2 ++
 bin/arithmetic/arithmetic.c |  5 +++--
 bin/arithmetic/main.h       |  1 +
 bin/arithmetic/ui.c         |  6 +++++-
 doc/gnuastro.texi           | 12 +++++++-----
 5 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index c95e9cdd..7ebbe815 100644
--- a/NEWS
+++ b/NEWS
@@ -97,6 +97,8 @@ See the end of the file for license conditions.
               and fixed by Raul Infante-Sainz.
   bug #62564: Arithmetic not parsing numbers in scientific notation (for
               example '1e5'). Found by Elham Saremi.
+  bug #62597: Arithmetic not writing single-valued output into a file when
+              called with '--output'. Reported by Raul Infante-Sainz.
 
 
 
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 52703aba..d89c1d38 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -1572,9 +1572,10 @@ reversepolish(struct arithmeticparams *p)
 
 
   /* If the final data structure has more than one element, write it as a
-     FITS file. Otherwise, print it in the standard output. */
+     FITS file. Otherwise, if the user didn't call '--output', print it in
+     the standard output. */
   data=p->operands->data;
-  if(data->size==1 && data->ndim==1 && p->onedasimage==0)
+  if(data->size==1 && data->ndim==1 && p->outnamerequested==0)
     {
       /* Make the string to print the number. */
       printnum=gal_type_to_string(data->array, data->type, 0);
diff --git a/bin/arithmetic/main.h b/bin/arithmetic/main.h
index 12cf141c..6d2aa4f0 100644
--- a/bin/arithmetic/main.h
+++ b/bin/arithmetic/main.h
@@ -89,6 +89,7 @@ struct arithmeticparams
   /* Internal: */
   uint8_t          envseed;  /* To setup the random number generator.   */
   struct operand *operands;  /* The operands linked list.               */
+  int     outnamerequested;  /* ==1 if the user has given '--otuput'.   */
   time_t           rawtime;  /* Starting time of the program.           */
 };
 
diff --git a/bin/arithmetic/ui.c b/bin/arithmetic/ui.c
index dab2f435..2aab601f 100644
--- a/bin/arithmetic/ui.c
+++ b/bin/arithmetic/ui.c
@@ -283,7 +283,11 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
      are already reversed, the first FITS file encountered, is the first
      FITS file given by the user. Also, note that these file name
      operations are only necessary for the first FITS file in the token
-     list. */
+     list. In some scnearios, we need to know if an actual output name was
+     given or if the output name was set automatically. So we'll set
+     'outnamerequested' before automatically filling the output if not
+     given. */
+  p->outnamerequested = cp->output ? 1 : 0;
   for(token=p->tokens; token!=NULL; token=token->next)
     {
       /* Strings given to the 'tofile' operator are also considered as
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index e1bca91f..eb736c80 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -16342,10 +16342,10 @@ $ astarithmetic img1.fits img2.fits img3.fits median  
              \
 
 Arithmetic's notation for giving operands to operators is fully described in 
@ref{Reverse polish notation}.
 The output dataset is last remaining operand on the stack.
-When the output dataset a single number, it will be printed on the 
command-line.
+When the output dataset a single number, and @option{--output} isn't called, 
it will be printed on the standard output (command-line).
 When the output is an array, it will be stored as a file.
 
-The name of the final file can be specified with the @option{--output} option, 
but if its not given, Arithmetic will use ``automatic output'' on the name of 
the first FITS image encountered to generate an output file name, see 
@ref{Automatic output}.
+The name of the final file can be specified with the @option{--output} option, 
but if its not given (and the output dataset has more than one element), 
Arithmetic will use ``automatic output'' on the name of the first FITS image 
encountered to generate an output file name, see @ref{Automatic output}.
 By default, if the output file already exists, it will be deleted before 
Arithmetic starts operation.
 However, this can be disabled with the @option{--dontdelete} option (see 
below).
 At any point during Arithmetic's operation, you can also write the top operand 
on the stack to a file, using the @code{tofile} or @code{tofilefree} operators, 
see @ref{Arithmetic operators}.
@@ -16410,13 +16410,15 @@ This is very important for obtaining reproducible 
results, for more see @ref{Gen
 
 @item -O
 @itemx --onedasimage
-When final dataset to write as output only has one dimension, write it as a 
FITS image/array.
+Write final dataset as a FITS image/array even if it has a single dimension.
 By default, if the output is 1D, it will be written as a table, see above.
+If the output has more than one dimension, this option is redundant.
 
 @item -s
 @itemx --onedonstdout
-When final dataset to write as output only has one dimension, print it on the 
standard output, not in a file.
-By default, if the output is 1D, it will be written as a table, see above.
+Write final dataset (only when it is 1D) to standard output, not as a file.
+By default 1D datasets will be written as a table, see above.
+If the output has more than one dimension, this option is redundant.
 
 @item -D
 @itemx --dontdelete



reply via email to

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