Debian Codegraph @

biosig4c++/t210/sopen_igor.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2013,2014 Alois Schloegl <[email protected]>
4
5 This file is part of the "BioSig for C/C++" repository
6 (biosig4c++) at http://biosig.sf.net/
7
8 BioSig is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 3
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22 */
23
24 #define _GNU_SOURCE
25
26 /*
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stddef.h>
30 */
31 #include <ctype.h>
32 #include <math.h> // define macro isnan()
33 #include <stdlib.h>
34 #include <string.h>
35 #include "../biosig-dev.h"
36 #include "../igor/IgorBin.h"
37
38 #define IGOROLD 1 // will be used for testing and migrating to new version
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 #define ITX_MAXLINELENGTH 400
46
47 char *IgorChanLabel(char *inLabel, HDRTYPE *hdr, size_t *ngroup, size_t *nseries, size_t *nsweep, size_t *ns) {
48 /*
49 extract Channel Label of IGOR ITX data format
50 */
51
52 *ns = 0;
53 // static char Label[ITX_MAXLINELENGTH+1];
54 int k, s = 0, pos4=0, pos1=0;
55 for (k = strlen(inLabel); inLabel[k] < ' '; k--);
56 inLabel[k+1] = 0;
57
58 while (inLabel[k] >= ' ') {
59 while ( inLabel[k] >= '0' && inLabel[k] <= '9' )
60 k--;
61 if (inLabel[k]=='_') {
62 s++;
63 if (s==1) pos4 = k;
64 if (s==4) pos1 = k;
65 k--;
66 }
67 if ( inLabel[k] < '0' || inLabel[k] > '9' )
68 break;
69 }
70
71 if (3 < s) {
72 char nvar = 0;
73 for (k = strlen(inLabel); 0 < k && nvar < 4; k--) {
74 if (inLabel[k] == '_') {
75 inLabel[k] = 0;
76 char *v = inLabel+k+1;
77 size_t n = atol(v);
78
79 switch (nvar) {
80 case 0: *ns = n;
81 nvar++;
82 break;
83 case 1: *nsweep = n;
84 nvar++;
85 break;
86 case 2: *nseries = n;
87 nvar++;
88 break;
89 case 3: *ngroup = n;
90 nvar++;
91 break;
92 }
93 inLabel[k] = 0;
94 }
95 }
96 for (k=1; inLabel[pos4+k-1]; k++) {
97 inLabel[pos1+k] = inLabel[pos4+k];
98 }
99 }
100
101 if ((*ns)+1 > hdr->NS) { // another channel
102 hdr->NS = (*ns)+1;
103 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
104 }
105
106 return(inLabel);
107 }
108
109
110 /* compute igor checksum */
111 int ibwChecksum(int16_t *data, int flag_swap, int oldcksum, int numbytes) {
112 numbytes >>= 1; // 2 bytes to a short -- ignore trailing odd byte.
113 if (flag_swap) {
114 while(numbytes-- > 0)
115 oldcksum += bswap_16(*(data++));
116 } else {
117 while(numbytes-- > 0)
118 oldcksum += *(data++);
119 }
120 return oldcksum&0xffff;
121 }
122
123
124 /*
125 void ReorderBytes(void *p, int bytesPerPoint, long numValues) // Reverses byte order.
126 {
127 unsigned char ch, *p1, *p2, *pEnd;
128
129 pEnd = (unsigned char *)p + numValues*bytesPerPoint;
130 while (p < (void *)pEnd) {
131 p1 = p;
132 p2 = (unsigned char *)p + bytesPerPoint-1;
133 while (p1 < p2) {
134 ch = *p1;
135 *p1++ = *p2;
136 *p2-- = ch;
137 }
138 p = (unsigned char *)p + bytesPerPoint;
139 }
140 }
141 */
142
143 void ReorderShort(void* sp) {
144 *(uint16_t*)sp = bswap_16(*(uint16_t*)sp);
145 }
146
147 void ReorderLong(void* lp) {
148 *(uint32_t*)lp = bswap_32(*(uint32_t*)lp);
149 }
150
151 void ReorderDouble(void* dp) {
152 *(uint64_t*)dp = bswap_64(*(uint64_t*)dp);
153 }
154
155 void ReorderBinHeader1(BinHeader1* p) {
156 ReorderShort(&p->version);
157 ReorderLong(&p->wfmSize);
158 ReorderShort(&p->checksum);
159 }
160
161 void ReorderBinHeader2(BinHeader2* p) {
162 ReorderShort(&p->version);
163 ReorderLong(&p->wfmSize);
164 ReorderLong(&p->noteSize);
165 // ReorderLong(&p->pictSize);
166 ReorderShort(&p->checksum);
167 }
168
169 void ReorderBinHeader3(BinHeader3* p) {
170 ReorderShort(&p->version);
171 ReorderLong(&p->wfmSize);
172 ReorderLong(&p->noteSize);
173 ReorderLong(&p->formulaSize);
174 // ReorderLong(&p->pictSize);
175 ReorderShort(&p->checksum);
176 }
177
178 void ReorderBinHeader5(BinHeader5* p) {
179 ReorderShort(&p->version);
180 ReorderShort(&p->checksum);
181 ReorderLong(&p->wfmSize);
182 ReorderLong(&p->formulaSize);
183 ReorderLong(&p->noteSize);
184 ReorderLong(&p->dataEUnitsSize);
185 // ReorderBytes(&p->dimEUnitsSize, 4, 4);
186 ReorderLong(&p->dimEUnitsSize[0]);
187 ReorderLong(&p->dimEUnitsSize[1]);
188 ReorderLong(&p->dimEUnitsSize[2]);
189 ReorderLong(&p->dimEUnitsSize[3]);
190 // ReorderBytes(&p->dimLabelsSize, 4, 4);
191 ReorderLong(&p->dimLabelsSize[0]);
192 ReorderLong(&p->dimLabelsSize[1]);
193 ReorderLong(&p->dimLabelsSize[2]);
194 ReorderLong(&p->dimLabelsSize[3]);
195 ReorderLong(&p->sIndicesSize);
196 // ReorderLong(&p->optionsSize1);
197 // ReorderLong(&p->optionsSize2);
198 }
199
200 void ReorderWaveHeader2(WaveHeader2* p) {
201 ReorderShort(&p->type);
202 // ReorderLong(&p->next);
203 // char bname does not need to be reordered.
204 // ReorderShort(&p->whVersion);
205 // ReorderShort(&p->srcFldr);
206 // ReorderLong(&p->fileName);
207 // char dataUnits does not need to be reordered.
208 // char xUnits does not need to be reordered.
209 ReorderLong(&p->npnts);
210 // ReorderShort(&p->aModified);
211 ReorderDouble(&p->hsA);
212 ReorderDouble(&p->hsB);
213 // ReorderShort(&p->wModified);
214 // ReorderShort(&p->swModified);
215 ReorderShort(&p->fsValid);
216 ReorderDouble(&p->topFullScale);
217 ReorderDouble(&p->botFullScale);
218 // char useBits does not need to be reordered.
219 // char kindBits does not need to be reordered.
220 // ReorderLong(&p->formula);
221 // ReorderLong(&p->depID);
222 ReorderLong(&p->creationDate);
223 // char wUnused does not need to be reordered.
224 ReorderLong(&p->modDate);
225 // ReorderLong(&p->waveNoteH);
226 // The wData field marks the start of the wave data which will be reordered separately.
227 }
228
229 void ReorderWaveHeader5(WaveHeader5* p) {
230 // ReorderLong(&p->next);
231 ReorderLong(&p->creationDate);
232 ReorderLong(&p->modDate);
233 ReorderLong(&p->npnts);
234 ReorderShort(&p->type);
235 // ReorderShort(&p->dLock);
236 // char whpad1 does not need to be reordered.
237 // ReorderShort(&p->whVersion);
238 // char bname does not need to be reordered.
239 // ReorderLong(&p->whpad2);
240 // ReorderLong(&p->dFolder);
241 // ReorderBytes(&p->nDim, 4, 4);
242 ReorderLong(&p->nDim[0]);
243 ReorderLong(&p->nDim[1]);
244 ReorderLong(&p->nDim[2]);
245 ReorderLong(&p->nDim[3]);
246 // ReorderBytes(&p->sfA, 8, 4);
247 ReorderDouble(&p->sfA[0]);
248 ReorderDouble(&p->sfA[1]);
249 ReorderDouble(&p->sfA[2]);
250 ReorderDouble(&p->sfA[3]);
251 // ReorderBytes(&p->sfB, 8, 4);
252 ReorderDouble(&p->sfB[0]);
253 ReorderDouble(&p->sfB[1]);
254 ReorderDouble(&p->sfB[2]);
255 ReorderDouble(&p->sfB[3]);
256 // char dataUnits does not need to be reordered.
257 // char dimUnits does not need to be reordered.
258 ReorderShort(&p->fsValid);
259 // ReorderShort(&p->whpad3);
260 ReorderDouble(&p->topFullScale);
261 ReorderDouble(&p->botFullScale);
262 /*
263 // according to IgorBin.h, the following stuff can be ignored for reading IBW files //
264
265 ReorderLong(&p->dataEUnits);
266 // ReorderBytes(&p->dimEUnits, 4, 4);
267 ReorderLong(&p->dimEUnits[0]);
268 ReorderLong(&p->dimEUnits[1]);
269 ReorderLong(&p->dimEUnits[2]);
270 ReorderLong(&p->dimEUnits[3]);
271 // ReorderBytes(&p->dimLabels, 4, 4);
272 ReorderLong(&p->dimLabels[0]);
273 ReorderLong(&p->dimLabels[1]);
274 ReorderLong(&p->dimLabels[2]);
275 ReorderLong(&p->dimLabels[3]);
276 ReorderLong(&p->waveNoteH);
277 // ReorderBytes(&p->whUnused, 4, 16);
278 ReorderShort(&p->aModified);
279 ReorderShort(&p->wModified);
280 ReorderShort(&p->swModified);
281 // char useBits does not need to be reordered.
282 // char kindBits does not need to be reordered.
283 ReorderLong(&p->formula);
284 ReorderLong(&p->depID);
285 ReorderShort(&p->whpad4);
286 ReorderShort(&p->srcFldr);
287 ReorderLong(&p->fileName);
288 ReorderLong(&p->sIndices);
289 // The wData field marks the start of the wave data which will be reordered separately.
290 */
291 }
292
293
294 void sopen_ibw_read (HDRTYPE* hdr) {
295 /*
296 this function will be called by the function SOPEN in "biosig.c"
297
298 Input:
299 char* Header // contains the file content
300
301 Output:
302 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
303 */
304
305 fprintf(stdout,"Warning: support for IBW is very experimental\n");
306
307 uint16_t version = *(uint16_t*)hdr->AS.Header;
308 char flag_swap = (version & 0xFF) == 0;
309 if (flag_swap)
310 version = bswap_16(version);
311
312 unsigned count=0;
313 int binHeaderSize;
314 int waveHeaderSize;
315
316 switch (version) {
317 case 1: binHeaderSize=sizeof(BinHeader1); waveHeaderSize=sizeof(WaveHeader2); break;
318 case 2: binHeaderSize=sizeof(BinHeader2); waveHeaderSize=sizeof(WaveHeader2); break;
319 case 3: binHeaderSize=sizeof(BinHeader3); waveHeaderSize=sizeof(WaveHeader2); break;
320 case 5: binHeaderSize=sizeof(BinHeader5); waveHeaderSize=sizeof(WaveHeader5); break;
321 default:
322 if (VERBOSE_LEVEL>7) fprintf(stderr,"ver=%x \n",version);
323 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Igor/IBW: unsupported version number");
324 return;
325 }
326 count = binHeaderSize+waveHeaderSize;
327
328 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i): IBW v%i %i %i %i\n",__FILE__,__LINE__,version, binHeaderSize,waveHeaderSize, (int)count);
329
330 if (hdr->HeadLen < count) {
331 hdr->AS.Header = realloc(hdr->AS.Header, count+1);
332 hdr->HeadLen += ifread(hdr->AS.Header + hdr->HeadLen, 1, count-hdr->HeadLen, hdr);
333 }
334
335 // compute check sum
336 if ((int)hdr->VERSION == 5) count -= 4;
337
338 void *buffer = hdr->AS.Header;
339 // Check the checksum.
340 int crc;
341 if ((ibwChecksum((int16_t*)buffer, flag_swap, 0, count))) {
342 if (VERBOSE_LEVEL>7) fprintf(stderr,"ver=%x crc = %x %i %i \n",version, crc, binHeaderSize, waveHeaderSize);
343 biosigERROR(hdr, B4C_CRC_ERROR, "Igor/IBW: checksum error");
344 return;
345 }
346
347 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i): sizeof BinHeaders %i %i %i %i\n",__FILE__,__LINE__,
348 (int)sizeof(BinHeader1),(int)sizeof(BinHeader2),(int)sizeof(BinHeader3),(int)sizeof(BinHeader5));
349
350 // Do byte reordering if the file is from another platform.
351 if (flag_swap) {
352 version = bswap_16(version);
353 switch(version) {
354 case 1:
355 ReorderBinHeader1((BinHeader1*)hdr->AS.Header);
356 break;
357 case 2:
358 ReorderBinHeader2((BinHeader2*)hdr->AS.Header);
359 break;
360 case 3:
361 ReorderBinHeader3((BinHeader3*)hdr->AS.Header);
362 break;
363 case 5:
364 ReorderBinHeader5((BinHeader5*)hdr->AS.Header);
365 break;
366 }
367 switch(version) {
368 case 1: // Version 1 and 2 files use WaveHeader2.
369 case 2:
370 case 3:
371 ReorderWaveHeader2((WaveHeader2*)(hdr->AS.Header+binHeaderSize));
372 break;
373 case 5:
374 ReorderWaveHeader5((WaveHeader5*)(hdr->AS.Header+binHeaderSize));
375 break;
376 }
377 }
378
379 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i): sizeof WaveHeaders %i %i %i v%i\n",__FILE__,__LINE__,
380 (int)sizeof(WaveHeader2), (int)sizeof(WaveHeader5), (int)iftell(hdr),version);
381
382 // Read some of the BinHeader fields.
383 int16_t type = 0; // See types (e.g. NT_FP64) above. Zero for text waves.
384
385 hdr->NS = 1;
386 hdr->SampleRate = 1.0;
387 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
388
389 // Read some of the WaveHeader fields.
390 switch(version) {
391 case 1:
392 case 2:
393 case 3:
394 {
395 WaveHeader2* w2;
396 w2 = (WaveHeader2*)(buffer+binHeaderSize);
397 type = w2->type;
398 strncpy(hdr->CHANNEL[0].Label, w2->bname, MAX_LENGTH_LABEL);
399 hdr->CHANNEL[0].PhysDimCode = PhysDimCode(w2->dataUnits);
400 hdr->CHANNEL[0].SPR = hdr->SPR = 1;
401 hdr->NRec = w2->npnts;
402 #ifdef IGOROLD
403 hdr->CHANNEL[0].Cal = w2->hsA;
404 hdr->CHANNEL[0].Off = w2->hsB;
405 /*
406 hdr->CHANNEL[0].DigMax = (w2->topFullScale-w2->hsB) / w2->hsA;
407 hdr->CHANNEL[0].DigMin = (w2->botFullScale-w2->hsB) / w2->hsA;
408 */
409 #else
410 uint16_t pdc = PhysDimCode(w5->dimUnits[0]);
411 hdr->SampleRate /= w2->hsA * (pdc==0 ? 0.001 : PhysDimScale(pdc)); // if physical units unspecified, assume millisecond
412 hdr->CHANNEL[0].PhysMax = w2->topFullScale;
413 hdr->CHANNEL[0].PhysMin = w2->botFullScale;
414 #endif
415 hdr->FLAG.OVERFLOWDETECTION = !w2->fsValid;
416
417 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i): %f %f <%s> <%s>\n",__FILE__,__LINE__,w2->hsA,w2->hsB,w2->xUnits,w2->dataUnits);
418 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i): %f %f\n",__FILE__,__LINE__,w2->topFullScale,w2->botFullScale);
419
420 hdr->HeadLen = binHeaderSize+waveHeaderSize-16; // 16 = size of wData field in WaveHeader2 structure.
421 }
422 break;
423
424 case 5:
425 {
426 WaveHeader5* w5;
427 w5 = (WaveHeader5*)(buffer+binHeaderSize);
428 type = w5->type;
429
430 int k;
431 size_t nTraces=1;
432
433 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i): %i %i %i %i\n", __FILE__, __LINE__, w5->nDim[0], w5->nDim[1], w5->nDim[2], w5->nDim[3]);
434
435 for (k=1; (w5->nDim[k]!=0) && (k<4); k++) {
436 // count number of traces
437 nTraces *= w5->nDim[k];
438 }
439 if (nTraces > 1) {
440 // set break marker between traces
441 hdr->EVENT.N = nTraces-1;
442 if (reallocEventTable(hdr, hdr->EVENT.N) == SIZE_MAX) {
443 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "Allocating memory for event table failed.");
444 return;
445 };
446 size_t n;
447 for (n = 0; n < hdr->EVENT.N; n++) {
448 hdr->EVENT.TYP[n] = 0x7ffe;
449 hdr->EVENT.POS[n] = (n+1)*w5->nDim[0];
450 hdr->EVENT.DUR[n] = 0;
451 hdr->EVENT.CHN[n] = 0;
452 }
453 }
454
455 if (VERBOSE_LEVEL>7) {
456 for (k=0; k<4; k++)
457 fprintf(stdout,"%i\t%f\t%f\n",w5->nDim[k],w5->sfA[k],w5->sfB[k]);
458 }
459
460 strncpy(hdr->CHANNEL[0].Label, w5->bname, MAX_LENGTH_LABEL);
461 hdr->CHANNEL[0].PhysDimCode = PhysDimCode(w5->dataUnits);
462 hdr->CHANNEL[0].SPR = hdr->SPR = 1;
463 hdr->NRec = w5->npnts;
464 uint16_t pdc = PhysDimCode(w5->dimUnits[0]);
465 hdr->SampleRate /= w5->sfA[0] * (pdc==0 ? 0.001 : PhysDimScale(pdc)); // if physical units unspecified, assume millisecond
466
467 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %g.x+%g \n",__FILE__,__LINE__,w5->sfA[0],w5->sfB[0]);
468 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): |%s|%s|%s|%s|\n",__FILE__,__LINE__,w5->dimUnits[0],w5->dimUnits[1],w5->dimUnits[2],w5->dimUnits[3]);
469
470 #ifdef IGOROLD
471 hdr->CHANNEL[0].Cal = 1.0;
472 hdr->CHANNEL[0].Off = 0.0;
473 #else
474 hdr->CHANNEL[0].PhysMax = w5->topFullScale;
475 hdr->CHANNEL[0].PhysMin = w5->botFullScale;
476 #endif
477 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %f %f\n",__FILE__,__LINE__,w5->topFullScale,w5->botFullScale);
478
479 hdr->FLAG.OVERFLOWDETECTION = !w5->fsValid;
480
481 hdr->HeadLen = binHeaderSize+waveHeaderSize-4; // 4 = size of wData field in WaveHeader5 structure.
482 }
483 break;
484 }
485
486 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) Wave name=%s, npnts=%d, type=0x%x.\n", __FILE__, __LINE__, hdr->CHANNEL[0].Label, (int)hdr->NRec, type);
487
488 uint16_t gdftyp;
489 double digmin=NAN, digmax=NAN;
490 // Consider the number type, not including the complex bit or the unsigned bit.
491 switch(type & ~(NT_CMPLX | NT_UNSIGNED)) {
492 case NT_I8:
493 gdftyp = 1;
494 hdr->AS.bpb = 1; // char
495 break;
496 if (type & NT_UNSIGNED) {
497 gdftyp++;
498 digmin = ldexp(-1,7);
499 digmax = ldexp( 1,7)-1;
500 } else {
501 digmin = 0;
502 digmax = ldexp( 1,8)-1;
503 }
504 break;
505 case NT_I16:
506 gdftyp = 3;
507 hdr->AS.bpb = 2; // short
508 if (type & NT_UNSIGNED) {
509 gdftyp++;
510 digmin = ldexp(-1,15);
511 digmax = ldexp( 1,15)-1;
512 } else {
513 digmin = 0;
514 digmax = ldexp( 1,16)-1;
515 }
516 break;
517 case NT_I32:
518 gdftyp = 5;
519 hdr->AS.bpb = 4; // long
520 if (type & NT_UNSIGNED) {
521 gdftyp++;
522 digmin = ldexp(-1,31);
523 digmax = ldexp( 1,31)-1;
524 } else {
525 digmin = 0;
526 digmax = ldexp( 1,32)-1;
527 }
528 break;
529 case NT_FP32:
530 gdftyp = 16;
531 hdr->AS.bpb = 4; // float
532 digmin = -__FLT_MAX__;
533 digmax = __FLT_MAX__;
534 break;
535 case NT_FP64:
536 gdftyp = 17;
537 hdr->AS.bpb = 8; // double
538 digmin = -__DBL_MAX__;
539 digmax = __DBL_MAX__;
540 break;
541 case 0: // text waves
542 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Igor/IBW: text waves not supported");
543 return;
544 default:
545 if (VERBOSE_LEVEL>7) fprintf(stderr,"type=%x \n",version);
546 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Igor/IBW: unsupported or unknown data type");
547 return;
548 break;
549 }
550
551 if (type & NT_CMPLX) {
552 hdr->AS.bpb *= 2; // Complex wave - twice as many points.
553 hdr->NS *= 2;
554 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
555 strcpy(hdr->CHANNEL[2].Label,"imag part");
556 hdr->CHANNEL[1].Cal = 1.0;
557 hdr->CHANNEL[1].Off = 0.0;
558 hdr->CHANNEL[1].SPR = hdr->SPR;
559 }
560
561 typeof (hdr->NS) k;
562 size_t bpb=0;
563 for (k = 0; k < hdr->NS; k++) {
564 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
565 hc->GDFTYP = gdftyp;
566 hc->OnOff = 1;
567 hc->DigMin = digmin;
568 hc->DigMax = digmax;
569 #ifdef IGOROLD
570 hc->PhysMax = digmax * hc->Cal + hc->Off;
571 hc->PhysMin = digmin * hc->Cal + hc->Off;
572 #else
573 hc->Cal = (hc->PhysMax - hc->PhysMin) / (digmax - digmin);
574 hc->Off = hc->PhysMin - hc->DigMin * hc->Cal;
575
576 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) %f %f %f %f.\n", __FILE__, __LINE__, hc->PhysMax, hc->PhysMin, digmax, digmin);
577 #endif
578 hc->LeadIdCode = 0;
579 hc->bi = bpb;
580 hc->Transducer[0] = 0;
581 hc->TOffset = 0;
582 hc->LowPass = NAN;
583 hc->HighPass = NAN;
584 hc->Notch = NAN;
585 hc->Impedance = NAN;
586 hc->XYZ[0] = 0;
587 hc->XYZ[1] = 0;
588 hc->XYZ[2] = 0;
589 bpb += GDFTYP_BITS[gdftyp]/8;
590 }
591
592 hdr->FILE.POS = 0;
593 hdr->AS.first = 0;
594 hdr->AS.length= 0;
595 hdr->data.block = NULL;
596 hdr->AS.rawdata = NULL;
597
598 if (VERBOSE_LEVEL > 7)
599 fprintf(stdout, "%s (line %i) %i %i %i 0x%x\n", __FILE__, __LINE__,
600 (int)hdr->HeadLen, (int)hdr->FILE.size, (int)(hdr->AS.bpb*hdr->NRec), (int)(hdr->HeadLen+hdr->AS.bpb*hdr->NRec));
601
602 #ifndef IGOROLD
603 size_t endpos = hdr->HeadLen + hdr->AS.bpb * hdr->NRec;
604 if (endpos < hdr->FILE.size) {
605 /*
606 * If data were recorded with NeuroMatic/NClamp: http://www.neuromatic.thinkrandom.com/
607 * some additional information like SamplingRate, Scaling, etc. can be obtained from
608 * a text block at the end of a file.
609 */
610
611 size_t sz = hdr->FILE.size-endpos;
612 char *tmpstr = malloc(sz+1);
613 ifseek(hdr, endpos, SEEK_SET);
614 ifread(tmpstr, 1, sz, hdr);
615 tmpstr[sz]=0;
616 char *ptr = tmpstr;
617 // skip 0-bytes at the beginning of the block
618 while ((ptr != (tmpstr+sz)) && (*ptr==0)) {
619 ptr++;
620 }
621 if (ptr != tmpstr + sz) {
622 // if block is not empty
623 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) [%i]<%s>\n", __FILE__, __LINE__, sz, ptr);
624
625 // parse lines
626 char *line = strtok(ptr,"\n\r\0");
627 CHANNEL_TYPE *hc = hdr->CHANNEL+0;
628 struct tm t;
629 while (line != NULL) {
630
631 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) <%s>\n", __FILE__, __LINE__, line);
632
633 size_t p = strcspn(line,":"); // search for field delimiter
634
635 if (!strncmp(line,"ADCname",p)) {
636 strncpy(hc->Label,line+p+1,MAX_LENGTH_LABEL+1);
637 hc->Label[MAX_LENGTH_LABEL]=0;
638 }
639 else if (!strncmp(line,"ADCunits",p)) {
640 hc->PhysDimCode = PhysDimCode(line+p+1);
641 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) %s<%s>\n", __FILE__, __LINE__, line+p+1, PhysDim3(hc->PhysDimCode));
642 }
643 else if (!strncmp(line,"ADCunitsX",p)) {
644 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) Fs=%f %s\n", __FILE__, __LINE__, hdr->SampleRate, line+p+1);
645 if (!strcmp(line+p+1,"msec")) line[p+3]=0;
646 hdr->SampleRate /= PhysDimScale(PhysDimCode(line+p+1));
647 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) %f Hz\n", __FILE__, __LINE__, hdr->SampleRate);
648 }
649 else if (!strncmp(line,"ADCscale",p)) {
650 hc->Cal = strtod(line+p+1,NULL);
651 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) Cal %f %s\n", __FILE__, __LINE__, hc->Cal, line+p+1);
652 hc->PhysMax = hc->DigMax * hc->Cal;
653 hc->PhysMin = hc->DigMin * hc->Cal;
654 hc->Label[MAX_LENGTH_LABEL]=0;
655 }
656 else if (!strncmp(line,"Time",p)) {
657 ptr = line; // replace separator with :
658 while (*ptr) {
659 if (*ptr==',') *ptr=':';
660 ptr++;
661 }
662 strptime(line+p+1,"%H:%M:%S",&t);
663 if (VERBOSE_LEVEL > 7) fprintf(stdout, "%s (line %i) %s\n", __FILE__, __LINE__, line);
664 if (VERBOSE_LEVEL > 7) {
665 char tmp[30];
666 strftime(tmp,30,"%F %T",&t);
667 fprintf(stdout, "%s (line %i) %s\n", __FILE__, __LINE__, tmp);
668 }
669 }
670 else if (!strncmp(line,"Date",p)) {
671 strptime(line+p+1,"%d %b %Y",&t);
672 t.tm_hour = 0;
673 t.tm_min = 0;
674 t.tm_sec = 0;
675 if (VERBOSE_LEVEL > 7) {
676 char tmp[30];
677 strftime(tmp,30,"%F %T",&t);
678 fprintf(stdout, "%s (line %i) %s\n", __FILE__, __LINE__, tmp);
679 }
680 }
681 else if (!strncmp(line,"Time Stamp",p)) {
682 //hdr->SampleRate *= hdr->SPR*hdr->NRec/strtod(line+p+1,NULL);
683 }
684
685 line = strtok(NULL, "\n\r\0");
686 }
687 hdr->T0 = tm_time2gdf_time(&t);
688 }
689
690 if (tmpstr) free(tmpstr);
691 }
692 #endif // not defined IGOROLD
693 }
694
695 /*
696 * utility functions for managing List of Sweep names
697 */
698
699 struct sweepnames_t {
700 size_t idx;
701 char *name;
702 struct sweepnames_t *next;
703 };
704
705 size_t search_sweepnames(struct sweepnames_t* list, const char* name) {
706 /* first element starts with 1
707 zero is returned in case of empty list
708 */
709 struct sweepnames_t* next=list;
710 for (next=list; next!=NULL; next=next->next) {
711 if (!strcmp(next->name, name))
712 return next->idx;
713 }
714 return 0;
715 }
716
717 struct sweepnames_t* add_sweepnames(struct sweepnames_t* list, const char* name) {
718 struct sweepnames_t* next = (struct sweepnames_t*)malloc(sizeof(struct sweepnames_t));
719 next->name = strdup(name);
720 next->idx = list==NULL ? 1 : list->idx+1;
721 next->next = list;
722 return next;
723 }
724
725 size_t count_sweepnames(struct sweepnames_t* list) {
726 size_t count = 0;
727 for (; list!=NULL; list=list->next)
728 count++;
729 return count;
730 }
731
732 void clear_sweepnames(struct sweepnames_t* list) {
733 if (list==NULL) return;
734 if (list->name) free(list->name);
735 clear_sweepnames(list->next);
736 free(list->next);
737 }
738
739 void sopen_itx_read (HDRTYPE* hdr) {
740 #define IGOR_MAXLENLINE 400
741
742 char line[IGOR_MAXLENLINE+1];
743 char flagData = 0;
744 char flagSupported = 1;
745
746 if (VERBOSE_LEVEL>7)
747 fprintf(stdout,"%s (line %i) start reading %s,v%4.2f format (%i)\n",__FILE__,__LINE__,GetFileTypeString(hdr->TYPE),hdr->VERSION,ifeof(hdr));
748
749 typeof(hdr->SPR) SPR = 0, spr = 0;
750 typeof(hdr->NS) ns = 0;
751 int chanNo=0, sweepNo=0;
752 hdr->SPR = 0;
753 hdr->NRec= 0;
754
755 struct sweepnames_t* sweepname_list=NULL;
756
757 /*
758 checks the structure of the file and extracts formating information
759 */
760
761 ifseek(hdr,0,SEEK_SET);
762 int c = ifgetc(hdr); // read first character
763 while (!ifeof(hdr)) {
764
765 if (VERBOSE_LEVEL>8)
766 fprintf(stdout,"%s (line %i) start reading %s,v%4.2f format (%i)\n",__FILE__,__LINE__,GetFileTypeString(hdr->TYPE),hdr->VERSION,(int)iftell(hdr));
767
768 int i = 0;
769 while ( (c != -1) && (c != 10) && (c != 13) && (i < IGOR_MAXLENLINE) ) {
770 // terminate when any line break occurs
771 line[i++] = c;
772 c = ifgetc(hdr);
773 };
774 line[i] = 0;
775 while (isspace(c)) c = ifgetc(hdr); // keep first non-space character of next line in buffer
776
777 if (VERBOSE_LEVEL>8)
778 fprintf(stdout,"\t%s (line %i) <%s> (%i)\n",__FILE__,__LINE__,line,(int)iftell(hdr));
779
780 if (!strncmp(line,"BEGIN",5)) {
781 flagData = 1;
782 spr = 0;
783 hdr->CHANNEL[ns].bi = SPR*sizeof(double);
784 }
785 else if (!strncmp(line,"END",3)) {
786 flagData = 0;
787 if ((SPR!=0) && (SPR != spr)) {
788 flagSupported = 0;
789 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) ITX (not supported): %i, %i \n",__FILE__,__LINE__, SPR, spr);
790 }
791 else
792 SPR = spr;
793
794 if (ns==0) hdr->SPR += SPR;
795 }
796
797 else if (!strncmp(line,"X SetScale/P x",14)) {
798 /*
799 This line seems to have inconsistant formating, some times an comma is following the first 14 bytes, some times no comma is found.
800 here are some examples
801 X SetScale/P x, 0.000000000E+00, 5.000000000E-05,"s", AP101222bp_1_63_1_2
802 X SetScale/P x 0,5e-05,"s", f0ch1w1_1_0to0_Co_TCh; SetScale y 0,1e-09,"A", f0ch1w1_1_0to0_Co_TCh
803 */
804
805 double TOffset = atof(strtok(line+15,","));
806 if (isnan(hdr->CHANNEL[ns].TOffset))
807 hdr->CHANNEL[ns].TOffset = TOffset;
808 else if (fabs(hdr->CHANNEL[ns].TOffset - TOffset) > 1e-12)
809 fprintf(stderr,"Warning TOffsets in channel #%i do not match (%f,%f)", ns, hdr->CHANNEL[ns].TOffset, TOffset);
810
811 double dur = atof(strtok(NULL,","));
812 char *p = strchr(line,'"');
813 if (p != NULL) {
814 p++;
815 char *p2 = strchr(p,'"');
816 if (p2 != NULL) *p2=0;
817 dur *= PhysDimScale(PhysDimCode(p));
818 }
819
820 double div = spr / (hdr->SampleRate * dur);
821 if (ns==0) {
822 hdr->SampleRate = 1.0 / dur;
823 }
824 else if (hdr->SampleRate == 1.0 / dur)
825 ;
826 else if (div == floor(div)) {
827 hdr->SampleRate *= div;
828 }
829 }
830
831 else if (!strncmp(line,"X SetScale y,",13)) {
832 char *p = strchr(line,'"');
833 if (p!=NULL) {
834 p++;
835 char *p2 = strchr(p,'"');
836 if (p2!=NULL) *p2=0;
837 if (hdr->CHANNEL[ns].PhysDimCode == 0)
838 hdr->CHANNEL[ns].PhysDimCode = PhysDimCode(p);
839 else if (hdr->CHANNEL[ns].PhysDimCode != PhysDimCode(p)) {
840 flagSupported = 0; // physical units do not match
841 if (VERBOSE_LEVEL>7) fprintf(stdout,"[%s:%i] ITX (not supported): %i, %i,<%s> \n",__FILE__,__LINE__, hdr->CHANNEL[ns].PhysDimCode,PhysDimCode(p),p);
842 }
843 }
844 }
845 else if (!strncmp(line,"WAVES",5)) {
846
847 if (VERBOSE_LEVEL>7)
848 fprintf(stdout,"%s (line %i): <%s>#%i: %i/%i\n",__FILE__,__LINE__,line,(int)ns,(int)spr,(int)hdr->SPR);
849
850 char *p;
851 p = strrchr(line,'_');
852 ns = 0;
853 sweepNo = 0;
854 if (p != NULL) {
855 chanNo = strtol(p+1, NULL, 10);
856 if (chanNo > 0)
857 ns = chanNo - 1; // if decoding fails, assume there is only a single channel
858
859 p[0] = 0;
860
861 if (search_sweepnames(sweepname_list, line) == 0) {
862 // when sweep not found, add to list
863 sweepname_list = add_sweepnames(sweepname_list, line);
864 }
865 }
866
867 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) ITX (supported %i): %i, %i, %i, %i, %i\n",__FILE__,__LINE__, flagSupported, (int)ns, (int)spr, (int)hdr->SPR, chanNo, sweepNo);
868
869 if (ns >= hdr->NS) {
870 hdr->NS = ns+1;
871 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
872 }
873
874 if (VERBOSE_LEVEL>7)
875 fprintf(stdout,"%s (line %i): <%s>#%i: %i/%i\n",__FILE__,__LINE__,line,(int)ns,(int)spr,(int)hdr->SPR);
876
877 CHANNEL_TYPE* hc = hdr->CHANNEL + ns;
878 strncpy(hc->Label, line+6, MAX_LENGTH_LABEL);
879
880 if (VERBOSE_LEVEL>7)
881 fprintf(stdout,"%s (line %i): <%s>#%i: %i/%i\n",__FILE__,__LINE__,line,(int)ns,(int)spr,(int)hdr->SPR);
882
883 hc->OnOff = 1;
884 hc->GDFTYP = 17;
885 hc->DigMax = (double)(int16_t)(0x7fff);
886 hc->DigMin = (double)(int16_t)(0x8000);
887 hc->LeadIdCode = 0;
888
889 hc->Cal = 1.0;
890 hc->Off = 0.0;
891 hc->Transducer[0] = '\0';
892 hc->LowPass = NAN;
893 hc->HighPass = NAN;
894 hc->TOffset = NAN;
895 hc->PhysMax = hc->Cal * hc->DigMax;
896 hc->PhysMin = hc->Cal * hc->DigMin;
897 hc->PhysDimCode = 0;
898
899 // decode channel number and sweep number
900 if (VERBOSE_LEVEL>7)
901 fprintf(stdout,"%s (line %i): <%s>#%i: %i/%i\n",__FILE__,__LINE__,line,(int)ns,(int)spr,(int)hdr->SPR);
902
903 }
904 else if (flagData)
905 spr++;
906 }
907
908 if (VERBOSE_LEVEL>7)
909 fprintf(stdout,"%s (line %i): scanning %s,v%4.2f format (supported: %i)\n",__FILE__,__LINE__,GetFileTypeString(hdr->TYPE),hdr->VERSION,flagSupported);
910
911 if (!flagSupported) {
912 clear_sweepnames(sweepname_list);
913 biosigERROR(hdr, hdr->AS.B4C_ERRNUM,
914 "This ITX format is not supported. Possible reasons: not generated by Heka-Patchmaster, corrupted, physical units do not match between sweeps, or do not fulfil some other requirements");
915 return;
916 }
917
918 hdr->NRec = count_sweepnames(sweepname_list);
919 if (VERBOSE_LEVEL>7)
920 fprintf(stdout,"%s (line %i): [%i,%i,%i] = %i, %i\n",__FILE__,__LINE__,(int)hdr->NS,(int)hdr->SPR,(int)hdr->NRec,(int)hdr->NRec*hdr->SPR*hdr->NS, (int)hdr->AS.bpb);
921
922 hdr->EVENT.N = hdr->NRec - 1;
923 hdr->EVENT.SampleRate = hdr->SampleRate;
924 hdr->EVENT.POS = (uint32_t*) realloc(hdr->EVENT.POS, hdr->EVENT.N * sizeof(*hdr->EVENT.POS));
925 hdr->EVENT.TYP = (uint16_t*) realloc(hdr->EVENT.TYP, hdr->EVENT.N * sizeof(*hdr->EVENT.TYP));
926 hdr->EVENT.CHN = (uint16_t*) realloc(hdr->EVENT.CHN, hdr->EVENT.N * sizeof(*hdr->EVENT.CHN));
927 hdr->EVENT.DUR = (uint32_t*) realloc(hdr->EVENT.DUR, hdr->EVENT.N * sizeof(*hdr->EVENT.DUR));
928 #if (BIOSIG_VERSION >= 10500)
929 hdr->EVENT.TimeStamp = (gdf_time*)realloc(hdr->EVENT.TimeStamp, hdr->EVENT.N*sizeof(gdf_time));
930 #endif
931
932 hdr->NRec = hdr->SPR;
933 hdr->SPR = 1;
934 hdr->AS.first = 0;
935 hdr->AS.length = hdr->NRec;
936 hdr->AS.bpb = sizeof(double)*hdr->NS;
937 for (ns=0; ns < hdr->NS; ns++) {
938 hdr->CHANNEL[ns].SPR = hdr->SPR;
939 hdr->CHANNEL[ns].bi = sizeof(double)*ns;
940 }
941
942 double *data = (double*)realloc(hdr->AS.rawdata,hdr->NRec*hdr->SPR*hdr->NS*sizeof(double));
943 hdr->FILE.LittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN); // no swapping
944 hdr->AS.rawdata = (uint8_t*) data;
945
946 /*
947 reads and converts data into biosig structure
948 */
949 spr = 0;SPR = 0;
950 ifseek(hdr, 0, SEEK_SET);
951 c = ifgetc(hdr); // read first character
952 while (!ifeof(hdr)) {
953 int i = 0;
954 while ( (c != -1) && (c != 10) && (c != 13) && (i < IGOR_MAXLENLINE) ) {
955 // terminate when any line break occurs
956 line[i++] = c;
957 c = ifgetc(hdr);
958 };
959 line[i] = 0;
960 while (isspace(c)) c = ifgetc(hdr); // keep first non-space character of next line in buffer
961
962 if (!strncmp(line,"BEGIN",5)) {
963 flagData = 1;
964 spr = 0;
965 }
966 else if (!strncmp(line,"END",3)) {
967 flagData = 0;
968 if (chanNo+1 == hdr->NS) SPR += spr;
969 }
970 else if (!strncmp(line,"X SetScale y,",13)) {
971 //ns++;
972 }
973 else if (!strncmp(line,"WAVES",5)) {
974 // decode channel number and sweep number
975
976 chanNo = 0;
977 sweepNo= 0;
978 char *p;
979 p = strrchr(line,'_');
980 if (p != NULL) {
981 chanNo = strtol(p+1,NULL,10);
982 if (chanNo > 0) chanNo--; // if decoding fails, assume there is only a single channel.
983 p[0] = 0;
984 sweepNo = search_sweepnames(sweepname_list, line);
985 if (sweepNo > 0) sweepNo--; // if decoding fails, assume there is only a single sweep
986 }
987 spr = 0;
988 if (sweepNo > 0 && chanNo==0) {
989 hdr->EVENT.POS[sweepNo-1] = SPR;
990 hdr->EVENT.TYP[sweepNo-1] = 0x7ffe;
991 hdr->EVENT.DUR[sweepNo-1] = 0;
992 hdr->EVENT.CHN[sweepNo-1] = 0;
993 #if (BIOSIG_VERSION >= 10500)
994 hdr->EVENT.TimeStamp[sweepNo-1] = 0;
995 #endif
996 }
997 }
998 else if (flagData) {
999 double val = atof(line);
1000 data[hdr->NS*(SPR + spr) + chanNo] = val;
1001 spr++;
1002 }
1003 }
1004 clear_sweepnames(sweepname_list);
1005 hdr->EVENT.N = sweepNo;
1006
1007 if (VERBOSE_LEVEL>7)
1008 fprintf(stdout,"%s (line %i): reading %s,v%4.2f format finished \n",__FILE__,__LINE__,GetFileTypeString(hdr->TYPE),hdr->VERSION);
1009
1010 hdr->SPR = 1;
1011 hdr->NRec *= hdr->SPR;
1012 hdr->AS.first = 0;
1013 hdr->AS.length = hdr->NRec;
1014 hdr->AS.bpb = sizeof(double)*hdr->NS;
1015 #undef IGOR_MAXLENLINE
1016
1017 }
1018
1019
1020 #ifdef __cplusplus
1021 }
1022 #endif