00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 char *module_name = "CookbookRecipe:06";
00018 char *version_id = "1.0";
00019
00020 #include <jsoc_main.h>
00021
00022 ModuleArgs_t module_args[] = {
00023 {ARG_STRING, "ds", "drms.noaa_ar", "name of data series"},
00024 {ARG_TIME, "date", "Not Specified", "report date"},
00025 {ARG_INT, "ar", "Not Specified", "active region number"},
00026 {ARG_STRING, "key", "", "key to be modified (required)"},
00027 {ARG_STRING, "value", "", "key value to be set (required)"},
00028 {ARG_FLAG, "f", "", "force update of multiple records"},
00029 {ARG_END}
00030 };
00031
00032 int setkey_general (DRMS_Record_t *rec, const char *key, char *val) {
00033 DRMS_Keyword_t *keywd = drms_keyword_lookup (rec, key, 0);
00034 double dv;
00035 long long lv;
00036 int iv;
00037
00038 if (!keywd) {
00039 fprintf (stderr, "Error: key %s not found\n", key);
00040 return 1;
00041 }
00042
00043 switch (drms_keyword_gettype (keywd)) {
00044 case DRMS_TYPE_STRING:
00045 return drms_setkey_string (rec, key, val);
00046 case DRMS_TYPE_TIME:
00047 return drms_setkey_time (rec, key, sscan_time (val));
00048 case DRMS_TYPE_DOUBLE:
00049 dv = atof (val);
00050 return drms_setkey_double (rec, key, dv);
00051 case DRMS_TYPE_FLOAT:
00052 dv = atof (val);
00053 return drms_setkey_float (rec, key, (float)dv);
00054 case DRMS_TYPE_INT:
00055 iv = atoi (val);
00056 return drms_setkey_int (rec, key, iv);
00057 case DRMS_TYPE_SHORT:
00058 iv = atoi (val);
00059 return drms_setkey_int (rec, key, (short)iv);
00060 case DRMS_TYPE_CHAR:
00061 iv = atoi (val);
00062 return drms_setkey_int (rec, key, (char)iv);
00063 case DRMS_TYPE_LONGLONG:
00064 lv = atoll (val);
00065 return drms_setkey_longlong (rec, key, lv);
00066 default:
00067 fprintf (stderr, "Error: unknown key type for key %s\n", key);
00068 return 1;
00069 }
00070
00071 return 1;
00072 }
00073
00074 int DoIt (void) {
00075 CmdParams_t *params = &cmdparams;
00076 DRMS_RecordSet_t *ds;
00077 DRMS_Record_t *record;
00078 int rec, recct, status = 0;
00079
00080 char *dspec = strdup (params_get_str (params, "ds"));
00081 TIME date = params_get_time (params, "date");
00082 int arnum = params_get_int (params, "ar");
00083 char *keynam = strdup (params_get_str (params, "key"));
00084 char *keyval = strdup (params_get_str (params, "value"));
00085 int force = params_isflagset (params, "f");
00086
00087 if (!(ds = drms_open_records (drms_env, dspec, &status))) {
00088 fprintf (stderr, "Error: unable to open input data set %s\n", dspec);
00089 return 1;
00090 }
00091
00092 if (!drms_ismissing_time (date) && !drms_ismissing_int (arnum)) {
00093
00094 char newspec[DRMS_MAXSERIESNAMELEN], timestr[16];
00095 char *brack = strchr (dspec, '[');
00096 if (brack) *brack = '\0';
00097 drms_close_records (ds, DRMS_FREE_RECORD);
00098 date += 43200.0;
00099 sprint_time (timestr, date, "Z", -3);
00100 sprintf (newspec, "%s[%s][%d]", dspec, timestr, arnum);
00101 if (!(ds = drms_open_records (drms_env, newspec, &status))) {
00102 fprintf (stderr, "Error: unable to open input data set %s\n", newspec);
00103 return 1;
00104 }
00105 }
00106 if ((recct = ds->n) > 1) {
00107 if (force) printf ("Updating %d records:\n", recct);
00108 else {
00109 fprintf (stderr, "Warning: %d records matched query.\n", recct);
00110 fprintf (stderr,
00111 " To update all of these records, use the -f flag\n");
00112 fprintf (stderr,
00113 " You may limit the query with dataset specification notation\n");
00114 fprintf (stderr,
00115 " or by specifying *both* the ar and date parameters\n");
00116 return 1;
00117 }
00118 }
00119 for (rec = 0; rec < recct; rec++) {
00120 record = drms_clone_record (ds->records[rec], DRMS_PERMANENT,
00121 DRMS_SHARE_SEGMENTS, &status);
00122 if (status = setkey_general (record, keynam, keyval)) {
00123 fprintf (stderr, "Error: unable to set %s = %s for record %d\n", keynam,
00124 keyval, rec);
00125 drms_close_record (record, DRMS_FREE_RECORD);
00126 } else drms_close_record (record, DRMS_INSERT_RECORD);
00127 }
00128 return 0;
00129 }
00130
00131
00132
00133
00134