Debian Codegraph @

biosig4c++/t210/sopen_axg_read.c

raw symbols SCIP
1 /*
2 sopen_axg_read is a helper function to sopen, reading AXG data.
3
4 Copyright (C) 2008-2014 Alois Schloegl <[email protected]>
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 #define _GNU_SOURCE
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <iconv.h>
30
31 #if !defined(__APPLE__) && defined (_LIBICONV_H)
32 #define iconv libiconv
33 #define iconv_open libiconv_open
34 #define iconv_close libiconv_close
35 #endif
36
37
38 #include "../biosig-dev.h"
39
40 #define min(a,b) (((a) < (b)) ? (a) : (b))
41 #define max(a,b) (((a) > (b)) ? (a) : (b))
42
43 void sopen_axg_read(HDRTYPE* hdr) {
44
45 hdr->FILE.LittleEndian = 0;
46
47 // read whole file into RAM
48 size_t count = hdr->HeadLen;
49 while (!ifeof(hdr)) {
50 const int minsize = 1024;
51 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, 2*count+minsize);
52 count += ifread(hdr->AS.Header+count, 1, count+minsize, hdr);
53 }
54 ifclose(hdr);
55
56 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %i\n", __FILE__, __LINE__, hdr->CHANNEL, hdr->NS );
57
58 int32_t nCol;
59 switch ((int) hdr->VERSION) {
60 case 1:
61 case 2:
62 nCol = bei16p(hdr->AS.Header+6);
63 hdr->HeadLen = 8;
64 break;
65 case 3:
66 case 4:
67 case 5:
68 case 6:
69 nCol = bei32p(hdr->AS.Header+8);
70 hdr->HeadLen = 12;
71 break;
72 default:
73 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - unsupported version number");
74 return;
75 }
76
77 /* hack: for now each trace (i.e. column) becomes a separate channel -
78 later the traces of the channels will be reorganized
79 */
80 CHANNEL_TYPE *TEMPCHANNEL = (CHANNEL_TYPE*)malloc(nCol*sizeof(CHANNEL_TYPE));
81 char **ValLabel = (char**)malloc(nCol*sizeof(char*));
82 uint32_t *keyLabel = (uint32_t*)malloc(nCol*sizeof(uint32_t));
83
84 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %i\n", __FILE__, __LINE__, hdr->CHANNEL, hdr->NS );
85
86 /******* read all column/traces ******/
87 int32_t k;
88 uint8_t *pos = hdr->AS.Header+hdr->HeadLen;
89 hdr->SPR = beu32p(pos);
90 switch ((int) hdr->VERSION) {
91 case 1:
92 for (k = 0; k < nCol; k++) {
93 CHANNEL_TYPE *hc = TEMPCHANNEL+k;
94 hc->GDFTYP = 16; //float
95 hc->PhysDimCode = 0;
96 hc->SPR = beu32p(pos);
97
98 int strlen = pos[4]; // string in Pascal format
99 if (strlen > 79) {
100 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - invalid title length ");
101 return;
102 }
103
104 /* Organize channels */
105 uint32_t i;
106 for (i = 0; i < hdr->NS; i++) {
107 // check if channel with same title already exists
108 if (!memcmp(ValLabel[hdr->NS], pos+4, strlen)) {
109 keyLabel[k] = hdr->NS;
110 break;
111 }
112 }
113 if (i==hdr->NS) {
114 // in case of new title, add another channel
115 ValLabel[hdr->NS] = (char*)pos+4;
116 keyLabel[k] = hdr->NS;
117 hdr->NS++;
118 }
119
120 // start of data section
121 hc->bufptr = pos+84;
122 pos += 84 + hc->SPR * sizeof(float);
123
124 }
125 break;
126 case 2:
127 for (k = 0; k < nCol; k++) {
128 CHANNEL_TYPE *hc = TEMPCHANNEL+k;
129 hc->GDFTYP = 3; // int16
130 hc->PhysDimCode = 0;
131 hc->SPR = beu32p(pos);
132 if (k==0) {
133 hc->Off = bef32p(pos+84);
134 hc->Cal = bef32p(pos+88);
135 hc->bufptr = NULL;
136 hdr->SampleRate = 1.0 / hc->Cal;
137 }
138 else {
139 hc->Cal = bef32p(pos+84);
140 hc->Off = 0.0;
141 hc->bufptr = pos + 88;
142 }
143 int strlen = pos[4]; // string in Pascal format
144 if (strlen > 79) {
145 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - invalid title length ");
146 return;
147 }
148
149 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - version 2 not supported yet ");
150 return;
151
152 // start of data sectioB
153 pos += (k==0 ? 92 : 88 + hc->SPR * sizeof(int16_t) );
154
155 }
156 break;
157 case 6:
158 for (k=0; k < nCol; k++) {
159
160 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %i\n", __FILE__, __LINE__, hdr->CHANNEL, k );
161
162 CHANNEL_TYPE *hc = TEMPCHANNEL+k;
163 hc->SPR = beu32p(pos);
164 uint32_t datatype = beu32p(pos+4);
165 size_t titleLen = beu32p(pos+8);
166 char *inbuf = (char*)pos + 12;
167 hc->bufptr = pos + 12 + titleLen;
168
169 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %i %i %i\n", __FILE__, __LINE__, (int)datatype, (int)titleLen, (int)hc->SPR);
170 /*
171 // The only types used for data file columns are...
172 // ShortArrayType = 4 IntArrayType = 5
173 // FloatArrayType = 6 DoubleArrayType = 7
174 // SeriesArrayType = 9 ScaledShortArrayType = 10
175 */
176 hc->Cal = 1.0;
177 hc->Off = 0.0;
178 hc->GDFTYP = datatype; //TEMPCHANNEL.GDFTYP uses a different encoding than standard GDFTYP
179 hc->OnOff = 1;
180 switch (datatype) {
181 case 4: // int16
182 hc->GDFTYP=3; break;
183 case 5: // int32
184 hc->GDFTYP=5; break;
185 case 6: // float32
186 hc->GDFTYP=16; break;
187 case 7: // double
188 hc->GDFTYP=17; break;
189 case 9: hc->GDFTYP = 17; // series
190 // double firstval = bef64p(hc->bufptr);
191 double increment = bef64p(hc->bufptr+8);
192 hc->bufptr = NULL;
193 hc->OnOff = 0;
194 if (!memcmp(inbuf,"\0T\0i\0m\0e\0 \0(\0s\0)\0",8)) {
195 hdr->SampleRate = 1.0/increment;
196 //hc->OnOff = 2; // time axis
197 }
198 else {
199 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "AXG: series data not being a Time axis is not supported. ");
200 return;
201 }
202 break;
203 case 10: // scaled short
204 hc->Cal = bef64p(hc->bufptr);
205 hc->Off = bef64p(hc->bufptr+8);
206 break;
207 default:
208 hc->OnOff = 0;
209 }
210
211 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %i\n", __FILE__, __LINE__, hdr->CHANNEL, k );
212 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i): %i %i %i %i\n", __FILE__, __LINE__, (int)hc->SPR, (int)datatype, (int)titleLen, (int)(pos-hdr->AS.Header) );
213
214 /* Organize channels
215 find number of channels and
216 setup data structure that assignes each column to a channel
217 ValLabel contains the different Labels - one for each channel
218 keyLabel contains the channel number for the corresponding column
219 */
220 uint32_t i;
221 for (i = 0; i < hdr->NS; i++) {
222 // check if channel with same title already exists
223 uint32_t prevTitleLen = beu32p((uint8_t*)(ValLabel[i])-4);
224 if ((titleLen == prevTitleLen) && !memcmp(ValLabel[i], pos+12, titleLen)) {
225 keyLabel[k] = i;
226 break;
227 }
228 }
229 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %i\n", __FILE__, __LINE__, hdr->CHANNEL, k );
230 if (i==hdr->NS) {
231 // in case of new title, add another channel
232 ValLabel[hdr->NS] = (char*)pos+12; // pointer to title of channel 'nLabel', length of title is stored in beu32p(pos+8)
233 keyLabel[k] = hdr->NS;
234 hdr->NS++;
235 }
236
237 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %i\n", __FILE__, __LINE__, hdr->CHANNEL, k );
238 // pointer to data sections
239 hc->bufptr = pos + 12 + titleLen;
240
241
242 // move pos to the starting position of the next column
243 pos += 12 + titleLen;
244 // position of next column
245 switch (datatype) {
246 case 4:
247 pos += hc->SPR * sizeof(int16_t);
248 break;
249 case 5: //int32
250 case 6: //float
251 pos += hc->SPR * 4;
252 break;
253 case 7:
254 pos += hc->SPR * sizeof(double);
255 break;
256 case 9:
257 pos += 2 * sizeof(double);
258 break;
259 case 10:
260 pos += 2 * sizeof(double) + hc->SPR * sizeof(int16_t);
261 break;
262 default:
263 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"error reading AXG: unsupported data type");
264 }
265 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i): %i %i %i %i\n", __FILE__, __LINE__, (int)hc->SPR, (int)datatype, (int)titleLen, (int)(pos-hdr->AS.Header) );
266
267 }
268 break;
269 default:
270 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG version is not supported");
271 }
272
273 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) %p %p %i %i\n", __FILE__, __LINE__, TEMPCHANNEL, hdr->CHANNEL, (int)hdr->NS , (int)sizeof(CHANNEL_TYPE));
274
275 /* convert columns/traces into channels */
276 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
277 uint32_t ns;
278 for (ns=0; ns < hdr->NS; ns++) {
279 CHANNEL_TYPE *hc = hdr->CHANNEL + ns;
280 hc->SPR = 0;
281 hc->GDFTYP = 0;
282 hc->OnOff = 1;
283 }
284 size_t EventN = 0;
285 hdr->SPR = 0;
286
287 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i) NS=%i nCol=%i\n", __FILE__, __LINE__, hdr->NS, nCol );
288
289 int flag_traces_of_first_sweep_done=0;
290 for (k=0; k < nCol; k++) {
291 /*
292 copy essential parameters ´(GDFTYP, OnOff, Cal, Off) from TEMPCHANNEL
293 and keep track of the number of samples SPR for each channel
294 */
295
296 // define GDFTYP, Cal, Off
297 ns = keyLabel[k]; // channel number for current column
298 CHANNEL_TYPE *hc = hdr->CHANNEL + ns;
299
300 hc->SPR += TEMPCHANNEL[k].SPR;
301
302 switch (TEMPCHANNEL[k].GDFTYP) {
303 case 4: // int16
304 if (hc->GDFTYP < 3) hc->GDFTYP = 3;
305 break;
306 case 5: // int32
307 if (hc->GDFTYP < 5) hc->GDFTYP = 5;
308 break;
309 case 6: // float32
310 if (hc->GDFTYP < 16) hc->GDFTYP = 16;
311 break;
312 case 7: // double
313 if (hc->GDFTYP < 17) hc->GDFTYP = 17;
314 break;
315 case 10: hc->GDFTYP = 3; // scaled short
316 if (hc->GDFTYP < 16) hc->GDFTYP = 16;
317 break;
318 default:
319 hc->OnOff = 0;
320 }
321
322 if (!flag_traces_of_first_sweep_done) {
323 hc->Cal = TEMPCHANNEL[k].Cal;
324 hc->Off = TEMPCHANNEL[k].Off;
325 }
326 else {
327 if (hc->Cal != TEMPCHANNEL[k].Cal || hc->Off != TEMPCHANNEL[k].Off) {
328 // in case input is scaled short, output shoud be float
329 hc->GDFTYP = max(16, hc->GDFTYP);
330 }
331 }
332
333 if (hdr->SPR < hc->SPR) hdr->SPR = hc->SPR;
334
335 if (ns+1 == hdr->NS) {
336 flag_traces_of_first_sweep_done = 1;
337 // if current column corresponds to last channel, ...
338 // check if all traces of the same sweep have the same length, and ...
339 for (ns=0; ns < hdr->NS; ns++) {
340 CHANNEL_TYPE *hc = hdr->CHANNEL + ns;
341 if (hc->OnOff != 1) continue;
342 else if (hdr->SPR != hc->SPR) {
343 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i): SPR=%d #%dspr=%d \n", __FILE__, __LINE__, hdr->SPR, ns, hc->SPR );
344 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - SPR differs between channel");
345 }
346 }
347
348 // ... add segment break in event table.
349 if ( hdr->EVENT.N + 1 >= EventN ) {
350 EventN += max(EventN, 16);
351 hdr->EVENT.POS = (uint32_t*)realloc(hdr->EVENT.POS, EventN * sizeof(*hdr->EVENT.POS));
352 hdr->EVENT.TYP = (uint16_t*)realloc(hdr->EVENT.TYP, EventN * sizeof(*hdr->EVENT.TYP));
353 }
354 hdr->EVENT.TYP[hdr->EVENT.N] = 0x7ffe;
355 hdr->EVENT.POS[hdr->EVENT.N] = hdr->SPR;
356 hdr->EVENT.N++;
357 }
358 }
359 hdr->EVENT.N--; // ignore last separator event
360
361 hdr->NRec = hdr->SPR;
362 hdr->SPR = 1;
363 uint32_t bi8 = 0;
364 for (ns=0; ns < hdr->NS; ns++) {
365 CHANNEL_TYPE *hc = hdr->CHANNEL+ns;
366 hc->bi8 = bi8;
367 hc->bi = bi8/8;
368 if (hc->OnOff != 1)
369 hc->SPR = 0;
370 else
371 bi8 += GDFTYP_BITS[hc->GDFTYP];
372 }
373 hdr->AS.bpb = bi8/8;
374
375 for (ns=0; ns < hdr->NS; ns++) {
376 CHANNEL_TYPE *hc = hdr->CHANNEL+ns;
377
378 // define hdr->channel[.].Label, hdr->channel[.].PhysDim
379 if (hdr->Version <= 2) {
380 // PascalToCString(ValLabel[ns]); // shift by 1 byte and terminate 0 char
381 int strlen = min(ValLabel[ns][0],MAX_LENGTH_LABEL);
382 strncpy(hc->Label, (ValLabel[ns])+1, strlen);
383
384 char *u1 = strrchr(ValLabel[ns],'(');
385 char *u2 = strrchr(ValLabel[ns],')');
386 if (u1 != NULL && u2 != NULL && u1 < u2) {
387 *u1 = 0;
388 *u2 = 0;
389 hc->PhysDimCode = PhysDimCode(u1+1);
390 }
391 }
392 else if (hdr->Version <= 6) {
393 char *inbuf = ValLabel[ns];
394 size_t inlen = beu32p((uint8_t*)(ValLabel[ns])-4);
395 char *outbuf = hc->Label;
396 size_t outlen = MAX_LENGTH_LABEL+1;
397 #if defined(_ICONV_H) || defined (_LIBICONV_H) || defined(_ICONV_H_)
398 iconv_t ICONV = iconv_open("UTF-8","UCS-2BE");
399 size_t reticonv = iconv(ICONV, &inbuf, &inlen, &outbuf, &outlen);
400 iconv_close(ICONV);
401
402 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i): %i %i %i %"PRIiPTR"\n", __FILE__, __LINE__, (int)hc->SPR, (int)inlen, (int)(pos-hdr->AS.Header), reticonv );
403
404 if (reticonv == (size_t)(-1) ) {
405 perror("AXG - conversion of title failed!!!");
406 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - conversion of title failed");
407 return;
408 }
409 *outbuf=0;
410 #else
411 ++inbuf;
412 int i = min(MAX_LENGTH_LABEL, titleLen/2);
413 for (; i>0 ; i-- ) {
414 *outbuf= *inbuf;
415 inbuf += 2;
416 outbuf++;
417 }
418 outbuf = 0;
419 #endif
420 char *u1 = strrchr(hc->Label,'(');
421 char *u2 = strrchr(hc->Label,')');
422 if (u1 != NULL && u2 != NULL && u1 < u2) {
423 *u1 = 0;
424 *u2 = 0;
425 hc->PhysDimCode = PhysDimCode(u1+1);
426 }
427 }
428
429 // these might be reorganized below
430 hc->DigMax = 1e9;
431 hc->DigMin = -1e9;
432 hc->PhysMax = hc->DigMax;
433 hc->PhysMin = hc->DigMin;
434
435 hc->LeadIdCode = 0;
436 hc->Transducer[0] = 0;
437
438 hc->Cal = 1.0;
439 hc->Off = 0.0;
440
441 hc->TOffset = 0;
442 hc->HighPass = NAN;
443 hc->LowPass = NAN;
444 hc->Notch = NAN;
445 hc->Impedance = INFINITY;
446 hc->fZ = NAN;
447 hc->XYZ[0] = 0.0;
448 hc->XYZ[1] = 0.0;
449 hc->XYZ[2] = 0.0;
450 }
451
452 hdr->AS.rawdata = (uint8_t*)realloc( hdr->AS.rawdata, hdr->AS.bpb*hdr->SPR*hdr->NRec);
453
454 for (ns=0; ns < hdr->NS; ns++) {
455 CHANNEL_TYPE *hc = hdr->CHANNEL + ns;
456 hc->SPR = 0;
457 }
458 for (k=0; k < nCol; k++) {
459
460 ns = keyLabel[k];
461 CHANNEL_TYPE *hc = hdr->CHANNEL + ns;
462
463 if (hc->OnOff != 1) continue;
464
465 uint32_t i;
466 switch (hc->GDFTYP) {
467 case 3:
468 assert(TEMPCHANNEL[k].GDFTYP==4);
469 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
470 *(int16_t*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bei16p(TEMPCHANNEL[k].bufptr + i*2);
471 }
472 break;
473 case 5:
474 switch (TEMPCHANNEL[k].GDFTYP) {
475 case 4:
476 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
477 *(int32_t*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bei16p(TEMPCHANNEL[k].bufptr + i*2);
478 }
479 break;
480 case 5:
481 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
482 *(int32_t*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bei32p(TEMPCHANNEL[k].bufptr + i*4);
483 }
484 break;
485 default:
486 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - data conversion not supported ");
487 return;
488 }
489 break;
490 case 16:
491 switch (TEMPCHANNEL[k].GDFTYP) {
492 case 4:
493 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
494 *(float*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = (float)bei16p(TEMPCHANNEL[k].bufptr + i*2);
495 }
496 break;
497 case 5:
498 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
499 *(float*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = (float)bei32p(TEMPCHANNEL[k].bufptr + i*4);
500 }
501 break;
502 case 6:
503 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
504 *(float*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bef32p(TEMPCHANNEL[k].bufptr + i*4);
505 }
506 break;
507 case 10:
508 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
509 *(float*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bei16p(TEMPCHANNEL[k].bufptr + i*2) * TEMPCHANNEL[k].Cal + TEMPCHANNEL[k].Off;
510 }
511 break;
512 default:
513 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - data conversion not supported ");
514 return;
515 }
516 break;
517 case 17:
518 switch (TEMPCHANNEL[k].GDFTYP) {
519 case 4:
520 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
521 *(double*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = (double)bei16p(TEMPCHANNEL[k].bufptr + i*2);
522 }
523 break;
524 case 5:
525 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
526 *(double*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = (double)bei32p(TEMPCHANNEL[k].bufptr + i*4);
527 }
528 break;
529 case 6:
530 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
531 *(double*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = (double)bef32p(TEMPCHANNEL[k].bufptr + i*4);
532 }
533 break;
534 case 7:
535 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
536 *(double*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bef64p(TEMPCHANNEL[k].bufptr + i*8);
537 }
538 break;
539 case 10:
540 for (i=0; i < TEMPCHANNEL[k].SPR; i++) {
541 *(double*)(hdr->AS.rawdata + hc->bi + (hc->SPR + i) * hdr->AS.bpb) = bei16p(TEMPCHANNEL[k].bufptr + i*2) * TEMPCHANNEL[k].Cal + TEMPCHANNEL[k].Off;
542 }
543 break;
544 default:
545 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - data conversion not supported ");
546 return;
547 }
548 break;
549 default:
550 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - unsupported target data type");
551 return;
552 }
553 hc->SPR += TEMPCHANNEL[k].SPR;
554 }
555
556 for (ns=0; ns < hdr->NS; ns++) {
557 CHANNEL_TYPE *hc = hdr->CHANNEL + ns;
558 hc->SPR = hdr->SPR;
559 }
560 // free intermediate data structure to reorganized column/trace to channels
561 if(TEMPCHANNEL) free(TEMPCHANNEL);
562 if(keyLabel) free(keyLabel);
563 if(ValLabel) free(ValLabel);
564
565 // data is stored on hdr->AS.rawdata in such a way that swapping must not be applied
566 hdr->FILE.LittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN);
567 hdr->AS.first = 0;
568 hdr->AS.length = (size_t)hdr->NRec;
569
570 // read Comments
571 size_t szComments = beu32p(pos);
572 char *inbuf = (char*)pos+4;
573 char *Comments = malloc(szComments+1);
574 char *outbuf = Comments;
575 size_t outlen = szComments+1;
576 size_t inlen = szComments;
577
578 iconv_t ICONV = iconv_open("UTF-8","UCS-2BE");
579 size_t reticonv = iconv(ICONV, &inbuf, &inlen, &outbuf, &outlen);
580 iconv_close(ICONV);
581 if (reticonv == (size_t)(-1) ) {
582 perror("AXG - conversion of comments failed!!!");
583 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - conversion of comments failed");
584 return;
585 }
586 Comments[outlen]=0;
587
588 if (VERBOSE_LEVEL >7)
589 fprintf(stdout,"\n=== COMMENT === \n %s\n",Comments);
590 pos += 4+szComments;
591
592
593 // read Notes
594 size_t szNotes = beu32p(pos);
595 inbuf = (char*)pos+4;
596 char *Notes = malloc(szNotes+1);
597 outbuf = Notes;
598 outlen = szNotes+1;
599 inlen = szNotes;
600
601 ICONV = iconv_open("UTF-8","UCS-2BE");
602 reticonv = iconv(ICONV, &inbuf, &inlen, &outbuf, &outlen);
603 iconv_close(ICONV);
604 if ( reticonv == (size_t)(-1) ) {
605 biosigERROR(hdr,B4C_FORMAT_UNSUPPORTED,"AXG - conversion of Notes failed");
606 return;
607 }
608 Notes[outlen]=0;
609
610 if (VERBOSE_LEVEL >7)
611 fprintf(stdout,"=== NOTES === \n %s\n",Notes);
612 pos += 4+szNotes;
613
614 /****** parse Date and Time ********/
615 struct tm T;
616 #ifdef __GLIBC__
617 strptime(strstr(Notes,"Created on ")+11, "%a %b %d %Y", &T);
618 strptime(strstr(Notes,"acquisition at ")+15, "%T", &T);
619 hdr->T0 = tm_time2gdf_time(&T);
620 #else
621 char DATE[30];
622 strncpy(DATE, strstr(Notes,"Created on ")+11, 30);
623 DATE[29] = 0;
624 strtok(DATE, "\n\r"); // cut at newline
625 char *tmp = strtok(DATE, " "); // day of week - skip
626
627 tmp = strtok(NULL, " "); // abreviated month name
628 T.tm_mon = month_string2int(tmp);
629
630 tmp = strtok(NULL, " "); // day of month
631 T.tm_mday = atoi(tmp);
632
633 tmp = strtok(NULL
634
635 , " "); // year
636 T.tm_year = atoi(tmp) - 1900;
637
638 strncpy(DATE, strstr(Notes,"acquisition at ")+15, 9);
639 DATE[9] = 0;
640 tmp = strtok(DATE, " :");
641 T.tm_hour = atoi(tmp);
642 tmp = strtok(NULL, " :");
643 T.tm_min = atoi(tmp);
644 tmp = strtok(NULL, " :");
645 T.tm_sec = atoi(tmp);
646
647 hdr->T0 = tm_time2gdf_time(&T);
648
649 #endif
650
651 hdr->AS.fpulse = Notes;
652 free(Comments);
653
654 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %i)\n", __FILE__, __LINE__ );
655
656 }
657
658
659