1 |
#include <string.h> |
2 |
#include "jsoc_main.h" |
3 |
#include "drms.h" |
4 |
#include "drms_names.h" |
5 |
|
6 |
/* Timing statements added by Art - 2/10/2012 */ |
7 |
|
8 |
#define NOT_SPECIFIED "***Not Specified***" |
9 |
#define DIE(msg) {fprintf(stderr,"$$$$ %s: %s\n", module_name, msg); return 1;} |
10 |
|
11 |
#define kTimerFlag "t" |
12 |
|
13 |
TIMER_t *gTimer = NULL; |
14 |
|
15 |
ModuleArgs_t module_args[] = |
16 |
{ |
17 |
{ARG_STRING, "dsinp", NOT_SPECIFIED, "Input series query"}, |
18 |
{ARG_STRING, "dsout", NOT_SPECIFIED, "Output series"}, |
19 |
{ARG_FLAG, "h", "0", "Print usage message and quit"}, |
20 |
{ARG_FLAG, "v", "0", "verbose flag"}, |
21 |
{ARG_FLAG, kTimerFlag, NULL, "When set, enables timing code and causes timing messages to be printed."}, |
22 |
{ARG_END} |
23 |
}; |
24 |
|
25 |
char *module_name = "aia_slot"; |
26 |
int verbose; |
27 |
|
28 |
static void PrintElapsedTime(const char *msg) |
29 |
{ |
30 |
if (gTimer) |
31 |
{ |
32 |
fprintf(stdout, " Elapsed Time - %s: %f seconds.\n", msg, GetElapsedTime(gTimer)); |
33 |
} |
34 |
} |
35 |
|
36 |
static void TimeReset() |
37 |
{ |
38 |
if (gTimer) |
39 |
{ |
40 |
ResetTimer(gTimer); |
41 |
} |
42 |
} |
43 |
|
44 |
int nice_intro(int help) |
45 |
{ |
46 |
int usage = cmdparams_get_int(&cmdparams, "h", NULL) != 0; |
47 |
verbose = cmdparams_get_int(&cmdparams, "v", NULL) != 0; |
48 |
if (usage || help) { |
49 |
printf("aia_slot {-h} {-v} dsinp=series_record_spec dsout=output_series\n" |
50 |
" -h: print this message\n" |
51 |
" -v: verbose\n" |
52 |
"dsinp=<recordset query> as <series>{[record specifier]} - required\n" |
53 |
"dsout=<series> - required\n"); |
54 |
return(1); |
55 |
} |
56 |
return(0); |
57 |
} |
58 |
|
59 |
void sprint_time_ISO (char *tstring, TIME t) |
60 |
{ |
61 |
sprint_at(tstring,t); |
62 |
tstring[4] = tstring[7] = '-'; |
63 |
tstring[10] = 'T'; |
64 |
tstring[19] = '\0'; |
65 |
} |
66 |
|
67 |
int DoIt () |
68 |
{ |
69 |
int irec, nrecs, status, wl, first = 1, cmdexp, explim; |
70 |
char *dsinp, *dsout, now_str[100]; |
71 |
double tr_step; |
72 |
long long tr_index; |
73 |
TIME t_rec, t_obs, tr_epoch; |
74 |
DRMS_Record_t *inprec, *outrec; |
75 |
DRMS_RecordSet_t *inprs; |
76 |
DRMS_Keyword_t *inpkey = NULL, *outkey = NULL; |
77 |
DRMS_Segment_t *inpseg, *outseg; |
78 |
int timestuff = cmdparams_isflagset(&cmdparams, kTimerFlag); |
79 |
|
80 |
if (timestuff) { gTimer = CreateTimer(); } |
81 |
if (nice_intro(0)) return(0); |
82 |
dsinp = strdup(cmdparams_get_str(&cmdparams, "dsinp", NULL)); |
83 |
dsout = strdup(cmdparams_get_str(&cmdparams, "dsout", NULL)); |
84 |
if (strcmp(dsinp, NOT_SPECIFIED)==0) DIE("dsinp argument is required"); |
85 |
if (strcmp(dsout, NOT_SPECIFIED)==0) DIE("dsout argument is required"); |
86 |
PrintElapsedTime("to read input arguments"); |
87 |
TimeReset(); |
88 |
inprs = drms_open_records(drms_env, dsinp, &status); |
89 |
if (dsinp) { free(dsinp); } |
90 |
PrintElapsedTime("to open input records"); |
91 |
if (status) DIE("cant open recordset query"); |
92 |
nrecs = inprs->n; |
93 |
for (irec=0; irec<nrecs; irec++) { |
94 |
inprec = inprs->records[irec]; |
95 |
TimeReset(); |
96 |
outrec = drms_create_record(drms_env, dsout, DRMS_PERMANENT, &status); |
97 |
PrintElapsedTime("to create 1 output record"); |
98 |
if (status) DIE("cant create recordset"); |
99 |
status = drms_copykeys(outrec, inprec, 0, kDRMS_KeyClass_Explicit); |
100 |
if (status) DIE("Error in drms_copykeys()"); |
101 |
if (first) { |
102 |
tr_step = drms_getkey_double(outrec, "T_REC_step", &status); |
103 |
if (status) DIE("T_REC_step not found!"); |
104 |
tr_epoch = drms_getkey_time(inprec, "T_REC_epoch", &status); |
105 |
if (status) DIE("T_REC_epoch not found!"); |
106 |
first = 0; |
107 |
} |
108 |
sprint_time_ISO(now_str, CURRENT_SYSTEM_TIME); |
109 |
drms_setkey_string(outrec, "DATE", now_str); |
110 |
t_obs = drms_getkey_time(inprec, "T_OBS", &status); |
111 |
if (status) DIE("T_OBS not found!"); |
112 |
cmdexp = drms_getkey_int(inprec, "AIMGSHCE", &status); |
113 |
if (status) { DIE("cant get commanded exposure AIMGSHCE"); } |
114 |
else drms_setkey_int(outrec, "AIMGSHCE", cmdexp); |
115 |
wl = drms_getkey_int(inprec, "WAVELNTH", &status); |
116 |
if (!status) drms_setkey_int(outrec, "WAVELNTH", wl); |
117 |
switch (wl) { |
118 |
case 94: |
119 |
case 131: |
120 |
case 211: |
121 |
case 304: |
122 |
case 335: |
123 |
case 1600: explim = 2800; break; |
124 |
case 171: |
125 |
case 193: explim = 1931; break; |
126 |
case 1700: explim = 966; break; |
127 |
case 4500: explim = 483; break; |
128 |
default: DIE("Bad wavelength"); |
129 |
} |
130 |
if (cmdexp < explim) { t_obs -= (1.0357*explim - cmdexp)*0.0005; } |
131 |
tr_index = (t_obs - tr_epoch)/tr_step; |
132 |
t_rec = tr_epoch + tr_index*tr_step; |
133 |
drms_setkey_time(outrec, "T_REC", t_rec); |
134 |
TimeReset(); |
135 |
status = drms_link_set("lev1", outrec, inprec); |
136 |
PrintElapsedTime("to set the link from the input record to the output record"); |
137 |
TimeReset(); |
138 |
drms_close_record(outrec, DRMS_INSERT_RECORD); |
139 |
PrintElapsedTime("to close 1 output record"); |
140 |
} |
141 |
|
142 |
if (inprs) { drms_close_records(inprs, DRMS_FREE_RECORD); } |
143 |
if (dsout) { free(dsout); } |
144 |
|
145 |
return 0; |
146 |
} |