1 |
/* |
2 |
* |
3 |
* index_convert - Converts a slot value to an index, or vise versa, for a given series. |
4 |
* |
5 |
*/ |
6 |
|
7 |
/** |
8 |
\defgroup index_convert - Convert index to key value or the reverse |
9 |
@ingroup drms_util |
10 |
|
11 |
\par Synopsis: |
12 |
index_convert [-h] |
13 |
index_convert ds=<seriesname> [<pkey>=<value>] | [<pkey>_index=<indexvalue>] |
14 |
index_convert {same options as above} |
15 |
\endcode |
16 |
|
17 |
\details |
18 |
|
19 |
\b Index_convert converts a prime key index value to a prime key value or the reverse. The series must be |
20 |
slotted. |
21 |
|
22 |
The operation is to get the template record then if the <pkey>_index aregument is present convert |
23 |
that value to the equivalent prime key value. If the <pkey> argument is present, i.e. no "_index" in |
24 |
the keyname, then the equivalent index value is computed and printed. |
25 |
|
26 |
\par Options: |
27 |
|
28 |
\par Flags: |
29 |
|
30 |
\li \c -h: help - print usage information and exit |
31 |
|
32 |
\par Parameters: |
33 |
|
34 |
\li \c ds=<seriesname> - required parameter specified the series to examine. |
35 |
\li \c <pkey>=<value> - optional prime key name and its test value. |
36 |
\li \c <pkey>_index=<indexvalue> - optional prime key slotted index name and its test index value. |
37 |
|
38 |
\par JSOC flags: |
39 |
\ref jsoc_main |
40 |
|
41 |
\par Usage: |
42 |
|
43 |
The program may be used to convert a single prime key value into its equivalent index value or the reverse. |
44 |
The keyname provided must be for a slotted prime key in the specified series. If the seriesname is |
45 |
not a valid series or if the keyname is not a prime key in that series an error code is returned. |
46 |
The direction of the conversion is determined by the presence or absence of the "_index" suffix on |
47 |
the single specified keyword argument. |
48 |
|
49 |
\par Examples: |
50 |
|
51 |
\b Example 1: |
52 |
\code |
53 |
index_convert ds=mdi.fd_V_lev18 T_REC=1996.05.01 |
54 |
\endcode |
55 |
reports: 1751040, the minute number for the given time using the proper epoch. |
56 |
|
57 |
\b Example 2: |
58 |
\code |
59 |
index_convert ds=mdi.fd_V_lev18 T_REC_index=1751040 |
60 |
\endcode |
61 |
reports: 1996.05.01_00:00:00_TAI, the time of the center of the specified slot. |
62 |
|
63 |
\bug |
64 |
|
65 |
\sa |
66 |
time_convert time_index(SOI) |
67 |
|
68 |
*/ |
69 |
|
70 |
#include "jsoc_main.h" |
71 |
#include "drms.h" |
72 |
#include "atoinc.h" |
73 |
|
74 |
#define NOT_SPECIFIED "NOT SPECIFIED" |
75 |
|
76 |
ModuleArgs_t module_args[] = |
77 |
{ |
78 |
{ARG_STRING, "ds", NOT_SPECIFIED, "Input data series."}, |
79 |
{ARG_FLAG, "h", "0", "Help - Print usage and exit"}, |
80 |
{ARG_END} |
81 |
}; |
82 |
|
83 |
#define DIE(msg) {fprintf(stderr,"%s\n",msg);exit(1);} |
84 |
|
85 |
char *module_name = "index_convert"; |
86 |
|
87 |
int nice_intro () |
88 |
{ |
89 |
int usage = cmdparams_get_int (&cmdparams, "h", NULL); |
90 |
if (usage) |
91 |
{ |
92 |
printf ("Usage:\nndex_convert [-h] " |
93 |
"ds=<seriesname> {<pkey>=<value>} | {<pkey>_index=<indexvalue>} \n" |
94 |
" -h: help - show this message then exit\n" |
95 |
"ds=<seriesname> - required\n" |
96 |
"<pkey> - prime key to use\n" |
97 |
"<pkey>_index - prime index key to use\n" |
98 |
"Only one of <pkey> or <pkey>_index is allowed\n" |
99 |
); |
100 |
return(1); |
101 |
} |
102 |
return (0); |
103 |
} |
104 |
|
105 |
/* Module main function. */ |
106 |
int DoIt(void) |
107 |
{ |
108 |
int status = 0; |
109 |
DRMS_Record_t *template; |
110 |
DRMS_Keyword_t *skey, *pkey; |
111 |
DRMS_Type_t ptype; |
112 |
int npkeys; |
113 |
char *pname=NULL; |
114 |
char *piname=NULL; |
115 |
char *seriesname; |
116 |
char *inbracket; |
117 |
const char *ds = cmdparams_get_str (&cmdparams, "ds", NULL); |
118 |
int ikey; |
119 |
|
120 |
if (nice_intro()) return(0); |
121 |
|
122 |
/* check for minimum inputs */ |
123 |
if (strcmp(ds, NOT_SPECIFIED) == 0 ) |
124 |
DIE("No dataseries: at least ds must be specified"); |
125 |
|
126 |
/* get series info and prime key type, etc. */ |
127 |
inbracket = index(ds, '['); |
128 |
if (inbracket) |
129 |
*inbracket = '\0'; |
130 |
template = drms_template_record (drms_env, ds, &status); |
131 |
if (!template || status) |
132 |
DIE("Series not found"); |
133 |
|
134 |
npkeys = template->seriesinfo->pidx_num; |
135 |
if (npkeys < 1) |
136 |
DIE("Series has no prime keys"); |
137 |
|
138 |
// for each prime key check command line for key or key_index args |
139 |
// ignore non-slotted prime keys |
140 |
// The first appropriate key is used. |
141 |
for (ikey=0; ikey < npkeys; ikey++) |
142 |
{ |
143 |
pkey = template->seriesinfo->pidx_keywords[ikey]; |
144 |
if (pkey->info->recscope > 1) |
145 |
{ |
146 |
if (cmdparams_exists(&cmdparams, pkey->info->name)) |
147 |
{ |
148 |
// pkey->value.longlong_val = params_get_int64(&cmdparams, pkey->info->name); |
149 |
skey = drms_keyword_slotfromindex(pkey); |
150 |
long long indexval = params_get_int64(&cmdparams, pkey->info->name); |
151 |
double epoch = drms_keyword_getslotepoch(skey, &status); |
152 |
double step = drms_keyword_getvalkeystep(skey, &status); |
153 |
skey->value.double_val = indexval * step + epoch; |
154 |
drms_keyword_printval(skey); |
155 |
printf("\n"); |
156 |
return(0); |
157 |
} |
158 |
skey = drms_keyword_slotfromindex(pkey); |
159 |
if (cmdparams_exists(&cmdparams, skey->info->name)) |
160 |
{ |
161 |
DRMS_Value_t indexval; |
162 |
DRMS_Value_t inval; |
163 |
if (skey->info->type == DRMS_TYPE_TIME) |
164 |
inval.value.time_val = params_get_time(&cmdparams, skey->info->name); |
165 |
else |
166 |
inval.value.double_val = params_get_double(&cmdparams, skey->info->name); |
167 |
inval.type = skey->info->type; |
168 |
drms_keyword_slotval2indexval(skey, &inval, &indexval, NULL); |
169 |
pkey->value.longlong_val = indexval.value.longlong_val; |
170 |
drms_keyword_printval(pkey); |
171 |
printf("\n"); |
172 |
return(0); |
173 |
} |
174 |
} |
175 |
} |
176 |
return(1); |
177 |
} |