gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 6d83291: Library (arithmetic.h): new operator


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 6d83291: Library (arithmetic.h): new operator called makenew
Date: Thu, 22 Oct 2020 18:55:29 -0400 (EDT)

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

    Library (arithmetic.h): new operator called makenew
    
    In many scenarios, we want to build an empty new dataset with a certain
    size. With this commit, the Arithmetic program has a new operator for this
    purpose.
---
 NEWS                        |  6 ++++
 bin/arithmetic/arithmetic.c |  9 +++++-
 bin/arithmetic/ui.c         |  9 ++++++
 doc/gnuastro.texi           | 16 ++++++++++
 lib/arithmetic.c            | 74 ++++++++++++++++++++++++++++++++++++++++++---
 lib/data.c                  |  4 +--
 lib/gnuastro/arithmetic.h   |  2 ++
 7 files changed, 112 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index 08d2012..50a159d 100644
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,11 @@ See the end of the file for license conditions.
          echo "113.64812416667 31.88732" \
               | asttable -c'arith $1 degree-to-ra $2 degree-to-dec'
 
+  Arithmetic:
+   - New operator:
+     - 'makenew': new operator to create an empty (zero-valued) new dataset
+       with given dimension and size (given as operands).
+
   Fits:
    - New '--skycoverage' option will report the area of the input image in
      RA/Dec, both in units of center/width, and box minimum/maximum
@@ -73,6 +78,7 @@ See the end of the file for license conditions.
      removed.
 
   Library:
+   - GAL_ARITHMETIC_OP_MAKENEW: new 'makenew' operator.
    - gal_blank_flag_remove: Remove all flagged elements in a dataset.
    - gal_blank_remove_rows: remove all rows that have atleast one blank.
    - gal_wcs_coverage: Return the sky coverage of given image HDU.
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 89e2321..4f5c19b 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -1241,7 +1241,14 @@ reversepolish(struct arithmeticparams *p)
           || operands_is_name(p, token->v) )
         operands_add(p, token->v, NULL);
       else if( (data=gal_data_copy_string_to_number(token->v)) )
-        operands_add(p, NULL, data);
+        {
+          /* The 'minmapsize' and 'quietmmap' parameters should be passed
+             onto the library within numbers also (since they are the
+             only things that go in the library sometimes). */
+          data->quietmmap=p->cp.quietmmap;
+          data->minmapsize=p->cp.minmapsize;
+          operands_add(p, NULL, data);
+        }
       /* Last option is an operator: the program will abort if the token
          isn't an operator. */
       else
diff --git a/bin/arithmetic/ui.c b/bin/arithmetic/ui.c
index 3e34c4a..5ab5fae 100644
--- a/bin/arithmetic/ui.c
+++ b/bin/arithmetic/ui.c
@@ -332,6 +332,15 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
         }
     }
 
+  /* In case no output name has been given (can happen with operators like
+     'makenew' when the user doesn't set an output name explicity), use a
+     default name. */
+  if(p->cp.output==NULL)
+    {
+      gal_checkset_allocate_copy("arithmetic.fits", &p->cp.output);
+      gal_checkset_writable_remove(p->cp.output, cp->keep, cp->dontdelete);
+    }
+
   /* Count the number of HDU values (if globalhdu isn't given) and check if
      its not less than the number of input FITS images. */
   if(p->globalhdu)
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 51712f8..67d5222 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -11455,6 +11455,16 @@ For example, the following command will produce the 
size of the first extension/
 astarithmetic a.fits 2 size
 @end example
 
+@item makenew
+Create a new dataset that only has zero values.
+The number of dimensions is read as the first popped operand and the number of 
elements along each dimension are the next popped operand (in reverse of the 
popping order).
+The type of the new dataset is an unsigned 8-bit integer and all pixel values 
have a value of zero.
+For example, if you want to create a new 100 by 200 pixel image, you can run 
this command:
+
+@example
+astarithmetic 100 200 2 makenew
+@end example
+
 @item set-AAA
 Set the characters after the dash (@code{AAA} in the case shown here) as a 
name for the first popped operand on the stack.
 The named dataset will be freed from memory as soon as it is no longer needed, 
or if the name is reset to refer to another dataset later in the command.
@@ -23872,6 +23882,12 @@ in @ref{Arithmetic}. So in your programs, it might be 
preferable to
 directly use those functions.
 @end deffn
 
