Debian Codegraph @

biosig4c++/t210/sopen_alpha_read.c

raw symbols SCIP
1 /*
2
3 $Id: sopen_alpha_read.c,v 1.2 2009-02-12 16:15:17 schloegl Exp $
4 Copyright (C) 2005,2006,2007,2008,2009 Alois Schloegl <[email protected]>
5
6 This file is part of the "BioSig for C/C++" repository
7 (biosig4c++) at http://biosig.sf.net/
8
9 BioSig is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 3
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22
23 */
24
25
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "../biosig.h"
31
32 #define min(a,b) (((a) < (b)) ? (a) : (b))
33 #define max(a,b) (((a) > (b)) ? (a) : (b))
34
35 EXTERN_C void sopen_alpha_read(HDRTYPE* hdr) {
36 /*
37 this function will be called by the function SOPEN in "biosig.c"
38
39 Input:
40 char* Header // contains the file content
41
42 Output:
43 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
44 */
45 size_t count;
46 unsigned int k;
47 const char *FileName = hdr->FileName;
48
49 fprintf(stdout,"Warning: support for alpha format is just experimental.\n");
50
51 char* fn = (char*)malloc(strlen(hdr->FileName)+15);
52 strcpy(fn,hdr->FileName); // Flawfinder: ignore
53
54 const size_t bufsiz = 4096;
55 char buf[bufsiz];
56
57 // alpha.alp cal_res digin digvidtc eog marker measure mkdef montage rawdata rawhead report.txt r_info sleep
58
59 const char *f2 = "alpha.alp";
60 char *tmpstr = strrchr(fn,FILESEP);
61 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
62 else strcpy(fn,f2); // Flawfinder: ignore
63
64 FILE *fid = fopen(fn,"r"); count = fread(buf,1,bufsiz-1,fid); fclose(fid); buf[count]=0; // terminating 0 character
65 char *t = strtok(buf,"\xA\xD");
66 while (t) {
67
68 if (VERBOSE_LEVEL>7) fprintf(stdout,"0: %s \n",t);
69
70 if (!strncmp(t,"Version = ",9))
71 hdr->VERSION = atof(t+9);
72 else if (!strncmp(t,"Id = ",4)) {
73 strncpy(hdr->ID.Manufacturer._field,t+5,MAX_LENGTH_MANUF);
74 hdr->ID.Manufacturer.Name = hdr->ID.Manufacturer._field;
75 }
76 t = strtok(NULL,"\xA\xD");
77 }
78
79 f2 = "rawhead";
80 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
81 else strcpy(fn,f2); // Flawfinder: ignore
82
83 int Notch = 0;
84 int Bits = 0;
85 double DigMax=0, DigMin=0;
86 uint16_t gdftyp = 0;
87 int ns = 0;
88 fid = fopen(fn,"r"); count = fread(buf,1,bufsiz-1,fid); fclose(fid); buf[count]=0; // terminating 0 character
89 t = strtok(buf,"\xA\xD");
90 char STATUS = 1;
91 uint32_t *ChanOrder=NULL;
92 char **ChanType = NULL;
93 while ((t!=NULL) && (STATUS<9)) {
94 char *t1 = strchr(t,'=');
95
96 if (VERBOSE_LEVEL>7) fprintf(stdout,"<%6.2f> %i- %s | %s\n",hdr->VERSION, STATUS,t,t1);
97
98 if (t1) {
99 t1[-1] = 0; t1++;
100
101 if (STATUS == 1) {
102 if (!strcmp(t,"Version"))
103 hdr->VERSION = atof(t1);
104 else if (!strcmp(t,"BitsPerValue")) {
105 Bits = atoi(t1);
106 switch (Bits) {
107 case 12: gdftyp = 255+12;
108 // hdr->FILE.LittleEndian = 0;
109 DigMax = (1<<(Bits-1))-1;
110 DigMin = -(1<<(Bits-1));
111 break;
112 case 16: gdftyp = 3;
113 DigMax = 32752.0; //!!! that's the maximum value found in alpha-trace files
114 DigMin = -32736.0; //!!! that's the minimum value found in alpha-trace files
115 break;
116 case 32: gdftyp = 5; break;
117 DigMax = (1<<(Bits-1))-1;
118 DigMin = -(1<<(Bits-1));
119 }
120 }
121 else if (!strcmp(t,"ChanCount")) {
122 hdr->NS = atoi(t1);
123 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS*sizeof(CHANNEL_TYPE));
124 ChanOrder = (uint32_t*)calloc(hdr->NS,sizeof(uint32_t)*2);
125 ChanType = (char**)calloc(hdr->NS,sizeof(char*));
126
127 }
128 else if (!strcmp(t,"SampleFreq"))
129 hdr->SampleRate = atof(t1);
130 else if (!strcmp(t,"SampleCount")) {
131 hdr->NRec = atoi(t1);
132 hdr->SPR = 1;
133 }
134 else if (!strcmp(t,"NotchFreq"))
135 Notch = atof(t1);
136 else if (!strcmp(t,"DispFlags") && (hdr->VERSION < 411.89))
137 STATUS = 2;
138 else if (!strcmp(t,"Sec2Marker") && (hdr->VERSION > 411.89))
139 STATUS = 2;
140 }
141
142 else if (STATUS == 2) {
143 if (ns>=hdr->NS) {
144 ns = 0;
145 STATUS = 3;
146 }
147 else {
148 CHANNEL_TYPE *hc = hdr->CHANNEL+ns;
149 hc->GDFTYP = gdftyp;
150 hc->Notch = Notch;
151 hc->LeadIdCode = 0;
152 hc->SPR = hdr->SPR;
153 //hc->bi8 = GDFTYP_BITS[gdftyp]*ns;
154 hc->DigMax = DigMax;
155 hc->DigMin = DigMin;
156 hc->OnOff = 1;
157 hc->Cal = 1.0;
158 hc->Off = 0.0;
159 hc->PhysMax = hc->DigMax;
160 hc->PhysMin = hc->DigMin;
161 hc->Transducer[0] = 0;
162
163 strncpy(hc->Label, t, MAX_LENGTH_LABEL+1);
164 char* t2= strchr(t1,',');
165 t2[0] = 0; while (isspace((++t2)[0]));
166 char* t3= strchr(t2,',');
167 t3[0] = 0; while (isspace((++t3)[0]));
168 char* t4= strchr(t3,',');
169 t4[0] = 0; while (isspace((++t4)[0]));
170
171 ChanOrder[ns*2] = atoi(t2);
172 ChanOrder[ns*2+1] = ns;
173 ChanType[ns] = t3;
174 ns++;
175 }
176 }
177 else if (STATUS == 3) {
178 // decode information (filters, PhysDim, etc.) and assign to corresponding channels.
179 char *pd = NULL;
180 float tmp1, tmp2, HighPass, LowPass;
181 #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
182 sscanf(t1, "%f, %f, %f, %f, %as", &HighPass,&LowPass, &tmp1,&tmp2, &pd);
183 #else
184 sscanf(t1, "%f, %f, %f, %f, %ms", &HighPass,&LowPass, &tmp1,&tmp2, &pd);
185 #endif
186 strrchr(pd,',')[0]=0;
187 if (!strcmp(pd,"%%")) pd[1]=0;
188 uint16_t pdc = PhysDimCode(pd);
189 if (pd) free(pd);
190
191 char flag = 0;
192 for (k=0; k < hdr->NS; k++) {
193 if ((ChanType[k]!=NULL) && !strcmp(t,ChanType[k])) {
194 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
195 hc->PhysDimCode = pdc;
196 //strcpy(hc->PhysDim,pd);
197 hc->LowPass = LowPass;
198 hc->HighPass = HighPass;
199 ChanType[k] = NULL;
200 }
201 if (ChanType[k] != NULL) flag = 1; // not done yet
202 }
203 if (!flag) STATUS = 99; // done with channel definition
204 }
205 }
206 t = strtok(NULL,"\xA\xD");
207 }
208 hdr->AS.bpb8 = (GDFTYP_BITS[gdftyp]*hdr->NS);
209 // hdr->AS.bpb = (GDFTYP_BITS[gdftyp]*hdr->NS)>>3; // do not rely on this, because some bits can get lost
210
211 // sort channels
212 qsort(ChanOrder,hdr->NS,2*sizeof(uint32_t),&u32cmp);
213 for (k=0; k<hdr->NS; k++) {
214 hdr->CHANNEL[ChanOrder[2*k+1]].bi8 = GDFTYP_BITS[gdftyp]*k;
215 hdr->CHANNEL[ChanOrder[2*k+1]].bi = (GDFTYP_BITS[gdftyp]*k)>>3;
216 }
217 free(ChanOrder);
218 free(ChanType);
219
220
221 f2 = "cal_res";
222 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
223 else strcpy(fn,f2); // Flawfinder: ignore
224
225 fid = fopen(fn,"r");
226 if (fid!=NULL) {
227 if (VERBOSE_LEVEL>7) fprintf(stdout,"cal_res: \n");
228
229 count = fread(buf,1,bufsiz-1,fid); fclose(fid); buf[count]=0; // terminating 0 character
230 t = strtok(buf,"\xA\xD");
231 t = strtok(NULL,"\xA\xD"); // skip lines 1 and 2
232 /*
233 char label[MAX_LENGTH_LABEL+1];
234 char flag[MAX_LENGTH_LABEL+1];
235 double cal,off;
236 */
237 char *t0,*t1,*t2,*t3;
238 unsigned n=0; //
239 for (k=0; max(k,n)<hdr->NS; k++) {
240 t = strtok(NULL,"\xA\xD");
241
242 if (t==NULL) {
243 fprintf(stderr,"Warning SOPEN(alpha): scaling coefficients not defined for all channels\n");
244 break;
245 }
246
247 // strncpy(hc->Label,t,min(strcspn(t," =,"),MAX_LENGTH_LABEL));
248 t0 = strchr(t,'=');t0[0]=0;t0++;
249 int ix = strlen(t)-1;
250 while ((ix>0) && isspace(t[ix])) t[ix--] = 0; // remove trailing spaces
251 t1 = strchr(t0,',');t1[0]=0;t1++;
252 t2 = strchr(t1,',');t2[0]=0;t2++;
253 t3 = strchr(t2,',');t3[0]=0;t3++;
254
255 n = atoi(t); // n==0 if label is provided, n>0 if channel number is provided
256
257 /* does not work because ambiguous labels are used in rawhead and cal_res (e.g. T3 and T7)
258 if (!n) for (n=0; n<hdr->NS; n++) {
259 if (!strcmp(hdr->CHANNEL[n].Label,t))
260 { n++;
261 break;
262 }
263 }
264 */
265
266 if (VERBOSE_LEVEL>7) fprintf(stdout,"cal_res: %i %i <%s> %s %s %s\n",k,n,t,t1,t2,t3);
267
268 CHANNEL_TYPE *hc = hdr->CHANNEL + (n>0 ? n-1 : k); // channel can be denoted by label or number
269 hc->Cal = atof(t1);
270 hc->Off = 0;
271
272 if (VERBOSE_LEVEL>7) fprintf(stdout," <%s> %s = ###, %f, %f\n", t1,hc->Label,hc->Cal,hc->Off);
273
274 hc->PhysMax = (hc->DigMax - hc->Off) * hc->Cal;
275 hc->PhysMin = (hc->DigMin - hc->Off) * hc->Cal;
276 hc->XYZ[0]=0;
277 hc->XYZ[1]=0;
278 hc->XYZ[2]=0;
279 }
280 }
281
282 f2 = "r_info";
283 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
284 else strcpy(fn,f2); // Flawfinder: ignore
285
286 fid = fopen(fn,"r");
287 if (fid!=NULL) {
288
289 if (VERBOSE_LEVEL>7) fprintf(stdout,"r_info: \n");
290
291 count = fread(buf,1,bufsiz-1,fid); fclose(fid); buf[count]=0; // terminating 0 character
292 struct tm T;
293 t = strtok(buf,"\xA\xD");
294 t = strtok(NULL,"\xA\xD"); // skip line 1
295 while (t!=NULL) {
296 char *t1 = strchr(t,'=');
297 t1[0] = 0;
298 while (isspace((++t1)[0]));
299 for (k=strlen(t); (k>0) && isspace(t[--k]); t[k]=0);
300 for (k=strlen(t1); (k>0) && isspace(t1[--k]); t1[k]=0);
301
302 if (VERBOSE_LEVEL>7) fprintf(stdout,"r_info: %s = %s\n",t,t1);
303
304 if (0) {}
305 else if (!strcmp(t,"RecId"))
306 strncpy(hdr->ID.Recording,t1,MAX_LENGTH_RID);
307 else if (!strcmp(t,"RecDate")) {
308 sscanf(t1,"%02d.%02d.%04d",&T.tm_mday,&T.tm_mon,&T.tm_year);
309 T.tm_year -=1900;
310 T.tm_mon -=1;
311 }
312 else if (!strcmp(t,"RecTime"))
313 sscanf(t1,"%02d.%02d.%02d",&T.tm_hour,&T.tm_min,&T.tm_sec);
314 else if (!strcmp(t,"TechSal")) {
315 if (hdr->ID.Technician) free(hdr->ID.Technician);
316 hdr->ID.Technician = strdup(t1);
317 }
318 else if (!strcmp(t,"TechTitle") || !strcmp(t,"TechLast") || !strcmp(t,"TechFirst")) {
319 size_t l0 = strlen(hdr->ID.Technician);
320 size_t l1 = strlen(t1);
321 hdr->ID.Technician = (char*)realloc(hdr->ID.Technician,l0+l1+2);
322 hdr->ID.Technician[l0] = ' ';
323 strcpy(hdr->ID.Technician+l0+1, t1); // Flawfinder: ignore
324 }
325
326 t = strtok(NULL,"\xA\xD");
327 }
328 hdr->T0 = tm_time2gdf_time(&T);
329 }
330
331 f2 = "marker";
332 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
333 else strcpy(fn,f2); // Flawfinder: ignore
334 fid = fopen(fn,"r");
335 if (fid != NULL) {
336 size_t n,N;
337 N=0; n=0;
338 while (!feof(fid)) {
339 hdr->AS.auxBUF = (uint8_t*) realloc(hdr->AS.auxBUF,N+bufsiz+1);
340 N += fread(hdr->AS.auxBUF+N, 1, bufsiz, fid);
341 }
342 fclose(fid);
343 hdr->AS.auxBUF[N] = 0; // terminating 0 character
344
345 N = 0;
346 t = (char*)hdr->AS.auxBUF+strcspn((char*)hdr->AS.auxBUF,"\xA\xD");
347 t = t+strspn(t,"\xA\xD"); // skip lines 1 and 2
348 while (t[0]) {
349 char* t1 = t;
350 size_t l1 = strcspn(t1,"=");
351 size_t p2 = strspn(t1+l1,"= ")+l1;
352 char* t2 = t+p2;
353 size_t l2 = strcspn(t2," ,");
354 size_t p3 = strspn(t2+l2," ,")+l2;
355 char* t3 = t2+p3;
356 size_t l3 = strcspn(t3," ,");
357 size_t p4 = strspn(t3+l3," ,")+l3;
358 char* t4 = t3+p4;
359 size_t l4 = strcspn(t4,"\xA\xD");
360 size_t p5 = strspn(t4+l4,"\xA\xD")+l4;
361 t1[l1] = 0;
362 while (isspace(t1[--l1])) t1[l1]=0;
363 t2[l2] = 0;
364 t3[l3] = 0;
365 t4[l4] = 0;
366 t = t4 + p5;
367
368 if (n+1 >= N) {
369 const size_t sz = 100;
370 hdr->EVENT.TYP = (typeof(hdr->EVENT.TYP)) realloc(hdr->EVENT.TYP,(N+sz)*sizeof(*hdr->EVENT.TYP));
371 hdr->EVENT.POS = (typeof(hdr->EVENT.POS)) realloc(hdr->EVENT.POS,(N+sz)*sizeof(*hdr->EVENT.POS));
372 N += sz;
373 }
374
375 hdr->EVENT.POS[n] = atol(t3);
376 if (!strcmp(t1,"REC"))
377 hdr->EVENT.TYP[n] = 0x7ffe;
378 else if (!strcmp(t1,"MON"))
379 hdr->EVENT.TYP[n] = 0;
380 else if (!strcmp(t1,"TXT"))
381 FreeTextEvent(hdr, n, t4);
382 else
383 FreeTextEvent(hdr, n, t1);
384
385 if (!strcmp(t2,"off"))
386 hdr->EVENT.TYP[n] |= 0x8000;
387
388 //fprintf(stdout,"#%u, 0x%04x,%u | t1=<%s> = t2=<%s>, t3=<%s>, t4=<%s>\n",n,hdr->EVENT.TYP[n],hdr->EVENT.POS[n],t1,t2,t3,t4);
389
390 n++;
391 // t = strtok(NULL,"\xA\xD");
392
393 //fprintf(stdout," <%s>\n",t1);
394 }
395 hdr->EVENT.N = n;
396 hdr->EVENT.SampleRate = hdr->SampleRate;
397 // convert2to4_eventtable(hdr);
398 }
399
400 tmpstr = strrchr(fn,FILESEP);
401 tmpstr[0] = 0;
402 tmpstr = strrchr(fn,FILESEP);
403 f2 = "s_info";
404 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
405 else strcpy(fn,f2); // Flawfinder: ignore
406
407 fid = fopen(fn,"r");
408 if (fid!=NULL) {
409 count = fread(buf,1,bufsiz-1,fid); fclose(fid); buf[count]=0; // terminating 0 character
410 char *Lastname = NULL;
411 char *Firstname = NULL;
412 struct tm T;
413 t = strtok(buf,"\xA\xD");
414 t = strtok(NULL,"\xA\xD"); // skip line 1
415 while (t!=NULL) {
416 char *t1 = strchr(t,'=');
417 t1[0] = 0;
418 while (isspace((++t1)[0]));
419 for (k=strlen(t); (k>0) && isspace(t[--k]); t[k]=0);
420 for (k=strlen(t1); (k>0) && isspace(t1[--k]); t1[k]=0);
421
422 if (VERBOSE_LEVEL>7) fprintf(stdout,"s_info: <%s> = <%s>\n",t,t1);
423
424 if (0) {}
425 else if (!strcmp(t,"SubjId"))
426 strncpy(hdr->Patient.Id,t1,MAX_LENGTH_PID);
427 else if (!strcmp(t,"Gender"))
428 switch (t1[0]) {
429 case 'm':
430 case 'M':
431 hdr->Patient.Sex = 1; break;
432 case 'w':
433 case 'W':
434 case 'f':
435 case 'F':
436 hdr->Patient.Sex = 2; break;
437 default:
438 hdr->Patient.Sex = 0; break;
439 }
440 else if (!strcmp(t,"Handedness"))
441 switch (t1[0]) {
442 case 'r':
443 case 'R':
444 hdr->Patient.Handedness = 1; break;
445 case 'l':
446 case 'L':
447 hdr->Patient.Handedness = 2; break;
448 default:
449 hdr->Patient.Handedness = 0; break;
450 }
451 else if (!strcmp(t,"Size"))
452 hdr->Patient.Height = atof(t1);
453 else if (!strcmp(t,"Weight"))
454 hdr->Patient.Weight = atof(t1);
455 else if (!strcmp(t,"FirstName")) {
456 Firstname = t1;
457 }
458 else if (!strcmp(t,"LastName")) {
459 Lastname = t1;
460 }
461 else if (!strcmp(t,"BirthDay")) {
462 int c = sscanf(t1,"%02d.%02d.%04d",&T.tm_mday,&T.tm_mon,&T.tm_year);
463 T.tm_year -=1900;
464 T.tm_mon -=1;
465 T.tm_hour =12;
466 T.tm_min =0;
467 T.tm_sec =0;
468 if (c > 2) hdr->Patient.Birthday = tm_time2gdf_time(&T);
469 }
470 t = strtok(NULL,"\xA\xD");
471 }
472
473 size_t l0 = strlen(Firstname);
474 size_t l1 = strlen(Lastname);
475 if (l0+l1+1 <= MAX_LENGTH_NAME) {
476 strcpy(hdr->Patient.Name, Firstname); // Flawfinder: ignore
477 hdr->Patient.Name[l0] = ' ';
478 strcpy(hdr->Patient.Name + l0 + 1, Lastname); // Flawfinder: ignore
479 } else
480 strncpy(hdr->Patient.Name, Lastname, MAX_LENGTH_NAME+1); // Flawfinder: ignore
481
482 }
483
484 strcpy(fn,hdr->FileName); // Flawfinder: ignore
485 tmpstr = strrchr(fn,FILESEP);
486 f2 = "rawdata";
487 if (tmpstr) strcpy(tmpstr+1,f2); // Flawfinder: ignore
488 else strcpy(fn,f2); // Flawfinder: ignore
489
490 if (VERBOSE_LEVEL>7) fprintf(stdout,"rawdata11: %s \n",f2);
491
492 hdr->FileName = fn;
493 ifopen(hdr,"r");
494
495 if (VERBOSE_LEVEL>7) fprintf(stdout,"rawdata22: %s open=%i\n",f2,hdr->FILE.OPEN);
496 if (hdr->FILE.OPEN) {
497 int16_t a[3];
498 ifread(a, 2, 3, hdr);
499 hdr->VERSION = a[0];
500 hdr->HeadLen = 6;
501
502 switch (a[2]) {
503 case 12: gdftyp = 255+12; break;
504 case 16: gdftyp = 3; break;
505 case 32: gdftyp = 5; break;
506 }
507 for (k=a[1]; k<hdr->NS; k++)
508 hdr->CHANNEL[k].OnOff = 0;
509 for (k=0; k<hdr->NS; k++) {
510 hdr->CHANNEL[k].GDFTYP = gdftyp;
511 }
512
513 hdr->AS.bpb = (GDFTYP_BITS[gdftyp]*a[1])>>3;
514 hdr->FILE.POS = 0;
515
516 size_t len = (GDFTYP_BITS[gdftyp]*a[1]*hdr->NRec*hdr->SPR)>>3;
517 if ((GDFTYP_BITS[gdftyp]*a[1]) & 0x07) {
518 /* hack: if SPR*NS*bits are not a multiple of bytes,
519 hdr->AS.bpb would be non-integer causing some problems in SREAD reading the correct number of bytes.
520 This hack makes sure that all data is loaded.
521 */
522 len++;
523 }
524 hdr->AS.rawdata = (uint8_t*) realloc(hdr->AS.rawdata, len);
525 size_t count = ifread(hdr->AS.rawdata,1,len,hdr);
526 hdr->AS.first = 0;
527 hdr->AS.length = (count<<3)/(GDFTYP_BITS[gdftyp]*a[1]);
528 }
529
530 if (VERBOSE_LEVEL>7) fprintf(stdout,"rawdata55: %s c=%i [%i, %i] sizeof(CHAN)=%i\n",fn,(int)count,(int)hdr->AS.first,(int)hdr->AS.length,(int)sizeof(hdr->CHANNEL[0]));
531
532 free(fn);
533 hdr->FileName = FileName;
534 }
535