gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master ceeb8a0 1/2: Reasonable Match output when ther


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master ceeb8a0 1/2: Reasonable Match output when there isn't any match
Date: Thu, 12 Jul 2018 20:21:08 -0400 (EDT)

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

    Reasonable Match output when there isn't any match
    
    Until now, When there isn't any match, no output would be made, even when
    the `--nomatched' option is called. But in the latter case, the user
    expects to see an output containing the rows that didn't match (the full
    contents of both catalogs).
    
    This is not the expected behavior. With this commit, when there is no match
    (and the matched columns are requested), an output will be made and will
    contain zero rows. When the `--notmatched' column is requested, the full
    input datasets will be put in the two columns.
    
    As part of this check, I also found out that that the
    `gal_dimension_is_different' cannot handle the cases when the size is zero
    (and `dsize' might be NULL). So this function was also modified to deal
    with such cases.
    
    This bug was found and reported by Dmitry Oparin.
    
    This fixes bug #54297.
---
 NEWS              |   1 +
 bin/match/match.c | 198 +++++++++++++++++++++++++++++-------------------------
 doc/gnuastro.texi |  29 ++++----
 lib/dimension.c   |  12 ++--
 4 files changed, 130 insertions(+), 110 deletions(-)

diff --git a/NEWS b/NEWS
index d7b9892..40f2773 100644
--- a/NEWS
+++ b/NEWS
@@ -40,6 +40,7 @@ GNU Astronomy Utilities NEWS                          -*- 
outline -*-
   bug #54284: Crop segrault when catalog contains no data.
   bug #54285: make check fails if g++ not present.
   bug #54286: BuildProgram's configuration file, not built by default.
+  bug #54297: No Match output when --notmatched called and no match.
 
 
 
