00001
00002
00003
00004
00005 #include "jsoc_main.h"
00006 #include "astro.h"
00007
00008 #define kORBSERIES "in"
00009 #define kINTERPALG "alg"
00010 #define kTGTTIMESARR "tgtarr"
00011 #define kTGTTIMES "tgt"
00012 #define kGRIDTIMES "grid"
00013 #define kTESTINTERP "i"
00014 #define kNOVELOCITY "n"
00015 #define kSAAHLZ "s"
00016
00017 #define kOBSDATE "OBS_DATE"
00018 #define kHCIX "HCIEC_X"
00019 #define kHCIY "HCIEC_Y"
00020 #define kHCIZ "HCIEC_Z"
00021 #define kHCIVX "HCIEC_VX"
00022 #define kHCIVY "HCIEC_VY"
00023 #define kHCIVZ "HCIEC_VZ"
00024
00025 ModuleArgs_t module_args[] =
00026 {
00027 {ARG_STRING, kORBSERIES, "sdo.fds_orbit_vectors", "Series containing orbit position and velocity vectors; it testing, then contains record-set query."},
00028 {ARG_STRING, kINTERPALG, "linear", "Supported algorithm to use when interpolating between grid times."},
00029 {ARG_DOUBLES, kTGTTIMESARR, "0.0", "Array of internal times (doubles) for which orbit information is to be returned. "},
00030 {ARG_STRING, kTGTTIMES, "problem", "File containing array of internal times (doubles) for which orbit information is to be returned. "},
00031 {ARG_STRING, kGRIDTIMES, "unspecified", "Record-set query that specifies grid-vector internal times."},
00032 {ARG_FLAG, kTESTINTERP, "", "Test interpolation routine."},
00033 {ARG_FLAG, kNOVELOCITY, "", "The orbit series has no velocity keywords (like iris.orbitvectors)"},
00034 {ARG_FLAG, kSAAHLZ, "", "Test the SAA-HLZ stuff out."},
00035 {ARG_END}
00036 };
00037
00038 char *module_name = "getorbitinfo";
00039
00040 enum sdoorb_stat_enum
00041 {
00042 kSDOORB_success = 0,
00043 kSDOORB_failure
00044 };
00045
00046 typedef enum sdoorb_stat_enum sdoorb_stat_t;
00047
00048 int DoIt(void)
00049 {
00050 sdoorb_stat_t status = kSDOORB_success;
00051 IORBIT_Info_t *info = NULL;
00052 IORBIT_Alg_t interpalg;
00053 const char *orbseries = NULL;
00054 const char *alg = NULL;
00055 const char *tgttimesstr = NULL;
00056 double *tgttimes = NULL;
00057 const char *gridtimes = NULL;
00058 int ntimes = 0;
00059 int testinterp;
00060 int novelocity;
00061 int saahlz;
00062 TIMER_t *tmr = NULL;
00063
00064 orbseries = cmdparams_get_str(&cmdparams, kORBSERIES, NULL);
00065 testinterp = cmdparams_isflagset(&cmdparams, kTESTINTERP);
00066 novelocity = cmdparams_isflagset(&cmdparams, kNOVELOCITY);
00067 saahlz = cmdparams_isflagset(&cmdparams, kSAAHLZ);
00068
00069 alg = cmdparams_get_str(&cmdparams, kINTERPALG, NULL);
00070
00071
00072
00073 tgttimesstr = cmdparams_get_str(&cmdparams, kTGTTIMES, NULL);
00074 if (tgttimesstr[0] == '@')
00075 {
00076
00077 struct stat stBuf;
00078 FILE *atfile = NULL;
00079 const char *filestr = NULL;
00080 char lineBuf[LINE_MAX];
00081 char *fullline = NULL;
00082 int stgt = 48;
00083
00084 if (drms_env->verbose)
00085 {
00086
00087 tmr = CreateTimer();
00088 }
00089
00090 filestr = tgttimesstr + 1;
00091
00092 if (!stat(filestr, &stBuf))
00093 {
00094 if (S_ISREG(stBuf.st_mode))
00095 {
00096
00097 if ((atfile = fopen(filestr, "r")) == NULL)
00098 {
00099 fprintf(stderr, "Cannot open @file %s for reading, skipping.\n", filestr);
00100 }
00101 else
00102 {
00103 int len = 0;
00104 int itgt = 0;
00105 tgttimes = malloc(sizeof(double) * stgt);
00106
00107 while (!(fgets(lineBuf, LINE_MAX, atfile) == NULL))
00108 {
00109
00110 len = strlen(lineBuf);
00111
00112 fullline = strdup(lineBuf);
00113
00114 if (len == LINE_MAX - 1)
00115 {
00116
00117 while (!(fgets(lineBuf, LINE_MAX, atfile) == NULL))
00118 {
00119 fullline = realloc(fullline, strlen(fullline) + strlen(lineBuf) + 1);
00120 snprintf(fullline + strlen(fullline),
00121 strlen(lineBuf) + 1,
00122 "%s",
00123 lineBuf);
00124 if (strlen(lineBuf) > 1 && lineBuf[strlen(lineBuf) - 1] == '\n')
00125 {
00126 break;
00127 }
00128 }
00129 }
00130
00131 len = strlen(fullline);
00132
00133 if (fullline[len - 1] == '\n')
00134 {
00135 fullline[len - 1] = '\0';
00136 }
00137
00138
00139 if (itgt == stgt)
00140 {
00141 stgt *= 2;
00142 tgttimes = realloc(tgttimes, sizeof(double) * stgt);
00143 }
00144
00145 sscanf(fullline, "%lf", &(tgttimes[itgt]));
00146
00147 if (fullline)
00148 {
00149 free(fullline);
00150 fullline = NULL;
00151 }
00152
00153 itgt++;
00154 }
00155
00156 ntimes = itgt;
00157 }
00158 }
00159 }
00160
00161 if (drms_env->verbose)
00162 {
00163 fprintf(stdout, "file read seconds elapsed: %f\n", GetElapsedTime(tmr));
00164 DestroyTimer(&tmr);
00165 }
00166 }
00167 else
00168 {
00169 ntimes = cmdparams_get_dblarr(&cmdparams, kTGTTIMESARR, &tgttimes, NULL);
00170 }
00171
00172 gridtimes = cmdparams_get_str(&cmdparams, kGRIDTIMES, NULL);
00173
00174 if (strcasecmp(gridtimes, "unspecified") == 0)
00175 {
00176 gridtimes = NULL;
00177 }
00178
00179 if (strcasecmp(alg, "linear") == 0)
00180 {
00181 interpalg = IORBIT_Alg_Linear;
00182 }
00183 else if (strncasecmp(alg, "quad", strlen("quad")) == 0)
00184 {
00185 interpalg = IORBIT_Alg_Quadratic;
00186 }
00187 else
00188 {
00189 fprintf(stderr, "Unsupported interpolation algorithm '%s'.\n", alg);
00190 status = kSDOORB_failure;
00191 }
00192
00193 if (status == kSDOORB_success)
00194 {
00195 int rv = 0;
00196
00197 if (drms_env->verbose)
00198 {
00199
00200 tmr = CreateTimer();
00201 }
00202
00203 if (novelocity)
00204 {
00205 HContainer_t *keymap = NULL;
00206
00207 keymap = hcon_create(DRMS_MAXKEYNAMELEN, DRMS_MAXKEYNAMELEN, NULL, NULL, NULL, NULL, 0);
00208
00209 if (keymap)
00210 {
00211 hcon_insert(keymap, "kXGCI", "geixobs");
00212 hcon_insert(keymap, "kYGCI", "geiyobs");
00213 hcon_insert(keymap, "kZGCI", "geizobs");
00214 hcon_insert(keymap, "kXHCI", "heixobs");
00215 hcon_insert(keymap, "kYHCI", "heiyobs");
00216 hcon_insert(keymap, "kZHCI", "heizobs");
00217 hcon_insert(keymap, "kRSUNOBS", "rsunobs");
00218 hcon_insert(keymap, "kOBSVR", "obsvr");
00219 hcon_insert(keymap, "kDSUNOBS", "dsunobs");
00220 hcon_insert(keymap, "kOBSDATE", "obsdate");
00221
00222 rv = iorbit_getinfo_ext(drms_env,
00223 orbseries,
00224 gridtimes,
00225 interpalg,
00226 tgttimes,
00227 ntimes,
00228 kIORBIT_CacheAction_DontCache,
00229 &info,
00230 keymap);
00231
00232 hcon_destroy(&keymap);
00233 }
00234 else
00235 {
00236 rv = kLIBASTRO_OutOfMemory;
00237 }
00238 }
00239 else if (saahlz)
00240 {
00241
00242 IORBIT_SaaHlzInfo_t *saahlzinfo = NULL;
00243 int itime;
00244 LinkedList_t *list = NULL;
00245 LinkedList_t **pList = NULL;
00246 ListNode_t *node = NULL;
00247 char *eventType = NULL;
00248 int nhlz;
00249 int shlz;
00250 int saa;
00251 HContainer_t *colToList = NULL;
00252 HContainer_t **pColToList = NULL;
00253 char timeStr[IORBIT_SAAHLZINFO_TIME_KEY_LEN];
00254
00255 rv = iorbit_getSaaHlzInfo(drms_env, "iris.saa_hlz", tgttimes, ntimes, &saahlzinfo);
00256
00257 if (rv == kLIBASTRO_Success)
00258 {
00259 for (itime = 0; itime < ntimes; itime++)
00260 {
00261 nhlz = 0;
00262 shlz = 0;
00263 saa = 0;
00264
00265 snprintf(timeStr, sizeof(timeStr), "%lf", tgttimes[itime]);
00266
00267 if ((pColToList = hcon_lookup(saahlzinfo, timeStr)) != NULL)
00268 {
00269 colToList = *pColToList;
00270
00271 if ((pList = hcon_lookup(colToList, IORBIT_SAAHLZINFO_KW_EVENT_TYPE)) != NULL)
00272 {
00273 list = *pList;
00274 list_llreset(list);
00275
00276 while((node = list_llnext(list)) != NULL)
00277 {
00278 eventType = (char *)node->data;
00279
00280 if (strcasecmp(eventType, "NHLZ") == 0)
00281 {
00282 nhlz = 1;
00283 }
00284 else if (strcasecmp(eventType, "SHLZ") == 0)
00285 {
00286 shlz = 1;
00287 }
00288 else if (strcasecmp(eventType, "SAA") == 0)
00289 {
00290 saa = 1;
00291 }
00292 }
00293
00294 printf("For time %lf:\n", tgttimes[itime]);
00295
00296 if (nhlz && shlz)
00297 {
00298
00299 printf(" ERROR - setting HLZ to 0.\n");
00300 }
00301 else if (nhlz)
00302 {
00303 printf(" Setting HLZ to 1.\n");
00304 }
00305 else if (shlz)
00306 {
00307 printf(" Setting HLZ to 2.\n");
00308 }
00309 else
00310 {
00311 printf(" Setting HLZ to 0.\n");
00312 }
00313
00314 if (saa)
00315 {
00316 printf(" Setting SAA to 1.\n");
00317 }
00318 else
00319 {
00320 printf(" Setting SAA to 0.\n");
00321 }
00322 }
00323 }
00324 }
00325
00326 iorbit_cleanSaaHlzInfo(&saahlzinfo);
00327 }
00328 else
00329 {
00330 printf("Couldn't query db properly.\n");
00331 status = kSDOORB_failure;
00332 }
00333
00334 return status;
00335 }
00336 else
00337 {
00338 rv = iorbit_getinfo(drms_env,
00339 orbseries,
00340 gridtimes,
00341 interpalg,
00342 tgttimes,
00343 ntimes,
00344 kIORBIT_CacheAction_DontCache,
00345 &info);
00346 }
00347
00348 if (rv != kLIBASTRO_Success)
00349 {
00350 status = kSDOORB_failure;
00351 }
00352 else
00353 {
00354 if (drms_env->verbose)
00355 {
00356 fprintf(stdout, "iorbit_getinfo() seconds elapsed: %f\n", GetElapsedTime(tmr));
00357 DestroyTimer(&tmr);
00358 }
00359
00360
00361 IORBIT_Info_t *infoitem = NULL;
00362 char timestr[128];
00363 int iinfo;
00364
00365 if (novelocity)
00366 {
00367 fprintf(stdout, "%-24s%-22s%-22s%-22s%-22s%-22s%-22s%-22s%-22s%-22s\n",
00368 "obstime", "hciX-interp", "hciY-interp", "hciZ-interp", "gciX-interp", "gciY-interp", "gciZ-interp", "rsunobs-interp", "obsvr-interp", "dsunobs-interp");
00369
00370 for (iinfo = 0; iinfo < ntimes; iinfo++)
00371 {
00372
00373 infoitem = &(info[iinfo]);
00374 sprint_time(timestr, infoitem->obstime, "UTC", 0);
00375
00376 if (!testinterp)
00377 {
00378 fprintf(stdout, "%-24s%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f\n",
00379 timestr, infoitem->hciX, infoitem->hciY, infoitem->hciZ,
00380 infoitem->gciX, infoitem->gciY, infoitem->gciZ, infoitem->rsun_obs, infoitem->obs_vr, infoitem->dsun_obs);
00381 }
00382 }
00383 }
00384 else
00385 {
00386 fprintf(stdout, "%-32s%-20s%-15s%-15s%-15s%-12s%-12s%-12s%-10s%-32s\n",
00387 "obstime", "dsun_obs", "obs_vr", "obs_vw", "obs_vn", "rsun_obs", "crln_obs", "crlt_obs", "car_rot", "orb_rec");
00388
00389 for (iinfo = 0; iinfo < ntimes; iinfo++)
00390 {
00391 infoitem = &(info[iinfo]);
00392 sprint_time(timestr, infoitem->obstime, "UTC", 0);
00393
00394 fprintf(stdout, "%-32s%-20.3f%-15.3f%-15.3f%-15.3f%-12.3f%-12.3f%-12.3f%-10d%-32s\n",
00395 timestr, infoitem->dsun_obs, infoitem->obs_vr, infoitem->obs_vw, infoitem->obs_vn,
00396 infoitem->rsun_obs, infoitem->crln_obs, infoitem->crlt_obs, infoitem->car_rot, infoitem->orb_rec);
00397 }
00398
00399
00400 fprintf(stdout, "\n\n");
00401
00402 if (!testinterp)
00403 {
00404 fprintf(stdout, "%-24s%-14s%-22s%-22s%-22s%-22s%-22s%-22s\n",
00405 "obstime", "secs", "hciX-interp", "hciY-interp", "hciZ-interp", "hciVX-interp", "hciVY-interp", "hciVZ-interp");
00406 }
00407 else
00408 {
00409 fprintf(stdout, "%-24s%-12s%-22s%-22s%-22s%-22s%-22s%-22s%-20s%-20s%-20s%-14s%-14s%-14s\n",
00410 "obstime", "secs",
00411 "hciX-interp", "hciX-actual",
00412 "hciY-interp", "hciY-actual",
00413 "hciZ-interp", "hciZ-actual",
00414 "hciVX-interp", "hciVX-actual",
00415 "hciVY-interp", "hciVY-actual",
00416 "hciVZ-interp", "hciVZ-actual");
00417 }
00418
00419 for (iinfo = 0; iinfo < ntimes; iinfo++)
00420 {
00421
00422 infoitem = &(info[iinfo]);
00423 sprint_time(timestr, infoitem->obstime, "UTC", 0);
00424
00425 if (!testinterp)
00426 {
00427 fprintf(stdout, "%-24s%-14.1f%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f%-22.4f\n",
00428 timestr, infoitem->obstime, infoitem->hciX, infoitem->hciY, infoitem->hciZ,
00429 infoitem->hciVX, infoitem->hciVY, infoitem->hciVZ);
00430 }
00431 else
00432 {
00433
00434
00435
00436
00437
00438
00439 char query[DRMS_MAXQUERYLEN];
00440 int drmsstatus = DRMS_SUCCESS;
00441 DRMS_RecordSet_t *rs = NULL;
00442 TIME obstime;
00443 double actual_hciX = 0;
00444 double actual_hciY = 0;
00445 double actual_hciZ = 0;
00446 double actual_hciVX = 0;
00447 double actual_hciVY = 0;
00448 double actual_hciVZ = 0;
00449
00450 snprintf(query, sizeof(query), "%s[%s]", orbseries, timestr);
00451
00452 rs = drms_open_records(drms_env, query, &drmsstatus);
00453 if (rs && rs->n == 1)
00454 {
00455 obstime = drms_getkey_time(rs->records[0], kOBSDATE, &drmsstatus);
00456
00457 if (obstime == infoitem->obstime)
00458 {
00459 actual_hciX = drms_getkey_double(rs->records[0], kHCIX, &drmsstatus);
00460 actual_hciY = drms_getkey_double(rs->records[0], kHCIY, &drmsstatus);
00461 actual_hciZ = drms_getkey_double(rs->records[0], kHCIZ, &drmsstatus);
00462 actual_hciVX = drms_getkey_double(rs->records[0], kHCIVX, &drmsstatus);
00463 actual_hciVY = drms_getkey_double(rs->records[0], kHCIVY, &drmsstatus);
00464 actual_hciVZ = drms_getkey_double(rs->records[0], kHCIVZ, &drmsstatus);
00465 }
00466 }
00467
00468 fprintf(stdout, "%-24s%-12.1f%-22.8f%-22.8f%-22.8f%-22.8f%-22.8f%-22.8f%-22.8f%-22.8f%-22.8f%-14.8f%-14.8f%-14.8f\n",
00469 timestr, infoitem->obstime,
00470 infoitem->hciX, actual_hciX,
00471 infoitem->hciY, actual_hciY,
00472 infoitem->hciZ, actual_hciZ,
00473 infoitem->hciVX, actual_hciVX,
00474 infoitem->hciVY, actual_hciVY,
00475 infoitem->hciVZ, actual_hciVZ);
00476 }
00477 }
00478 }
00479
00480
00481 if (info)
00482 {
00483 free(info);
00484 }
00485 }
00486
00487 iorbit_cleanup();
00488 }
00489
00490 return status;
00491 }