gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 9eb1286: Table: improved/new implementation of


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 9eb1286: Table: improved/new implementation of WCS conversion
Date: Sat, 25 May 2019 23:35:06 -0400 (EDT)

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

    Table: improved/new implementation of WCS conversion
    
    The old/initial implementation was very crude and hard to generalize. With
    this commit, it can now work on any number of dimentions and also the
    syntax has become the reverse polish notation, similar to Arithmetic. This
    will allow easy inclusion of many operators in the near future.
    
    Also, while doing this, I noticed that in the functions were we check file
    formats based on file names/suffixes, we hadn't accounted for a
    standard-input `filename' (which is NULL). This is now accounted for.
---
 NEWS              |   8 +--
 bin/table/main.h  |   6 +-
 bin/table/table.c |  83 ++++++++++++++++++++++++----
 bin/table/ui.c    | 161 ++++++++++++++++++++++++++++++------------------------
 bootstrap.conf    |   1 +
 doc/gnuastro.texi |  61 ++++++++++++++-------
 lib/eps.c         |  38 ++++++++-----
 lib/fits.c        |  51 ++++++++++-------
 lib/jpeg.c        |  55 +++++++++++--------
 lib/pdf.c         |  31 +++++++----
 lib/tableintern.c |   2 +-
 lib/tiff.c        |  39 ++++++++-----
 12 files changed, 345 insertions(+), 191 deletions(-)

diff --git a/NEWS b/NEWS
index c0cc5a9..a06fd7e 100644
--- a/NEWS
+++ b/NEWS
@@ -29,10 +29,10 @@ See the end of the file for license conditions.
   Table:
    - As part of printing output columns, Table can now convert Image to WCS
      coordinates and vice-versa. For example if the input catalog has
-     atleast an `ID' column and two `RA' and `DEC' columns the following
-     `-cID,RA,DEC -cwcstoimg(RA,DEC) --wcsfile=a.fits' options will print 5
-     columns, where the last two columns are the image coordinates based on
-     the WCS in `a.fits'.
+     atleast an `ID' column and two `RA' and `DEC' columns the set of
+     options below will produce 5 columns where the last two columns are
+     the image coordinates based on the WCS in `a.fits':
+           `-cID,RA,DEC -c"arith RA DEC wcstoimg" --wcsfile=a.fits'
    --head: Only output the given number of rows from the top of columns.
    --tail: Only output the given number of rows from the botoom of columns.
 
diff --git a/bin/table/main.h b/bin/table/main.h
index 005424b..8c8750d 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -74,9 +74,13 @@ struct tableparams
   uint8_t            freesort;  /* If the sort column should be freed.  */
   uint8_t          *freerange;  /* If the range column should be freed. */
   uint8_t              sortin;  /* If the sort column is in the output. */
+  time_t              rawtime;  /* Starting time of the program.        */
+
+  /* For arithmetic operators. */
+  gal_list_str_t  *wcstoimg_p;  /* Pointer to the node.                 */
+  gal_list_str_t  *imgtowcs_p;  /* Pointer to the node.                 */
   size_t             wcstoimg;  /* Output column no, for conversion.    */
   size_t             imgtowcs;  /* Output column no, for conversion.    */
-  time_t              rawtime;  /* Starting time of the program.        */
 };
 
 #endif
diff --git a/bin/table/table.c b/bin/table/table.c
index 50b83c7..b284aa6 100644
--- a/bin/table/table.c
+++ b/bin/table/table.c
@@ -324,12 +324,33 @@ table_head_tail(struct tableparams *p)
 
 
 
+/* Set the converted column metadata. */
+static void
+table_unit_update_metadata(gal_data_t *col, char *name, char *unit,
+                           char *comment)
+{
+  if(col)
+    {
+      if(col->name)    free(col->name);
+      if(col->unit)    free(col->unit);
+      if(col->comment) free(col->comment);
+      gal_checkset_allocate_copy(name, &col->name);
+      gal_checkset_allocate_copy(unit, &col->unit);
+      gal_checkset_allocate_copy(comment, &col->comment);
+    }
+}
+
+
+
+
+
 static void
 table_unit_conversion(struct tableparams *p, int w0i1)
 {
   int isfirstcol;
-  gal_data_t *t1, *t2, *tmp, *before, *after;
-  size_t i, startcol = w0i1 ? p->imgtowcs : p->wcstoimg;
+  struct wcsprm *wcs=p->wcs;
+  gal_data_t *tmp, *before, *after, *t1=NULL, *t2=NULL, *t3=NULL;
+  size_t i, j, ndim=wcs->naxis, startcol = w0i1 ? p->imgtowcs : p->wcstoimg;
 
   /* Go to the column that we need to convert. */
   i=0;
@@ -338,7 +359,9 @@ table_unit_conversion(struct tableparams *p, int w0i1)
     {
       if( i++ == startcol )
         {
-          after=tmp->next->next;
+          j=1;
+          for(after=tmp->next;j<ndim;after=after->next)
+            ++j;
           break;
         }
       before=tmp;
@@ -348,25 +371,65 @@ table_unit_conversion(struct tableparams *p, int w0i1)
   /* Make sure the types are double-precision floating point. NOTE that
      since we are freeing afterwards, the second column needs to be
      read first.*/
-  t2=gal_data_copy_to_new_type_free(tmp->next, GAL_TYPE_FLOAT64);
-  t1=gal_data_copy_to_new_type_free(tmp,       GAL_TYPE_FLOAT64);
+  if(ndim==3)
+    t3=gal_data_copy_to_new_type_free(tmp->next->next, GAL_TYPE_FLOAT64);
+  if(ndim>=2)
+    t2=gal_data_copy_to_new_type_free(tmp->next, GAL_TYPE_FLOAT64);
+  t1=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT64);
 
-  /* Do the conversion. */
+  /* Define the list of coordinates and do the conversion. */
   t1->next=t2;
-  t2->next=NULL;
+  if(t2) t2->next=t3;
+  if(t3) t3->next=NULL;
   if(w0i1) gal_wcs_img_to_world(t1, p->wcs, 1);
   else     gal_wcs_world_to_img(t1, p->wcs, 1);
 
   /* In image mode, double-precision floating point is too much. */
   if(w0i1==0)
     {
+      /* Convert them to 32-bit floating point. */
       t1=gal_data_copy_to_new_type_free(t1, GAL_TYPE_FLOAT32);
-      t2=gal_data_copy_to_new_type_free(t2, GAL_TYPE_FLOAT32);
+      if(t2)
+        {
+          t2=gal_data_copy_to_new_type_free(t2, GAL_TYPE_FLOAT32);
+          t1->next=t2;
+        }
+      if(t3)
+        {
+          t3=gal_data_copy_to_new_type_free(t3, GAL_TYPE_FLOAT32);
+          t2->next=t3;
+        }
+
+      /* Set the names, units and comments for each dataset. */
+      table_unit_update_metadata(t1, "X", "pixel", "Converted from WCS");
+      table_unit_update_metadata(t2, "Y", "pixel", "Converted from WCS");
+      table_unit_update_metadata(t3, "Z", "pixel", "Converted from WCS");
+    }
+  else
+    {
+      table_unit_update_metadata(t1, wcs->ctype[0], wcs->cunit[0],
+                                 "Converted from pixel coordinates");
+      table_unit_update_metadata(t2, t2?wcs->ctype[1]:NULL,
+                                 t2?wcs->cunit[1]:NULL,
+                                 "Converted from pixel coordinates");
+      table_unit_update_metadata(t3, t3?wcs->ctype[2]:NULL,
+                                 t3?wcs->cunit[2]:NULL,
+                                 "Converted from pixel coordinates");
     }
 
   /* Put them back into the output table. */
-  t1->next=t2;
-  t2->next=after;
+  switch(ndim)
+    {
+    case 1: t1->next=after; break;
+    case 2: t2->next=after; break;
+    case 3: t3->next=after; break;
+    default:
+      error(EXIT_FAILURE, 0, "a bug! Please contact us at `%s' to fix the "
+            "problem. This program is not set for `%zu' dimensions",
+            PACKAGE_BUGREPORT, ndim);
+    }
+
+  /* If the desired columns were at the start, we'll need to fix it. */
   if(isfirstcol) p->table=t1; else before->next=t1;
 }
 
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 392a94e..02674ac 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -435,75 +435,89 @@ ui_print_info_exit(struct tableparams *p)
 
 
 
