1 |
tplarson |
1.5 |
char *cvsinfo_jretile_maxmem = "cvsinfo: $Header: /home/cvsuser/cvsroot/JSOC/proj/globalhs/apps/jretile_maxmem.c,v 1.4 2017/01/21 20:00:56 tplarson Exp $"; |
2 |
tplarson |
1.1 |
|
3 |
|
|
int jretile_maxmem(void) |
4 |
|
|
{ |
5 |
|
|
int newstat = 0; |
6 |
|
|
int status = DRMS_SUCCESS; |
7 |
|
|
|
8 |
|
|
char *inrecquery = NULL; |
9 |
|
|
char *outseries = NULL; |
10 |
|
|
char *segnamein = NULL; |
11 |
|
|
char *segnameout = NULL; |
12 |
|
|
DRMS_RecordSet_t *inrecset = NULL; |
13 |
|
|
DRMS_RecordSet_t *outrecset = NULL; |
14 |
|
|
int irecin, irecout, nrecsin=0, nrecsout=0, nlchunks; |
15 |
|
|
DRMS_Record_t *inrec = NULL; |
16 |
|
|
DRMS_Record_t *outrec = NULL; |
17 |
|
|
DRMS_Segment_t *segin = NULL; |
18 |
|
|
DRMS_Segment_t *segout = NULL; |
19 |
|
|
DRMS_Array_t *inarr = NULL; |
20 |
|
|
DRMS_Array_t *outarr = NULL; |
21 |
|
|
DRMS_RecLifetime_t lifetime; |
22 |
|
|
DRMS_Type_t usetype = DRMS_TYPE_FLOAT; |
23 |
|
|
int length[2], startind[2], endind[2]; |
24 |
|
|
float *inptr, *outptr; |
25 |
|
|
long long histrecnum=-1; |
26 |
|
|
int quality; |
27 |
|
|
int mapmmax=-1; |
28 |
|
|
int sinbdivs=-1; |
29 |
|
|
double cadence=0; |
30 |
|
|
int i; |
31 |
|
|
|
32 |
|
|
TIME tnow, UNIX_epoch = -220924792.000; /* 1970.01.01_00:00:00_UTC */ |
33 |
|
|
char tstartstr[100]; |
34 |
|
|
|
35 |
|
|
double tstart, tepoch, tstep, tround, tstop, tstartin, tstopin, tstepin, tstartuse, tstopuse, nseconds, chunksecs; |
36 |
|
|
char *ttotal, *tchunk; |
37 |
|
|
int ndt, ndtin; |
38 |
|
|
int lmin, lmax, lminin, lmaxin, lminuse, lmaxuse, lchunk, lchunksize, lchunkfirst, lchunklast; |
39 |
|
|
int ntimechunks, nmodes, npts, imode, itime; |
40 |
|
|
int out_time_offset, out_modes_offset, out_offset, in_time_offset, in_modes_offset, in_offset, out_index, in_index; |
41 |
|
|
int iset, lminout, lmaxout; |
42 |
|
|
double tstartout, tstopout; |
43 |
|
|
float *arrptr; |
44 |
|
|
int nrecords, nsegments, nskip; |
45 |
|
|
|
46 |
|
|
int errbufstat=setvbuf(stderr, NULL, _IONBF, BUFSIZ); |
47 |
|
|
int outbufstat=setvbuf(stdout, NULL, _IONBF, BUFSIZ); |
48 |
|
|
|
49 |
|
|
double wt0, wt1, wt2, wt3, wt; |
50 |
|
|
double ut0, ut1, ut2, ut3, ut; |
51 |
|
|
double st0, st1, st2, st3, st; |
52 |
|
|
double ct0, ct1, ct2, ct3, ct; |
53 |
|
|
|
54 |
|
|
wt0=getwalltime(); |
55 |
|
|
ct0=getcputime(&ut0, &st0); |
56 |
|
|
|
57 |
|
|
inrecquery = (char *)cmdparams_save_str(&cmdparams, "in", &newstat); |
58 |
|
|
outseries = (char *)cmdparams_save_str(&cmdparams, "out", &newstat); |
59 |
|
|
segnamein = (char *)cmdparams_save_str(&cmdparams, "segin", &newstat); |
60 |
|
|
segnameout = (char *)cmdparams_save_str(&cmdparams, "segout", &newstat); |
61 |
|
|
int seginflag = strcmp(kNOTSPECIFIED, segnamein); |
62 |
|
|
int segoutflag = strcmp(kNOTSPECIFIED, segnameout); |
63 |
|
|
int verbflag = cmdparams_save_int(&cmdparams, "VERB", &newstat); |
64 |
|
|
int permflag = cmdparams_save_int(&cmdparams, "PERM", &newstat); |
65 |
|
|
if (permflag) |
66 |
|
|
lifetime = DRMS_PERMANENT; |
67 |
|
|
else |
68 |
|
|
lifetime = DRMS_TRANSIENT; |
69 |
|
|
unsigned short calverkey = (unsigned short)cmdparams_save_int(&cmdparams, "CALVERKEY", &newstat); |
70 |
|
|
|
71 |
|
|
char *histlinkname = (char *)cmdparams_save_str(&cmdparams, "histlink", &newstat); |
72 |
|
|
|
73 |
|
|
tstart=cmdparams_save_time(&cmdparams, "TSTART", &newstat); |
74 |
|
|
ttotal=(char *)cmdparams_save_str(&cmdparams, "TTOTAL", &newstat); |
75 |
|
|
status=drms_names_parseduration(&ttotal, &nseconds, 1); |
76 |
|
|
if (status != DRMS_SUCCESS) |
77 |
|
|
{ |
78 |
|
|
// newstat = newstat | CPSAVE_UNKNOWN_ERROR; |
79 |
|
|
fprintf(stderr, "ERROR: problem parsing TTOTAL, = %s\n", ttotal); |
80 |
|
|
return 1; |
81 |
|
|
} |
82 |
|
|
tchunk=(char *)cmdparams_save_str(&cmdparams, "TCHUNK", &newstat); |
83 |
|
|
if (strcmp(kNOTSPECIFIED, tchunk)) |
84 |
|
|
{ |
85 |
|
|
status=drms_names_parseduration(&tchunk, &chunksecs, 1); |
86 |
|
|
if (status != DRMS_SUCCESS) |
87 |
|
|
newstat = newstat | CPSAVE_UNKNOWN_ERROR; |
88 |
|
|
} |
89 |
|
|
else |
90 |
|
|
chunksecs=0; |
91 |
|
|
|
92 |
|
|
lmin=cmdparams_save_int(&cmdparams, "LMIN", &newstat); |
93 |
|
|
lmax=cmdparams_save_int(&cmdparams, "LMAX", &newstat); |
94 |
|
|
lchunksize=cmdparams_save_int(&cmdparams, "LCHUNK", &newstat); |
95 |
|
|
if (lchunksize == 0) |
96 |
|
|
lchunksize=lmax+1; |
97 |
|
|
|
98 |
|
|
if (newstat) |
99 |
|
|
{ |
100 |
|
|
fprintf(stderr, "ERROR: problem with input arguments, status = %d, diagnosis follows\n", newstat); |
101 |
|
|
cpsave_decode_error(newstat); |
102 |
|
|
return 1; |
103 |
|
|
} |
104 |
|
|
else if (savestrlen != strlen(savestr)) |
105 |
|
|
{ |
106 |
|
|
fprintf(stderr, "ERROR: problem with savestr, savestrlen = %d, strlen(savestr) = %d\n", savestrlen, (int)strlen(savestr)); |
107 |
|
|
return 1; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
DRMS_Record_t *tempoutrec = drms_create_record(drms_env, |
111 |
|
|
outseries, |
112 |
|
|
DRMS_TRANSIENT, |
113 |
|
|
&status); |
114 |
|
|
|
115 |
|
|
if (status != DRMS_SUCCESS) |
116 |
|
|
{ |
117 |
|
|
fprintf(stderr,"ERROR: couldn't open a record in output dataseries %s, status = %d\n", outseries, status); |
118 |
|
|
return 1; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
// set up ancillary dataseries for processing metadata |
122 |
|
|
DRMS_Link_t *histlink = hcon_lookup_lower(&tempoutrec->links, histlinkname); |
123 |
|
|
if (histlink != NULL) |
124 |
|
|
{ |
125 |
|
|
histrecnum=set_history(histlink); |
126 |
|
|
if (histrecnum < 0) |
127 |
|
|
{ |
128 |
|
|
drms_close_record(tempoutrec, DRMS_FREE_RECORD); |
129 |
|
|
return 1; |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
else |
133 |
|
|
{ |
134 |
|
|
fprintf(stderr,"WARNING: could not find history link in output dataseries\n"); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
// these must be present in the output dataseries and variable, not links or constants |
138 |
|
|
// now done in DoIt() that calls this function |
139 |
|
|
/* |
140 |
|
|
char *outchecklist[] = {"T_START", "QUALITY", "LMIN", "LMAX", "NDT"}; |
141 |
|
|
int itest; |
142 |
|
|
for (itest=0; itest < ARRLENGTH(outchecklist); itest++) |
143 |
|
|
{ |
144 |
|
|
DRMS_Keyword_t *outkeytest = hcon_lookup_lower(&tempoutrec->keywords, outchecklist[itest]); |
145 |
|
|
if (outkeytest == NULL || outkeytest->info->islink || outkeytest->info->recscope == 1) |
146 |
|
|
{ |
147 |
|
|
fprintf(stderr, "ERROR: output keyword %s is either missing, constant, or a link\n", outchecklist[itest]); |
148 |
|
|
drms_close_record(tempoutrec, DRMS_FREE_RECORD); |
149 |
|
|
return 1; |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
*/ |
153 |
|
|
|
154 |
|
|
tepoch=drms_getkey_time(tempoutrec, "T_START_epoch", &status); |
155 |
|
|
tstep=drms_getkey_float(tempoutrec, "T_START_step", &status); |
156 |
|
|
tround=drms_getkey_float(tempoutrec, "T_START_round", &status); |
157 |
|
|
cadence=drms_getkey_float(tempoutrec, "T_STEP", &status); |
158 |
|
|
if (fmod(tstart-tepoch,tstep) > tround/2) |
159 |
|
|
{ |
160 |
|
|
fprintf(stderr, "ERROR: output dataseries seems incompatible with input parameters (tstep must divide tstart-tepoch): tstart = %f, tepoch = %f, tstep = %f\n", tstart, tepoch, tstep); |
161 |
|
|
drms_close_record(tempoutrec, DRMS_FREE_RECORD); |
162 |
|
|
return 1; |
163 |
|
|
} |
164 |
|
|
if (chunksecs == 0.0) |
165 |
|
|
chunksecs = tstep; |
166 |
|
|
else if (fmod(chunksecs,tstep)) |
167 |
|
|
{ |
168 |
|
|
fprintf(stderr, "ERROR: output dataseries seems incompatible with input parameters (tstep must divide chunksecs): chunksecs = %f, tstep = %f\n", chunksecs, tstep); |
169 |
|
|
drms_close_record(tempoutrec, DRMS_FREE_RECORD); |
170 |
|
|
return 1; |
171 |
|
|
} |
172 |
|
|
if (fmod(nseconds,chunksecs) != 0.0) |
173 |
|
|
{ |
174 |
|
|
fprintf(stderr, "ERROR: input parameters seem incompatible (chunksecs must divide nseconds): nseconds = %f, chunksecs = %f\n", nseconds, chunksecs); |
175 |
|
|
drms_close_record(tempoutrec, DRMS_FREE_RECORD); |
176 |
|
|
return 1; |
177 |
|
|
} |
178 |
|
|
ntimechunks=nseconds/chunksecs; |
179 |
|
|
ndt=chunksecs/cadence; |
180 |
|
|
if (verbflag) |
181 |
|
|
{ |
182 |
|
|
printf("%d timechunks, %.1f seconds per chunk\n", ntimechunks, chunksecs); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
int mapmmaxout=-1; |
186 |
|
|
int sinbdivsout=-1; |
187 |
|
|
DRMS_Keyword_t *outkeytest = hcon_lookup_lower(&tempoutrec->keywords, "MAPMMAX"); |
188 |
|
|
if (outkeytest != NULL && outkeytest->info->recscope == 1) |
189 |
|
|
mapmmaxout=drms_getkey_int(tempoutrec, "MAPMMAX", &status); |
190 |
|
|
outkeytest = hcon_lookup_lower(&tempoutrec->keywords, "SINBDIVS"); |
191 |
|
|
if (outkeytest != NULL && outkeytest->info->recscope == 1) |
192 |
|
|
sinbdivsout=drms_getkey_int(tempoutrec, "SINBDIVS", &status); |
193 |
|
|
|
194 |
tplarson |
1.5 |
float apinnerout=0.0; |
195 |
|
|
float apwidthout=0.0; |
196 |
|
|
outkeytest = hcon_lookup_lower(&tempoutrec->keywords, "APINNER"); |
197 |
|
|
if (outkeytest != NULL && outkeytest->info->recscope == 1) |
198 |
|
|
apinnerout=drms_getkey_float(tempoutrec, "APINNER", &status); |
199 |
|
|
outkeytest = hcon_lookup_lower(&tempoutrec->keywords, "APWIDTH"); |
200 |
|
|
if (outkeytest != NULL && outkeytest->info->recscope == 1) |
201 |
|
|
apwidthout=drms_getkey_float(tempoutrec, "APWIDTH", &status); |
202 |
|
|
|
203 |
tplarson |
1.1 |
drms_close_record(tempoutrec, DRMS_FREE_RECORD); |
204 |
|
|
|
205 |
|
|
|
206 |
|
|
char *inchecklist[] = {"T_START", "QUALITY", "LMIN", "LMAX", "T_STEP"}; |
207 |
|
|
DRMS_Keyword_t *inkeytest; |
208 |
|
|
int itest; |
209 |
|
|
inrecset = drms_open_records(drms_env, inrecquery, &status); |
210 |
|
|
// inrecset = drms_open_recordset(drms_env, inrecquery, &status); |
211 |
|
|
|
212 |
|
|
if (status != DRMS_SUCCESS || inrecset == NULL) |
213 |
|
|
{ |
214 |
|
|
fprintf(stderr, "ERROR: problem opening input recordset: status = %d\n", status); |
215 |
|
|
return 1; |
216 |
|
|
} |
217 |
|
|
nrecsin = inrecset->n; |
218 |
|
|
|
219 |
|
|
if (verbflag) |
220 |
|
|
printf("input recordset opened, nrecs = %d\n", nrecsin); |
221 |
|
|
|
222 |
|
|
int noinput=0; |
223 |
|
|
if (nrecsin == 0) |
224 |
|
|
{ |
225 |
|
|
printf("WARNING: input recordset contains no records\n"); |
226 |
|
|
noinput=1; |
227 |
|
|
goto skip1; |
228 |
|
|
// return 1; |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
inrec = inrecset->records[0]; |
232 |
|
|
|
233 |
|
|
for (itest=0; itest < ARRLENGTH(inchecklist); itest++) |
234 |
|
|
{ |
235 |
|
|
inkeytest = hcon_lookup_lower(&inrec->keywords, inchecklist[itest]); |
236 |
|
|
if (inkeytest == NULL) |
237 |
|
|
{ |
238 |
|
|
fprintf(stderr, "ERROR: required input keyword %s is missing\n", inchecklist[itest]); |
239 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
240 |
|
|
return 1; |
241 |
|
|
} |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
if (cadence != drms_getkey_float(inrec, "T_STEP", &status)) |
245 |
|
|
{ |
246 |
|
|
fprintf(stderr, "ERROR: input T_STEP does not equal output T_STEP\n"); |
247 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
248 |
|
|
return 1; |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
inkeytest = hcon_lookup_lower(&inrec->keywords, "MAPMMAX"); |
252 |
|
|
if (inkeytest != NULL) |
253 |
|
|
mapmmax=drms_getkey_int(inrec, "MAPMMAX", &status); |
254 |
|
|
if (mapmmaxout != -1 && mapmmaxout != mapmmax) |
255 |
|
|
{ |
256 |
|
|
fprintf(stderr, "ERROR: input MAPMMAX does not equal output MAPMMAX, in=%d, out=%d\n", mapmmax, mapmmaxout); |
257 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
258 |
|
|
return 1; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
inkeytest = hcon_lookup_lower(&inrec->keywords, "SINBDIVS"); |
262 |
tplarson |
1.5 |
if (inkeytest != NULL) |
263 |
tplarson |
1.1 |
sinbdivs=drms_getkey_int(inrec, "SINBDIVS", &status); |
264 |
|
|
if (sinbdivsout != -1 && sinbdivsout != sinbdivs) |
265 |
|
|
{ |
266 |
|
|
fprintf(stderr, "ERROR: input SINBDIVS does not equal output SINBDIVS, in=%d, out=%d\n", sinbdivs, sinbdivsout); |
267 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
268 |
|
|
return 1; |
269 |
|
|
} |
270 |
|
|
|
271 |
tplarson |
1.5 |
float apinner=0.0; |
272 |
|
|
float apwidth=0.0; |
273 |
|
|
inkeytest = hcon_lookup_lower(&inrec->keywords, "APINNER"); |
274 |
|
|
if (inkeytest != NULL) |
275 |
|
|
apinner=drms_getkey_float(inrec, "APINNER", &status); |
276 |
|
|
if (apinnerout != 0.0 && (int)(10000*apinnerout) != (int)(10000*apinner)) |
277 |
|
|
{ |
278 |
|
|
fprintf(stderr, "ERROR: input APINNER does not equal output APINNER, in=%d, out=%d\n",apinner, apinnerout); |
279 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
280 |
|
|
return 1; |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
inkeytest = hcon_lookup_lower(&inrec->keywords, "APWIDTH"); |
284 |
|
|
if (inkeytest != NULL) |
285 |
|
|
apwidth=drms_getkey_float(inrec, "APWIDTH", &status); |
286 |
|
|
if (apinnerout != 0.0 && (int)(10000*apwidthout) != (int)(10000*apwidth)) |
287 |
|
|
{ |
288 |
|
|
fprintf(stderr, "ERROR: input APWIDTH does not equal output APWIDTH, in=%d, out=%d\n", apwidth, apwidth); |
289 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
290 |
|
|
return 1; |
291 |
|
|
} |
292 |
|
|
|
293 |
tplarson |
1.1 |
|
294 |
|
|
status=drms_stage_records(inrecset, 1, 0); |
295 |
|
|
if (status != DRMS_SUCCESS) |
296 |
|
|
{ |
297 |
|
|
fprintf(stderr, "ERROR: drms_stage_records returned status = %d\n", status); |
298 |
|
|
return 1; |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
skip1: |
302 |
|
|
|
303 |
|
|
nrecords=0; |
304 |
|
|
nsegments=0; |
305 |
|
|
nskip=0; |
306 |
|
|
|
307 |
|
|
lchunkfirst = lmin/lchunksize; |
308 |
|
|
lchunklast = lmax/lchunksize; |
309 |
|
|
|
310 |
|
|
nlchunks = (lchunklast - lchunkfirst) + 1; |
311 |
|
|
nrecsout = nlchunks*ntimechunks; |
312 |
|
|
outrecset = drms_create_records(drms_env, nrecsout, outseries, lifetime, &status); |
313 |
|
|
if (status != DRMS_SUCCESS || outrecset == NULL) |
314 |
|
|
{ |
315 |
|
|
fprintf(stderr,"ERROR: unable to create records record in output dataseries %s, status = %d\n", outseries, status); |
316 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
317 |
|
|
return 1; |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
unsigned long long calversout, calvers; |
321 |
|
|
int calversunset=1; |
322 |
|
|
int mixflag=0; |
323 |
|
|
unsigned int nybblearrout[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
324 |
|
|
int fixflagarr[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
325 |
|
|
for (i=0;i<16;i++) |
326 |
|
|
{ |
327 |
|
|
if (getbits(calverkey,i,1)) |
328 |
|
|
fixflagarr[i]=1; |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
int *firstflagarr=(int *)malloc(nrecsin*sizeof(int)); |
332 |
tplarson |
1.3 |
DRMS_Array_t **inarrarr=(DRMS_Array_t **)malloc(nrecsin*sizeof(DRMS_Array_t *)); |
333 |
tplarson |
1.1 |
for (irecin=0; irecin < nrecsin; irecin++) |
334 |
tplarson |
1.3 |
{ |
335 |
tplarson |
1.1 |
firstflagarr[irecin]=1; |
336 |
tplarson |
1.3 |
inarrarr[irecin]=NULL; |
337 |
|
|
} |
338 |
tplarson |
1.1 |
|
339 |
|
|
irecout=0; |
340 |
|
|
for (iset=0; iset < ntimechunks; iset++) |
341 |
|
|
{ |
342 |
|
|
tstartout=tstart + iset * chunksecs; |
343 |
|
|
tstopout=tstartout+chunksecs; |
344 |
|
|
sprint_time(tstartstr, tstartout, "TAI", 0); |
345 |
|
|
|
346 |
|
|
if (verbflag) |
347 |
|
|
{ |
348 |
|
|
wt1=getwalltime(); |
349 |
|
|
ct1=getcputime(&ut1, &st1); |
350 |
|
|
printf("processing timechunk %d, tstart = %s\n", iset, tstartstr); |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
for (lchunk = lchunkfirst; lchunk <= lchunklast; lchunk++) |
354 |
|
|
{ |
355 |
|
|
lminout = lchunk * lchunksize; |
356 |
|
|
lmaxout = lminout + lchunksize - 1; |
357 |
|
|
lminout = MAXIMUM(lminout,lmin); |
358 |
|
|
lmaxout = MINIMUM(lmaxout,lmax); |
359 |
|
|
|
360 |
|
|
outrec = outrecset->records[irecout++]; |
361 |
|
|
if (histlink != NULL) |
362 |
|
|
drms_setlink_static(outrec, histlinkname, histrecnum); |
363 |
|
|
|
364 |
|
|
if (verbflag > 1) |
365 |
|
|
{ |
366 |
|
|
wt2=getwalltime(); |
367 |
|
|
ct2=getcputime(&ut2, &st2); |
368 |
|
|
printf(" processing lchunk %d, min = %d, max = %d\n", lchunk-lchunkfirst, lminout, lmaxout); |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
if (noinput) |
372 |
|
|
{ |
373 |
|
|
goto skip2; |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
if (segoutflag) |
377 |
|
|
segout = drms_segment_lookup(outrec, segnameout); |
378 |
|
|
else |
379 |
|
|
segout = drms_segment_lookupnum(outrec, 0); |
380 |
|
|
|
381 |
|
|
length[0]=2*ndt; |
382 |
|
|
length[1]=lmaxout*(lmaxout+1)/2+lmaxout - lminout*(lminout+1)/2 + 1; |
383 |
|
|
arrptr=(float *)(calloc(length[0]*length[1],sizeof(float))); |
384 |
|
|
outarr = drms_array_create(usetype, 2, length, arrptr, &status); |
385 |
|
|
if (status != DRMS_SUCCESS || outarr == NULL || segout == NULL) |
386 |
|
|
{ |
387 |
|
|
fprintf(stderr,"ERROR: problem creating output array or segment: length = [%d, %d], status = %d, histrecnum = %lld", length[0], length[1], status, histrecnum); |
388 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
389 |
|
|
drms_close_records(outrecset, DRMS_FREE_RECORD); |
390 |
|
|
return 0; |
391 |
|
|
} |
392 |
|
|
outptr = (float *)(outarr->data); |
393 |
|
|
|
394 |
|
|
nskip=0; |
395 |
|
|
mixflag=0; |
396 |
|
|
calversunset=1; |
397 |
|
|
for (irecin=0; irecin < nrecsin; irecin++) |
398 |
|
|
{ |
399 |
|
|
inrec = inrecset->records[irecin]; |
400 |
|
|
quality=drms_getkey_int(inrec, "QUALITY", &status); |
401 |
|
|
if (status != DRMS_SUCCESS || (quality & QUAL_NODATA)) //may want stricter test on quality here |
402 |
|
|
{ |
403 |
tplarson |
1.2 |
if (verbflag>2) printf("SKIP: input data not used due to quality, irecin = %d, status = %d, quality = %08x\n", irecin, status, quality); |
404 |
tplarson |
1.1 |
nskip++; |
405 |
|
|
continue; |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
tstartin=drms_getkey_time(inrec, "T_START", &status); |
409 |
|
|
tstopin=drms_getkey_time(inrec, "T_STOP", &status); |
410 |
|
|
lminin=drms_getkey_int(inrec, "LMIN", &status); |
411 |
|
|
lmaxin=drms_getkey_int(inrec, "LMAX", &status); |
412 |
|
|
ndtin=(tstopin-tstartin)/cadence; |
413 |
|
|
|
414 |
|
|
if (tstartin >= tstopout || tstopin <= tstartout || lminin > lmaxout || lmaxin < lminout) |
415 |
|
|
{ |
416 |
|
|
nskip++; |
417 |
|
|
continue; |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
if (calversunset) |
421 |
|
|
{ |
422 |
|
|
calversout=drms_getkey_longlong(inrec, "CALVER64", &status); |
423 |
|
|
if (status != DRMS_SUCCESS) |
424 |
|
|
calversout = 0; |
425 |
|
|
calversunset=0; |
426 |
|
|
for (i=0;i<16;i++) |
427 |
|
|
nybblearrout[i]=getbits(calversout,4*i+3,4); |
428 |
|
|
} |
429 |
|
|
|
430 |
tplarson |
1.4 |
calvers=drms_getkey_longlong(inrec, "CALVER64", &status); |
431 |
|
|
if (status != DRMS_SUCCESS) |
432 |
|
|
calvers = 0; |
433 |
|
|
|
434 |
|
|
for (i=0;i<16;i++) |
435 |
tplarson |
1.1 |
{ |
436 |
tplarson |
1.4 |
int nybble=getbits(calvers,4*i+3,4); |
437 |
|
|
if (fixflagarr[i]) |
438 |
tplarson |
1.1 |
{ |
439 |
tplarson |
1.4 |
if (nybble != nybblearrout[i]) |
440 |
tplarson |
1.1 |
{ |
441 |
tplarson |
1.4 |
fprintf(stderr, "ERROR: input data has mixed values for field %d of CALVER64: %d and %d, recnum = %lld, histrecnum = %lld\n", i, nybblearrout[i], nybble, inrec->recnum, histrecnum); |
442 |
|
|
return 0; |
443 |
tplarson |
1.1 |
} |
444 |
|
|
} |
445 |
tplarson |
1.4 |
else |
446 |
|
|
{ |
447 |
|
|
if (nybble < nybblearrout[i]) |
448 |
|
|
nybblearrout[i]=nybble; |
449 |
|
|
} |
450 |
|
|
} |
451 |
tplarson |
1.1 |
|
452 |
tplarson |
1.4 |
if (!mixflag && (quality & QUAL_MIXEDCALVER || calvers != calversout)) |
453 |
|
|
mixflag=1; |
454 |
tplarson |
1.1 |
|
455 |
tplarson |
1.4 |
if (firstflagarr[irecin]) |
456 |
|
|
{ |
457 |
tplarson |
1.1 |
if (seginflag) |
458 |
|
|
segin = drms_segment_lookup(inrec, segnamein); |
459 |
|
|
else |
460 |
|
|
segin = drms_segment_lookupnum(inrec, 0); |
461 |
|
|
if (segin != NULL) |
462 |
|
|
inarr = drms_segment_read(segin, usetype, &status); |
463 |
|
|
if (status != DRMS_SUCCESS || inarr == NULL || segin == NULL) |
464 |
|
|
{ |
465 |
|
|
fprintf(stderr, "ERROR: problem reading input segment: tstart = %f, lmin = %d, lmax = %d, irecin = %d, status = %d, histrecnum = %lld\n", tstartin, lminin, lmaxin, irecin, status, histrecnum); |
466 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
467 |
|
|
drms_close_records(outrecset, DRMS_FREE_RECORD); |
468 |
|
|
return 0; |
469 |
|
|
} |
470 |
|
|
else |
471 |
|
|
{ |
472 |
|
|
inarrarr[irecin]=inarr; |
473 |
|
|
} |
474 |
|
|
firstflagarr[irecin]=0; |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
inptr = (float *)(inarrarr[irecin])->data; |
478 |
|
|
|
479 |
|
|
tstartuse=MAXIMUM(tstartout, tstartin); |
480 |
|
|
tstopuse= MINIMUM(tstopout, tstopin); |
481 |
|
|
lminuse=MAXIMUM(lminout, lminin); |
482 |
|
|
lmaxuse=MINIMUM(lmaxout, lmaxin); |
483 |
|
|
nmodes=MODES(lmaxuse+1)-MODES(lminuse); |
484 |
|
|
npts=(tstopuse - tstartuse)/cadence; |
485 |
|
|
|
486 |
|
|
out_time_offset = (tstartuse - tstartout)/cadence; |
487 |
|
|
out_modes_offset = MODES(lminuse) - MODES(lminout); |
488 |
|
|
out_offset = 2 * (out_modes_offset * ndt + out_time_offset); |
489 |
|
|
in_time_offset = (tstartuse - tstartin)/cadence; |
490 |
|
|
in_modes_offset = MODES(lminuse) - MODES(lminin); |
491 |
|
|
in_offset = 2 * (in_modes_offset * ndtin + in_time_offset); |
492 |
|
|
|
493 |
|
|
for (imode=0; imode<nmodes; imode++) |
494 |
|
|
{ |
495 |
|
|
for (itime=0; itime<npts; itime++) |
496 |
|
|
{ |
497 |
|
|
in_index=in_offset + 2*itime; |
498 |
|
|
out_index=out_offset + 2*itime; |
499 |
|
|
outptr[out_index] = inptr[in_index]; |
500 |
|
|
outptr[out_index+1] = inptr[in_index+1]; |
501 |
|
|
} |
502 |
|
|
out_offset+=2*ndt; |
503 |
|
|
in_offset+=2*ndtin; |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
if (nskip == nrecsin) |
509 |
|
|
{ |
510 |
|
|
fprintf(stderr, "WARNING: no inputs for this output, no segment written\n"); |
511 |
|
|
} |
512 |
|
|
else |
513 |
|
|
{ |
514 |
|
|
outarr->bzero=segout->bzero; |
515 |
|
|
outarr->bscale=segout->bscale; |
516 |
|
|
status=drms_segment_write(segout, outarr, 0); |
517 |
|
|
nsegments++; |
518 |
|
|
if (status != DRMS_SUCCESS) |
519 |
|
|
{ |
520 |
|
|
fprintf(stderr, "ERROR: problem writing output segment: status = %d, T_START = %s, LMIN = %d, LMAX = %d, histrecnum = %lld\n", status, tstartstr, lminout, lmaxout, histrecnum); |
521 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
522 |
|
|
drms_close_records(outrecset, DRMS_FREE_RECORD); |
523 |
|
|
return 0; |
524 |
|
|
} |
525 |
|
|
} |
526 |
|
|
drms_free_array(outarr); |
527 |
|
|
|
528 |
|
|
drms_copykeys(outrec, inrec, 0, kDRMS_KeyClass_Explicit); |
529 |
|
|
// copykeys takes care of MAPMMAX, SINBDIVS, etc. that should be constant across the input |
530 |
|
|
skip2: |
531 |
|
|
|
532 |
|
|
drms_setkey_int(outrec, "LMIN", lminout); |
533 |
|
|
drms_setkey_int(outrec, "LMAX", lmaxout); |
534 |
|
|
drms_setkey_time(outrec, "T_START", tstartout); |
535 |
|
|
drms_setkey_time(outrec, "T_STOP", tstopout); |
536 |
|
|
drms_setkey_time(outrec, "T_OBS", tstartout+chunksecs/2); |
537 |
|
|
drms_setkey_int(outrec, "NDT", ndt); |
538 |
|
|
|
539 |
|
|
for (i=0;i<16;i++) |
540 |
|
|
calversout=setbits(calversout,4*i+3,4,nybblearrout[i]); |
541 |
|
|
drms_setkey_longlong(outrec, "CALVER64", calversout); |
542 |
|
|
|
543 |
|
|
if (noinput || nskip == nrecsin) |
544 |
|
|
drms_setkey_int(outrec, "QUALITY", QUAL_NODATA); |
545 |
|
|
else if (mixflag) |
546 |
|
|
drms_setkey_int(outrec, "QUALITY", QUAL_MIXEDCALVER); |
547 |
|
|
else |
548 |
|
|
drms_setkey_int(outrec, "QUALITY", 0); |
549 |
|
|
|
550 |
|
|
tnow = (double)time(NULL); |
551 |
|
|
tnow += UNIX_epoch; |
552 |
|
|
drms_setkey_time(outrec, "DATE", tnow); |
553 |
|
|
|
554 |
|
|
nrecords++; |
555 |
|
|
|
556 |
|
|
if (verbflag > 1) |
557 |
|
|
{ |
558 |
|
|
wt=getwalltime(); |
559 |
|
|
ct=getcputime(&ut, &st); |
560 |
|
|
fprintf(stdout, " lchunk %d done: %.2f ms wall time, %.2f ms cpu time\n", lchunk-lchunkfirst, |
561 |
|
|
wt-wt2, |
562 |
|
|
ct-ct2); |
563 |
|
|
} |
564 |
|
|
|
565 |
|
|
} |
566 |
|
|
|
567 |
|
|
if (verbflag) |
568 |
|
|
{ |
569 |
|
|
wt=getwalltime(); |
570 |
|
|
ct=getcputime(&ut, &st); |
571 |
|
|
fprintf(stdout, "timechunk %d done: %.2f ms wall time, %.2f ms cpu time\n", iset, |
572 |
|
|
wt-wt1, |
573 |
|
|
ct-ct1); |
574 |
|
|
|
575 |
|
|
} |
576 |
|
|
|
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
for (irecin=0; irecin < nrecsin; irecin++) |
580 |
tplarson |
1.3 |
if (inarrarr[irecin] != NULL) drms_free_array(inarrarr[irecin]); |
581 |
tplarson |
1.1 |
free(inarrarr); |
582 |
|
|
free(firstflagarr); |
583 |
|
|
|
584 |
|
|
drms_close_records(inrecset, DRMS_FREE_RECORD); |
585 |
|
|
drms_close_records(outrecset, DRMS_INSERT_RECORD); |
586 |
|
|
|
587 |
|
|
wt=getwalltime(); |
588 |
|
|
ct=getcputime(&ut, &st); |
589 |
|
|
if (verbflag) |
590 |
|
|
{ |
591 |
|
|
printf("number of records created = %d\n", nrecords); |
592 |
|
|
printf("number of segments created = %d\n", nsegments); |
593 |
|
|
fprintf(stdout, "total time spent: %.2f ms wall time, %.2f ms cpu time\n", |
594 |
|
|
wt-wt0, ct-ct0); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
printf("module %s successful completion\n", cmdparams.argv[0]); |
598 |
|
|
|
599 |
|
|
return 0; |
600 |
|
|
|
601 |
|
|
} |