gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 8090e6d 038/125: Corrections to FITS table rea


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 8090e6d 038/125: Corrections to FITS table reading
Date: Sun, 23 Apr 2017 22:36:32 -0400 (EDT)

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

    Corrections to FITS table reading
    
    During the FITS table reading, it is much more convenient to have an array
    of data structures (with column information) to easily get to the desired
    column information. But there was no systematic way to allocate an array of
    data structures, so a new `gal_data_calloc_dataarray' was defined to make
    the job of allocating (and initializing the values) easy.
    
    Other minor corrections:
    
     - The array keeping the column information is now uniformly named
       `allcols' in all `lib/table.c', `lib/fits.c', and `lib/txt.c'.
    
     - `gal_fits_table_read' would pop elements from `indexll'. But it is
       better for reading/debugging that memory be freed in the same function
       they were allocated. So this function now only goes over the list, it
       doesn't pop/free the allocations. This was applied in `lib/txt.c', so
       when reading a FITS table, we would get segmentation errors.
---
 lib/data.c          | 43 ++++++++++++++++++++++++++++++++++++++++---
 lib/fits.c          | 41 +++++++++++++++++------------------------
 lib/gnuastro/data.h |  3 +++
 lib/table.c         |  3 ++-
 lib/txt.c           |  6 +-----
 5 files changed, 63 insertions(+), 33 deletions(-)

diff --git a/lib/data.c b/lib/data.c
index 21da39e..f03185b 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -476,7 +476,6 @@ gal_data_alloc(void *array, int type, size_t ndim, long 
*dsize,
 {
   gal_data_t *out;
 
-
   /* Allocate the space for the actual structure. */
   errno=0;
   out=malloc(sizeof *out);
@@ -484,12 +483,10 @@ gal_data_alloc(void *array, int type, size_t ndim, long 
*dsize,
     error(EXIT_FAILURE, errno, "%zu bytes for gal_data_t in gal_data_alloc",
           sizeof *out);
 
-
   /* Initialize the allocated array. */
   gal_data_initialize(out, array, type, ndim, dsize, wcs, clear, minmapsize,
                       name, unit, comment);
 
-
   /* Return the final structure. */
   return out;
 }
@@ -498,6 +495,46 @@ gal_data_alloc(void *array, int type, size_t ndim, long 
*dsize,
 
 
 
+/* Allocate an array of data structures and initialize all the values. */
+gal_data_t *
+gal_data_calloc_dataarray(size_t size)
+{
+  size_t i;
+  gal_data_t *out;
+
+  /* Allocate the array to keep the structures. */
+  errno=0;
+  out=malloc(size*sizeof *out);
+  if(out==NULL)
+    error(EXIT_FAILURE, errno, "%zu bytes for `out' in "
+          "`gal_data_calloc_dataarray'", size*sizeof *out);
+
+
+  /* Set the pointers to NULL if they didn't exist and the non-pointers to
+     impossible integers (so the caller knows the array is only
+     allocated. `minmapsize' should be set when allocating the array and
+     should be set when you run `gal_data_initialize'. */
+  for(i=0;i<size;++i)
+    {
+      out[i].array      = NULL;
+      out[i].type       = -1;
+      out[i].ndim       = 0;
+      out[i].dsize      = NULL;
+      out[i].nwcs       = 0;
+      out[i].wcs        = NULL;
+      out[i].mmapname   = NULL;
+      out[i].name = out[i].unit = out[i].comment = NULL;
+      out[i].disp_fmt = out[i].disp_width = out[i].disp_precision = -1;
+    }
+
+  /* Return the array pointer. */
+  return out;
+}
+
+
+
+
+
 /* Free the allocated contents of a data structure and possibly also the
    data structure its self. When `only_contents' is zero, the actual data
    structure will also be freed, see bellow.
diff --git a/lib/fits.c b/lib/fits.c
index a14c9fb..1f270d0 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -1736,7 +1736,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
   int tfields;        /* The maximum number of fields in FITS is 999 */
   fitsfile *fptr;
   size_t i, index;
-  gal_data_t *cols=NULL;
+  gal_data_t *allcols;
   int status=0, datatype;
   char *tailptr, keyname[FLEN_KEYWORD]="XXXXXXXXXXXXX", value[FLEN_VALUE];
 
@@ -1748,13 +1748,9 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
 
 
   /* Read the total number of fields, then allocate space for the data
-     structure and store the information within it. */
+     structure array and store the information within it. */
   fits_read_key(fptr, TINT, "TFIELDS", &tfields, NULL, &status);
-  errno=0;
-  cols=malloc(tfields*sizeof *cols);
-  if(cols==NULL)
-    error(EXIT_FAILURE, errno, "%zu bytes for cols in `gal_fits_table_info'",
-          tfields*sizeof *cols);
+  allcols=gal_data_calloc_dataarray(tfields);
 
 
   /* Read all the keywords one by one and if they match, then put them in
@@ -1794,7 +1790,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
              meantime, also do a sanity check. */
           index = strtoul(&keyname[5], &tailptr, 10) - 1;
           if(index<tfields)     /* Counting from zero was corrected above. */
-            cols[index].type=gal_fits_datatype_to_type(datatype);
+            allcols[index].type=gal_fits_datatype_to_type(datatype);
         }
 
       /* COLUMN NAME. All strings in CFITSIO start and finish with single
@@ -1807,7 +1803,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
           remove_trailing_space(value);
           index = strtoul(&keyname[5], &tailptr, 10) - 1;
           if(index<tfields)
-            gal_checkset_allocate_copy(&value[1], &cols[index].name);
+            gal_checkset_allocate_copy(&value[1], &allcols[index].name);
         }
 
       /* COLUMN UNITS. */
@@ -1817,7 +1813,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
           remove_trailing_space(value);
           index = strtoul(&keyname[5], &tailptr, 10) - 1;
           if(index<tfields)
-            gal_checkset_allocate_copy(&value[1], &cols[index].unit);
+            gal_checkset_allocate_copy(&value[1], &allcols[index].unit);
         }
 
       /* COLUMN COMMENTS */
@@ -1827,7 +1823,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
           remove_trailing_space(value);
           index = strtoul(&keyname[5], &tailptr, 10) - 1;
           if(index<tfields)
-            gal_checkset_allocate_copy(&value[1], &cols[index].comment);
+            gal_checkset_allocate_copy(&value[1], &allcols[index].comment);
         }
 
       /* COLUMN DISPLAY FORMAT */
