[Top][All Lists]

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

[gnuastro-commits] master 1ff1c25 1/2: Arithmetic works with only one op

From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 1ff1c25 1/2: Arithmetic works with only one operand
Date: Mon, 24 Jul 2017 11:14:37 -0400 (EDT)

branch: master
commit 1ff1c252e77b9789dcd0c824e77f87f48e8c00fa
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Arithmetic works with only one operand
    Until now, when Arithmetic was run with only one image and no operators it
    would crash. This was because of the behavior we have defined for
    efficiency: an image is only actually read into memory when it is ready to
    be operated on by an operator, not when it is confronted. In this way, when
    processing a large number of files, at any moment only the ones that are
    actually being operated on are in memory.
    Cases like this (when there is only one file operand and no operator) can
    happen in scripts. So when there was no operator, the contents of the image
    were not read into memory and we got a segmentation fault. To fix the
    problem a check was added after all the input tokens (operators and
    operands) were read and applied, so if the dataset is NULL, it reads it
    into memory.
    This fixes bug #51559.
 bin/arithmetic/arithmetic.c | 28 ++++++++++++++++++++++++++--
 bin/arithmetic/operands.c   |  6 +++---
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 1456a12..a82e256 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -29,6 +29,7 @@ along with Gnuastro. If not, see 
 #include <string.h>
 #include <stdlib.h>
+#include <gnuastro/wcs.h>
 #include <gnuastro/fits.h>
 #include <gnuastro/arithmetic.h>
@@ -132,6 +133,7 @@ void
 reversepolish(struct arithmeticparams *p)
   int op=0, nop=0;
+  char *filename, *hdu;
   unsigned int numop, i;
   gal_list_str_t *token;
   gal_data_t *d1=NULL, *d2=NULL, *d3=NULL;
@@ -333,12 +335,34 @@ reversepolish(struct arithmeticparams *p)
     error(EXIT_FAILURE, 0, "too many operands");
-  /* Set the output type. */
-  d1=p->operands->data;
+  /* If the final operand has a filename, but it `data' element is NULL,
+     then the file hasn't actually be read yet. In this case, we need to
+     read the contents of the file and put the resulting dataset into the
+     operands `data' element. This can happen for example if no operators
+     are called and there is only one filename as an argument (which can
+     happen in scripts). */
+  if(p->operands->data==NULL && p->operands->filename)
+    {
+      /* Read the desired image and report it if necessary. */
+      hdu=p->operands->hdu;
+      filename=p->operands->filename;
+      if( gal_fits_name_is_fits(filename) )
+        {
+          p->operands->data=gal_fits_img_read(filename,hdu,p->cp.minmapsize);
+          p->refdata.wcs=gal_wcs_read(filename, hdu, 0, 0, &p->refdata.nwcs);
+          if(!p->cp.quiet) printf(" - %s (hdu %s) is read.\n", filename, hdu);
+        }
+      else
+        error(EXIT_FAILURE, 0, "%s: a bug! please contact us at %s to fix "
+              "the problem. While `operands->data' is NULL, the filename "
+              "(`%s') is not recognized as a FITS file", __func__,
+              PACKAGE_BUGREPORT, filename);
+    }
   /* If the final data structure has more than one element, write it as a
      FITS file. Otherwise, print it in the standard output. */
+  d1=p->operands->data;
       /* To simplify the printing process, we will first change it to
diff --git a/bin/arithmetic/operands.c b/bin/arithmetic/operands.c
index c069f34..585ea0b 100644
--- a/bin/arithmetic/operands.c
+++ b/bin/arithmetic/operands.c
@@ -110,8 +110,8 @@ operands_pop(struct arithmeticparams *p, char *operator)
   /* If the operand linked list has finished, then give an error and
      exit. */
-    error(EXIT_FAILURE, 0, "not enough operands for the \"%s\" "
-          "operator", operator);
+    error(EXIT_FAILURE, 0, "not enough operands for the \"%s\" operator",
+          operator);
   /* Set the dataset. If filename is present then read the file
@@ -126,7 +126,7 @@ operands_pop(struct arithmeticparams *p, char *operator)
         p->refdata.wcs=gal_wcs_read(filename, hdu, 0, 0, &p->refdata.nwcs);
-      /* Read the input image as a double type array. */
+      /* Read the dataset. */
       data=gal_fits_img_read(filename, hdu, p->cp.minmapsize);
       /* When the reference data structure's dimensionality is non-zero, it

reply via email to

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