00001 #include "jsoc_main.h"
00002 #include "drms.h"
00003 #include "dsdsmigr.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #define NOT_SPECIFIED "NOT_SPECIFIED"
00062
00063 #define DATA_OK ('\0')
00064 #define DATA_MISS ('\1')
00065 #define DATA_UNK ('\2')
00066
00067
00068 char primestr[100];
00069
00070 #define RSUNM (6.96e5)
00071 #define AUM (1.49597870e8)
00072 #define SECRAD (206264.8062)
00073
00074 int set_soho_ephemeris_keys (DRMS_Record_t *rec, TIME t)
00075 {
00076 TIME tbl_mod;
00077 double dist, lat, lon, vr, vn, vw, rsun;
00078 int cr, status;
00079
00080 status = soho_ephemeris (t, &dist, &lat, &lon, &vr, &vn, &vw, &tbl_mod);
00081 if (!status)
00082 {
00083 drms_setkey_double (rec, "CRLN_OBS", lon);
00084 drms_setkey_double (rec, "CRLT_OBS", lat);
00085 drms_setkey_double (rec, "OBS_DIST", dist);
00086 drms_setkey_double (rec, "OBS_VR", vr);
00087 drms_setkey_double (rec, "OBS_VN", vn);
00088 drms_setkey_double (rec, "OBS_VW", vw);
00089 }
00090 return status;
00091 }
00092
00093 char *primevalstr(TIME prime, DRMS_Type_t type, char *unit, char *format)
00094 {
00095 if (type==DRMS_TYPE_TIME)
00096 sprint_time(primestr, prime, unit, atoi(format));
00097 else
00098 sprintf(primestr, (type <= DRMS_TYPE_LONGLONG ? "%.0f" : "%f"), prime);
00099 return(primestr);
00100 }
00101
00102 void printprime(FILE *fp, TIME prime, DRMS_Type_t type, char *unit, char *format)
00103 {
00104 fprintf(fp, primevalstr(prime, type, unit, format));
00105 }
00106
00107 ModuleArgs_t module_args[] =
00108 {
00109 {ARG_STRING, "ds", NOT_SPECIFIED, "Input data series."},
00110 {ARG_STRING, "low", NOT_SPECIFIED, "Low limit for coverage map."},
00111 {ARG_STRING, "high", NOT_SPECIFIED, "High limit for coverage map."},
00112 {ARG_STRING, "key", NOT_SPECIFIED, "Prime key name to use, default is first prime"},
00113 {ARG_FLAG, "i", "0", "Index - Print index values instead of prime slot values"},
00114 {ARG_FLAG, "q", "0", "Quiet - omit series header info"},
00115 {ARG_END}
00116 };
00117
00118 #define DIE(msg) {fprintf(stderr,"%s\n",msg);exit(1);}
00119
00120
00121 char *module_name = "show_coverage";
00122
00123
00124 int DoIt(void)
00125 {
00126 FILE *out;
00127 int status = 0;
00128 int slotted;
00129 long long lowslot, highslot, serieslowslot, serieshighslot;
00130 DRMS_RecordSet_t *rs;
00131 DRMS_Record_t *rec, *template;
00132 DRMS_Keyword_t *skey, *pkey;
00133 DRMS_Type_t ptype;
00134 char name[DRMS_MAXNAMELEN];
00135 int npkeys;
00136 char *pname;
00137 char *piname;
00138 char *punit;
00139 char *pformat;
00140 char *seriesname;
00141 TIME step, epoch;
00142 TIME series_low, series_high, low, high;
00143 char in[DRMS_MAXQUERYLEN];
00144 char *inbracket;
00145 char otherkeys[20*DRMS_MAXQUERYLEN];
00146 const char *ds = cmdparams_get_str (&cmdparams, "ds", NULL);
00147 const char *lowstr = cmdparams_get_str (&cmdparams, "low", NULL);
00148 const char *highstr = cmdparams_get_str (&cmdparams, "high", NULL);
00149 const char *skeyname = cmdparams_get_str (&cmdparams, "key", NULL);
00150 int quiet = cmdparams_get_int (&cmdparams, "q", NULL) != 0;
00151 int useindex = cmdparams_get_int (&cmdparams, "i", NULL) != 0;
00152 char *map;
00153 long long islot, jslot, nslots;
00154 char *qualkey;
00155 int qualkind;
00156 int ikey;
00157 int nOtherPrimes;
00158 struct OtherKeyStruct
00159 {
00160 char *name;
00161 char *value;
00162 } *OtherPrimes;
00163
00164
00165 if (strcmp(ds, NOT_SPECIFIED) == 0 )
00166 DIE("No files: at least ds must be specified");
00167 out = stdout;
00168
00169
00170
00171 strcpy(in,ds);
00172 inbracket = index(in, '[');
00173 if (inbracket)
00174 *inbracket = '\0';
00175 else
00176 inbracket = in + strlen(in);
00177 template = drms_template_record (drms_env, in, &status);
00178 if (!template || status)
00179 DIE("Series not found or empty");
00180
00181 npkeys = template->seriesinfo->pidx_num;
00182 if (npkeys < 1)
00183 DIE("Series has no prime keys");
00184 if (strcmp(skeyname, NOT_SPECIFIED) != 0)
00185 {
00186 for (ikey=0; ikey < npkeys; ikey++)
00187 {
00188 pkey = template->seriesinfo->pidx_keywords[ikey];
00189 if (pkey->info->recscope > 1)
00190 skey = drms_keyword_slotfromindex(pkey);
00191 if (strcmp(skeyname, skey->info->name) == 0)
00192 break;
00193 }
00194 if (ikey == template->seriesinfo->pidx_num)
00195 DIE("name in key command line arg is not a prime key of this series");
00196 }
00197 else
00198 {
00199 skey = pkey = template->seriesinfo->pidx_keywords[0];
00200 }
00201 if (pkey->info->recscope > 1)
00202 {
00203 skey = drms_keyword_slotfromindex(pkey);
00204 slotted = 1;
00205 }
00206 else
00207 slotted = 0;
00208
00209
00210 ptype = skey->info->type;
00211 pname = strdup(skey->info->name);
00212 piname = strdup(pkey->info->name);
00213 punit = strdup(skey->info->unit);
00214 pformat = strdup(skey->info->format);
00215 seriesname = strdup(template->seriesinfo->seriesname);
00216
00217 otherkeys[0] = '\0';
00218 OtherPrimes = (struct OtherKeyStruct *)malloc(npkeys * sizeof(struct OtherKeyStruct));
00219 nOtherPrimes = 0;
00220 for (ikey=0; ikey < npkeys; ikey++)
00221 {
00222 DRMS_Keyword_t *tmppkey = template->seriesinfo->pidx_keywords[ikey];
00223 if (tmppkey->info->recscope > 1)
00224 tmppkey = drms_keyword_slotfromindex(pkey);
00225 if (cmdparams_exists(&cmdparams, tmppkey->info->name))
00226 {
00227 const char *value;
00228 char tmp[DRMS_MAXQUERYLEN];
00229 if (strcmp(tmppkey->info->name, pname) == 0)
00230 DIE("Can not have main prime key listed explicitly");
00231 value = cmdparams_get_str(&cmdparams, tmppkey->info->name, NULL);
00232 sprintf(tmp, "[%s=%s]", tmppkey->info->name, value);
00233 strcat(otherkeys, tmp);
00234 OtherPrimes[nOtherPrimes].name = strdup(tmppkey->info->name);
00235 OtherPrimes[nOtherPrimes].value = strdup(value);
00236 nOtherPrimes += 1;
00237 }
00238 }
00239
00240
00241 DRMS_Keyword_t *qualitykeyword = drms_keyword_lookup(template, "QUALITY", 1);
00242 if (qualitykeyword && qualitykeyword->info->type == DRMS_TYPE_INT)
00243 {
00244 qualkey = "QUALITY";
00245 qualkind = 1;
00246 }
00247 else if (drms_keyword_lookup(template, "DATAVALS", 1))
00248 {
00249 qualkey = "DATAVALS";
00250 qualkind = 2;
00251 }
00252 else
00253 {
00254 qualkey = NULL;
00255 qualkind = 0;
00256 }
00257
00258 if (slotted == 0 && ( ptype != DRMS_TYPE_SHORT && ptype != DRMS_TYPE_INT && ptype != DRMS_TYPE_LONGLONG))
00259 DIE("Must be slotted or integer type first prime key");
00260 if (ptype == DRMS_TYPE_TIME)
00261 {
00262 strcpy(name, pname);
00263 strcat(name, "_epoch");
00264 epoch = drms_getkey_time(template, name, &status);
00265 strcpy(name, pname);
00266 strcat(name, "_step");
00267 step = drms_getkey_double(template, name, &status);
00268 }
00269 else if (slotted)
00270 {
00271 strcpy(name, pname);
00272 strcat(name, "_base");
00273 epoch = (TIME)drms_getkey_double(template, name, &status);
00274 strcpy(name, pname);
00275 strcat(name, "_step");
00276 step = (TIME)drms_getkey_double(template, name, &status);
00277 }
00278 else
00279 {
00280 epoch = (TIME)0.0;
00281 step = (TIME)1.0;
00282 }
00283
00284 sprintf(in, "%s[%s=^]", seriesname, pname);
00285
00286 rs = drms_open_records (drms_env, in, &status);
00287 if (status || !rs || rs->n == 0)
00288 DIE("Series is empty");
00289 rec = rs->records[0];
00290 if (ptype == DRMS_TYPE_TIME)
00291 series_low = drms_getkey_time(rec, pname, &status);
00292 else if (slotted)
00293 series_low = (TIME)drms_getkey_double(rec, pname, &status);
00294 else
00295 series_low = (TIME)drms_getkey_longlong(rec, pname, &status);
00296 if (slotted)
00297 serieslowslot = drms_getkey_longlong(rec, piname, &status);
00298 else
00299 serieslowslot = series_low;
00300 drms_close_records(rs, DRMS_FREE_RECORD);
00301 if (strcmp(lowstr, NOT_SPECIFIED) == 0)
00302 low = series_low;
00303 else
00304 {
00305 if (ptype == DRMS_TYPE_TIME)
00306 low = sscan_time((char *)lowstr);
00307 else
00308 low = (TIME)atof(lowstr);
00309 }
00310
00311 sprintf(in, "%s[%s=$]", seriesname, pname);
00312
00313 rs = drms_open_records (drms_env, in, &status);
00314 rec = rs->records[0];
00315 if (ptype == DRMS_TYPE_TIME)
00316 series_high = drms_getkey_time(rec, pname, &status);
00317 else if (slotted)
00318 series_high = (TIME)drms_getkey_double(rec, pname, &status);
00319 else
00320 series_high = (TIME)drms_getkey_longlong(rec, pname, &status);
00321 if (slotted)
00322 serieshighslot = drms_getkey_longlong(rec, piname, &status);
00323 else
00324 serieshighslot = series_high;
00325 drms_close_records(rs, DRMS_FREE_RECORD);
00326 if (strcmp(highstr, NOT_SPECIFIED) == 0)
00327 high = series_high;
00328 else
00329 {
00330 if (ptype == DRMS_TYPE_TIME)
00331 high = sscan_time((char *)highstr);
00332 else
00333 high = atof(highstr);
00334 }
00335
00336
00337 if (slotted)
00338 {
00339 DRMS_Value_t indexval;
00340 DRMS_Value_t inval;
00341 inval.value.double_val = low;
00342 inval.type = skey->info->type;
00343 drms_keyword_slotval2indexval(skey, &inval, &indexval, NULL);
00344 lowslot = indexval.value.longlong_val;
00345
00346 inval.value.double_val = high;
00347 inval.type = pkey->info->type;
00348 drms_keyword_slotval2indexval(skey, &inval, &indexval, NULL);
00349 highslot = indexval.value.longlong_val;
00350 }
00351 else
00352 {
00353 lowslot = low;
00354 highslot = high;
00355 }
00356
00357
00358 nslots = highslot - lowslot + 1;
00359 map = (char *)malloc(sizeof(char) * nslots);
00360 for (islot=0; islot<nslots; islot++)
00361 map[islot] = DATA_UNK;
00362 islot = 0;
00363 while (islot < nslots)
00364 {
00365 DRMS_Array_t *data;
00366 int nrecs, irec;
00367 char query[DRMS_MAXQUERYLEN];
00368 char keylist[DRMS_MAXQUERYLEN];
00369 int qualindex=0;
00370 jslot = islot + 1000000;
00371 if (jslot >= nslots) jslot = nslots - 1;
00372 sprintf(query, "%s[%s=#%lld-#%lld]%s", seriesname, pname, lowslot+islot, lowslot+jslot,otherkeys);
00373 strcpy(keylist, piname);
00374 if (qualkind)
00375 {
00376 strcat(keylist, ",");
00377 strcat(keylist, qualkey);
00378 qualindex = 1;
00379 }
00380 data = drms_record_getvector(drms_env, query, keylist, DRMS_TYPE_LONGLONG, 0, &status);
00381 if (!data || status)
00382 {
00383 fprintf(stderr, "getkey_vector failed status=%d\n", status);
00384 DIE("getkey_vector failure");
00385 }
00386 nrecs = data->axis[1];
00387 for (irec = 0; irec < nrecs; irec++)
00388 {
00389 long long thisslot = *((long long *)data->data + irec);
00390 long long qualval;
00391 char val = DATA_OK;
00392 if (qualkind)
00393 {
00394 qualval = *((long long *)data->data + qualindex*nrecs + irec);
00395 if ((qualkind == 1 && qualval < 0) || (qualkind == 2 && qualval == 0))
00396 val = DATA_MISS;
00397 }
00398 map[thisslot - lowslot] = val;
00399 }
00400 islot = jslot + 1;
00401 drms_free_array(data);
00402 }
00403
00404
00405
00406 if (!quiet)
00407 {
00408 fprintf(out, "series=%s\n", seriesname);
00409 fprintf(out, "key=%s\n", pname);
00410 fprintf(out, "type=%s\n", drms_type2str(ptype));
00411 fprintf(out, "slotted=%s\n", (slotted ? "T" : "F"));
00412 fprintf(out, "epoch="); printprime(out, epoch, ptype, punit, pformat); fprintf(out, "\n");
00413 fprintf(out, "step=%f\n", step);
00414 fprintf(out, "low="); printprime(out, low, ptype, punit, pformat); fprintf(out, "\n");
00415 fprintf(out, "high="); printprime(out, high, ptype, punit, pformat); fprintf(out, "\n");
00416 fprintf(out, "series_low="); printprime(out, series_low, ptype, punit, pformat); fprintf(out, "\n");
00417 fprintf(out, "series_high="); printprime(out, series_high, ptype, punit, pformat); fprintf(out, "\n");
00418 fprintf(out, "qualkey=%s\n", qualkey);
00419 }
00420
00421
00422 islot = 0;
00423 while (islot < nslots)
00424 {
00425 long long startslot = islot;
00426 char pval[DRMS_MAXQUERYLEN];
00427 char primeval[DRMS_MAXQUERYLEN];
00428 int nsame = 1;
00429 if (useindex)
00430 sprintf(pval, "%lld", lowslot + islot);
00431 else
00432 sprintf(pval, "%s",
00433 primevalstr(epoch + (lowslot + islot) * step, ptype, punit, pformat));
00434 char thisval = map[islot], nval = 0;
00435 for (islot += 1; islot < nslots && map[islot] == thisval; islot++)
00436 nsame += 1;
00437
00438
00439 if (thisval == DATA_UNK)
00440 {
00441 int irec;
00442 DRMS_RecordSet_t *rs = drms_create_records(drms_env, nsame, seriesname, DRMS_PERMANENT, &status);
00443 TIME primekeytime;
00444 if (status || !rs)
00445 DIE("Can not create records.");
00446 fprintf(stderr,"setting %d records missing.\n",nsame);
00447 for (irec = 0; irec<nsame; irec++)
00448 {
00449 int iother;
00450 DRMS_Record_t *rec = rs->records[irec];
00451 sprintf(primeval, "%s",
00452 primevalstr(epoch + (lowslot + startslot + irec) * step, ptype, punit, pformat));
00453 drms_setkey_string(rec, pname, primeval);
00454 for (iother=0; iother<nOtherPrimes; iother++)
00455 drms_setkey_string(rec, OtherPrimes[iother].name, OtherPrimes[iother].value);
00456 if (qualkind == 1)
00457 drms_setkey_int(rec, qualkey, 0X80000000);
00458 else if (qualkind == 2)
00459 drms_setkey_int(rec, qualkey, 0);
00460 if (qualitykeyword && qualitykeyword->info->type == DRMS_TYPE_STRING)
00461 drms_setkey_string(rec, "QUALITY", "0X80000000");
00462
00463 primekeytime = sscan_time(primeval);
00464
00465 set_soho_ephemeris_keys(rec, primekeytime);
00466 }
00467 if (drms_close_records(rs, DRMS_INSERT_RECORD))
00468 DIE("Failed at close new records\n");
00469 }
00470 fprintf(out, "%12s %s %d\n",
00471 (thisval == DATA_OK ? "OK" :
00472 (thisval == DATA_MISS ? "MISS" : "NOWMISSING")),
00473 pval, nsame );
00474 }
00475 return(0);
00476 }
00477
00478