00001
00002
00003
00004
00005
00006
00007
00008 #include "drms.h"
00009 #include "serverdefs.h"
00010 #include "drms_types.h"
00011 #include "drms_record.h"
00012 #include "drms_names.h"
00013 #include "drms_env.h"
00014 #include "drms_network.h"
00015 #include "drms_keyword.h"
00016
00017 #define kDefaultSeries "sdo.fdsStateVectors"
00018 #define kMaxKeys 1024
00019
00020 #define PI (M_PI)
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 static void KeywordSnprintval(DRMS_Keyword_t *key, char *buf, size_t size)
00039 {
00040 switch(key->info->type)
00041 {
00042 case DRMS_TYPE_CHAR:
00043 snprintf(buf, size, key->info->format, key->value.char_val);
00044 break;
00045 case DRMS_TYPE_SHORT:
00046 snprintf(buf, size, key->info->format, key->value.short_val);
00047 break;
00048 case DRMS_TYPE_INT:
00049 snprintf(buf, size, key->info->format, key->value.int_val);
00050 break;
00051 case DRMS_TYPE_LONGLONG:
00052 snprintf(buf, size, key->info->format, key->value.longlong_val);
00053 break;
00054 case DRMS_TYPE_FLOAT:
00055 snprintf(buf, size, key->info->format, key->value.float_val);
00056 break;
00057 case DRMS_TYPE_DOUBLE:
00058 snprintf(buf, size, key->info->format, key->value.double_val);
00059 break;
00060 case DRMS_TYPE_TIME:
00061 {
00062 char timeBuf[1024];
00063 sprint_time(timeBuf, key->value.time_val, key->info->format, 0);
00064 snprintf(buf, size, "%s", timeBuf);
00065 }
00066 break;
00067 case DRMS_TYPE_STRING:
00068 snprintf(buf, size, key->info->format, key->value.string_val);
00069 break;
00070 default:
00071 break;
00072 }
00073 }
00074
00075 int ConvAndInterpFDS(DRMS_Env_t *drmsEnv, char *seriesName, char *dateRange)
00076 {
00077 char *series = NULL;
00078 char *dates = NULL;
00079 int error = 0;
00080
00081 if (drmsEnv == NULL)
00082 {
00083 error = 1;
00084 }
00085 else
00086 {
00087 if (seriesName != NULL)
00088 {
00089 series = strdup(seriesName);
00090 }
00091 else
00092 {
00093 series = strdup(kDefaultSeries);
00094 }
00095
00096 if (dateRange != NULL && series != NULL)
00097 {
00098
00099 int nPrime;
00100 DRMS_Keyword_t **primeKeys = NULL;
00101
00102
00103 DRMS_Record_t *template = drms_template_record(drmsEnv, series, &error);
00104
00105 if (template == NULL || error != 0)
00106 {
00107 printf("Series '%s' does not exist. drms_template_record returned "
00108 "status = %d\n", series, error);
00109 error = 1;
00110 }
00111 else
00112 {
00113 nPrime = template->seriesinfo->pidx_num;
00114 primeKeys = template->seriesinfo->pidx_keywords;
00115
00116 if (nPrime == 1)
00117 {
00118 size_t len = strlen(primeKeys[0]->info->name) + strlen(dateRange) + 3;
00119 dates = malloc(sizeof(char) * len + 1);
00120 if (dates != NULL)
00121 {
00122 snprintf(dates, len + 1, "[%s=%s]", primeKeys[0]->info->name, dateRange);
00123 }
00124 else
00125 {
00126 error = 1;
00127 }
00128 }
00129 else
00130 {
00131 printf("Unexpected number of prime keys.\n");
00132 error = 1;
00133 }
00134 }
00135 }
00136
00137 if (error == 0 && series != NULL)
00138 {
00139 char *recSet = NULL;
00140 size_t len = strlen(series);
00141
00142 if (dates != NULL)
00143 {
00144 len += strlen(dates);
00145 recSet = malloc(sizeof(char) * len + 1);
00146 if (recSet != NULL)
00147 {
00148 snprintf(recSet, len + 1, "%s%s", series, dates);
00149 }
00150 else
00151 {
00152 error = 1;
00153 }
00154 }
00155 else
00156 {
00157 recSet = strdup(series);
00158 }
00159
00160 if (error == 0 && recSet != NULL)
00161 {
00162 DRMS_RecordSet_t *recordSet = NULL;
00163 int nRecs = 0;
00164
00165 recordSet = drms_open_records(drmsEnv, recSet, &error);
00166
00167 if (error != 0 || recordSet == NULL)
00168 {
00169 printf("drms_open_records failed, recSet=%s, error=%d. Aborting.\n", recSet, error);
00170 }
00171 else
00172 {
00173 nRecs = recordSet->n;
00174
00175 if (nRecs == 0)
00176 {
00177 printf ("** No records in selected data set **\n");
00178 }
00179 else
00180 {
00181 int firstRec = 0;
00182 int lastRec = nRecs - 1;
00183 int iRec;
00184
00185
00186 for (iRec = firstRec; iRec <= lastRec; iRec++)
00187 {
00188
00189 DRMS_Record_t *rec = recordSet->records[iRec];
00190 DRMS_Keyword_t *keys[kMaxKeys];
00191 bzero(keys, sizeof(DRMS_Keyword_t *) * kMaxKeys);
00192
00193 int nKeys = 0;
00194
00195
00196 DRMS_Keyword_t *key = NULL;
00197 HIterator_t hit;
00198 hiter_new(&hit, &rec->keywords);
00199 while ((key = (DRMS_Keyword_t *)hiter_getnext(&hit)))
00200 {
00201 keys[nKeys++] = key;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211 if (iRec == firstRec)
00212 {
00213 int nKeysC = nKeys;
00214 while (nKeysC > 0)
00215 {
00216 DRMS_Keyword_t *key = keys[nKeys - nKeysC];
00217 char buf[1024];
00218 KeywordSnprintval(key, buf, sizeof(buf));
00219
00220 int keyValLen = strlen(buf);
00221 int nSp = keyValLen - strlen(key->info->name);
00222
00223 if (nSp < 0)
00224 {
00225 char *trunc = strdup(key->info->name);
00226 trunc[strlen(key->info->name) + nSp] = '\0';
00227 printf("%s", trunc);
00228 }
00229 else
00230 {
00231 printf("%s", key->info->name);
00232 nSp += 5;
00233
00234 while (nSp > 0)
00235 {
00236 printf(" ");
00237 nSp--;
00238 }
00239 }
00240
00241 nKeysC--;
00242 }
00243
00244 printf("\n");
00245 }
00246
00247 int iKey;
00248
00249 for (iKey = 0; iKey < nKeys; iKey++)
00250 {
00251 if (keys[iKey])
00252 {
00253 if (iKey > 0)
00254 {
00255 printf (" ");
00256 }
00257
00258 drms_keyword_printval(keys[iKey]);
00259 }
00260 else
00261 {
00262 printf("MISSING");
00263 }
00264 }
00265
00266 printf("\n");
00267 }
00268 }
00269 }
00270
00271 if (recordSet != NULL)
00272 {
00273 drms_close_records(recordSet, DRMS_FREE_RECORD);
00274 }
00275 }
00276
00277 if (recSet != NULL)
00278 {
00279 free(recSet);
00280 }
00281 }
00282 }
00283
00284 if (series != NULL)
00285 {
00286 free(series);
00287 }
00288
00289 if (dates != NULL)
00290 {
00291 free(dates);
00292 }
00293
00294 return error;
00295 }