Logo coherent WaveBurst  
Library Reference Guide
Logo
readfrfile.cc
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Sergey Klimenko
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 /*---------------------------------------------------------------------
20  * Package: Wavelet Analysis Tool
21  * File name: readfrfile.cc
22  * Authors: S.Klimenko, A.Sazonov : University of Florida, Gainesville
23  *
24  * Virgo Frame Library rev. >= 4.41 is required.
25  *---------------------------------------------------------------------
26 */
27 
28 #include "readfrfile.hh"
29 
30 // Function ReadFrFile transparently reads both WAT-compressed
31 // and standard-compressed ADC data from single frame file.
32 // wavearray<double> - output array,
33 // tlen - length of data in seconds,
34 // tskip - skip time from the begining of the file, in seconds
35 // *cname - ADC channel name
36 // *fname - frame file name
37 // seek - if true then it is allowed to seek data continuation
38 // in next file which name the function will try to guess
40  double tlen,
41  double tskip,
42  char *cname,
43  char *fname,
44  bool seek,
45  char *channel_type)
46 {
47  FrFile *iFile = FrFileINew(fname);
48  if(iFile == NULL)
49  {
50  printf(" ReadFrFile(): cannot open the input file %s\n", fname);
51  return false;
52  }
53  if(&out == NULL)
54  {
55  printf(" ReadFrFile(): no output array specified \n");
56  return false;
57  }
58 
59  bool failure = false; // true if channel not found
60  int n=0, l=0, k=0;
61  int nu, nn=0;
62  double rate=0., d;
63 
64  short ref = 0x1234; // for byte-swapping check
65  char *refC = (char *)&ref;
66 
67  int iskip = 0; // skip in index space
68  if (tskip<0) tskip=0.;
69 
71 
72  char nname[512]; // char array for computable file name
73  sprintf(nname,"%s",fname);
74 
75 // printf("%s %s\n",nname,fname);
76 
77  char gpstime[12];
78  char newgpstime[12];
79  char *ptime; // pointer to GPS time substring in file name
80 
81  FrAdcData *adc = NULL;
82  FrProcData *proc = NULL;
83  FrSerData *ser = NULL;
84  FrSimData *sim = NULL;
85  FrVect *datap = NULL;
86 
87 // frame length (should be determined from frame file )
88  double frlen = 16.;
89 
90 // this doesn't work
91 // frlen = iFile->length; // frame length
92 // cout<<frlen<<endl;
93 
94  double gt,gt0,gts,gte; // GPS time, GPS time of file end
95  gts = FrFileITStart(iFile);
96  gte = FrFileITEnd(iFile);
97 
98 // nskip is number of full frames (or multiframe files) in tskip
99  int nskip = int( tskip/frlen );
100 
101 // fskip is a fraction of tskip: 0 <= fskip < frlen
102  double fskip = tskip - nskip*frlen;
103 
104  gt = gts + nskip*frlen; // begin GPS time of the first frame to read
105  gt0 = gts + tskip; // GPS time where data begins
106 
107 //printf(" gts = %16.6f, gte = %16.6f, gt = %16.6f\n", gts, gte, gt);
108 
109  iFile->compress = 255;
110 
111  do { // read many files if names are computable
112 
113  while(gt<gte) { // read frames from a file
114 
115 //printf(" gts = %16.6f, gte = %16.6f, gt = %16.6f\n", gts, gte, gt);
116 
117 // gt+1.e-7 in function call to prevent uncertainty in frame access
118 
119  failure = false;
120 
121  if (!strcasecmp(channel_type,"adc"))
122  {
123  adc = FrAdcDataReadT(iFile,cname,gt+1.e-7);
124  if (adc == NULL) { failure = true; }
125  else { datap = adc->data; }
126  }
127  else if (!strcasecmp(channel_type,"proc"))
128  {
129  proc = FrProcDataReadT(iFile,cname,gt+1.e-7);
130  if (proc == NULL) { failure = true; }
131  else { datap = proc->data; }
132  }
133  else
134  {
135  printf(" ReadFrFile() error: unknown channel type %s\n",
136  channel_type);
137  }
138 
139  if (failure) {
140  printf(" ReadFrFile() error: channel %s is not found in file %s\n",
141  cname, nname);
142  return false;
143  }
144 
145  gt += frlen;
146 
147  if(datap == NULL ) continue;
148 
149 // printf("startx=%f%s\n",datap->startX[0],datap->unitX[0]);
150 
151  n = datap->nData;
152  nu = n;
153  if ((int)wd.size() != n) wd.resize(n);
154 
155  if (!nn) {
156 // rate = adc->sampleRate; // not available for proc
157  if (!datap->dx[0]==0.) rate = 1./datap->dx[0];
158  nn = int(tlen * rate);
159 
160  if ( nn == 0 ) //
161  {
162  printf(" ReadFrFile: time interval too short, return no data.\n");
163  return false;
164  }
165 
166  if(out.size() != size_t(nn)) out.resize(nn);
167 
168 // k counts aready done data samples
169 // l counts how much to do in current frame
170 // set initial values
171  k = 0;
172  l = nn;
173  }
174 
175  iskip = int(fskip * rate);
176  l = l < (n - iskip)? l : n - iskip;
177 
178 // do we need to swap the bytes in compressed data?
179  bool swap = (refC[0] == 0x34 && datap->compress == 255) ||
180  (refC[0] == 0x12 && datap->compress == 511);
181 
182 // Swap bytes assuming the vector is used as an int vector
183 // Note: this code perform very slowly on Alpha CPU unless
184 // compiler can take advantage of instruction sets of ev56
185 // or ev6 architecture (option -mcpu=ev56 or ev6 for gcc/g++)
186  if(swap) {
187  unsigned char local[2];
188  char *buf = datap->data;
189 
190  for(unsigned int i=0; i<datap->nBytes-3; i=i+4) {
191  local[0] = buf[3];
192  local[1] = buf[2];
193  buf[3] = buf[0];
194  buf[2] = buf[1];
195  buf[1] = local[1];
196  buf[0] = local[0];
197  buf = buf + 4;
198  }
199  }
200 
201  if( (datap->compress & 0xff) == 255 ){
202 
203  nu = unCompress(datap->dataI, wd);
204 
205  if (nu!=n) {
206  printf(" ReadFrFile: unCompress returned wrong data length\n");
207  break; // break the frame reading loop
208  }
209 
210 // round data for compatibility with uncompressed data stored in frame files
211  switch(datap->type) {
212 
213  case FR_VECT_2S:
214  for(int i=iskip; i<n; i++) {
215  d=wd.data[i];
216  wd.data[i]=float(short(d>0.? d+0.5 : d-0.5));
217  }
218  break;
219 
220  default:
221  break;
222  }
223  for(int i=iskip; i<l; i++)
224  out.data[k+i]=double(wd.data[i]);
225  }
226 
227  else {
228 
229  switch(datap->type) {
230 
231  case FR_VECT_2S:
232  for(int i=0; i<l; i++)
233  out.data[i+k] = double(datap->dataS[i+iskip]);
234  break;
235 
236  case FR_VECT_4R:
237  for(int i=0; i<l; i++)
238  out.data[i+k] = double(datap->dataF[i+iskip]);
239  break;
240 
241  default:;
242  }
243  }
244 
245  if (adc) FrAdcDataFree(adc);
246  if (proc) FrProcDataFree(proc);
247  if (sim) FrSimDataFree(sim);
248  if (ser) FrSerDataFree(ser);
249 
250  k += l;
251  l = nn - k; // how much to read?
252  fskip = 0.; // fskip applyes to first frame only
253 
254  if ( l <= 0 ) break; // break the frame reading loop
255 
256  } // end of frames reading loop
257 
258  FrFileIEnd(iFile);
259 
260  if (nn && ((nn-k)<=0)) break; // no more data to read
261 
262 // try to calculate next file name
263  sprintf(gpstime, "%9d", int(gts));
264  sprintf(newgpstime, "%9d", int(gte));
265  ptime=strstr(nname, gpstime);
266 
267  if ( ptime != NULL && atoi(ptime) == int(gts) &&
268  strlen(gpstime) == strlen(newgpstime) ){
269 
270  strncpy(ptime, newgpstime, strlen( newgpstime ));
271 // printf(" guess next file name to be %s\n",nname);
272 
273  }
274  else break; // break file search loop
275 
276  iFile = FrFileINew(nname);
277 
278  if(iFile == NULL) {
279  printf(" ReadFrFile(): cannot open next input file %s\n", nname);
280  break; // break file search loop
281  }
282 
283  gts = FrFileITStart(iFile);
284  iFile->compress = 255;
285 
286  if (gts != gte) {
287  printf(" ReadFrFile(): next input file");
288  printf(" %s doesn't provide continuous data\n", nname);
289  FrFileIEnd(iFile);
290  break; // break file search loop
291  }
292 
293  gte = FrFileITEnd(iFile);
294 
295  } while (seek); // end of file search loop
296 
297  if (out.size() != size_t(nn)) return false;
298 
299  if (( nn - k ) > 0) // fill rest of data by zeroes
300  {
301  for (int i=k; i<nn; i++) out.data[i]=0.;
302  }
303 
304 // delete nname;
305 // if (out == 0) return NULL;
306 
307 // normal return
308  out.rate(rate);
309  out.start(gt0);
310 
311  return true;
312 }
313 
314 
315 // Function ReadFrFile transparently reads both WAT-compressed
316 // and standard-compressed ADC data from single frame file.
317 // tlen - length of data in seconds,
318 // tskip - skip time from the begining of the file, in seconds
319 // *cname - ADC channel name
320 // *fname - frame file name
321 // seek - if true then it is allowed to seek data continuation
322 // in next file which name the function will try to guess
324  double tskip,
325  char *cname,
326  char *fname,
327  bool seek,
328  char *channel_type)
329 {
330  FrFile *iFile = FrFileINew(fname);
331  if(iFile == NULL)
332  {
333  printf(" ReadFrFile(): cannot open the input file %s\n", fname);
334  return NULL;
335  }
336 
337  bool failure = false; // true if channel not found
338  int n=0, l=0, k=0;
339  int nu, nn=0;
340  double rate=0., d;
341 
342  short ref = 0x1234; // for byte-swapping check
343  char *refC = (char *)&ref;
344 
345  int iskip = 0; // skip in index space
346  if (tskip<0) tskip=0.;
347 
348  wavearray<float> *out = NULL;
349  wavearray<float> wd;
350 
351  char* nname = new char[strlen(fname)]; // char array for computable file name
352  strcpy(nname,fname); // copy original file name
353 
354  char gpstime[12];
355  char newgpstime[12];
356  char *ptime; // pointer to GPS time substring in file name
357 
358  FrAdcData *adc = NULL;
359  FrProcData *proc = NULL;
360  FrSerData *ser = NULL;
361  FrSimData *sim = NULL;
362  FrVect *datap = NULL;
363 
364 // frame length (should be determined from frame file )
365  double frlen = 16.;
366 
367 // this doesn't work
368 // frlen = iFile->length; // frame length
369 // cout<<frlen<<endl;
370 
371  double gt,gt0,gts,gte; // GPS time, GPS time of file end
372  gts = FrFileITStart(iFile);
373  gte = FrFileITEnd(iFile);
374 
375 // nskip is number of full frames (or multiframe files) in tskip
376  int nskip = int( tskip/frlen );
377 
378 // fskip is a fraction of tskip: 0 <= fskip < frlen
379  double fskip = tskip - nskip*frlen;
380 
381  gt = gts + nskip*frlen; // begin GPS time of the first frame to read
382  gt0 = gts + tskip; // GPS time of data begin
383 
384 //printf(" gts = %16.6f, gte = %16.6f, gt = %16.6f\n", gts, gte, gt);
385 
386  iFile->compress = 255;
387 
388  do { // read many files if names are computable
389 
390  while(gt<gte) { // read frames from file
391 
392 //printf(" gts = %16.6f, gte = %16.6f, gt = %16.6f\n", gts, gte, gt);
393 
394 // gt+1.e-7 in function call to prevent uncertainty in frame access
395 
396  failure = false;
397 
398  if (!strcasecmp(channel_type,"adc"))
399  {
400  adc = FrAdcDataReadT(iFile,cname,gt+1.e-7);
401  if (adc == NULL) { failure = true; }
402  else { datap = adc->data; }
403  }
404  else if (!strcasecmp(channel_type,"proc"))
405  {
406  proc = FrProcDataReadT(iFile,cname,gt+1.e-7);
407  if (proc == NULL) { failure = true; }
408  else { datap = proc->data; }
409  }
410  else
411  {
412  printf(" ReadFrFile() error: unknown channel type %s\n",
413  channel_type);
414  }
415 
416  if (failure) {
417  printf(" ReadFrFile() error: channel %s is not found in file %s\n",
418  cname, nname);
419  if (out) delete out;
420  return NULL;
421  }
422 
423  gt += frlen;
424 
425  if(datap == NULL ) continue;
426 
427 // printf("startx=%f%s\n",datap->startX[0],datap->unitX[0]);
428  n = datap->nData;
429  nu = n;
430  if ((int)wd.size() != n) wd.resize(n);
431 
432  if (!out) {
433 // rate = adc->sampleRate; // not available for proc
434  if (!datap->dx[0]==0.) rate = 1./datap->dx[0];
435  nn = int(tlen * rate);
436 
437  if ( nn == 0 ) //
438  {
439  printf(" ReadFrFile: time interval too short, return no data.\n");
440  return NULL;
441  }
442 
443  out = new wavearray<float>(nn);
444 
445 // k counts aready done data samples
446 // l counts how much to do in current frame
447 // set initial values
448  k = 0;
449  l = nn;
450  }
451 
452  iskip = int(fskip * rate);
453  l = l < (n - iskip)? l : n - iskip;
454 
455 // do we need to swap the bytes in compressed data?
456  bool swap = (refC[0] == 0x34 && datap->compress == 255) ||
457  (refC[0] == 0x12 && datap->compress == 511);
458 
459 // Swap bytes assuming the vector is used as an int vector
460 // Note: this code perform very slowly on Alpha CPU unless
461 // compiler can take advantage of instruction sets of ev56
462 // or ev6 architecture (option -mcpu=ev56 or ev6 for gcc/g++)
463  if(swap) {
464  unsigned char local[2];
465  char *buf = datap->data;
466 
467  for(unsigned int i=0; i<datap->nBytes-3; i=i+4) {
468  local[0] = buf[3];
469  local[1] = buf[2];
470  buf[3] = buf[0];
471  buf[2] = buf[1];
472  buf[1] = local[1];
473  buf[0] = local[0];
474  buf = buf + 4;
475  }
476  }
477 
478  if( (datap->compress & 0xff) == 255 ){
479 
480  nu = unCompress(datap->dataI, wd);
481 
482  if (nu!=n) {
483  printf(" ReadFrFile: unCompress returned wrong data length\n");
484  break; // break the frame reading loop
485  }
486 
487 // round data for compatibility with uncompressed data stored in frame files
488  switch(datap->type) {
489 
490  case FR_VECT_2S:
491  for(int i=iskip; i<n; i++) {
492  d=wd.data[i];
493  wd.data[i]=float(short(d>0.? d+0.5 : d-0.5));
494  }
495  break;
496 
497  default:
498  break;
499  }
500  out->cpf(wd,l,iskip,k);
501  }
502 
503  else {
504 
505  switch(datap->type) {
506 
507  case FR_VECT_2S:
508  for(int i=0; i<l; i++)
509  out->data[i+k] = datap->dataS[i+iskip];
510  break;
511 
512  case FR_VECT_4R:
513  for(int i=0; i<l; i++)
514  out->data[i+k] = datap->dataF[i+iskip];
515  break;
516 
517  default:;
518  }
519  }
520 
521  if (adc) FrAdcDataFree(adc);
522  if (proc) FrProcDataFree(proc);
523  if (sim) FrSimDataFree(sim);
524  if (ser) FrSerDataFree(ser);
525 
526  k += l;
527  l = nn - k; // how much to read?
528  fskip = 0.; // fskip applyes to first frame only
529 
530  if ( l <= 0 ) break; // break the frame reading loop
531  } // end of frames reading loop
532 
533  FrFileIEnd(iFile);
534 
535  if (out && ((nn-k)<=0)) break; // no more data to read
536 
537 // try to calculate next file name
538  sprintf(gpstime, "%9d", int(gts));
539  sprintf(newgpstime, "%9d", int(gte));
540  ptime=strstr(nname, gpstime);
541 
542  if ( ptime != NULL && atoi(ptime) == int(gts) &&
543  strlen(gpstime) == strlen(newgpstime) ){
544 
545  strncpy(ptime, newgpstime, strlen( newgpstime ));
546 // printf(" guess next file name to be %s\n",nname);
547 
548  }
549  else break; // break file search loop
550 
551  iFile = FrFileINew(nname);
552 
553  if(iFile == NULL) {
554  printf(" ReadFrFile(): cannot open next input file %s\n", nname);
555  break; // break file search loop
556  }
557 
558  gts = FrFileITStart(iFile);
559  iFile->compress = 255;
560 
561  if (gts != gte) {
562  printf(" ReadFrFile(): next input file");
563  printf(" %s doesn't provide continuous data\n", nname);
564  FrFileIEnd(iFile);
565  break; // break file search loop
566  }
567 
568  gte = FrFileITEnd(iFile);
569 
570  } while (seek); // end of file search loop
571 
572  if (out==NULL) return NULL;
573 
574  if (( nn - k ) > 0) // fill rest of data by zeroes
575  {
576  for (int i=k; i<nn; i++) out->data[i]=0.;
577  }
578 
579 // delete nname;
580 
581  if (out == 0) return NULL;
582 
583 // normal return
584  out->rate(rate);
585  out->start(gt0);
586 
587  return out;
588 }
589 
TChain sim("waveburst")
virtual void rate(double r)
Definition: wavearray.hh:141
int n
Definition: cwb_net.C:28
ofstream out
Definition: cwb_merge.C:214
int unCompress(int *in, wavearray< float > &out)
Definition: lossy.cc:176
virtual void start(double s)
Definition: wavearray.hh:137
i drho i
virtual size_t size() const
Definition: wavearray.hh:145
i() int(T_cor *100))
printf("total live time: non-zero lags = %10.1f \, liveTot)
int l
char fname[1024]
int k
double e
bool ReadFrFile(wavearray< double > &out, double tlen, double tskip, char *cname, char *fname, bool seek, char *channel_type)
Definition: readfrfile.cc:39
strcpy(RunLabel, RUN_LABEL)
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
DataType_t * data
Definition: wavearray.hh:319
virtual void resize(unsigned int)
Definition: wavearray.cc:463
void cpf(const wavearray< DataType_t > &, int=0, int=0, int=0)
Definition: wavearray.cc:717