gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master c36f753 01/19: Match: Option to work with k-d


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master c36f753 01/19: Match: Option to work with k-d trees has been defined
Date: Sun, 14 Nov 2021 20:40:57 -0500 (EST)

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

    Match: Option to work with k-d trees has been defined
    
    Things are ready to go Sachin, if you simply run a command like this
    
    astmatch input.fits --ccol1=X,Y --aperture=3 --kdtree=build
    
    it will take you to the function where you can start working
    ('match_catalog_kdtree' in 'bin/match/match.c').
---
 bin/match/args.h  | 13 ++++++++++
 bin/match/main.h  | 11 +++++++++
 bin/match/match.c | 36 ++++++++++++++++++++++++---
 bin/match/ui.c    | 73 +++++++++++++++++++++++++++++++++++--------------------
 bin/match/ui.h    |  3 ++-
 5 files changed, 106 insertions(+), 30 deletions(-)

diff --git a/bin/match/args.h b/bin/match/args.h
index 67515d1..d567c0f 100644
--- a/bin/match/args.h
+++ b/bin/match/args.h
@@ -156,6 +156,19 @@ struct argp_option program_options[] =
       GAL_OPTIONS_NOT_SET,
       gal_options_parse_csv_float64
     },
+    {
+      "kdtree",
+      UI_KEY_KDTREE,
+      "STR",
+      0,
+      "build, internal, automatic, none, FILE.",
+      UI_GROUP_CATALOGMATCH,
+      &p->kdtree,
+      GAL_TYPE_STRING,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET,
+    },
 
 
 
diff --git a/bin/match/main.h b/bin/match/main.h
index d9d0c5b..9084357 100644
--- a/bin/match/main.h
+++ b/bin/match/main.h
@@ -42,6 +42,15 @@ enum match_modes
   MATCH_MODE_CATALOG,
 };
 
+enum kdtree_modes
+{
+  MATCH_KDTREE_INVALID,           /* ==0 by default. */
+  MATCH_KDTREE_BUILD,
+  MATCH_KDTREE_INTERNAL,
+  MATCH_KDTREE_AUTO,
+  MATCH_KDTREE_DISABLE,
+  MATCH_KDTREE_FILE,
+};
 
 
 
@@ -58,6 +67,7 @@ struct matchparams
   gal_data_t           *coord;  /* Array of manual coordinate values.   */
   gal_data_t         *outcols;  /* Array of second input column names.  */
   gal_data_t        *aperture;  /* Acceptable matching aperture.        */
+  char                *kdtree;  /* The mode to use k-d tree mode.       */
   uint8_t         logasoutput;  /* Don't rearrange inputs, out is log.  */
   uint8_t          notmatched;  /* Output is rows that don't match.     */
 
@@ -73,6 +83,7 @@ struct matchparams
   char              *out1name;  /* Name of first matched output.        */
   char              *out2name;  /* Name of second matched output.       */
   gal_list_str_t  *stdinlines;  /* Lines given by Standard input.       */
+  int              kdtreemode;  /* The k-d tree mode.                   */
 
   /* Output: */
   time_t              rawtime;  /* Starting time of the program.        */
