00001
00002
00003
00004
00005
00006
00007 #include "jsoc_main.h"
00008 #include "drms.h"
00009 #include "drms_names.h"
00010
00011 ModuleArgs_t module_args[] =
00012 {
00013 {ARG_STRING, "ds", "Not Specified", "<record_set query>"},
00014 {ARG_STRING, "key", "Not Specified", "<comma delimited keyword list>"},
00015 {ARG_FLAG, "a", "0", "Show info for all keywords"},
00016 {ARG_FLAG, "h", "0", "help - print usage info"},
00017 {ARG_FLAG, "l", "0", "just list series keywords with descriptions"},
00018 {ARG_INT, "n", "0", "number of records to show, +from first, -from last"},
00019 {ARG_FLAG, "r", "0", "recnum - show record number as first keyword"},
00020 {ARG_END}
00021 };
00022
00023 char *module_name = "plot_keys";
00024
00025 int nice_intro ()
00026 {
00027 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00028 if (usage)
00029 {
00030 printf ("Usage:\nplot_keys [-ahlr] "
00031 "ds=<recordset query> {n=0} {key=<keylist>} {seg=<segment_list>}\n"
00032 " details are:\n"
00033 " -a: show information for all keywords\n"
00034 " -h: help - show this message then exit\n"
00035 " -l: list all series keywords with description, then exit\n"
00036 " -r: recnum - show record number as first keyword\n"
00037 "ds=<recordset query> as <series>{[record specifier]} - required\n"
00038 "n=0 number of records in query to plot, +n from start or -n from end\n"
00039 "key=<comma delimited keyword list>, for all use -a flag\n");
00040 return(1);
00041 }
00042 return (0);
00043 }
00044
00045
00046 int DoIt(void)
00047 {
00048 int firstrec=1;
00049 int status = 0;
00050 DRMS_RecordSet_t *recordset;
00051 DRMS_Record_t *rec;
00052 int first_rec, last_rec, nrecs, irec;
00053 char *keys[1000];
00054 int ikey, nkeys = 0;
00055 char *inqry;
00056
00057 char *in = cmdparams_get_str (&cmdparams, "ds", NULL);
00058 char *keylist = strdup (cmdparams_get_str (&cmdparams, "key", NULL));
00059 int plot_keys = strcmp (keylist, "Not Specified");
00060 int list_keys = cmdparams_get_int (&cmdparams, "l", NULL) != 0;
00061 int show_all = cmdparams_get_int (&cmdparams, "a", NULL) != 0;
00062 int max_recs = cmdparams_get_int (&cmdparams, "n", NULL);
00063 int show_recnum = cmdparams_get_int(&cmdparams, "r", NULL) != 0;
00064 int keyword_list = cmdparams_get_int(&cmdparams, "k", NULL) != 0;
00065
00066 if (nice_intro ()) return (0);
00067
00068 if (strcmp(in, "Not Specified") == 0)
00069 {
00070 printf("### plot_keys: ds=<record_query> parameter is required, but I will look for a query without the ds=\n");
00071 if (cmdparams_numargs(&cmdparams) >= 1 && (in = cmdparams_getarg (&cmdparams, 1)))
00072 {
00073 printf("### found \"%s\", using it for the record_query.\n",in);
00074 }
00075 else
00076 {
00077 printf("### plot_keys: Oops, still no query found, quit\n");
00078 return(1);
00079 }
00080 }
00081 if (list_keys)
00082 {
00083 char *p, *seriesname;
00084 DRMS_Record_t *rec;
00085 DRMS_Keyword_t *key, **prime_keys;
00086 DRMS_Segment_t *seg;
00087 HIterator_t hit;
00088 int nprime, iprime;
00089
00090
00091 seriesname = strdup (in);
00092 if ((p = index(seriesname,'['))) *p = '\0';
00093 rec = drms_template_record (drms_env, seriesname, &status);
00094 if (status)
00095 {
00096 fprintf(stderr,"### plot_keys: series %s not found.\n",seriesname);
00097 return(1);
00098 }
00099 free (seriesname);
00100
00101
00102 nprime = rec->seriesinfo->pidx_num;
00103 prime_keys = rec->seriesinfo->pidx_keywords;
00104 if (nprime > 0)
00105 {
00106 printf("Prime Keys are:\n");
00107 for (iprime = 0; iprime < nprime; iprime++)
00108 printf("\t%s\n", prime_keys[iprime]->info->name);
00109 }
00110
00111
00112 printf("All Keywords for series %s:\n",rec->seriesinfo->seriesname);
00113 hiter_new (&hit, &rec->keywords);
00114 while ((key = (DRMS_Keyword_t *)hiter_getnext (&hit)))
00115 printf ("\t%-10s\t%s (%s)\n", key->info->name, key->info->description,
00116 drms_type_names[key->info->type]);
00117
00118
00119 if (rec->segments.num_total)
00120 {
00121 printf("Segments for series %s:\n",rec->seriesinfo->seriesname);
00122 hiter_new (&hit, &rec->segments);
00123 while ((seg = (DRMS_Segment_t *)hiter_getnext (&hit)))
00124 printf ("\t%-10s\t%s\n", seg->info->name, seg->info->description);
00125 }
00126 return (0);
00127 }
00128
00129
00130 inqry = index(in, '[');
00131 if (!inqry && !max_recs)
00132 {
00133 fprintf(stderr, "### plot_keys query must have n=recs or record query specified\n");
00134 return(1);
00135 }
00136
00137
00138 recordset = drms_open_records (drms_env, in, &status);
00139 if (status)
00140 {
00141 printf ("drms_open_records failed, in=%s, status=%d. Aborting.\n", in,
00142 status);
00143 return (1);
00144 }
00145
00146
00147
00148 nrecs = recordset->n;
00149 if (nrecs == 0)
00150 {
00151 printf ("** No records in selected data set, query was %s **\n",in);
00152 }
00153 if (max_recs > 0 && max_recs < nrecs)
00154 nrecs = max_recs;
00155 last_rec = nrecs - 1;
00156 if (max_recs < 0 && nrecs+max_recs > 0)
00157 first_rec = nrecs + max_recs;
00158 else
00159 first_rec = 0;
00160
00161
00162 nkeys = 0;
00163 if (show_all)
00164 {
00165 DRMS_Keyword_t *key;
00166 HIterator_t hit;
00167 hiter_new (&hit, &recordset->records[0]->keywords);
00168 while ((key = (DRMS_Keyword_t *)hiter_getnext (&hit)))
00169 keys[nkeys++] = strdup (key->info->name);
00170 }
00171 else if (plot_keys)
00172 {
00173 char *thiskey;
00174 for (thiskey=strtok(keylist, ","); thiskey; thiskey=strtok(NULL,","))
00175 keys[nkeys++] = strdup(thiskey);
00176 }
00177 free (keylist);
00178
00179
00180 for (irec = first_rec; irec <= last_rec; irec++)
00181 {
00182 rec = recordset->records[irec];
00183 if (firstrec)
00184 {
00185 firstrec=0;
00186 if (!keyword_list)
00187 {
00188 if (show_recnum)
00189 printf ("recnum\t");
00190 for (ikey=0 ; ikey<nkeys; ikey++)
00191 printf ("%s\t",keys[ikey]);
00192 printf ("\n");
00193 }
00194 }
00195
00196 if (keyword_list)
00197 {
00198
00199 FILE *myfile;
00200 myfile = fopen("/tmp/gnuplot_cmd.txt", "w"); if (myfile == NULL) {
00201 fprintf(stderr, "Can't open datafile.txt for reading");
00202 exit(1);
00203 }
00204
00205 void drms_fprint_gnuplot_header(FILE *plotfile, DRMS_Record_t *rec);
00206 void drms_fprint_query_rec(FILE *plotfile, DRMS_Record_t *rec);
00207 drms_fprint_gnuplot_header(myfile, rec);
00208 drms_fprint_query_rec(myfile, rec);
00209 fclose(myfile);
00210 }
00211
00212
00213
00214
00215 if (show_recnum)
00216 {
00217 if (keyword_list)
00218 printf("recnum=%lld\n",rec->recnum);
00219 else
00220 printf ("%6lld\t", rec->recnum);
00221 }
00222
00223
00224 for (ikey=0; ikey<nkeys; ikey++)
00225 {
00226 DRMS_Keyword_t *rec_key_ikey = drms_keyword_lookup (rec, keys[ikey], 1);
00227 if (ikey)
00228 printf (keyword_list ? "\n" : "\t");
00229 if (rec_key_ikey)
00230 {
00231 if (keyword_list)
00232 {
00233 printf("%s=", keys[ikey]);
00234 if (rec_key_ikey->info->type != DRMS_TYPE_STRING)
00235 drms_keyword_printval (rec_key_ikey);
00236 else
00237 {
00238 printf("\"");
00239 drms_keyword_printval (rec_key_ikey);
00240 printf("\"");
00241 }
00242 }
00243 else
00244 drms_keyword_printval (rec_key_ikey);
00245 }
00246 else if (!keyword_list)
00247 printf ("MISSING");
00248 }
00249 if(nkeys)
00250 printf (keyword_list ? "\n" : "\t");
00251 }
00252
00253 for (ikey=0; ikey<nkeys; ikey++)
00254 free(keys[ikey]);
00255 drms_close_records(recordset, DRMS_FREE_RECORD);
00256 return status;
00257 }
00258
00259
00260 void drms_fprint_gnuplot_header(FILE *plotfile, DRMS_Record_t *rec)
00261 {
00262 DRMS_Keyword_t *rec_key, *key, **prime_keys;
00263
00264 fprintf(plotfile,"# set terminal png transparent nocrop enhanced font arial 10 size 400,400\n");
00265 fprintf(plotfile,"# set output 'plot_keys.png'\n");
00266 fprintf(plotfile,"set boxwidth 0.75 absolute\n");
00267 fprintf(plotfile,"set style fill solid 1.00 border -1\n");
00268 fprintf(plotfile,"set key outside right top vertical Left reverse enhanced autotitles columnhead nobox\n");
00269 fprintf(plotfile,"set title `%s`\n",rec->seriesinfo->seriesname);
00270
00271 }
00272
00273
00274 void drms_fprint_query_rec(FILE *plotfile, DRMS_Record_t *rec)
00275 {
00276 int iprime, nprime;
00277 DRMS_Keyword_t *rec_key, *key, **prime_keys;
00278 fprintf(plotfile,"%s",rec->seriesinfo->seriesname);
00279 nprime = rec->seriesinfo->pidx_num;
00280 prime_keys = rec->seriesinfo->pidx_keywords;
00281 if (nprime > 0)
00282 {
00283 for (iprime = 0; iprime < nprime; iprime++)
00284 {
00285 key = prime_keys[iprime];
00286 rec_key = drms_keyword_lookup (rec, key->info->name, 1);
00287 fprintf(plotfile,"[");
00288 if (key->info->type != DRMS_TYPE_STRING)
00289 drms_keyword_fprintval (plotfile, rec_key);
00290 else
00291 {
00292 fprintf(plotfile,"\"");
00293 drms_keyword_fprintval (plotfile, rec_key);
00294 fprintf(plotfile,"\"");
00295 }
00296 fprintf(plotfile,"]");
00297 }
00298 }
00299 else
00300 fprintf(plotfile,"[:#%lld]",rec->recnum);
00301 }