gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master e51bf75f 3/3: Arithmetic: new repeat operator


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master e51bf75f 3/3: Arithmetic: new repeat operator
Date: Thu, 10 Feb 2022 19:51:12 -0500 (EST)

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

    Arithmetic: new repeat operator
    
    Until now, the only way to repeat a certain dataset on the stack was to
    manually repeat its name on the command line. This is very inconvenient and
    could even cause problems on systems that have a limited buffer for the
    command-line (when thousands of copies are necessary!).
    
    With this commit, a new 'repeat' operator has been added to Arithmetic for
    this purpose. It is now very easy to make thousands of copies of a dataset
    if necessary.
---
 NEWS                        | 15 +++++++++------
 bin/arithmetic/arithmetic.c | 38 ++++++++++++++++++++++++++++++++++++++
 bin/arithmetic/arithmetic.h |  1 +
 doc/gnuastro.texi           | 12 +++++++++++-
 4 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index f14cd1bc..54a32466 100644
--- a/NEWS
+++ b/NEWS
@@ -32,12 +32,15 @@ See the end of the file for license conditions.
      based on feedback from Andrés Del Pino Molina.
 
   Arithmetic:
-   - 'add-dimension-fast': Add a new dataset along the "fastest" dimension
-     of the first dataset (in a FITS image, the "fast" dimension is the
-     horizontal one). For example if you have N one-dimensional datasets
-     with M elements, you can use this operator to construct a
-     2-dimensional FITS image that has N pixels along the horizontal and M
-     pixels along the vertical.
+   - New operators:
+     - add-dimension-fast: Add a new dataset along the "fastest" dimension
+       of the first dataset (in a FITS image, the "fast" dimension is the
+       horizontal one). For example if you have N one-dimensional datasets
+       with M elements, you can use this operator to construct a
+       2-dimensional FITS image that has N pixels along the horizontal and
+       M pixels along the vertical.
+     - repeat: Put a copy of the given dataset onto the top of the stack of
+       operators for the requested number of times.
 
   Crop:
    --widthinpix: when in WCS-mode, the value given to '--width' will be
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 6d1bc1c9..52703aba 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -1181,6 +1181,38 @@ arithmetic_add_dimension(struct arithmeticparams *p, 
char *token,
 
 
 
+void
+arithmetic_repeat(struct arithmeticparams *p, char *token, int operator)
+{
+  size_t i, num;
+  gal_data_t *ttmp, *tmp = operands_pop(p, token);
+
+  /* Make sure the first operand is a number. */
+  if(tmp->size!=1)
+    error(EXIT_FAILURE, 0, "first popped operand to '%s' must be a "
+          "number (specifying how many datasets to use)", token);
+
+  /* Put the first-popped value into 'num'. */
+  tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_SIZE_T);
+  num=*(size_t *)(tmp->array);
+  gal_data_free(tmp);
+
+  /* Pop the dataset to be copied. */
+  tmp=operands_pop(p, token);
+
+  /* Add it to the stack for the desired number of times. */
+  for(i=0; i<num; ++i)
+    {
+      ttmp=gal_data_copy(tmp);
+      operands_add(p, NULL, ttmp);
+    }
+
+  /* Clean up. */
+  gal_data_free(tmp);
+}
+
+
+
 
 
 
@@ -1253,6 +1285,8 @@ arithmetic_set_operator(char *string, size_t 
*num_operands)
         { op=ARITHMETIC_OP_ADD_DIMENSION_SLOW;    *num_operands=0; }
       else if (!strcmp(string, "add-dimension-fast"))
         { op=ARITHMETIC_OP_ADD_DIMENSION_FAST;    *num_operands=0; }
+      else if (!strcmp(string, "repeat"))
+        { op=ARITHMETIC_OP_REPEAT;                *num_operands=0; }
       else
         error(EXIT_FAILURE, 0, "the argument '%s' could not be "
               "interpretted as a file name, named dataset, number, "
@@ -1391,6 +1425,10 @@ arithmetic_operator_run(struct arithmeticparams *p, int 
operator,
           arithmetic_add_dimension(p, operator_string, operator);
           break;
 
+        case ARITHMETIC_OP_REPEAT:
+          arithmetic_repeat(p, operator_string, operator);
+          break;
+
         default:
           error(EXIT_FAILURE, 0, "%s: a bug! please contact us at "
                 "%s to fix the problem. The code %d is not "
diff --git a/bin/arithmetic/arithmetic.h b/bin/arithmetic/arithmetic.h
index 62828c56..3df9d097 100644
--- a/bin/arithmetic/arithmetic.h
+++ b/bin/arithmetic/arithmetic.h
@@ -53,6 +53,7 @@ enum arithmetic_prog_operators
   ARITHMETIC_OP_UNIQUE,
   ARITHMETIC_OP_ADD_DIMENSION_SLOW,
   ARITHMETIC_OP_ADD_DIMENSION_FAST,
+  ARITHMETIC_OP_REPEAT,
 };
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 6f346ca9..a0309ef6 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -15041,7 +15041,17 @@ $ astarithmetic image.fits image.fits 5 gt nan where 
-g1
 But with this operator you can simply give @file{image.fits} the name @code{i} 
and simplify the command above to the more readable one below (which greatly 
helps when the filename is long):
 
 @example
-$ astarithmetic image.fits set-i   i i 5 gt nan where
+$ astarithmetic image.fits set-i i i 5 gt nan where
+@end example
+
+@item repeat
+Add N copies of the second popped operand to the stack of operands.
+N is the first popped operand.
+For example, let's assume @file{image.fits} is a @mymath{100\times100} image.
+The output of the command below will be a 3D datacube of size 
@mymath{100\times100\times20} voxels (volume-pixels):
+
+@example
+$ astarithmetic image.fits 20 repeat 20 add-dimension-slow
 @end example
 
 @item tofile-AAA



reply via email to

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