diff --git a/bin/match/match.c b/bin/match/match.c
index bd659c3..44c685c 100644
--- a/bin/match/match.c
+++ b/bin/match/match.c
@@ -72,31 +72,46 @@ match_catalog_read_write_all(struct matchparams *p, size_t 
*permutation,
 
 
   /* Go over each column and permute its contents. */
-  for(tmp=cat; tmp!=NULL; tmp=tmp->next)
-    {
-      /* Do the permutation. */
-      gal_permutation_apply(tmp, permutation);
-
-      /* Correct the size of the array so only the matching columns are
-         saved as output. This is only Gnuastro's convention, it has no
-         effect on later freeing of the array in the memory. */
-      if(p->notmatched)
-        {
-          /* Add the original array pointer to a list (we need to reset it
-             later). */
-          gal_list_void_add(&arrays, tmp->array);
-
-          /* Reset the data structure's array element to start where the
-             non-matched elements start. */
-          tmp->array=gal_pointer_increment(tmp->array, nummatched, tmp->type);
-
-          /* Correct the size of the tile. */
-          tmp->size = tmp->dsize[0] = tmp->size - nummatched;
-        }
-      else
-        tmp->size=tmp->dsize[0]=nummatched;
-    }
+  if(permutation)
+    for(tmp=cat; tmp!=NULL; tmp=tmp->next)
+      {
+        /* Do the permutation. */
+        gal_permutation_apply(tmp, permutation);
+
+        /* Correct the size of the array so only the matching columns are
+           saved as output. This is only Gnuastro's convention, it has no
+           effect on later freeing of the array in the memory. */
+        if(p->notmatched)
+          {
+            /* Add the original array pointer to a list (we need to reset it
+               later). */
+            gal_list_void_add(&arrays, tmp->array);
+
+            /* Reset the data structure's array element to start where the
+               non-matched elements start. */
+            tmp->array=gal_pointer_increment(tmp->array, nummatched,
+                                             tmp->type);
+
+            /* Correct the size of the tile. */
+            tmp->size = tmp->dsize[0] = tmp->size - nummatched;
+          }
+        else
+          tmp->size=tmp->dsize[0]=nummatched;
+      }
 
+  /* If no match was found (`permutation==NULL'), and the matched columns
+     are requested, empty all the columns that are to be written (only
+     keeping the meta-data). */
+  else
+    if(p->notmatched==0)
+      {
+        for(tmp=cat; tmp!=NULL; tmp=tmp->next)
+          {
+            tmp->size=0;
+            free(tmp->dsize); tmp->dsize=NULL;
+            free(tmp->array); tmp->array=NULL;
+          }
+      }
 
   /* Write the catalog to the output. */
   if(p->outcols)
@@ -189,81 +204,80 @@ match_catalog(struct matchparams *p)
   mcols=gal_match_coordinates(p->cols1, p->cols2, p->aperture->array, 0, 1,
                               p->cp.minmapsize, &nummatched);
 
-  /* If a match was found, then make the output files. */
-  if(mcols)
+  /* If the output is to be taken from the input columns (it isn't just the
+     log), then do the job. */
+  if(p->logasoutput==0)
     {
-      /* Read all the first catalog columns. */
-      if(p->logasoutput==0)
+      /* Read (and possibly write) the outputs. Note that we only need to
+         read the table when it is necessary for the output (the user might
+         have asked for `--outcols', only with columns of one of the two
+         inputs). */
+      if(p->outcols==NULL || p->acols)
+        a=match_catalog_read_write_all(p, mcols?mcols->array:NULL,
+                                       nummatched, 1, &acolmatch);
+      if(p->outcols==NULL || p->bcols)
+        b=match_catalog_read_write_all(p, mcols?mcols->next->array:NULL,
+                                       nummatched, 2, &bcolmatch);
+
+      /* If one catalog (with specific columns from either of the two
+         inputs) was requested, then write it out. */
+      if(p->outcols)
         {
-          /* Read (and possibly write) the outputs. Note that we only need
-             to read the table when it is necessary for the output (the
-             user user might have asked for `--outcols', only with columns
-             of one of the two inputs. */
-          if(p->outcols==NULL || p->acols)
-            a=match_catalog_read_write_all(p, mcols->array, nummatched,
-                                           1, &acolmatch);
-          if(p->outcols==NULL || p->bcols)
-            b=match_catalog_read_write_all(p, mcols->next->array, nummatched,
-                                           2, &bcolmatch);
-
-          /* If one catalog (with specific columns from either of the two
-             inputs) was requested, then write it out. */
-          if(p->outcols)
-            {
-              /* Arrange the columns and write the output. */
-              match_catalog_write_one(p, a, b, acolmatch, bcolmatch);
+          /* Arrange the columns and write the output. */
+          match_catalog_write_one(p, a, b, acolmatch, bcolmatch);
 
-              /* Clean up. */
-              if(acolmatch) free(acolmatch);
-              if(bcolmatch) free(bcolmatch);
-            }
+          /* Clean up. */
+          if(acolmatch) free(acolmatch);
+          if(bcolmatch) free(bcolmatch);
         }
+    }
 
-      /* Write the raw information in a log file if necessary.  */
-      if(p->logname)
-        {
-          /* Note that unsigned 64-bit integers are not recognized in FITS
-             tables. So if the log file is a FITS table, covert the two
-             index columns to uint32. */
-          tmp=gal_data_copy_to_new_type(mcols, GAL_TYPE_UINT32);
-          tmp->next=mcols->next;
-          tmp->size=nummatched;
-          gal_data_free(mcols);
-          mcols=tmp;
-
-          /* We also want everything to be incremented by one. In a C
-             program, counting starts with zero, so `gal_match_coordinates'
-             will return indexs starting from zero. But outside a C
-             program, on the command-line people expect counting to start
-             from 1 (for example with AWK). */
-          uf = (u=mcols->array) + tmp->size; do (*u)++; while(++u<uf);
-
-          /* Same for the second set of indexs. */
-          tmp=gal_data_copy_to_new_type(mcols->next, GAL_TYPE_UINT32);
-          uf = (u=tmp->array) + tmp->size; do (*u)++; while(++u<uf);
-          tmp->next=mcols->next->next;
-          gal_data_free(mcols->next);
-          tmp->size=nummatched;
-          mcols->next=tmp;
-
-          /* Correct the comments. */
-          free(mcols->comment);
-          mcols->comment="Row index in first catalog (counting from 1).";
-          free(mcols->next->comment);
-          mcols->next->comment="Row index in second catalog (counting "
-            "from 1).";
-
-          /* Write them into the table. */
-          gal_table_write(mcols, NULL, p->cp.tableformat, p->logname,
-                          "LOG_INFO", 0);
-
-          /* Set the comment pointer to NULL: they weren't allocated. */
-          mcols->comment=NULL;
-          mcols->next->comment=NULL;
-        }
-      gal_list_data_free(mcols);
+  /* Write the raw information in a log file if necessary.  */
+  if(p->logname && mcols)
+    {
+      /* Note that unsigned 64-bit integers are not recognized in FITS
+         tables. So if the log file is a FITS table, covert the two
+         index columns to uint32. */
+      tmp=gal_data_copy_to_new_type(mcols, GAL_TYPE_UINT32);
+      tmp->next=mcols->next;
+      tmp->size=nummatched;
+      gal_data_free(mcols);
+      mcols=tmp;
+
+      /* We also want everything to be incremented by one. In a C
+         program, counting starts with zero, so `gal_match_coordinates'
+         will return indexs starting from zero. But outside a C
+         program, on the command-line people expect counting to start
+         from 1 (for example with AWK). */
+      uf = (u=mcols->array) + tmp->size; do (*u)++; while(++u<uf);
+
+      /* Same for the second set of indexs. */
+      tmp=gal_data_copy_to_new_type(mcols->next, GAL_TYPE_UINT32);
+      uf = (u=tmp->array) + tmp->size; do (*u)++; while(++u<uf);
+      tmp->next=mcols->next->next;
+      gal_data_free(mcols->next);
+      tmp->size=nummatched;
+      mcols->next=tmp;
+
+      /* Correct the comments. */
+      free(mcols->comment);
+      mcols->comment="Row index in first catalog (counting from 1).";
+      free(mcols->next->comment);
+      mcols->next->comment="Row index in second catalog (counting "
+        "from 1).";
+
+      /* Write them into the table. */
+      gal_table_write(mcols, NULL, p->cp.tableformat, p->logname,
+                      "LOG_INFO", 0);
+
+      /* Set the comment pointer to NULL: they weren't allocated. */
+      mcols->comment=NULL;
+      mcols->next->comment=NULL;
     }
 
+  /* Clean up. */
+  gal_list_data_free(mcols);
+
   /* Print the number of matches if not in quiet mode. */
   if(!p->cp.quiet)
     fprintf(stdout, "%zu\n", nummatched);
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index edc1e7e..154201d 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -18331,15 +18331,15 @@ identify the extension. When the second input is 
FITS, the extension must
 be specified with @option{--hdu2}.
 
 When @option{--quiet} is not called, Match will print the number of matches
-found in standard output (on the command-line). If no match was found, no
-output file will be created (table or log file). When matches are found, by
+found in standard output (on the command-line). When matches are found, by
 default, the output file(s) will be the re-arranged input tables such that
 the rows match each other: both output tables will have the same number of
 rows which are matched with each other. If @option{--outcols} is called,
 the output is a single table with rows chosen from either of the two inputs
 in any order, see the description of @option{--outcols}. If the
 @option{--logasoutput} option is called, the output will be a single table
-with the contents of the log file, see below.
+with the contents of the log file, see below. If no matches are found, the
+columns of the output table(s) will have zero rows (with proper meta-data).
 
 If no output file name is given with the @option{--output} option, then
 automatic output @ref{Automatic output} will be used to determine the
@@ -18351,17 +18351,18 @@ file. With @option{--outcols} and 
@option{--logasoutput}, the FITS output
 will be a single table (in one extension).
 
 When the @option{--log} option is called (see @ref{Operating mode
-options}), Match will also create a file named @file{astmatch.fits} (or
address@hidden, depending on @option{--tableformat}, see @ref{Input
-output options}) in the directory it is run in. This log table will have
-three columns. The first and second columns show the matching row/record
-number (counting from 1) of the first and second input catalogs
-respectively. The third column is the distance between the two matched
-positions. The units of the distance are the same as the given coordinates
-(given the possible ellipticity, see description of @option{--aperture}
-below). When @option{--logasoutput} is called, no log file (with a fixed
-name) will be created. In this case, the output file (possibly given by the
address@hidden option) will have the contents of this log file.
+options}), and there was a match, Match will also create a file named
address@hidden (or @file{astmatch.txt}, depending on
address@hidden, see @ref{Input output options}) in the directory it
+is run in. This log table will have three columns. The first and second
+columns show the matching row/record number (counting from 1) of the first
+and second input catalogs respectively. The third column is the distance
+between the two matched positions. The units of the distance are the same
+as the given coordinates (given the possible ellipticity, see description
+of @option{--aperture} below). When @option{--logasoutput} is called, no
+log file (with a fixed name) will be created. In this case, the output file
+(possibly given by the @option{--output} option) will have the contents of
+this log file.
 
 @cartouche
 @noindent
diff --git a/lib/dimension.c b/lib/dimension.c
index 4cd4fed..9b1aea7 100644
--- a/lib/dimension.c
+++ b/lib/dimension.c
@@ -60,10 +60,14 @@ gal_dimension_is_different(gal_data_t *first, gal_data_t 
*second)
   if(first->ndim!=second->ndim)
     return 1;
 
-  /* Check if the sizes along all dimensions are the same: */
-  for(i=0;i<first->ndim;++i)
-    if( first->dsize[i] != second->dsize[i] )
-      return 1;
+  /* If the sizes are not zero, check if each dimension also has the same
+     length. */
+  if(first->size==0 && first->size==second->size)
+    return 0;
+  else
+    for(i=0;i<first->ndim;++i)
+      if( first->dsize[i] != second->dsize[i] )
+        return 1;
 
   /* If it got to here, we know the dimensions have the same length. */
   return 0;



reply via email to

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