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

  1 rick  1.1 /*
  2            *  smpl_06.c						$DRMS/proj/cookbook/
  3            *
  4 rick  1.2  *  Updates a record in the data series created in smpl_05 with new data
  5            *
  6            *  Illustrates features of the DRMS_Keyword struct, and "updating" of
  7            *    records in DRMS via cloning and recnum; also illustrates processing
  8            *    of multiple records via the DRMS_RecordSet struct
  9 rick  1.1  *
 10            *  Usage:
 11            *    smpl_06 [ds= cols= rows= ]
 12            *
 13            *  Revision history is at end of file.
 14            */
 15           
 16           
 17           char *module_name = "CookbookRecipe:06";
 18           char *version_id = "1.0";
 19           
 20           #include <jsoc_main.h>
 21           
 22           ModuleArgs_t module_args[] = {
 23             {ARG_STRING,	"ds",   "drms.noaa_ar", "name of data series"},
 24             {ARG_TIME,    "date", "Not Specified", "report date"},
 25             {ARG_INT,     "ar", "Not Specified", "active region number"},
 26             {ARG_STRING,	"key",  "", "key to be modified (required)"},
 27             {ARG_STRING,	"value",  "", "key value to be set (required)"},
 28             {ARG_FLAG,	"f",	"", "force update of multiple records"}, 
 29             {ARG_END}
 30 rick  1.1 };
 31           
 32           int setkey_general (DRMS_Record_t *rec, const char *key, char *val) {
 33             DRMS_Keyword_t *keywd = drms_keyword_lookup (rec, key, 0);
 34             double dv;
 35             long long lv;
 36             int iv;
 37           
 38             if (!keywd) {
 39               fprintf (stderr, "Error: key %s not found\n", key);
 40               return 1;
 41             }
 42           
 43             switch (drms_keyword_gettype (keywd)) {
 44               case DRMS_TYPE_STRING:
 45                 return drms_setkey_string (rec, key, val);
 46               case DRMS_TYPE_TIME:
 47                 return drms_setkey_time (rec, key, sscan_time (val));
 48               case DRMS_TYPE_DOUBLE:
 49                 dv = atof (val);
 50                 return drms_setkey_double (rec, key, dv);
 51 rick  1.1     case DRMS_TYPE_FLOAT:
 52                 dv = atof (val);
 53                 return drms_setkey_float (rec, key, (float)dv);
 54               case DRMS_TYPE_INT:
 55                 iv = atoi (val);
 56                 return drms_setkey_int (rec, key, iv);
 57               case DRMS_TYPE_SHORT:
 58                 iv = atoi (val);
 59                 return drms_setkey_int (rec, key, (short)iv);
 60               case DRMS_TYPE_CHAR:
 61                 iv = atoi (val);
 62                 return drms_setkey_int (rec, key, (char)iv);
 63               case DRMS_TYPE_LONGLONG:
 64                 lv = atoll (val);
 65                 return drms_setkey_longlong (rec, key, lv);
 66               default:
 67                 fprintf (stderr, "Error: unknown key type for key %s\n", key);
 68                 return 1;
 69             }
 70           
 71             return 1;
 72 rick  1.1 }
 73           
 74           int DoIt (void) {
 75             CmdParams_t *params = &cmdparams;
 76             DRMS_RecordSet_t *ds;
 77             DRMS_Record_t *record;
 78             int rec, recct, status = 0;
 79           
 80 rick  1.3   char *dspec = strdup (params_get_str (params, "ds"));
 81 rick  1.1   TIME date = params_get_time (params, "date");
 82             int arnum = params_get_int (params, "ar");
 83 rick  1.3   char *keynam = strdup (params_get_str (params, "key"));
 84             char *keyval = strdup (params_get_str (params, "value"));
 85 rick  1.1   int force = params_isflagset (params, "f");
 86           
 87             if (!(ds = drms_open_records (drms_env, dspec, &status))) {
 88               fprintf (stderr, "Error: unable to open input data set %s\n", dspec);
 89               return 1;
 90             }
 91           
 92             if (!drms_ismissing_time (date) && !drms_ismissing_int (arnum)) {
 93           	    /*  if valid values supplied for both date and ar, form a query  */
 94               char newspec[DRMS_MAXSERIESNAMELEN], timestr[16];
 95               char *brack = strchr (dspec, '[');	       /* strip existing query spec  */
 96               if (brack) *brack = '\0';
 97               drms_close_records (ds, DRMS_FREE_RECORD);
 98               date += 43200.0;				    /* round to nearest day  */
 99               sprint_time (timestr, date, "Z", -3);
100               sprintf (newspec, "%s[%s][%d]", dspec, timestr, arnum);
101               if (!(ds = drms_open_records (drms_env, newspec, &status))) {
102                 fprintf (stderr, "Error: unable to open input data set %s\n", newspec);
103                 return 1;
104               }
105             }
106 rick  1.1   if ((recct = ds->n) > 1) {
107               if (force) printf ("Updating %d records:\n", recct);
108               else {
109                 fprintf (stderr, "Warning: %d records matched query.\n", recct);
110                 fprintf (stderr,
111           	  "         To update all of these records, use the -f flag\n");
112                 fprintf (stderr,
113           	  "         You may limit the query with dataset specification notation\n");
114                 fprintf (stderr,
115           	  "           or by specifying *both* the ar and date parameters\n");
116                 return 1;
117               }
118             }
119             for (rec = 0; rec < recct; rec++) {
120               record = drms_clone_record (ds->records[rec], DRMS_PERMANENT,
121           	DRMS_SHARE_SEGMENTS, &status);
122               if (status = setkey_general (record, keynam, keyval)) {
123                 fprintf (stderr, "Error: unable to set %s = %s for record %d\n", keynam,
124           	  keyval, rec);
125                 drms_close_record (record, DRMS_FREE_RECORD);
126               } else drms_close_record (record, DRMS_INSERT_RECORD);
127 rick  1.1   }
128             return 0;
129           }
130           /*
131            *  Revision History
132            *
133            *  09.08.03	file created by R Bogart
134            */

Karen Tian
Powered by
ViewCVS 0.9.4