gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 270d299: Inputs to Match program and library m


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 270d299: Inputs to Match program and library must not be float64
Date: Mon, 4 Dec 2017 20:54:40 -0500 (EST)

branch: master
commit 270d299880ac65b55753f37b9770ebc247c50299
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Inputs to Match program and library must not be float64
    
    Until now the Match program and library needed inputs of type `float64',
    otherwise, they would abort with an error. With this commit, the match
    library only aborts with a different type when `inplace==1'. The Match
    program will also convert the types when reading them.
---
 bin/match/ui.c    | 85 ++++++++++++++++++++++++++++++++++++++++---------------
 doc/gnuastro.texi | 17 ++++++++---
 lib/match.c       | 69 +++++++++++++++++++++++++-------------------
 3 files changed, 115 insertions(+), 56 deletions(-)

diff --git a/bin/match/ui.c b/bin/match/ui.c
index b625545..c08eba2 100644
--- a/bin/match/ui.c
+++ b/bin/match/ui.c
@@ -209,12 +209,13 @@ 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)
 {
 
 }
+*/
 
 
 
@@ -386,6 +387,59 @@ ui_read_columns_aperture_2d(struct matchparams *p)
 
 
 
+/* We want to keep the columns as double type. So what-ever their original
+   type is, convert it. */
+static gal_data_t *
+ui_read_columns_to_double(struct matchparams *p, char *filename, char *hdu,
+                          gal_list_str_t *cols, size_t numcols)
+{
+  gal_data_t *tmp, *ttmp, *tout, *out=NULL;
+  struct gal_options_common_params *cp=&p->cp;
+  char *diff_cols_error="%s: the number of columns matched (%zu) "
+    "differs from the number of usable calls to `--ccol1' (%zu). "
+    "Please give more specific values to `--ccol1' (column "
+    "numberes are the only identifiers guaranteed to be unique).";
+
+  /* Read the columns. */
+  tout=gal_table_read(filename, hdu, cols, cp->searchin, cp->ignorecase,
+                     cp->minmapsize);
+
+  /* A small sanity check. */
+  if(gal_list_data_number(tout)!=numcols)
+    error(EXIT_FAILURE, 0, diff_cols_error,
+          gal_checkset_dataset_name(filename, hdu),
+          gal_list_data_number(tout), numcols);
+
+  /* Go over the columns and see if they are double or not. To keep things
+     simple, we'll keep a new list even if all the types are float64.*/
+  tmp=tout;
+  while(tmp!=NULL)
+    {
+      /* We need ot set the `next' pointer  */
+      ttmp=tmp->next;
+      tmp->next=NULL;
+
+      /* Correct the type if necessary. */
+      if(tmp->type==GAL_TYPE_FLOAT64)
+        gal_list_data_add(&out, tmp);
+      else
+        gal_list_data_add(&out,
+                          gal_data_copy_to_new_type_free(tmp,
+                                                         GAL_TYPE_FLOAT64) );
+
+      /* Set `tmp' to the initial `next pointer. */
+      tmp=ttmp;
+    }
+
+  /* The `out' above is in reverse, so correct it and return */
+  gal_list_data_reverse(&out);
+  return out;
+}
+
+
+
+
+
 /* Read catalog columns */
 static void
 ui_read_columns(struct matchparams *p)
@@ -394,12 +448,7 @@ ui_read_columns(struct matchparams *p)
   size_t ccol1n=p->ccol1->size;
   size_t ccol2n=p->ccol2->size;
   gal_list_str_t *cols1=NULL, *cols2=NULL;
-  struct gal_options_common_params *cp=&p->cp;
   char **strarr1=p->ccol1->array, **strarr2=p->ccol2->array;
-  char *diff_cols_error="%s: the number of columns matched (%zu) "
-    "differs from the number of usable calls to `--ccol1' (%zu). "
-    "Please give more specific values to `--ccol1' (column "
-    "numberes are the only identifiers guaranteed to be unique).";
 
   /* Make sure the same number of columns is given to both. */
   if(ccol1n!=ccol2n)
@@ -450,23 +499,13 @@ ui_read_columns(struct matchparams *p)
 
 
   /* Read the columns. */
