gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 0bc2af58: Library (convolve.c): defined convol


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0bc2af58: Library (convolve.c): defined convolve_spatial function separately
Date: Thu, 4 Jan 2024 12:55:41 -0500 (EST)

branch: master
commit 0bc2af580deda1798f1609fa361ced5e52f29568
Author: Faezeh Bidjarchian <fbidjarchian@gmail.com>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Library (convolve.c): defined convolve_spatial function separately
    
    Until now, calculations for spatial convolution were done in the main
    high-level 'convolve' function. This caused this high-level function to
    become long, decreasing the readability and modularity (un-necessarily
    going to low-level spatial convolution issues; while it is shared with
    frequency domain). Additionally, the 'multivalue' option of the TEMPLATE
    program was just a basic linked list, not allowing multiple calls to the
    same option.
    
    With this commit, a new function for spatial convolution called
    'convolve_spatial' has been defined separately. Also, the
    'gal_options_parse_csv_strings_append' function has been added to the
    multi-value option structure of the TEMPLATE program in its 'args.h'. This
    will help developers consider this useful option in their programs in the
    future.
---
 bin/TEMPLATE/args.h     |  3 +-
 bin/convolve/convolve.c | 91 +++++++++++++++++++++++++++----------------------
 bin/convolve/main.c     |  2 +-
 bin/convolve/ui.c       |  8 ++---
 4 files changed, 57 insertions(+), 47 deletions(-)

diff --git a/bin/TEMPLATE/args.h b/bin/TEMPLATE/args.h
index e6a6e18e..104cfa98 100644
--- a/bin/TEMPLATE/args.h
+++ b/bin/TEMPLATE/args.h
@@ -42,7 +42,8 @@ struct argp_option program_options[] =
       GAL_TYPE_STRLL,
       GAL_OPTIONS_RANGE_ANY,
       GAL_OPTIONS_NOT_MANDATORY,
-      GAL_OPTIONS_NOT_SET
+      GAL_OPTIONS_NOT_SET,
+      gal_options_parse_csv_strings_append
     },
 
 
diff --git a/bin/convolve/convolve.c b/bin/convolve/convolve.c
index 122b4585..af028a9b 100644
--- a/bin/convolve/convolve.c
+++ b/bin/convolve/convolve.c
@@ -260,7 +260,7 @@ removepaddingcorrectroundoff(struct convolveparams *p)
      hi1 are the coordinates of the first pixel in the output image. In the
      case of deconvolution, if the maximum radius is larger than the input
      image, we will also only be using region that contains non-zero rows
-     and columns.*/
+     and columns. */
   if(p->makekernel)
     {
       hi0      = mkwidth < isize[0] ? p->ps0/2-p->makekernel : 0;
@@ -305,7 +305,7 @@ fftinitializer(struct convolveparams *p, struct 
fftonthreadparams **outfp)
   size_t i;
   struct fftonthreadparams *fp;
 
-  /* Allocate the fftonthreadparams array.  */
+  /* Allocate the fftonthreadparams array. */
   errno=0;
   *outfp=fp=malloc(p->cp.numthreads*sizeof *fp);
   if(fp==NULL)
@@ -369,7 +369,7 @@ correctdeconvolve(struct convolveparams *p, double 
**spatial)
   /* First convert the complex image to a real image: */
   complextoreal(p->pimg, ps0*ps1, COMPLEX_TO_REAL_SPEC, &s);
 
-  /* Allocate the array to keep the new values */
+  /* Allocate the array to keep the new values. */
   errno=0;
   n=malloc(ps0*ps1*sizeof *n);
   if(n==NULL)
@@ -404,7 +404,7 @@ correctdeconvolve(struct convolveparams *p, double 
**spatial)
           r=sqrt( (ii-ci)*(ii-ci) + (jj-cj)*(jj-cj) );
           sum += n[ii*ps1+jj] = r < p->makekernel ? s[i*ps1+j] : 0;
 
-          /*printf("(%zu, %zu) --> (%zu, %zu)\n", i, j, ii, jj);*/
+          /*printf("(%zu, %zu) --> (%zu, %zu)\n", i, j, ii, jj); */
         }
     }
 
@@ -413,7 +413,7 @@ correctdeconvolve(struct convolveparams *p, double 
**spatial)
   df=(d=n)+ps0*ps1; do *d++/=sum; while(d<df);
 
 
-  /* Clean up: */
+  /* Clean up. */
   free(s);
   *spatial=n;
 }
