gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 30c7c07 074/125: CosmicCalculator working with


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 30c7c07 074/125: CosmicCalculator working with changes in this branch
Date: Sun, 23 Apr 2017 22:36:40 -0400 (EDT)

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

    CosmicCalculator working with changes in this branch
    
    CosmicCalculator was one of the simplest applications, so its files (in
    particular `args.h' and `ui.c' were updated to use the new option
    management system. Also, a sanity check was added to check if the
    fractional densities sum up to 1.0 or not. It now builds properly and its
    simple test script in `make check' passes successfully.
---
 bin/cosmiccal/args.h                     | 274 ++++++------------------
 bin/cosmiccal/{cite.h => authors-cite.h} |  30 +--
 bin/cosmiccal/cosmiccal.c                |   2 +-
 bin/cosmiccal/main.c                     |   4 +-
 bin/cosmiccal/main.h                     |  33 +--
 bin/cosmiccal/ui.c                       | 356 ++++++++++++-------------------
 bin/cosmiccal/ui.h                       |   3 +-
 tmpfs-config-make                        |   5 +-
 8 files changed, 241 insertions(+), 466 deletions(-)

diff --git a/bin/cosmiccal/args.h b/bin/cosmiccal/args.h
index 51dc40b..0a3e69a 100644
--- a/bin/cosmiccal/args.h
+++ b/bin/cosmiccal/args.h
@@ -23,140 +23,112 @@ 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[] = "";
-
-
-
-
-
-const char doc[] =
-  /* Before the list of options: */
-  GAL_STRINGS_TOP_HELP_INFO
-  SPACK_NAME" will produce cosmological calculations.\n"
-  GAL_STRINGS_MORE_HELP_INFO
-  /* After the list of options: */
-  "\v"
-  PACKAGE_NAME" home page: "PACKAGE_URL;
-
-
-
-
-
-/* Available letters for short options:
-
-   b c d e f g i j k m n p s t u w x y
-   A B C E F G I J L M O Q R T U W X Y Z
-
-   Number keys used: <=500
-
-   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
-    },
-    {
       "redshift",
-      'z',
+      ARGS_OPTION_KEY_REDSHIFT,
       "FLT",
       0,
       "Redshift of interest.",
-      1
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->redshift,
+      GAL_DATA_TYPE_DOUBLE,
+      GAL_OPTIONS_RANGE_GE_0,
+      GAL_OPTIONS_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "H0",
-      'H',
+      ARGS_OPTION_KEY_H0,
       "FLT",
       0,
       "Current expansion rate (Hubble constant).",
-      1
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->H0,
+      GAL_DATA_TYPE_DOUBLE,
+      GAL_OPTIONS_RANGE_GE_0,
+      GAL_OPTIONS_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "olambda",
-      'l',
+      ARGS_OPTION_KEY_OLAMBDA,
       "FLT",
       0,
       "Current cosmological cst. dens. per crit. dens.",
-      1
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->olambda,
+      GAL_DATA_TYPE_DOUBLE,
+      GAL_OPTIONS_RANGE_GE_0_LE_1,
+      GAL_OPTIONS_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "omatter",
-      'm',
+      ARGS_OPTION_KEY_OMATTER,
       "FLT",
       0,
       "Current matter density per critical density.",
-      1
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->omatter,
+      GAL_DATA_TYPE_DOUBLE,
+      GAL_OPTIONS_RANGE_GE_0_LE_1,
+      GAL_OPTIONS_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "oradiation",
-      'r',
+      ARGS_OPTION_KEY_ORADIATION,
       "FLT",
       0,
       "Current radiation density per critical density.",
-      1
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->oradiation,
+      GAL_DATA_TYPE_DOUBLE,
+      GAL_OPTIONS_RANGE_GE_0_LE_1,
+      GAL_OPTIONS_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
 
 
 
+
     {
-      0, 0, 0, 0,
-      "Output:",
-      2
-    },
-    {
-      "onlyvolume",
-      'v',
+      "onlyvolumne",
+      ARGS_OPTION_KEY_ONLYVOLUME,
       0,
       0,
-      "Only print comoving volume in Mpc^3",
-      2
+      "Only print comoving volume in Mpc^3.",
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->onlyvolume,
+      GAL_OPTIONS_NO_ARG_TYPE,
+      GAL_OPTIONS_RANGE_0_OR_1,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
     },
     {
       "onlyabsmagconv",
-      'a',
+      ARGS_OPTION_KEY_ONLYABSMAGCONV,
       0,
       0,
       "Only print conversion to absolute magnitude.",
-      2
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->onlyabsmagconv,
+      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
-    },
+
 
 
     {0}
@@ -166,127 +138,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 cosmiccalparams *p = state->input;
-
-  /* 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: */
-    case 'z':
-      gal_checkset_double_el_0(arg, &p->redshift, "redshift", key,
-                               SPACK, NULL, 0);
-      p->up.redshiftset=1;
-      break;
-    case 'H':
-      gal_checkset_double_el_0(arg, &p->H0, "H0", key, SPACK, NULL, 0);
-      p->up.H0set=1;
-      break;
-    case 'l':
-      gal_checkset_double_el_0(arg, &p->olambda, "olambda", key,
-                               SPACK, NULL, 0);
-      p->up.olambdaset=1;
-      break;
-    case 'm':
-      gal_checkset_double_el_0(arg, &p->omatter, "omatter", key,
-                               SPACK, NULL, 0);
-      p->up.omatterset=1;
-      break;
-    case 'r':
-      gal_checkset_double_el_0(arg, &p->oradiation, "oradiation",
-                               key, SPACK, NULL, 0);
-      p->up.oradiationset=1;
-      break;
-
-
-    /* Output: */
-    case 'v':
-      p->onlyvolume=1;
-      p->up.onlyvolumeset=1;
-      break;
-    case 'a':
-      p->onlyabsmagconv=1;
-      p->up.onlyabsmagconvset=1;
-      break;
-
-
-    /* Operating modes: */
-
-
-    /* Read the non-option arguments: */
-    case ARGP_KEY_ARG:
-      argp_error(state, SPACK_NAME" only takes options as "
-                 "input, currently no arguments are supported");
-      break;
-
-
-
-
-
-    /* The command line options and arguments are finished. */
-    case ARGP_KEY_END:
-      /* Currently there are no arguments, or input files, so there is
-         no need for such checks.
-
-      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;
+/* Define the child argp structure. */
+struct argp
+gal_options_common_child = {gal_commonopts_options,
+                            gal_options_common_argp_parse,
+                            NULL, NULL, NULL, 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}
+};
 
-
-
-
-    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};
-
+/* Set all the necessary argp parameters. */
+struct argp
+thisargp = {program_options, parse_opt, args_doc, doc, children, NULL, NULL};
 #endif
diff --git a/bin/cosmiccal/cite.h b/bin/cosmiccal/authors-cite.h
similarity index 57%
rename from bin/cosmiccal/cite.h
rename to bin/cosmiccal/authors-cite.h
index bab546e..a5ac70e 100644
--- a/bin/cosmiccal/cite.h
+++ b/bin/cosmiccal/authors-cite.h
@@ -5,7 +5,7 @@ CosmicCalculator is part of GNU Astronomy Utilities (Gnuastro) 
package.
 Original author:
      Mohammad Akhlaghi <address@hidden>
 Contributing author(s):
-Copyright (C) 2016, 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 COSMICCALBIBTEX ""
-
-#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, COSMICCALBIBTEX);                   \
-    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/cosmiccal/cosmiccal.c b/bin/cosmiccal/cosmiccal.c
index 23b9a7a..d040154 100644
--- a/bin/cosmiccal/cosmiccal.c
+++ b/bin/cosmiccal/cosmiccal.c
@@ -270,7 +270,7 @@ cosmiccal(struct cosmiccalparams *p)
   absmagconv=distmod-2.5*log10(1+p->redshift);
 
   /* Print out results: */
-  printf("%s\n", SPACK_STRING);
+  printf("%s\n", PROGRAM_STRING);
   printf("\n Input parameters\n");
   printf(  " ----------------\n");
   printf(FLTFORMAT, "Desired redshift for calculations (z):", p->redshift);
diff --git a/bin/cosmiccal/main.c b/bin/cosmiccal/main.c
index eb9f144..cd99936 100644
--- a/bin/cosmiccal/main.c
+++ b/bin/cosmiccal/main.c
@@ -35,13 +35,13 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 int
 main (int argc, char *argv[])
 {
-  struct cosmiccalparams p={{0}, {0}, 0};
+  struct cosmiccalparams 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 */
   cosmiccal(&p);
diff --git a/bin/cosmiccal/main.h b/bin/cosmiccal/main.h
index 61c81f4..6755958 100644
--- a/bin/cosmiccal/main.h
+++ b/bin/cosmiccal/main.h
@@ -23,40 +23,27 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #ifndef MAIN_H
 #define MAIN_H
 
-#include <commonparams.h>
+/* Include necessary headers */
+#include <gnuastro/data.h>
 
-/* Progarm name macros: */
-#define SPACK           "astcosmiccal" /* Subpackage executable name. */
-#define SPACK_NAME      "CosmicCalculator"  /* Subpackage full name.  */
-#define SPACK_STRING    SPACK_NAME" ("PACKAGE_NAME") "PACKAGE_VERSION
+#include <options.h>
 
+/* Progarm names.  */
+#define PROGRAM_NAME "CosmicCalculator" /* Program full name.       */
+#define PROGRAM_EXEC "astcosmiccal"     /* Program executable name. */
+#define PROGRAM_STRING PROGRAM_NAME" (" PACKAGE_NAME ") " PACKAGE_VERSION
 
 
 
 
 
-struct uiparams
-{
-  int             redshiftset;
-  int            curvatureset;
-  int                   H0set;
-  int              olambdaset;
-  int              omatterset;
-  int           oradiationset;
-
-  int           onlyvolumeset;
-  int       onlyabsmagconvset;
-};
-
-
-
 
 
+/* Main program parameters structure */
 struct cosmiccalparams
 {
   /* Other structures: */
-  struct uiparams          up;  /* User interface parameters.           */
-  struct gal_commonparams  cp;  /* Common parameters.                   */
+  struct gal_options_common_params cp;  /* Common parameters.           */
 
   /* Input: */
   double             redshift;  /* Redshift of interest.                */
@@ -70,8 +57,6 @@ struct cosmiccalparams
   int              onlyvolume;  /* Only print the volume in Mpc^3.      */
   int          onlyabsmagconv;  /* Only print conversion to abs. mag.   */
 
-  /* Operating mode: */
-
   /* Internal: */
   double                    K;  /* Curvature constant.                  */
   double                    c;  /* Speed of light.                      */
diff --git a/bin/cosmiccal/ui.c b/bin/cosmiccal/ui.c
index 81be577..eb49d2b 100644
--- a/bin/cosmiccal/ui.c
+++ b/bin/cosmiccal/ui.c
@@ -22,221 +22,87 @@ 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 <string.h>
-#include <fitsio.h>
+#include <stdio.h>
 
-#include <nproc.h>               /* From Gnulib.                   */
 #include <gsl/gsl_const_mksa.h>
 
 #include <gnuastro/fits.h>
-#include <gnuastro/txtarray.h>
+#include <gnuastro/table.h>
+#include <gnuastro/linkedlist.h>
 
-#include <timing.h>     /* Includes time.h and sys/time.h */
+#include <timing.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. */
-
-
-/* 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
-
-
-
-
-
+#include "ui.h"
+#include "authors-cite.h"
 
 
 
 
 
 /**************************************************************/
-/**************       Options and parameters    ***************/
+/*********      Argp necessary global entities     ************/
 /**************************************************************/
-void
-readconfig(char *filename, struct cosmiccalparams *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);
+/* Definition parameters for the Argp: */
+const char *
+argp_program_version = PROGRAM_STRING "\n"
+                       GAL_STRINGS_COPYRIGHT
+                       "\n\nWritten/developed by "PROGRAM_AUTHORS;
 
-  /* 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;
-
-
-
-
-      /* Inputs: */
-      if(strcmp(name, "redshift")==0)
-        {
-          if(up->redshiftset) continue;
-          gal_checkset_double_el_0(value, &p->redshift, name, key,
-                                      SPACK, filename, lineno);
-          up->redshiftset=1;
-        }
-      else if(strcmp(name, "H0")==0)
-        {
-          if(up->H0set) continue;
-          gal_checkset_double_el_0(value, &p->H0, name, key, SPACK,
-                                      filename, lineno);
-          up->H0set=1;
-        }
-      else if(strcmp(name, "olambda")==0)
-        {
-          if(up->olambdaset) continue;
-          gal_checkset_double_el_0(value, &p->olambda, name, key,
-                                      SPACK, filename, lineno);
-          up->olambdaset=1;
-        }
-      else if(strcmp(name, "omatter")==0)
-        {
-          if(up->omatterset) continue;
-          gal_checkset_double_el_0(value, &p->omatter, name, key,
-                                      SPACK, filename, lineno);
-          up->omatterset=1;
-        }
-      else if(strcmp(name, "oradiation")==0)
-        {
-          if(up->oradiationset) continue;
-          gal_checkset_double_el_0(value, &p->oradiation, name, key,
-                                   SPACK, filename, lineno);
-          up->oradiationset=1;
-        }
-
-
-
-      /* Outputs */
-      else if(strcmp(name, "onlyvolume")==0)
-        {
-          if(up->onlyvolumeset) continue;
-          gal_checkset_int_zero_or_one(value, &p->onlyvolume, name, key,
-                                       SPACK, filename, lineno);
-          up->onlyvolumeset=1;
-        }
-      else if(strcmp(name, "onlyabsmagconv")==0)
-        {
-          if(up->onlyabsmagconvset) continue;
-          gal_checkset_int_zero_or_one(value, &p->onlyabsmagconv, name,
-                                       key, SPACK, filename, lineno);
-          up->onlyabsmagconvset=1;
-        }
-
-
-
-      /* 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);
-    }
+const char *
+argp_program_bug_address = PACKAGE_BUGREPORT;
 
-  free(line);
-  fclose(fp);
-}
+static char
+args_doc[] = "";
 
+const char
+doc[] = GAL_STRINGS_TOP_HELP_INFO PROGRAM_NAME" will do cosmological "
+  "calculations.\n"
+  GAL_STRINGS_MORE_HELP_INFO
+  /* After the list of options: */
+  "\v"
+  PACKAGE_NAME" home page: "PACKAGE_URL;
 
 
 
 
-void
-printvalues(FILE *fp, struct cosmiccalparams *p)
+
+/* 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
 {
-  struct uiparams *up=&p->up;
-  struct gal_commonparams *cp=&p->cp;
+  /* With short-option version. */
+  ARGS_OPTION_KEY_REDSHIFT       = 'z',
+  ARGS_OPTION_KEY_H0             = 'H',
+  ARGS_OPTION_KEY_OLAMBDA        = 'l',
+  ARGS_OPTION_KEY_OMATTER        = 'm',
+  ARGS_OPTION_KEY_ORADIATION     = 'r',
+  ARGS_OPTION_KEY_ONLYVOLUME     = 'v',
+  ARGS_OPTION_KEY_ONLYABSMAGCONV = 'a',
 
 
-  /* Print all the options that are set. Separate each group with a
-     commented line explaining the options in that group. */
-  fprintf(fp, "\n# Input:\n");
-  if(up->redshiftset)
-    fprintf(fp, CONF_SHOWFMT"%.3f\n", "redshift", p->redshift);
-  if(up->H0set)
-    fprintf(fp, CONF_SHOWFMT"%.3f\n", "H0", p->H0);
+  /* Only with long version (start with a value 1000, the rest will be set
+     automatically). */
+};
 
 
-  fprintf(fp, "\n# Current densities per current critical density:\n");
-  if(up->olambdaset)
-    fprintf(fp, CONF_SHOWFMT"%.5f\n", "olambda", p->olambda);
-  if(up->omatterset)
-    fprintf(fp, CONF_SHOWFMT"%.5f\n", "omatter", p->omatter);
-  if(up->oradiationset)
-    fprintf(fp, CONF_SHOWFMT"%.5f\n", "oradiation", p->oradiation);
 
 
-  /* 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;
-}
 
 
 
 
 
 
-/* Note that numthreads will be used automatically based on the
-   configure time. */
-void
-checkifset(struct cosmiccalparams *p)
-{
-  struct uiparams *up=&p->up;
-  /*struct gal_commonparams *cp=&p->cp;*/
-
-  int intro=0;
-  if(up->redshiftset==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("redshift");
-  if(up->H0set==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("H0");
-  if(up->olambdaset==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("olambda");
-  if(up->omatterset==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("omatter");
-  if(up->oradiationset==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("oradiation");
-
-
-  GAL_CONFIGFILES_END_OF_NOTSET_REPORT;
-}
 
 
 
@@ -246,32 +112,72 @@ checkifset(struct cosmiccalparams *p)
 
 
 
+/**************************************************************/
+/*********    Initialize & Parse command-line    **************/
+/**************************************************************/
+static void
+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;
+  cp->program_exec       = PROGRAM_EXEC;
+  cp->program_bibtex     = PROGRAM_BIBTEX;
+  cp->program_authors    = PROGRAM_AUTHORS;
+  cp->coptions           = gal_commonopts_options;
 
+  /* Speed of light: */
+  p->c=GSL_CONST_MKSA_SPEED_OF_LIGHT;
+}
 
 
 
 
 
+/* Parse a single option: */
+error_t
+parse_opt(int key, char *arg, struct argp_state *state)
+{
+  struct cosmiccalparams *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)
+    {
 
+    /* Read the non-option tokens (arguments): */
+    case ARGP_KEY_ARG:
+      argp_error(state, "currently %s doesn't take any arguments",
+                 PROGRAM_NAME);
+      break;
 
 
-/**************************************************************/
-/************            Sanity check             *************/
-/**************************************************************/
-void
-sanitycheck(struct cosmiccalparams *p)
-{
-  int check=p->onlyvolume+p->onlyabsmagconv;
-
-  /* If only one of the single output options are called, then check
-     should be 1, if none are called, then it should be zero. However,
-     if more than one is called, check will be larger than one. So in
-     this case, report an error. */
-  if(check>1)
-    error(EXIT_FAILURE, 0, "only a single option starting with `--only' "
-          "can be called");
+    /* This is an option, set its value. */
+    default:
+      return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
+    }
+
+  return 0;
 }
 
 
