FrameL.c 731 KB
Newer Older
1

Emmanuel Pacaud's avatar
Emmanuel Pacaud committed
2
#define _POSIX_SOURCE
swmgr's avatar
swmgr committed
3
4
5
/*---------------------------------------------------------------------------*/
/* File: FrameL.c                                                            */
/*                                                                           */
swmgr's avatar
swmgr committed
6
/* Copyright (C) 2003, B. Mours.                                             */
swmgr's avatar
swmgr committed
7
8
9
/* For the licensing terms see the LICENSE file.                             */
/* For the list of contributors see the history section of the documentation */
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
10
11
#include <math.h>
#include <stdarg.h>
Christian Arnault's avatar
Import    
Christian Arnault committed
12
#include <ctype.h>
swmgr's avatar
swmgr committed
13
#include <errno.h>
Christian Arnault's avatar
Import  
Christian Arnault committed
14
#include "FrameL.h"
15
#include <sys/time.h>
Christian Arnault's avatar
Import  
Christian Arnault committed
16

root's avatar
root committed
17
/*------------------------------------ buffer space used in several places --*/
Christian Arnault's avatar
Import  
Christian Arnault committed
18
#define FRBUFSIZE 100000
Emmanuel Pacaud's avatar
Emmanuel Pacaud committed
19
char FrBuf[FRBUFSIZE];
Christian Arnault's avatar
Import  
Christian Arnault committed
20
int FrBufSize = FRBUFSIZE;
swmgr's avatar
swmgr committed
21
char FrErrMsg[1024];
root's avatar
root committed
22
23
#define FRMAXTAG 1000
/*--------------------------------------------------miscellaneous variables--*/
Christian Arnault's avatar
Import  
Christian Arnault committed
24
int    FrSshort, FrSint, FrSlong, FrSfloat, FrSdouble;
25
FILE  *FrFOut = NULL;
Christian Arnault's avatar
Import    
Christian Arnault committed
26
int    FrDebugLvl = 0;
teuler's avatar
teuler committed
27
char  *FrErrorMsg[] = {"OK",
swmgr's avatar
swmgr committed
28
              "No Frame Error",
teuler's avatar
teuler committed
29
30
31
32
33
34
              "No file Error",
              "malloc failed error",
              "Buffer overflow error",
              "Write Error",
              "Read Error",
              "FRSET Error",
swmgr's avatar
swmgr committed
35
              "Wrong character length",
teuler's avatar
teuler committed
36
37
              "Bad end of frame Error",
              "File Open Error",
Benoit Mours's avatar
Benoit Mours committed
38
39
40
41
42
              "Cheksum Error",
              "Bad nEvent",
              "Bad nStat",
              "Too long string",
              "FrVectRead: too large nBytes"};
43
char *FrFlipPrefix = NULL;      /*------------- until v8r18 it was: "V1:";---*/
44
int FrFormatVersion = 8;
root's avatar
root committed
45
/*---------------------------------------Header for each class of structure--*/
root's avatar
root committed
46
FrSH *FrSHRoot[FRMAPMAXSH];
Christian Arnault's avatar
Import    
Christian Arnault committed
47
int FrnSH = 0;
root's avatar
root committed
48
/*-----------------------------static variable used by the FrError functions-*/
Christian Arnault's avatar
Import  
Christian Arnault committed
49
void (*FrErrorHandler)() = FrErrorDefHandler;
root's avatar
root committed
50
51
52
53
54
/*----------------------------------------------------- Define for the zlib--*/
int Frz_compress   (unsigned char *dest, unsigned long *destLen,
	                   char *source, unsigned long sourceLen, int level);
int Frz_uncompress (unsigned char *dest, unsigned long *destLen,
		           char *source, unsigned long sourceLen);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*---------------------------------define to use FFTW malloc/free functions--*/
#ifdef FFTW_MALLOC
void *fftw_free  (void *p);
void *fftw_malloc(size_t n);
void *FrCalloc(size_t nobj, size_t size)
{void *mem;
 mem = fftw_malloc(nobj*size);
 if(mem == NULL) return(NULL);
 memset(mem,0,nobj*size);
 return(mem);};

#define malloc fftw_malloc
#define calloc FrCalloc
#define free   fftw_free
#endif
root's avatar
root committed
70
71
72
73
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------- FrLibIni -*/
Christian Arnault's avatar
Import  
Christian Arnault committed
74
75
FILE *FrLibIni(char *outFile, 
               FILE *fOut, 
Christian Arnault's avatar
Import    
Christian Arnault committed
76
               int dbglvl)
Christian Arnault's avatar
Import  
Christian Arnault committed
77
                 /*if not outFile == NULL, we use fOut for output file*/
root's avatar
root committed
78
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{static int icall = 0;
 
  icall++;
  if(icall > 1) 
    {printf(" Warning, FrLibIni is call twice\n");
     return(FrFOut);}

  if(outFile == NULL)
    {FrFOut = fOut;
     if(FrFOut == NULL)  {FrFOut = stdout;}}
  else
    {printf("FrLibIni: output will be sent on file:%s\n",outFile);
     FrFOut = fopen(outFile,"w");
     if(FrFOut == NULL)
         {printf("\n !! Opening file error, output stay on screen\n");
          FrFOut = stdout;}}
  
  FrDebugLvl = dbglvl;

Christian Arnault's avatar
Import    
Christian Arnault committed
98
99
  FrLibVersion(FrFOut);

Christian Arnault's avatar
Import  
Christian Arnault committed
100
101
102
  FrLibShortIni();

  return(FrFOut);}
Christian Arnault's avatar
Import    
Christian Arnault committed
103