@@ -448,7 +448,7 @@ correctdeconvolve(struct convolveparams *p, double 
**spatial)
   p->s1. In this case, those index values which are smaller than p->s0
   or p->s1 belong to the input image and those which are equal or
   larger than larger belong to the kernel image (after subtraction for
-  p->s0 or p->s1).*/
+  p->s0 or p->s1). */
 void *
 onedimensionfft(void *inparam)
 {
@@ -754,6 +754,48 @@ convolve_frequency(struct convolveparams *p)
 
 
 
+void
+convolve_spatial(struct convolveparams *p)
+{
+  gal_data_t *out, *check;
+  int multidim=p->input->ndim>1;
+  struct gal_options_common_params *cp=&p->cp;
+
+
+  /* Prepare the mesh structure. */
+  if(multidim) gal_tile_full_two_layers(p->input, &cp->tl);
+
+  /* Save the tile IDs if they are requested. */
+  if(multidim && cp->tl.tilecheckname)
+    {
+      check=gal_tile_block_check_tiles(cp->tl.tiles);
+      gal_fits_img_write(check, cp->tl.tilecheckname, NULL, 0);
+      gal_data_free(check);
+    }
+
+  /* Do the spatial convolution. One of the main reason someone would
+     want to do spatial domain convolution with this Convolve program
+     is edge correction. So by default we assume it and will only
+     ignore it if the user asks. */
+  out=gal_convolve_spatial(multidim ? cp->tl.tiles : p->input,
+                           p->kernel,
+                           cp->numthreads,
+                           multidim ? !p->noedgecorrection : 1,
+                           multidim ? cp->tl.workoverch : 1,
+                           p->conv_on_blank);
+
+  /* Clean up: free the actual input and replace it's pointer with the
+     convolved dataset to save as output. */
+  gal_tile_full_free_contents(&cp->tl);
+  gal_data_free(p->input);
+  p->input=out;
+}
+
+
+
+
+
+
 
 
 
@@ -774,44 +816,11 @@ convolve_frequency(struct convolveparams *p)
 void
 convolve(struct convolveparams *p)
 {
-  gal_data_t *out, *check;
-  int multidim=p->input->ndim>1;
   struct gal_options_common_params *cp=&p->cp;
 
-
   /* Do the convolution. */
-  if(p->domain==CONVOLVE_DOMAIN_SPATIAL)
-    {
-      /* Prepare the mesh structure. */
-      if(multidim) gal_tile_full_two_layers(p->input, &cp->tl);
-
-      /* Save the tile IDs if they are requested. */
-      if(multidim && cp->tl.tilecheckname)
-        {
-          check=gal_tile_block_check_tiles(cp->tl.tiles);
-          gal_fits_img_write(check, cp->tl.tilecheckname, NULL, 0);
-          gal_data_free(check);
-        }
-
-      /* Do the spatial convolution. One of the main reason someone would
-         want to do spatial domain convolution with this Convolve program
-         is edge correction. So by default we assume it and will only
-         ignore it if the user asks.*/
-      out=gal_convolve_spatial(multidim ? cp->tl.tiles : p->input,
-                               p->kernel,
-                               cp->numthreads,
-                               multidim ? !p->noedgecorrection : 1,
-                               multidim ? cp->tl.workoverch : 1,
-                               p->conv_on_blank);
-
-      /* Clean up: free the actual input and replace it's pointer with the
-         convolved dataset to save as output. */
-      gal_tile_full_free_contents(&cp->tl);
-      gal_data_free(p->input);
-      p->input=out;
-    }
-  else
-    convolve_frequency(p);
+  if(p->domain==CONVOLVE_DOMAIN_SPATIAL) convolve_spatial(p);
+  else                                   convolve_frequency(p);
 
   /* Write Convolve's parameters as keywords into the first extension of
      the output. */
diff --git a/bin/convolve/main.c b/bin/convolve/main.c
index 7ac2e1d3..48000ed8 100644
--- a/bin/convolve/main.c
+++ b/bin/convolve/main.c
@@ -38,7 +38,7 @@ main(int argc, char *argv[])
   struct timeval t1;
   struct convolveparams p={{{0},0},0};
 
-  /* Set the starting time.*/
+  /* Set the starting time. */
   time(&p.rawtime);
   gettimeofday(&t1, NULL);
 
diff --git a/bin/convolve/ui.c b/bin/convolve/ui.c
index 860d0024..cc12690c 100644
--- a/bin/convolve/ui.c
+++ b/bin/convolve/ui.c
@@ -147,7 +147,7 @@ parse_opt(int key, char *arg, struct argp_state *state)
 {
   struct convolveparams *p = state->input;
 
-  /* Pass 'gal_options_common_params' into the child parser.  */
+  /* Pass 'gal_options_common_params' into the child parser. */
   state->child_inputs[0] = &p->cp;
 
   /* In case the user incorrectly uses the equal sign (for example
@@ -575,7 +575,7 @@ ui_preparations(struct convolveparams *p)
               "on 1D datasets");
       else
         {
-          /* Make sure the size of the kernel is the same as the input */
+          /* Make sure the size of the kernel is the same as the input. */
           if( p->input->dsize[0]!=p->kernel->dsize[0]
               || p->input->dsize[1]!=p->kernel->dsize[1] )
             error(EXIT_FAILURE, 0, "with the '--makekernel' ('-m') option, "
@@ -628,7 +628,7 @@ ui_preparations(struct convolveparams *p)
 
       /* If there are any NaN pixels, set them to zero and normalize it. A
          blank pixel in a kernel is going to make a completely blank
-         output.*/
+         output. */
       if( !p->nokernelnorm )
         {
           sumv=0;
@@ -732,7 +732,7 @@ ui_read_check_inputs_setup(int argc, char *argv[], struct 
convolveparams *p)
 #include "args.h"
 
 
-  /* Initialize the options and necessary information.  */
+  /* Initialize the options and necessary information. */
   ui_initialize_options(p, program_options, gal_commonopts_options);
 
 



reply via email to

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