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