diff --git a/bin/match/match.c b/bin/match/match.c
index c31f322..e508141 100644
--- a/bin/match/match.c
+++ b/bin/match/match.c
@@ -447,6 +447,29 @@ match_catalog_write_one_col(struct matchparams *p, 
gal_data_t *a,
 
 
 
+/* Wrapper over the k-d tree library to return an output in the same format
+   as 'gal_match_coordinates'. If this funciton  */
+static gal_data_t *
+match_catalog_kdtree(struct matchparams *p)
+{
+  /* The input coordinates are already available in 'p->cols1' (which is
+     already stored as double).
+
+     Also, the 'p->kdtreemode' contains the k-d tree operation mode (mode
+     codes are defined as an 'enum' in 'main.h'): you can use a
+     'switch'. If the mode isn't 'MATCH_KDTREE_BUILD', you also have the
+     second input (to match against) in 'p->cols2' (again, already stored
+     as 'double'). */
+
+  /* Remove this after it works. */
+  printf("%s: ... read to go ;-) ...\n", __func__);
+  exit(0);
+}
+
+
+
+
+
 static void
 match_catalog(struct matchparams *p)
 {
@@ -456,9 +479,16 @@ match_catalog(struct matchparams *p)
   size_t nummatched, *acolmatch=NULL, *bcolmatch=NULL;
 
   /* Find the matching coordinates. We are doing the processing in
-     place, */
-  mcols=gal_match_coordinates(p->cols1, p->cols2, p->aperture->array, 0, 1,
-                              p->cp.minmapsize, p->cp.quietmmap, &nummatched);
+     place. */
+  if(p->kdtreemode!=MATCH_KDTREE_DISABLE)
+    mcols=match_catalog_kdtree(p);
+
+  /* Incase it was decided not to use a k-d tree at all (in 'automatic'
+     mode), then we need to use the classic mode.*/
+  if(mcols==NULL)
+    mcols=gal_match_coordinates(p->cols1, p->cols2, p->aperture->array,
+                                0, 1, p->cp.minmapsize, p->cp.quietmmap,
+                                &nummatched);
 
   /* If the output is to be taken from the input columns (it isn't just the
      log), then do the job. */
diff --git a/bin/match/ui.c b/bin/match/ui.c
index ce74de2..6a5f241 100644
--- a/bin/match/ui.c
+++ b/bin/match/ui.c
@@ -26,6 +26,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <errno.h>
 #include <error.h>
 #include <stdio.h>
+#include <string.h>
 
 #include <gnuastro/fits.h>
 
@@ -212,13 +213,26 @@ parse_opt(int key, char *arg, struct argp_state *state)
 /***************       Sanity Check         *******************/
 /**************************************************************/
 /* Read and check ONLY the options. When arguments are involved, do the
-   check in 'ui_check_options_and_arguments'.
+   check in 'ui_check_options_and_arguments'. */
 static void
 ui_read_check_only_options(struct matchparams *p)
 {
-
+  /* Set the k-d tree mode. */
+  if(      !strcmp(p->kdtree,"build")    ) p->kdtreemode=MATCH_KDTREE_BUILD;
+  else if( !strcmp(p->kdtree,"internal") ) p->kdtreemode=MATCH_KDTREE_INTERNAL;
+  else if( !strcmp(p->kdtree,"auto")     ) p->kdtreemode=MATCH_KDTREE_AUTO;
+  else if( !strcmp(p->kdtree,"disable")  ) p->kdtreemode=MATCH_KDTREE_DISABLE;
+  else if( gal_fits_name_is_fits(p->kdtree) ) p->kdtreemode=MATCH_KDTREE_FILE;
+  else
+    error(EXIT_FAILURE, 0, "'%s' is not a valid value for '--kdtree'. The "
+          "following values are accepted: 'build' (to build the k-d tree in "
+          "the file given to '--output'), 'internal' (to force internal "
+          "usage of a k-d tree for the matching), 'auto' (to decide "
+          "automatically if a k-d tree should be used or not), 'disable' "
+          "(to not use a k-d tree at all), a FITS file name (the file to "
+          "read a created k-d tree from)", p->kdtree);
 }
-*/
+
 
 
 
@@ -231,12 +245,12 @@ ui_check_options_and_arguments(struct matchparams *p)
 {
   /* When '--coord' is given, there should be no second catalog
      (argument). */
-  if(p->coord)
+  if(p->coord || p->kdtreemode==MATCH_KDTREE_BUILD)
     {
       /* Make sure no second argument is given. */
       if(p->input2name)
         error(EXIT_FAILURE, 0, "only one argument can be given with the "
-              "'--coord' option");
+              "'--coord' of '--kdtree=build' options");
 
       /* In case '--ccol2' is given. */
       if(p->ccol2 && p->cp.quiet==0)
@@ -587,10 +601,10 @@ ui_read_columns_aperture_3d(struct matchparams *p)
 static size_t
 ui_set_columns_sanity_check_read_aperture(struct matchparams *p)
 {
-  size_t ccol1n, ccol2n;
+  size_t ccol1n=0, ccol2n=0;
 
-  /* Make sure the columns to read are given. */
-  if(p->coord)
+  /* Make sure the columns to match are given. */
+  if(p->coord || p->kdtreemode==MATCH_KDTREE_BUILD)
     {
       if(p->ccol1==NULL)
         error(EXIT_FAILURE, 0, "no value given to '--ccol1' (necessary with "
@@ -603,17 +617,22 @@ ui_set_columns_sanity_check_read_aperture(struct 
matchparams *p)
               "They specify the columns containing the coordinates to match");
     }
 
-  /* Make sure the same number of columns is given to both. */
+  /* Make sure the same number of columns is given to both. Note that a
+     second catalog is only necessary when we aren't building a k-d
+     tree. */
   ccol1n = p->ccol1->size;
-  ccol2n = p->coord ? p->coord->size : p->ccol2->size;
-  if(ccol1n!=ccol2n)
-    error(EXIT_FAILURE, 0, "number of coordinates given to '--ccol1' "
-          "(%zu) and '--%s' (%zu) must be equal.\n\n"
-          "If you didn't call these options, run with '--checkconfig' to "
-          "see which configuration file is responsible. You can always "
-          "override the configuration file values by calling the option "
-          "manually on the command-line",
-          ccol1n, p->coord ? "coord" : "ccol2", ccol2n);
+  if( p->kdtreemode!=MATCH_KDTREE_BUILD )
+    {
+      ccol2n = p->coord ? p->coord->size : p->ccol2->size;
+      if(ccol1n!=ccol2n)
+        error(EXIT_FAILURE, 0, "number of coordinates given to '--ccol1' "
+              "(%zu) and '--%s' (%zu) must be equal.\n\n"
+              "If you didn't call these options, run with '--checkconfig' "
+              "to see which configuration file is responsible. You can "
+              "always override the configuration file values by calling "
+              "the option manually on the command-line", ccol1n,
+              p->coord ? "coord" : "ccol2", ccol2n);
+    }
 
   /* Read/check the aperture values. */
   if(p->aperture)
@@ -750,8 +769,10 @@ ui_read_columns(struct matchparams *p)
 
   /* Convert the array of strings to a list of strings for the column
      names. */
-  strarr1=p->ccol1->array;
-  strarr2=p->coord?NULL:p->ccol2->array;
+  strarr1 = p->ccol1->array;
+  strarr2 = ( (p->coord || p->kdtreemode==MATCH_KDTREE_BUILD)
+              ? NULL
+              : p->ccol2->array );
   for(i=0;i<ndim;++i)
     {
       gal_list_str_add(&cols1, strarr1[i], 1);
@@ -763,10 +784,11 @@ ui_read_columns(struct matchparams *p)
   /* Read-in the columns. */
   p->cols1=ui_read_columns_to_double(p, p->input1name, p->cp.hdu,
                                      cols1, ndim);
-  p->cols2=( p->coord
-             ? ui_set_columns_from_coord(p)
-             : ui_read_columns_to_double(p, p->input2name, p->hdu2,
-                                         cols2, ndim) );
+  if( p->kdtreemode!=MATCH_KDTREE_BUILD )
+    p->cols2=( p->coord
+               ? ui_set_columns_from_coord(p)
+               : ui_read_columns_to_double(p, p->input2name, p->hdu2,
+                                           cols2, ndim) );
 
   /* Free the extra spaces. */
   gal_list_str_free(cols1, 1);
@@ -1047,9 +1069,8 @@ ui_read_check_inputs_setup(int argc, char *argv[], struct 
matchparams *p)
 
 
   /* Read the options into the program's structure, and check them and
-     their relations prior to printing.
+     their relations prior to printing. */
   ui_read_check_only_options(p);
-  */
 
 
   /* Print the option values if asked. Note that this needs to be done
diff --git a/bin/match/ui.h b/bin/match/ui.h
index 2c172fd..bf42f70 100644
--- a/bin/match/ui.h
+++ b/bin/match/ui.h
@@ -40,7 +40,7 @@ enum program_args_groups
 
 /* Available letters for short options:
 
-   b e f g i j k m n p r s t u v w x y z
+   b e f g i j m n p r s t u v w x y z
    A B E G J L O Q R W X Y
 */
 enum option_keys_enum
@@ -52,6 +52,7 @@ enum option_keys_enum
   UI_KEY_CCOL1           = 'c',
   UI_KEY_CCOL2           = 'C',
   UI_KEY_COORD           = 'd',
+  UI_KEY_KDTREE          = 'k',
 
   /* Only with long version (start with a value 1000, the rest will be set
      automatically). */



reply via email to

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