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