@@ -294,13 +200,20 @@ sanitycheck(struct cosmiccalparams *p)
 
 
 /**************************************************************/
-/************             Preparations            *************/
+/***************       Sanity Check         *******************/
 /**************************************************************/
-void
-preparations(struct cosmiccalparams *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 cosmiccalparams *p)
 {
-  /* Speed of light: */
-  p->c=GSL_CONST_MKSA_SPEED_OF_LIGHT;
+  /* Check if the density fractions add up to 1. */
+  if( (p->olambda + p->omatter + p->oradiation) != 1.0f )
+    error(EXIT_FAILURE, 0, "sum of fractional densities is not 1, but %f. "
+          "The cosmological constant (`olambda'), matter (`omatter') "
+          "and radiation (`oradiation') densities are given as %f, %f, %f",
+          p->olambda + p->omatter + p->oradiation, p->olambda, p->omatter,
+          p->oradiation);
 
   /* The curvature fractional density: */
   p->ocurv=1-(p->olambda+p->omatter+p->oradiation);
@@ -331,36 +244,45 @@ preparations(struct cosmiccalparams *p)
 /**************************************************************/
 /************         Set the parameters          *************/
 /**************************************************************/
+
 void
-setparams(int argc, char *argv[], struct cosmiccalparams *p)
+ui_read_check_inputs_setup(int argc, char *argv[], struct cosmiccalparams *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;
 
-  /* Read the arguments. */
+  /* 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"
+
+
+  /* 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);
+
 
-  /* Do a sanity check */
-  sanitycheck(p);
+  /* Read the options into the program's structure, and check them and
+     their relations prior to printing. */
+  ui_read_check_only_options(p);
 
-  /* Make the preparations */
-  preparations(p);
 
-  /* Print the values for each parameter. */
-  if(cp->printparams)
-    GAL_CONFIGFILES_REPORT_PARAMETERS_SET;
+  /* 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);
 }
diff --git a/bin/cosmiccal/ui.h b/bin/cosmiccal/ui.h
index 7113aa7..3667e11 100644
--- a/bin/cosmiccal/ui.h
+++ b/bin/cosmiccal/ui.h
@@ -24,6 +24,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #define UI_H
 
 void
-setparams(int argc, char *argv[], struct cosmiccalparams *p);
+ui_read_check_inputs_setup(int argc, char *argv[],
+                           struct cosmiccalparams *p);
 
 #endif
diff --git a/tmpfs-config-make b/tmpfs-config-make
index a6a2f59..b597e51 100755
--- a/tmpfs-config-make
+++ b/tmpfs-config-make
@@ -130,8 +130,9 @@ cd $build_dir
 #
 # ####################################
 if [ ! -f Makefile ]; then
-    $srcdir/configure --srcdir=$srcdir --disable-shared CFLAGS="-g -O0"  \
-                      --enable-arithmetic --enable-mkprof --enable-table
+    $srcdir/configure --srcdir=$srcdir --disable-shared CFLAGS="-g -O0"      \
+                      --enable-arithmetic --enable-cosmiccal --enable-mkprof \
+                      --enable-table
 fi
 
 



reply via email to

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