gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 2491c91 064/113: Imported work in master, mino


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 2491c91 064/113: Imported work in master, minor conflict in book fixed
Date: Fri, 16 Apr 2021 10:33:47 -0400 (EDT)

branch: master
commit 2491c91ebe417728045b06cb4b809c1dffef40b1
Merge: c91d988 2f0d95a
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Imported work in master, minor conflict in book fixed
    
    A minor conflict in the example of `gal_fits_key_read_from_ptr' was fixed.
---
 bin/buildprog/ui.h   |   8 ++--
 doc/gnuastro.texi    |  20 ++++----
 lib/fits.c           | 128 ++++++++++++++++++++++++++++++++++++++++++---------
 lib/gnuastro/fits.h  |   2 +-
 lib/gnuastro/table.h |   2 +-
 lib/table.c          |   2 +-
 lib/txt.c            |   2 +-
 7 files changed, 124 insertions(+), 40 deletions(-)

diff --git a/bin/buildprog/ui.h b/bin/buildprog/ui.h
index 85ee1bb..9089e84 100644
--- a/bin/buildprog/ui.h
+++ b/bin/buildprog/ui.h
@@ -32,7 +32,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 /* Available letters for short options:
 
-   c d e f i j k n p r s t u v w x y z
+   c e f i j k n p r s u v w x y z
    A B C E G H J Q R X Y
 */
 enum option_keys_enum
@@ -41,16 +41,16 @@ enum option_keys_enum
   UI_KEY_INCLUDE        = 'I',
   UI_KEY_LINKDIR        = 'L',
   UI_KEY_LINKLIB        = 'l',
-  UI_KEY_LA             = 'a',
   UI_KEY_ONLYBUILD      = 'b',
   UI_KEY_DEBUG          = 'g',
   UI_KEY_OPTIMIZE       = 'O',
   UI_KEY_WARNING        = 'W',
+  UI_KEY_TAG            = 't',
+  UI_KEY_DETELECOMPILED = 'd',
+  UI_KEY_LA             = 'a',
 
   /* Only with long version (start with a value 1000, the rest will be set
      automatically). */
-  UI_KEY_DETELECOMPILED = 1000,
-  UI_KEY_TAG,
 };
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 3338cd5..f400017 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -20308,7 +20308,8 @@ list. Some of the most common values to this option 
are: @option{pedantic}
 (Warnings related to standard C) and @option{all} (all issues the compiler
 confronts).
 
-@item --tag=STR
+@item -t
+@itemx --tag=STR
 The language configuration information. Libtool can build objects and
 libraries in many languages. In many cases, it can identify the language
 automatically, but when it doesn't you can use this option to explicitly
@@ -20323,7 +20324,8 @@ all these languages.
 Only build the program, don't run it. By default, the built program is
 immediately run afterwards.
 
-@item --deletecompiled
+@item -d
+@itemx --deletecompiled
 Delete the compiled binary file after running it. This option is only
 relevant when the compiled program is run after being built. In other
 words, it is only relevant when @option{--onlybuild} is not called. It can
@@ -21930,7 +21932,7 @@ remain. Here is one short/simple example of 
initializing and adding
 elements to a string list:
 
 @example
-gal_list_str_t *strlist;
+gal_list_str_t *strlist=NULL;
 gal_list_str_add(&strlist, "bottom of list.");
 gal_list_str_add(&strlist, "second last element of list.");
 @end example
@@ -22756,7 +22758,7 @@ $ asttable --info table.fits
 @end example
 @end deftypefun
 
-@deftypefun {gal_data_t *} gal_table_read (char @code{*filename}, char 
@code{*hdu}, gal_list_str_t @code{*cols}, int @code{searchin}, int 
@code{ignorecase}, int @code{minmapsize}, size_t @code{colmatch})
+@deftypefun {gal_data_t *} gal_table_read (char @code{*filename}, char 
@code{*hdu}, gal_list_str_t @code{*cols}, int @code{searchin}, int 
@code{ignorecase}, size_t @code{minmapsize}, size_t @code{colmatch})
 Read the specified columns in a text file (named @code{filename}) into a
 linked list of data structures. If the file is FITS, then @code{hdu} will
 also be used, otherwise, @code{hdu} is ignored.
@@ -23101,8 +23103,8 @@ Here is one example of using this function:
 /* Allocate an array of datasets. */
 gal_data_t *keysll=gal_data_array_calloc(N);
 
-/* Use the array as a list.*/
-for(i=0;i<N-2;++i) keysll[i].next=keysll[i+1];
+/* Make the array usable as a list too (by setting `next'). */
+for(i=0;i<N-1;++i) keysll[i].next=keysll[i+1];
 
 /* Fill the datasets with a `name' and a `type'. */
 keysll[0].name="NAME1";     keysll[0].type=GAL_TYPE_INT32;