+@deffn  Macro GAL_ARITHMETIC_OP_MAKENEW
+Create a new, zero-valued dataset with an unsigned 8-bit data type.
+The length along each dimension of the dataset should be given as a single 
list of @code{gal_data_t}s.
+The number of dimensions is derived from the number of nodes in the list and 
the length along each dimension is the single-valued element within that list.
+Just note that the list should be in the reverse of the desired dimensions.
+@end deffn
 
 @deftypefun {gal_data_t *} gal_arithmetic (int @code{operator}, size_t 
@code{numthreads}, int @code{flags}, ...)
 Do the arithmetic operation of @code{operator} on the given operands (the
diff --git a/lib/arithmetic.c b/lib/arithmetic.c
index 0138642..852a6e1 100644
--- a/lib/arithmetic.c
+++ b/lib/arithmetic.c
@@ -1794,6 +1794,60 @@ arithmetic_binary_function_flt(int operator, int flags, 
gal_data_t *il,
 
 
 
+/* Make a new dataset. */
+gal_data_t *
+arithmetic_makenew(gal_data_t *sizes)
+{
+  gal_data_t *out, *tmp, *ttmp;
+  int quietmmap=sizes->quietmmap;
+  size_t minmapsize=sizes->minmapsize;
+  size_t i, *dsize, ndim=gal_list_data_number(sizes);
+
+  /* Make sure all the elements are a single, integer number. */
+  for(tmp=sizes; tmp!=NULL; tmp=tmp->next)
+    {
+      if(tmp->size!=1)
+        error(EXIT_FAILURE, 0, "%s: operands given to 'makenew' operator "
+              "should only be a single number. However, at least one of "
+              "the input operands has %zu elements", __func__, tmp->size);
+
+      if( tmp->type==GAL_TYPE_FLOAT32 || tmp->type==GAL_TYPE_FLOAT64)
+        error(EXIT_FAILURE, 0, "%s: operands given to 'makenew' operator "
+              "should have integer types. However, at least one of "
+              "the input operands is floating point", __func__);
+    }
+
+  /* Fill the 'dsize' array based on the given values. */
+  i=ndim-1;
+  tmp=sizes;
+  dsize=gal_pointer_allocate(GAL_TYPE_SIZE_T, ndim, 1, __func__, "dsize");
+  while(tmp!=NULL)
+    {
+      /* Set the next pointer and conver this one to size_t.  */
+      ttmp=tmp->next;
+      tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_SIZE_T);
+
+      /* Write the dimension's length into 'dsize'. */
+      dsize[i--] = ((size_t *)(tmp->array))[0];
+
+      /* Free 'tmp' and re-set it to the next element. */
+      free(tmp);
+      tmp=ttmp;
+    }
+
+  /* allocate the necessary dataset. */
+  out=gal_data_alloc(NULL, GAL_TYPE_UINT8, ndim, dsize, NULL, 1, minmapsize,
+                     quietmmap, "EMPTY", "NOT-SET",
+                     "Empty dataset created by arithmetic.");
+
+  /* Clean up and return. */
+  free(dsize);
+  return out;
+}
+
+
+
+
 
 
 
@@ -1956,6 +2010,10 @@ gal_arithmetic_set_operator(char *string, size_t 
*num_operands)
   else if (!strcmp(string, "float64"))
     { op=GAL_ARITHMETIC_OP_TO_FLOAT64;        *num_operands=1;  }
 
+  /* New dataset. */
+  else if (!strcmp(string, "makenew"))
+    { op=GAL_ARITHMETIC_OP_MAKENEW;           *num_operands=-1;  }
+
   /* Operator not defined. */
   else
     { op=GAL_ARITHMETIC_OP_INVALID; *num_operands=GAL_BLANK_INT; }
@@ -2041,6 +2099,8 @@ gal_arithmetic_operator_string(int operator)
     case GAL_ARITHMETIC_OP_TO_FLOAT32:      return "float32";
     case GAL_ARITHMETIC_OP_TO_FLOAT64:      return "float64";
 
+    case GAL_ARITHMETIC_OP_MAKENEW:         return "makenew";
+
     default:                                return NULL;
     }
   return NULL;
@@ -2062,7 +2122,6 @@ gal_arithmetic(int operator, size_t numthreads, int 
flags, ...)
   /* Depending on the operator, do the job: */
   switch(operator)
     {
-
     /* Binary operators with any data type. */
     case GAL_ARITHMETIC_OP_PLUS:
     case GAL_ARITHMETIC_OP_MINUS:
@@ -2175,6 +2234,12 @@ gal_arithmetic(int operator, size_t numthreads, int 
flags, ...)
       out=arithmetic_bitwise_not(flags, d1);
       break;
 
+    /* Size operator */
+    case GAL_ARITHMETIC_OP_SIZE:
+      d1 = va_arg(va, gal_data_t *);
+      d2 = va_arg(va, gal_data_t *);
+      out=arithmetic_size(operator, flags, d1, d2);
+      break;
 
     /* Conversion operators. */
     case GAL_ARITHMETIC_OP_TO_UINT8:
@@ -2191,11 +2256,10 @@ gal_arithmetic(int operator, size_t numthreads, int 
flags, ...)
       out=arithmetic_change_type(d1, operator, flags);
       break;
 
-    /* Size operator */
-    case GAL_ARITHMETIC_OP_SIZE:
+    /* Build dataset from scratch. */
+    case GAL_ARITHMETIC_OP_MAKENEW:
       d1 = va_arg(va, gal_data_t *);
-      d2 = va_arg(va, gal_data_t *);
-      out=arithmetic_size(operator, flags, d1, d2);
+      out=arithmetic_makenew(d1);
       break;
 
     /* When the operator is not recognized. */
diff --git a/lib/data.c b/lib/data.c
index 2d60805..e22b50e 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -327,10 +327,10 @@ gal_data_initialize(gal_data_t *data, void *array, 
uint8_t type,
               availableram=data_available_ram();
               bytesize=gal_type_sizeof(type)*data->size;
 
-              /* For a check: */
+              /* For a check:
               printf("check: %zu (data), %zu (ram)\n",
                      bytesize, availableram-nouseram);
-
+              */
               /* If the final size is larger than the user's maximum, or is
                  larger than the available memory minus 500Mb (to leave the
                  system some breathing space!), then read the array into
diff --git a/lib/gnuastro/arithmetic.h b/lib/gnuastro/arithmetic.h
index b3f97ad..24721c7 100644
--- a/lib/gnuastro/arithmetic.h
+++ b/lib/gnuastro/arithmetic.h
@@ -145,6 +145,8 @@ enum gal_arithmetic_operators
   GAL_ARITHMETIC_OP_TO_FLOAT32,   /* Convert to float32.                   */
   GAL_ARITHMETIC_OP_TO_FLOAT64,   /* Convert to float64.                   */
 
+  GAL_ARITHMETIC_OP_MAKENEW,      /* Build a new dataset, containing zeros.*/
+
   GAL_ARITHMETIC_OP_LAST_CODE,    /* Last code of the library operands.    */
 };
 



reply via email to

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