(file) Return to smpl_02.c CVS log (file) (dir) Up to [Development] / JSOC / proj / cookbook

Diff for /JSOC/proj/cookbook/smpl_02.c between version 1.1 and 1.2

version 1.1, 2009/04/20 22:23:17 version 1.2, 2009/07/27 23:42:16
Line 1 
Line 1 
 /* /*
  *  smpl_02.c                                           $DRMS/proj/cookbook/  *  smpl_02.c                                           $DRMS/proj/cookbook/
  *  *
  *  Prints a list of the series names known to DRMS, with record counts   *  Annother simple module that does nothing but echo back the values of
  *    for each   *    arguments. It illustrates the use of command line parsing and the
  *  Illustrates the connection to the DRMS database and ways that SQL   *    variety of argument types; also introduces time string functions
  *    queries can be directly run and the results analyzed at the lowest  
  *    level of the DRMS API  
  *  *
  *  Usage:  *  Usage:
  *    smpl_02 [nmax= ...]   *    smpl_02 [-avVH]
    *
    *  Bugs:
    *    The module is of no particular use, and exists merely for heuristic
    *      and testing purposes.
  *  *
  *  Revision history is at end of file.  *  Revision history is at end of file.
  */  */
  
   #include <jsoc_main.h>
 char *module_name = "CookbookRecipe:02"; char *module_name = "CookbookRecipe:02";
 char *version_id = "1.0"; char *version_id = "1.0";
  
 #include <jsoc_main.h>  
 #include <regex.h>  
   
 ModuleArgs_t module_args[] = { ModuleArgs_t module_args[] = {
   {ARG_INT,     "nmax", "100", "maximum number of series to be listed"},    {ARG_STRING,  "name", "Not Specified", "a string"},
     {ARG_INT,     "ival", "1", "a positive integer", "[1,)"},
     {ARG_INTS,    "iptr", "[0]", "array of integers"},
     {ARG_FLOAT,   "fval", "0.0", "a real number"},
     {ARG_FLOAT,   "fvalns", "Unspecified", "a real number"},
     {ARG_FLOATS,  "fptr", "{2.71828, 3.14159, -1}", "array of real numbers"},
     {ARG_TIME,    "time", "1582.10.5_00:00:00",
         "a time, in standard date_time format"},
     {ARG_NUME, "colour", "", "enumerated choice without a default",
         "red, orange, yellow, green, blue, indigo, violet"},
     {ARG_NUME, "mois", "Brumaire", "enumerated choice with a default",
         "Vendémiaire, Brumaire, Frimaire, Nivôse, Pluviôse, Ventôse, Germinal, \
         Florial, Prairial, Messidor, Thermidor, Fructidor"},
     {ARG_FLAG,    "e",    "", "a flag value"},
   {ARG_END}   {ARG_END}
 }; };
  
 int DoIt (void) { int DoIt (void) {
   CmdParams_t *params = &cmdparams;   CmdParams_t *params = &cmdparams;
   DB_Text_Result_t *qres, *sqres;    double *fpval;
   int series, seriesct;    int *ipval;
   char query[DRMS_MAXQUERYLEN];    int i, fpvals, ipvals;
     char *colours[] = {"red", "orange", "yellow", "green", "blue", "violet"};
   int nmax = params_get_int (params, "nmax");    char *moiss[] = {"Vendémiaire", "Brumaire", "Frimaire", "Nivôse", "Pluviôse",
         /*  Query the database to get all series names from the master list  */        "Ventôse", "Germinal", "Florial", "Prairial", "Messidor", "Thermidor",
   sprintf (query, "select seriesname from %s()", DRMS_MASTER_SERIES_TABLE);        "Fructidor"};
   if ((qres = drms_query_txt (drms_env->session, query)) == NULL) {    char key[64], tbuf[64];
     fprintf (stderr, "Cant find DRMS\n");  
     return 1;    TIME tval = params_get_time (params, "time");
   }    double fval = params_get_double (params, "fval");
   seriesct = qres->num_rows;    int ival = params_get_int (params, "ival");
   printf ("%d series found", seriesct);    int colour = params_get_int (params, "colour");
   if (seriesct > nmax) {    int mois = params_get_int (params, "mois");
     seriesct = nmax;    char *name = params_get_str (params, "name");
     printf (" (only the first %d will be listed)", seriesct);    int flagset = params_isflagset (params, "e");
   
     printf ("name = %s\n", name);
     printf ("fval = %g\n", fval);
     printf ("ival = %d\n", ival);
     sprint_time (tbuf, tval, "UT", 3);
     printf ("time = %s\n", tbuf);
     printf ("colour = %d (%s)\n", colour, colours[colour]);
     printf ("mois = %d (%s)\n", mois, moiss[mois]);
     printf ("-e? : %d\n", flagset);
   
     printf ("fpvals = %d\n", fpvals = params_get_int (params, "fptr_nvals"));
     fpval = (double *)malloc (fpvals * sizeof (double));
     for (i = 0; i < fpvals; i++) {
       sprintf (key, "fptr_%d_value", i);
       fpval[i] = params_get_double (params, key);
       printf ("fpval[%d] = %g\n", i, fpval[i]);
   }   }
   printf ("\n");    printf ("ipvals = %d\n", ipvals = params_get_int (params, "iptr_nvals"));
     ipval = (int *)malloc (ipvals * sizeof (int));
   for (series = 0; series < seriesct; series++) {    for (i = 0; i < ipvals; i++) {
     char *seriesname = qres->field[series][0];      sprintf (key, "iptr_%d_value", i);
     printf ("%s\t", seriesname);      ipval[i] = params_get_int (params, key);
     sprintf (query, "select count (recnum) from %s", seriesname);      printf ("ipval[%d] = %d\n", i, ipval[i]);
        /*  Query the database to get the record count from the series table  */  
                           /*  (every data series must have a "recnum" field) */  
     if (sqres = drms_query_txt (drms_env->session, query)) {  
       printf ("%s", sqres->field[0][0]);  
       db_free_text_result (sqres);  
     } else printf ("?");  
     printf ("\n");  
   }   }
  
   db_free_text_result (qres);    return (0);
   return 0;  
 } }
  
 /* /*
  *  Revision History  *  Revision History
  *  *
  *  09.04.20    file created by R Bogart   *  07.02.27    created by RSB, based on original using SSSC API
    *  09.04.14    minor mods to illustrate additional features of arg parsing
  */  */


Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

Karen Tian
Powered by
ViewCVS 0.9.4