gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master c315491 075/125: Header using new option manag


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master c315491 075/125: Header using new option management system
Date: Sun, 23 Apr 2017 22:36:41 -0400 (EDT)

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

    Header using new option management system
    
    The new option management system has been implemented for the Header
    program. It can still benefit in many aspects from `gal_data_t', but for
    the time being they aren't crucial (and I don't have much time!).
---
 bin/cosmiccal/main.h                  |  26 +--
 bin/cosmiccal/ui.c                    |   2 -
 bin/header/args.h                     | 294 ++++++++--------------------
 bin/header/{cite.h => authors-cite.h} |  30 +--
 bin/header/header.c                   |  16 +-
 bin/header/main.c                     |   6 +-
 bin/header/main.h                     |  56 +++---
 bin/header/ui.c                       | 356 +++++++++++++++++++---------------
 bin/header/ui.h                       |   7 +-
 tests/Makefile.am                     |   2 +-
 tmpfs-config-make                     |   4 +-
 11 files changed, 354 insertions(+), 445 deletions(-)

diff --git a/bin/cosmiccal/main.h b/bin/cosmiccal/main.h
index 6755958..644999d 100644
--- a/bin/cosmiccal/main.h
+++ b/bin/cosmiccal/main.h
@@ -46,24 +46,24 @@ struct cosmiccalparams
   struct gal_options_common_params cp;  /* Common parameters.           */
 
   /* Input: */
-  double             redshift;  /* Redshift of interest.                */
-  double                   H0;  /* Current expansion rate (km/sec/Mpc). */
-  double              olambda;  /* Current cosmological constant dens.  */
-  double              omatter;  /* Current matter density.              */
-  double           oradiation;  /* Current radiation density.           */
-  double           solidangle;  /* Solid angle for volume (in stradian).*/
+  double              redshift; /* Redshift of interest.                */
+  double                    H0; /* Current expansion rate (km/sec/Mpc). */
+  double               olambda; /* Current cosmological constant dens.  */
+  double               omatter; /* Current matter density.              */
+  double            oradiation; /* Current radiation density.           */
+  double            solidangle; /* Solid angle for volume (in stradian).*/
 
   /* Output: */
-  int              onlyvolume;  /* Only print the volume in Mpc^3.      */
-  int          onlyabsmagconv;  /* Only print conversion to abs. mag.   */
+  unsigned char     onlyvolume; /* Only print the volume in Mpc^3.      */
+  unsigned char onlyabsmagconv; /* Only print conversion to abs. mag.   */
 
   /* Internal: */
-  double                    K;  /* Curvature constant.                  */
-  double                    c;  /* Speed of light.                      */
-  double                  H0s;  /* Current expansion rate (1/sec).      */
-  double                ocurv;  /* Curvature density today.             */
+  double                     K; /* Curvature constant.                  */
+  double                     c; /* Speed of light.                      */
+  double                   H0s; /* Current expansion rate (1/sec).      */
+  double                 ocurv; /* Curvature density today.             */
 
-  time_t              rawtime;  /* Starting time of the program.        */
+  time_t               rawtime; /* Starting time of the program.        */
 };
 
 #endif
diff --git a/bin/cosmiccal/ui.c b/bin/cosmiccal/ui.c
index eb49d2b..fe4aa75 100644
--- a/bin/cosmiccal/ui.c
+++ b/bin/cosmiccal/ui.c
@@ -120,10 +120,8 @@ ui_initialize_options(struct cosmiccalparams *p,
                       struct argp_option *program_options,
                       struct argp_option *gal_commonopts_options)
 {
-  size_t i;
   struct gal_options_common_params *cp=&p->cp;
 
-
   /* Set the necessary common parameters structure. */
   cp->poptions           = program_options;
   cp->program_name       = PROGRAM_NAME;
diff --git a/bin/header/args.h b/bin/header/args.h
index 3d96892..614f4cb 100644
--- a/bin/header/args.h
+++ b/bin/header/args.h
@@ -5,7 +5,7 @@ Header is part of GNU Astronomy Utilities (Gnuastro) package.
 Original author:
      Mohammad Akhlaghi <address@hidden>
 Contributing author(s):
-Copyright (C) 2015, Free Software Foundation, Inc.
+Copyright (C) 2016, Free Software Foundation, Inc.
 
 Gnuastro is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
@@ -23,162 +23,137 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #ifndef ARGS_H
 #define ARGS_H
 
-#include <argp.h>
 
-#include <gnuastro/linkedlist.h>
 
-#include <commonargs.h>
-#include <fixedstringmacros.h>
 
 
 
-
-
-
-
-
-
-
-/**************************************************************/
-/**************        argp.h definitions       ***************/
-/**************************************************************/
-
-
-
-
-/* Definition parameters for the argp: */
-const char *argp_program_version=SPACK_STRING"\n"GAL_STRINGS_COPYRIGHT
-  "\n\nWritten by Mohammad Akhlaghi";
-const char *argp_program_bug_address=PACKAGE_BUGREPORT;
-static char args_doc[] = "ASTRdata";
-
-
-
-
-
-const char doc[] =
-  /* Before the list of options: */
-  GAL_STRINGS_TOP_HELP_INFO
-  SPACK_NAME" print the header information in any astronomical data file"
-  "header. It can also manipulate (add, remove or modify) any of the "
-  "existing keywords in a data header. \n"
-  GAL_STRINGS_MORE_HELP_INFO
-  /* After the list of options: */
-  "\v"
-  PACKAGE_NAME" home page: "PACKAGE_URL;
-
-
-
-
-
-/* Available letters for short options:
-
-   b e f g i j k l m n p s v x y z
-   A B C E F G I J L M O R T U W X Y Z
-
-   Number keys used: Nothing!
-
-   Options with keys (second structure element) larger than 500 do not
-   have a short version.
- */
-static struct argp_option options[] =
+/* Array of acceptable options. */
+struct argp_option program_options[] =
   {
     {
-      0, 0, 0, 0,
-      "Input:",
-      1
-    },
-
-
-
-
-    {
-      0, 0, 0, 0,
-      "Output:",
-      2
-    },
-    {
       "asis",
-      'a',
+      ARGS_OPTION_KEY_ASIS,
       "STR",
       0,
       "Write the argument string as is into the header.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->asis,
+      GAL_DATA_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "delete",
-      'd',
+      ARGS_OPTION_KEY_DELETE,
       "STR",
       0,
       "Delete a keyword from the header.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->delete,
+      GAL_DATA_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "rename",
-      'r',
+      ARGS_OPTION_KEY_RENAME,
       "STR",
       0,
       "Rename keyword, keeping value and comments.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->rename,
+      GAL_DATA_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "update",
-      'u',
+      ARGS_OPTION_KEY_UPDATE,
       "STR",
       0,
       "Update a keyword value or comments.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->updatestr,
+      GAL_DATA_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "write",
-      'w',
+      ARGS_OPTION_KEY_WRITE,
       "STR",
       0,
       "Write a keyword (with value, comments and units).",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->writestr,
+      GAL_DATA_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "history",
-      'H',
+      ARGS_OPTION_KEY_HISTORY,
       "STR",
       0,
       "Add HISTORY keyword, any length is ok.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->history,
+      GAL_DATA_TYPE_STRING,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "comment",
-      'c',
+      ARGS_OPTION_KEY_COMMENT,
       "STR",
       0,
       "Add COMMENT keyword, any length is ok.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->comment,
+      GAL_DATA_TYPE_STRING,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "date",
-      't',
+      ARGS_OPTION_KEY_DATE,
       0,
       0,
       "Set the DATE keyword to the current time.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->date,
+      GAL_OPTIONS_NO_ARG_TYPE,
+      GAL_OPTIONS_RANGE_0_OR_1,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
 
 
 
     {
-      0, 0, 0, 0,
-      "Operating modes:",
-      -1
-    },
-    {
       "quitonerror",
-      'Q',
+      ARGS_OPTION_KEY_QUITONERROR,
       0,
       0,
       "Quit if there is an error on any action.",
-      -1
+      GAL_OPTIONS_GROUP_OPERATING_MODE,
+      &p->quitonerror,
+      GAL_OPTIONS_NO_ARG_TYPE,
+      GAL_OPTIONS_RANGE_0_OR_1,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
 
 
+
     {0}
   };
 
@@ -186,126 +161,21 @@ static struct argp_option options[] =
 
 
 
-/* Parse a single option: */
-static error_t
-parse_opt(int key, char *arg, struct argp_state *state)
-{
-  /* Save the arguments structure: */
-  struct headerparams *p = state->input;
+/* Define the child argp structure. */
+struct argp
+gal_options_common_child = {gal_commonopts_options,
+                            gal_options_common_argp_parse,
+                            NULL, NULL, NULL, NULL, NULL};
 
-  /* Set the pointer to the common parameters for all programs
-     here: */
-  state->child_inputs[0]=&p->cp;
-
-  /* In case the user incorrectly uses the equal sign (for example
-     with a short format or with space in the long format, then `arg`
-     start with (if the short version was called) or be (if the long
-     version was called with a space) the equal sign. So, here we
-     check if the first character of arg is the equal sign, then the
-     user is warned and the program is stopped: */
-  if(arg && arg[0]=='=')
-    argp_error(state, "incorrect use of the equal sign (`=`). For short "
-               "options, `=` should not be used and for long options, "
-               "there should be no space between the option, equal sign "
-               "and value");
-
-  switch(key)
-    {
-
-
-    /* Input: */
-
-
-
-    /* Output: */
-    case 'a':
-      gal_linkedlist_add_to_stll(&p->asis, arg);
-      break;
-    case 'd':
-      gal_linkedlist_add_to_stll(&p->delete, arg);
-      break;
-    case 'r':
-      gal_linkedlist_add_to_stll(&p->up.rename, arg);
-      break;
-    case 'u':
-      gal_linkedlist_add_to_stll(&p->up.update, arg);
-      break;
-    case 'w':
-      gal_linkedlist_add_to_stll(&p->up.write, arg);
-      break;
-    case 'c':
-      p->comment=arg;
-      break;
-    case 'H':
-      p->history=arg;
-      break;
-    case 't':
-      p->date=1;
-      break;
-
-    /* Operating modes: */
-    case 'Q':
-      p->quitonerror=1;
-
-
-    /* Read the non-option arguments: */
-    case ARGP_KEY_ARG:
-
-      /* See what type of input value it is and put it in. */
-      if( gal_fits_name_is_fits(arg) )
-        {
-          if(p->up.inputname)
-            argp_error(state, "only one input image should be given");
-          else
-            p->up.inputname=arg;
-        }
-      else
-        argp_error(state, "%s is not a valid file type", arg);
-      break;
-
-
-
-
-
-    /* The command line options and arguments are finished. */
-    case ARGP_KEY_END:
-      if(p->cp.setdirconf==0 && p->cp.setusrconf==0
-         && p->cp.printparams==0)
-        {
-          if(state->arg_num==0)
-            argp_error(state, "no argument given");
-          if(p->up.inputname==NULL)
-            argp_error(state, "no input FITS image(s) provided");
-        }
-      break;
-
-
-
-
-
-    default:
-      return ARGP_ERR_UNKNOWN;
-    }
-  return 0;
-}
-
-
-
-
-
-/* Specify the children parsers: */
-struct argp_child children[]=
-  {
-    {&commonargp, 0, NULL, 0},
-    {0, 0, 0, 0}
-  };
-
-
-
-
-
-/* Basic structure defining the whole argument reading process. */
-static struct argp thisargp = {options, parse_opt, args_doc,
-                               doc, children, NULL, NULL};
+/* Use the child argp structure in list of children (only one for now). */
+struct argp_child
+children[]=
+{
+  {&gal_options_common_child, 0, NULL, 0},
+  {0, 0, 0, 0}
+};
 
+/* Set all the necessary argp parameters. */
+struct argp
+thisargp = {program_options, parse_opt, args_doc, doc, children, NULL, NULL};
 #endif
diff --git a/bin/header/cite.h b/bin/header/authors-cite.h
similarity index 57%
rename from bin/header/cite.h
rename to bin/header/authors-cite.h
index dcf0fd9..3ab492d 100644
--- a/bin/header/cite.h
+++ b/bin/header/authors-cite.h
@@ -5,7 +5,7 @@ Header is part of GNU Astronomy Utilities (Gnuastro) package.
 Original author:
      Mohammad Akhlaghi <address@hidden>
 Contributing author(s):
-Copyright (C) 2015, Free Software Foundation, Inc.
+Copyright (C) 2017, Free Software Foundation, Inc.
 
 Gnuastro is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
@@ -20,19 +20,19 @@ General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/
-#ifndef CITE_H
-#define CITE_H
-
-#define HEADERBIBTEX ""
-
-#define PRINTCITEABORT {                                                \
-    printf("\nWe hope %s has been useful for your research.\n"          \
-           "Citations are vital for the continued work on %s.\n"        \
-           "Thank you for citing it in your research paper.\n"          \
-           "\nPlease cite as \"%s\":\n\n%s\n\n%s",                      \
-           SPACK_NAME, SPACK_NAME, SPACK_STRING,                        \
-           GAL_STRINGS_MAIN_BIBTEX, HEADERBIBTEX);                      \
-    exit(EXIT_SUCCESS);                                                 \
-  }
+#ifndef AUTHORS_CITE_H
+#define AUTHORS_CITE_H
+
+/* When any specific citation is necessary, please add its BibTeX (from ADS
+   hopefully) to this variable along with a title decribing what this
+   paper/book does for the progarm in a short line. In the following line
+   put a row of `-' with the same length and then put the BibTeX.
+
+   See the `gnuastro_bibtex' variable in `lib/options' (from the top
+   Gnuastro source code directory) as an example.*/
+
+#define PROGRAM_BIBTEX "";
+
+#define PROGRAM_AUTHORS "Mohammad Akhlaghi";
 
 #endif