-/* WCS <-> Image conversion */
-static void
-ui_wcs_conversion(struct tableparams *p, char *instr,
-                  gal_list_str_t **wcstoimg_ptr,
-                  gal_list_str_t **imgtowcs_ptr,
-                  gal_list_str_t **new)
+static int
+ui_columns_wcs(struct tableparams *p, char *operator, size_t counter)
 {
-  const char delimiter[]="/";
-  char *c1, *c2, *c3, *optname, *saveptr;
+  char *opname;
+  gal_list_str_t **ptr;
+  int fromwcs=!strncmp("wcstoimg", operator, 8);
 
-  /* Set the basic option properties. */
-  instr[8]='\0';
-  optname = instr;
+  /* Set the basic properties. */
+  opname = fromwcs ? "wcstoimg"       : "imgtowcs";
+  ptr    = fromwcs ? (&p->wcstoimg_p) : (&p->imgtowcs_p);
 
   /* If a WCS hasn't been read yet, read it.*/
   if(p->wcs==NULL)
     {
       /* A small sanity check. */
       if(p->wcsfile==NULL || p->wcshdu==NULL)
-        error(EXIT_FAILURE, 0, "`--wcsfile' and `--wcshdu' are necessary for "
-              "the `%s' conversion", optname);
+        error(EXIT_FAILURE, 0, "`--wcsfile' and `--wcshdu' are necessary "
+              "for the `%s' conversion", opname);
 
       /* Read the WCS. */
       p->wcs=gal_wcs_read(p->wcsfile, p->wcshdu, 0, 0, &p->nwcs);
       if(p->wcs==NULL)
         error(EXIT_FAILURE, 0, "%s (hdu: %s): no WCS could be read by "
               "WCSLIB", p->wcsfile, p->wcshdu);
+
+      /* Make sure it doesn't have more than 3 axises. */
+      if(p->wcs->naxis>3)
+        error(EXIT_FAILURE, 0, "%s (hdu %s): the WCS has %d dimensions. "
+              "So far, only up to three dimensions are supported",
+              p->wcsfile, p->wcshdu, p->wcs->naxis);
     }
 
-  /* Make sure this conversion is only requested once. */
-  if( instr[0]=='w' ? *wcstoimg_ptr : *imgtowcs_ptr )
-    error(EXIT_FAILURE, 0, "`%s' can only be called once.", optname);
-
-  /* Read the first token. */
-  c1=strtok_r(instr+9, delimiter, &saveptr);
-  if(c1==NULL)
-    error(EXIT_FAILURE, 0, "`%s' must end with `)'", optname);
-  if(*c1==')')
-    error(EXIT_FAILURE, 0, "`%s' has no defining column. Please specify "
-          "two column names or numbers with a slash between them for "
-          "example `%s(RA/DEC)'", optname, optname);
-
-  /* Read the second token and make sure it ends with `)'. Then set the `)'
-     as the string-NULL character. */
-  c2=strtok_r(NULL,        delimiter, &saveptr);
-  if(c2==NULL || c2[strlen(c2)-1]!=')')
-    error(EXIT_FAILURE, 0, "`%s' needs two input columns. Please "
-          "specify two columns with a slash between them for "
-          "example `%s(RA/DEC)'", optname, optname);
-  c2[strlen(c2)-1]='\0';
-
-  /* Make sure there aren't any more elements. */
-  c3=strtok_r(NULL, delimiter, &saveptr);
-  if(c3!=NULL)
-    error(EXIT_FAILURE, 0, "only two values can be given to `%s'", optname);
-
-  /* Add the column name/number to the list of columns to read, then keep
-     the pointer to the node (so we can later identify it). */
-  gal_list_str_add(new, c1, 1);
-  gal_list_str_add(new, c2, 1);
-
-  /* Keep this node's pointer. */
-  if(instr[0]=='w') *wcstoimg_ptr=*new;
-  else              *imgtowcs_ptr=*new;
-
-  /* Clean up (the `c1' and `c2') are actually within the allocated space
-     of `instr'. That is why we are setting the last argument of
-     `gal_list_str_add' to `1' (to allocate space for them). */
-  free(instr);
+  /* Some basic sanity checks. */
+  if(counter!=p->wcs->naxis)
+    error(EXIT_FAILURE, 0, "%s (%s): WCS has %d dimensions but only %zu "
+          "columns were given to the `%s' operator", p->wcsfile, p->wcshdu,
+          p->wcs->naxis, counter, opname);
+  if(*ptr)
+    error(EXIT_FAILURE, 0, "`%s' can only be called once.", opname);
+
+  /* Return the type of operator. */
+  return fromwcs;
+}
+
+
+
+
+
+/* When arithmetic operations are requested. */
+static void
+ui_columns_arith(struct tableparams *p, char *expression,
+                 gal_list_str_t **new)
+{
+  int fromwcs;
+  size_t counter=0;
+  char *token=NULL, *saveptr;
+  char *str, *delimiter=" \t";
+
+  token=strtok_r(expression, delimiter, &saveptr);
+  while(token!=NULL)
+    {
+      /* Check the token value and take the proper action. */
+      if(!strncmp("wcstoimg", token, 8) || !strncmp("imgtowcs", token, 8))
+        {
+          /* Set the WCS-conversion parameters, then break out of the loop
+             (so far, no more operators are defined). */
+          fromwcs=ui_columns_wcs(p, token, counter);
+          if(fromwcs) p->wcstoimg_p = (*new)->next;
+          else        p->imgtowcs_p = (*new)->next;
+          break;
+        }
+      else
+        {
+          str = ( (token[0]=='c' && isdigit(token[1]))
+                  ? &token[1]
+                  : token );
+          gal_list_str_add(new, str, 1);
+          ++counter;
+        }
+
+      /* Go to the next token. */
+      token=strtok_r(NULL, delimiter, &saveptr);
+    }
 }
 
 