-  if(cp->searchin)
+  if(p->cp.searchin)
     {
       /* Read the first dataset. */
-      p->cols1=gal_table_read(p->input1name, cp->hdu, cols1,
-                              cp->searchin, cp->ignorecase, cp->minmapsize);
-      if(gal_list_data_number(p->cols1)!=ccol1n)
-        error(EXIT_FAILURE, 0, diff_cols_error,
-              gal_checkset_dataset_name(p->input1name, cp->hdu),
-              gal_list_data_number(p->cols1), ccol1n);
-
-      /* Read the second dataset. */
-      p->cols2=gal_table_read(p->input2name, p->hdu2, cols2,
-                              cp->searchin, cp->ignorecase, cp->minmapsize);
-      if(gal_list_data_number(p->cols2)!=ccol2n)
-        error(EXIT_FAILURE, 0, diff_cols_error,
-              gal_checkset_dataset_name(p->input2name, p->hdu2),
-              gal_list_data_number(p->cols2), ccol2n);
+      p->cols1=ui_read_columns_to_double(p, p->input1name, p->cp.hdu,
+                                         cols1, ccol1n);
+      p->cols2=ui_read_columns_to_double(p, p->input2name, p->hdu2,
+                                         cols2, ccol2n);
     }
   else
     error(EXIT_FAILURE, 0, "no `--searchin' option specified. Please run "
@@ -630,9 +669,9 @@ 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
      after the option checks so un-sane values are not printed in the
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index b1f63a6..be75c4c 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -2340,8 +2340,14 @@ analysis. Here, we will use Gnuastro's programs to 
detect objects in a
 Hubble Space Telescope (HST) image and measure their colors. We will use
 the @url{https://archive.stsci.edu/prepds/xdf, eXtreme Deep Field}
 dataset. Like almost all astronomical surveys, this dataset is free for
-download and usable by the public. You will need the following tools in
-this tutorial: Gnuastro, SAO DS9 @footnote{See @ref{SAO ds9}, available at
+download and usable by the public. This tutorial was first prepared for the
+``Exploring the Ultra-Low Surface Brightness Universe'' workshop (November
+2017) at the International Space Science Institute (ISSI) in Bern,
+Switzerland. We would like to thank them and the attendees for a very
+fruiteful week.
+
+You will need the following tools in this tutorial: Gnuastro, SAO DS9
address@hidden @ref{SAO ds9}, available at
 @url{http://ds9.si.edu/site/Home.html}.}, GNU
 address@hidden@url{https://www.gnu.org/software/wget}.}, and AWK (most
 common implementation is GNU
@@ -20957,8 +20963,11 @@ linked list and thus any number of tiles can be 
represented with one
 variable.
 
 @deftypefun void gal_list_data_add (gal_data_t @code{**list}, gal_data_t 
@code{*newnode})
-Add an already allocated dataset (@code{newnode}) to top of @code{list}. In
-this example multiple images are linked together as a list:
+Add an already allocated dataset (@code{newnode}) to top of
address@hidden Note that if @code{newnode->next!=NULL} (@code{newnode} is
+itself a list), then @code{list} will be added to its end.
+
+In this example multiple images are linked together as a list:
 @example
 size_t minmapsize=-1;
 gal_data_t *tmp, *list=NULL;
diff --git a/lib/match.c b/lib/match.c
index 6c1574e..22401f1 100644
--- a/lib/match.c
+++ b/lib/match.c
@@ -115,16 +115,23 @@ match_coordinate_pop_from_sfll(struct 
match_coordinate_sfll **list,
 /* Since these checks are repetative, its easier to have a separate
    function for both inputs. */
 static void
-match_coordinate_sanity_check_columns(gal_data_t *coord, char *info)
+match_coordinate_sanity_check_columns(gal_data_t *coord, char *info,
+                                      int inplace, int *allf64)
 {
   gal_data_t *tmp;
 
   for(tmp=coord; tmp!=NULL; tmp=tmp->next)
     {
       if(tmp->type!=GAL_TYPE_FLOAT64)
-        error(EXIT_FAILURE, 0, "%s: the input coordinates must have "
-              "`float64' type. At least one node of the %s list has type "
-              "of `%s'", __func__, info, gal_type_name(tmp->type, 1));
+        {
+          if(inplace)
+            error(EXIT_FAILURE, 0, "%s: when `inplace' is activated, the "
+                  "input coordinates must have `float64' type. At least "
+                  "one node of the %s list has type of `%s'", __func__, info,
+                  gal_type_name(tmp->type, 1));
+          else
+            *allf64=0;
+        }
 
       if(tmp->ndim!=1)
         error(EXIT_FAILURE, 0, "%s: each input coordinate column must have "
@@ -146,7 +153,7 @@ match_coordinate_sanity_check_columns(gal_data_t *coord, 
char *info)
 /* To keep the main function clean, we'll do the sanity checks here. */
 static void
 match_coordinaes_sanity_check(gal_data_t *coord1, gal_data_t *coord2,
-                              double *aperture)
+                              double *aperture, int inplace, int *allf64)
 {
   size_t ncoord1=gal_list_data_number(coord1);
 
@@ -165,8 +172,8 @@ match_coordinaes_sanity_check(gal_data_t *coord1, 
gal_data_t *coord2,
           "dimensions", __func__, ncoord1);
 
   /* Check the column properties. */
-  match_coordinate_sanity_check_columns(coord1, "first");
-  match_coordinate_sanity_check_columns(coord2, "second");
+  match_coordinate_sanity_check_columns(coord1, "first", inplace, allf64);
+  match_coordinate_sanity_check_columns(coord2, "second", inplace, allf64);
 
   /* Check the aperture values. */
   if(aperture[0]<=0)
@@ -211,7 +218,7 @@ match_coordinates_prepare_sort(gal_data_t *coords, size_t 
minmapsize)
 /* Do the preparations for matching of coordinates. */
 static void
 match_coordinates_prepare(gal_data_t *coord1, gal_data_t *coord2,
-                          int sorted_by_first, int inplace,
+                          int sorted_by_first, int inplace, int allf64,
                           gal_data_t **A_out, gal_data_t **B_out,
                           size_t **A_perm, size_t **B_perm,
                           size_t minmapsize)
@@ -220,10 +227,21 @@ match_coordinates_prepare(gal_data_t *coord1, gal_data_t 
*coord2,
 
   /* Sort the datasets if they aren't sorted. If the dataset is already
      sorted, then `inplace' is irrelevant. */
-  if(!sorted_by_first)
+  if(sorted_by_first && allf64)
+    {
+      *A_out=coord1;
+      *B_out=coord2;
+    }
+  else
     {
-      /* Allocating a new list is only necessary when  */
-      if(!inplace)
+      /* Allocating a new list is only necessary when `inplace==0' or all
+         the columns are double. */
+      if( inplace && allf64 )
+        {
+          *A_out=coord1;
+          *B_out=coord2;
+        }
+      else
         {
           /* Copy the first list. */
           for(tmp=coord1; tmp!=NULL; tmp=tmp->next)
@@ -249,21 +267,11 @@ match_coordinates_prepare(gal_data_t *coord1, gal_data_t 
*coord2,
           *A_out=A;
           *B_out=B;
         }
-      else
-        {
-          *A_out=coord1;
-          *B_out=coord2;
-        }
 
       /* Sort each dataset by the first coordinate. */
       *A_perm = match_coordinates_prepare_sort(*A_out, minmapsize);
       *B_perm = match_coordinates_prepare_sort(*B_out, minmapsize);
     }
-  else
-    {
-      *A_out=coord1;
-      *B_out=coord2;
-    }
 }
 
 
@@ -667,17 +675,18 @@ gal_match_coordinates_output(gal_data_t *A, gal_data_t 
*B, size_t *A_perm,
           /* Note that the permutation keeps the original indexs. */
           match_coordinate_pop_from_sfll(&bina[ai], &bi, &r);
           rval[ match_i   ] = r;
-          aind[ match_i   ] = A_perm[ai];
-          bind[ match_i++ ] = B_perm[bi];
+          aind[ match_i   ] = A_perm ? A_perm[ai] : ai;
+          bind[ match_i++ ] = B_perm ? B_perm[bi] : bi;
 
           /* Set a `1' for this object in the second catalog. This will
              later be used to find which rows didn't match to fill in the
              output.. */
-          Bmatched[ B_perm[bi] ] = 1;
+          Bmatched[ B_perm ? B_perm[bi] : bi ] = 1;
         }
+
       /* No match found. At this stage, we can only fill the indexs of the
          first input. The second input needs to be matched afterwards.*/
-      else aind[ nomatch_i++ ] = A_perm[ai];
+      else aind[ nomatch_i++ ] = A_perm ? A_perm[ai] : ai;
     }
 
 
@@ -748,14 +757,16 @@ gal_match_coordinates(gal_data_t *coord1, gal_data_t 
*coord2,
                       double *aperture, int sorted_by_first,
                       int inplace, size_t minmapsize, size_t *nummatched)
 {
+  int allf64=1;
   gal_data_t *A, *B, *out;
   size_t *A_perm=NULL, *B_perm=NULL;
   struct match_coordinate_sfll **bina;
 
   /* Do a small sanity check and make the preparations. After this point,
      we'll call the two arrays `a' and `b'.*/
-  match_coordinaes_sanity_check(coord1, coord2, aperture);
-  match_coordinates_prepare(coord1, coord2, sorted_by_first, inplace,
+  match_coordinaes_sanity_check(coord1, coord2, aperture, inplace,
+                                &allf64);
+  match_coordinates_prepare(coord1, coord2, sorted_by_first, inplace, allf64,
                             &A, &B, &A_perm, &B_perm, minmapsize);
 
 
@@ -784,13 +795,13 @@ gal_match_coordinates(gal_data_t *coord1, gal_data_t 
*coord2,
 
   /* Clean up. */
   free(bina);
-  free(A_perm);
-  free(B_perm);
   if(A!=coord1)
     {
       gal_list_data_free(A);
       gal_list_data_free(B);
     }
+  if(A_perm) free(A_perm);
+  if(B_perm) free(B_perm);
 
 
   /* Set `nummatched' and return output. */



reply via email to

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