Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
readframe.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------
2  * Package: Wavelet Analysis Tool
3  * File name: readframe.cc
4  * Authors: S.Klimenko, A.Sazonov : University of Florida, Gainesville
5  *
6  * Virgo Frame Library rev. >=4.41 is required.
7  *---------------------------------------------------------------------
8 */
9 
10 
11 #include "readframe.hh"
12 
13 // Function ReadFrame
14 // double t
15 // *cname - ADC channel name
16 // *fname - frame file name
17 // seek - if true then it is allowed to seek data continuation
18 // in next file which name the function will try to guess
20  char *cname,
21  char *fname,
22  bool seek)
23 {
24  FrFile *iFile = FrFileINew(fname);
25 
26  if(iFile == NULL)
27  {
28  printf(" ReadFrame(): cannot open the input file %s\n", fname);
29  return NULL;
30  }
31 
32  int n=0;
33  int nu;
34  double rate=0.;
35 
36  short ref = 0x1234; // for byte-swapping check
37  char *refC = (char *)&ref;
38 
39  WSeries<float> *out = NULL;
40  WSeries<float> wd;
41 
42  char* nname = new char[strlen(fname)]; //computable file name
43  strcpy(nname,fname);
44 
45  char gpstime[12];
46  char newgpstime[12];
47  char *ptime; // pointer to GPS time substring in file name
48 
49  FrAdcData *adc = NULL;
50 
51  double gt, gt0, gts, gte; // GPS time, GPS time of file end
52  gts = FrFileITStart( iFile );
53  gte = FrFileITEnd( iFile );
54 
55  gt = gts + t;
56  gt0 = gt;
57 
58  iFile->compress = 255;
59 
60  do { // repeat for the next file with computed name
61 
62 // look for the frame containing specified time 'gt' in the next file
63  if ( gt < gte )
64  {
65 
66 // gt+1.e-7 in function call to prevent uncertainty in frame access
67  adc = FrAdcDataReadT(iFile, cname, gt + 1.e-7);
68 
69  if (adc == NULL) {
70  printf(" ReadFrame() error: channel %s is not found in file %s\n",
71  cname, nname);
72  if (out) delete out;
73  return NULL;
74  }
75 
76 // gt += frlen;
77 
78  if( adc->data == NULL ) continue;
79 
80  n = adc->data->nData;
81 printf(" got %d data samples\n",n);
82 
83  nu = n;
84  if ((int)wd.size() != n) wd.resize(n);
85 
86  if (!out) {
87  rate = adc->sampleRate;
88 
89  if ( n == 0 ) break; // break the frame reading loop
90 printf(" creating new WSeries\n");
91 
92  out = new WSeries<float>(n);
93 
94  }
95 
96 // do we need to swap the bytes in compressed data?
97  bool swap = (refC[0] == 0x34 && adc->data->compress < 256) ||
98  (refC[0] == 0x12 && adc->data->compress > 255);
99 
100 printf("check for swap\n");
101 // Swap bytes assuming the vector is used as an int vector
102 // Note: this code perform very slowly on Alpha CPU unless
103 // compiler can take advantage of instruction sets of ev56
104 // or ev6 architecture (option -mcpu=ev56 or ev6 for gcc/g++)
105  if(swap) {
106  unsigned char local[2];
107  char *buf = adc->data->data;
108 
109  for(unsigned int i=0; i<adc->data->nBytes-3; i=i+4) {
110  local[0] = buf[3];
111  local[1] = buf[2];
112  buf[3] = buf[0];
113  buf[2] = buf[1];
114  buf[1] = local[1];
115  buf[0] = local[0];
116  buf = buf + 4;
117  }
118  }
119 
120 printf("check for WAT compress\n");
121 /*
122  if( (adc->data->compress & 0xff) == 255 )
123  {
124 printf("call WAT unCompress\n");
125  nu = unCompress(adc->data->dataI, wd);
126 
127  if (nu!=n)
128  {
129  printf(" ReadFrame: unCompress returned wrong data length\n");
130  break; // break the frame reading loop
131  }
132 
133 // round data for compatibility with uncompressed data stored in frame files
134 printf("now rounding data\n");
135 
136  switch(adc->data->type) {
137 
138  case FR_VECT_2S:
139  for(int i=0; i<n; i++) {
140  d=wd.data[i];
141  wd.data[i]=float(short(d>0.? d+0.5 : d-0.5));
142  }
143  break;
144 
145  default:
146  break;
147  }
148  out->cpf(wd,0,0,0);
149  }
150 
151  else {
152 */
153  switch(adc->data->type) {
154 
155  case FR_VECT_2S:
156  for(int i=0; i<n; i++)
157  out->data[i] = adc->data->dataS[i];
158  break;
159 
160  case FR_VECT_4R:
161  for(int i=0; i<n; i++)
162  out->data[i] = adc->data->dataF[i];
163  break;
164 
165  default:;
166  }
167 // }
168 
169  if (adc) FrAdcDataFree(adc);
170 
171  } // end of reading frame from the next file
172 
173  FrFileIEnd(iFile);
174 
175  if (out) break; // break if frame found and data read
176 
177 // try to calculate next file name
178  sprintf(gpstime, "%9d", int(gts));
179  sprintf(newgpstime, "%9d", int(gte));
180  ptime=strstr(nname, gpstime);
181 
182  if ( ptime != NULL && atoi(ptime)==int(gts) &&
183  strlen(gpstime) == strlen(newgpstime) )
184  {
185 
186  strncpy(ptime,newgpstime,strlen(newgpstime));
187 // printf(" guess next file name to be %s\n",nname);
188 
189  }
190  else break; // break file search loop
191 
192  iFile = FrFileINew(nname);
193 
194  if(iFile == NULL) {
195  printf(" ReadFrame(): cannot open next input file %s\n", nname);
196  break; // break file search loop
197  }
198 
199  gts=FrFileITStart(iFile);
200  iFile->compress = 255;
201 
202  if (gts!=gte) {
203  printf(" ReadFrame(): next input file");
204  printf(" %s doesn't provide continuous data\n", nname);
205  FrFileIEnd(iFile);
206  break; // break file search loop
207  }
208 
209  gte=FrFileITEnd(iFile);
210 
211  } while (seek); // end of file search loop
212 
213  if (out == NULL || n == 0) return NULL;
214 
215 // delete nname;
216 
217  out->rate(rate);
218  out->start(gt0);
219 
220  return out;
221 }
WSeries< float > * ReadFrame(double t, char *cname, char *fname, bool seek)
Definition: readframe.cc:19
wavearray< double > t(hp.size())
virtual void resize(unsigned int)
Definition: wseries.cc:883
virtual size_t size() const
Definition: wavearray.hh:127
printf("total live time: non-zero lags = %10.1f \n", liveTot)
virtual void rate(double r)
Definition: wavearray.hh:123
int n
Definition: cwb_net.C:10
virtual void start(double s)
Definition: wavearray.hh:119
i drho i
ofstream out
Definition: cwb_merge.C:196
char fname[1024]
double e
strcpy(RunLabel, RUN_LABEL)
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
DataType_t * data
Definition: wavearray.hh:301