00001 #include <string.h>
00002 #include "jsoc_main.h"
00003 #include "drms.h"
00004 #include "drms_names.h"
00005
00006
00007
00008 #define NOT_SPECIFIED "***Not Specified***"
00009 #define DIE(msg) {fprintf(stderr,"$$$$ %s: %s\n", module_name, msg); return 1;}
00010
00011 #define kTimerFlag "t"
00012
00013 TIMER_t *gTimer = NULL;
00014
00015 ModuleArgs_t module_args[] =
00016 {
00017 {ARG_STRING, "dsinp", NOT_SPECIFIED, "Input series query"},
00018 {ARG_STRING, "dsout", NOT_SPECIFIED, "Output series"},
00019 {ARG_FLAG, "h", "0", "Print usage message and quit"},
00020 {ARG_FLAG, "v", "0", "verbose flag"},
00021 {ARG_FLAG, kTimerFlag, NULL, "When set, enables timing code and causes timing messages to be printed."},
00022 {ARG_END}
00023 };
00024
00025 char *module_name = "aia_slot";
00026 int verbose;
00027
00028 static void PrintElapsedTime(const char *msg)
00029 {
00030 if (gTimer)
00031 {
00032 fprintf(stdout, " Elapsed Time - %s: %f seconds.\n", msg, GetElapsedTime(gTimer));
00033 }
00034 }
00035
00036 static void TimeReset()
00037 {
00038 if (gTimer)
00039 {
00040 ResetTimer(gTimer);
00041 }
00042 }
00043
00044 int nice_intro(int help)
00045 {
00046 int usage = cmdparams_get_int(&cmdparams, "h", NULL) != 0;
00047 verbose = cmdparams_get_int(&cmdparams, "v", NULL) != 0;
00048 if (usage || help) {
00049 printf("aia_slot {-h} {-v} dsinp=series_record_spec dsout=output_series\n"
00050 " -h: print this message\n"
00051 " -v: verbose\n"
00052 "dsinp=<recordset query> as <series>{[record specifier]} - required\n"
00053 "dsout=<series> - required\n");
00054 return(1);
00055 }
00056 return(0);
00057 }
00058
00059 void sprint_time_ISO (char *tstring, TIME t)
00060 {
00061 sprint_at(tstring,t);
00062 tstring[4] = tstring[7] = '-';
00063 tstring[10] = 'T';
00064 tstring[19] = '\0';
00065 }
00066
00067 int DoIt ()
00068 {
00069 int irec, nrecs, status, wl, first = 1, cmdexp, explim;
00070 char *dsinp, *dsout, now_str[100];
00071 double tr_step;
00072 long long tr_index;
00073 TIME t_rec, t_obs, tr_epoch;
00074 DRMS_Record_t *inprec, *outrec;
00075 DRMS_RecordSet_t *inprs;
00076 DRMS_Keyword_t *inpkey = NULL, *outkey = NULL;
00077 DRMS_Segment_t *inpseg, *outseg;
00078 int timestuff = cmdparams_isflagset(&cmdparams, kTimerFlag);
00079
00080 if (timestuff) { gTimer = CreateTimer(); }
00081 if (nice_intro(0)) return(0);
00082 dsinp = strdup(cmdparams_get_str(&cmdparams, "dsinp", NULL));
00083 dsout = strdup(cmdparams_get_str(&cmdparams, "dsout", NULL));
00084 if (strcmp(dsinp, NOT_SPECIFIED)==0) DIE("dsinp argument is required");
00085 if (strcmp(dsout, NOT_SPECIFIED)==0) DIE("dsout argument is required");
00086 PrintElapsedTime("to read input arguments");
00087 TimeReset();
00088 inprs = drms_open_records(drms_env, dsinp, &status);
00089 if (dsinp) { free(dsinp); }
00090 PrintElapsedTime("to open input records");
00091 if (status) DIE("cant open recordset query");
00092 nrecs = inprs->n;
00093 for (irec=0; irec<nrecs; irec++) {
00094 inprec = inprs->records[irec];
00095 TimeReset();
00096 outrec = drms_create_record(drms_env, dsout, DRMS_PERMANENT, &status);
00097 PrintElapsedTime("to create 1 output record");
00098 if (status) DIE("cant create recordset");
00099 status = drms_copykeys(outrec, inprec, 0, kDRMS_KeyClass_Explicit);
00100 if (status) DIE("Error in drms_copykeys()");
00101 if (first) {
00102 tr_step = drms_getkey_double(outrec, "T_REC_step", &status);
00103 if (status) DIE("T_REC_step not found!");
00104 tr_epoch = drms_getkey_time(inprec, "T_REC_epoch", &status);
00105 if (status) DIE("T_REC_epoch not found!");
00106 first = 0;
00107 }
00108 sprint_time_ISO(now_str, CURRENT_SYSTEM_TIME);
00109 drms_setkey_string(outrec, "DATE", now_str);
00110 t_obs = drms_getkey_time(inprec, "T_OBS", &status);
00111 if (status) DIE("T_OBS not found!");
00112 cmdexp = drms_getkey_int(inprec, "AIMGSHCE", &status);
00113 if (status) { DIE("cant get commanded exposure AIMGSHCE"); }
00114 else drms_setkey_int(outrec, "AIMGSHCE", cmdexp);
00115 wl = drms_getkey_int(inprec, "WAVELNTH", &status);
00116 if (!status) drms_setkey_int(outrec, "WAVELNTH", wl);
00117 switch (wl) {
00118 case 94:
00119 case 131:
00120 case 211:
00121 case 304:
00122 case 335:
00123 case 1600: explim = 2800; break;
00124 case 171:
00125 case 193: explim = 1931; break;
00126 case 1700: explim = 966; break;
00127 case 4500: explim = 483; break;
00128 default: DIE("Bad wavelength");
00129 }
00130 if (cmdexp < explim) { t_obs -= (1.0357*explim - cmdexp)*0.0005; }
00131 tr_index = (t_obs - tr_epoch)/tr_step;
00132 t_rec = tr_epoch + tr_index*tr_step;
00133 drms_setkey_time(outrec, "T_REC", t_rec);
00134 TimeReset();
00135 status = drms_link_set("lev1", outrec, inprec);
00136 PrintElapsedTime("to set the link from the input record to the output record");
00137 TimeReset();
00138 drms_close_record(outrec, DRMS_INSERT_RECORD);
00139 PrintElapsedTime("to close 1 output record");
00140 }
00141
00142 if (inprs) { drms_close_records(inprs, DRMS_FREE_RECORD); }
00143 if (dsout) { free(dsout); }
00144
00145 return 0;
00146 }