Debian Codegraph @

biosig4c++/t210/sopen_cfs_read.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2010-2019 Alois Schloegl <[email protected]>
4
5 This file is part of the "BioSig for C/C++" repository
6 (biosig4c++) at http://biosig.sourceforge.io/
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 #include <assert.h>
25 #include <ctype.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "../biosig.h"
30
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
32
33 uint16_t cfs_data_type(uint8_t dataType) {
34 switch (dataType) {
35 case 0: //int8
36 return 1;
37 case 1: //uint8
38 return 3;
39 case 2: //int16
40 return 3;
41 case 3: // uint16
42 return 4;
43 case 4: // int32
44 return 5;
45 case 5: //float
46 return 16;
47 case 6: // double
48 return 17;
49 case 7: // char
50 return 0;
51 default:
52 return 0xffff;
53 }
54 }
55
56
57 /*
58 trim_trailing_space:
59 trims trailing whitespaces from pascal string (pstr), and the string
60 becomes always 0-terminated. This may mean that the last string character
61 might be deleted, if all bytes are filled with non-zero values.
62
63 no memory allocation or reallication is performed, maxLength+1 is
64 the size of the memory allocation for pstr (pstr[0] to pstr[maxLength]).
65 maxLength specify the string length w/o count or terminating zero byte.
66
67 pascal string: first byte contains lengths, followed by characters, not null-terminated
68 */
69 char *trim_trailing_space(uint8_t *pstr, unsigned maxLength) {
70 unsigned len = min(pstr[0], maxLength);
71 while (isspace(pstr[len]) && (len>0)) {
72 len--;
73 }
74
75 if (len==maxLength) fprintf(stdout,"Warning %s: last and %i-th character of string <%c%c%c%c...> has been deleted\n",__func__,len,pstr[1],pstr[2],pstr[3],pstr[4]);
76
77 len = min(len+1,maxLength);
78 pstr[len]=0;
79 pstr[0]=len;
80
81 return (pstr+1);
82 }
83
84
85 const char *Signal6_StateTable[]={
86 "","State 1","State 2","State 3","State 4","State 5","State 6","State 7","State 8","State 9",
87 "State 10","State 11","State 12","State 13","State 14","State 15","State 16","State 17","State 18","State 19",
88 "State 20","State 21","State 22","State 23","State 24","State 25","State 26","State 27","State 28","State 29",
89 "State 30","State 31","State 32","State 33","State 34","State 35","State 36","State 37","State 38","State 39",
90 "State 40","State 41","State 42","State 43","State 44","State 45","State 46","State 47","State 48","State 49",
91 "State 50","State 51","State 52","State 53","State 54","State 55","State 56","State 57","State 58","State 59",
92 "State 60","State 61","State 62","State 63","State 64","State 65","State 66","State 67","State 68","State 69",
93 "State 70" };
94
95 EXTERN_C void sopen_cfs_read(HDRTYPE* hdr) {
96 /*
97 this function will be called by the function SOPEN in "biosig.c"
98
99 Input:
100 char* Header // contains the file content
101
102 Output:
103 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
104 */
105
106 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s:%i sopen_cfs_read started - %i bytes already loaded\n",__FILE__,__LINE__,hdr->HeadLen);
107
108 #define H1LEN (8+14+4+8+8+2+2+2+2+2+4+2+2+74+4+40)
109
110 size_t count = hdr->HeadLen;
111 char flag_FPulse = 0; // indicates whether data was recorded using FPulse
112
113 #define CFS_NEW // this flag allows to switch back to old version
114
115 while (!ifeof(hdr)) {
116 hdr->AS.Header = (uint8_t*) realloc(hdr->AS.Header,count*2+1);
117 count += ifread(hdr->AS.Header+count,1,count,hdr);
118 }
119 hdr->AS.Header[count] = 0;
120 hdr->FLAG.OVERFLOWDETECTION = 0;
121 hdr->FILE.LittleEndian = 1;
122
123 /*
124 Implementation is based on the following reference:
125 CFS - The CED Filing System October 2006
126 */
127
128 /* General Header */
129 // uint32_t filesize = leu32p(hdr->AS.Header+22); // unused
130 hdr->FILE.size = leu32p(hdr->AS.Header+0x16); // file size
131 hdr->NS = leu16p(hdr->AS.Header+0x2a); // 6 number of channels
132 uint16_t n = leu16p(hdr->AS.Header+0x2c); // 7 number of file variables
133 uint16_t d = leu16p(hdr->AS.Header+0x2e); // 8 number of data section variables
134 uint16_t FileHeaderSize = leu16p(hdr->AS.Header+0x30); // 9 byte size of file header
135 uint16_t DataHeaderSize = leu16p(hdr->AS.Header+0x32); // 10 byte size of data section header
136 uint32_t LastDataSectionHeaderOffset = leu32p(hdr->AS.Header+0x34); // 11 last data section header offset
137 uint16_t NumberOfDataSections = leu16p(hdr->AS.Header+0x38); // 12 last data section header offset
138
139 // decode start time and date
140 {
141 struct tm t;
142 char strtmp[9];
143 memcpy(strtmp, hdr->AS.Header+0x1a, 8);
144 strtmp[8] = 0; // terminating null character
145 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s:%i sopen_cfs_read started [%s]\n",__FILE__,__LINE__,strtmp);
146
147 t.tm_hour = atoi(strtok(strtmp,":/"));
148 t.tm_min = atoi(strtok(NULL,":/"));
149 t.tm_sec = atoi(strtok(NULL,":/"));
150 memcpy(strtmp, hdr->AS.Header+0x22, 8);
151 t.tm_mday = atoi(strtok(strtmp,"-:/"));
152 t.tm_mon = atoi(strtok(NULL,"-:/"))-1;
153 t.tm_year = atoi(strtok(NULL,"-:/"));
154 if (t.tm_year<39) t.tm_year+=100;
155 hdr->T0 = tm_time2gdf_time(&t);
156 }
157
158 if (NumberOfDataSections) {
159 hdr->EVENT.TYP = (typeof(hdr->EVENT.TYP)) realloc(hdr->EVENT.TYP, (hdr->EVENT.N + 2*NumberOfDataSections - 1) * sizeof(*hdr->EVENT.TYP));
160 hdr->EVENT.POS = (typeof(hdr->EVENT.POS)) realloc(hdr->EVENT.POS, (hdr->EVENT.N + 2*NumberOfDataSections - 1) * sizeof(*hdr->EVENT.POS));
161 hdr->EVENT.TimeStamp = (typeof(hdr->EVENT.TimeStamp)) realloc(hdr->EVENT.TimeStamp, (hdr->EVENT.N + 2*NumberOfDataSections - 1) * sizeof(*hdr->EVENT.TimeStamp));
162 }
163
164 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) CFS - %d,%d,%d,0x%x,0x%x,0x%x,%d,0x%x\n",__FILE__,__LINE__,hdr->NS,n,d,FileHeaderSize,DataHeaderSize,LastDataSectionHeaderOffset,NumberOfDataSections,leu32p(hdr->AS.Header+0x86));
165
166 /* channel information */
167 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
168 #define H2LEN (22+10+10+1+1+2+2)
169 char* H2 = (char*)(hdr->AS.Header + H1LEN);
170 double xPhysDimScale[100]; // CFS is limited to 99 channels
171 typeof(hdr->NS) NS = hdr->NS;
172 uint8_t k;
173 uint32_t bpb=0;
174 for (k = 0; k < hdr->NS; k++) {
175 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
176 /*
177 1 offset because CFS uses pascal type strings (first byte contains string length)
178 in addition, the strings are \0 terminated.
179 */
180 hc->OnOff = 1;
181 hc->LeadIdCode = 0;
182 hc->bi8 = 0;
183
184 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i) Channel #%i/%i: %i<%s>/%i<%s>\n",__FILE__,__LINE__,k+1, hdr->NS, H2[22 + k*H2LEN], H2 + 23 + k*H2LEN, H2[32 + k*H2LEN], H2 + 33 + k*H2LEN);
185
186 strncpy(hc->Label, trim_trailing_space(H2 + k*H2LEN, 20), 21); // Channel name
187 hc->PhysDimCode = PhysDimCode (trim_trailing_space(H2 + 22 + k*H2LEN,9)); // Y axis units
188 xPhysDimScale[k] = PhysDimScale(PhysDimCode(trim_trailing_space(H2 + 32 + k*H2LEN,9))); // X axis units
189
190 uint8_t dataType = H2[42 + k*H2LEN];
191 uint8_t dataKind = H2[43 + k*H2LEN];
192 //uint16_t byteSpace = leu16p(H2+44 + k*H2LEN); // stride
193 //uint16_t next = leu16p(H2+46 + k*H2LEN);
194 hc->GDFTYP = cfs_data_type(dataType);
195 if (hc->GDFTYP > 580) // sizeof GDFTYP_BITS[]
196 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "CFS: unknown/unsupported data type !");
197
198 if (H2[43 + k * H2LEN] == 1) {
199 // Matrix data does not return an extra channel, but contains Markers and goes into the event table.
200 hc->OnOff = 0;
201 NS--;
202 }
203 hc->bi = bpb;
204 bpb += GDFTYP_BITS[hc->GDFTYP]>>3; // per single sample
205 hc->Transducer[0] = '\0';
206 hc->Impedance = NAN;
207 hc->TOffset = NAN;
208 hc->LowPass = NAN;
209 hc->HighPass = NAN;
210 hc->Notch = NAN;
211 hc->XYZ[0] = NAN;
212 hc->XYZ[1] = NAN;
213 hc->XYZ[2] = NAN;
214
215 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(line %i) Channel #%i: [%s](%i/%i) <%s>/<%s> ByteSpace%i,Next#%i\n",
216 __FILE__, __LINE__, k+1, H2 + 1 + k*H2LEN, dataType, dataKind,
217 H2 + 23 + k*H2LEN, H2 + 33 + k*H2LEN, leu16p(H2+44+k*H2LEN), leu16p(H2+46+k*H2LEN));
218
219 }
220 hdr->AS.bpb = bpb;
221
222 size_t datapos = H1LEN + H2LEN*hdr->NS;
223
224 /* file variable information :
225 will be extracted as application specific information
226 // n*36 bytes
227 */
228 if (VERBOSE_LEVEL>7) fprintf(stdout,"\n******* file variable information (n=%i) *********\n", n);
229 hdr->AS.bci2000= realloc(hdr->AS.bci2000, 3);
230 hdr->AS.bci2000[0]='\0';
231 for (k = 0; k < n; k++) {
232 int i=-1; double f=NAN;
233 size_t pos = datapos + k*36;
234 char *desc = (char*)(hdr->AS.Header+pos+1);
235 uint16_t typ = leu16p(hdr->AS.Header+pos+22);
236 char *unit = (char*)(hdr->AS.Header+pos+25);
237 uint16_t off = leu16p(hdr->AS.Header+pos+34);
238
239 // if (flag_FPulse && !strcmp(desc, "Spare")) continue;
240
241 /*
242 H1LEN General Header
243 H2LEN*NS Channel Information a 48 byte
244 n*36 File Variable information a 36 byte
245 d*36 DS Variable information
246 */
247 size_t p3 = H1LEN + H2LEN*hdr->NS + (n+d+2)*36 + off;
248 /***** file variable k *****/
249 switch (typ) {
250 case 0:
251 case 1: i = hdr->AS.Header[p3]; break;
252 case 2: i = lei16p(hdr->AS.Header+p3); break;
253 case 3: i = leu16p(hdr->AS.Header+p3); break;
254 case 4: i = lei32p(hdr->AS.Header+p3); break;
255 case 5: f = lef32p(hdr->AS.Header+p3); break;
256 case 6: f = lef64p(hdr->AS.Header+p3); break;
257 }
258 if (VERBOSE_LEVEL>8) {
259 fprintf(stdout,"#%2i [%i:%i] <%s>",k,typ,off,desc);
260 if (typ<1) ;
261 else if (typ<5) fprintf(stdout,"[%d]",i);
262 else if (typ<7) fprintf(stdout,"[%g]",f);
263 else if (typ==7) fprintf(stdout,"[%s]",hdr->AS.Header+p3+1);
264 fprintf(stdout,"[%s]\n",unit);
265 }
266 else if (VERBOSE_LEVEL>7)
267 {
268 if (typ==7) fprintf(stdout,"#%2i [%i:] <%s> <%s> <%s>\n",k,typ,desc, hdr->AS.Header+p3+1,unit);
269 }
270
271 if ((typ==7) && !strncmp(desc,"Script",6)) {
272 char *scriptline=(char*)hdr->AS.Header+p3+1;
273 if (!strncmp(desc,"ScriptBlock",11)) {
274 // only the script block is extracted
275 assert(hdr->AS.Header[p3]==strlen(scriptline));
276 assert(hdr->AS.Header[p3+1+hdr->AS.Header[p3]]==0);
277
278 // replace '\r' <CR> with '\n' <NEWLINE> in scriptline
279 while (*scriptline) {
280 switch (*scriptline) {
281 case '\r': *scriptline='\n';
282 }
283 scriptline++;
284 }
285
286 scriptline=(char*)hdr->AS.Header+p3+1;
287 hdr->AS.bci2000=realloc(hdr->AS.bci2000, strlen(hdr->AS.bci2000) + strlen(scriptline) + 1);
288 strcat(hdr->AS.bci2000, scriptline); // Flawfinder: ignore
289 }
290 else if (!strncmp(desc,"Scriptline",10)) {
291 hdr->AS.bci2000=realloc(hdr->AS.bci2000, strlen(hdr->AS.bci2000) + strlen(scriptline) + 3);
292 strcat(hdr->AS.bci2000, scriptline); // Flawfinder: ignore
293 strcat(hdr->AS.bci2000, "\n"); // Flawfinder: ignore
294 }
295 }
296 if (k==0) {
297 // File variable zero
298 flag_FPulse = !strcmp(unit,"FPulse");
299
300 if (typ==2)
301 hdr->VERSION = i/100.0;
302 else
303 fprintf(stderr,"Warning (%s line %i): File variable zeros is not of type INT2\n",__func__,__LINE__);
304
305 char *e = (char*)&(hdr->ID.Equipment);
306 memcpy(e,unit,8);
307 lei16a(i,e+6);
308 }
309 }
310 hdr->ID.Manufacturer.Name = "CED - Cambridge Electronic Devices";
311 hdr->ID.Manufacturer.Model = NULL;
312 hdr->ID.Manufacturer.Version = NULL;
313 hdr->ID.Manufacturer.SerialNumber = NULL;
314
315 if (VERBOSE_LEVEL>7) fprintf(stdout,"\n******* Data Section variable information (n=%i,%i)*********\n", d,NumberOfDataSections);
316 datapos = LastDataSectionHeaderOffset; //H1LEN + H2LEN*hdr->NS + n*36;
317 // reverse order of data sections
318 uint32_t *DATAPOS = (uint32_t*)malloc(sizeof(uint32_t)*NumberOfDataSections);
319
320 uint16_t m;
321 for (m = NumberOfDataSections; 0 < m; ) {
322 DATAPOS[--m] = datapos;
323 datapos = leu32p(hdr->AS.Header + datapos);
324 if (VERBOSE_LEVEL>7) fprintf(stdout, "%s:%i sopen_cfs_read started: section %"PRIi16" pos %"PRIuPTR" 0x%"PRIxPTR"\n",__FILE__,__LINE__,m,datapos,datapos);
325 }
326
327 if (hdr->AS.SegSel[0] > NumberOfDataSections) {
328 fprintf(stderr,"Warning loading CFS file: selected sweep number is larger than number of sweeps [%d,%d] - no data is loaded\n",hdr->AS.SegSel[0], NumberOfDataSections);
329 NumberOfDataSections = 0;
330 }
331 else if (0 < hdr->AS.SegSel[0]) {
332 // hack: if sweep is selected, use same method than for data with a single sweep
333 DATAPOS[0] = DATAPOS[hdr->AS.SegSel[0]-1];
334 NumberOfDataSections = 1;
335 }
336
337 // void *VarChanInfoPos = hdr->AS.Header + datapos + 30; // unused
338 char flag_ChanInfoChanged = 0;
339 hdr->NRec = NumberOfDataSections;
340 int Signal6_CodeDescLen = 0;
341 size_t SPR = 0, SZ = 0;
342 for (m = 0; m < NumberOfDataSections; m++) {
343 int32_t tmp_event_typ=0;
344 double tmp_event_pos=0;
345
346 datapos = DATAPOS[m];
347 for (k = 0; k < d; k++) {
348 /***** DS variable k information *****/
349 int i=-1; double f=NAN;
350 size_t pos = H1LEN + H2LEN*hdr->NS + (n+k+1)*36;
351 char *desc = (char*)(hdr->AS.Header+pos+1);
352 uint16_t typ = leu16p(hdr->AS.Header+pos+22);
353 int unitlen = (uint8_t)hdr->AS.Header[pos+24];
354 char *unit = (char*)(hdr->AS.Header+pos+25);
355 uint16_t off = leu16p(hdr->AS.Header+pos+34);
356
357 if (flag_FPulse && !strcmp(desc, "Spare")) continue;
358
359 /***** data section variable k *****/
360 size_t p3 = DATAPOS[m] + 30 + 24 * hdr->NS + off;
361
362 switch (typ) {
363 case 0:
364 case 1: i = hdr->AS.Header[p3]; break;
365 case 2: i = lei16p(hdr->AS.Header+p3); break;
366 case 3: i = leu16p(hdr->AS.Header+p3); break;
367 case 4: i = lei32p(hdr->AS.Header+p3); break;
368 case 5: f = lef32p(hdr->AS.Header+p3); break;
369 case 6: f = lef64p(hdr->AS.Header+p3); break;
370 }
371
372 if ((k==1) && (typ==4) && !strcmp(desc,"State")) {
373 tmp_event_typ = i;
374 if ((Signal6_CodeDescLen < i) && (i < 256))
375 Signal6_CodeDescLen = i;
376 }
377 else if ((k==3) && (typ==6) && !strcmp(desc,"Start") && (unitlen>0) && !strncmp(unit,"s",unitlen))
378 tmp_event_pos = f;
379
380 if (VERBOSE_LEVEL>7) {
381 fprintf(stdout,"#%2i [%i:%i] <%s>",k,typ,off,desc);
382 if (typ<1) ;
383 else if (typ<5) fprintf(stdout,"[%d]",i);
384 else if (typ<7) fprintf(stdout,"[%g]",f);
385 else if (typ==7) fprintf(stdout,"[%s]",hdr->AS.Header+p3+1);
386 fprintf(stdout,"[%s]\n",unit);
387 }
388 }
389
390 typeof (hdr->T0) TS = tmp_event_pos ? (hdr->T0 + ldexp(tmp_event_pos/(24.0*3600.0),32)) : 0;
391 if (m>0) {
392 hdr->EVENT.TYP[hdr->EVENT.N] = 0x7ffe;
393 hdr->EVENT.POS[hdr->EVENT.N] = SPR;
394 hdr->EVENT.TimeStamp[hdr->EVENT.N] = TS;
395 hdr->EVENT.N++;
396 }
397 if (tmp_event_typ > 0) {
398 hdr->EVENT.TYP[hdr->EVENT.N] = tmp_event_typ;
399 hdr->EVENT.POS[hdr->EVENT.N] = SPR;
400 hdr->EVENT.TimeStamp[hdr->EVENT.N] = TS;
401 hdr->EVENT.N++;
402 }
403
404 if (!leu32p(hdr->AS.Header+datapos+8)) continue; // empty segment
405
406 if (VERBOSE_LEVEL>7) fprintf(stdout,"\n******* DATA SECTION --%03i-- %i *********\n",m,flag_ChanInfoChanged);
407 if (VERBOSE_LEVEL>7) fprintf(stdout,"\n[DS#%3i] 0x%x 0x%x [0x%x 0x%x szChanData=%i] 0x02%x\n", m, FileHeaderSize, \
408 (int)datapos, leu32p(hdr->AS.Header+datapos), leu32p(hdr->AS.Header+datapos+4), \
409 leu32p(hdr->AS.Header+datapos+8), leu16p(hdr->AS.Header+datapos+12));
410
411 uint32_t sz = 0;
412 uint32_t bpb = 0, spr = 0;
413 hdr->AS.first = 0;
414 hdr->AS.length = 0;
415 char flag_firstchan = 1;
416 uint32_t xspr0 = 0;
417
418 for (k = 0; k < hdr->NS; k++) {
419 uint8_t *pos = hdr->AS.Header + datapos + 30 + 24 * k;
420
421 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
422
423 // uint32_t bi = leu32p(pos); // unused
424 uint32_t xspr = leu32p(pos+4);
425 double Cal = lef32p(pos+8);
426 double Off = lef32p(pos+12);
427 double XCal = lef32p(pos+16);
428 double XOff = lef32p(pos+20);// unused
429 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %i/%i %i/%i %f+%f change SPR:%i->%i, Cal:%f->%f, Off: %f->%f\n", \
430 __FILE__,__LINE__, (int)m, (int)NumberOfDataSections, (int)k,(int)hdr->NS, XCal, XOff, \
431 (int)hc->SPR,(int)xspr,hc->Cal,Cal,hc->Off,Off);
432
433 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) [%i %i] %i %i %p %i\n",
434 __FILE__, __LINE__, m, k, xspr0, xspr, pos, (int)(datapos + 30 + 24 * k));
435
436 if (xspr0 == 0)
437 xspr0 = xspr;
438 else if (xspr>0)
439 xspr0 = lcm(xspr0,xspr);
440 else if (xspr==0 && hc->OnOff)
441 if (VERBOSE_LEVEL>7) fprintf(stdout,"Warning : #%i of section %i contains %i samples (%d)\n", (int)k, (int)m, (int)xspr, hc->OnOff);
442
443 if (m > 0) {
444 if ( (hc->Cal != Cal)
445 || (hc->Off != Off)
446 )
447 switch (hc->GDFTYP) {
448 case 0:
449 case 1:
450 case 2:
451 case 3:
452 case 4:
453 case 5:
454 case 6:
455 case 16:
456 hc->GDFTYP = 16; // float, single precision
457 hc->Cal = 1.0;
458 hc->Off = 0.0;
459 break;
460 case 7:
461 case 8:
462 case 17:
463 hc->GDFTYP = 17; // double
464 hc->Cal = 1.0;
465 hc->Off = 0.0;
466 break;
467 case 18: // quadruple
468 default: ;
469 fprintf(stderr, "ERROR: datatype %d in #%i of section %i not supported\n", hc->GDFTYP, (int)k, (int)m);
470 }
471 }
472 else {
473 hc->Cal = Cal;
474 hc->Off = Off;
475 }
476
477 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %i #%i: SPR=%i=%i=%i x%f+-%f %i x%g %g %g\n",
478 __func__,__LINE__,m,k,spr,(int)SPR,hc->SPR,hc->Cal,hc->Off,hc->bi,xPhysDimScale[k], XCal, XOff);
479
480 double Fs = 1.0 / (xPhysDimScale[k] * XCal);
481 if ( (hc->OnOff == 0) || (XCal == 0.0) ) {
482 ; // do nothing:
483 }
484 else if (flag_firstchan) {
485 hdr->SampleRate = Fs;
486 flag_firstchan = 0;
487 }
488 else if (fabs(hdr->SampleRate - Fs) > 1e-3) {
489 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: different sampling rates are not supported\n");
490 }
491
492 sz += hc->SPR * GDFTYP_BITS[hc->GDFTYP] >> 3;
493 hc->bi = bpb;
494 bpb += GDFTYP_BITS[hc->GDFTYP]>>3; // per single sample
495 hdr->AS.length += hc->SPR;
496
497 }
498
499 SPR += xspr0;
500 SZ += sz;
501 hdr->AS.bpb = bpb;
502 }
503 if (Signal6_CodeDescLen > 0) {
504
505 if (Signal6_CodeDescLen > 70) {
506 // 19 is sizeof Signal6_StateTable
507 fprintf(stderr, "Warning %s (line %i): Event code description exceed table (Signal6_CodeDescLen=%i)\n",__func__,__LINE__,Signal6_CodeDescLen);
508 Signal6_CodeDescLen = 70;
509 }
510
511 hdr->EVENT.LenCodeDesc = Signal6_CodeDescLen+1; //70 is sizeof(Signal6_StateTable)
512 hdr->EVENT.CodeDesc = realloc(hdr->EVENT.CodeDesc, sizeof(char*) * (Signal6_CodeDescLen+1));
513 for (int k=0; k <= Signal6_CodeDescLen; k++) {
514 hdr->EVENT.CodeDesc[k] = Signal6_StateTable[k];
515 }
516 }
517
518 /*
519 The previous and the next loop have been split, in order to avoid
520 multiple reallocations. Multiple reallocations seem to be a major
521 issue for performance problems of Windows (a 50 MB file with 480 sweeps took 50 s to load).
522 However, the two loops might contain some redundant operations.
523 */
524 hdr->AS.flag_collapsed_rawdata = 0;
525 void *ptr = realloc(hdr->AS.rawdata, hdr->AS.bpb * SPR);
526 if (!ptr) biosigERROR(hdr,B4C_MEMORY_ALLOCATION_FAILED, "CFS: memory allocation failed in line __LINE__");
527 hdr->AS.rawdata = (uint8_t*)ptr;
528
529 SPR = 0, SZ = 0;
530 for (m = 0; m < NumberOfDataSections; m++) {
531
532 datapos = DATAPOS[m];
533 if (!leu32p(hdr->AS.Header+datapos+8)) continue; // empty segment
534
535 uint32_t sz = 0;
536 uint32_t bpb = 0, spr = 0;
537 hdr->AS.first = 0;
538 hdr->AS.length = 0;
539 char flag_firstchan = 1;
540 uint32_t xspr0 = 0;
541 for (k = 0; k < hdr->NS; k++) {
542 uint8_t *pos = hdr->AS.Header + datapos + 30 + 24 * k;
543
544 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
545
546 //uint32_t bi = leu32p(pos); // unused
547 uint32_t xspr = leu32p(pos+4);
548 hc->SPR = leu32p(pos+4);
549 double XCal = lef32p(pos+16);
550 double XOff = lef32p(pos+20);// unused
551
552 if (xspr0 == 0)
553 xspr0 = xspr;
554 else if (xspr>0)
555 xspr0 = lcm(xspr0, xspr);
556 else if (xspr==0 && hc->OnOff)
557 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s line %d: Warning : #%i of section %i contains %i samples (OnOff=%d)\n", __FILE__, __LINE__, (int)k, (int)m, (int)xspr, hc->OnOff);
558
559 assert( (xspr==0) || ((xspr0 % xspr) == 0) );
560
561 double Fs = 1.0 / (xPhysDimScale[k] * XCal);
562 if ( (hc->OnOff == 0) || (XCal == 0.0) ) {
563 ; // do nothing:
564 }
565 else if (flag_firstchan) {
566 hdr->SampleRate = Fs;
567 flag_firstchan = 0;
568 }
569 else if (fabs(hdr->SampleRate - Fs) > 1e-3) {
570 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: different sampling rates are not supported\n");
571 }
572
573 sz += xspr * GDFTYP_BITS[hc->GDFTYP] >> 3;
574 hc->bi = bpb;
575 bpb += GDFTYP_BITS[hc->GDFTYP]>>3; // per single sample
576 hdr->AS.length += xspr;
577
578 }
579
580 if (xspr0 > 0) {
581 /* hack: copy data into a single block (rawdata)
582 this is used as a data cache, no I/O is needed in sread, at the cost that sopen is slower
583 sread_raw does not attempt to reload the data
584 */
585
586 hdr->AS.first = 0;
587 uint8_t ns = 0;
588 for (k = 0; k < hdr->NS; k++) {
589 uint8_t *pos = hdr->AS.Header + datapos + 30 + 24 * k;
590 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
591
592 uint32_t memoffset = leu32p(pos);
593 uint8_t *srcaddr = hdr->AS.Header + leu32p(hdr->AS.Header+datapos + 4) ;
594 //uint16_t byteSpace = leu16p(H2+44 + k*H2LEN);
595
596 double Cal = lef32p(pos+8);
597 double Off = lef32p(pos+12);
598 uint8_t dataType = H2[42 + k*H2LEN];
599 uint8_t dataKind = H2[43 + k*H2LEN]; // equidistant, Subsidiary or Matrix data
600 uint16_t stride = leu16p(H2+44 + k*H2LEN); // byteSpace
601 uint16_t next = leu16p(H2+46 + k*H2LEN);
602 uint16_t gdftyp = cfs_data_type(dataType);
603 if (gdftyp > 580) // sizeof GDFTYP_BITS[]
604 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "CFS: unknown/unsupported data type !");
605
606 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) #%i %i,%i %i %i %i %i: %i @%p %i\n",
607 __func__, __LINE__, k, dataType, dataKind, hc->SPR, gdftyp,hc->GDFTYP, stride, memoffset,
608 srcaddr, leu32p(hdr->AS.Header+datapos + 4) + leu32p(hdr->AS.Header + datapos + 30 + 24 * k));
609
610 if (hc->OnOff==0) continue; // Time and Marker channels are not supported, yet
611
612 if ((hc->SPR > 0) && (xspr0 != hc->SPR)) {
613 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: samples-per-record (SPR) changes between channels - this is not supported yet.");
614 }
615
616 size_t k2;
617 if (gdftyp == hc->GDFTYP && Cal==hc->Cal && Off==hc->Off) {
618 for (k2 = 0; k2 < xspr0; k2++) {
619
620 uint8_t *ptr = srcaddr + memoffset + k2*stride;
621 uint8_t *dest = hdr->AS.rawdata + hc->bi + (SPR + k2) * hdr->AS.bpb;
622 double val=NAN;
623
624 switch (gdftyp) {
625 // reorder for performance reasons - more frequent gdftyp's come first
626 case 3: //val = lei16p(ptr);
627 case 4: //val = leu16p(ptr);
628 memcpy(dest, ptr, 2);
629 break;
630 case 16: //val = lef32p(ptr);
631 memcpy(dest, ptr, 4);
632 break;
633 case 7: //val = lei64p(ptr);
634 case 8: //val = leu64p(ptr);
635 case 17: //val = lef64p(ptr);
636 memcpy(dest, ptr, 8);
637 break;
638 case 0: //val = *( char*) ptr;
639 case 1: //val = *( int8_t*) ptr;
640 case 2: //val = *(uint8_t*) ptr;
641 *dest = *ptr;
642 break;
643 case 5: val = lei32p(ptr);
644 case 6: //val = leu32p(ptr);
645 memcpy(dest, ptr, 4);
646 break;
647 default:
648 val = NAN;
649 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: invalid data type");
650 }
651
652 if (hc->OnOff) continue;
653
654 if (!strncmp(hc->Label,"Marker",6) && hc->PhysDimCode==2176 && hc->GDFTYP==5 && next != 0) {
655 // matrix data might contain time markers.
656
657 // memory allocation for additional events - more efficient implementation would be nice
658 hdr->EVENT.TYP = (typeof(hdr->EVENT.TYP)) realloc(hdr->EVENT.TYP, (hdr->EVENT.N + NumberOfDataSections) * sizeof(*hdr->EVENT.TYP));
659 hdr->EVENT.POS = (typeof(hdr->EVENT.POS)) realloc(hdr->EVENT.POS, (hdr->EVENT.N + NumberOfDataSections) * sizeof(*hdr->EVENT.POS));
660
661 /*
662 char Desc[2]; Desc[0] = srcaddr[hdr->CHANNEL[next].bi + k2*stride]; Desc[1] = 0;
663 this does currently not work because FreeTextEvent expects that
664 the string constant is available as long as hdr, which is not the case here.
665 */
666
667 // typically a single character within a 4 byte integer, this should be sufficient to ensure \0 termination
668 char *Desc = (char*)srcaddr + hdr->CHANNEL[next].bi + k2 * stride;
669 Desc[1] = 0;
670 FreeTextEvent(hdr, hdr->EVENT.N, Desc);
671 hdr->EVENT.POS[hdr->EVENT.N] = lround( (val * hc->Cal + hc->Off) * hdr->SampleRate) + SPR;
672 hdr->EVENT.N++;
673 }
674 }
675 }
676 else {
677 for (k2 = 0; k2 < xspr0; k2++) {
678
679 uint8_t *ptr = srcaddr + memoffset + k2*stride;
680 uint8_t *dest = hdr->AS.rawdata + hc->bi + (SPR + k2) * hdr->AS.bpb;
681 double val;
682
683 if (hc->GDFTYP==16) { // float
684 switch (gdftyp) {
685 // reorder for performance reasons - more frequent gdftyp's come first
686 case 3: val = lei16p(ptr) * Cal + Off;
687 break;
688 case 4: val = leu16p(ptr) * Cal + Off;
689 break;
690 case 16: val = lef32p(ptr) * Cal + Off;
691 break;
692 case 7: val = lei64p(ptr) * Cal + Off;
693 break;
694 case 8: val = leu64p(ptr) * Cal + Off;
695 break;
696 case 17: val = lef64p(ptr) * Cal + Off;
697 break;
698 case 0: val = (*( char*) ptr) * Cal + Off;
699 break;
700 case 1: val = (*( int8_t*) ptr) * Cal + Off;
701 break;
702 case 2: val = (*(uint8_t*) ptr) * Cal + Off;
703 break;
704 case 5: val = lei32p(ptr) * Cal + Off;
705 break;
706 case 6: val = leu32p(ptr) * Cal + Off;
707 break;
708 default:
709 val = NAN;
710 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: invalid data type");
711 }
712
713 lef32a(val, dest); // hdr->AS.rawdata uses the native endian format of the platform
714 }
715 else {
716 // any conversion to anything but float is not supported yet.
717 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: data type changes between segments - this is not supported yet.");
718 }
719
720 // scaling has been already consider when converting to float, do not scale in sread anymore.
721 hc->Cal = 1.0;
722 hc->Off = 0.0;
723 if (hc->OnOff) continue;
724
725 if (!strncmp(hc->Label,"Marker",6) && hc->PhysDimCode==2176 && hc->GDFTYP==5 && next != 0) {
726 // matrix data might contain time markers.
727
728 // memory allocation for additional events - more efficient implementation would be nice
729 hdr->EVENT.TYP = (typeof(hdr->EVENT.TYP)) realloc(hdr->EVENT.TYP, (hdr->EVENT.N + NumberOfDataSections) * sizeof(*hdr->EVENT.TYP));
730 hdr->EVENT.POS = (typeof(hdr->EVENT.POS)) realloc(hdr->EVENT.POS, (hdr->EVENT.N + NumberOfDataSections) * sizeof(*hdr->EVENT.POS));
731
732 /*
733 char Desc[2]; Desc[0] = srcaddr[hdr->CHANNEL[next].bi + k2*stride]; Desc[1] = 0;
734 this does currently not work because FreeTextEvent expects that
735 the string constant is available as long as hdr, which is not the case here.
736 */
737
738 // typically a single character within a 4 byte integer, this should be sufficient to ensure \0 termination
739 char *Desc = (char*)srcaddr + hdr->CHANNEL[next].bi + k2 * stride;
740 Desc[1] = 0;
741 FreeTextEvent(hdr, hdr->EVENT.N, Desc);
742 hdr->EVENT.POS[hdr->EVENT.N] = lround( (val * hc->Cal + hc->Off) * hdr->SampleRate) + SPR;
743 hdr->EVENT.N++;
744 }
745 }
746 }
747 ns += hc->OnOff;
748 }
749 }
750
751 SPR += xspr0;
752 SZ += sz;
753
754 datapos = leu32p(hdr->AS.Header + datapos);
755 }
756 free(DATAPOS);
757
758 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): SPR=%i=%i NRec=%i @%p\n",__FILE__,__LINE__,(int)SPR,hdr->SPR,(int)hdr->NRec, hdr->AS.rawdata);
759
760 // set variables such that sread_raw does not attempt to reload the data
761 hdr->AS.first = 0;
762 hdr->EVENT.SampleRate = hdr->SampleRate;
763 if (NumberOfDataSections < 1) {
764 hdr->SPR = 0;
765 }
766 else {
767 hdr->SPR = 1;
768 hdr->NRec = SPR;
769 hdr->AS.length = SPR;
770
771 size_t bpb = 0;
772 for (k = 0; k < hdr->NS; k++) {
773 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
774 assert(hc->bi==bpb);
775 bpb += GDFTYP_BITS[hc->GDFTYP] >> 3;
776 hc->SPR = hdr->SPR;
777 }
778 assert(hdr->AS.bpb==bpb);
779 }
780
781 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): SPR=%i=%i NRec=%i\n",__func__,__LINE__,(int)SPR,hdr->SPR,(int)hdr->NRec);
782
783 datapos = FileHeaderSize; //+DataHeaderSize;
784
785 if (flag_ChanInfoChanged) {
786 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "CED/CFS: varying channel information not supported");
787 }
788
789 for (k = 0; k < hdr->NS; k++) {
790 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
791 switch (hc->GDFTYP) {
792 case 0:
793 case 1:
794 hc->DigMax = 127;
795 hc->DigMin = -128;
796 break;
797 case 2:
798 hc->DigMax = 255;
799 hc->DigMin = 0;
800 break;
801 case 3:
802 hc->DigMax = (int16_t)0x7fff;
803 hc->DigMin = (int16_t)0x8000;
804 break;
805 case 4:
806 hc->DigMax = 0xffff;
807 hc->DigMin = 0;
808 break;
809 case 16:
810 case 17:
811 hc->DigMax = 1e9;
812 hc->DigMin = -1e9;
813 break;
814 }
815 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (%i): %f %f %f %f %f %f\n",__FILE__,__LINE__,hc->PhysMax, hc->DigMax, hc->Cal, hc->Off,hc->PhysMin, hc->DigMin );
816 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
817 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
818 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (%i): %f %f %f %f %f %f\n",__FILE__,__LINE__,hc->PhysMax, hc->DigMax, hc->Cal, hc->Off,hc->PhysMin, hc->DigMin );
819 }
820
821 #undef H1LEN
822 }
823
824
825
826 /*****************************************************************************
827
828 SOPEN_SMR_READ for reading SON/SMR files
829
830
831 son.h, sonintl.h are used just for understanding the file format.
832 and are not needed for the core functionality.
833
834
835 TODO:
836 Currently waveform data is supported,
837 events and marker information is ignored
838
839
840 *****************************************************************************/
841
842 #if defined(WITH_SON)
843 #include "sonintl.h"
844 // defines __SONINTL__
845 #else
846 #define ADCdataBlkSize 32000
847
848 #endif
849
850
851 EXTERN_C void sopen_smr_read(HDRTYPE* hdr) {
852 /*TODO: implemnt SON/SMR format */
853 fprintf(stdout,"SOPEN: Support for CED's SMR/SON format is under construction \n");
854
855 size_t count = hdr->HeadLen;
856 if (count < 512) {
857 hdr->HeadLen = 512;
858 // make sure fixed header (first 512 bytes) are read
859 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header,hdr->HeadLen+1);
860 count += ifread(hdr->AS.Header+count, 1, hdr->HeadLen-count, hdr);
861 hdr->AS.Header[count]=0;
862 }
863
864 // get Endianity, Version and Header size
865 hdr->FILE.LittleEndian = (*(uint16_t*)(hdr->AS.Header+38) == 0); // 0x0000: little endian, 0x0101: big endian
866 if (hdr->FILE.LittleEndian) {
867 hdr->VERSION = leu16p(hdr->AS.Header);
868 hdr->HeadLen = leu32p(hdr->AS.Header + 26 ); // first data
869 } else {
870 hdr->VERSION = beu16p(hdr->AS.Header);
871 hdr->HeadLen = beu32p(hdr->AS.Header + 26); // first data
872 // TODO: relax this restriction
873 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SON/SMR: big-endian file not supported,yet");
874 }
875
876 size_t HeadLen = hdr->HeadLen;
877 size_t size_factor = 1;
878 if (hdr->VERSION >= 9) {
879 size_factor = 512;
880 HeadLen *= size_factor;
881 if (sizeof(size_t) <= 4) {
882 // TODO: relax that restriction
883 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SON/SMR: version 9 (bigfile) not supported,yet");
884 return;
885 }
886 }
887
888 if (count < HeadLen) {
889 // read channel header and extra data (i.e. everything up to firstData)
890 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header,HeadLen+1);
891 count += ifread(hdr->AS.Header+count, 1, HeadLen-count, hdr);
892 hdr->AS.Header[count]=0;
893 }
894
895
896 #ifdef __SONINTL__
897 if (VERBOSE_LEVEL > 6) {
898
899 TFileHead *tfh = (void*)(hdr->AS.Header);
900 TChannel *tc = (void*)(hdr->AS.Header+512);
901
902 // debugging information
903 fprintf(stdout,"\tsizeof TFileHead %i\n\tsizeof TChannel %i\n\tsizeof TDataBlock %i\n",
904 (int)sizeof(TFileHead),(int)sizeof(TChannel),(int)sizeof(TDataBlock));
905
906 fprintf(stdout,"%i NS=%i %i %i %i\n",
907 (int)sizeof(*tfh),hdr->NS,(int)offsetof(TFileHead,channels),(int)sizeof(TChannel),(int)sizeof(TDataBlock));
908
909 fprintf(stdout,"systemID\t%i %i %i\n",(int)offsetof(TFileHead,systemID),
910 (int)sizeof(tfh->systemID),lei16p(hdr->AS.Header+offsetof(TFileHead,systemID)));
911 fprintf(stdout,"copyright\t%i %i\n",(int)offsetof(TFileHead,copyright),(int)sizeof(tfh->copyright));
912 fprintf(stdout,"creator \t%i %i <%8s>\n",(int)offsetof(TFileHead,creator),
913 (int)sizeof(tfh->creator),hdr->AS.Header+offsetof(TFileHead,creator));
914 fprintf(stdout,"usPerTime\t%i %i %i\n",(int)offsetof(TFileHead,usPerTime),
915 (int)sizeof(tfh->usPerTime),lei16p(hdr->AS.Header+offsetof(TFileHead,usPerTime)));
916 fprintf(stdout,"timePerADC\t%i %i %i\n",(int)offsetof(TFileHead,timePerADC),
917 (int)sizeof(tfh->timePerADC),lei16p(hdr->AS.Header+offsetof(TFileHead,timePerADC)));
918 fprintf(stdout,"fileState\t%i %i %i\n",(int)offsetof(TFileHead,fileState),
919 (int)sizeof(tfh->fileState),lei16p(hdr->AS.Header+offsetof(TFileHead,fileState)));
920 fprintf(stdout,"firstData\t%i %i %i\n",(int)offsetof(TFileHead,firstData),
921 (int)sizeof(tfh->firstData),lei32p(hdr->AS.Header+offsetof(TFileHead,firstData)));
922 fprintf(stdout,"channels\t%i %i 0x%04x\n",(int)offsetof(TFileHead,channels),
923 (int)sizeof(tfh->channels),lei16p(hdr->AS.Header+offsetof(TFileHead,channels)));
924 fprintf(stdout,"chanSize\t%i %i 0x%04x\n",(int)offsetof(TFileHead,chanSize),
925 (int)sizeof(tfh->chanSize),lei16p(hdr->AS.Header+offsetof(TFileHead,chanSize)));
926 fprintf(stdout,"extraData\t%i %i %i\n",(int)offsetof(TFileHead,extraData),
927 (int)sizeof(tfh->extraData),lei16p(hdr->AS.Header+offsetof(TFileHead,extraData)));
928 fprintf(stdout,"bufferSz\t%i %i %i\n",(int)offsetof(TFileHead,bufferSz),
929 (int)sizeof(tfh->bufferSz),lei16p(hdr->AS.Header+offsetof(TFileHead,bufferSz)));
930 fprintf(stdout,"osFormat\t%i %i %i\n",(int)offsetof(TFileHead,osFormat),
931 (int)sizeof(tfh->osFormat),lei16p(hdr->AS.Header+offsetof(TFileHead,osFormat)));
932 fprintf(stdout,"maxFTime\t%i %i %i\n",(int)offsetof(TFileHead,maxFTime),
933 (int)sizeof(tfh->maxFTime),lei32p(hdr->AS.Header+offsetof(TFileHead,maxFTime)));
934 fprintf(stdout,"dTimeBase\t%i %i %g\n",(int)offsetof(TFileHead,dTimeBase),
935 (int)sizeof(tfh->dTimeBase),lef64p(hdr->AS.Header+offsetof(TFileHead,dTimeBase)));
936 fprintf(stdout,"timeDate\t%i %i\n",(int)offsetof(TFileHead,timeDate),(int)sizeof(tfh->timeDate));
937 fprintf(stdout,"cAlignFlag\t%i %i\n",(int)offsetof(TFileHead,cAlignFlag),(int)sizeof(tfh->cAlignFlag));
938 fprintf(stdout,"pad0 \t%i %i\n",(int)offsetof(TFileHead,pad0),(int)sizeof(tfh->pad0));
939 fprintf(stdout,"LUTable \t%i %i\n",(int)offsetof(TFileHead,LUTable),(int)sizeof(tfh->LUTable));
940 fprintf(stdout,"pad \t%i %i\n",(int)offsetof(TFileHead,pad),(int)sizeof(tfh->pad));
941 fprintf(stdout,"fileComment\t%i %i\n",(int)offsetof(TFileHead,fileComment),(int)sizeof(tfh->fileComment));
942
943 fprintf(stdout,"==CHANNEL==\n");
944 fprintf(stdout,"delSize \t%i %i\n",(int)offsetof(TChannel,delSize),(int)sizeof(tc->delSize));
945 fprintf(stdout,"nextDelBlock\t%i %i\n",(int)offsetof(TChannel,nextDelBlock),(int)sizeof(tc->nextDelBlock));
946 fprintf(stdout,"firstBlock\t%i %i\n",(int)offsetof(TChannel,firstBlock),(int)sizeof(tc->firstBlock));
947 fprintf(stdout,"lastBlock\t%i %i\n",(int)offsetof(TChannel,lastBlock),(int)sizeof(tc->lastBlock));
948 fprintf(stdout,"blocks \t%i %i\n",(int)offsetof(TChannel,blocks),(int)sizeof(tc->blocks));
949 fprintf(stdout,"nExtra \t%i %i\n",(int)offsetof(TChannel,nExtra),(int)sizeof(tc->nExtra));
950 fprintf(stdout,"preTrig \t%i %i\n",(int)offsetof(TChannel,preTrig),(int)sizeof(tc->preTrig));
951 fprintf(stdout,"blocksMSW\t%i %i\n",(int)offsetof(TChannel,blocksMSW),(int)sizeof(tc->blocksMSW));
952 fprintf(stdout,"phySz \t%i %i\n",(int)offsetof(TChannel,phySz),(int)sizeof(tc->phySz));
953 fprintf(stdout,"maxData \t%i %i\n",(int)offsetof(TChannel,maxData),(int)sizeof(tc->maxData));
954 fprintf(stdout,"comment \t%i %i\n",(int)offsetof(TChannel,comment),(int)sizeof(tc->comment));
955
956 fprintf(stdout,"maxChanTime \t%i %i\n",(int)offsetof(TChannel,maxChanTime),(int)sizeof(tc->maxChanTime));
957
958 fprintf(stdout,"lChanDvd\t%i %i\n",(int)offsetof(TChannel,lChanDvd),(int)sizeof(tc->lChanDvd));
959 fprintf(stdout,"phyChan \t%i %i\n",(int)offsetof(TChannel,phyChan),(int)sizeof(tc->phyChan));
960 fprintf(stdout,"title \t%i %i %s\n",(int)offsetof(TChannel,title),(int)sizeof(tc->title),((char*)&(tc->title))+1);
961 fprintf(stdout,"idealRate\t%i %i\n",(int)offsetof(TChannel,idealRate),(int)sizeof(tc->idealRate));
962 fprintf(stdout,"kind \t%i %i\n",(int)offsetof(TChannel,kind),(int)sizeof(tc->kind));
963 fprintf(stdout,"delSizeMSB\t%i %i\n",(int)offsetof(TChannel,delSizeMSB),(int)sizeof(tc->delSizeMSB));
964
965 fprintf(stdout,"v.adc.scale\t%i %i\n",(int)offsetof(TChannel,v.adc.scale),(int)sizeof(tc->v.adc.scale));
966 fprintf(stdout,"v.adc.offset\t%i %i\n",(int)offsetof(TChannel,v.adc.offset),(int)sizeof(tc->v.adc.offset));
967 fprintf(stdout,"v.adc.units\t%i %i\n",(int)offsetof(TChannel,v.adc.units),(int)sizeof(tc->v.adc.units));
968 fprintf(stdout,"v.adc.divide\t%i %i\n",(int)offsetof(TChannel,v.adc.divide),(int)sizeof(tc->v.adc.divide));
969
970 fprintf(stdout,"v.event.initLow\t%i %i\n",(int)offsetof(TChannel,v.event.initLow),(int)sizeof(tc->v.event.initLow));
971 fprintf(stdout,"v.event.nextLow\t%i %i\n",(int)offsetof(TChannel,v.event.nextLow),(int)sizeof(tc->v.event.nextLow));
972
973 fprintf(stdout,"v.real.min\t%i %i\n",(int)offsetof(TChannel,v.real.min),(int)sizeof(tc->v.real.min));
974 fprintf(stdout,"v.real.max\t%i %i\n",(int)offsetof(TChannel,v.real.max),(int)sizeof(tc->v.real.max));
975 fprintf(stdout,"v.real.units\t%i %i\n",(int)offsetof(TChannel,v.real.units),(int)sizeof(tc->v.real.units));
976
977 assert(sizeof(TSONTimeDate)==8);
978 assert(sizeof(TFileHead)==512);
979 assert(sizeof(TChannel)==140);
980 assert(sizeof(TDataBlock)==64020);
981 }
982 #endif
983
984 uint16_t timePerADC;
985 uint32_t maxFTime;
986 double timebase,dTimeBase;
987 memcpy(&hdr->ID.Equipment, hdr->AS.Header+12, 8);
988 if (hdr->FILE.LittleEndian) {
989 timebase = hdr->VERSION < 6 ? 1e-6 : lef64p(hdr->AS.Header + 44);
990 hdr->SampleRate = 1.0 / (timebase * leu16p(hdr->AS.Header + 20) );
991 hdr->NS = leu16p(hdr->AS.Header + 30 );
992 timePerADC = lei16p(hdr->AS.Header + 22);
993 maxFTime = leu32p(hdr->AS.Header + 40);
994 dTimeBase = lef64p(hdr->AS.Header + 44);
995 } else {
996 timebase = hdr->VERSION < 6 ? 1e-6 : bef64p(hdr->AS.Header + 44);
997 hdr->SampleRate = 1.0 / (timebase * beu16p(hdr->AS.Header + 20));
998 hdr->NS = beu16p(hdr->AS.Header + 30);
999 timePerADC = bei16p(hdr->AS.Header + 22);
1000 maxFTime = beu32p(hdr->AS.Header + 40);
1001 dTimeBase = bef64p(hdr->AS.Header + 44);
1002 }
1003 hdr->SPR = 1;
1004
1005 while (!ifeof(hdr)) {
1006 // read channel header and extra data
1007 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header,hdr->HeadLen*2);
1008 hdr->HeadLen *= 2;
1009 count += ifread(hdr->AS.Header+count, 1, hdr->HeadLen-count, hdr);
1010 }
1011 hdr->HeadLen = count;
1012
1013 /*********************************************
1014 read channel header
1015 *********************************************/
1016 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
1017
1018 typeof(hdr->NS) k;
1019 size_t bpb = 0;
1020 for (k = 0; k < hdr->NS; k++) {
1021 uint32_t off = 512 + k*140;
1022 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
1023
1024 hc->Cal = lef32p(hdr->AS.Header + off + 124); // v.adc.scale
1025 hc->Off = lef32p(hdr->AS.Header + off + 128); // v.adc.off
1026 hc->OnOff = 0; //*(int16_t*)(hdr->AS.Header + off + 106) != 0;
1027 hc->SPR = 0;
1028 hc->GDFTYP = 3;
1029 hc->LeadIdCode = 0;
1030 hc->Transducer[0] = '\0';
1031
1032 int stringLength = hdr->AS.Header[off+108];
1033 assert(stringLength < MAX_LENGTH_LABEL);
1034 memcpy(hc->Label, hdr->AS.Header+off+108+1, stringLength);
1035 hc->Label[stringLength] = '\0';
1036
1037 {
1038 // extract Physical units (pascal string - first byte indicates the length, no 0-terminator)
1039 stringLength = hdr->AS.Header[off+132];
1040 char PhysicalUnit[6];
1041 assert( stringLength < sizeof(PhysicalUnit) );
1042 memcpy(PhysicalUnit, hdr->AS.Header+off+132+1, stringLength);
1043 PhysicalUnit[stringLength] = '\0';
1044 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): #%i <%s> <%s>\n",__FILE__,__LINE__, k+1, hc->Label, PhysicalUnit);
1045
1046 if (!strcmp(PhysicalUnit,"Volt") || !strcmp(PhysicalUnit," Volt"))
1047 hc->PhysDimCode = 4256;
1048 else if (!strcmp(PhysicalUnit,"mVolt"))
1049 hc->PhysDimCode = 4274;
1050 else if (!strcmp(PhysicalUnit,"uVolt"))
1051 hc->PhysDimCode = 4275;
1052 else
1053 hc->PhysDimCode = PhysDimCode(PhysicalUnit);
1054 }
1055
1056 uint32_t firstBlock = leu32p(hdr->AS.Header+off+6);
1057 uint32_t lastBlock = leu32p(hdr->AS.Header+off+10);
1058
1059 hc->bi = bpb;
1060 hc->bi8= 0;
1061 uint8_t kind = hdr->AS.Header[off+122];
1062 switch (kind) {
1063 case 0: //
1064 // ChanOff=0, /* the channel is OFF - */
1065 hc->OnOff = 0;
1066 break;
1067 case 1: {
1068 // Adc, /* a 16-bit waveform channel */
1069 hc->OnOff = 1;
1070 hc->GDFTYP = 3;
1071 hc->Cal = lef32p(hdr->AS.Header + off + 124) / 6553.6;
1072 hc->Off = lef32p(hdr->AS.Header + off + 128);
1073 hc->DigMax = (double)(int16_t)0x7fff;
1074 hc->DigMin = (double)(int16_t)0x8000;
1075 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
1076 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
1077
1078 uint32_t blocks = leu16p(hdr->AS.Header+off+14);
1079 size_t bpbnew = bpb + blocks * ADCdataBlkSize * 2;
1080 hdr->AS.rawdata = realloc(hdr->AS.rawdata,bpbnew);
1081
1082 uint32_t pos = firstBlock;
1083 while (1) {
1084 uint32_t items = leu16p(hdr->AS.Header+pos+18);
1085 memcpy(hdr->AS.rawdata+bpb, hdr->AS.Header+pos+20, items*2);
1086 bpb += items*2;
1087 hc->SPR += items;
1088 if (pos == lastBlock) break;
1089 pos = leu32p(hdr->AS.Header+pos+4);
1090 };
1091
1092 hdr->SPR = lcm(hdr->SPR, hc->SPR);
1093 break;
1094 }
1095 case 2:
1096 // EventFall, /* Event times (falling edges) */
1097 hc->OnOff = 0;
1098 break;
1099 case 3:
1100 // EventRise, /* Event times (rising edges) */
1101 hc->OnOff = 0;
1102 break;
1103 case 4:
1104 // EventBoth, /* Event times (both edges) */
1105 hc->OnOff = 0;
1106 break;
1107 case 5:
1108 // Marker, /* Event time plus 4 8-bit codes */
1109 hc->OnOff = 0;
1110 break;
1111 case 6:
1112 // AdcMark, /* Marker plus Adc waveform data */
1113 hc->OnOff = 0;
1114 break;
1115 case 7:
1116 // RealMark, /* Marker plus float numbers */
1117 hc->OnOff = 0;
1118 break;
1119 case 8:
1120 // TextMark, /* Marker plus text string */
1121 hc->OnOff = 0;
1122 break;
1123 case 9: {
1124 // RealWave /* waveform of float numbers */
1125 hc->OnOff = 1;
1126 hc->GDFTYP = 16;
1127 hc->LeadIdCode = 0;
1128 hc->Cal = lef32p(hdr->AS.Header + off + 124) / 6553.6;
1129 hc->Off = lef32p(hdr->AS.Header + off + 128);
1130 hc->DigMax = +1e9;
1131 hc->DigMin = -1e9;
1132 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
1133 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
1134
1135 uint32_t blocks = leu16p(hdr->AS.Header+off+14);
1136 size_t bpbnew = bpb + blocks * ADCdataBlkSize * 4;
1137 hdr->AS.rawdata = realloc(hdr->AS.rawdata, bpbnew);
1138
1139 uint32_t pos = firstBlock;
1140 while (1) {
1141 uint32_t items = leu16p(hdr->AS.Header+pos+18);
1142 memcpy(hdr->AS.rawdata+bpb, hdr->AS.Header+pos+20, items*4);
1143 bpb += items*4;
1144 hc->SPR += items;
1145 if (pos == lastBlock) break;
1146 pos = leu32p(hdr->AS.Header+pos+4);
1147 }
1148
1149 hdr->SPR = lcm(hdr->SPR, hc->SPR);
1150 break;
1151 }
1152 default:
1153 // unknown
1154 hc->OnOff = 0;
1155 fprintf(stderr,"SMR/SON: channel %i ignored - unknown type %i\n",k,kind);
1156 }
1157
1158 if (VERBOSE_LEVEL > 6) {
1159 char tmp[98-26+1];
1160 fprintf(stdout,"[%i].delSize\t%i\n",k,lei16p(hdr->AS.Header+off));
1161
1162 fprintf(stdout,"[%i].nextDelBlock\t%i\n",k,leu32p(hdr->AS.Header+off+2));
1163 fprintf(stdout,"[%i].firstBlock\t%i\n",k,leu32p(hdr->AS.Header+off+6));
1164 fprintf(stdout,"[%i].lastBlock\t%i\n",k,leu32p(hdr->AS.Header+off+10));
1165
1166 fprintf(stdout,"[%i].blocks\t%i\n",k,leu16p(hdr->AS.Header+off+14));
1167 fprintf(stdout,"[%i].nExtra\t%i\n",k,leu16p(hdr->AS.Header+off+16));
1168 fprintf(stdout,"[%i].preTrig\t%i\n",k,lei16p(hdr->AS.Header+off+18));
1169 fprintf(stdout,"[%i].blocksMSW\t%i\n",k,lei16p(hdr->AS.Header+off+20));
1170 fprintf(stdout,"[%i].phySz\t%i\n",k,lei16p(hdr->AS.Header+off+22));
1171 fprintf(stdout,"[%i].maxData\t%i\n",k,lei16p(hdr->AS.Header+off+24));
1172
1173 stringLength = hdr->AS.Header[off+26];
1174 assert(stringLength < sizeof(tmp));
1175 memcpy(tmp, hdr->AS.Header+off+26+1, 9);
1176 tmp[stringLength] = 0;
1177 fprintf(stdout,"[%i].comment\t<%s>\n",(int)k,tmp);
1178
1179 fprintf(stdout,"[%i].maxChanTime\t%i\t%i\n",(int)k,lei32p(hdr->AS.Header+off+98),*(int32_t*)(hdr->AS.Header+off+98));
1180 fprintf(stdout,"[%i].lChanDvd\t%i\n",(int)k,lei32p(hdr->AS.Header+off+102));
1181 fprintf(stdout,"[%i].phyChan\t%i\n",(int)k,lei16p(hdr->AS.Header+off+106));
1182
1183 stringLength = hdr->AS.Header[off+108];
1184 assert(stringLength < sizeof(tmp));
1185 memcpy(tmp, hdr->AS.Header+off+108+1,9);
1186 tmp[stringLength] = 0;
1187 fprintf(stdout,"[%i].title\t<%s>\n",(int)k,tmp);
1188
1189 fprintf(stdout,"[%i].idealRate\t%f\n",(int)k,lef32p(hdr->AS.Header+off+118));
1190 fprintf(stdout,"[%i].kind\t%i\n",(int)k,*(hdr->AS.Header+off+122));
1191 fprintf(stdout,"[%i].delSizeMSB\t%i\n",(int)k,*(hdr->AS.Header+off+123));
1192
1193 fprintf(stdout,"[%i].v.adc.scale\t%f\n",(int)k,lef32p(hdr->AS.Header+off+124));
1194 fprintf(stdout,"[%i].v.adc.offset\t%f\n",(int)k,lef32p(hdr->AS.Header+off+128));
1195
1196 stringLength = hdr->AS.Header[off+132];
1197 assert(stringLength < sizeof(tmp));
1198 memcpy(tmp, hdr->AS.Header+off+132+1, 5);
1199 tmp[stringLength] = 0;
1200 fprintf(stdout,"[%i].v.adc.units\t%s\n", (int)k, tmp);
1201
1202 fprintf(stdout,"[%i].v.adc.divide\t%i\n", (int)k, lei16p(hdr->AS.Header+off+138));
1203
1204 fprintf(stdout,"[%i].v.real.max\t%f\n", (int)k, lef32p(hdr->AS.Header+off+124));
1205 fprintf(stdout,"[%i].v.real.min\t%f\n", (int)k, lef32p(hdr->AS.Header+off+128));
1206 fprintf(stdout,"[%i].v.real.units\t%s\n", (int)k, hdr->AS.Header+off+132+1);
1207
1208 fprintf(stdout,"[%i].v.event\t%0x\t%g\n", (int)k, leu32p(hdr->AS.Header+off+124), lef32p(hdr->AS.Header+off+124));
1209 }
1210 }
1211
1212 hdr->NRec = 1;
1213 hdr->AS.bpb = bpb;
1214 }
1215
1216