@@ -1837,7 +1833,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
           remove_trailing_space(value);
           index = strtoul(&keyname[5], &tailptr, 10) - 1;
           if(index<tfields)
-            set_display_format(&value[1], &cols[index], filename, hdu,
+            set_display_format(&value[1], &allcols[index], filename, hdu,
                                keyname);
         }
     }
@@ -1845,7 +1841,7 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
   /* Close the FITS file and report an error if we had any. */
   fits_close_file(fptr, &status);
   gal_fits_io_error(status, NULL);
-  return cols;
+  return allcols;
 }
 
 
@@ -1858,37 +1854,34 @@ gal_fits_table_info(char *filename, char *hdu, size_t 
*numcols,
    input indexs linked list. You can use */
 gal_data_t *
 gal_fits_table_read(char *filename, char *hdu, size_t numrows,
-                    gal_data_t *colinfo, struct gal_linkedlist_sll *indexll,
+                    gal_data_t *allcols, struct gal_linkedlist_sll *indexll,
                     int minmapsize)
 {
-  size_t ind;
   long dsize;
   void *blank;
   fitsfile *fptr;
   int status=0, anynul;
   gal_data_t *out=NULL;
+  struct gal_linkedlist_sll *ind;
 
   /* Open the FITS file */
   fptr=gal_fits_read_hdu(filename, hdu, 1);
 
   /* Pop each index and read/store the array. */
-  while(indexll!=NULL)
+  for(ind=indexll; ind!=NULL; ind=ind->next)
     {
-      /* Pop the index. */
-      gal_linkedlist_pop_from_sll(&indexll, &ind);
-
       /* Allocate the necessary data structure (including the array) for
          this column. */
       dsize=numrows;
-      gal_data_add_to_ll(&out, NULL, colinfo[ind].type, 1, &dsize, NULL, 0,
-                         minmapsize, colinfo[ind].name, colinfo[ind].unit,
-                         colinfo[ind].comment);
+      gal_data_add_to_ll(&out, NULL, allcols[ind->v].type, 1, &dsize, NULL, 0,
+                         minmapsize, allcols[ind->v].name,
+                         allcols[ind->v].unit, allcols[ind->v].comment);
 
       /* Allocate a blank value for the given type and read/store the
          column using CFITSIO. Afterwards, free the blank value. */
       blank=gal_data_alloc_blank(out->type);
-      fits_read_col(fptr, gal_fits_type_to_datatype(out->type), ind+1, 1, 1,
-                    out->size, blank, out->array, &anynul, &status);
+      fits_read_col(fptr, gal_fits_type_to_datatype(out->type), ind->v+1,
+                    1, 1, out->size, blank, out->array, &anynul, &status);
       free(blank);
     }
 
diff --git a/lib/gnuastro/data.h b/lib/gnuastro/data.h
index 6e3934c..1156b49 100644
--- a/lib/gnuastro/data.h
+++ b/lib/gnuastro/data.h
@@ -232,6 +232,9 @@ gal_data_alloc(void *array, int type, size_t ndim, long 
*dsize,
                struct wcsprm *wcs, int clear, size_t minmapsize,
                char *name, char *unit, char *comment);
 
+gal_data_t *
+gal_data_calloc_dataarray(size_t size);
+
 void
 gal_data_free(gal_data_t *data, int only_contents);
 
diff --git a/lib/table.c b/lib/table.c
index dfead6c..9d3e38c 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -206,7 +206,8 @@ make_list_of_indexs(struct gal_linkedlist_stll *cols, 
gal_data_t *allcols,
           str = tmp->v + 1;
 
           /* Allocate the regex_t structure: */
-          errno=0; regex=malloc(sizeof *regex);
+          errno=0;
+          regex=malloc(sizeof *regex);
           if(regex==NULL)
             error(EXIT_FAILURE, errno, "%zu bytes for regex", sizeof *regex);
 
diff --git a/lib/txt.c b/lib/txt.c
index d353fa2..4e9ac1f 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -418,11 +418,7 @@ txt_infoll_to_array(gal_data_t *colsll, size_t *numcols)
     numc = numc > col->status ? numc : col->status;
 
   /* Now, allocate the array and put in the values. */
-  errno=0;
-  allcols=malloc(numc*sizeof *allcols);
-  if(allcols==NULL)
-    error(EXIT_FAILURE, errno, "%zu bytes for `allcols' in "
-          "`txt_infoll_to_array'", numc*sizeof *allcols);
+  allcols=gal_data_calloc_dataarray(numc);
 
   /* Put each column into its proper place in the array. After the copy,
      all the (possibly) allocated spaces in the linked list are set to



reply via email to

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