00001
00002
00003
00004
00005
00006
00070 #include "jsoc_main.h"
00071 #include "drms.h"
00072 #include "atoinc.h"
00073
00074 #define NOT_SPECIFIED "NOT SPECIFIED"
00075
00076 ModuleArgs_t module_args[] =
00077 {
00078 {ARG_STRING, "ds", NOT_SPECIFIED, "Input data series."},
00079 {ARG_FLAG, "h", "0", "Help - Print usage and exit"},
00080 {ARG_END}
00081 };
00082
00083 #define DIE(msg) {fprintf(stderr,"%s\n",msg);exit(1);}
00084
00085 char *module_name = "index_convert";
00086
00087 int nice_intro ()
00088 {
00089 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00090 if (usage)
00091 {
00092 printf ("Usage:\nndex_convert [-h] "
00093 "ds=<seriesname> {<pkey>=<value>} | {<pkey>_index=<indexvalue>} \n"
00094 " -h: help - show this message then exit\n"
00095 "ds=<seriesname> - required\n"
00096 "<pkey> - prime key to use\n"
00097 "<pkey>_index - prime index key to use\n"
00098 "Only one of <pkey> or <pkey>_index is allowed\n"
00099 );
00100 return(1);
00101 }
00102 return (0);
00103 }
00104
00105
00106 int DoIt(void)
00107 {
00108 int status = 0;
00109 DRMS_Record_t *template;
00110 DRMS_Keyword_t *skey, *pkey;
00111 DRMS_Type_t ptype;
00112 int npkeys;
00113 char *pname=NULL;
00114 char *piname=NULL;
00115 char *seriesname;
00116 char *inbracket;
00117 const char *ds = cmdparams_get_str (&cmdparams, "ds", NULL);
00118 int ikey;
00119
00120 if (nice_intro()) return(0);
00121
00122
00123 if (strcmp(ds, NOT_SPECIFIED) == 0 )
00124 DIE("No dataseries: at least ds must be specified");
00125
00126
00127 inbracket = index(ds, '[');
00128 if (inbracket)
00129 *inbracket = '\0';
00130 template = drms_template_record (drms_env, ds, &status);
00131 if (!template || status)
00132 DIE("Series not found");
00133
00134 npkeys = template->seriesinfo->pidx_num;
00135 if (npkeys < 1)
00136 DIE("Series has no prime keys");
00137
00138
00139
00140
00141 for (ikey=0; ikey < npkeys; ikey++)
00142 {
00143 pkey = template->seriesinfo->pidx_keywords[ikey];
00144 if (pkey->info->recscope > 1)
00145 {
00146 if (cmdparams_exists(&cmdparams, pkey->info->name))
00147 {
00148
00149 skey = drms_keyword_slotfromindex(pkey);
00150 long long indexval = params_get_int64(&cmdparams, pkey->info->name);
00151 double epoch = drms_keyword_getslotepoch(skey, &status);
00152 double step = drms_keyword_getvalkeystep(skey, &status);
00153 skey->value.double_val = indexval * step + epoch;
00154 drms_keyword_printval(skey);
00155 printf("\n");
00156 return(0);
00157 }
00158 skey = drms_keyword_slotfromindex(pkey);
00159 if (cmdparams_exists(&cmdparams, skey->info->name))
00160 {
00161 DRMS_Value_t indexval;
00162 DRMS_Value_t inval;
00163 if (skey->info->type == DRMS_TYPE_TIME)
00164 inval.value.time_val = params_get_time(&cmdparams, skey->info->name);
00165 else
00166 inval.value.double_val = params_get_double(&cmdparams, skey->info->name);
00167 inval.type = skey->info->type;
00168 drms_keyword_slotval2indexval(skey, &inval, &indexval, NULL);
00169 pkey->value.longlong_val = indexval.value.longlong_val;
00170 drms_keyword_printval(pkey);
00171 printf("\n");
00172 return(0);
00173 }
00174 }
00175 }
00176 return(1);
00177 }