root's avatar
root committed
104
/*----------------------------------------------------------- FrLibShortIni -*/
Christian Arnault's avatar
Import  
Christian Arnault committed
105
void FrLibShortIni()
root's avatar
root committed
106
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
107
108
109
110
{static int icall = 0;
 
  icall++;
  if(icall > 1) return;
teuler's avatar
teuler committed
111
                                    /*----- i/o package initialization ------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
112
113
114
	
  FrSshort  = sizeof(short);
  FrSint    = sizeof(int);
root's avatar
root committed
115
  FrSlong   = sizeof(FRLONG);
Christian Arnault's avatar
Import  
Christian Arnault committed
116
117
118
119
  FrSfloat  = sizeof(float);
  FrSdouble = sizeof(double);

    /*--- check the size of words. Unpredictible behevior will results
teuler's avatar
teuler committed
120
              from the use of non standard values ---------------------------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
121
122
123
124
125
126
127
128
129
130
131

  if(FrSshort != 2)
     {FrError(3,"FrLibShortIni","Warning: sizeof(short) != 2 !!!!!");}
  if(FrSint != 4)
     {FrError(3,"FrLibShortIni","Warning: sizeof(int) != 4 !!!!!!!");}
  if(FrSlong != 4 && FrSlong != 8 )
     {FrError(3,"FrLibShortIni","Warning: sizeof(long) != 4 or 8 !");}
  if(FrSfloat != 4)
     {FrError(3,"FrLibShortIni","Warning: sizeof(float) != 4 !!!!!");}
  if(FrSdouble != 8)
     {FrError(3,"FrLibShortIni","Warning: sizeof(double) != 8 !!!!");}
Benoit Mours's avatar
v8r06    
Benoit Mours committed
132
133
  if(sizeof(long long) != 8)
     {FrError(3,"FrLibShortIni","Warning: sizeof(long long) != 8 !!!!!");}
teuler's avatar
teuler committed
134
135

                              /*----- declare the standard structures -------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
136
137
138
139
140
141
142
143
144

  if(FrObjectDef()     == NULL) return;
  if(FrSHDef()         == NULL) return;
  if(FrSEDef()         == NULL) return;

  if(FrameHDef()       == NULL) return;
  if(FrAdcDataDef()    == NULL) return;
  if(FrDetectorDef()   == NULL) return;
  if(FrEndOfFrameDef() == NULL) return;
teuler's avatar
teuler committed
145
  if(FrEventDef()      == NULL) return;
Christian Arnault's avatar
Import  
Christian Arnault committed
146
147
148
149
150
  if(FrMsgDef()        == NULL) return;
  if(FrHistoryDef()    == NULL) return;
  if(FrRawDataDef()    == NULL) return;
  if(FrProcDataDef()   == NULL) return;
  if(FrSimDataDef()    == NULL) return;
Christian Arnault's avatar
Import    
Christian Arnault committed
151
  if(FrSimEventDef()   == NULL) return;
Christian Arnault's avatar
Import  
Christian Arnault committed
152
153
154
  if(FrSerDataDef()    == NULL) return;
  if(FrStatDataDef()   == NULL) return;
  if(FrSummaryDef()    == NULL) return;
Christian Arnault's avatar
Import    
Christian Arnault committed
155
156
  if(FrTableDef()      == NULL) return;
  if(FrTOCDef()        == NULL) return;
Christian Arnault's avatar
Import  
Christian Arnault committed
157
158
159
160

  if(FrVectDef()       == NULL) return;
  
  if(FrEndOfFileDef()  == NULL) return;
161
 
Christian Arnault's avatar
Import  
Christian Arnault committed
162
163
  return;}

root's avatar
root committed
164
/*------------------------------------------------------------- FrLibSetLvl -*/
Christian Arnault's avatar
Import    
Christian Arnault committed
165
void FrLibSetLvl(int dbglvl)
root's avatar
root committed
166
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
167
168
169
170
171
172
{ 
  FrDebugLvl = dbglvl;
  if(FrFOut == NULL) FrFOut = stdout;
  
  return;}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-------------------------------------------------------FrLibSetFlipPrefix--*/
void FrLibSetFlipPrefix(char *prefix)
/*---------------------------------------------------------------------------*/
{ 
  FrStrCpy(&FrFlipPrefix, prefix);
  
  return;}

/*----------------------------------------------------------FrLibSetVersion--*/
void FrLibSetVersion(int formatVersion)
/*---------------------------------------------------------------------------*/
{ 
  FrFormatVersion = formatVersion;
  
  return;}

root's avatar
root committed
189
/*------------------------------------------------------------ FrLibVersion -*/
Christian Arnault's avatar
Import    
Christian Arnault committed
190
float FrLibVersion(FILE *fOut)
root's avatar
root committed
191
192
                              /*if fOut != NULL, we use print version on fOut*/
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
193
194
{float version;

195
 sscanf(FR_VERSION,"%f",&version);
root's avatar
root committed
196

197
198
 if(fOut != NULL) fprintf(fOut,"  FrameL Version:%s %s(Compiled: %s %s)\n",
	                       FR_VERSION, FR_PATH,__DATE__,__TIME__);
Christian Arnault's avatar
Import    
Christian Arnault committed
199
200
201
  
 return(version);}

swmgr's avatar
swmgr committed
202
203
204
205
206
/*----------------------------------------------------------- FrLibVersionF -*/
char *FrLibVersionF()
/*---------------------------------------------------------------------------*/
{static char version[256];

207
208
 snprintf(version,254,"Fr     Version:%s %s(Compiled: %s %s)\n",
                               FR_VERSION, FR_PATH,__DATE__,__TIME__);
swmgr's avatar
swmgr committed
209
210
211
212

 return(version);}


root's avatar
root committed
213
/*--------------------------------------------------------FrAdcDataCopy------*/
root's avatar
root committed
214
215
FrAdcData *FrAdcDataCopy(FrAdcData *in,
                         FrameH *frame)
root's avatar
root committed
216
/*---------------------------------------------------------------------------*/
root's avatar
root committed
217
{FrAdcData *out;
Christian Arnault's avatar
Import  
Christian Arnault committed
218
219

  if(in == NULL) return(NULL); 
root's avatar
root committed
220
  out = (FrAdcData *) calloc(1,sizeof(FrAdcData));
Christian Arnault's avatar
Import  
Christian Arnault committed
221
222
  if(out == NULL) {return(NULL);} 

root's avatar
root committed
223
  memcpy(out, in, sizeof(FrAdcData));
Christian Arnault's avatar
Import  
Christian Arnault committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
	 
  if(FrStrCpy(&out->name,   in->name)    == NULL) return(NULL);
  if(in->comment != NULL)
     {if(FrStrCpy(&out->comment,in->comment) == NULL) return(NULL);}
  if(in->units != NULL)
     {if(FrStrCpy(&out->units,  in->units)   == NULL) return(NULL);}
 
  if(in->data != NULL) 
     {out->data = FrVectCopy(in->data);
      if(out->data == NULL) return(NULL);}
  if(in->aux != NULL)
     {out->aux  = FrVectCopy(in->aux);
      if(out->aux == NULL) return(NULL);}

  out->next = NULL;
teuler's avatar
teuler committed
239
                           /*----- now store it in the Frame structures -----*/
Christian Arnault's avatar
Import  
Christian Arnault committed
240

241
  if(frame != NULL) FrameAddAdc(frame, out);
Christian Arnault's avatar
Import  
Christian Arnault committed
242
243
244
  
  return(out);} 

root's avatar
root committed
245
/*------------------------------------------------------- FrAdcDataDecimate--*/
root's avatar
root committed
246
247
int FrAdcDataDecimate(FrAdcData *adc,
                      int nGroup)
root's avatar
root committed
248
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
249
{int i, j, sum, group;
teuler's avatar
teuler committed
250
251
 float  *dataF;
 double *dataD, sumD, coef;
root's avatar
root committed
252
253
 short  *dataS;
 int    *dataI;
Christian Arnault's avatar
Import    
Christian Arnault committed
254
255
256
257

 if(adc == NULL) return(1);
 if(nGroup == 0) return(2);

teuler's avatar
teuler committed
258
259
260
 if(adc->data->compress != 0) FrVectExpand(adc->data);

              /*------ this version works only for short, int and float -----*/
Christian Arnault's avatar
Import    
Christian Arnault committed
261
262

 if((adc->data->type != FR_VECT_2S) &&
root's avatar
root committed
263
    (adc->data->type != FR_VECT_4S) &&
root's avatar
root committed
264
265
    (adc->data->type != FR_VECT_4R) &&
    (adc->data->type != FR_VECT_8R)) return(4);
Christian Arnault's avatar
Import    
Christian Arnault committed
266
267
268
269
270
271
272

 if(nGroup < 4)         {group = 0;}
 else if(nGroup < 16)   {group = 1;}
 else if(nGroup < 64)   {group = 2;}
 else if(nGroup < 256)  {group = 3;}
 else if(nGroup < 1024) {group = 4;}
 else                   {group = 5;}
root's avatar
root committed
273
274
 if (adc->data->type == FR_VECT_4R) group = 0;

teuler's avatar
teuler committed
275
              /*---- if ngroup is negative, we do not change the resolution--*/
Christian Arnault's avatar
Import    
Christian Arnault committed
276
277
278
279
280
281
282
283
284
285

 if(nGroup < 0) nGroup = -nGroup;
 if(adc->data->nData % nGroup != 0) return(3);

 coef = (1.+group)/nGroup;

 adc->slope       = adc->slope * (1+group);
 adc->nBits       = adc->nBits + group;
 adc->sampleRate  = adc->sampleRate  / nGroup;
 adc->data->nData = adc->data->nData / nGroup;
root's avatar
root committed
286
 adc->data->nx[0] = adc->data->nx[0] / nGroup;
Christian Arnault's avatar
Import    
Christian Arnault committed
287
 adc->data->dx[0] = adc->data->dx[0] * nGroup;
root's avatar
root committed
288
 adc->data->nBytes= adc->data->nBytes /nGroup;
Christian Arnault's avatar
Import    
Christian Arnault committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

 if(adc->data->type == FR_VECT_2S)
   {if(adc->nBits > 16) adc->data->type = FR_VECT_4S;

    dataS = adc->data->dataS;
    for(i=0; i < adc->data->nData; i++)
       {sum = 0.;
        for(j=0; j<nGroup; j++) {sum += dataS[j];}
        if(adc->nBits < 17)
             {adc->data->dataS[i] = sum*coef;}
        else {adc->data->dataI[i] = sum*coef;}
        dataS = dataS + nGroup;}}

 else if(adc->data->type == FR_VECT_4S)
   {dataI = adc->data->dataI;
    for(i=0; i < adc->data->nData; i++)
       {sum = 0.;
        for(j=0; j<nGroup; j++) {sum += dataI[j];}
        adc->data->dataI[i] = sum*coef;
        dataI = dataI + nGroup;}}
    
root's avatar
root committed
310
311
312
 else if(adc->data->type == FR_VECT_4R)
   {dataF = adc->data->dataF;
    for(i=0; i < adc->data->nData; i++)
teuler's avatar
teuler committed
313
314
315
       {sumD = 0.;
        for(j=0; j<nGroup; j++) {sumD += dataF[j];}
        adc->data->dataF[i] = sumD*coef;
root's avatar
root committed
316
317
        dataF = dataF + nGroup;}}
    
root's avatar
root committed
318
319
320
321
322
323
324
 else if(adc->data->type == FR_VECT_8R)
   {dataD = adc->data->dataD;
    for(i=0; i < adc->data->nData; i++)
       {sumD = 0.;
        for(j=0; j<nGroup; j++) {sumD += dataD[j];}
        adc->data->dataD[i] = sumD*coef;
        dataD = dataD + nGroup;}}
teuler's avatar
teuler committed
325

326
 adc->data->data = realloc(adc->data->data, adc->data->nBytes);
teuler's avatar
teuler committed
327
 FrVectMap(adc->data);
root's avatar
root committed
328
    
Christian Arnault's avatar
Import    
Christian Arnault committed
329
330
 return(0);}

root's avatar
root committed
331
/*------------------------------------------------------------ FrAdcDataDef--*/
root's avatar
root committed
332
FrSH *FrAdcDataDef()
root's avatar
root committed
333
/*---------------------------------------------------------------------------*/
root's avatar
root committed
334
{static FrSH *classe = NULL;
Christian Arnault's avatar
Import  
Christian Arnault committed
335
336

  if(classe != NULL) return(classe);
337
  classe = FrDicAddS("FrAdcData", (void (*)(void))FrAdcDataRead);
Christian Arnault's avatar
Import  
Christian Arnault committed
338
339
340

  FrSENew(classe, "STRING", "name","-");
  FrSENew(classe, "STRING", "comment","-");
Christian Arnault's avatar
Import    
Christian Arnault committed
341
342
  FrSENew(classe, "INT_4U", "channelGroup","-");
  FrSENew(classe, "INT_4U", "channelNumber","-");
Christian Arnault's avatar
Import  
Christian Arnault committed
343
344
345
346
347
  FrSENew(classe, "INT_4U", "nBits","-");
  FrSENew(classe, "REAL_4", "bias","-");
  FrSENew(classe, "REAL_4", "slope","-");
  FrSENew(classe, "STRING", "units","-");
  FrSENew(classe, "REAL_8", "sampleRate","-");
teuler's avatar
teuler committed
348
349
350
351
  FrSENew(classe, "REAL_8", "timeOffset","-");
  FrSENew(classe, "REAL_8", "fShift","-");
  FrSENew(classe, "REAL_4", "phase","-");
  FrSENew(classe, "INT_2U", "dataValid","-");
Christian Arnault's avatar
Import  
Christian Arnault committed
352
  FrSENew(classe, "PTR_STRUCT(FrVect *)",   "data","-");
Christian Arnault's avatar
Import    
Christian Arnault committed
353
354
  FrSENew(classe, "PTR_STRUCT(FrVect *)",   "aux","-");
  FrSENew(classe, "PTR_STRUCT(FrAdcData *)", "next","-"); 
355
  if(FrFormatVersion >= 8) FrSENew(classe, "INT_4U", "chkSum","-");
Christian Arnault's avatar
Import  
Christian Arnault committed
356
357
358

  return(classe);}
  
root's avatar
root committed
359
/*----------------------------------------------------------- FrAdcDataDump--*/
root's avatar
root committed
360
void FrAdcDataDump(FrAdcData *adc, 
Christian Arnault's avatar
Import    
Christian Arnault committed
361
362
                   FILE *fp, 
                   int debugLvl)
root's avatar
root committed
363
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
364
{
root's avatar
root committed
365
366
 if(adc == NULL) return;

teuler's avatar
teuler committed
367
 fprintf(fp," ADC:    %s channel(G/N):%d-%d nBits=%d bias=%g slope=%g", 
Christian Arnault's avatar
Import    
Christian Arnault committed
368
     adc->name, adc->channelGroup, adc->channelNumber, 
teuler's avatar
teuler committed
369
     adc->nBits, adc->bias, adc->slope);
370
371
372
373
 if(adc->units != NULL) fprintf(fp," units=%s",adc->units);
 if(adc->fShift != 0 || adc->phase != 0)   
   fprintf(fp," fShift=%g phase=%g",adc->fShift, adc->phase);
 fprintf(fp,"\n");
root's avatar
root committed
374

teuler's avatar
teuler committed
375
 if(adc->dataValid != 0)
376
377
  {fprintf(fp,"   DataValid=%x (",adc->dataValid);
   if((adc->dataValid & 1) != 0)fprintf(fp,"Non valid floating point;");
Benoit Mours's avatar
Benoit Mours committed
378
   if((adc->dataValid & 2) != 0)fprintf(fp,"Missing data at known position;");
379
380
381
382
383
   if((adc->dataValid & 4) != 0)fprintf(fp,"Missing data at unknown position");
   if((adc->dataValid & 8) != 0)fprintf(fp,"Hardware parity error;");
   if((adc->dataValid & 16)!= 0)fprintf(fp,"Too slow DAQ (like FIFO full)");
   if((adc->dataValid & 32)!= 0)fprintf(fp,"Invalid front end format");
   printf(")\n");}
teuler's avatar
teuler committed
384

root's avatar
root committed
385
386
 if(adc->comment != NULL) 
   {if(strlen(adc->comment) > 2) fprintf(fp,"   comment:%s\n", adc->comment);}
Christian Arnault's avatar
Import    
Christian Arnault committed
387
388
389
    
 FrVectDump( adc->data,  fp, debugLvl);

390
391
392
393
 if(adc->aux != NULL) 
   {fprintf(fp,"  Auxiliary");
    FrVectDump(adc->aux, fp, debugLvl);}

Christian Arnault's avatar
Import    
Christian Arnault committed
394
 return;}
swmgr's avatar
swmgr committed
395

root's avatar
root committed
396
397
398
399
400
401
/*------------------------------------------------------------FrAdcDataFind--*/
FrAdcData *FrAdcDataFind(FrameH *frame, 
                         char *adcName)
/*---------------------------------------------------------------------------*/
/* Return a pointer to the adc structure or NULL if not found                */
{FrAdcData *adc;
Christian Arnault's avatar
Import  
Christian Arnault committed
402

root's avatar
root committed
403
404
405
 if(frame == NULL)             return (NULL);
 if(frame->rawData == NULL)    return (NULL);
 if(adcName == NULL)           return (NULL);
Christian Arnault's avatar
Import  
Christian Arnault committed
406

407
408
 adc = (FrAdcData*) FrameFindBasic((FrBasic*) frame->rawData->firstAdc, adcName);
 if(adc == NULL) return(NULL);
root's avatar
root committed
409

410
411
 if(adc->data->compress != 0) FrVectExpandF(adc->data);
 if(adc->aux != NULL)         FrVectExpandF(adc->aux);
root's avatar
root committed
412

413
414
415
416
417
418
 adc->data->GTime     = frame->GTimeS +
                        frame->GTimeN * 1.e-9 + adc->timeOffset;
 adc->data->ULeapS    = frame->ULeapS;
 adc->data->localTime = FrameGetLTOffset(frame,adcName);
 
 return (adc);}
Christian Arnault's avatar
Import  
Christian Arnault committed
419

root's avatar
root committed
420
/*------------------------------------------------------------FrAdcDataFree--*/
root's avatar
root committed
421
void FrAdcDataFree(FrAdcData *adcData)
root's avatar
root committed
422
/*---------------------------------------------------------------------------*/
root's avatar
root committed
423
{
daq's avatar
v8r07    
daq committed
424
425
426
427
428
429
430
431
432
433
434
435
 while(adcData != NULL) {adcData = FrAdcDataFreeOne(adcData);}

 return;}

/*---------------------------------------------------------FrAdcDataFreeOne--*/
FrAdcData* FrAdcDataFreeOne(FrAdcData *adcData)
/*---------------------------------------------------------------------------*/
{
 FrAdcData* next;

 if(adcData == NULL) return(NULL);
 next = adcData->next;
Christian Arnault's avatar
Import  
Christian Arnault committed
436
437
438

 if(adcData->name    != NULL) free (adcData->name);
 if(adcData->comment != NULL) free (adcData->comment);
Christian Arnault's avatar
Import    
Christian Arnault committed
439
 if(adcData->units   != NULL) free (adcData->units);
root's avatar
root committed
440
441
 if(adcData->data    != NULL) FrVectFree (adcData->data);
 if(adcData->aux     != NULL) FrVectFree (adcData->aux);
Christian Arnault's avatar
Import  
Christian Arnault committed
442
443
444

 free(adcData);

daq's avatar
v8r07    
daq committed
445
 return(next);}
Christian Arnault's avatar
Import  
Christian Arnault committed
446

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*---------------------------------------------------------FrAdcDataGetSize--*/
FRLONG FrAdcDataGetSize(FrAdcData *adc)
/*---------------------------------------------------------------------------*/
{FRLONG size;

 if(adc == NULL) return(0);

 size = 69;
 if(adc->name    != NULL) size += strlen(adc->name);
 if(adc->comment != NULL) size += strlen(adc->comment);
 if(adc->units   != NULL) size += strlen(adc->units);
 size += FrVectGetTotSize(adc->data);
 size += FrVectGetTotSize(adc->aux);

 return(size);}

463
464
/*--------------------------------WARNING:OBSOLETE FUNCTION---FrAdcDataGetV--*/
FrVect *FrAdcDataGetV(FrameH *frame, char *name)
root's avatar
root committed
465
/*---------------------------------------------------------------------------*/
466
{return(FrameFindAdcVect(frame,name));}
root's avatar
root committed
467

root's avatar
root committed
468
469
470
471
/*-------------------------------------------------------------FrAdcDataNew--*/
FrAdcData *FrAdcDataNew(FrameH *frame,  
                        char *name, 
                        double sampleRate, 
teuler's avatar
teuler committed
472
                        FRLONG nData, 
root's avatar
root committed
473
474
                        int nBits)
/*---------------------------------------------------------------------------*/
teuler's avatar
teuler committed
475
{FrAdcData *adcData;
Christian Arnault's avatar
Import    
Christian Arnault committed
476
477

 adcData = FrAdcDataNewF(frame, name, NULL, 0, 0,
478
                          nBits, 0, 1., NULL, sampleRate, nData); 
Christian Arnault's avatar
Import    
Christian Arnault committed
479
480
481
  
 return(adcData);} 

root's avatar
root committed
482
483
484
485
486
487
488
489
490
491
492
/*------------------------------------------------------------FrAdcDataNewF--*/
FrAdcData *FrAdcDataNewF(FrameH *frame,  
                         char *name,
                         char *comment,
                         unsigned int channelGroup,
                         unsigned int channelNumber, 
                         int nBits,
                         float bias,
                         float slope,
                         char *units,
                         double sampleRate, 
teuler's avatar
teuler committed
493
                         FRLONG nData)
root's avatar
root committed
494
/*---------------------------------------------------------------------------*/
teuler's avatar
teuler committed
495
{FrAdcData *adcData;
Christian Arnault's avatar
Import  
Christian Arnault committed
496
 double dx;
Christian Arnault's avatar
Import    
Christian Arnault committed
497
 int type;
swmgr's avatar
swmgr committed
498
 char *aUnits = "Counts.";
Christian Arnault's avatar
Import  
Christian Arnault committed
499
 
teuler's avatar
teuler committed
500
501
502
  adcData = (FrAdcData *) calloc(1,sizeof(FrAdcData));
  if(adcData == NULL)   return(NULL);
  adcData->classe = FrAdcDataDef();
Christian Arnault's avatar
Import  
Christian Arnault committed
503
	 
swmgr's avatar
swmgr committed
504
  if(units == NULL) units = aUnits;
Christian Arnault's avatar
Import    
Christian Arnault committed
505
506
507
  if(FrStrCpy(&adcData->name,name)       == NULL) return(NULL);
  if(FrStrCpy(&adcData->comment,comment) == NULL) return(NULL);
  if(FrStrCpy(&adcData->units,units)     == NULL) return(NULL);
Christian Arnault's avatar
Import  
Christian Arnault committed
508

Christian Arnault's avatar
Import    
Christian Arnault committed
509
510
  adcData->channelGroup  = channelGroup;
  adcData->channelNumber = channelNumber;
root's avatar
root committed
511
512
  if(nBits > 0) adcData->nBits =   nBits;
  else          adcData->nBits =  -nBits;
Christian Arnault's avatar
Import    
Christian Arnault committed
513
514
515
  adcData->bias  = bias;
  adcData->slope = slope;
  adcData->sampleRate = sampleRate;
teuler's avatar
teuler committed
516
517
518
519
520
  adcData->timeOffset = 0;
  adcData->fShift     = 0;
  adcData->phase      = 0;
  adcData->dataValid  = 0;
  adcData->aux        = NULL;
Christian Arnault's avatar
Import  
Christian Arnault committed
521
522
523
524

  if(nBits >16)       {type = FR_VECT_4S;}
  else if(nBits >  8) {type = FR_VECT_2S;}
  else if(nBits >  0) {type = FR_VECT_C;}
Christian Arnault's avatar
Import    
Christian Arnault committed
525
  else if(nBits >-33) {type = FR_VECT_4R;}
Christian Arnault's avatar
Import    
Christian Arnault committed
526
  else                {type = FR_VECT_8R;}
Christian Arnault's avatar
Import  
Christian Arnault committed
527
528
529
530
531

  if(sampleRate == 0.)
       {dx = 0.;}
  else {dx = 1./sampleRate;}

swmgr's avatar
swmgr committed
532
  adcData->data = FrVectNew1D(name, -type, nData,dx, "Time (sec)", units);
Christian Arnault's avatar
Import  
Christian Arnault committed
533
  if(adcData->data == NULL) return(NULL);
Christian Arnault's avatar
Import    
Christian Arnault committed
534
  
535
  if(frame != NULL) FrameAddAdc(frame, adcData);
Christian Arnault's avatar
Import  
Christian Arnault committed
536
537
538
  
  return(adcData);} 

teuler's avatar
teuler committed
539
540
541
542
543
/*------------------------------------------------------------FrAdcDataRead--*/
FrAdcData *FrAdcDataRead(FrFile *iFile)
/*---------------------------------------------------------------------------*/
{FrAdcData *adcData;
 unsigned int timeOffsetS, timeOffsetN;
swmgr's avatar
swmgr committed
544
 char message[128];
Christian Arnault's avatar
Import  
Christian Arnault committed
545

teuler's avatar
teuler committed
546
  adcData = (FrAdcData *) calloc(1,sizeof(FrAdcData));
Christian Arnault's avatar
Import  
Christian Arnault committed
547
548
  if(adcData == NULL) 
       {iFile->error = FR_ERROR_MALLOC_FAILED;
root's avatar
root committed
549
        return(NULL);}
teuler's avatar
teuler committed
550
  adcData->classe = FrAdcDataDef();
Christian Arnault's avatar
Import  
Christian Arnault committed
551
552
553
554

  FrReadHeader(iFile,  adcData);
  FrReadSChar (iFile, &adcData->name); 
  FrReadSChar (iFile, &adcData->comment);
Christian Arnault's avatar
Import    
Christian Arnault committed
555
556
  FrReadIntU  (iFile, &adcData->channelGroup);
  FrReadIntU  (iFile, &adcData->channelNumber);
Christian Arnault's avatar
Import  
Christian Arnault committed
557
558
559
560
561
  FrReadIntU  (iFile, &adcData->nBits);
  FrReadFloat (iFile, &adcData->bias);
  FrReadFloat (iFile, &adcData->slope);
  FrReadSChar (iFile, &adcData->units);
  FrReadDouble(iFile, &adcData->sampleRate);
teuler's avatar
teuler committed
562
563
564
565
566
  if(iFile->fmtVersion > 5)
       {FrReadDouble(iFile, &adcData->timeOffset);}
  else {FrReadIntU  (iFile, &timeOffsetS);
        FrReadIntU  (iFile, &timeOffsetN);
        adcData->timeOffset = timeOffsetS + 1.e-9*timeOffsetN;}
Christian Arnault's avatar
Import  
Christian Arnault committed
567
  FrReadDouble(iFile, &adcData->fShift);
teuler's avatar
teuler committed
568
569
  if(iFile->fmtVersion > 4)
       {FrReadFloat (iFile, &adcData->phase);}
Christian Arnault's avatar
Import    
Christian Arnault committed
570
  FrReadShortU(iFile, &adcData->dataValid);
Christian Arnault's avatar
Import  
Christian Arnault committed
571
  FrReadStruct(iFile, &adcData->data);
teuler's avatar
teuler committed
572
  iFile->vectInstance = iFile->instance;
Christian Arnault's avatar
Import  
Christian Arnault committed
573
574
  FrReadStruct(iFile, &adcData->aux);
  FrReadStruct(iFile, &adcData->next);
575
  if(iFile->fmtVersion >=8) FrReadStructChksum(iFile);
Christian Arnault's avatar
Import  
Christian Arnault committed
576

swmgr's avatar
swmgr committed
577
  if(iFile->nBytes != iFile->nBytesR + iFile->length)
578
579
580
     {sprintf(message,": Record length error: "
         "nBytes=%"FRLLD" nBytesR=%"FRLLD" length=%"FRLLD"\n",
          iFile->nBytes, iFile->nBytesR, iFile->length);
swmgr's avatar
swmgr committed
581
582
      FrError(3,"FrAdcDataRead",message);
      return(NULL);}     
583

swmgr's avatar
swmgr committed
584
585
  if(FrDebugLvl > 2) fprintf(FrFOut," %s\n", adcData->name);
  
root's avatar
root committed
586
587
return(adcData);}

teuler's avatar
teuler committed
588
589
590
591
592
593
594
595
/*---------------------------------------------------------- FrAdcDataReadI--*/
FrAdcData *FrAdcDataReadI(FrFile *iFile,
                          char *name,
                          int index)
/*---------------------------------------------------------------------------*/
{FrAdcData *adc;
 FrTOCts *ts;
 FrTOC *toc;
teuler's avatar
teuler committed
596
 double gtime;
Christian Arnault's avatar
Import    
Christian Arnault committed
597

root's avatar
root committed
598
599
600
601
 toc = FrTOCReadFull(iFile);
 if(toc == NULL)          return(NULL);
 if(index == -1)          return(NULL);
 if(index >= toc->nFrame) return(NULL);
Christian Arnault's avatar
Import    
Christian Arnault committed
602

teuler's avatar
teuler committed
603
                                /*------ find TOC info for this ADC ---------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
604

root's avatar
root committed
605
606
 for(ts = toc->adc; ts != NULL; ts = ts->next)
   {if(strcmp(name, ts->name) == 0)  break;}
Christian Arnault's avatar
Import    
Christian Arnault committed
607
608
 if(ts == NULL) return(NULL); 

teuler's avatar
teuler committed
609
                        /*---- set the file pointer and read the data -------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
610

root's avatar
root committed
611
 if(FrTOCSetPos(iFile, ts->position[index]) != 0) {return (NULL);}
root's avatar
root committed
612
613
 adc = FrAdcDataRead(iFile);
 if(adc == NULL) return(NULL);
Christian Arnault's avatar
Import    
Christian Arnault committed
614

teuler's avatar
teuler committed
615
616
 gtime = toc->GTimeS[index] + 1.e-9 *toc->GTimeN[index] + adc->timeOffset;
 adc->data = FrVectReadNext(iFile, gtime, adc->name);
Christian Arnault's avatar
Import    
Christian Arnault committed
617

root's avatar
root committed
618
 return(adc);}
Christian Arnault's avatar
Import    
Christian Arnault committed
619

root's avatar
root committed
620
621
622
623
624
/*---------------------------------------------------------- FrAdcDataReadT--*/
FrAdcData *FrAdcDataReadT(FrFile *iFile,
                          char *name,
                          double gtime)
/*---------------------------------------------------------------------------*/
root's avatar
root committed
625
{FrAdcData  **current, *adc, *root;
root's avatar
root committed
626
 FrTOCts *ts;
root's avatar
root committed
627
628
629
 FrTag *frtag;
 int index;
          /*------- find frame(it will read the FrTOC if needed) ------------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
630

root's avatar
root committed
631
632
 index = FrTOCFrameFindT(iFile, gtime);
 if(index <0) return(NULL);
Christian Arnault's avatar
Import    
Christian Arnault committed
633

root's avatar
root committed
634
635
636
637
638
 if(name == NULL) return(NULL);              /*----- build tag object -------*/
 frtag = FrTagNew(name);
 if(frtag == NULL) return(NULL);
                                   
 root = NULL;                           /*------ find the ADC in the TOC ----*/
root's avatar
root committed
639
640
 current = &root;

root's avatar
root committed
641
 for(ts = iFile->toc->adc; ts != NULL; ts = ts->next)
root's avatar
root committed
642

root's avatar
root committed
643
                          /*-------- check if we need to copy this adc ?-----*/
root's avatar
root committed
644

root's avatar
root committed
645
   {if(FrTagMatch(frtag, ts->name) == FR_NO) continue; 
root's avatar
root committed
646

root's avatar
root committed
647
                       /*---- set the file pointer and read the data -------*/
Christian Arnault's avatar
Import    
Christian Arnault committed
648

teuler's avatar
teuler committed
649
    if(FrTOCSetPos(iFile, ts->position[index]) != 0) continue;
root's avatar
root committed
650
    adc = FrAdcDataRead(iFile);
teuler's avatar
teuler committed
651
    if(adc == NULL) continue;
swmgr's avatar
swmgr committed
652
653
654
655
656
                                 /*--- check that we read the right ADC-----*/

    if(FrTagMatch(frtag, adc->name) == FR_NO) 
        {FrError(3,"FrAdcDataReadT","name missmatch");
         return(NULL);}
Christian Arnault's avatar
Import    
Christian Arnault committed
657

teuler's avatar
teuler committed
658
659
660
    gtime = iFile->toc->GTimeS[index] + 1.e-9 *
            iFile->toc->GTimeN[index] + adc->timeOffset;
    adc->data = FrVectReadNext(iFile, gtime, adc->name);
root's avatar
root committed
661

root's avatar
root committed
662
663
    *current = adc;
    current = &(adc->next);}
root's avatar
root committed
664
665
666

 FrTagFree(frtag); 

root's avatar
root committed
667
 return(root);}
Christian Arnault's avatar
Import    
Christian Arnault committed
668

swmgr's avatar
swmgr committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*--------------------------------------------------------- FrAdcDataSetAux--*/
void FrAdcDataSetAux(FrAdcData *adc, 
                     FrVect *aux)
/*---------------------------------------------------------------------------*/
{
 if(adc == NULL) return;
 if(aux == NULL) return;

 aux->next = adc->aux;
 adc->aux = aux;

 return;}

/*--------------------------------------------------- FrAdcDataSetDataValid--*/
void FrAdcDataSetDataValid(FrAdcData *adc, 
                           unsigned short dataValid)
/*---------------------------------------------------------------------------*/
{
 if(adc != NULL) adc->dataValid = dataValid;

 return;}

/*----------------------------------------------------- FrAdcDataSetTOffset--*/
void FrAdcDataSetFShift(FrAdcData *adc, 
                        double fShift,
                        float phase)
/*---------------------------------------------------------------------------*/
{
 if(adc == NULL) return;

 adc->fShift = fShift;
 adc->phase  = phase;

 return;}

/*----------------------------------------------------- FrAdcDataSetTOffset--*/
void FrAdcDataSetTOffset(FrAdcData *adc, 
                         double timeOffset)
/*---------------------------------------------------------------------------*/
{
 if(adc != NULL) adc->timeOffset = timeOffset;

 return;}

teuler's avatar
teuler committed
713
714
715
716
/*-----------------------------------------------------------FrAdcDataWrite--*/
void FrAdcDataWrite(FrAdcData *adcData,
                    FrFile *oFile)
/*---------------------------------------------------------------------------*/
Christian Arnault's avatar
Import  
Christian Arnault committed
717
{
root's avatar
root committed
718
  FrPutNewRecord(oFile, adcData, FR_YES);
Christian Arnault's avatar
Import  
Christian Arnault committed
719
720
  FrPutSChar (oFile, adcData->name);
  FrPutSChar (oFile, adcData->comment);
Christian Arnault's avatar
Import    
Christian Arnault committed
721
722
  FrPutIntU  (oFile, adcData->channelGroup);
  FrPutIntU  (oFile, adcData->channelNumber);
Christian Arnault's avatar
Import  
Christian Arnault committed
723
  FrPutIntU  (oFile, adcData->nBits);
Christian Arnault's avatar
Import    
Christian Arnault committed
724
725
  FrPutFloat (oFile, adcData->bias);
  FrPutFloat (oFile, adcData->slope);
Christian Arnault's avatar
Import  
Christian Arnault committed
726
727
  FrPutSChar (oFile, adcData->units);
  FrPutDouble(oFile, adcData->sampleRate);
teuler's avatar
teuler committed
728
  FrPutDouble(oFile, adcData->timeOffset);
Christian Arnault's avatar
Import  
Christian Arnault committed
729
  FrPutDouble(oFile, adcData->fShift);
teuler's avatar
teuler committed
730
  FrPutFloat (oFile, adcData->phase);
Christian Arnault's avatar
Import    
Christian Arnault committed
731
  FrPutShortU(oFile, adcData->dataValid);
Christian Arnault's avatar
Import  
Christian Arnault committed
732
733
734
  FrPutStruct(oFile, adcData->data);
  FrPutStruct(oFile, adcData->aux);
  FrPutStruct(oFile, adcData->next);
root's avatar
root committed
735

root's avatar
root committed
736
  if(oFile->toc != NULL) FrTOCtsMark(oFile, &oFile->toc->adcH, 
root's avatar
root committed
737
          adcData->name, adcData->channelGroup, adcData->channelNumber);
root's avatar
root committed
738

739
  FrPutWriteRecord(oFile, FR_NO);
Christian Arnault's avatar
Import  
Christian Arnault committed
740
741
742
743
744
745
746

  FrVectWrite(adcData->data, oFile);

  if(adcData->aux != NULL) FrVectWrite(adcData->aux, oFile);
   
 return;}

747
748
/*--------------------------------------------------------------FrameAddAdc--*/
void FrameAddAdc(FrameH *frame,  FrAdcData* adc)
swmgr's avatar
swmgr committed
749
/*---------------------------------------------------------------------------*/
750
751
752
753
754
{
  FrRawData *rawData;
 
  if(frame == NULL) return;
  if(adc   == NULL) return;
swmgr's avatar
swmgr committed
755

756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
  if(frame->rawData == NULL)  FrRawDataNew(frame);
  rawData = frame->rawData;
  if(rawData == NULL) return;

  adc->next = rawData->firstAdc;
  rawData->firstAdc = adc;
 
  if(rawData->firstAdcOld != NULL) {    /*---------- if tagging is used---*/
    adc->nextOld = rawData->firstAdcOld;
    rawData->firstAdcOld = adc;}
  
  return;
} 
/*------------------------------------------------------------FrameAddEvent--*/
void FrameAddEvent(FrameH *frame,  FrEvent* event)
/*---------------------------------------------------------------------------*/
{
773
774
  FrEvent **last;

swmgr's avatar
swmgr committed
775
  if(frame == NULL) return;
776
  if(event == NULL) return;
swmgr's avatar
swmgr committed
777

778
779
780
781
782
783
  /*-------------------------------add event at the end of the linked list---*/
  last = &frame->event;
  while((*last) != NULL) {last = &((*last)->next);}
  (*last) = event;

  if(frame->eventOld != NULL) {/*if tagging is used the order does not matter*/
784
785
    event->nextOld = frame->eventOld;
    frame->eventOld = event;}
786

787
788
  return;
} 
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*--------------------------------------------------------FrameAddEventFast--*/
void FrameAddEventFast(FrameH *frame,  FrEvent* event)
/*---------------------------------------------------------------------------*/
{
  if(frame == NULL) return;
  if(event == NULL) return;

  /*-------------------------------add event at the end of the linked list---*/
  event->next = frame->event;
  frame->event = event; 

  if(frame->eventOld != NULL) {/*if tagging is used the order does not matter*/
    event->nextOld = frame->eventOld;
    frame->eventOld = event;}

  return;
} 
806
807
808
809
810
811
/*-------------------------------------------------------------FrameAddProc--*/
void FrameAddProc(FrameH *frame,  FrProcData* proc)
/*---------------------------------------------------------------------------*/
{
  if(frame == NULL) return;
  if(proc  == NULL) return;
swmgr's avatar
swmgr committed
812

813
814
815
816
817
818
819
820
821
822
823
  proc->next = frame->procData;
  frame->procData = proc;
 
  if(frame->procDataOld != NULL) {    /*---------- if tagging is used---*/
    proc->nextOld = frame->procDataOld;
    frame->procDataOld = proc;}
  
  return;
} 
/*--------------------------------------------------------------FrameAddSer--*/
void FrameAddSer(FrameH *frame,  FrSerData* ser)
swmgr's avatar
swmgr committed
824
/*---------------------------------------------------------------------------*/
825
826
827
828
829
{
  FrRawData *rawData;
 
  if(frame == NULL) return;
  if(ser   == NULL) return;
830

831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
  if(frame->rawData == NULL)  FrRawDataNew(frame);
  rawData = frame->rawData;
  if(rawData == NULL) return;

  ser->next = rawData->firstSer;
  rawData->firstSer = ser;
 
  if(rawData->firstSerOld != NULL) {    /*---------- if tagging is used---*/
    ser->nextOld = rawData->firstSerOld;
    rawData->firstSerOld = ser;}
  
  return;
} 
/*----------------------------------------------------------FrameAddSimData--*/
void FrameAddSimData(FrameH *frame,  FrSimData* sim)
/*---------------------------------------------------------------------------*/
{
swmgr's avatar
swmgr committed
848
  if(frame == NULL) return;
849
  if(sim   == NULL) return;
850

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  sim->next = frame->simData;
  frame->simData = sim;
 
  if(frame->simDataOld != NULL) {    /*---------- if tagging is used---*/
    sim->nextOld = frame->simDataOld;
    frame->simDataOld = sim;}
  
  return;
} 
/*---------------------------------------------------------FrameAddSimEvent--*/
void FrameAddSimEvent(FrameH *frame,  FrSimEvent* event)
/*---------------------------------------------------------------------------*/
{
  if(frame == NULL) return;
  if(event == NULL) return;
swmgr's avatar
swmgr committed
866

867
868
869
870
871
872
873
874
875
  event->next = frame->simEvent;
  frame->simEvent = event;
 
  if(frame->simEventOld != NULL) {    /*---------- if tagging is used---*/
    event->nextOld = frame->simEventOld;
    frame->simEventOld = event;}
  
  return;
} 
swmgr's avatar
swmgr committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/*---------------------------------------------------------------------------*/
int FrameAddStatData(FrameH* frame,
                      char* detectorName,
                      FrStatData *stat)
/*---------------------------------------------------------------------------*/
/* Attached a static data to a detector structure belonging to this frame.   */
/* If no detector exist for this name, a new one is created.                 */
/* If name is NULL, it is attached to the first detector or to a new detector*/
/* called "Default" is there is no detector structure.                       */
/* Any new detector structure is attached to the frame->detectProc list.     */
/*---------------------------------------------------------------------------*/
{FrDetector *detector;

 if(frame == NULL) return(1);
 if(stat  == NULL) return(2);

 if(detectorName == NULL) detector = frame->detectProc;
 else                     detector = FrameFindDetector(frame, detectorName);

 if(detector == NULL)
   {if(detectorName == NULL) detector = FrDetectorNew("Dummy");
    else                     detector = FrDetectorNew(detectorName);
    if(detector == NULL) return(3);
899
    detector->next = frame->detectProc;
swmgr's avatar
swmgr committed
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
    frame->detectProc = detector;}

 FrDetectorAddStatData(detector, stat);
 
 return(0);}
/*---------------------------------------------------------------------------*/
FrStatData* FrameAddStatVector(FrameH* frame,
                       char* detectorName,
                       char* statDataName,
		       unsigned int tStart,
		       unsigned int tEnd,
		       unsigned int version,
                       FrVect* vect)
/*---------------------------------------------------------------------------*/
/* Attached a vector to a static data to a detector structure belonging to this frame.   */
/* If no detector exist for this name, a new one is created.                 */
/* If name is NULL, it is attached to the first detector or to a new detector*/
/* called "Default" is there is no detector structure.                       */
/* Any new detector structure is attached to the frame->detectProc list.     */
/*---------------------------------------------------------------------------*/
{FrStatData *sData;

 if(frame == NULL) return(NULL);
 if(vect  == NULL) return(NULL);

 sData = FrameFindStatData(frame, detectorName, statDataName, (tStart+tEnd)/2);
 
 if(sData == NULL)
   {sData = FrStatDataNew(statDataName, NULL, NULL, 
                         tStart, tEnd, version, NULL, NULL); 
    FrameAddStatData(frame, detectorName, sData);}

 FrStatDataAddVect(sData, vect);

 return(sData);}
935
936
937
938
939
940
/*--------------------------------------------------------------FrameAddSum--*/
void FrameAddSum(FrameH *frame,  FrSummary* sum)
/*---------------------------------------------------------------------------*/
{
  if(frame == NULL) return;
  if(sum   == NULL) return;
root's avatar
root committed
941

942
943
944
945
946
947
948
949
950
  sum->next = frame->summaryData;
  frame->summaryData = sum;
 
  if(frame->summaryDataOld != NULL) {    /*---------- if tagging is used---*/
    sum->nextOld = frame->summaryDataOld;
    frame->summaryDataOld = sum;}
  
  return;
} 
Benoit Mours's avatar
Benoit Mours committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/*------------------------------------------------------------FrameBufCheck--*/
int FrameBufCheck(char *buf, FRLONG nBytes, FRBOOL chkChkSum)
/*---------------------------------------------------------------------------*/
/* This function check a frame buffer buf of size nBytes.
 * If the checksum is available and if chkChkSum == FR_YES, it performs the
 * file checksum. Otherwise, it just check that the number of bytes in the
 * end of file is consistreant with the provided nBytes value.
 * it returns the number of frame (from the end of frame) or a negative
 * error code.                                                               */
/*---------------------------------------------------------------------------*/
{
  FrFile *iFile;
  unsigned int nFrames, instance, chkSumFi,chkSumFrHeader;
  FRLONG nBytesEOF, seekTOC;

 if(buf == NULL) return(-1);
 if(nBytes <= 0) return(-2);

 iFile = FrFileNew(NULL, -1, buf, nBytes);
 if(iFile == NULL) return (-3);

  /*-------------------------- ask to check the file checksum if available---*/
  if(chkChkSum == FR_YES && buf[39] == 1)
        iFile->chkSumFiFlag = FR_YES;
  else  iFile->chkSumFiFlag = FR_NO;
  iFile->chkSumFrFlag = FR_NO;

  FrFileIOpen(iFile);
  if(iFile->error != FR_OK) {
    FrFileIEnd(iFile);
    return(-4);}

  /*--------------------------------------------skip the bulk of the frame---*/
  int nJump = nBytes-36-40;
  iFile->p      += nJump;
  iFile->nBytes += nJump;
987
  if(iFile->chkSumFiFlag == FR_YES) FrCksumGnu(buf+40 , nJump, &(iFile->chkSumFi));
Benoit Mours's avatar
Benoit Mours committed
988
989
990
991
992
993
994
995
996
997

  /*------------------------------------------------------- read EndOfFile---*/
  FrReadIntU(iFile, &instance);
  FrReadIntU(iFile, &nFrames);
  FrReadLong(iFile, &nBytesEOF);
  if(nBytes != nBytesEOF) {
    FrFileIEnd(iFile);
    return(-5);}

  /*--------------------------------------------------------checkSum check---*/
998
999
1000
  if(iFile->chkSumFiFlag != FR_YES) {
    FrFileIEnd(iFile);
    return(nFrames);}
For faster browsing, not all history is shown. View entire blame