gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 0ba08c1c: Crop: returned value to shell is non


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0ba08c1c: Crop: returned value to shell is non-zero if no outputs made
Date: Mon, 8 Apr 2024 15:40:52 -0400 (EDT)

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

    Crop: returned value to shell is non-zero if no outputs made
    
    Until now, when the Crop program was given a crop that did not overlap with
    the input image(s), it would not produce any output, and return control
    back to the shell successfully! However, this is not expected behavior in
    the command-line and many shell scripts or pipelines depend on the return
    value for the evaluation of their next stages.
    
    With this commit, when no outputs are created by crop, it will return a
    value of 0; allowing easy usage of the shell's conditionals to adjust
    automatic pipelines.
    
    This bug was reported by Sepideh Eskandarlou.
    
    This fixes bug #65571.
---
 NEWS               |  3 +++
 bin/crop/crop.c    | 29 ++++++++++++++++++++++-------
 bin/crop/crop.h    |  2 +-
 bin/crop/main.c    |  5 +++--
 bin/crop/main.h    |  1 +
 bin/crop/onecrop.h |  2 +-
 doc/gnuastro.texi  |  6 ++++++
 7 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/NEWS b/NEWS
index 40471b25..cb0aa805 100644
--- a/NEWS
+++ b/NEWS
@@ -141,6 +141,9 @@ See the end of the file for license conditions.
   - bug #65494: Crop segmentation fault with older CFITSIOs (three word
     version format). Reported by Giacomo Lorenzetti.
 
+  - bug #65571: Crop returns successfully when no output was made. Reported
+    by Sepideh Eskandarlou.
+
 
 
 
diff --git a/bin/crop/crop.c b/bin/crop/crop.c
index 229799da..2be219e1 100644
--- a/bin/crop/crop.c
+++ b/bin/crop/crop.c
@@ -255,7 +255,8 @@ crop_mode_img(void *inparam)
         }
       else crp->centerfilled=0;
 
-      /* Report the status on stdout if verbose mode is requested. */
+      /* Status update (for return value and standard output or log.*/
+      p->outmade[crp->out_ind] = crp->outfits!=NULL;
       if(!p->cp.quiet) crop_verbose_info(crp);
       if(p->cp.log)    crop_write_to_log(crp);
     }
@@ -361,8 +362,8 @@ crop_mode_wcs(void *inparam)
         }
 
 
-
-      /* Report the status on stdout if verbose mode is requested. */
+      /* Status update (for return value and standard output or log.*/
+      p->outmade[crp->out_ind] = crp->outfits!=NULL;
       if(!p->cp.quiet) crop_verbose_info(crp);
       if(p->cp.log)    crop_write_to_log(crp);
     }
@@ -399,13 +400,13 @@ crop_mode_wcs(void *inparam)
    crop box from each input image is desired, the first and last
    pixels are already set, irrespective of how the user specified that
    box. */
-void
+int
 crop(struct cropparams *p)
 {
-  int err=0;
   char *tmp;
   pthread_t t; /* We don't use the thread id, so all are saved here. */
   char *mmapname;
+  int err=0, out;
   pthread_attr_t attr;
   pthread_barrier_t b;
   struct onecropparams *crp;
@@ -419,13 +420,16 @@ crop(struct cropparams *p)
   modefunction = p->mode==IMGCROP_MODE_IMG ? &crop_mode_img : &crop_mode_wcs;
 
 
-  /* Allocate the array of structures to keep the thread and parameters for
-     each thread. */
+  /* Necessary allocations: the array of structures to keep the thread and
+     parameters for each thread and an array to keep track of the built
+     outputs. */
   errno=0;
   crp=malloc(nt*sizeof *crp);
   if(crp==NULL)
     error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for 'crp'",
           __func__, nt*sizeof *crp);
+  p->outmade=gal_pointer_allocate(GAL_TYPE_UINT8, p->numin, 1, __func__,
+                                  "p->outmade");
 
 
   /* Distribute the indexs into the threads (for clarity, this is needed
@@ -489,9 +493,20 @@ crop(struct cropparams *p)
       gal_list_str_free(comments, 1);
     }
 
+
+  /* Prepare the return value: if any outputs were made, return
+     EXIT_SUCCESS, otherwise, return EXIT_FAILURE. */
+  out=EXIT_FAILURE;
+  for(i=0;i<p->numin;++i) if(p->outmade[i]) { out=EXIT_SUCCESS; break; }
+
+
   /* Print the final verbose info, save log, and clean up: */
   if(mmapname) gal_pointer_mmap_free(&mmapname, p->cp.quietmmap);
   else         free(indexs);
   crop_verbose_final(p);