@@ -517,11 +531,10 @@ ui_wcs_conversion(struct tableparams *p, char *instr,
 static void
 ui_columns_prepare(struct tableparams *p, size_t *wcstoimg, size_t *imgtowcs)
 {
-  size_t i;
   char **strarr;
+  size_t i, ncols;
   gal_data_t *strs;
   gal_list_str_t *tmp, *new=NULL;
-  gal_list_str_t *wcstoimg_ptr=NULL, *imgtowcs_ptr=NULL;
 
   /* Go over the whole original list (where each node may have more than
      one value separated by a comma. */
@@ -535,9 +548,11 @@ ui_columns_prepare(struct tableparams *p, size_t 
*wcstoimg, size_t *imgtowcs)
       /* Go over all the given colum names/numbers. */
       for(i=0;i<strs->size;++i)
         {
-          if(!strncmp(strarr[i],"wcstoimg(",9)
-             || !strncmp(strarr[i],"imgtowcs(",9))
-            ui_wcs_conversion(p, strarr[i], &wcstoimg_ptr, &imgtowcs_ptr, 
&new);
+          if(!strncmp(strarr[i], "arith ", 6))
+            {
+              ui_columns_arith(p, strarr[i]+6, &new);
+              free(strarr[i]);
+            }
           else
             gal_list_str_add(&new, strarr[i], 0);
 
@@ -551,22 +566,26 @@ ui_columns_prepare(struct tableparams *p, size_t 
*wcstoimg, size_t *imgtowcs)
       gal_data_free(strs);
     }
 
-  /* Delete the old list. */
-  gal_list_str_free(p->columns, 1);
+  /* If conversion is necessary, set the final column number. */
+  i=0;
+  if(p->wcstoimg_p || p->imgtowcs_p)
+    {
+      ncols=gal_list_str_number(new);
+      for(tmp=new;tmp!=NULL;tmp=tmp->next)
+        {
+          /* Note that we are going to reverse the list after this, so we
+             need to reset the counter. */
+          if(p->wcstoimg_p==tmp) *wcstoimg=ncols - i - (p->wcs->naxis-1);
+          if(p->imgtowcs_p==tmp) *imgtowcs=ncols - i - (p->wcs->naxis-1);
+          ++i;
+        }
+    }
 
-  /* Reverse the new list, then put it into `p->columns'. */
+  /* Delete the old list, then reverse the new list, and put it into
+     `p->columns'. */
+  gal_list_str_free(p->columns, 1);
   gal_list_str_reverse(&new);
   p->columns=new;
-
-  /* If conversion is necessary, set the final column number. */
-  i=0;
-  if(wcstoimg_ptr || imgtowcs_ptr)
-    for(tmp=p->columns;tmp!=NULL;tmp=tmp->next)
-      {
-        if(wcstoimg_ptr==tmp) *wcstoimg=i-1;
-        if(imgtowcs_ptr==tmp) *imgtowcs=i-1;
-        ++i;
-      }
 }
 
 
diff --git a/bootstrap.conf b/bootstrap.conf
index 16a3739..f51eec1 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -229,6 +229,7 @@ gnulib_modules="
     gendocs
     gpl-3.0
     mbstok_r
+    strtok_r
     inttypes
     sys_time
     strptime
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 13a0f5e..77080c4 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -11730,32 +11730,53 @@ input table columns are output. When this option is 
called multiple times,
 it is possible to output one column more than once.
 
 This option can also modify the input values based on certain
-operations. The operators are called just like a column name, allowing you
-to place the modified columns anywhere in the output table. For example the
-output of the two commands below (which will produce identical outputs)
-will be 5 columns. The first three columns are the input table's IDs and RA
-and Dec of each object. The fourth and fifth will be the pixel positions in
address@hidden that correspond to those positions.
+operations. To use this feature, the first 6 characters of the value should
+be address@hidden }' (note the space character in the end, after
address@hidden'). Afterwards, you can use the reverse polish notation to
+identify the operators and their operands, see @ref{Reverse polish
+notation}. Just note that currently the range of operators available here
+is less than @ref{Arithmetic} and listed below. Also note that the
+delimiter between the tokens (value to @option{--column}) is a white-space
+character. Therefore the whole value has to be quoted (see the examples
+below).
+
+To identify a column you can directly use its name, or specify its number
+(counting from one, see @ref{Selecting table columns}). When its a number,
+is necessary to prefix it with a @code{c} (otherwise it is not
+distinguisable from a constant number to use in the processing). For
+example the two commands below (which have the same output) will produce 5
+columns. The first three columns are the input table's ID, RA and Dec
+columns. The fourth and fifth columns will be the pixel positions in
address@hidden that correspond to each RA and Dec.
 
 @example