@@ -23117,8 +23119,8 @@ gal_fits_key_read_from_ptr(fptr, keysll, 0, 0);
 
 /* Free all the allocated spaces. Note that `name' wasn't allocated
    in this example, so we should explicitly set it to NULL before
-   calling `gal_data_array_free'.                                   */
-for(i=0;i<N-2;++i) keysll[i].name=NULL;
+   calling `gal_data_array_free'. */
+for(i=0;i<N;++i) keysll[i].name=NULL;
 gal_data_array_free(keysll, N, 1);
 @end example
 
@@ -23378,7 +23380,7 @@ reading tables in FITS format. To be generic, it is 
recommended to use
 of table formats based on the filename (see @ref{Table input output}).
 @end deftypefun
 
-@deftypefun {gal_data_t *} gal_fits_tab_read (char @code{*filename}, char 
@code{*hdu}, size_t @code{numrows}, gal_data_t @code{*colinfo}, 
gal_list_sizet_t @code{*indexll}, int @code{minmapsize})
+@deftypefun {gal_data_t *} gal_fits_tab_read (char @code{*filename}, char 
@code{*hdu}, size_t @code{numrows}, gal_data_t @code{*colinfo}, 
gal_list_sizet_t @code{*indexll}, size_t @code{minmapsize})
 Read the columns given in the list @code{indexll} from a FITS table into a
 linked list of data structures, see @ref{List of size_t} and @ref{List of
 gal_data_t}. If the necessary space for each column is larger than
diff --git a/lib/fits.c b/lib/fits.c
index 8ef4c7e..e584704 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -2343,6 +2343,65 @@ gal_fits_tab_info(char *filename, char *hdu, size_t 
*numcols,
 
 
 
+/* Read CFITSIO un-readable (INF, -INF or NAN) floating point values in
+   FITS ASCII tables. */
+static void
+fits_tab_read_ascii_float_special(char *filename, char *hdu, fitsfile *fptr,
+                                  gal_data_t *out, size_t colnum,
+                                  size_t numrows, size_t minmapsize)
+{
+  double tmp;
+  char **strarr;
+  char *tailptr;
+  gal_data_t *strrows;
+  size_t i, colwidth=50;
+  int anynul=0, status=0;
+
+  /* Allocate the dataset to keep the string values. */
+  strrows=gal_data_alloc(NULL, GAL_TYPE_STRING, 1, &numrows, NULL, 0,
+                         minmapsize, NULL, NULL, NULL);
+
+  /* Allocate the space to keep the string values. */
+  strarr=strrows->array;
+  for(i=0;i<numrows;++i)
+    {
+      errno=0;
+      strarr[i]=calloc(colwidth, sizeof *strarr[i]);
+      if(strarr[i]==NULL)
+        error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for "
+              "strarr[%zu]", __func__, colwidth * sizeof *strarr[i], i);
+    }
+
+  /* Read the column as a string. */
+  fits_read_col(fptr, TSTRING, colnum, 1, 1, out->size, NULL, strrows->array,
+                &anynul, &status);
+  gal_fits_io_error(status, NULL);
+
+  /* Convert the strings to float. */
+  for(i=0;i<numrows;++i)
+    {
+      /* Parse the string. */
+      tmp=strtod(strarr[i], &tailptr);
+      if(tailptr==strarr[i])
+        error(EXIT_FAILURE, 0, "%s (hdu %s): couldn't parse row %zu of "
+              "column %zu (with value `%s') as a floating point number",
+              filename, hdu, i+1, colnum, strarr[i]);
+
+      /* Write it into the output dataset. */
+      if(out->type==GAL_TYPE_FLOAT32)
+        ((float *)(out->array))[i]=tmp;
+      else
+        ((double *)(out->array))[i]=tmp;
+    }
+
+  /* Clean up. */
+  gal_data_free(strrows);
+}
+
+
+
+
+
 /* Read the column indexs given in the `indexll' linked list from a FITS
    table into a linked list of data structures, note that this is a
    low-level function, so the output data linked list is the inverse of the
@@ -2350,7 +2409,7 @@ gal_fits_tab_info(char *filename, char *hdu, size_t 
*numcols,
 gal_data_t *
 gal_fits_tab_read(char *filename, char *hdu, size_t numrows,
                   gal_data_t *allcols, gal_list_sizet_t *indexll,
-                  int minmapsize)
+                  size_t minmapsize)
 {
   size_t i=0;
   void *blank;
@@ -2358,12 +2417,17 @@ gal_fits_tab_read(char *filename, char *hdu, size_t 
numrows,
   char **strarr;
   fitsfile *fptr;
   gal_data_t *out=NULL;
-  int status=0, anynul=0;
   gal_list_sizet_t *ind;
+  int isfloat, status=0, anynul=0, hdutype;
 
   /* Open the FITS file */
   fptr=gal_fits_hdu_open_format(filename, hdu, 1);
 
