[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 4068336 1/2: Library(arithmetic): new size ope
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 4068336 1/2: Library(arithmetic): new size operator for dataset's dimensions size |
Date: |
Sun, 8 Mar 2020 10:09:44 -0400 (EDT) |
branch: master
commit 4068336de5918c0c8223c0ec18ad6ad9a2ddbf5d
Author: Sachin Kumar Singh <address@hidden>
Commit: Sachin Kumar Singh <address@hidden>
Library(arithmetic): new size operator for dataset's dimensions size
The size operator prints the size of a dataset/HDU along a given
dimension. This only needs the metadata to operate, which improves its
efficiency.
---
.gitignore | 3 ++
doc/gnuastro.texi | 15 +++++++++
lib/arithmetic.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++
lib/gnuastro/arithmetic.h | 2 ++
4 files changed, 103 insertions(+)
diff --git a/.gitignore b/.gitignore
index 5d38b96..c13ddaa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -160,3 +160,6 @@ bin/buildprog/astbuildprog.conf
# directory, we have abandoned the convention above.
/bootstrapped/build-aux
+
+# vscode
+.vscode/
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index aecf724..53b2a9e 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -10154,6 +10154,15 @@ For each pixel, find the sigma-clipped standard
deviation in all given datasets.
The output will have the a single-precision (32-bit) floating point type.
This operator is called similar to the @command{sigclip-number} operator,
please see there for more.
+@item size
+Size of the dataset along a given dimension.
+The output will be a single unsigned integer(dimensions cannot be negative).
+For example, the following command will produce the dimension of the
hdu1(default) in the first operand(a.fits) along the second axis.
+
+@example
+astarithmetic a.fits 2 size
+@end example
+
@item filter-mean
Apply mean filtering (or @url{https://en.wikipedia.org/wiki/Moving_average,
moving average}) on the input dataset.
During mean filtering, each pixel (data element) is replaced by the mean value
of all its surrounding pixels (excluding blank values).
@@ -22265,6 +22274,12 @@ the termination criteria (see @ref{Sigma clipping}).
The output type of
for the rest it will be @code{GAL_TYPE_FLOAT32}.
@end deffn
+@deffn Macro GAL_ARITHMETIC_OP_SIZE
+Size operator that will return a single value for datasets of any kind. When
@code{gal_arithmetic} is called with this operator, it requires two arguments.
+The first is the dataset, and the second is a single integer value.
+The output type is a single integer.
+@end deffn
+
@deffn Macro GAL_ARITHMETIC_OP_POW
Binary operator to-power operator. When @code{gal_arithmetic} is called
with any of these operators, it will expect two operands: raising the first
diff --git a/lib/arithmetic.c b/lib/arithmetic.c
index 9cb21e4..b759428 100644
--- a/lib/arithmetic.c
+++ b/lib/arithmetic.c
@@ -551,6 +551,77 @@ arithmetic_from_statistics(int operator, int flags,
gal_data_t *input)
/***********************************************************************/
+/*************** Metadata **************/
+/***********************************************************************/
+
+/* The size operator. Reports the size along a given dimension. */
+static gal_data_t *
+arithmetic_size(int operator, int flags, gal_data_t *in, gal_data_t *arg)
+{
+ gal_data_t *out=NULL;
+ size_t *out_arr;
+ size_t arg_val, temp_dsize=1;
+ size_t *dsize=&temp_dsize;
+
+ /* Check the type */
+ switch(arg->type)
+ {
+ case GAL_TYPE_FLOAT32:
+ case GAL_TYPE_FLOAT64:
+ error(EXIT_FAILURE, 0, "%s: size "
+ "operator can only work on integer types", __func__);
+ }
+
+ /* Convert `arg` to GAL_TYPE_SIZE_T */
+ arg=gal_data_copy_to_new_type_free(arg, GAL_TYPE_SIZE_T);
+
+ arg_val=*(size_t *)(arg->array);
+
+ /* Allocate size for the output data to make changes.
+ Set the output array value. */
+ dsize[0]=temp_dsize;
+ out=gal_data_alloc(NULL, GAL_TYPE_UINT64, 1, dsize,
+ NULL, 0, in->minmapsize, 0,
+ NULL, NULL, NULL);
+
+ out_arr=out->array;
+ out_arr[0]=in->dsize[arg_val-1];
+
+ /* The first argument must be a single integer. */
+ if(arg->size != 1)
+ {
+ /* If an image/array is passed, report error. */
+ error(EXIT_FAILURE, 0, "%s: first argument must be a single integer. ",
+ __func__);
+ }
+
+ /* Clean up and return */
+ if( flags & GAL_ARITHMETIC_FREE)
+ gal_data_free(in);
+
+ return out;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/***********************************************************************/
/*************** Where **************/
/***********************************************************************/
/* When the `iftrue' dataset only has one element and the element is blank,
@@ -1694,6 +1765,10 @@ gal_arithmetic_set_operator(char *string, size_t
*num_operands)
else if (!strcmp(string, "sigclip-std"))
{ op=GAL_ARITHMETIC_OP_SIGCLIP_STD; *num_operands=-1; }
+ /* The size operator */
+ else if (!strcmp(string, "size"))
+ { op=GAL_ARITHMETIC_OP_SIZE; *num_operands=2; }
+
/* Conditional operators. */
else if (!strcmp(string, "lt" ))
{ op=GAL_ARITHMETIC_OP_LT; *num_operands=2; }
@@ -1821,6 +1896,8 @@ gal_arithmetic_operator_string(int operator)
case GAL_ARITHMETIC_OP_SIGCLIP_MEAN: return "sigclip-mean";
case GAL_ARITHMETIC_OP_SIGCLIP_STD: return "sigclip-number";
+ case GAL_ARITHMETIC_OP_SIZE: return "size";
+
case GAL_ARITHMETIC_OP_TO_UINT8: return "uchar";
case GAL_ARITHMETIC_OP_TO_INT8: return "char";
case GAL_ARITHMETIC_OP_TO_UINT16: return "ushort";
@@ -1978,6 +2055,12 @@ gal_arithmetic(int operator, size_t numthreads, int
flags, ...)
out=arithmetic_change_type(d1, operator, flags);
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;
/* When the operator is not recognized. */
default:
diff --git a/lib/gnuastro/arithmetic.h b/lib/gnuastro/arithmetic.h
index b3c61eb..55fb624 100644
--- a/lib/gnuastro/arithmetic.h
+++ b/lib/gnuastro/arithmetic.h
@@ -127,6 +127,8 @@ enum gal_arithmetic_operators
GAL_ARITHMETIC_OP_SIGCLIP_MEDIAN,/* Sigma-clipped median of mult. arrays.*/
GAL_ARITHMETIC_OP_SIGCLIP_STD, /* Sigma-clipped STD of multiple arrays. */
+ GAL_ARITHMETIC_OP_SIZE, /* Size of the dataset along an axis */
+
GAL_ARITHMETIC_OP_TO_UINT8, /* Convert to uint8_t. */
GAL_ARITHMETIC_OP_TO_INT8, /* Convert to int8_t. */
GAL_ARITHMETIC_OP_TO_UINT16, /* Convert to uint16_t. */