-$ asttable table.fits -cID,RA,DEC,wcstoimg(RA/DEC) \
-           --wcsfile=image.fits
-$ asttable table.fits -cID,RA -cDEC -cwcstoimg(RA/DEC) \
+$ asttable table.fits -cID,RA,DEC,"arith RA DEC wcstoimg" \
            --wcsfile=image.fits
+$ asttable table.fits -cID,RA -cDEC \
+           -c"arith RA DEC wcstoimg" --wcsfile=image.fits
address@hidden example
+
+If we assume the @code{RA} and @code{DEC} columns are the 2nd and 3rd
+columns, then the commands above can also be written like this:
+
address@hidden
+$ asttable table.fits -cID,2 -c3 \
+           -c"arith c2 c3 wcstoimg" --wcsfile=image.fits
 @end example
 
+The current list of operands are:
address@hidden WCS: World Coordinate System
address@hidden World Coordinate System (WCS)
 @table @code
address@hidden wcstoimg(C1/C2)
-Assume that the column specfied by @code{C1} (either through name or
-number) is the RA and @code{C2} is the Declination. As output, the two
-outputs of this operator are the image coordinates. Note that within the
-operator, the delimiter/separator between column identifiers is a slash
-(address@hidden/}'). The WCS structure necessary for this conversion is derived
-from the @option{--wcshdu} extension/HDU of @option{--wcsfile}.
-
address@hidden imgtowcs(C1/C2)
-Similar to @code{wcstoimg}, except that the two input columns are assumed
-to be image coordinates and the output columns will be
address@hidden wcstoimg
+Convert the given WCS positions to image/dataset coordinates based on the
+number of dimensions in the WCS structure of @option{--wcshdu}
+extension/HDU in @option{--wcsfile}. It will output the same number of
+columns. The first poppoed operand is the last FITS dimension.
+
address@hidden imgtowcs
+Similar to @code{wcstoimg}, except that image/dataset coordinates are
+converted to WCS coordinates.
 @end table
 
 @item -w STR