+  /* See if its a Binary or ASCII table (necessary for floating point blank
+     values). */
+  if (fits_get_hdu_type(fptr, &hdutype, &status) )
+    gal_fits_io_error(status, NULL);
+
   /* Pop each index and read/store the array. */
   for(ind=indexll; ind!=NULL; ind=ind->next)
     {
@@ -2379,35 +2443,53 @@ gal_fits_tab_read(char *filename, char *hdu, size_t 
numrows,
          disp_width element of the data structure, which is done
          automatically in `gal_fits_table_info'. */
       if(out->type==GAL_TYPE_STRING)
-        for(i=0;i<numrows;++i)
-          {
-            strarr=out->array;
-            errno=0;
-            strarr[i]=calloc(allcols[ind->v].disp_width+1, sizeof *strarr[i]);
-            if(strarr[i]==NULL)
-              error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for "
-                    "strarr[%zu]", __func__,
-                    (allcols[ind->v].disp_width+1) * sizeof *strarr[i], i);
-          }
+        {
+          strarr=out->array;
+          for(i=0;i<numrows;++i)
+            {
+              errno=0;
+              strarr[i]=calloc(allcols[ind->v].disp_width+1,
+                               sizeof *strarr[i]);
+              if(strarr[i]==NULL)
+                error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for "
+                      "strarr[%zu]", __func__,
+                      (allcols[ind->v].disp_width+1) * sizeof *strarr[i], i);
+            }
+        }
 
       /* Allocate a blank value for the given type and read/store the
-         column using CFITSIO. Afterwards, free the blank value. Note that
-         we only need blank values for integer types. For floating point
-         types, the FITS standard defines blanks as NaN (same as almost any
-         other software like Gnuastro). However if a blank value is
-         specified, CFITSIO will convert other special numbers like `inf'
-         to NaN also. We want to be able to distringuish `inf' and NaN
-         here, so for floating point types, we won't define any blank
-         value. */
-      blank = ( (out->type==GAL_TYPE_FLOAT32 || out->type==GAL_TYPE_FLOAT64)
+         column using CFITSIO. Note that for binary tables, we only need
+         blank values for integer types. For binary floating point types,
+         the FITS standard defines blanks as NaN (same as almost any other
+         software like Gnuastro). However if a blank value is specified,
+         CFITSIO will convert other special numbers like `inf' to NaN
+         also. We want to be able to distringuish `inf' and NaN here, so
+         for floating point types in binary tables, we won't define any
+         blank value. In ASCII tables, CFITSIO doesn't read the `NAN'
+         values (that it has written itself) unless we specify a blank
+         pointer/value. */
+      isfloat = out->type==GAL_TYPE_FLOAT32 || out->type==GAL_TYPE_FLOAT64;
+      blank = ( ( hdutype==BINARY_TBL && isfloat )
                 ? NULL
                 : gal_blank_alloc_write(out->type) );
       fits_read_col(fptr, gal_fits_type_to_datatype(out->type), ind->v+1,
                     1, 1, out->size, blank, out->array, &anynul, &status);
-      gal_fits_io_error(status, NULL);
 
-      /* Clean up. */
+      /* In the ASCII table format, CFITSIO might not be able to read `INF'
+         or `-INF'. In this case, it will set status to `BAD_C2D' or
+         `BAD_C2F'. So, we'll use our own parser for the column values. */
+      if( hdutype==ASCII_TBL
+          && isfloat
+          && (status==BAD_C2D || status==BAD_C2F) )
+        {
+          fits_tab_read_ascii_float_special(filename, hdu, fptr, out,
+                                            ind->v+1, numrows, minmapsize);
+          status=0;
+        }
+
+      /* Clean up and sanity check. */
       if(blank) free(blank);
+      gal_fits_io_error(status, NULL);
     }
 
   /* Close the FITS file */
diff --git a/lib/gnuastro/fits.h b/lib/gnuastro/fits.h
index 370355f..2960378 100644
--- a/lib/gnuastro/fits.h
+++ b/lib/gnuastro/fits.h
@@ -257,7 +257,7 @@ gal_fits_tab_info(char *filename, char *hdu, size_t 
*numcols,
 gal_data_t *
 gal_fits_tab_read(char *filename, char *hdu, size_t numrows,
                   gal_data_t *colinfo, gal_list_sizet_t *indexll,
-                  int minmapsize);
+                  size_t minmapsize);
 
 void
 gal_fits_tab_write(gal_data_t *cols, gal_list_str_t *comments,
diff --git a/lib/gnuastro/table.h b/lib/gnuastro/table.h
index 69ace6f..456380e 100644
--- a/lib/gnuastro/table.h
+++ b/lib/gnuastro/table.h
@@ -138,7 +138,7 @@ gal_table_print_info(gal_data_t *allcols, size_t numcols, 
size_t numrows);
 /************************************************************************/
 gal_data_t *
 gal_table_read(char *filename, char *hdu, gal_list_str_t *cols,
-               int searchin, int ignorecase, int minmapsize,
+               int searchin, int ignorecase, size_t minmapsize,
                size_t *colmatch);
 
 
diff --git a/lib/table.c b/lib/table.c
index e22fb4b..ec174e9 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -404,7 +404,7 @@ make_list_of_indexs(gal_list_str_t *cols, gal_data_t 
*allcols,
    on. */
 gal_data_t *
 gal_table_read(char *filename, char *hdu, gal_list_str_t *cols,
-               int searchin, int ignorecase, int minmapsize,
+               int searchin, int ignorecase, size_t minmapsize,
                size_t *colmatch)
 {
   int tableformat;
diff --git a/lib/txt.c b/lib/txt.c
index f329232..5ea795e 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -856,7 +856,7 @@ txt_fill(char *line, char **tokens, size_t maxcolnum, 
gal_data_t *info,
 
 static gal_data_t *
 gal_txt_read(char *filename, size_t *dsize, gal_data_t *info,
-             gal_list_sizet_t *indexll, int minmapsize, int format)
+             gal_list_sizet_t *indexll, size_t minmapsize, int format)
 {
   FILE *fp;
   char *line;



reply via email to

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