Debian Codegraph @

biosig4c++/t210/sopen_heka_read.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2008-2013,2018 Alois Schloegl <[email protected]>
4 This file is part of the "BioSig for C/C++" repository
5 (biosig4c++) at http://biosig.sf.net/
6
7 BioSig is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 3
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 */
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #ifdef _WIN32
29 // Can't include sys/stat.h or sopen is declared twice.
30 #include <sys/types.h>
31 struct stat {
32 _dev_t st_dev;
33 _ino_t st_ino;
34 unsigned short st_mode;
35 short st_nlink;
36 short st_uid;
37 short st_gid;
38 _dev_t st_rdev;
39 _off_t st_size;
40 time_t st_atime;
41 time_t st_mtime;
42 time_t st_ctime;
43 };
44 int __cdecl stat(const char *_Filename,struct stat *_Stat);
45 #else
46 #include <sys/stat.h>
47 #endif
48
49 #include "../biosig.h"
50
51 /* TODO:
52 - need to separate sopen_heka() and sread_heka()
53 - data swapping
54 */
55
56 #define min(a,b) (((a) < (b)) ? (a) : (b))
57 #define max(a,b) (((a) > (b)) ? (a) : (b))
58
59 /****************************************************************************
60 rational :
61 computes the rational approximation of a floating point number
62 such that n/d is an approximation for r with an relative
63 error smaller than tol
64
65 see Octave's rat.m
66 ****************************************************************************/
67 void rational (double x, double tol, long *n, long *d) {
68
69 if (x != x) { // i.e. isnan(x)
70 *n = 0;
71 *d = 0;
72 return;
73 }
74
75 if (!isfinite(x)) {
76 *n = x>0; // i.e. sign(x)
77 *d = 0;
78 return;
79 }
80
81 tol *= fabs(x);
82 *n = lround(x);
83 *d = 1;
84 double frac = x - *n;
85 long lastn = 1, lastd = 0;
86
87 while (fabs((*d) * x - (*n) ) >= fabs((*d) * tol)) {
88 double flip = 1.0/frac;
89 long step = lround(flip);
90 frac = flip - step;
91
92 long nextn = *n, nextd = *d;
93 *n = *n * step + lastn;
94 *d = *d * step + lastd;
95 lastn = nextn;
96 lastd = nextd;
97 }
98
99 if (*d < 0) {
100 *n = - *n;
101 *d = - *d;
102 }
103 }
104
105
106 /****************************************************************************
107 heka2gdftime
108 converts heka time format into gdftime
109 ****************************************************************************/
110 gdf_time heka2gdftime(double t) {
111 t -= 1580970496; if (t<0) t += 4294967296; t += 9561652096;
112 return (uint64_t)ldexp(t/(24.0*60*60) + 584755, 32); // +datenum(1601,1,1));
113 }
114
115 /****************************************************************************
116 sopen_heka
117 reads heka format
118
119 if itx is not null, the file is converted into an ITX formated file
120 and streamed to itx, too.
121 ****************************************************************************/
122 void sopen_heka(HDRTYPE* hdr, FILE *itx) {
123 size_t count = hdr->HeadLen;
124
125 if (hdr->TYPE==HEKA) {
126 uint32_t Levels=0;
127 uint16_t k;
128 int32_t Counts[5], counts[5];
129 memset(Counts,0,20);
130 memset(counts,0,20);
131 uint32_t StartOfData=0,StartOfPulse=0;
132
133 union {
134 struct {
135 int32_t Root;
136 int32_t Group;
137 int32_t Series;
138 int32_t Sweep;
139 int32_t Trace;
140 } Rec;
141 int32_t all[5];
142 } Sizes;
143 memset(&Sizes,0,20);
144
145 // HEKA PatchMaster file format
146
147 count = hdr->HeadLen;
148 struct stat FileBuf;
149 stat(hdr->FileName,&FileBuf);
150 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, FileBuf.st_size);
151 if (count < 1024)
152 count += ifread(hdr->AS.Header+count, 1, 1024-count, hdr);
153 hdr->HeadLen = count;
154
155 char SWAP = ( hdr->FILE.LittleEndian && (__BYTE_ORDER == __BIG_ENDIAN)) \
156 || (!hdr->FILE.LittleEndian && (__BYTE_ORDER == __LITTLE_ENDIAN));
157
158 if (SWAP) {
159 // FIXME: support HEKA files that require swapping, it might be releated to the version of the HEKA format
160 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster format: this is not fully supported yet.");
161 }
162
163 /* get file size and read whole file */
164 count += ifread(hdr->AS.Header+count, 1, FileBuf.st_size - count, hdr);
165
166 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s(...): %i bytes read\n",__FILE__,__LINE__,__func__, (int)count);
167
168 // double oTime = lef64p(hdr->AS.Header+40); // not used, do not forget to swap if needed
169
170 uint32_t nItems = *(uint32_t*)(hdr->AS.Header+48);
171 Levels = *(int32_t*)(hdr->AS.Header+4);
172 if (SWAP) {
173 nItems = bswap_32(nItems);
174 Levels = bswap_32(Levels);
175 }
176
177 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s(...): nItems=%d Levels=%d\n",__FILE__,__LINE__,__func__, nItems,Levels);
178
179 if (Levels>5) {
180 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster format with more than 5 levels not supported");
181 return;
182 }
183 memcpy(Sizes.all, hdr->AS.Header+8, sizeof(uint32_t)*Levels);
184 if (SWAP) {
185 for (int l=0; l < Levels; l++)
186 Sizes.all[l] = bswap_32(Sizes.all[l]);
187 }
188
189 if (VERBOSE_LEVEL>6)
190 for (int l=0; l < Levels; l++)
191 fprintf(stdout,"%s (line %i) %s(...): Sizes[level=%d]=%d\n",__FILE__,__LINE__,__func__, l, Sizes.all[l]);
192
193
194 if (hdr->VERSION == 0) {
195 StartOfPulse = 8 + 4 * Levels;
196 }
197 else if (hdr->VERSION == 1) {
198 Sizes.Rec.Root = 544;
199 Sizes.Rec.Group = 128;
200 Sizes.Rec.Series = 1120;
201 Sizes.Rec.Sweep = 160;
202 Sizes.Rec.Trace = 296;
203 }
204 else if (hdr->VERSION == 2)
205 for (k=0; k < min(12,nItems); k++) {
206
207 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): HEKA nItems=%i\n",__func__,__LINE__, k);
208
209 uint32_t start = *(uint32_t*)(hdr->AS.Header+k*16+64);
210 uint32_t length = *(uint32_t*)(hdr->AS.Header+k*16+64+4);
211 if (SWAP) {
212 start = bswap_32(start);
213 length = bswap_32(length);
214 }
215 uint8_t *ext = hdr->AS.Header + k*16 + 64 + 8;
216
217 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): HEKA #%i: <%s> [%i:+%i]\n",__func__,__LINE__,k,ext,start,length);
218
219 if (!start) break;
220
221 if ((start+8) > count) {
222 biosigERROR(hdr, B4C_INCOMPLETE_FILE, "Heka/Patchmaster: file is corrupted - segment with pulse data is not available!");
223 return;
224 }
225
226 if (!memcmp(ext,".pul\0\0\0\0",8)) {
227 // find pulse data
228 ifseek(hdr, start, SEEK_SET);
229
230 //magic = *(int32_t*)(hdr->AS.Header+start);
231 Levels = *(int32_t*)(hdr->AS.Header+start+4);
232 if (SWAP) Levels = bswap_32(Levels);
233 if (Levels>5) {
234 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster format with more than 5 levels not supported");
235 return;
236 }
237
238 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): HEKA #%i Levels=%i\n",__func__,__LINE__,k,Levels);
239
240 memcpy(Sizes.all,hdr->AS.Header+start+8,sizeof(int32_t)*Levels);
241 if (SWAP) {
242 int l;
243 for (l=0; l < Levels; l++) Sizes.all[l] = bswap_32(Sizes.all[l]);
244 }
245
246 if (VERBOSE_LEVEL>7) {int l; for (l=0; l < Levels; l++) fprintf(stdout,"%s (line %i): HEKA #%i %i\n",__func__,__LINE__,l, Sizes.all[l]); }
247
248 StartOfPulse = start + 8 + 4 * Levels;
249 }
250
251 else if (!memcmp(ext,".dat\0\0\0\0",8)) {
252 StartOfData = start;
253 }
254 }
255
256 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s(...): \n",__FILE__,__LINE__,__func__);
257
258 // if (!Sizes) free(Sizes); Sizes=NULL;
259
260 /* DONE: HEKA, check channel number and label
261 pass 1:
262 + get number of sweeps
263 + get number of channels
264 + check whether all traces of a single sweep have the same SPR, and Fs
265 + check whether channelnumber (TrAdcChannel), scaling (DataScaler) and Label fit among all sweeps
266 + extract the total number of samples
267 + physical units
268 + level 4 may have no children
269 + count event descriptions Level2/SeLabel
270 pass 2:
271 + initialize data to NAN
272 + skip sweeps if selected channel is not in it
273 + Y scale, physical scale
274 + Event.CodeDescription, Events,
275 resampling
276 */
277
278 uint32_t k1=0, k2=0, k3=0, k4=0;
279 uint32_t K1=0, K2=0, K3=0, K4=0, K5=0;
280 double t;
281 size_t pos;
282
283 // read K1
284 if (SWAP) {
285 K1 = bswap_32(*(uint32_t*)(hdr->AS.Header + StartOfPulse + Sizes.Rec.Root));
286 hdr->VERSION = bswap_32(*(uint32_t*)(hdr->AS.Header + StartOfPulse));
287 union {
288 double f64;
289 uint64_t u64;
290 } c;
291 c.u64 = bswap_64(*(uint64_t*)(hdr->AS.Header + StartOfPulse + 520));
292 t = c.f64;
293 } else {
294 K1 = (*(uint32_t*)(hdr->AS.Header + StartOfPulse + Sizes.Rec.Root));
295 hdr->VERSION = (*(uint32_t*)(hdr->AS.Header + StartOfPulse));
296 t = (*(double*)(hdr->AS.Header + StartOfPulse + 520));
297 }
298
299 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %s(...): \n",__FILE__,__LINE__,__func__);
300
301 hdr->T0 = heka2gdftime(t); // this is when when heka was started, data is recorded later.
302 hdr->SampleRate = 0.0;
303 double *DT = NULL; // list of sampling intervals per channel
304 hdr->SPR = 0;
305
306 /*******************************************************************************************************
307 HEKA: read structural information
308 *******************************************************************************************************/
309
310 pos = StartOfPulse + Sizes.Rec.Root + 4;
311 size_t EventN=0;
312
313 for (k1=0; k1<K1; k1++) {
314 // read group
315
316 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA L1 @%i=\t%i/%i \n",(int)(pos+StartOfData),k1,K1);
317
318 pos += Sizes.Rec.Group+4;
319 // read number of children
320 K2 = (*(uint32_t*)(hdr->AS.Header+pos-4));
321
322 hdr->AS.auxBUF = (uint8_t*)realloc(hdr->AS.auxBUF,K2*33); // used to store name of series
323 for (k2=0; k2<K2; k2++) {
324 // read series
325 union {
326 double f64;
327 uint64_t u64;
328 } Delay;
329 char *SeLabel = (char*)(hdr->AS.Header+pos+4); // max 32 bytes
330 strncpy((char*)hdr->AS.auxBUF + 33*k2, (char*)hdr->AS.Header+pos+4, 32); hdr->AS.auxBUF[33*k2+32] = 0;
331 SeLabel = (char*)hdr->AS.auxBUF + 33*k2;
332 double tt = *(double*)(hdr->AS.Header+pos+136); // time of series. TODO: this time should be taken into account
333 Delay.u64 = bswap_64(*(uint64_t*)(hdr->AS.Header+pos+472+176));
334
335 gdf_time t = heka2gdftime(tt);
336
337 if (VERBOSE_LEVEL>7) {
338 char tmp[60];
339 snprintf_gdfdate(tmp, sizeof(tmp), t);
340 fprintf(stdout,"HEKA L2 @%i=%s %f\t%i/%i %i/%i t=%.17g %s\n",(int)(pos+StartOfData),SeLabel,Delay.f64,k1,K1,k2,K2,ldexp(t,-32),tmp);
341 }
342
343 pos += Sizes.Rec.Series + 4;
344 // read number of children
345 K3 = (*(uint32_t*)(hdr->AS.Header+pos-4));
346 if (EventN <= hdr->EVENT.N + K3 + 2) {
347 EventN = max(max(16,EventN),hdr->EVENT.N+K3+2) * 2;
348 if (reallocEventTable(hdr, EventN) == SIZE_MAX) {
349 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "Allocating memory for event table failed.");
350 return;
351 };
352 }
353
354 if (!hdr->AS.SegSel[0] && !hdr->AS.SegSel[1] && !hdr->AS.SegSel[2]) {
355 // in case of reading the whole file (no sweep selection), include marker for start of series
356 FreeTextEvent(hdr, hdr->EVENT.N, SeLabel);
357 hdr->EVENT.POS[hdr->EVENT.N] = hdr->SPR; // within reading the structure, hdr->SPR is used as a intermediate variable counting the number of samples
358 #if (BIOSIG_VERSION >= 10500)
359 hdr->EVENT.TimeStamp[hdr->EVENT.N] = t;
360 #endif
361 hdr->EVENT.N++;
362 }
363
364 for (k3=0; k3<K3; k3++) {
365 // read sweep
366 hdr->NRec++; // increase number of sweeps
367 size_t SPR = 0, spr = 0;
368 gdf_time t = heka2gdftime(*(double*)(hdr->AS.Header+pos+48)); // time of sweep. TODO: this should be taken into account
369
370 if (VERBOSE_LEVEL>7) {
371 char tmp[60];
372 snprintf_gdfdate(tmp, sizeof(tmp), t);
373 fprintf(stdout,"HEKA L3 @%i= %fHz\t%i/%i %i/%i %i/%i %s\n",(int)(pos+StartOfData),hdr->SampleRate,k1,K1,k2,K2,k3,K3,tmp);
374 }
375
376 char flagSweepSelected = (hdr->AS.SegSel[0]==0 || k1+1==hdr->AS.SegSel[0])
377 && (hdr->AS.SegSel[1]==0 || k2+1==hdr->AS.SegSel[1])
378 && (hdr->AS.SegSel[2]==0 || k3+1==hdr->AS.SegSel[2]);
379
380 // hdr->SPR
381 if (hdr->SPR==0)
382 hdr->T0 = t; // start time of first recording determines the start time of the recording
383 else if (flagSweepSelected && hdr->SPR > 0) {
384 // marker for start of sweep
385 hdr->EVENT.POS[hdr->EVENT.N] = hdr->SPR; // within reading the structure, hdr->SPR is used as a intermediate variable counting the number of samples
386 hdr->EVENT.TYP[hdr->EVENT.N] = 0x7ffe;
387 #if (BIOSIG_VERSION >= 10500)
388 hdr->EVENT.TimeStamp[hdr->EVENT.N] = t;
389 #endif
390 hdr->EVENT.N++;
391 }
392
393 pos += Sizes.Rec.Sweep + 4;
394 // read number of children
395 K4 = (*(uint32_t*)(hdr->AS.Header+pos-4));
396 for (k4=0; k4<K4; k4++) {
397 // read trace
398 double DigMin, DigMax;
399 uint16_t gdftyp = 0;
400 uint32_t ns = (*(uint32_t*)(hdr->AS.Header+pos+36));
401 uint32_t DataPos = (*(uint32_t*)(hdr->AS.Header+pos+40));
402 spr = (*(uint32_t*)(hdr->AS.Header+pos+44));
403 double DataScaler= (*(double*)(hdr->AS.Header+pos+72));
404 double Toffset = (*(double*)(hdr->AS.Header+pos+80)); // time offset of
405 uint16_t pdc = PhysDimCode((char*)(hdr->AS.Header + pos + 96));
406 double dT = (*(double*)(hdr->AS.Header+pos+104));
407 //double XStart = (*(double*)(hdr->AS.Header+pos+112));
408 uint16_t XUnits = PhysDimCode((char*)(hdr->AS.Header+pos+120));
409 double YRange = (*(double*)(hdr->AS.Header+pos+128));
410 double YOffset = (*(double*)(hdr->AS.Header+pos+136));
411 double Bandwidth = (*(double*)(hdr->AS.Header+pos+144));
412 //double PipetteResistance = (*(double*)(hdr->AS.Header+pos+152));
413 double RsValue = (*(double*)(hdr->AS.Header+pos+192));
414
415 uint8_t ValidYRange = hdr->AS.Header[pos+220];
416 uint16_t AdcChan = (*(uint16_t*)(hdr->AS.Header+pos+222));
417 /* obsolete: range is defined by DigMin/DigMax * DataScaler + YOffset
418 double PhysMin = (*(double*)(hdr->AS.Header+pos+224));
419 double PhysMax = (*(double*)(hdr->AS.Header+pos+232));
420 */
421
422 if (VERBOSE_LEVEL>7) fprintf(stdout, "%s (line %i): %i %i %i %i %i %g %g 0x%x xUnits=%i %g %g %g %g %i %i\n", __FILE__,__LINE__, k1, k2, k3, k4, ns, DataScaler, Toffset, pdc, XUnits, YRange, YOffset, Bandwidth, RsValue, ValidYRange, AdcChan);
423
424 switch (hdr->AS.Header[pos+70]) {
425 case 0: gdftyp = 3; //int16
426 /*
427 It seems that the range is 1.024*(2^15-1)/2^15 nA or V
428 and symetric around zero. i.e. YOffset is zero
429 */
430 DigMax = ldexp(1.0,15) - 1.0;
431 DigMin = -DigMax;
432 break;
433 case 1: gdftyp = 5; //int32
434 DigMax = ldexp(1.0, 31) - 1.0;
435 DigMin = -DigMax;
436 break;
437 case 2: gdftyp = 16; //float32
438 DigMax = 1e9;
439 DigMin = -1e9;
440 break;
441 case 3: gdftyp = 17; //float64
442 DigMax = 1e9;
443 DigMin = -1e9;
444 break;
445 default:
446 DigMax = NAN;
447 DigMin = NAN;
448 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster: data type not supported");
449 };
450
451 if (SWAP) {
452 AdcChan = bswap_16(AdcChan);
453 ns = bswap_32(ns);
454 DataPos = bswap_32(DataPos);
455 spr = bswap_32(spr);
456 // avoid breaking strict-aliasing rules
457 union {
458 double f64;
459 uint64_t u64;
460 } c;
461 c.f64 = dT; c.u64 = bswap_64(c.u64); dT = c.f64;
462 c.f64 = YRange; c.u64 = bswap_64(c.u64); YRange = c.f64;
463 c.f64 = YOffset; c.u64 = bswap_64(c.u64); YOffset = c.f64;
464 //c.f64 = PhysMax; c.u64 = bswap_64(c.u64); PhysMax = c.f64;
465 //c.f64 = PhysMin; c.u64 = bswap_64(c.u64); PhysMin = c.f64;
466 c.f64 = Toffset; c.u64 = bswap_64(c.u64); Toffset = c.f64;
467 c.f64 = DataScaler; c.u64 = bswap_64(c.u64); DataScaler = c.f64;
468 }
469
470 if (YOffset != 0.0)
471 fprintf(stderr,"!!! WARNING !!! HEKA: the offset is not zero - "
472 "this case is not tested and might result in incorrect scaling of "
473 "the data,\n!!! YOU ARE WARNED !!!\n");
474
475 // scale to standard units - no prefix
476 double Cal = DataScaler * PhysDimScale(pdc);
477 double Off = YOffset * PhysDimScale(pdc);
478 pdc &= 0xffe0;
479 float Fs = 1.0 / ( dT * PhysDimScale(XUnits) ) ; // float is used to avoid spurios accuracy, round to single precision accuracy
480
481 if (flagSweepSelected) {
482
483 if (hdr->SampleRate <= 0.0) hdr->SampleRate = Fs;
484 if (fabs(hdr->SampleRate - Fs) > 1e-9*Fs) {
485 long DIV1 = 1, DIV2 = 1;
486 rational(hdr->SampleRate*dT*PhysDimScale(XUnits), 1e-6, &DIV2, &DIV1);
487
488 if (DIV1 > 1) {
489 if ( ((size_t)DIV1 * hdr->SPR) > 0xffffffffffffffff) {
490 fprintf(stderr,"!!! WARNING sopen_heka(%s) !!! due to resampling, the data will have more then 2^31 samples !!!\n", hdr->FileName);
491 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"HEKA file has more than 2^32 samples - this is not supported yet");
492 }
493 hdr->SPR *= DIV1;
494 hdr->SampleRate *= DIV1;
495 hdr->EVENT.SampleRate = hdr->SampleRate;
496 size_t n = 0;
497 while (n < hdr->EVENT.N)
498 hdr->EVENT.POS[n++] *= DIV1;
499 }
500 if (DIV2 > 1) spr *= DIV2;
501 }
502
503 // samples per sweep
504 if (k4==0) SPR = spr;
505 else if (SPR != spr) {
506 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster: number of samples among channels within a single sweep do not match.");
507 return;
508 }
509 }
510
511 char *Label = (char*)hdr->AS.Header+pos+4;
512 for (ns=0; ns < hdr->NS; ns++) {
513 if (!strcmp(hdr->CHANNEL[ns].Label,Label)) break;
514 }
515
516 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA L4 @%i= #%i,%i, %s %f %fHz\t%i/%i %i/%i %i/%i %i/%i \n",(int)(pos+StartOfData),ns,AdcChan,Label,hdr->SampleRate,Fs,k1,K1,k2,K2,k3,K3,k4,K4);
517
518 CHANNEL_TYPE *hc;
519 if (ns >= hdr->NS) {
520 hdr->NS = ns + 1;
521 #ifdef WITH_TIMESTAMPCHANNEL
522 // allocate memory for an extra time stamp channel, which is define only after the end of the channel loop - see below
523 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, (hdr->NS + 1) * sizeof(CHANNEL_TYPE));
524 #else
525 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
526 #endif
527 hc = hdr->CHANNEL + ns;
528 strncpy(hc->Label, Label, max(32, MAX_LENGTH_LABEL));
529 hc->Label[max(32,MAX_LENGTH_LABEL)] = 0;
530 hc->Transducer[0] = 0;
531 hc->SPR = 1;
532 hc->PhysDimCode = pdc;
533 hc->OnOff = 1;
534 hc->GDFTYP = gdftyp;
535 hc->LeadIdCode = 0;
536 hc->DigMin = DigMin;
537 hc->DigMax = DigMax;
538
539 // TODO: case of non-zero YOffset is not tested //
540 hc->PhysMax = DigMax * Cal + Off;
541 hc->PhysMin = DigMin * Cal + Off;
542
543 hc->Cal = Cal;
544 hc->Off = Off;
545 hc->TOffset = Toffset;
546
547 #ifndef NDEBUG
548 double Cal2 = (hc->PhysMax - hc->PhysMin) / (hc->DigMax - hc->DigMin);
549 double Off2 = hc->PhysMin - Cal2 * hc->DigMin;
550 double Off3 = hc->PhysMax - Cal2 * hc->DigMax;
551
552 assert(fabs(Cal-Cal2) < 1e-8 * Cal);
553 assert(fabs(Off-Off2) < 1e-8 * Cal);
554 assert(fabs(Off-Off3) < 1e-8 * Cal);
555 #endif
556
557 /* TODO: fix remaining channel header */
558 /* LowPass, HighPass, Notch, Impedance, */
559 hc->HighPass = NAN;
560 hc->LowPass = (Bandwidth > 0) ? Bandwidth : NAN;
561 hc->Notch = NAN;
562 hc->Impedance = (RsValue > 0) ? RsValue : NAN;
563
564 DT = (double*) realloc(DT, hdr->NS*sizeof(double));
565 DT[ns] = dT;
566 }
567 else {
568 /*
569 channel has been already defined in earlier sweep.
570 check compatibility and adapt internal format when needed
571 */
572 hc = hdr->CHANNEL + ns;
573 double PhysMax = DigMax * Cal + Off;
574 double PhysMin = DigMin * Cal + Off;
575 // get max value to avoid false positive saturation detection when scaling changes
576 if (hc->PhysMax < PhysMax) hc->PhysMax = PhysMax;
577 if (hc->PhysMin > PhysMin) hc->PhysMin = PhysMin;
578
579 if (hc->GDFTYP < gdftyp) {
580 /* when data type changes, use the largest data type */
581 if (4 < hc->GDFTYP && hc->GDFTYP < 9 && gdftyp==16)
582 /* (U)INT32, (U)INT64 + FLOAT32 -> DOUBLE */
583 hc->GDFTYP = 17;
584 else
585 hc->GDFTYP = gdftyp;
586 }
587 else if (hc->GDFTYP > gdftyp) {
588 /* when data type changes, use the largest data type */
589 if (4 < gdftyp && gdftyp < 9 && hc->GDFTYP==16)
590 /* (U)INT32, (U)INT64 + FLOAT32 -> DOUBLE */
591 hc->GDFTYP = 17;
592 }
593
594 if (fabs(hc->Cal - Cal) > 1e-9*Cal) {
595 /* when scaling changes from sweep to sweep, use floating point numbers internally. */
596 if (hc->GDFTYP < 5) // int16 or smaller
597 hc->GDFTYP = 16;
598 else if (hc->GDFTYP < 9) // int32, int64 -> double
599 hc->GDFTYP = 17;
600 }
601
602 if ((pdc & 0xFFE0) != (hc->PhysDimCode & 0xFFE0)) {
603 fprintf(stdout, "ERROR: [%i,%i,%i,%i] Yunits in %s do not match %04x(%s) ! %04x(%s)\n",k1,k2,k3,k4, Label, pdc, PhysDim3(pdc), hc->PhysDimCode, PhysDim3(hc->PhysDimCode));
604 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster: Yunits do not match");
605 }
606 if ( ( VERBOSE_LEVEL > 7 ) && ( fabs( DT[ns] - dT) > 1e-9 * dT) ) {
607 fprintf(stdout, "%s (line %i) different sampling rates [%i,%i,%i,%i]#%i,%f/%f \n",__FILE__,__LINE__,(int)k1,(int)k2,(int)k3,(int)k4,(int)ns, 1.0/DT[ns],1.0/dT);
608 }
609 }
610
611 if (YOffset) {
612 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster: YOffset is not zero");
613 }
614 if (hdr->AS.Header[pos+220] != 1) {
615 fprintf(stderr,"WARNING Heka/Patchmaster: ValidYRange not set to 1 but %i in sweep [%i,%i,%i,%i]\n", hdr->AS.Header[pos+220],k1+1,k2+1,k3+1,k4+1);
616 }
617
618 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA L6 @%i= #%i,%i, %s %f-%fHz\t%i/%i %i/%i %i/%i %i/%i \n",(int)(pos+StartOfData),ns,AdcChan,Label,hdr->SampleRate,Fs,k1,K1,k2,K2,k3,K3,k4,K4);
619
620 pos += Sizes.Rec.Trace+4;
621 // read number of children -- this should be 0 - ALWAYS;
622 K5 = (*(uint32_t*)(hdr->AS.Header+pos-4));
623 if (K5) {
624 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster: Level 4 has some children");
625 }
626 } // end loop k4
627
628 // if sweep is selected, add number of samples to counter
629 if (flagSweepSelected) {
630 if ( hdr->SPR > 0xffffffffffffffffu-SPR) {
631 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"HEKA file has more than 2^32 samples - this is not supported yet");
632 }
633 hdr->SPR += SPR;
634 }
635 } // end loop k3
636 } // end loop k2
637 } // end loop k1
638
639 #ifndef NO_BI
640 if (DT) free(DT);
641 #else
642 size_t *BI = (size_t*) DT; // DT is not used anymore, use space for BI
643 #endif
644 DT = NULL;
645
646 #ifdef WITH_TIMESTAMPCHANNEL
647 {
648 /*
649 define time stamp channel, memory is already allocated above
650 */
651 CHANNEL_TYPE *hc = hdr->CHANNEL + hdr->NS;
652 hc->GDFTYP = 7; // corresponds to int64_t, gdf_time
653 strcpy(hc->Label,"Timestamp");
654 hc->Transducer[0]=0;
655 hc->PhysDimCode = 2272; // units: days [d]
656 hc->LeadIdCode = 0;
657 hc->SPR = 1;
658 hc->Cal = ldexp(1.0, -32);
659 hc->Off = 0.0;
660 hc->OnOff = 1;
661 hc->DigMax = ldexp( 1.0, 61);
662 hc->DigMin = 0;
663 hc->PhysMax = hc->DigMax * hc->Cal;
664 hc->PhysMin = hc->DigMin * hc->Cal;
665 hc->TOffset = 0.0;
666 hc->Impedance = NAN;
667 hc->HighPass = NAN;
668 hc->LowPass = NAN;
669 hc->Notch = NAN;
670 hc->XYZ[0] = 0.0;
671 hc->XYZ[1] = 0.0;
672 hc->XYZ[2] = 0.0;
673
674 hdr->NS++;
675 }
676 #endif
677
678 hdr->NRec = 1;
679 hdr->AS.bpb = 0;
680 for (k = 0; k < hdr->NS; k++) {
681 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
682
683 hc->Cal = (hc->PhysMax - hc->PhysMin) / (hc->DigMax - hc->DigMin);
684 hc->Off = hc->PhysMin - hc->DigMin * hc->Cal;
685 #ifndef NO_BI
686 hc->bi = hdr->AS.bpb;
687 #else
688 BI[k] = hdr->AS.bpb;
689 #endif
690 hc->SPR = hdr->SPR;
691 hdr->AS.bpb += hc->SPR * (GDFTYP_BITS[hc->GDFTYP]>>3); // multiplation must not exceed 32 bit limit
692 }
693
694 if (hdr->AS.B4C_ERRNUM) {
695 #ifdef NO_BI
696 if (BI) free(BI);
697 #endif
698 return;
699 }
700 hdr->ID.Manufacturer.Name = "HEKA/Patchmaster";
701
702
703
704 /******************************************************************************
705 SREAD_HEKA
706
707 void sread_heka(HDRTYPE* hdr, FILE *itx, ... ) {
708
709 ******************************************************************************/
710
711 if (VERBOSE_LEVEL > 7) fprintf(stdout,"HEKA: 400: %"PRIi64" %"PRIi32" %"PRIi64"\n",hdr->NRec, hdr->AS.bpb, hdr->NRec * (size_t)hdr->AS.bpb);
712
713 size_t sz = hdr->NRec * (size_t)hdr->AS.bpb;
714 if (sz/hdr->NRec < hdr->AS.bpb) {
715 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "memory allocation failed - more than 2GB required but platform supports only 32 bit!");
716 return;
717 }
718
719 void* tmpptr = realloc(hdr->AS.rawdata, sz);
720 if (tmpptr!=NULL)
721 hdr->AS.rawdata = (uint8_t*) tmpptr;
722 else {
723 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "memory allocation failed - not enough memory!");
724 return;
725 }
726 assert(hdr->NRec >= 0);
727 memset(hdr->AS.rawdata, 0xff, hdr->NRec * (size_t)hdr->AS.bpb); // initialize with NAN's
728
729
730 #ifdef NO_BI
731 #define _BI (BI[k])
732 #else
733 #define _BI (hc->bi)
734 #endif
735 /* initialize with NAN's */
736 for (k=0; k<hdr->NS; k++) {
737 size_t k1;
738 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
739 switch (hc->GDFTYP) {
740 case 3:
741 for (k1=0; k1<hc->SPR; k1++) {
742 *(uint16_t*)(hdr->AS.rawdata + _BI + k1 * 2) = 0x8000;
743 }
744 break;
745 case 5:
746 for (k1=0; k1<hc->SPR; k1++) *(uint32_t*)(hdr->AS.rawdata + _BI + k1 * 4) = 0x80000000;
747 break;
748 case 7:
749 for (k1=0; k1<hc->SPR; k1++) *(int64_t*)(hdr->AS.rawdata + _BI + k1 * 4) = 0x8000000000000000LL;
750 break;
751 case 16:
752 for (k1=0; k1<hc->SPR; k1++) *(float*)(hdr->AS.rawdata + _BI + k1 * 4) = NAN;
753 break;
754 case 17:
755 for (k1=0; k1<hc->SPR; k1++) *(double*)(hdr->AS.rawdata + _BI + k1 * 8) = NAN;
756 break;
757 }
758 }
759 #undef _BI
760
761 char *WAVENAME = NULL;
762 if (itx) {
763 fprintf(itx, "IGOR\r\nX Silent 1\r\n");
764 const char *fn = strrchr(hdr->FileName,'\\');
765 if (fn) fn++;
766 else fn = strrchr(hdr->FileName,'/');
767 if (fn) fn++;
768 else fn = hdr->FileName;
769
770 size_t len = strspn(fn,".");
771 WAVENAME = (char*)malloc(strlen(hdr->FileName)+7);
772 if (len)
773 strncpy(WAVENAME, fn, len);
774 else
775 strcpy(WAVENAME, fn); // Flawfinder: ignore
776 }
777
778 if (VERBOSE_LEVEL>7) hdr2ascii(hdr,stdout,4);
779
780 /*******************************************************************************************************
781 HEKA: read data blocks
782 *******************************************************************************************************/
783 uint32_t SPR = 0;
784 pos = StartOfPulse + Sizes.Rec.Root + 4;
785 for (k1=0; k1<K1; k1++) {
786 // read group
787
788 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA+L1 @%i=\t%i/%i \n",(int)(pos+StartOfData),k1,K1);
789
790 pos += Sizes.Rec.Group+4;
791 // read number of children
792 K2 = (*(uint32_t*)(hdr->AS.Header+pos-4));
793
794 for (k2=0; k2<K2; k2++) {
795 // read series
796 union {
797 double f64;
798 uint64_t u64;
799 } Delay;
800 uint32_t spr = 0;
801 char *SeLabel = (char*)(hdr->AS.Header+pos+4); // max 32 bytes
802 Delay.u64 = bswap_64(*(uint64_t*)(hdr->AS.Header+pos+472+176));
803
804 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA+L2 @%i=%s %f\t%i/%i %i/%i \n",(int)(pos+StartOfData),SeLabel,Delay.f64,k1,K1,k2,K2);
805
806 /* move to reading of data */
807 pos += Sizes.Rec.Series+4;
808 // read number of children
809 K3 = (*(uint32_t*)(hdr->AS.Header+pos-4));
810 for (k3=0; k3<K3; k3++) {
811 #if defined(WITH_TIMESTAMPCHANNEL)
812
813 #ifdef NO_BI
814 #define _BI (BI[hdr->NS-1])
815 #else
816 #define _BI (hdr->CHANNEL[hdr->NS-1].bi)
817 #endif
818 gdf_time t = heka2gdftime(*(double*)(hdr->AS.Header+pos+48)); // time of sweep. TODO: this should be taken into account
819 *(int64_t*)(hdr->AS.rawdata + _BI + SPR * 8) = t;
820 #undef _BI
821
822 #endif // WITH_TIMESTAMPCHANNEL
823 // read sweep
824 char flagSweepSelected = (hdr->AS.SegSel[0]==0 || k1+1==hdr->AS.SegSel[0])
825 && (hdr->AS.SegSel[1]==0 || k2+1==hdr->AS.SegSel[1])
826 && (hdr->AS.SegSel[2]==0 || k3+1==hdr->AS.SegSel[2]);
827
828 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA+L3 @%i=\t%i/%i %i/%i %i/%i sel=%i\n",(int)(pos+StartOfData),k1,K1,k2,K2,k3,K3,flagSweepSelected);
829
830 pos += Sizes.Rec.Sweep + 4;
831 // read number of children
832 K4 = (*(uint32_t*)(hdr->AS.Header+pos-4));
833 size_t DIV=1;
834 for (k4=0; k4<K4; k4++) {
835 if (!flagSweepSelected) {
836 pos += Sizes.Rec.Trace+4;
837 continue;
838 }
839
840 // read trace
841 uint16_t gdftyp = 0;
842 uint32_t ns = (*(uint32_t*)(hdr->AS.Header+pos+36));
843 uint32_t DataPos = (*(uint32_t*)(hdr->AS.Header+pos+40));
844 spr = (*(uint32_t*)(hdr->AS.Header+pos+44));
845 double DataScaler= (*(double*)(hdr->AS.Header+pos+72));
846 double Toffset = (*(double*)(hdr->AS.Header+pos+80)); // time offset of
847 uint16_t pdc = PhysDimCode((char*)(hdr->AS.Header + pos + 96));
848 char *physdim = (char*)(hdr->AS.Header + pos + 96);
849 double dT = (*(double*)(hdr->AS.Header+pos+104));
850 // double XStart = (*(double*)(hdr->AS.Header+pos+112));
851 // uint16_t XUnits = PhysDimCode((char*)(hdr->AS.Header+pos+120));
852 double YRange = (*(double*)(hdr->AS.Header+pos+128));
853 double YOffset = (*(double*)(hdr->AS.Header+pos+136));
854 // double Bandwidth = (*(double*)(hdr->AS.Header+pos+144));
855 uint16_t AdcChan = (*(uint16_t*)(hdr->AS.Header+pos+222));
856 /*
857 double PhysMin = (*(double*)(hdr->AS.Header+pos+224));
858 double PhysMax = (*(double*)(hdr->AS.Header+pos+232));
859 */
860 switch (hdr->AS.Header[pos+70]) {
861 case 0: gdftyp = 3; break; // int16
862 case 1: gdftyp = 5; break; // int32
863 case 2: gdftyp = 16; break; // float32
864 case 3: gdftyp = 17; break; // float64
865 default:
866 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster unknown data type is used");
867 };
868
869 if (SWAP) {
870 AdcChan = bswap_16(AdcChan);
871 ns = bswap_32(ns);
872 DataPos = bswap_32(DataPos);
873 spr = bswap_32(spr);
874 // avoid breaking strict-aliasing rules
875 union {
876 double f64;
877 uint64_t u64;
878 } c;
879 c.f64 = dT; c.u64 = bswap_64(c.u64); dT = c.f64;
880 c.f64 = YRange; c.u64 = bswap_64(c.u64); YRange = c.f64;
881 c.f64 = YOffset; c.u64 = bswap_64(c.u64); YOffset = c.f64;
882 /*
883 c.f64 = PhysMax; c.u64 = bswap_64(c.u64); PhysMax = c.f64;
884 c.f64 = PhysMin; c.u64 = bswap_64(c.u64); PhysMin = c.f64;
885 */
886 c.f64 = Toffset; c.u64 = bswap_64(c.u64); Toffset = c.f64;
887 }
888 double Fs = round(1.0 / dT);
889 DIV = round(hdr->SampleRate / Fs);
890
891 char *Label = (char*)(hdr->AS.Header+pos+4);
892 for (ns=0; ns < hdr->NS; ns++) {
893 if (!strcmp(hdr->CHANNEL[ns].Label, Label)) break;
894 }
895 CHANNEL_TYPE *hc = hdr->CHANNEL+ns;
896
897 if (VERBOSE_LEVEL>7) fprintf(stdout,"HEKA+L4 @%i= #%i,%i,%i/%i %s\t%i/%i %i/%i %i/%i %i/%i DIV=%i,%i,%i\n",(int)(pos+StartOfData),ns,AdcChan,spr,SPR,Label,k1,K1,k2,K2,k3,K3,k4,K4,(int)DIV,gdftyp,hc->GDFTYP);
898
899 if (itx) {
900 uint32_t k5;
901 double Cal = DataScaler;
902
903 assert(hdr->CHANNEL[ns].Off==0.0);
904 double Off = 0.0;
905
906 fprintf(itx, "\r\nWAVES %s_%i_%i_%i_%i\r\nBEGIN\r\n", WAVENAME,k1+1,k2+1,k3+1,k4+1);
907 switch (hc->GDFTYP) {
908 case 3:
909 for (k5 = 0; k5 < spr; ++k5)
910 fprintf(itx,"% e\n", (double)*(int16_t*)(hdr->AS.Header + DataPos + k5 * 2) * Cal + Off);
911 break;
912 case 5:
913 for (k5 = 0; k5 < spr; ++k5)
914 fprintf(itx,"% e\n", (double)*(int32_t*)(hdr->AS.Header + DataPos + k5 * 4) * Cal + Off);
915 break;
916 case 16:
917 for (k5 = 0; k5 < spr; ++k5)
918 fprintf(itx,"% e\n", (double)*(float*)(hdr->AS.Header + DataPos + k5 * 4) * Cal + Off);
919 break;
920 case 17:
921 for (k5 = 0; k5 < spr; ++k5)
922 fprintf(itx,"% e\n", *(double*)(hdr->AS.Header + DataPos + k5 * 8) * Cal + Off);
923 break;
924 }
925 fprintf(itx, "END\r\nX SetScale/P x, %g, %g, \"s\", %s_%i_%i_%i_%i\r\n", Toffset, dT, WAVENAME, k1+1,k2+1,k3+1,k4+1);
926 fprintf(itx, "X SetScale y,0,0,\"%s\", %s_%i_%i_%i_%i\n", physdim, WAVENAME, k1+1,k2+1,k3+1,k4+1);
927 }
928
929 #ifdef NO_BI
930 #define _BI (BI[ns])
931 #else
932 #define _BI (hc->bi)
933 #endif
934 // no need to check byte order because File.Endian is set and endian conversion is done in sread
935 if ((DIV==1) && (gdftyp == hc->GDFTYP)) {
936 uint16_t sz = GDFTYP_BITS[hc->GDFTYP]>>3;
937 memcpy(hdr->AS.rawdata + _BI + SPR * sz, hdr->AS.Header + DataPos, spr * sz);
938 }
939 else if (1) {
940 double Cal = DataScaler * PhysDimScale(pdc) / hdr->CHANNEL[ns].Cal;
941 assert(Cal==1.0 || hc->GDFTYP > 15); // when scaling changes, target data type is always float/double -> see above
942 uint32_t k5,k6;
943 switch (gdftyp) {
944 case 3:
945 switch (hc->GDFTYP) {
946 case 3:
947 for (k5 = 0; k5 < spr; ++k5) {
948 int16_t ival = *(int16_t*)(hdr->AS.Header + DataPos + k5 * 2);
949 for (k6 = 0; k6 < DIV; ++k6)
950 *(int16_t*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 2) = ival;
951 }
952 break;
953 case 5:
954 for (k5 = 0; k5 < spr; ++k5) {
955 int16_t ival = *(int16_t*)(hdr->AS.Header + DataPos + k5 * 2);
956 for (k6 = 0; k6 < DIV; ++k6)
957 *(int32_t*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 4) = (int32_t)ival;
958 }
959 break;
960 case 16:
961 for (k5 = 0; k5 < spr; ++k5) {
962 int16_t ival = *(int16_t*)(hdr->AS.Header + DataPos + k5 * 2);
963 for (k6 = 0; k6 < DIV; ++k6)
964 *(float*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 4) = (float)ival * Cal;
965 }
966 break;
967 case 17:
968 for (k5 = 0; k5 < spr; ++k5) {
969 int16_t ival = *(int16_t*)(hdr->AS.Header + DataPos + k5 * 2);
970 for (k6 = 0; k6 < DIV; ++k6)
971 *(double*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 8) = (double)ival * Cal;
972 }
973 break;
974 }
975 break;
976 case 5:
977 switch (hc->GDFTYP) {
978 case 5:
979 for (k5 = 0; k5 < spr; ++k5) {
980 int32_t ival = *(int32_t*)(hdr->AS.Header + DataPos + k5 * 4);
981 for (k6 = 0; k6 < DIV; ++k6)
982 *(int32_t*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 4) = ival;
983 }
984 break;
985 case 16:
986 for (k5 = 0; k5 < spr; ++k5) {
987 int32_t ival = *(int32_t*)(hdr->AS.Header + DataPos + k5 * 4);
988 for (k6 = 0; k6 < DIV; ++k6)
989 *(float*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 4) = (float)ival * Cal;
990 }
991 break;
992 case 17:
993 for (k5 = 0; k5 < spr; ++k5) {
994 int32_t ival = *(int32_t*)(hdr->AS.Header + DataPos + k5 * 4);
995 for (k6 = 0; k6 < DIV; ++k6)
996 *(double*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 8) = (double)ival * Cal;
997 }
998 break;
999 }
1000 break;
1001 case 16:
1002 switch (hc->GDFTYP) {
1003 case 16:
1004 for (k5 = 0; k5 < spr; ++k5) {
1005 float ival = *(float*)(hdr->AS.Header + DataPos + k5 * 4);
1006 for (k6 = 0; k6 < DIV; ++k6)
1007 *(float*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 4) = ival * Cal;
1008 }
1009 break;
1010 case 17:
1011 for (k5 = 0; k5 < spr; ++k5) {
1012 float ival = *(float*)(hdr->AS.Header + DataPos + k5 * 4);
1013 for (k6 = 0; k6 < DIV; ++k6)
1014 *(double*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 8) = (double)ival * Cal;
1015 }
1016 break;
1017 }
1018 break;
1019 case 17:
1020 switch (hc->GDFTYP) {
1021 case 17:
1022 for (k5 = 0; k5 < spr; ++k5) {
1023 double ival = *(double*)(hdr->AS.Header + DataPos + k5 * 8);
1024 for (k6 = 0; k6 < DIV; ++k6)
1025 *(double*)(hdr->AS.rawdata + _BI + (SPR + k5*DIV + k6) * 8) = ival * Cal;
1026 }
1027 break;
1028 }
1029 break;
1030 }
1031 }
1032 #undef _BI
1033 pos += Sizes.Rec.Trace+4;
1034
1035 }
1036 if (flagSweepSelected) SPR += spr * DIV;
1037 }
1038 }
1039 }
1040 #ifdef NO_BI
1041 if (BI) free(BI);
1042 #endif
1043 hdr->AS.first = 0;
1044 hdr->AS.length = hdr->NRec;
1045 free(hdr->AS.Header);
1046 hdr->AS.Header = NULL;
1047
1048 if (VERBOSE_LEVEL>7) fprintf(stdout,"End of SOPEN_HEKA\n");
1049 }
1050
1051 else {
1052 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Heka/Patchmaster format has unsupported version number");
1053 }
1054 }
1055
1056
1057 #ifdef __cplusplus
1058 }
1059 #endif
1060