diff --git a/lib/eps.c b/lib/eps.c
index 997fc09..8c95e88 100644
--- a/lib/eps.c
+++ b/lib/eps.c
@@ -46,14 +46,18 @@ int
 gal_eps_name_is_eps(char *name)
 {
   size_t len;
-  len=strlen(name);
-  if ( ( len>=3 && strcmp(&name[len-3], "eps") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "EPS") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "epsf") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "epsi") == 0 ) )
-    return 1;
-  else
-    return 0;
+  if(name)
+    {
+      len=strlen(name);
+      if ( ( len>=3 && strcmp(&name[len-3], "eps") == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "EPS") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "epsf") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "epsi") == 0 ) )
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
@@ -63,13 +67,17 @@ gal_eps_name_is_eps(char *name)
 int
 gal_eps_suffix_is_eps(char *name)
 {
-  if (strcmp(name, "eps") == 0 || strcmp(name, ".eps") == 0
-      || strcmp(name, "EPS") == 0 || strcmp(name, ".EPS") == 0
-      || strcmp(name, "epsf") == 0 || strcmp(name, ".epsf") == 0
-      || strcmp(name, "epsi") == 0 || strcmp(name, ".epsi") == 0)
-    return 1;
-  else
-    return 0;
+  if(name)
+    {
+      if (strcmp(name, "eps") == 0 || strcmp(name, ".eps") == 0
+          || strcmp(name, "EPS") == 0 || strcmp(name, ".EPS") == 0
+          || strcmp(name, "epsf") == 0 || strcmp(name, ".epsf") == 0
+          || strcmp(name, "epsi") == 0 || strcmp(name, ".epsi") == 0)
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
diff --git a/lib/fits.c b/lib/fits.c
index dd2d3bf..fbbea32 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -101,16 +101,21 @@ int
 gal_fits_name_is_fits(char *name)
 {
   size_t len;
-  len=strlen(name);
-  if ( (    len>=3 && strcmp(&name[len-3], "fit"     ) == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "fits"    ) == 0 )
-       || ( len>=7 && strcmp(&name[len-7], "fits.gz" ) == 0 )
-       || ( len>=6 && strcmp(&name[len-6], "fits.Z"  ) == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "imh"     ) == 0 )
-       || ( len>=7 && strcmp(&name[len-7], "fits.fz" ) == 0 ) )
-    return 1;
-  else
-    return 0;
+
+  if(name)
+    {
+      len=strlen(name);
+      if ( (    len>=3 && strcmp(&name[len-3], "fit"     ) == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "fits"    ) == 0 )
+           || ( len>=7 && strcmp(&name[len-7], "fits.gz" ) == 0 )
+           || ( len>=6 && strcmp(&name[len-6], "fits.Z"  ) == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "imh"     ) == 0 )
+           || ( len>=7 && strcmp(&name[len-7], "fits.fz" ) == 0 ) )
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
@@ -124,16 +129,22 @@ gal_fits_name_is_fits(char *name)
 int
 gal_fits_suffix_is_fits(char *suffix)
 {
-  char *nodot = suffix[0]=='.' ? (suffix+1) : suffix;
-  if ( strcmp(   nodot, "fit"     ) == 0
-       || strcmp(nodot, "fits"    ) == 0
-       || strcmp(nodot, "fits.gz" ) == 0
-       || strcmp(nodot, "fits.Z"  ) == 0
-       || strcmp(nodot, "imh"     ) == 0
-       || strcmp(nodot, "fits.fz" ) == 0 )
-    return 1;
-  else
-    return 0;
+  char *nodot;
+
+  if(suffix)
+    {
+      nodot=suffix[0]=='.' ? (suffix+1) : suffix;
+      if ( strcmp(   nodot, "fit"     ) == 0
+           || strcmp(nodot, "fits"    ) == 0
+           || strcmp(nodot, "fits.gz" ) == 0
+           || strcmp(nodot, "fits.Z"  ) == 0
+           || strcmp(nodot, "imh"     ) == 0
+           || strcmp(nodot, "fits.fz" ) == 0 )
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
diff --git a/lib/jpeg.c b/lib/jpeg.c
index 34dc71b..2e86e60 100644
--- a/lib/jpeg.c
+++ b/lib/jpeg.c
@@ -48,18 +48,23 @@ int
 gal_jpeg_name_is_jpeg(char *name)
 {
   size_t len;
-  len=strlen(name);
-  if ( ( len>=3 && strcmp(&name[len-3], "jpg") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "JPG") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "jpeg") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "JPEG") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "jpe") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "jif") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "jfif") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "jfi") == 0 ) )
-    return 1;
-  else
-    return 0;
+
+  if(name)
+    {
+      len=strlen(name);
+      if ( ( len>=3 && strcmp(&name[len-3], "jpg") == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "JPG") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "jpeg") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "JPEG") == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "jpe") == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "jif") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "jfif") == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "jfi") == 0 ) )
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
@@ -69,17 +74,21 @@ gal_jpeg_name_is_jpeg(char *name)
 int
 gal_jpeg_suffix_is_jpeg(char *name)
 {
-  if (strcmp(name, "jpg") == 0   || strcmp(name, ".jpg") == 0
-      || strcmp(name, "JPG") == 0 || strcmp(name, ".JPG") == 0
-      || strcmp(name, "jpeg") == 0 || strcmp(name, ".jpeg") == 0
-      || strcmp(name, "JPEG") == 0 || strcmp(name, ".JPEG") == 0
-      || strcmp(name, "jpe") == 0 || strcmp(name, ".jpe") == 0
-      || strcmp(name, "jif") == 0 || strcmp(name, ".jif") == 0
-      || strcmp(name, "jfif") == 0 || strcmp(name, ".jfif") == 0
-      || strcmp(name, "jfi") == 0 || strcmp(name, ".jfi") == 0)
-    return 1;
-  else
-    return 0;
+  if(name)
+    {
+      if (strcmp(name, "jpg") == 0   || strcmp(name, ".jpg") == 0
+          || strcmp(name, "JPG") == 0 || strcmp(name, ".JPG") == 0
+          || strcmp(name, "jpeg") == 0 || strcmp(name, ".jpeg") == 0
+          || strcmp(name, "JPEG") == 0 || strcmp(name, ".JPEG") == 0
+          || strcmp(name, "jpe") == 0 || strcmp(name, ".jpe") == 0
+          || strcmp(name, "jif") == 0 || strcmp(name, ".jif") == 0
+          || strcmp(name, "jfif") == 0 || strcmp(name, ".jfif") == 0
+          || strcmp(name, "jfi") == 0 || strcmp(name, ".jfi") == 0)
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
diff --git a/lib/pdf.c b/lib/pdf.c
index 29628c8..c3b71ee 100644
--- a/lib/pdf.c
+++ b/lib/pdf.c
@@ -44,12 +44,17 @@ int
 gal_pdf_name_is_pdf(char *name)
 {
   size_t len;
-  len=strlen(name);
-  if (strcmp(&name[len-3], "pdf") == 0
-      || strcmp(&name[len-3], "PDF") == 0)
-    return 1;
-  else
-    return 0;
+
+  if(name)
+    {
+      len=strlen(name);
+      if (strcmp(&name[len-3], "pdf") == 0
+          || strcmp(&name[len-3], "PDF") == 0)
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
@@ -59,11 +64,15 @@ gal_pdf_name_is_pdf(char *name)
 int
 gal_pdf_suffix_is_pdf(char *name)
 {
-  if (strcmp(name, "pdf") == 0 || strcmp(name, ".pdf") == 0
-      || strcmp(name, "PDF") == 0 || strcmp(name, ".PDF") == 0)
-    return 1;
-  else
-    return 0;
+  if(name)
+    {
+      if (strcmp(name, "pdf") == 0 || strcmp(name, ".pdf") == 0
+          || strcmp(name, "PDF") == 0 || strcmp(name, ".PDF") == 0)
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
diff --git a/lib/tableintern.c b/lib/tableintern.c
index f1c8e8b..25a1c24 100644
--- a/lib/tableintern.c
+++ b/lib/tableintern.c
@@ -62,7 +62,7 @@ gal_tableintern_error_col_selection(char *filename, char *hdu,
                    filename, hdu)<0 )
         error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
     }
-  else command=name=filename;
+  else command=name=filename?filename:"stdin";
 
   /* Abort with with the proper error. */
   error(EXIT_FAILURE, 0, "%s: %s\n\nFor more information on selecting "
diff --git a/lib/tiff.c b/lib/tiff.c
index 3e9894b..a853746 100644
--- a/lib/tiff.c
+++ b/lib/tiff.c
@@ -52,14 +52,19 @@ int
 gal_tiff_name_is_tiff(char *name)
 {
   size_t len;
-  len=strlen(name);
-  if ( ( len>=3 && strcmp(&name[len-3], "tif") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "TIF") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "tiff") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "TIFF") == 0 ) )
-    return 1;
-  else
-    return 0;
+
+  if(name)
+    {
+      len=strlen(name);
+      if ( ( len>=3 && strcmp(&name[len-3], "tif") == 0 )
+           || ( len>=3 && strcmp(&name[len-3], "TIF") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "tiff") == 0 )
+           || ( len>=4 && strcmp(&name[len-4], "TIFF") == 0 ) )
+        return 1;
+      else
+        return 0;
+    }
+  else return 0;
 }
 
 
@@ -69,13 +74,17 @@ gal_tiff_name_is_tiff(char *name)
 int
 gal_tiff_suffix_is_tiff(char *name)
 {
-  if (strcmp(name, "tif") == 0   || strcmp(name, ".tif") == 0
-      || strcmp(name, "TIF") == 0 || strcmp(name, ".TIF") == 0
-      || strcmp(name, "tiff") == 0 || strcmp(name, ".tiff") == 0
-      || strcmp(name, "TIFF") == 0 || strcmp(name, ".TIFF") == 0 )
-    return 1;
-  else
-    return 0;
+  if(name)
+    {
+    if (strcmp(name, "tif") == 0   || strcmp(name, ".tif") == 0
+        || strcmp(name, "TIF") == 0 || strcmp(name, ".TIF") == 0
+        || strcmp(name, "tiff") == 0 || strcmp(name, ".tiff") == 0
+        || strcmp(name, "TIFF") == 0 || strcmp(name, ".TIFF") == 0 )
+      return 1;
+    else
+      return 0;
+    }
+  else return 0;
 }
 
 



reply via email to

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