Debian Codegraph @

biosig4c++/t210/sopen_abf_read.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2012-2026 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 #include <assert.h>
25 #include <ctype.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "../biosig.h"
32
33 #define ABFLONG int32_t
34 #include "axon_structs.h" // ABF2
35 #include "abfheadr.h" // ABF1
36
37 #define min(a,b) (((a) < (b)) ? (a) : (b))
38 #define max(a,b) (((a) > (b)) ? (a) : (b))
39
40
41 /******************************************************************************
42 read ATF file
43 ******************************************************************************/
44 EXTERN_C void sopen_atf_read(HDRTYPE* hdr) {
45
46 ifseek(hdr, 0, SEEK_SET);
47 size_t ll = 0;
48 char *line = NULL;
49 ssize_t nc;
50 typeof (hdr->NS) k;
51
52 // first line - skip: contains "ATF\t1.0" or alike
53 nc = getline(&line, &ll, hdr->FILE.FID);
54
55 // 2nd line: number of rows and colums
56 nc = getline(&line, &ll, hdr->FILE.FID);
57 char *str = line;
58 long numHdrLines = strtoul(str,&str,10); // number of additional header lines (?) - not used
59 hdr->SPR = 1;
60 hdr->NRec = 1;
61 long numTraces = strtoul(str,&str,10);
62 hdr->NS = 0;
63
64 // 3rd line: trace labels,
65 while (strncmp(line, "\"Time (", 7)) {
66 // skip all (numHdrLines?) header lines
67 nc = getline(&line, &ll, hdr->FILE.FID);
68 if (VERBOSE_LEVEL>7) fprintf(stdout,"ATF line <%s>\n",line);
69 }
70
71 /* obtain hdr->NS from the number of equal labels.
72 This will distinguish between ATF files with single channel and multiple sections and
73 and ATF files with single section and multiple channels
74 */
75 char**Label = malloc(numTraces*sizeof(void*));
76 size_t *traceList = (size_t*) malloc((numTraces-1)*sizeof(size_t));
77 size_t *chanList = (size_t*) malloc((numTraces-1)*sizeof(size_t));
78 char *str0 = strtok(line, "\t");
79 char *str1 = str0;
80 size_t lenLabel = strcspn(str0,"(");
81 char *tmpptr = NULL;
82
83 char *timeunit = strchr(str0,'('); *timeunit=0; timeunit++; tmpptr=strchr(timeunit,')'); *tmpptr=0;
84 hdr->SampleRate = 1.0 / PhysDimScale(PhysDimCode(timeunit));
85
86 k = 0;
87 long K2 = 1;
88 long k2 = 0;
89 for (k2 = 0; k2 < numTraces-1; k2++) {
90 k++;
91 str0 = strtok(NULL, "\t");
92 Label[k2] = str0;
93
94 char *p1 = NULL;
95 long ll=0;
96 if (!strncmp(str0, "\"Section", 8)) {
97 p1 = strchr(str0,'[');
98 ll = strtol(p1+1, &tmpptr, 10);
99 }
100 else if (!strncmp(str0, "\"Trace #", 8)) {
101 p1 = strchr(str0,'#');
102 ll = strtol(p1+1, &tmpptr, 10)-1;
103 }
104 else {
105 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, " (ATF) invalid/unsupported label name");
106 }
107
108 if (ll<0) biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, " (ATF) invalid trace number");
109 traceList[k2] = ll;
110
111 if (!strncmp(str1, str0, lenLabel)) {
112 if (k > hdr->NS) {
113 K2 = k - hdr->NS + 1;
114 hdr->NS = k;
115 }
116 }
117 else {
118 k = 0;
119 str1 = str0;
120 lenLabel = strcspn(str0,"(");
121 }
122 chanList[k2] = k; // zero-based indexing
123 }
124 hdr->NS = K2;
125
126 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS*sizeof(CHANNEL_TYPE));
127 hdr->AS.bpb = 0;
128 for (k = 0; k < hdr->NS; k++) {
129 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
130
131 //init_channel(hc);
132 hc->GDFTYP = 17; // float64
133 hc->PhysMin = -1e9;
134 hc->PhysMax = +1e9;
135 hc->DigMin = -1e9;
136 hc->DigMax = +1e9;
137 hc->Cal = 1.0;
138 hc->Off = 0.0;
139
140 hc->OnOff = 1;
141 hc->LeadIdCode = 0;
142 hc->Transducer[0] = '\0';
143 hc->PhysDimCode = 0;
144 #ifdef MAX_LENGTH_PHYSDIM
145 hc->PhysDim[0] = '?';
146 #endif
147 hc->TOffset = 0.0;
148 hc->HighPass = NAN;
149 hc->LowPass = NAN;
150 hc->Notch = NAN;
151 hc->XYZ[0] = 0;
152 hc->XYZ[1] = 0;
153 hc->XYZ[2] = 0;
154 hc->Impedance = NAN;
155
156 hc->bufptr = NULL;
157 hc->SPR = 1;
158 hc->bi8 = k * GDFTYP_BITS[hc->GDFTYP];
159 hc->bi = hc->bi8 / 8;
160 hdr->AS.bpb += GDFTYP_BITS[hc->GDFTYP]/8;
161
162 // split string into label and unit "label (unit)";
163 char *label = strtok(Label[K2+k], "(\"");
164 char *unit = strtok(NULL, ")\"");
165
166 if (label != NULL) strncpy(hc->Label, label, MAX_LENGTH_LABEL+1);
167 if (unit != NULL) {
168 hc->PhysDimCode = PhysDimCode(unit);
169 #ifdef MAX_LENGTH_PHYSDIM
170 strncpy(hc->PhysDim, unit, MAX_LENGTH_PHYSDIM+1);
171 #endif
172 }
173 }
174 hdr->HeadLen = iftell(hdr);
175
176 /***********************************************************************
177 read data block into memory
178 ***********************************************************************/
179 size_t ln = 0;
180 size_t numLines=0;
181 double t1;
182 while (~ifeof(hdr)) {
183 ssize_t nc = getline(&line, &ll, hdr->FILE.FID);
184 if (nc < 0) break;
185
186 if (numLines <= (ln+1) ) {
187 numLines = max(1024, ln*2);
188 hdr->AS.rawdata = realloc(hdr->AS.rawdata, numLines * (numTraces-1) * sizeof(double));
189 }
190
191 char *str = NULL;
192 double val = strtod(line, &str);
193 if (ln==0) t1 = val;
194 if (ln==1) hdr->SampleRate /= (val-t1);
195
196 for (size_t nc = 0; nc < numTraces-1; nc++) {
197 val = strtod(str, &str);
198 *(double*)(hdr->AS.rawdata + sizeof(double) * (ln * (numTraces-1) + nc)) = val;
199 }
200 ln++;
201 }
202 free(line);
203 ifclose(hdr);
204 hdr->SPR = ln;
205 hdr->NRec = (numTraces-1) / hdr->NS;
206
207 /* set up event table */
208 if (hdr->NRec > 1) {
209 reallocEventTable(hdr, hdr->NRec - 1);
210 hdr->EVENT.N = hdr->NRec - 1;
211 hdr->EVENT.SampleRate = hdr->SampleRate;
212 for (size_t k=0; k < hdr->EVENT.N ; k++) {
213 hdr->EVENT.TYP[k] = 0x7ffe;
214 hdr->EVENT.POS[k] = (k+1)*hdr->SPR;
215 }
216 if (hdr->EVENT.DUR!=NULL && hdr->EVENT.CHN!=NULL) {
217 for (size_t k=0; k < hdr->EVENT.N ; k++) {
218 hdr->EVENT.DUR[k]=0; // duration
219 hdr->EVENT.CHN[k]=0; // all channels
220 }
221 }
222 }
223
224 /* reshape data */
225 double *data = malloc(hdr->NS * hdr->NRec * hdr->SPR * sizeof(double));
226 for (size_t nr = 0; nr < hdr->SPR; nr++)
227 for (size_t nc = 0; nc < (numTraces-1); nc++) {
228 double val = *(double*)(hdr->AS.rawdata + sizeof(double) * (nr * (numTraces-1) + nc));
229 size_t idx = traceList[nc] * hdr->SPR + chanList[nc]*hdr->SPR*hdr->NRec + nr;
230 data[idx] = val;
231 }
232 free(hdr->AS.rawdata);
233 hdr->AS.rawdata = (void*)data;
234 hdr->NRec *= hdr->SPR;
235 hdr->SPR = 1;
236
237 free(Label);
238 free(traceList);
239 free(chanList);
240
241 hdr->FILE.POS = 0;
242 hdr->AS.first = 0;
243 hdr->AS.length = hdr->NRec;
244 }
245
246
247 EXTERN_C void sopen_abf_read(HDRTYPE* hdr) {
248 /*
249 this function will be called by the function SOPEN in "biosig.c"
250 Input:
251 char* Header // contains the file content
252 Output:
253 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
254 */
255
256 if (VERBOSE_LEVEL>7) fprintf(stdout,"sopen_abf_read 101\n");
257
258 if (VERBOSE_LEVEL>7) fprintf(stderr,"%s (line %i) %"PRIiPTR" %i %i %"PRIiPTR"\n",__FILE__,__LINE__,
259 sizeof(struct ABFFileHeader),ABF_OLDHEADERSIZE, ABF_HEADERSIZE,
260 offsetof(struct ABFFileHeader, lHeaderSize));
261
262 if (VERBOSE_LEVEL>7) fprintf(stderr,"%s (line %i) %i %"PRIiPTR" %i\n",__FILE__,__LINE__,
263 ABF_BLOCKSIZE, offsetof(struct ABFFileHeader, lDataSectionPtr),
264 ABF_BLOCKSIZE*lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDataSectionPtr)) );
265
266 size_t count = hdr->HeadLen;
267 hdr->VERSION = lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fFileVersionNumber));
268
269 hdr->HeadLen = ABF_BLOCKSIZE * lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDataSectionPtr));
270 if (count < hdr->HeadLen) {
271 hdr->AS.Header = (uint8_t*) realloc(hdr->AS.Header, hdr->HeadLen);
272 count += ifread(hdr->AS.Header+count, 1, hdr->HeadLen-count, hdr);
273 }
274 if ( count < (hdr->VERSION < 1.6 ? ABF_OLDHEADERSIZE : ABF_HEADERSIZE)
275 || count < hdr->HeadLen )
276 {
277 biosigERROR(hdr, B4C_INCOMPLETE_FILE, "Reading ABF Header block failed"); return;
278 }
279 hdr->HeadLen = count;
280
281 {
282 struct tm t;
283 uint32_t u = leu32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lFileStartDate));
284 t.tm_year = u / 10000 - 1900;
285 t.tm_mon = (u % 10000)/100 - 1;
286 t.tm_mday = (u % 100);
287 u = leu32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lFileStartTime));
288 t.tm_hour = u / 3600;
289 t.tm_min = (u % 3600)/60;
290 t.tm_sec = (u % 60);
291 //u = leu16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nFileStartMillisecs));
292 hdr->T0 = tm_time2gdf_time(&t);
293 }
294
295 uint16_t gdftyp = 3;
296 switch (lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nDataFormat))) {
297 case 0: gdftyp = 3;
298 // FIXME
299 fprintf(stderr,"WARNING: %s(ABF1/integer:...) %s: scaling of y-axis might be incorrect \n", __func__, hdr->FileName);
300 break;
301 case 1: gdftyp = 16; break;
302 default:
303 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "ABF datatype/gdftyp unknown/unsupported (neither int16 nor float32)");
304 break;
305 }
306
307 if (VERBOSE_LEVEL>7) fprintf(stderr,"%s (line %i)\n",__FILE__,__LINE__);
308
309 size_t slen;
310 slen = min(MAX_LENGTH_MANUF, sizeof(((struct ABFFileHeader*)(hdr->AS.Header))->sCreatorInfo));
311 slen = min(MAX_LENGTH_MANUF, ABF_CREATORINFOLEN);
312 strncpy(hdr->ID.Manufacturer._field, (char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sCreatorInfo), slen);
313 hdr->ID.Manufacturer._field[slen] = 0;
314 hdr->ID.Manufacturer.Name = hdr->ID.Manufacturer._field;
315
316 if (hdr->Version > 1.5) {
317 slen = min(MAX_LENGTH_RID, sizeof(((struct ABFFileHeader*)(hdr->AS.Header))->sFileComment));
318 strncpy(hdr->ID.Recording,(char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sFileComment), slen);
319 hdr->ID.Recording[slen] = 0;
320 }
321
322 if (VERBOSE_LEVEL>7) {
323 fprintf(stdout,"sCreatorInfo:\t%s\n",hdr->AS.Header + offsetof(struct ABFFileHeader, sCreatorInfo));
324 fprintf(stdout,"_sFileComment:\t%s\n",hdr->AS.Header + offsetof(struct ABFFileHeader, _sFileComment));
325 if (hdr->Version > 1.5)
326 fprintf(stdout,"sFileComment:\t%s\n",hdr->AS.Header + offsetof(struct ABFFileHeader, sFileComment));
327
328
329 fprintf(stdout,"\nlHeaderSize:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lHeaderSize)));
330 fprintf(stdout,"lTagSectionPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lTagSectionPtr)));
331 fprintf(stdout,"lNumTagEntries:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumTagEntries)));
332 fprintf(stdout,"lVoiceTagPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lVoiceTagPtr)));
333 fprintf(stdout,"lVoiceTagEntries:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lVoiceTagEntries)));
334 fprintf(stdout,"lSynchArrayPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lSynchArrayPtr)));
335 fprintf(stdout,"lSynchArraySize:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lSynchArraySize)));
336
337 fprintf(stdout,"\nlDataSectionPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDataSectionPtr)));
338 fprintf(stdout,"lScopeConfigPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lScopeConfigPtr)));
339 fprintf(stdout,"lNumScopes:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumScopes)));
340 fprintf(stdout,"_lDACFilePtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, _lDACFilePtr)));
341 fprintf(stdout,"_lDACFileNumEpisodes:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, _lDACFileNumEpisodes)));
342 fprintf(stdout,"lDeltaArrayPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDeltaArrayPtr)));
343 fprintf(stdout,"lNumDeltas:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumDeltas)));
344 fprintf(stdout,"nDataFormat:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nDataFormat)));
345 fprintf(stdout,"nSimultaneousScan:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nSimultaneousScan)));
346 fprintf(stdout,"lStatisticsConfigPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lStatisticsConfigPtr)));
347 fprintf(stdout,"lAnnotationSectionPtr:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lAnnotationSectionPtr)));
348 fprintf(stdout,"lNumAnnotations:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumAnnotations)));
349
350 fprintf(stdout,"\nlNumSamplesPerEpisode:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumSamplesPerEpisode)));
351 fprintf(stdout,"lPreTriggerSamples:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lPreTriggerSamples)));
352 fprintf(stdout,"lEpisodesPerRun:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lEpisodesPerRun)));
353 fprintf(stdout,"lActualAcqLength:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lActualAcqLength)));
354 fprintf(stdout,"lActualEpisodes:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lActualEpisodes)));
355 fprintf(stdout,"lRunsPerTrial:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lRunsPerTrial)));
356 fprintf(stdout,"lNumberOfTrials:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumberOfTrials)));
357 fprintf(stdout,"fADCRange:\t%g\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCRange)));
358 fprintf(stdout,"fDACRange:\t%g\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fDACRange)));
359 fprintf(stdout,"lADCResolution:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lADCResolution)));
360 fprintf(stdout,"lDACResolution:\t%i\n",lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDACResolution)));
361
362 fprintf(stdout,"\nchannel_count_acquired:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, channel_count_acquired)));
363 fprintf(stdout,"nADCNumChannels:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCNumChannels)));
364 fprintf(stdout,"fADCSampleInterval:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCSampleInterval)));
365 fprintf(stdout,"nDigitalDACChannel:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nDigitalDACChannel)));
366 fprintf(stdout,"nOperationMode:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nOperationMode)));
367 fprintf(stdout,"nDigitalEnable:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nDigitalEnable)));
368
369 fprintf(stdout,"\nfFileVersionNumber:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fFileVersionNumber)));
370 fprintf(stdout,"fHeaderVersionNumber:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fHeaderVersionNumber)));
371 fprintf(stdout,"fADCSampleInterval:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCSampleInterval)));
372 fprintf(stdout,"fADCSecondSampleInterval:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCSecondSampleInterval)));
373 fprintf(stdout,"fSynchTimeUnit:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSynchTimeUnit)));
374 fprintf(stdout,"fSecondsPerRun:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSecondsPerRun)));
375 fprintf(stdout,"fTriggerThreshold:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fTriggerThreshold)));
376
377 fprintf(stdout,"\nfScopeOutputInterval:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fScopeOutputInterval)));
378 fprintf(stdout,"fEpisodeStartToStart:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fEpisodeStartToStart)));
379 fprintf(stdout,"fRunStartToStart:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fRunStartToStart)));
380 fprintf(stdout,"fTrialStartToStart:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fTrialStartToStart)));
381 fprintf(stdout,"fStatisticsPeriod:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fStatisticsPeriod)));
382 fprintf(stdout,"fADCRange:\t%g\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCRange)));
383 fprintf(stdout,"fDACRange:\t%g\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fTriggerThreshold)));
384 fprintf(stdout,"fTriggerThreshold:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fDACRange)));
385 fprintf(stdout,"dFileDuration:\t%g\n",lef64p(hdr->AS.Header + offsetof(struct ABFFileHeader, dFileDuration)));
386 fprintf(stdout,"fTriggerThreshold:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fTriggerThreshold)));
387
388 fprintf(stdout,"nExperimentType:\t%f\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nExperimentType)));
389
390 }
391
392 if (VERBOSE_LEVEL>7) fprintf(stdout,"sopen_abf_read 201\n");
393 if (VERBOSE_LEVEL>7) fprintf(stderr,"%s (line %i)\n",__FILE__,__LINE__);
394
395
396 hdr->NS = lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCNumChannels));
397 if (lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nDigitalEnable)))
398 hdr->NS += lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nDigitalDACChannel));
399
400 hdr->SampleRate = 1e6 / (hdr->NS * lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCSampleInterval)));
401
402 hdr->NRec = lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lActualAcqLength)) / lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCNumChannels));
403 hdr->SPR = 1;
404 hdr->AS.bpb = hdr->NS*GDFTYP_BITS[gdftyp]/8;
405
406 hdr->CHANNEL = realloc(hdr->CHANNEL, hdr->NS*sizeof(CHANNEL_TYPE));
407 uint32_t k1=0,k; // ABF is 32 bits only, no need for more
408 for (k=0; k < ABF_ADCCOUNT + ABF_DACCOUNT; k++) {
409 CHANNEL_TYPE *hc = hdr->CHANNEL+k1;
410 uint16_t nss = leu16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCSamplingSeq) + 2 * k);
411 if ((k < ABF_ADCCOUNT) && (nss < hdr->NS)) {
412 hc->OnOff = 1;
413 hc->bufptr = NULL;
414 hc->LeadIdCode = 0;
415 int ll = min(ABF_ADCNAMELEN, MAX_LENGTH_LABEL);
416 strncpy(hc->Label, (char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sADCChannelName) + nss*ABF_ADCNAMELEN, ll);
417 while (ll > 0 && isspace(hc->Label[--ll]));
418 hc->Label[ll+1] = 0;
419 char units[ABF_ADCUNITLEN+1];
420 {
421 memcpy(units, (char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sADCUnits) + nss*ABF_ADCUNITLEN, ABF_ADCUNITLEN);
422 units[ABF_ADCUNITLEN] = 0;
423 int p=ABF_ADCUNITLEN; while ( (0<p) && isspace(units[--p])) units[p]=0; // remove trailing white space
424 hc->PhysDimCode = PhysDimCode(units);
425 }
426 hc->Transducer[0] = '\0';
427 hc->LowPass = lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalLowpassFilter) + 4 * k);
428 hc->HighPass = lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalHighpassFilter) + 4 * k);
429 hc->Notch = NAN;
430 hc->TOffset = NAN;
431 hc->XYZ[0] = 0;
432 hc->XYZ[1] = 0;
433 hc->XYZ[2] = 0;
434 hc->GDFTYP = gdftyp;
435 hc->SPR = hdr->SPR;
436 hc->bi = k1*GDFTYP_BITS[gdftyp]/8;
437 hc->bi8 = k1*GDFTYP_BITS[gdftyp];
438 double PhysMax = lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCRange));
439 double DigMax = (double)lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lADCResolution));
440 hc->Cal = 1.0f;
441 hc->Off = 0.0f;
442
443 if (gdftyp==3) {
444 // FIXME: scaling of integer format, ABF1: scaling of one channel off by factor 4
445 double fTotalScaleFactor = lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fInstrumentScaleFactor) + 4 * k)
446 * lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCProgrammableGain) + 4 * k);
447 hc->Off = +lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fInstrumentOffset) + 4 * k);
448 if (lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nSignalType) + 2 * k) != 0) {
449 hc->Off -= lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalOffset) + 4 * k);
450 fTotalScaleFactor *= lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalGain) + 4 * k);
451 }
452
453 if (hdr->Version > 1.5 &&
454 lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nTelegraphEnable) + 2 * k) != 0)
455 {
456 fTotalScaleFactor *= lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fTelegraphAdditGain) + 4 * k);
457 }
458 hc->Cal = PhysMax / (fTotalScaleFactor * DigMax);
459 }
460 hc->DigMax = DigMax-1.0;
461 hc->DigMin = -hc->DigMax;
462 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
463 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
464
465 if (VERBOSE_LEVEL>7) {
466 fprintf(stdout,"==== CHANNEL %i [%s] ====\n",k,units);
467 fprintf(stdout,"PhysMax: %g / DigMax: %g\n",PhysMax,DigMax);
468 fprintf(stdout,"nSignalType:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nSignalType) + 2 * k));
469 fprintf(stdout,"nADCPtoLChannelMap:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCPtoLChannelMap) + 2 * k));
470 fprintf(stdout,"nADCSamplingSeq:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCSamplingSeq) + 2 * k));
471 fprintf(stdout,"fADCProgrammableGain:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCProgrammableGain) + 4 * k));
472 fprintf(stdout,"fADCRange:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCRange) + 4 * k));
473
474 fprintf(stdout,"fADCDisplayAmplification:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCDisplayAmplification) + 4 * k));
475 fprintf(stdout,"fADCDisplayOffset:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCDisplayOffset) + 4 * k));
476 fprintf(stdout,"fInstrumentScaleFactor:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fInstrumentScaleFactor) + 4 * k));
477 fprintf(stdout,"fInstrumentOffset:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fInstrumentOffset) + 4 * k));
478 fprintf(stdout,"fSignalGain:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalGain) + 4 * k));
479 fprintf(stdout,"fSignalOffset:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalOffset) + 4 * k));
480 fprintf(stdout,"fSignalLowpassFilter:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalLowpassFilter) + 4 * k));
481 fprintf(stdout,"fSignalHighpassFilter:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalHighpassFilter) + 4 * k));
482 if (hdr->Version > 1.5)
483 fprintf(stdout,"fTelegraphAdditGain:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fTelegraphAdditGain) + 4 * k));
484 }
485 k1++;
486 }
487 else if (k < ABF_ADCCOUNT) {
488 // do not increase k1;
489 if (VERBOSE_LEVEL>7) {
490 fprintf(stdout,"==== CHANNEL %i ====\n",k);
491
492 fprintf(stdout,"nADCPtoLChannelMap:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCPtoLChannelMap) + 2 * k));
493 fprintf(stdout,"nADCSamplingSeq:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nADCSamplingSeq) + 2 * k));
494
495 fprintf(stdout,"fADCProgrammableGain:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCProgrammableGain) + 4 * k));
496
497 fprintf(stdout,"fADCDisplayAmplification:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCDisplayAmplification) + 4 * k));
498 fprintf(stdout,"fADCDisplayOffset:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fADCDisplayOffset) + 4 * k));
499 fprintf(stdout,"fInstrumentScaleFactor:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fInstrumentScaleFactor) + 4 * k));
500 fprintf(stdout,"fInstrumentOffset:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fInstrumentOffset) + 4 * k));
501 fprintf(stdout,"fSignalGain:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalGain) + 4 * k));
502 fprintf(stdout,"fSignalOffset:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalOffset) + 4 * k));
503 fprintf(stdout,"fSignalLowpassFilter:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalLowpassFilter) + 4 * k));
504 fprintf(stdout,"fSignalHighpassFilter:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalHighpassFilter) + 4 * k));
505 }
506 }
507 else if ( (k1 < ABF_ADCCOUNT+ABF_DACCOUNT) && (k < hdr->NS) ) {
508 hc->OnOff = 1;
509 hc->bufptr = NULL;
510 hc->LeadIdCode = 0;
511 strncpy(hc->Label, (char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sDACChannelName) + (k-ABF_ADCCOUNT)*ABF_DACNAMELEN,
512 min(ABF_DACNAMELEN,MAX_LENGTH_LABEL));
513 hc->Label[ABF_DACNAMELEN] = 0;
514
515 char units[ABF_DACUNITLEN+1]; {
516 memcpy(units, hdr->AS.Header + offsetof(struct ABFFileHeader, sDACChannelUnits) + (k-ABF_ADCCOUNT)*ABF_DACUNITLEN, ABF_DACUNITLEN);
517 units[ABF_DACUNITLEN] = 0;
518 int p=ABF_ADCUNITLEN; while ( (0<p) && isspace(units[--p])) units[p]=0; // remove trailing white space
519 hc->PhysDimCode = PhysDimCode(units);
520 }
521 hc->Transducer[0] = '\0';
522 hc->HighPass = NAN;
523 hc->LowPass = NAN;
524 hc->Notch = NAN;
525 hc->TOffset = NAN;
526 hc->XYZ[0] = 0;
527 hc->XYZ[1] = 0;
528 hc->XYZ[2] = 0;
529 hc->GDFTYP = gdftyp;
530 hc->SPR = hdr->SPR;
531 hc->bi = k1*GDFTYP_BITS[gdftyp]/8;
532 hc->bi8 = k1*GDFTYP_BITS[gdftyp];
533
534 double PhysMax = lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fDACRange));
535 double DigMax = (double)lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDACResolution));
536 hc->Cal = PhysMax / DigMax;
537 hc->Off = 0.0;
538 hc->PhysMax = PhysMax * (DigMax-1.0) / DigMax;
539 hc->PhysMin = -hc->PhysMax;
540 hc->DigMax = DigMax-1;
541 hc->DigMin = -hc->DigMax;
542
543 if (VERBOSE_LEVEL>7) {
544 fprintf(stdout,"==== CHANNEL %i [%s] ====\nfDACScaleFactor:\t%f\n",k,units,
545 lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fDACScaleFactor) + 4 * (k-ABF_ADCCOUNT)));
546 fprintf(stdout,"fDACHoldingLevel:\t%f\n",lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fDACHoldingLevel) + 4 * (k-ABF_ADCCOUNT)));
547 }
548 k1++;
549 }
550 }
551
552 /* ===== EVENT TABLE ===== */
553 size_t n1,n2;
554 n1 = leu32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lActualEpisodes));
555 if (n1 > 0) n1--;
556 n2 = leu32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumTagEntries));
557 if (0x0fffffff < (n1+n2)) {
558 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "reading ABF with 2^28 events or more is not supported");
559 return;
560 }
561 hdr->EVENT.N = n1+n2;
562
563 /* add breaks between sweeps */
564 size_t spr = leu32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lNumSamplesPerEpisode))/hdr->NS;
565 hdr->EVENT.SampleRate = hdr->SampleRate;
566 hdr->EVENT.POS = (uint32_t*) realloc(hdr->EVENT.POS, hdr->EVENT.N * sizeof(*hdr->EVENT.POS));
567 hdr->EVENT.TYP = (uint16_t*) realloc(hdr->EVENT.TYP, hdr->EVENT.N * sizeof(*hdr->EVENT.TYP));
568 for (k=0; k < n1; k++) {
569 hdr->EVENT.TYP[k] = 0x7ffe;
570 hdr->EVENT.POS[k] = (k+1)*spr;
571 }
572 if (hdr->EVENT.DUR!=NULL && hdr->EVENT.CHN!=NULL) {
573 for (k=0; k < n1; k++) {
574 hdr->EVENT.DUR[k]=0; // duration
575 hdr->EVENT.CHN[k]=0; // all channels
576 }
577 }
578 /* add tags, note sizeof(struct ABFTag)==64 */
579 hdr->AS.auxBUF = realloc(hdr->AS.auxBUF, n2 * sizeof(struct ABFTag));
580 ifseek(hdr, lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lTagSectionPtr))*ABF_BLOCKSIZE, SEEK_SET);
581 count = ifread(hdr->AS.auxBUF, sizeof(struct ABFTag), n2, hdr);
582 hdr->EVENT.N = n1+count;
583 for (k=0; k < count; k++) {
584 uint8_t *abftag = hdr->AS.auxBUF + k * sizeof(struct ABFTag);
585 hdr->EVENT.POS[k+n1] = leu32p(abftag)/hdr->NS;
586 abftag[ABF_TAGCOMMENTLEN+4-1]=0;
587 FreeTextEvent(hdr, k+n1, (char*)(abftag+4));
588 }
589
590 // set HeadLen to begin of data block
591 hdr->HeadLen = ABF_BLOCKSIZE * lei32p(hdr->AS.Header + offsetof(struct ABFFileHeader, lDataSectionPtr));
592 ifseek(hdr, hdr->HeadLen, SEEK_SET);
593
594 } // end of sopen_abf_read
595
596
597 size_t readABF2block(uint8_t *block, HDRTYPE *hdr, struct ABF_Section *S) {
598 S->uBlockIndex = leu32p(block + offsetof(struct ABF_Section, uBlockIndex));
599 if (S->uBlockIndex==0) return 0;
600
601 S->uBytes = leu32p(block + offsetof(struct ABF_Section, uBytes));
602 if (S->uBytes==0) return 0;
603
604 S->llNumEntries = leu64p(block + offsetof(struct ABF_Section, llNumEntries));
605 hdr->AS.auxBUF = realloc(hdr->AS.auxBUF, S->llNumEntries*S->uBytes);
606 ifseek(hdr, S->uBlockIndex*(size_t)512, SEEK_SET);
607 return ifread(hdr->AS.auxBUF, 1, S->llNumEntries*S->uBytes, hdr);
608 }
609
610
611 EXTERN_C void sopen_abf2_read(HDRTYPE* hdr) {
612 /*
613 this function will be called by the function SOPEN in "biosig.c"
614 Input:
615 char* Header // contains the file content
616 Output:
617 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
618 */
619
620 if (VERBOSE_LEVEL>7) fprintf(stdout,"sopen_abf2_read 101\n");
621
622 if (hdr->HeadLen < 512) {
623 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, 512);
624 hdr->HeadLen += ifread(hdr->AS.Header+hdr->HeadLen, 1, 512-hdr->HeadLen, hdr);
625 }
626
627 {
628 struct tm t;
629 uint32_t u = leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uFileStartDate));
630 t.tm_year = u / 10000 - 1900;
631 t.tm_mon = (u % 10000)/100 - 1;
632 t.tm_mday = (u % 100);
633 u = leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uFileStartTimeMS))/1000;
634 t.tm_hour = u / 3600;
635 t.tm_min = (u % 3600)/60;
636 t.tm_sec = (u % 60);
637 //u = leu16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nFileStartMillisecs));
638 hdr->T0 = tm_time2gdf_time(&t);
639 }
640
641 hdr->SPR = 1;
642 uint16_t gdftyp = 3;
643
644 unsigned numSweeps = leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uActualEpisodes));
645 if (VERBOSE_LEVEL>7) {
646 fprintf(stdout,"\nuFileInfoSize:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uFileInfoSize)));
647 fprintf(stdout,"uActualEpisodes:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uActualEpisodes)));
648 fprintf(stdout,"uFileStartDate:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uFileStartDate)));
649 fprintf(stdout,"uFileStartTimeMS:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uFileStartTimeMS)));
650 fprintf(stdout,"uStopwatchTime:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uStopwatchTime)));
651
652 fprintf(stdout,"nFileType:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABF_FileInfo, nFileType)));
653 fprintf(stdout,"nDataFormat:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABF_FileInfo, nDataFormat)));
654 fprintf(stdout,"nSimultaneousScan:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABF_FileInfo, nSimultaneousScan)));
655 fprintf(stdout,"nCRCEnable:\t%i\n",lei16p(hdr->AS.Header + offsetof(struct ABF_FileInfo, nCRCEnable)));
656
657 fprintf(stdout,"uFileCRC: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uFileCRC)));
658 fprintf(stdout,"uCreatorVersion: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uCreatorVersion)));
659 fprintf(stdout,"uCreatorNameIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uCreatorNameIndex)));
660 fprintf(stdout,"uModifierVersion: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uModifierVersion)));
661 fprintf(stdout,"uModifierNameIndex:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uModifierNameIndex)));
662 fprintf(stdout,"uProtocolPathIndex:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, uProtocolPathIndex)));
663
664 fprintf(stdout,"ProtocolSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ProtocolSection.uBlockIndex)));
665 fprintf(stdout,"ProtocolSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ProtocolSection.uBytes)));
666 fprintf(stdout,"ProtocolSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ProtocolSection.llNumEntries)));
667
668 fprintf(stdout,"ADCSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCSection.uBlockIndex)));
669 fprintf(stdout,"ADCSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCSection.uBytes)));
670 fprintf(stdout,"ADCSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCSection.llNumEntries)));
671
672 fprintf(stdout,"DACSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DACSection.uBlockIndex)));
673 fprintf(stdout,"DACSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DACSection.uBytes)));
674 fprintf(stdout,"DACSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DACSection.llNumEntries)));
675
676 fprintf(stdout,"EpochSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochSection.uBlockIndex)));
677 fprintf(stdout,"EpochSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochSection.uBytes)));
678 fprintf(stdout,"EpochSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochSection.llNumEntries)));
679
680 fprintf(stdout,"ADCPerDACSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCPerDACSection.uBlockIndex)));
681 fprintf(stdout,"ADCPerDACSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCPerDACSection.uBytes)));
682 fprintf(stdout,"ADCPerDACSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCPerDACSection.llNumEntries)));
683
684 fprintf(stdout,"EpochPerDACSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochPerDACSection.uBlockIndex)));
685 fprintf(stdout,"EpochPerDACSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochPerDACSection.uBytes)));
686 fprintf(stdout,"EpochPerDACSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochPerDACSection.llNumEntries)));
687
688 fprintf(stdout,"UserListSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, UserListSection.uBlockIndex)));
689 fprintf(stdout,"UserListSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, UserListSection.uBytes)));
690 fprintf(stdout,"UserListSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, UserListSection.llNumEntries)));
691
692 fprintf(stdout,"StatsRegionSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsRegionSection.uBlockIndex)));
693 fprintf(stdout,"StatsRegionSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsRegionSection.uBytes)));
694 fprintf(stdout,"StatsRegionSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsRegionSection.llNumEntries)));
695
696 fprintf(stdout,"MathSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, MathSection.uBlockIndex)));
697 fprintf(stdout,"MathSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, MathSection.uBytes)));
698 fprintf(stdout,"MathSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, MathSection.llNumEntries)));
699
700 fprintf(stdout,"StringsSection.uBlockIndex:\t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StringsSection.uBlockIndex)));
701 fprintf(stdout,"StringsSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StringsSection.uBytes)));
702 fprintf(stdout,"StringsSection.llNumEntries:\t%i\n",(int)lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StringsSection.llNumEntries)));
703
704 fprintf(stdout,"DataSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DataSection.uBlockIndex)));
705 fprintf(stdout,"DataSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DataSection.uBytes)));
706 fprintf(stdout,"DataSection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DataSection.llNumEntries)));
707
708 fprintf(stdout,"TagSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, TagSection.uBlockIndex)));
709 fprintf(stdout,"TagSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, TagSection.uBytes)));
710 fprintf(stdout,"TagSection.llNumEntries: \t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, TagSection.llNumEntries)));
711
712 fprintf(stdout,"ScopeSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ScopeSection.uBlockIndex)));
713 fprintf(stdout,"ScopeSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ScopeSection.uBytes)));
714 fprintf(stdout,"ScopeSection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, ScopeSection.llNumEntries)));
715
716 fprintf(stdout,"DeltaSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DeltaSection.uBlockIndex)));
717 fprintf(stdout,"DeltaSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DeltaSection.uBytes)));
718 fprintf(stdout,"DeltaSection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DeltaSection.llNumEntries)));
719
720 fprintf(stdout,"VoiceTagSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, VoiceTagSection.uBlockIndex)));
721 fprintf(stdout,"VoiceTagSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, VoiceTagSection.uBytes)));
722 fprintf(stdout,"VoiceTagSection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, VoiceTagSection.llNumEntries)));
723
724 fprintf(stdout,"SynchArraySection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, SynchArraySection.uBlockIndex)));
725 fprintf(stdout,"SynchArraySection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, SynchArraySection.uBytes)));
726 fprintf(stdout,"SynchArraySection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, SynchArraySection.llNumEntries)));
727
728 fprintf(stdout,"AnnotationSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, AnnotationSection.uBlockIndex)));
729 fprintf(stdout,"AnnotationSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, AnnotationSection.uBytes)));
730 fprintf(stdout,"AnnotationSection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, AnnotationSection.llNumEntries)));
731
732 fprintf(stdout,"StatsSection.uBlockIndex: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsSection.uBlockIndex)));
733 fprintf(stdout,"StatsSection.uBytes: \t%i\n",leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsSection.uBytes)));
734 fprintf(stdout,"StatsSection.llNumEntries:\t%"PRIi64"\n",lei64p(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsSection.llNumEntries)));
735
736 }
737
738 switch(leu32p(hdr->AS.Header + offsetof(struct ABF_FileInfo, DataSection.uBytes))) {
739 case 2: gdftyp=3;
740 // FIXME
741 fprintf(stderr,"WARNING: %s(ABF2/integer:...) %s: scaling of y-axis might be incorrect \n", __func__, hdr->FileName);
742 break;
743 case 4: gdftyp=16; break;
744 default:
745 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "ABF2 datatype/gdftyp unknown/unsupported (neither int16 nor float32)");
746 break;
747 }
748
749 struct ABF_Section S;
750 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, ProtocolSection), hdr, &S);
751 // TODO: checking why it requires big-endian here
752 float fADCRange = bef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fADCRange));
753 float fDACRange = bef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fDACRange));
754 int32_t lADCResolution = bei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lADCResolution));
755 int32_t lDACResolution = bei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lDACResolution));
756 hdr->SampleRate = 1e6 / lef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fADCSequenceInterval));
757
758 int16_t nOperationMode = lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nOperationMode));
759 switch (nOperationMode) {
760 case 3: // Gap free
761 break;
762 case 1: // Variable-Length Event-Driven
763 case 2: // Fixed-Length Event-Driven
764 case 4: // High-Speed Oscilloscope Mode
765 case 5: // Episodic Stimulation Mode - a number of equal-length sweeps (also known as episodes) are acquired
766 fprintf(stdout,"Warning ABF2 v%4.2f: nOperationMode=%d is very experimental - double check the data you get.\n", hdr->VERSION, nOperationMode);
767 break;
768 default:
769 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "ABF2 nOperationMode unknown and unsupported");
770 break;
771 }
772
773 if (VERBOSE_LEVEL>7) {
774 fprintf(stdout,"nOperationMode:\t%i\n", nOperationMode);
775 fprintf(stdout,"fADCSequenceInterval:\t%g\n", lef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fADCSequenceInterval)));
776 fprintf(stdout,"fSecondsPerRun:\t%g\n", lef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fSecondsPerRun)));
777 fprintf(stdout,"fSecondsPerRun:\t%g\n", bef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fSecondsPerRun)));
778 fprintf(stdout,"lNumSamplesPerEpisode:\t%i\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lNumSamplesPerEpisode)));
779 fprintf(stdout,"lPreTriggerSamples:\t%i\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lPreTriggerSamples)));
780 fprintf(stdout,"lEpisodesPerRun:\t%i\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lEpisodesPerRun)));
781 fprintf(stdout,"lRunsPerTrial:\t%i\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lRunsPerTrial)));
782 fprintf(stdout,"lNumberOfTrials:\t%i\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lNumberOfTrials)));
783
784 fprintf(stdout,"nDigitalEnable:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitalEnable)));
785 fprintf(stdout,"nActiveDACChannel:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nActiveDACChannel)));
786 fprintf(stdout,"nDigitalHolding:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitalHolding)));
787 fprintf(stdout,"nDigitalInterEpisode:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitalInterEpisode)));
788 fprintf(stdout,"nDigitalDACChannel:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitalDACChannel)));
789
790 fprintf(stdout,"nDigitizerADCs:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitizerADCs)));
791 fprintf(stdout,"nDigitizerDACs:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitizerDACs)));
792 fprintf(stdout,"nDigitizerTotalDigitalOuts:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitizerTotalDigitalOuts)));
793 fprintf(stdout,"nDigitizerSynchDigitalOuts:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitizerSynchDigitalOuts)));
794 fprintf(stdout,"nDigitizerType:\t%i\n", lei16p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, nDigitizerType)));
795
796 fprintf(stdout,"fADCRange:\t(b)%g\n", bef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fADCRange)));
797 fprintf(stdout,"fDACRange:\t(b)%g\n", bef32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, fDACRange)));
798
799 fprintf(stdout,"lADCResolution:\t0x%08x\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lADCResolution)));
800 fprintf(stdout,"lDACResolution:\t0x%08x\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lDACResolution)));
801 fprintf(stdout,"lADCResolution:\t(l)%d\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lADCResolution)));
802 fprintf(stdout,"lDACResolution:\t(l)%d\n", lei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lDACResolution)));
803 fprintf(stdout,"lADCResolution:\t(b)%d\n", bei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lADCResolution)));
804 fprintf(stdout,"lDACResolution:\t(b)%d\n", bei32p(hdr->AS.auxBUF + offsetof(struct ABF_ProtocolInfo, lDACResolution)));
805 }
806
807 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, StringsSection), hdr, &S);
808 const char* StringSectionBlock = hdr->AS.auxBUF;
809 hdr->AS.auxBUF = NULL;
810
811 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s L%d %s(...) bix:%d bytes:%d num:%ld\n", __FILE__, __LINE__, __func__, S.uBlockIndex, S.uBytes, S.llNumEntries);
812 /*
813 for (size_t k=0; k<S.llNumEntries*S.uBytes;k++) {
814 fprintf(stdout,"%4d: %03d %02x %c\n",k,StringSectionBlock[k],StringSectionBlock[k],StringSectionBlock[k]);
815 }
816 */
817
818 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCSection), hdr, &S);
819 hdr->NS = S.llNumEntries;
820 hdr->CHANNEL = realloc(hdr->CHANNEL, hdr->NS*sizeof(CHANNEL_TYPE));
821 int k;
822 size_t bi=0;
823 for (k = 0; k < hdr->NS; k++) {
824 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
825 hc->bufptr = NULL;
826 hc->LeadIdCode = 0;
827 hc->OnOff = 1;
828 hc->Transducer[0] = 0;
829 hc->Label[0] = 0;
830
831 int16_t nss = lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nADCNum));
832 hc->LowPass = lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalLowpassFilter));
833 hc->HighPass = lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalHighpassFilter));
834 hc->GDFTYP = gdftyp;
835 hc->SPR = hdr->SPR;
836 hc->bi = bi;
837 bi += GDFTYP_BITS[gdftyp] / 8;
838
839 strncpy(hc->Label, (char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sADCChannelName) + nss*ABF_ADCNAMELEN, min(ABF_ADCNAMELEN,MAX_LENGTH_LABEL));
840 hc->Label[ABF_ADCNAMELEN] = 0;
841
842 // TODO: where to find information about PhysDim and Label in ABF2 file ? Currently, this is hardcoded below
843 char units[ABF_ADCUNITLEN+1]; {
844 memcpy(units, (char*)hdr->AS.Header + offsetof(struct ABFFileHeader, sADCUnits) + nss*ABF_ADCUNITLEN, ABF_ADCUNITLEN);
845 units[ABF_ADCUNITLEN] = 0;
846 int p=ABF_ADCUNITLEN; while ( (0<p) && isspace(units[--p])) units[p]=0; // remove trailing white space
847 hc->PhysDimCode = PhysDimCode(units);
848 }
849
850 hc->Cal = 1.0f;
851 hc->Off = 0.0f;
852 double PhysMax = fADCRange;
853 double DigMax = lADCResolution;
854
855 // https://swharden.com/pyabf/abf2-file-format/#reading-multi-byte-integers-from-bytestrings
856 // https://support.moleculardevices.com/s/article/Convert-data-file-from-another-program-to-an-ABF-file-so-that-it-can-be-read-by-pCLAMP
857 // https://github.com/yamad/libabf/blob/master/src/AxAbfFio32/abfheader.cpp -> GetADCtoUUFactors
858
859 if (gdftyp==3) {
860 hc->Off = lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fInstrumentOffset));
861 double iCal = lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fInstrumentScaleFactor));
862 iCal *= lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fADCProgrammableGain));
863 if (lei16p(hdr->AS.Header + offsetof(struct ABFFileHeader, nSignalType) + 2 * k) != 0) {
864 iCal *= lef32p(hdr->AS.Header + offsetof(struct ABFFileHeader, fSignalGain) + 4 * k);
865 hc->Off -= lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalOffset));
866 }
867 if (!lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nTelegraphEnable)))
868 iCal *= lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fTelegraphAdditGain));
869 if (iCal == 0.0) iCal = 1.0;
870 hc->Cal = fADCRange / (iCal * lADCResolution);
871 }
872 hc->DigMax = lADCResolution - 1.0;
873 hc->DigMin = -hc->DigMax;
874 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
875 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
876
877 if (VERBOSE_LEVEL>7) {
878 fprintf(stdout,"nADCNum:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nADCNum)));
879
880 fprintf(stdout,"ADCInfo::nTelegraphEnable:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nTelegraphEnable)));
881 fprintf(stdout,"ADCInfo::nTelegraphInstrument:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nTelegraphInstrument)));
882
883 fprintf(stdout,"ADCInfo::fTelegraphAdditGain:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fTelegraphAdditGain)));
884 fprintf(stdout,"ADCInfo::fTelegraphFilter:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fTelegraphFilter)));
885 fprintf(stdout,"ADCInfo::fTelegraphMembraneCap:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fTelegraphMembraneCap)));
886 fprintf(stdout,"ADCInfo::nTelegraphMode:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nTelegraphMode)));
887 fprintf(stdout,"ADCInfo::fTelegraphAccessResistance:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fTelegraphAccessResistance)));
888
889 fprintf(stdout,"ADCInfo::nADCPtoLChannelMap:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nADCPtoLChannelMap)));
890 fprintf(stdout,"nADCSamplingSeq:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nADCSamplingSeq)));
891 fprintf(stdout,"nADCPtoLChannelMap:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nADCPtoLChannelMap)));
892 fprintf(stdout,"fADCProgrammableGain:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fADCProgrammableGain)));
893 fprintf(stdout,"fInstrumentScaleFactor:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fInstrumentScaleFactor)));
894 fprintf(stdout,"fInstrumentOffset:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fInstrumentOffset)));
895 fprintf(stdout,"fSignalGain:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalGain)));
896 fprintf(stdout,"fSignalOffset:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalOffset)));
897 fprintf(stdout,"fSignalLowpassFilter:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalLowpassFilter)));
898 fprintf(stdout,"fSignalHighpassFilter:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fSignalHighpassFilter)));
899 // char nLowpassFilterType;
900 // char nHighpassFilterType;
901
902 fprintf(stdout,"ADCInfo::fPostProcessLowpassFilter:\t%g\n", lef32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, fPostProcessLowpassFilter)));
903 // char nPostProcessLowpassFilterType;
904
905 fprintf(stdout,"ADCInfo::bEnabledDuringPN:\t%d\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, bEnabledDuringPN)));
906 fprintf(stdout,"ADCInfo::nStatsChannelPolarity:\t%i\n", lei16p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, nStatsChannelPolarity)));
907
908 fprintf(stdout,"ADCInfo::lADCChannelNameIndex:\t%i\n", bei32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, lADCChannelNameIndex)));
909 fprintf(stdout,"ADCInfo::lADCUnitsIndex:\t%i\n", bei32p(hdr->AS.auxBUF + S.uBytes*k + offsetof(struct ABF_ADCInfo, lADCUnitsIndex)));
910 }
911 }
912 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, DACSection), hdr, &S);
913 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochSection), hdr, &S);
914 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, ADCPerDACSection), hdr, &S);
915 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, EpochPerDACSection), hdr, &S);
916 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, UserListSection), hdr, &S);
917 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsRegionSection), hdr, &S);
918 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, MathSection), hdr, &S);
919 // readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, StringsSection), hdr, &S);
920 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, DataSection), hdr, &S);
921 hdr->AS.rawdata = hdr->AS.auxBUF;
922 hdr->AS.auxBUF = NULL;
923 hdr->AS.first = 0;
924 hdr->AS.length = S.llNumEntries / hdr->NS;
925 hdr->NRec = S.llNumEntries / hdr->NS;
926 hdr->AS.bpb = bi;
927
928 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, TagSection), hdr, &S);
929 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, ScopeSection), hdr, &S);
930 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, DeltaSection), hdr, &S);
931 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, VoiceTagSection), hdr, &S);
932 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, SynchArraySection), hdr, &S);
933 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, AnnotationSection), hdr, &S);
934 readABF2block(hdr->AS.Header + offsetof(struct ABF_FileInfo, StatsSection), hdr, &S);
935
936 if (StringSectionBlock) free(StringSectionBlock);
937
938 /* set up event table */
939 if (numSweeps > 1) {
940 reallocEventTable(hdr, numSweeps - 1);
941 hdr->EVENT.N = numSweeps - 1;
942 hdr->EVENT.SampleRate = hdr->SampleRate;
943 for (size_t k=0; k < hdr->EVENT.N ; k++) {
944 hdr->EVENT.TYP[k] = 0x7ffe;
945 hdr->EVENT.POS[k] = (k+1)*((uint64_t)hdr->SPR * hdr->NRec) / numSweeps;
946 }
947 if (hdr->EVENT.DUR!=NULL && hdr->EVENT.CHN!=NULL) {
948 for (size_t k=0; k < hdr->EVENT.N ; k++) {
949 hdr->EVENT.DUR[k] = 0; // duration
950 hdr->EVENT.CHN[k] = 0; // all channels
951 }
952 }
953 }
954 }
955