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