+  free(p->outmade);
   free(crp);
+
+  /* Return with the progarm's return value. */
+  return out;
 }
diff --git a/bin/crop/crop.h b/bin/crop/crop.h
index 98c46709..569670bc 100644
--- a/bin/crop/crop.h
+++ b/bin/crop/crop.h
@@ -23,7 +23,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #ifndef IMGMODE_H
 #define IMGMODE_H
 
-void
+int
 crop(struct cropparams *p);
 
 #endif
diff --git a/bin/crop/main.c b/bin/crop/main.c
index f9604643..d4e2d258 100644
--- a/bin/crop/main.c
+++ b/bin/crop/main.c
@@ -36,6 +36,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 int
 main (int argc, char *argv[])
 {
+  int out;
   struct timeval t1;
   struct cropparams p={{{0},0},0};
 
@@ -47,11 +48,11 @@ main (int argc, char *argv[])
   ui_read_check_inputs_setup(argc, argv, &p);
 
   /* Run Image Crop. */
-  crop(&p);
+  out=crop(&p);
 
   /* Free all non-freed allocations. */
   ui_free_report(&p, &t1);
 
   /* Return successfully. */
-  return EXIT_SUCCESS;
+  return out;
 }
diff --git a/bin/crop/main.h b/bin/crop/main.h
index acd4a0dd..678993d0 100644
--- a/bin/crop/main.h
+++ b/bin/crop/main.h
@@ -121,6 +121,7 @@ struct cropparams
   void          *blankptrwrite;  /* Null value for writing of output type.*/
   struct inputimgs       *imgs;  /* WCS and size information for inputs.  */
   gal_data_t              *log;  /* Log file contents.                    */
+  uint8_t             *outmade;  /* Array showing if each output was made.*/
   int            oneelemstdout;  /* Print one element crops on stdout.    */
 };
 
diff --git a/bin/crop/onecrop.h b/bin/crop/onecrop.h
index 2506d65c..704861ae 100644
--- a/bin/crop/onecrop.h
+++ b/bin/crop/onecrop.h
@@ -49,7 +49,7 @@ struct onecropparams
   double      equatorcorr[2];  /* Crop crosses the equator, see wcsmode.c. */
   fitsfile          *outfits;  /* Pointer to the output FITS image.        */
 
-  /* For log */
+  /* For log or return value */
   char                 *name;  /* Filename of crop.                        */
   size_t              numimg;  /* Number of images used to make this crop. */
   unsigned char centerfilled;  /* ==1 if the center is filled.             */
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 0a505406..5f95a867 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -20769,6 +20769,12 @@ When only one crop is desired, the value to 
@option{--output} will be read as a
 If no output is specified or if it is a directory, the output file name will 
follow the automatic output names of Gnuastro, see @ref{Automatic output}: The 
string given to @option{--suffix} will be replaced with the @file{.fits} suffix 
of the input.
 @end itemize
 
+When the desired crop is not within the input image(s) crop will not produce 
any image for that crop.
+If the @option{--quiet} option is not given, crop will report which one of the 
inputs was created and which wasn't (due to an overlap or the center being 
empty, see @option{--checkcenter}.
+This information can be formally written in an optional log-file also, see 
below.
+At the end, Crop will return successfully to the shell if at least one output 
file was created.
+If no output was created at all, then crop will return to the shell with a 
failure.
+
 By default, as suggested by the FITS standard and implemented in all Gnuastro 
programs, the first/primary extension of the output files will only contain 
metadata.
 The cropped images/cubes will be written into the 2nd HDU of their respective 
FITS file (which is actually counted as @code{1} because HDU counting starts 
from @code{0}).
 However, if you want the cropped data to be written into the primary (0-th) 
HDU, run Crop with the @option{--primaryimghdu} option.



reply via email to

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