diff --git a/bin/header/header.c b/bin/header/header.c
index 16ea9eb..95797c7 100644
--- a/bin/header/header.c
+++ b/bin/header/header.c
@@ -88,14 +88,14 @@ writeupdatekeys(fitsfile *fptr, struct gal_fits_key_ll 
**keylist,
   tmp=*keylist;
   while(tmp!=NULL)
     {
-
       /* Write the information: */
       if(u1w2==1)
         {
           if(tmp->value)
             {
-              if( fits_update_key(fptr, tmp->datatype, tmp->keyname,
-                                  tmp->value, tmp->comment, &status) )
+              if( fits_update_key(fptr, gal_fits_type_to_datatype(tmp->type),
+                                  tmp->keyname, tmp->value, tmp->comment,
+                                  &status) )
                 gal_fits_io_error(status, NULL);
             }
           else
@@ -109,8 +109,9 @@ writeupdatekeys(fitsfile *fptr, struct gal_fits_key_ll 
**keylist,
         {
           if(tmp->value)
             {
-              if( fits_write_key(fptr, tmp->datatype, tmp->keyname,
-                                 tmp->value, tmp->comment, &status) )
+              if( fits_write_key(fptr, gal_fits_type_to_datatype(tmp->type),
+                                 tmp->keyname, tmp->value, tmp->comment,
+                                 &status) )
                 gal_fits_io_error(status, NULL);
             }
           else
@@ -134,8 +135,8 @@ writeupdatekeys(fitsfile *fptr, struct gal_fits_key_ll 
**keylist,
         gal_fits_io_error(status, NULL);
 
       /* Free the value pointer if desired: */
-      if(tmp->kfree) free(tmp->keyname);
       if(tmp->vfree) free(tmp->value);
+      if(tmp->kfree) free(tmp->keyname);
       if(tmp->cfree) free(tmp->comment);
 
       /* Keep the pointer to the next keyword and free the allocated
@@ -194,7 +195,7 @@ header(struct headerparams *p)
               if(status) r=haserror(p, 1, tstll->v, status);
             }
         }
-      if(p->up.rename)
+      if(p->rename)
         {
           ttstll=p->renameto;
           for(tstll=p->renamefrom; tstll!=NULL; tstll=tstll->next)
@@ -230,6 +231,5 @@ header(struct headerparams *p)
           if(status) r=haserror(p, 4, "DATE", status);
         }
     }
-
   return r;
 }
diff --git a/bin/header/main.c b/bin/header/main.c
index 3613b47..6197358 100644
--- a/bin/header/main.c
+++ b/bin/header/main.c
@@ -36,19 +36,19 @@ int
 main (int argc, char *argv[])
 {
   int r;
-  struct headerparams p={{0}, {0}, 0};
+  struct headerparams p={{0}, 0};
 
   /* Get the starting time. */
   time(&p.rawtime);
 
   /* Read the input parameters. */
-  setparams(argc, argv, &p);
+  ui_read_check_inputs_setup(argc, argv, &p);
 
   /* Run MakeProfiles */
   r=header(&p);
 
   /* Free all non-freed allocations. */
-  freeandreport(&p);
+  ui_free_and_report(&p);
 
   /* Return successfully.*/
   return r;
diff --git a/bin/header/main.h b/bin/header/main.h
index c024a15..12e1099 100644
--- a/bin/header/main.h
+++ b/bin/header/main.h
@@ -25,58 +25,50 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 #include <gnuastro/fits.h>
 
-#include <commonparams.h>
+#include <options.h>
 
 /* Progarm name macros: */
-#define SPACK           "astheader" /* Subpackage executable name. */
-#define SPACK_NAME      "Header"    /* Subpackage full name.       */
-#define SPACK_STRING    SPACK_NAME" ("PACKAGE_NAME") "PACKAGE_VERSION
+#define PROGRAM_NAME "Header"        /* Program full name.       */
+#define PROGRAM_EXEC "astheader"     /* Program executable name. */
+#define PROGRAM_STRING PROGRAM_NAME" (" PACKAGE_NAME ") " PACKAGE_VERSION
 
 
 
 
 
 
-struct uiparams
-{
-  char                     *inputname;  /* Name of input file.             */
-  struct gal_linkedlist_stll  *rename;  /* Rename a keyword.               */
-  struct gal_linkedlist_stll  *update;  /* For keywords to update.         */
-  struct gal_linkedlist_stll   *write;  /* Full arg. for keywords to add.  */
-};
-
-
-
-
 
 struct headerparams
 {
-  /* Other structures: */
-  struct uiparams          up;  /* User interface parameters.         */
-  struct gal_commonparams  cp;  /* Common parameters.                 */
+  /* Common parameters */
+  struct gal_options_common_params  cp;  /* Common parameters.        */
 
   /* Input: */
-  int                    nwcs;  /* Number of WCS structures.          */
-  fitsfile              *fptr;  /* FITS file pointer.                 */
-  struct wcsprm          *wcs;  /* Pointer to WCS structures.         */
+  int                            nwcs;  /* Number of WCS structures.       */
+  fitsfile                      *fptr;  /* FITS file pointer.              */
+  struct wcsprm                  *wcs;  /* Pointer to WCS structures.      */
 
   /* Output: */
-  int                    date;  /* Set DATE to current time.          */
-  char               *comment;  /* COMMENT value.                     */
-  char               *history;  /* HISTORY value.                     */
-  struct gal_linkedlist_stll *asis; /* Strings to write as is into header.*/
-  struct gal_linkedlist_stll *delete; /* Keywords to remove.          */
-  struct gal_linkedlist_stll *renamefrom; /* The initial value of the keyword. 
*/
+  unsigned char                  date;  /* Set DATE to current time.       */
+  char                       *comment;  /* COMMENT value.                  */
+  char                       *history;  /* HISTORY value.                  */
+  struct gal_linkedlist_stll    *asis;  /* Strings to write asis.          */
+  struct gal_linkedlist_stll  *delete;  /* Keywords to remove.             */
+  struct gal_linkedlist_stll *renamefrom; /* Initial value of the keyword. */
   struct gal_linkedlist_stll *renameto; /* The final value of the keyword. */
-  struct gal_fits_key_ll *update; /* Linked list of keywords to update. */
-  struct gal_fits_key_ll *write;  /* Linked list of keywords to add. */
+  struct gal_fits_key_ll      *update;  /* Keywords to update. */
+  struct gal_fits_key_ll       *write;  /* Keywords to add.                */
 
   /* Operating mode: */
-  int             quitonerror;  /* Quit if an error occurs.           */
+  unsigned char           quitonerror;  /* Quit if an error occurs.        */
 
   /* Internal: */
-  int                onlyview;
-  time_t              rawtime;  /* Starting time of the program.      */
+  int                        onlyview;
+  time_t                      rawtime;  /* Starting time of the program.   */
+  char                      *filename;  /* Name of input file.             */
+  struct gal_linkedlist_stll  *rename;  /* Rename a keyword.               */
+  struct gal_linkedlist_stll *updatestr;/* For keywords to update.         */
+  struct gal_linkedlist_stll *writestr; /* Full arg. for keywords to add.  */
 };
 
 #endif
diff --git a/bin/header/ui.c b/bin/header/ui.c
index 65706bf..ec9022b 100644
--- a/bin/header/ui.c
+++ b/bin/header/ui.c
@@ -5,7 +5,7 @@ Header is part of GNU Astronomy Utilities (Gnuastro) package.
 Original author:
      Mohammad Akhlaghi <address@hidden>
 Contributing author(s):
-Copyright (C) 2015, Free Software Foundation, Inc.
+Copyright (C) 2016, Free Software Foundation, Inc.
 
 Gnuastro is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
@@ -22,153 +22,167 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 **********************************************************************/
 #include <config.h>
 
-#include <math.h>
-#include <stdio.h>
+#include <argp.h>
 #include <errno.h>
 #include <error.h>
-#include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
-#include <fitsio.h>
 
 #include <gnuastro/fits.h>
-#include <gnuastro/txtarray.h>
+#include <gnuastro/linkedlist.h>
 
-#include <nproc.h>               /* From Gnulib.                   */
-#include <timing.h>              /* Includes time.h and sys/time.h */
+#include <options.h>
 #include <checkset.h>
-#include <commonargs.h>
-#include <configfiles.h>
+#include <fixedstringmacros.h>
 
 #include "main.h"
 
-#include "ui.h"                  /* Needs main.h                   */
-#include "args.h"                /* Needs main.h, includes argp.h. */
+#include "ui.h"
+#include "authors-cite.h"
 
 
-/* Set the file names of the places where the default parameters are
-   put. */
-#define CONFIG_FILE SPACK CONF_POSTFIX
-#define SYSCONFIG_FILE SYSCONFIG_DIR "/" CONFIG_FILE
-#define USERCONFIG_FILEEND USERCONFIG_DIR CONFIG_FILE
-#define CURDIRCONFIG_FILE CURDIRCONFIG_DIR CONFIG_FILE
 
 
 
+/**************************************************************/
+/*********      Argp necessary global entities     ************/
+/**************************************************************/
+/* Definition parameters for the Argp: */
+const char *
+argp_program_version = PROGRAM_STRING "\n"
+                       GAL_STRINGS_COPYRIGHT
+                       "\n\nWritten/developed by "PROGRAM_AUTHORS;
 
+const char *
+argp_program_bug_address = PACKAGE_BUGREPORT;
 
+static char
+args_doc[] = "ASTRdata";
 
+const char
+doc[] = GAL_STRINGS_TOP_HELP_INFO PROGRAM_NAME" print the header "
+  "information in any astronomical data file header. It can also "
+  "manipulate (add, remove or modify) any of the existing keywords in "
+  "a data header. \n"
+  GAL_STRINGS_MORE_HELP_INFO
+  /* After the list of options: */
+  "\v"
+  PACKAGE_NAME" home page: "PACKAGE_URL;
 
 
 
 
-/**************************************************************/
-/**************       Options and parameters    ***************/
-/**************************************************************/
-void
-readconfig(char *filename, struct headerparams *p)
-{
-  FILE *fp;
-  size_t lineno=0, len=200;
-  char *line, *name, *value;
-  /*struct uiparams *up=&p->up;*/
-  struct gal_commonparams *cp=&p->cp;
-  char key='a';        /* Not used, just a place holder. */
-
-  /* When the file doesn't exist or can't be opened, it is ignored. It
-     might be intentional, so there is no error. If a parameter is
-     missing, it will be reported after all defaults are read. */
-  fp=fopen(filename, "r");
-  if (fp==NULL) return;
-
-
-  /* Allocate some space for `line` with `len` elements so it can
-     easily be freed later on. The value of `len` is arbitarary at
-     this point, during the run, getline will change it along with the
-     pointer to line. */
-  errno=0;
-  line=malloc(len*sizeof *line);
-  if(line==NULL)
-    error(EXIT_FAILURE, errno, "ui.c: %zu bytes in readdefaults",
-          len * sizeof *line);
 
-  /* Read the tokens in the file:  */
-  while(getline(&line, &len, fp) != -1)
-    {
-      /* Prepare the "name" and "value" strings, also set lineno. */
-      GAL_CONFIGFILES_START_READING_LINE;
+/* Available letters for short options:
 
+   a b d e f g j k l m n p r s t u v w x y z
+   A B C E F G H J L M O Q R T U W X Y Z  */
+enum option_keys_enum
+{
+  /* With short-option version. */
+  ARGS_OPTION_KEY_ASIS        = 'a',
+  ARGS_OPTION_KEY_DELETE      = 'd',
+  ARGS_OPTION_KEY_RENAME      = 'r',
+  ARGS_OPTION_KEY_UPDATE      = 'u',
+  ARGS_OPTION_KEY_WRITE       = 'w',
+  ARGS_OPTION_KEY_COMMENT     = 'c',
+  ARGS_OPTION_KEY_HISTORY     = 'h',
+  ARGS_OPTION_KEY_DATE        = 't',
+  ARGS_OPTION_KEY_QUITONERROR = 'Q',
 
 
+  /* Only with long version (start with a value 1000, the rest will be set
+     automatically). */
+};
+
 
-      /* Inputs: */
-      if(strcmp(name, "hdu")==0)
-        gal_checkset_allocate_copy_set(value, &cp->hdu, &cp->hduset);
 
 
 
-      /* Outputs */
 
 
 
 
-      /* Operating modes: */
-      /* Read options common to all programs */
-      GAL_CONFIGFILES_READ_COMMONOPTIONS_FROM_CONF
 
 
-      else
-        error_at_line(EXIT_FAILURE, 0, filename, lineno,
-                      "`%s` not recognized.\n", name);
-    }
 
-  free(line);
-  fclose(fp);
-}
 
 
 
 
 
-void
-printvalues(FILE *fp, struct headerparams *p)
-{
-  /*struct uiparams *up=&p->up;*/
-  struct gal_commonparams *cp=&p->cp;
 
 
-  /* Print all the options that are set. Separate each group with a
-     commented line explaining the options in that group. */
-  fprintf(fp, "\n# Input image:\n");
-  if(cp->hduset)
-    GAL_CHECKSET_PRINT_STRING_MAYBE_WITH_SPACE("hdu", cp->hdu);
+/**************************************************************/
+/*********    Initialize & Parse command-line    **************/
+/**************************************************************/
+static void
+ui_initialize_options(struct headerparams *p,
+                      struct argp_option *program_options,
+                      struct argp_option *gal_commonopts_options)
+{
+  struct gal_options_common_params *cp=&p->cp;
 
 
-  /* For the operating mode, first put the macro to print the common
-     options, then the (possible options particular to this
-     program). */
-  fprintf(fp, "\n# Operating mode:\n");
-  GAL_CONFIGFILES_PRINT_COMMONOPTIONS;
+  /* Set the necessary common parameters structure. */
+  cp->poptions           = program_options;
+  cp->program_name       = PROGRAM_NAME;
+  cp->program_exec       = PROGRAM_EXEC;
+  cp->program_bibtex     = PROGRAM_BIBTEX;
+  cp->program_authors    = PROGRAM_AUTHORS;
+  cp->coptions           = gal_commonopts_options;
 }
 
 
 
 
 
-
-/* Note that numthreads will be used automatically based on the
-   configure time. */
-void
-checkifset(struct headerparams *p)
+/* Parse a single option: */
+error_t
+parse_opt(int key, char *arg, struct argp_state *state)
 {
-  /*struct uiparams *up=&p->up;*/
-  struct gal_commonparams *cp=&p->cp;
+  struct headerparams *p = state->input;
+
+  /* Pass `gal_options_common_params' into the child parser.  */
+  state->child_inputs[0] = &p->cp;
+
+  /* In case the user incorrectly uses the equal sign (for example
+     with a short format or with space in the long format, then `arg`
+     start with (if the short version was called) or be (if the long
+     version was called with a space) the equal sign. So, here we
+     check if the first character of arg is the equal sign, then the
+     user is warned and the program is stopped: */
+  if(arg && arg[0]=='=')
+    argp_error(state, "incorrect use of the equal sign (`=`). For short "
+               "options, `=` should not be used and for long options, "
+               "there should be no space between the option, equal sign "
+               "and value");
+
+  /* Set the key to this option. */
+  switch(key)
+    {
 
-  int intro=0;
-  if(cp->hduset==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("hdu");
+    /* Read the non-option tokens (arguments): */
+    case ARGP_KEY_ARG:
+      /* Only FITS files are acceptable. */
+      if( gal_fits_name_is_fits(arg) )
+        {
+          if(p->filename)
+            argp_error(state, "only one input file should be given");
+          else
+            p->filename=arg;
+        }
+      else
+        argp_error(state, "%s is not a recognized FITS file", arg);
+      break;
 
 
-  GAL_CONFIGFILES_END_OF_NOTSET_REPORT;
+    /* This is an option, set its value. */
+    default:
+      return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
+    }
+
+  return 0;
 }
 
 
@@ -193,11 +207,15 @@ checkifset(struct headerparams *p)
 /**************************************************************/
 /***************       Sanity Check         *******************/
 /**************************************************************/
-void
-sanitycheck(struct headerparams *p)
+/* Read and check ONLY the options. When arguments are involved, do the
+   check in `ui_check_options_and_arguments'. */
+static void
+ui_read_check_only_options(struct headerparams *p)
 {
-  if(p->delete || p->up.update || p->up.write || p->asis || p->comment
-     || p->history || p->date || p->up.rename)
+  /* See if the user just wants to view the header or actually do
+     something. */
+  if(p->delete || p->updatestr || p->writestr || p->asis || p->comment
+     || p->history || p->date || p->rename)
     p->onlyview=0;
   else
     p->onlyview=1;
@@ -207,6 +225,29 @@ sanitycheck(struct headerparams *p)
 
 
 
+static void
+ui_check_options_and_arguments(struct headerparams *p)
+{
+  /* Make sure an input file name was given and if it was a FITS file, that
+     a HDU is also given. */
+  if(p->filename)
+    {
+      if( gal_fits_name_is_fits(p->filename) && p->cp.hdu==NULL )
+        error(EXIT_FAILURE, 0, "%s: no HDU specified. A FITS file can "
+              "contain, multiple HDUs. You can use the `--hdu' (`-h') "
+              "option and give it the HDU number (starting from zero), "
+              "extension name, or anything acceptable by CFITSIO",
+              p->filename);
+    }
+  else
+    error(EXIT_FAILURE, 0, "no input file is specified");
+}
+
+
+
+
+
+
 
 
 
@@ -222,15 +263,15 @@ sanitycheck(struct headerparams *p)
 
 
 /**************************************************************/
-/***************       Preparations         *******************/
+/*****************       Preparations      ********************/
 /**************************************************************/
-void
-setuprename(struct headerparams *p)
+static void
+ui_setup_rename(struct headerparams *p)
 {
   char *c;
   struct gal_linkedlist_stll *tmp;
 
-  for(tmp=p->up.rename; tmp!=NULL; tmp=tmp->next)
+  for(tmp=p->rename; tmp!=NULL; tmp=tmp->next)
     {
       /* `c' is created in case of an error, so the input value can be
          reported. */
@@ -240,8 +281,8 @@ setuprename(struct headerparams *p)
       strcpy(c, tmp->v);
 
       /* Tokenize the input. */
-      gal_linkedlist_add_to_stll(&p->renamefrom, strtok(tmp->v, ", "));
-      gal_linkedlist_add_to_stll(&p->renameto, strtok(NULL, ", "));
+      gal_linkedlist_add_to_stll(&p->renamefrom, strtok(tmp->v, ", "), 1);
+      gal_linkedlist_add_to_stll(&p->renameto, strtok(NULL, ", "), 1);
       if(p->renamefrom->v==NULL || p->renameto->v==NULL)
         error(EXIT_FAILURE, 0, "`%s' could not be tokenized in order to "
               "complete rename. There should be a space character "
@@ -266,16 +307,16 @@ setuprename(struct headerparams *p)
 
 
 
-void
-fillfitsheaderll(struct gal_linkedlist_stll *input,
-                 struct gal_fits_key_ll **output)
+static void
+ui_fill_fits_headerll(struct gal_linkedlist_stll *input,
+                      struct gal_fits_key_ll **output)
 {
   long l, *lp;
   void *fvalue;
   double d, *dp;
-  struct gal_linkedlist_stll *tmp;
-  int i=0, datatype, vfree;
+  int i=0, type, vfree;
   char *c, *cf, *start, *tailptr;
+  struct gal_linkedlist_stll *tmp;
   char *original, *keyname, *value, *comment, *unit;
 
   for(tmp=input; tmp!=NULL; tmp=tmp->next)
@@ -342,13 +383,13 @@ fillfitsheaderll(struct gal_linkedlist_stll *input,
       printf("\n\n-%s-\n-%s-\n-%s-\n-%s-\n", keyname, value, comment, unit);
       */
 
-      /* Find the datatype of the value: */
+      /* Find the of the value: */
       errno=0;
       l=strtol(value, &tailptr, 10);
       if(*tailptr=='\0' && errno==0)
         {
           vfree=1;
-          datatype=TLONG;
+          type=GAL_DATA_TYPE_LONG;
           errno=0;
           fvalue=lp=malloc(sizeof *lp);
           if(lp==NULL)
@@ -363,7 +404,7 @@ fillfitsheaderll(struct gal_linkedlist_stll *input,
           if(*tailptr=='\0' && errno==0)
             {
               vfree=1;
-              datatype=TDOUBLE;
+              type=GAL_DATA_TYPE_DOUBLE;
               errno=0;
               fvalue=dp=malloc(sizeof *dp);
               if(dp==NULL)
@@ -372,11 +413,11 @@ fillfitsheaderll(struct gal_linkedlist_stll *input,
               *dp=d;
             }
           else
-            { fvalue=value; datatype=TSTRING; vfree=0; }
+            { fvalue=value; type=GAL_DATA_TYPE_STRING; vfree=0; }
         }
 
 
-      gal_fits_add_to_key_ll(output, datatype, keyname, 0,
+      gal_fits_add_to_key_ll(output, type, keyname, 0,
                              fvalue, vfree, comment, 0, unit);
       free(original);
     }
@@ -386,38 +427,28 @@ fillfitsheaderll(struct gal_linkedlist_stll *input,
 
 
 
-void
-preparearrays(struct headerparams *p)
+static void
+ui_preparations(struct headerparams *p)
 {
-  size_t len;
   char *ffname;
   int status=0, iomode;
 
   /* Add hdu to filename: */
-  errno=0;
-  len=strlen(p->up.inputname)+strlen(p->cp.hdu)+4;
-  ffname=malloc(len*sizeof *ffname);
-  if(ffname==NULL)
-    error(EXIT_FAILURE, errno, "%zu characters", len);
-  sprintf(ffname, "%s[%s#]", p->up.inputname, p->cp.hdu);
+  asprintf(&ffname, "%s[%s#]", p->filename, p->cp.hdu);
 
   /* Open the FITS file: */
-  if(p->onlyview)
-    iomode=READONLY;
-  else
-    iomode=READWRITE;
+  iomode = p->onlyview ? READONLY : READWRITE;
   if( fits_open_file(&p->fptr, ffname, iomode, &status) )
     gal_fits_io_error(status, "reading file");
   free(ffname);
 
   /* Separate the comma-separated values:  */
-  if(p->up.rename)
-    setuprename(p);
-  if(p->up.update)
-    fillfitsheaderll(p->up.update, &p->update);
-  if(p->up.write)
-    fillfitsheaderll(p->up.write, &p->write);
-
+  if(p->rename)
+    ui_setup_rename(p);
+  if(p->updatestr)
+    ui_fill_fits_headerll(p->updatestr, &p->update);
+  if(p->writestr)
+    ui_fill_fits_headerll(p->writestr, &p->write);
 }
 
 
@@ -437,42 +468,59 @@ preparearrays(struct headerparams *p)
 
 
 
-
 /**************************************************************/
 /************         Set the parameters          *************/
 /**************************************************************/
+
 void
-setparams(int argc, char *argv[], struct headerparams *p)
+ui_read_check_inputs_setup(int argc, char *argv[], struct headerparams *p)
 {
-  struct gal_commonparams *cp=&p->cp;
+  struct gal_options_common_params *cp=&p->cp;
+
 
-  /* Set the non-zero initial values, the structure was initialized to
-     have a zero value for all elements. */
-  cp->spack         = SPACK;
-  cp->verb          = 1;
-  cp->numthreads    = num_processors(NPROC_CURRENT);
-  cp->removedirinfo = 1;
+  /* Include the parameters necessary for argp from this program (`args.h')
+     and for the common options to all Gnuastro (`commonopts.h'). We want
+     to directly put the pointers to the fields in `p' and `cp', so we are
+     simply including the header here to not have to use long macros in
+     those headers which make them hard to read and modify. This also helps
+     in having a clean environment: everything in those headers is only
+     available within the scope of this function. */
+#include <commonopts.h>
+#include "args.h"
 
-  /* Read the arguments. */
+
+  /* Initialize the options and necessary information.  */
+  ui_initialize_options(p, program_options, gal_commonopts_options);
+
+  /* Read the command-line options and arguments. */
   errno=0;
   if(argp_parse(&thisargp, argc, argv, 0, 0, p))
     error(EXIT_FAILURE, errno, "parsing arguments");
 
-  /* Add the user default values and save them if asked. */
-  GAL_CONFIGFILES_CHECK_SET_CONFIG;
 
-  /* Check if all the required parameters are set. */
-  checkifset(p);
+  /* Read the configuration files and set the common values. */
+  gal_options_read_config_set(&p->cp);
+
+
+  /* Read the options into the program's structure, and check them and
+     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
+     output state. */
+  gal_options_print_state(&p->cp);
+
 
-  /* Print the values for each parameter. */
-  if(cp->printparams)
-    GAL_CONFIGFILES_REPORT_PARAMETERS_SET;
+  /* Check that the options and arguments fit well with each other. Note
+     that arguments don't go in a configuration file. So this test should
+     be done after (possibly) printing the option values. */
+  ui_check_options_and_arguments(p);
 
-  /* Do a sanity check. */
-  sanitycheck(p);
 
-  /* Make the array of input images. */
-  preparearrays(p);
+  /* Read/allocate all the necessary starting arrays. */
+  ui_preparations(p);
 }
 
 
@@ -498,7 +546,7 @@ setparams(int argc, char *argv[], struct headerparams *p)
 /************      Free allocated, report         *************/
 /**************************************************************/
 void
-freeandreport(struct headerparams *p)
+ui_free_and_report(struct headerparams *p)
 {
   int status=0;
 
diff --git a/bin/header/ui.h b/bin/header/ui.h
index f596506..2fef561 100644
--- a/bin/header/ui.h
+++ b/bin/header/ui.h
@@ -5,7 +5,7 @@ Header is part of GNU Astronomy Utilities (Gnuastro) package.
 Original author:
      Mohammad Akhlaghi <address@hidden>
 Contributing author(s):
-Copyright (C) 2015, Free Software Foundation, Inc.
+Copyright (C) 2016, Free Software Foundation, Inc.
 
 Gnuastro is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
@@ -24,9 +24,10 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #define UI_H
 
 void
-setparams(int argc, char *argv[], struct headerparams *p);
+ui_read_check_inputs_setup(int argc, char *argv[],
+                           struct headerparams *p);
 
 void
-freeandreport(struct headerparams *p);
+ui_free_and_report(struct headerparams *p);
 
 #endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b852146..ea5074e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -159,7 +159,7 @@ imgwarp/homographic.sh: convolve/spatial.sh.log
 mknoise/addnoise.sh: imgwarp/imgwarp_scale.sh.log
 mkprof/ellipticalmasks.sh: mknoise/addnoise.sh.log
 mkprof/inputascanvas.sh: mknoise/addnoise.sh.log
-header/write.sh: convolve/spatial.sh.log
+header/write.sh: mkprof/mosaic1.sh.log
 header/print.sh: header/write.sh.log
 header/update.sh: header/write.sh.log
 header/delete.sh: header/write.sh.log
diff --git a/tmpfs-config-make b/tmpfs-config-make
index b597e51..60b7fba 100755
--- a/tmpfs-config-make
+++ b/tmpfs-config-make
@@ -131,8 +131,8 @@ cd $build_dir
 # ####################################
 if [ ! -f Makefile ]; then
     $srcdir/configure --srcdir=$srcdir --disable-shared CFLAGS="-g -O0"      \
-                      --enable-arithmetic --enable-cosmiccal --enable-mkprof \
-                      --enable-table
+                      --enable-arithmetic --enable-cosmiccal --enable-header \
+                      --enable-mkprof --enable-table
 fi
 
 



reply via email to

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