Debian Codegraph @

biosig4c++/t220/sopen_scp_write.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2005-2018 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
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 3
12 of the License, or (at your option) any later version.
13
14
15 */
16
17
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "../biosig-dev.h"
23
24 #define min(a,b) (((a) < (b)) ? (a) : (b))
25 #define max(a,b) (((a) > (b)) ? (a) : (b))
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 int sopen_SCP_write(HDRTYPE* hdr) {
32 /*
33 This function is an auxillary function and is only called by the function SOPEN in "biosig.c"
34
35 Input:
36 HDRTYPE *hdr // defines the HDR structure according to "biosig.h"
37 hdr->VERSION specifies the target version
38 */
39 uint8_t* ptr; // pointer to memory mapping of the file layout
40 uint8_t* PtrCurSect; // point to current section
41 int curSect;
42 uint32_t len;
43 uint16_t crc;
44 uint32_t i;
45 uint32_t sectionStart;
46 struct tm* T0_tm;
47 double AVM, avm;
48 uint16_t avm16;
49 struct aecg* aECG;
50
51 assert(hdr != NULL);
52 assert(hdr->TYPE == SCP_ECG);
53
54 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) : V%f\n",__FILE__,__LINE__,hdr->VERSION);
55
56 if ((fabs(hdr->VERSION - 1.3)<0.01) && (fabs(hdr->VERSION-2.0)<0.01) && (fabs(hdr->VERSION-3.0)<0.01))
57 fprintf(stderr,"Warning SOPEN (SCP-WRITE): Version %f not supported\n",hdr->VERSION);
58
59 uint8_t versionSection = (hdr->VERSION < 3.0) ? 20 : 29; // (uint8_t)round(hdr->VERSION*10); // implemented version number
60 uint8_t versionProtocol = versionSection;
61
62 if (hdr->aECG==NULL) {
63 fprintf(stderr,"Warning SOPEN_SCP_WRITE: No aECG info defined\n");
64 hdr->aECG = malloc(sizeof(struct aecg));
65 aECG = (struct aecg*)hdr->aECG;
66 aECG->diastolicBloodPressure=0.0;
67 aECG->systolicBloodPressure=0.0;
68 aECG->MedicationDrugs="/0";
69 aECG->ReferringPhysician="/0";
70 aECG->LatestConfirmingPhysician="/0";
71 aECG->Diagnosis="/0";
72 aECG->EmergencyLevel=0;
73 #if (BIOSIG_VERSION > 10500)
74 aECG->Section8.NumberOfStatements = 0;
75 aECG->Section8.Statements = NULL;
76 aECG->Section11.NumberOfStatements = 0;
77 aECG->Section11.Statements = NULL;
78 #endif
79 }
80 else
81 aECG = (struct aecg*)hdr->aECG;
82
83
84 //fprintf(stdout,"SCP-Write: IIb %s\n",hdr->aECG->ReferringPhysician);
85 /* predefined values */
86 aECG->Section1.Tag14.INST_NUMBER = 0; // tag 14, byte 1-2
87 aECG->Section1.Tag14.DEPT_NUMBER = 0; // tag 14, byte 3-4
88 aECG->Section1.Tag14.DEVICE_ID = 0; // tag 14, byte 5-6
89 aECG->Section1.Tag14.DeviceType = 0; // tag 14, byte 7: 0: Cart, 1: System (or Host)
90 aECG->Section1.Tag14.MANUF_CODE = 255; // tag 14, byte 8 (MANUF_CODE has to be 255)
91 aECG->Section1.Tag14.MOD_DESC = "Cart1"; // tag 14, byte 9 (MOD_DESC has to be "Cart1")
92 aECG->Section1.Tag14.VERSION = versionSection; // tag 14, byte 15 (VERSION * 10)
93 aECG->Section1.Tag14.PROT_COMP_LEVEL = 0xA0; // tag 14, byte 16 (PROT_COMP_LEVEL has to be 0xA0 => level II)
94 // tag 14, byte 17 (LANG_SUPP_CODE has to be 0x00 => Ascii only, latin and 1-byte code, 0x37: UTF-8)
95 aECG->Section1.Tag14.LANG_SUPP_CODE = (versionSection < 25) ? 0x00 : 0x37;
96 aECG->Section1.Tag14.ECG_CAP_DEV = 0xD0; // tag 14, byte 18 (ECG_CAP_DEV has to be 0xD0 => Acquire, (No Analysis), Print and Store)
97 aECG->Section1.Tag14.MAINS_FREQ = 0; // tag 14, byte 19 (MAINS_FREQ has to be 0: unspecified, 1: 50 Hz, 2: 60Hz)
98 aECG->Section1.Tag14.ANAL_PROG_REV_NUM = "";
99 aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV = "";
100 aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID = "";
101 aECG->Section1.Tag14.ACQ_DEV_SCP_SW = "OpenECG XML-SCP 1.00"; // tag 14, byte 38 (SCP_IMPL_SW has to be "OpenECG XML-SCP 1.00")
102 aECG->Section1.Tag14.ACQ_DEV_MANUF = "Manufacturer"; // tag 14, byte 38 (ACQ_DEV_MANUF has to be "Manufacturer")
103
104 aECG->Section5.Length = 0;
105 aECG->Section6.Length = 0;
106 aECG->Section7.Length = 0;
107
108
109 /* */
110 aECG->FLAG.HUFFMAN = 0;
111 aECG->FLAG.REF_BEAT= 0;
112 aECG->FLAG.DIFF = 0;
113 aECG->FLAG.BIMODAL = 0;
114
115
116 /*
117 check channels:
118 disable channels that do not have a known ECG LeadId
119 disable channels with physical units other than Voltage.
120 The number of channels for conversion is stored in NS.
121 */
122 typeof(hdr->NS) NS = 0, k;
123 for (k=0; k < hdr->NS; k++) {
124 CHANNEL_TYPE *CH=hdr->CHANNEL+k;
125
126 if ( CH->LeadIdCode > 255) CH->OnOff = 0;
127 if ( (CH->PhysDimCode & 0xffe0) != PhysDimCode("V")) CH->OnOff = 0;
128
129 if (CH->OnOff != 1) continue;
130 NS++;
131 }
132
133 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) : v%f VERSION=%i\n",__FILE__,__LINE__, hdr->VERSION, versionSection);
134
135 ptr = (uint8_t*)hdr->AS.Header;
136
137 int NSections = (versionSection < 25) ? 12 : 19;
138 // initialize section 0
139 sectionStart = 6+16+NSections*10;
140 ptr = (uint8_t*)realloc(ptr,sectionStart);
141 memset(ptr,0,sectionStart);
142
143 uint32_t curSectLen; // current section length
144 for (curSect=NSections-1; curSect>=0; curSect--) {
145
146 curSectLen = 0; // current section length
147 //ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen);
148
149 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) : Section %i/%i %i %p\n",__FILE__,__LINE__,curSect,NSections,sectionStart,ptr);
150
151 if (curSect==0) // SECTION 0
152 {
153 hdr->HeadLen = sectionStart; // length of all other blocks together
154 ptr = (uint8_t*)realloc(ptr,hdr->HeadLen); // total file length
155
156 curSectLen = 16; // current section length
157 sectionStart = 6;
158
159 curSectLen += NSections*10;
160 }
161 else if (curSect==1) // SECTION 1
162 {
163 ptr = (uint8_t*)realloc(ptr,sectionStart+10000);
164 PtrCurSect = ptr+sectionStart;
165 curSectLen = 16; // current section length
166 char *nextPartOfPatientName=hdr->Patient.Name;
167
168 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 0 \n");
169
170 // Tag 0 (max len = 64)
171 if (!hdr->FLAG.ANONYMOUS && (nextPartOfPatientName != NULL) && strlen(nextPartOfPatientName))
172 {
173 *(ptr+sectionStart+curSectLen) = 0; // tag
174 len = strcspn(nextPartOfPatientName, "\x1f");
175 leu16a(len, ptr+sectionStart+curSectLen+1); // length
176 strncpy((char*)ptr+sectionStart+curSectLen+3,nextPartOfPatientName,len); // field
177 nextPartOfPatientName += len+1;
178 curSectLen += len+3;
179 }
180
181 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 1 \n");
182
183 // Tag 1 (max len = 64) Firstname
184 if (!hdr->FLAG.ANONYMOUS && (nextPartOfPatientName != NULL) && strlen(nextPartOfPatientName))
185 {
186 *(ptr+sectionStart+curSectLen) = 1; // tag
187 len = strcspn(nextPartOfPatientName, "\x1f");
188 leu16a(len, ptr+sectionStart+curSectLen+1); // length
189 strncpy((char*)ptr+sectionStart+curSectLen+3,nextPartOfPatientName,len); // field
190 nextPartOfPatientName += len+1;
191 curSectLen += len+3;
192 }
193
194 // Tag 2 (max len = 64) Patient ID
195 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 2 \n");
196
197 // if (hdr->Patient.Id != NULL) {
198 if (strlen(hdr->Patient.Id)>0) {
199 *(ptr+sectionStart+curSectLen) = 2; // tag
200 len = strlen(hdr->Patient.Id) + 1;
201 leu16a(len, ptr+sectionStart+curSectLen+1); // length
202 strncpy((char*)ptr+sectionStart+curSectLen+3,hdr->Patient.Id,len); // field
203 curSectLen += len+3;
204 }
205
206 // Tag 3 (max len = 64) Second Last Name
207 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 3 \n");
208
209 if (!hdr->FLAG.ANONYMOUS && (nextPartOfPatientName != NULL) && strlen(nextPartOfPatientName))
210 {
211 *(ptr+sectionStart+curSectLen) = 3; // tag
212 len = strcspn(nextPartOfPatientName, "\x1f");
213 leu16a(len, ptr+sectionStart+curSectLen+1); // length
214 strncpy((char*)ptr+sectionStart+curSectLen+3,nextPartOfPatientName,len); // field
215 nextPartOfPatientName += len+1;
216 curSectLen += len+3;
217 }
218
219 // Tag 5 (len = 4)
220 if ((hdr->Patient.Birthday) > 0) {
221 T0_tm = gdf_time2tm_time(hdr->Patient.Birthday);
222
223 *(ptr+sectionStart+curSectLen) = 5; // tag
224 leu16a(4, ptr+sectionStart+curSectLen+1); // length
225 leu16a(T0_tm->tm_year+1900, ptr+sectionStart+curSectLen+3);// year
226 *(ptr+sectionStart+curSectLen+5) = (uint8_t)(T0_tm->tm_mon + 1); // month
227 *(ptr+sectionStart+curSectLen+6) = (uint8_t)(T0_tm->tm_mday); // day
228 curSectLen += 7;
229 }
230
231 // Tag 6 (len = 3) Height
232 if (hdr->Patient.Height>0.0) {
233 *(ptr+sectionStart+curSectLen) = 6; // tag
234 leu16a(3, ptr+sectionStart+curSectLen+1); // length
235 leu16a(hdr->Patient.Height, ptr+sectionStart+curSectLen+3); // value
236 *(ptr+sectionStart+curSectLen+5) = 1; // cm
237 curSectLen += 6;
238 }
239
240 // Tag 7 (len = 3) Weight
241 if (hdr->Patient.Weight>0.0) {
242 *(ptr+sectionStart+curSectLen) = 7; // tag
243 leu16a(3, ptr+sectionStart+curSectLen+1); // length
244 leu16a(hdr->Patient.Weight, ptr+sectionStart+curSectLen+3); // value
245 *(ptr+sectionStart+curSectLen+5) = 1; // kg
246 curSectLen += 6;
247 }
248
249 // Tag 8 (len = 1)
250 if (hdr->Patient.Sex != 0) {
251 *(ptr+sectionStart+curSectLen) = 8; // tag
252 leu16a(1, ptr+sectionStart+curSectLen+1); // length
253 *(ptr+sectionStart+curSectLen+3) = hdr->Patient.Sex; // value
254 curSectLen += 4;
255 }
256
257 // Tag 11 (len = 2)
258 if (aECG->systolicBloodPressure>0.0) {
259 *(ptr+sectionStart+curSectLen) = 11; // tag
260 leu16a(2, ptr+sectionStart+curSectLen+1); // length
261 leu16a((uint16_t)aECG->systolicBloodPressure, ptr+sectionStart+curSectLen+3); // value
262 curSectLen += 5;
263 };
264
265 // Tag 12 (len = 2)
266 if (aECG->diastolicBloodPressure>0.0) {
267 *(ptr+sectionStart+curSectLen) = 12; // tag
268 leu16a(2, ptr+sectionStart+curSectLen+1); // length
269 leu16a((uint16_t)aECG->diastolicBloodPressure, ptr+sectionStart+curSectLen+3); // value
270 curSectLen += 5;
271 };
272 // Tag 13 (max len = 80)
273 aECG->Diagnosis="";
274 len = strlen(aECG->Diagnosis);
275 if (len>0) {
276 *(ptr+sectionStart+curSectLen) = 13; // tag
277 len = min(64,len+1);
278 leu16a(len, ptr+sectionStart+curSectLen+1); // length
279 strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->Diagnosis,len);
280 curSectLen += 3+len;
281 };
282
283 // Tag 14 (max len = 2 + 2 + 2 + 1 + 1 + 6 + 1 + 1 + 1 + 1 + 1 + 16 + 1 + 25 + 25 + 25 + 25 + 25)
284 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 14 \n");
285
286 // Total = 161 (max value)
287 *(ptr+sectionStart+curSectLen) = 14; // tag
288 //len = 41; // minimum length
289 // leu16a(len, ptr+sectionStart+curSectLen+1); // length
290 memset(ptr+sectionStart+curSectLen+3,0,41); // dummy value
291
292 curSectLen += 3;
293 leu16a(aECG->Section1.Tag14.INST_NUMBER, ptr+sectionStart+curSectLen);
294 leu16a(aECG->Section1.Tag14.DEPT_NUMBER, ptr+sectionStart+curSectLen+2);
295 leu16a(aECG->Section1.Tag14.DEVICE_ID, ptr+sectionStart+curSectLen+4);
296 *(ptr+sectionStart+curSectLen+ 6) = aECG->Section1.Tag14.DeviceType;
297 *(ptr+sectionStart+curSectLen+ 7) = aECG->Section1.Tag14.MANUF_CODE; // tag 14, byte 7 (MANUF_CODE has to be 255)
298 strncpy((char*)(ptr+sectionStart+curSectLen+8), aECG->Section1.Tag14.MOD_DESC, 6); // tag 14, byte 7 (MOD_DESC has to be "Cart1")
299 *(ptr+sectionStart+curSectLen+14) = versionSection; // tag 14, byte 14 (VERSION has to be 20)
300 *(ptr+sectionStart+curSectLen+14) = aECG->Section1.Tag14.VERSION;
301 *(ptr+sectionStart+curSectLen+15) = aECG->Section1.Tag14.PROT_COMP_LEVEL; // tag 14, byte 15 (PROT_COMP_LEVEL has to be 0xA0 => level II)
302 *(ptr+sectionStart+curSectLen+16) = aECG->Section1.Tag14.LANG_SUPP_CODE; // tag 14, byte 16 (LANG_SUPP_CODE has to be 0x00 => Ascii only, latin and 1-byte code)
303 *(ptr+sectionStart+curSectLen+17) = aECG->Section1.Tag14.ECG_CAP_DEV; // tag 14, byte 17 (ECG_CAP_DEV has to be 0xD0 => Acquire, (No Analysis), Print and Store)
304 *(ptr+sectionStart+curSectLen+18) = aECG->Section1.Tag14.MAINS_FREQ; // tag 14, byte 18 (MAINS_FREQ has to be 0: unspecified, 1: 50 Hz, 2: 60Hz)
305 *(ptr+sectionStart+curSectLen+35) = strlen(aECG->Section1.Tag14.ANAL_PROG_REV_NUM)+1; // tag 14, byte 34 => length of ANAL_PROG_REV_NUM + 1 = 1
306 uint16_t len1 = 36;
307
308 char* tmp;
309 tmp = aECG->Section1.Tag14.ANAL_PROG_REV_NUM;
310 len = min(25, strlen(tmp) + 1);
311 strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len);
312 len1 += len;
313
314 tmp = aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV;
315 len = min(25, strlen(tmp) + 1);
316 strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len);
317 len1 += len;
318
319 tmp = aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID;
320 len = min(25, strlen(tmp) + 1);
321 strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len);
322 len1 += len;
323
324 tmp = aECG->Section1.Tag14.ACQ_DEV_SCP_SW;
325 len = min(25, strlen(tmp) + 1);
326 strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len);
327 len1 += len;
328
329 tmp = aECG->Section1.Tag14.ACQ_DEV_MANUF;
330 len = min(25, strlen(tmp) + 1);
331 strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len);
332 len1 += len;
333
334 leu16a(len1, ptr+sectionStart+curSectLen+1-3); // length
335 curSectLen += len1;
336
337 // Tag 16 (max len = 80)
338 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 16 \n");
339 len = hdr->ID.Hospital ? strlen(hdr->ID.Hospital) : 0;
340 if (len > 0) {
341 *(ptr+sectionStart+curSectLen) = 16; // tag
342 len = min(64,len+1);
343 leu16a(len, ptr+sectionStart+curSectLen+1); // length
344 strncpy((char*)(ptr+sectionStart+curSectLen+3),hdr->ID.Hospital,len);
345 curSectLen += 3+len;
346 }
347
348 // Tag 20 (max len = 64 )
349 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 20 \n");
350 len = aECG->ReferringPhysician ? strlen(aECG->ReferringPhysician) : 0;
351 if (len > 0) {
352 *(ptr+sectionStart+curSectLen) = 20; // tag
353 len = min(64,len+1);
354 leu16a(len, ptr+sectionStart+curSectLen+1); // length
355 strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->ReferringPhysician,len);
356 curSectLen += 3+len;
357 };
358
359 // Tag 21 (max len = 64 )
360 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 21 \n");
361 len = aECG->MedicationDrugs ? strlen(aECG->MedicationDrugs) : 0;
362 if (len>0) {
363 *(ptr+sectionStart+curSectLen) = 21; // tag
364 len = min(64,len+1);
365 leu16a(len, ptr+sectionStart+curSectLen+1); // length
366 strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->MedicationDrugs,len);
367 curSectLen += 3+len;
368 };
369
370 // Tag 22 (max len = 40 )
371 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 22 \n");
372 len = hdr->ID.Technician ? strlen(hdr->ID.Technician) : 0;
373 if (len > 0) {
374 *(ptr+sectionStart+curSectLen) = 22; // tag
375 len = min(64,len+1);
376 leu16a(len, ptr+sectionStart+curSectLen+1); // length
377 strncpy((char*)(ptr+sectionStart+curSectLen+3),hdr->ID.Technician,len);
378 curSectLen += 3+len;
379 }
380
381 // Tag 24 ( len = 1 )
382 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 24 \n");
383 *(ptr+sectionStart+curSectLen) = 24; // tag
384 leu16a(1, ptr+sectionStart+curSectLen+1); // length
385 *(ptr+sectionStart+curSectLen+3) = aECG->EmergencyLevel;
386 curSectLen += 4;
387
388 // Tag 25 (len = 4)
389 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 25 \n");
390
391 #if __FreeBSD__ || __APPLE__ || __NetBSD__
392 T0_tm = gdf_time2tm_time(hdr->T0);
393 T0_tm->tm_gmtoff = hdr->tzmin*60;
394 #else
395 gdf_time T1 = hdr->T0;
396 T1 += (int32_t)ldexp(timezone/86400.0,32);
397 T0_tm = gdf_time2tm_time(T1);
398 #endif
399
400 *(ptr+sectionStart+curSectLen) = 25; // tag
401 leu16a(4, ptr+sectionStart+curSectLen+1); // length
402 leu16a(T0_tm->tm_year+1900, ptr+sectionStart+curSectLen+3);// year
403 *(ptr+sectionStart+curSectLen+5) = (uint8_t)(T0_tm->tm_mon + 1);// month
404 *(ptr+sectionStart+curSectLen+6) = (uint8_t)T0_tm->tm_mday; // day
405 curSectLen += 7;
406
407 // Tag 26 (len = 3)
408 *(ptr+sectionStart+curSectLen) = 26; // tag
409 leu16a(3, ptr+sectionStart+curSectLen+1); // length
410 *(ptr+sectionStart+curSectLen+3) = (uint8_t)T0_tm->tm_hour; // hour
411 *(ptr+sectionStart+curSectLen+4) = (uint8_t)T0_tm->tm_min; // minute
412 *(ptr+sectionStart+curSectLen+5) = (uint8_t)T0_tm->tm_sec; // second
413 curSectLen += 6;
414
415 if (NS>0) {
416 CHANNEL_TYPE *CH = hdr->CHANNEL;
417 while (CH->OnOff != 1) CH++;
418
419 // Tag 27 (len = 3) highpass filter
420 *(ptr+sectionStart+curSectLen) = 27; // tag
421 leu16a(2, ptr+sectionStart+curSectLen+1); // length
422 leu16a((uint16_t)CH->HighPass, ptr+sectionStart+curSectLen+3); // hour
423 curSectLen += 5;
424
425 // Tag 28 (len = 3) lowpass filter
426 *(ptr+sectionStart+curSectLen) = 28; // tag
427 leu16a(2, ptr+sectionStart+curSectLen+1); // length
428 leu16a((uint16_t)CH->LowPass, ptr+sectionStart+curSectLen+3); // hour
429 curSectLen += 5;
430
431 // Tag 29 (len = 1) filter bitmap
432 uint8_t bitmap = 0;
433 if (fabs(CH->LowPass-60.0)<0.01)
434 bitmap = 1;
435 else if (fabs(CH->LowPass-50.0)<0.01)
436 bitmap = 2;
437 else
438 bitmap = 0;
439 *(ptr+sectionStart+curSectLen) = 29; // tag
440 leu16a(1, ptr+sectionStart+curSectLen+1); // length
441 *(ptr+sectionStart+curSectLen+3) = bitmap;
442 curSectLen += 4;
443
444 }
445
446 // Tag 32 (len = 5)
447 if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 32 \n");
448
449 *(ptr+sectionStart+curSectLen) = 32; // tag
450 leu16a(2, ptr+sectionStart+curSectLen+1); // length
451 if (hdr->Patient.Impairment.Heart==1) {
452 *(ptr+sectionStart+curSectLen+3) = 0;
453 *(ptr+sectionStart+curSectLen+4) = 1; // Apparently healthy
454 curSectLen += 5;
455 }
456 else if (hdr->Patient.Impairment.Heart==3) {
457 *(ptr+sectionStart+curSectLen+3) = 0;
458 *(ptr+sectionStart+curSectLen+4) = 42; // Implanted cardiac pacemaker
459 curSectLen += 5;
460 }
461
462 // Tag 34 (len = 5)
463 *(ptr+sectionStart+curSectLen) = 34; // tag
464 leu16a(5, ptr+sectionStart+curSectLen+1); // length
465 lei16a(hdr->tzmin, ptr+sectionStart+curSectLen+3);
466 lei16a(0, ptr+sectionStart+curSectLen+5);
467 curSectLen += 8;
468
469 // Tag 255 (len = 0)
470 *(ptr+sectionStart+curSectLen) = 255; // tag
471 leu16a(0, ptr+sectionStart+curSectLen+1); // length
472 curSectLen += 3;
473
474 // Evaluate the size and correct it if odd
475 if (curSectLen & 1) {
476 *(ptr+sectionStart+curSectLen++) = 0;
477 }
478
479 if (VERBOSE_LEVEL>7) fprintf(stdout,"End-of-Section %i %p\n",curSect,ptr);
480
481 }
482 else if (curSect==2) // SECTION 2
483 {
484 }
485 else if (curSect==3) // SECTION 3
486 {
487 ptr = (uint8_t*)realloc(ptr,sectionStart+16+2+9*NS+1);
488 PtrCurSect = ptr+sectionStart;
489 curSectLen = 16; // current section length
490
491 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) data.size=[%d,%d]\n",__FILE__,__LINE__,__func__,hdr->data.size[0],hdr->data.size[1]);
492
493 // Number of leads enclosed
494 *(ptr+sectionStart+curSectLen++) = NS;
495
496 // ### Situations with reference beat subtraction are not supported
497 // Situations with not all the leads simultaneously recorded are not supported
498 // Situations number of leads simultaneouly recorded != total number of leads are not supported
499 // We assume all the leads are recorded simultaneously
500 *(ptr+sectionStart+curSectLen++) = (NS<<3) | 0x04;
501 uint32_t SPR = hdr->SPR * hdr->NRec;
502 for (i = 0; i < hdr->NS; i++) {
503 CHANNEL_TYPE *CH=hdr->CHANNEL+i;
504 if (CH->OnOff != 1) continue;
505
506 leu32a(1L, ptr+sectionStart+curSectLen);
507 leu32a(SPR, ptr+sectionStart+curSectLen+4);
508 *(ptr+sectionStart+curSectLen+8) = (uint8_t)CH->LeadIdCode;
509 curSectLen += 9;
510 }
511
512 // Evaluate the size and correct it if odd
513 if ((curSectLen % 2) != 0) {
514 *(ptr+sectionStart+curSectLen++) = 0;
515 }
516 memset(ptr+sectionStart+10,0,6); // reserved
517 }
518 else if (curSect==4) // SECTION 4
519 {
520 }
521 else if (curSect==5) // SECTION 5
522 {
523 curSectLen = 0; // current section length
524 aECG->Section5.StartPtr = sectionStart;
525 aECG->Section5.Length = curSectLen;
526 }
527 else if (curSect==6) // SECTION 6
528 {
529
530 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): Section %d v%3.1g %i,%i \n",__func__,__LINE__,curSect, versionSection*0.1,(int)aECG->Section6.StartPtr,(int)aECG->Section6.Length);
531
532 if (versionSection < 25) // SECTION 6
533 {
534 uint16_t GDFTYP = 3;
535 size_t SZ = GDFTYP_BITS[GDFTYP]>>3;
536 typeof(hdr->NS) i=0;
537 for (i = 0; i < hdr->NS; i++) {
538 CHANNEL_TYPE *hc=hdr->CHANNEL+i;
539 if (hc->OnOff != 1) continue;
540 hc->GDFTYP = GDFTYP;
541 }
542 ptr = (uint8_t*)realloc(ptr,sectionStart+16+6+2*NS+SZ*(hdr->data.size[0]*hdr->data.size[1]));
543
544 PtrCurSect = ptr+sectionStart;
545 curSectLen = 16; // current section length
546
547 // Create all the fields
548 char flagfirst = 1;
549 for (i = 0; i < hdr->NS; i++) {
550 CHANNEL_TYPE *CH=hdr->CHANNEL+i;
551
552 if (VERBOSE_LEVEL>8) fprintf(stdout,"%s (line %i): %d#%d %g/%g \n",__func__,__LINE__,CH->OnOff,i,avm,AVM);
553
554 if (CH->OnOff != 1) continue;
555
556 // check for physical dimension and adjust scaling factor to "nV"
557 avm = CH->Cal * 1e9 * PhysDimScale(CH->PhysDimCode);
558 if (flagfirst) {
559 // Amplitude Value Multiplier (AVM)
560 AVM = avm;
561 flagfirst=0;
562 continue;
563 }
564
565 // check whether all channels have the same scaling factor
566 if (fabs((AVM - avm)/AVM) > 1e-14)
567 fprintf(stderr,"Warning SOPEN (SCP-WRITE): scaling factors differ between channel #1 and #%i. Scaling factor of 1st channel is used.\n",i+1);
568 };
569 avm16 = lrint(AVM);
570 leu16a(avm16, ptr+sectionStart+curSectLen);
571 avm = leu16p(ptr+sectionStart+curSectLen);
572
573 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): avm:%g avm16:%d AVM:%g\n", __func__, __LINE__, avm, avm16, AVM);
574
575 curSectLen += 2;
576 if (fabs((AVM - avm)/AVM)>1e-14)
577 fprintf(stderr,"Warning SOPEN (SCP-WRITE): Scaling factor has been truncated (%f instead %f).\n",avm,AVM);
578
579 // Sample interval
580 AVM = 1e6/hdr->SampleRate;
581 avm16 = lrint(AVM);
582 leu16a(avm16, ptr+sectionStart+curSectLen);
583 avm = leu16p(ptr+sectionStart+curSectLen);
584 curSectLen += 2;
585 if (fabs((AVM - avm)/AVM)>1e-14)
586 fprintf(stderr,"Warning SOPEN (SCP-WRITE): Sampling interval has been truncated (%f instead %f us).\n",avm,AVM);
587
588 // Diff used
589 *(ptr+sectionStart+curSectLen++) = 0;
590
591 // Bimodal/Non-bimodal
592 *(ptr+sectionStart+curSectLen++) = 0;
593
594
595 /* DATA COMPRESSION
596 currently, no compression method is supported. In case of data compression, the
597 data compression can happen here.
598 */
599
600 // Fill the length block
601 for (i = 0; i < NS; i++) {
602 leu16a((uint16_t)hdr->data.size[0]*2, ptr+sectionStart+curSectLen);
603 avm = leu16p(ptr+sectionStart+curSectLen);
604 AVM = hdr->SPR*hdr->NRec*2;
605 if (fabs((AVM - avm)/AVM)>1e-14)
606 fprintf(stderr,"Warning SOPEN (SCP-WRITE): Block length truncated (%f instead %f us).\n",avm,AVM);
607 curSectLen += 2;
608 }
609
610 /* data in channel multiplexed order */
611 for (i = 0; i < hdr->NS; i++) {
612 hdr->CHANNEL[i].SPR *= hdr->NRec;
613 };
614 hdr->NRec = 1;
615
616 // Prepare filling the data block with the ECG samples by SWRITE
617 curSectLen += SZ * hdr->SPR*hdr->NRec * NS;
618
619 // Evaluate the size and correct it if odd
620 if ((curSectLen % 2) != 0) {
621 fprintf(stderr,"Warning Section 6 has an odd length\n");
622 *(ptr+sectionStart+curSectLen++) = 0;
623 }
624
625 memset(ptr+sectionStart+10,0,6); // reserved
626 aECG->Section6.StartPtr = sectionStart;
627 aECG->Section6.Length = curSectLen;
628 }
629 }
630 else if (curSect==7) // SECTION 7
631 {
632 if (hdr->SCP.Section7 != NULL) {
633 curSectLen = hdr->SCP.Section7Length+16; // current section length
634 ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen);
635 PtrCurSect = ptr+sectionStart;
636 memcpy(PtrCurSect+16,hdr->SCP.Section7,hdr->SCP.Section7Length);
637 }
638 }
639 else if (curSect==8) // SECTION 8
640 {
641 if (hdr->SCP.Section8 != NULL) {
642 curSectLen = hdr->SCP.Section8Length+16; // current section length
643 ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen);
644 PtrCurSect = ptr+sectionStart;
645 memcpy(PtrCurSect,hdr->SCP.Section8,hdr->SCP.Section8Length);
646 }
647 }
648 else if (curSect==9) // SECTION 9
649 {
650 if (hdr->SCP.Section9 != NULL) {
651 curSectLen = hdr->SCP.Section9Length+16; // current section length
652 ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen);
653 PtrCurSect = ptr+sectionStart;
654 memcpy(PtrCurSect,hdr->SCP.Section9,hdr->SCP.Section9Length);
655 }
656 }
657 else if (curSect==10) // SECTION 10
658 {
659 if (hdr->SCP.Section10 != NULL) {
660 curSectLen = hdr->SCP.Section10Length+16; // current section length
661 ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen);
662 PtrCurSect = ptr+sectionStart;
663 memcpy(PtrCurSect+16,hdr->SCP.Section10,hdr->SCP.Section10Length);
664 }
665 }
666 else if (curSect==11) // SECTION 11
667 {
668 if (hdr->SCP.Section11 != NULL) {
669 curSectLen = hdr->SCP.Section11Length+16; // current section length
670 ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen);
671 PtrCurSect = ptr+sectionStart;
672 memcpy(PtrCurSect+16,hdr->SCP.Section11,hdr->SCP.Section11Length);
673 }
674 }
675 else if (curSect==12) // SECTION 12
676 {
677 if (versionSection > 25 ) // SECTION 12, SCP version 3
678 {
679 uint16_t gdftyp= 0;
680 uint8_t bps = 0;
681 //double PhysMax = -1.0/0.0;
682 uint16_t PhysDimBaseCode = 4256; // Volt
683 for (k = 0; k < hdr->NS; k++) {
684 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
685 if (hc->OnOff == 1) {
686 if (bps < GDFTYP_BITS[gdftyp]) {
687 bps = GDFTYP_BITS[hc->GDFTYP];
688 gdftyp = hc->GDFTYP;
689 }
690 }
691 }
692
693 if (NS>255) {
694 NS=255;
695 fprintf(stderr,"Warning SOPEN (SCP-WRITE): Number of channels exceeds 255, limited to 255.\n");
696 }
697
698 bps = GDFTYP_BITS[gdftyp]>>3;
699 double DigMax = ldexp(1.0, GDFTYP_BITS[gdftyp]-1)-1;
700
701 int ns=0;
702 for (k=0; k < hdr->NS; k++) {
703 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
704 if (hc->OnOff > 0) {
705 if (ns > 255) {
706 hc->OnOff = 0;
707 continue;
708 }
709 ns++;
710
711 double scale = PhysDimScale(hc->PhysDimCode);
712 hc->GDFTYP = gdftyp;
713 hc->PhysMax = max(fabs(hc->PhysMax),fabs(-hc->PhysMin))*scale*1e9;
714 hc->PhysMin = -hc->PhysMax;
715 hc->Off = 0.0;
716 hc->Cal = ceil(hc->PhysMax / hc->DigMax); // AVM must be integer
717 //hc->DigMax = hc->PhysMax / hc->Cal;
718 hc->DigMin = -hc->DigMax;
719 hc->PhysDimCode = 4276; // nV
720 }
721 }
722
723 curSectLen = 16 + 70 + NS * (4 + hdr->SPR*hdr->NRec * bps); // current section length without 16 bytes
724 curSectLen+= (curSectLen & 1);
725
726 ptr = (uint8_t*)realloc(ptr, sectionStart + curSectLen);
727 memset(ptr + sectionStart, 0, 16+70);
728
729 PtrCurSect = ptr + sectionStart + 16;
730 leu32a((uint32_t)hdr->SampleRate, PtrCurSect);
731 PtrCurSect[4] = (uint8_t)NS;
732 leu32a((uint32_t)(hdr->NRec*hdr->SPR), PtrCurSect + 5);
733 PtrCurSect[9] = (uint8_t)bps;
734
735 // Recording Date and Time
736 #if __FreeBSD__ || __APPLE__ || __NetBSD__
737 T0_tm = gdf_time2tm_time(hdr->T0);
738 T0_tm->tm_gmtoff = hdr->tzmin*60;
739 #else
740 gdf_time T1 = hdr->T0;
741 T1 += (int32_t)ldexp(timezone/86400.0,32);
742 T0_tm = gdf_time2tm_time(T1);
743 #endif
744
745 leu16a(T0_tm->tm_year+1900, PtrCurSect+10); // year
746 PtrCurSect[12] = (uint8_t)(T0_tm->tm_mon + 1); // month
747 PtrCurSect[13] = (uint8_t)T0_tm->tm_mday; // day
748 PtrCurSect[14] = (uint8_t)T0_tm->tm_hour; // hour
749 PtrCurSect[15] = (uint8_t)T0_tm->tm_min; // minute
750 PtrCurSect[16] = (uint8_t)T0_tm->tm_sec; // second
751 // all other fields are set to zero. filter settings are defined in Section 1, Tags 27-29
752
753 /* Leads Definition block */
754 PtrCurSect += 70;
755 for (k=0,ns=0; k < hdr->NS; k++) {
756 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
757 if (hc->OnOff > 0) {
758 PtrCurSect[ns*4] = hc->LeadIdCode;
759 leu16a(hc->Cal, PtrCurSect+1+ns*4); // AVM per lead
760 PtrCurSect[3+ns*4] = 0;
761 ns++;
762 }
763 }
764
765 /* ECG signals data block */
766 PtrCurSect += 4*NS;
767 }
768 aECG->Section12.StartPtr = sectionStart;
769 aECG->Section12.Length = curSectLen;
770 }
771 else {
772 }
773
774 // write to pointer field in Section 0
775 leu16a(curSect, ptr+curSect*10+6+16); //
776 leu32a(curSectLen, ptr+curSect*10+6+16+2); // length
777 // Section start - must be odd. See EN1064:2005(E) Section 5.2.1
778
779 // write to Section ID Header
780 if (curSectLen>0) {
781 // Section 0: startpos in pointer field
782 leu32a(sectionStart+1, ptr+curSect*10+6+16+6);
783
784 // Section ID header (16 bytes)
785 leu16a(curSect, ptr+sectionStart+2); // Section ID
786 leu32a(curSectLen, ptr+sectionStart+4); // section length->section header
787 ptr[sectionStart+8] = versionSection; // Section Version Number
788 ptr[sectionStart+9] = versionProtocol; // Protocol Version Number
789 if (curSect==0) {
790 memcpy(ptr+16,"SCPECG",6); // defined as in ISO/DIS 11073-91064 Section 5.3.2
791 }
792 else {
793 memset(ptr+sectionStart+10,0,6); // reserved according to ISO/DIS 11073-91064 Section 5.2.7
794 }
795 crc = CRCEvaluate(ptr+sectionStart+2,curSectLen-2); // compute CRC
796 leu16a(crc, ptr+sectionStart);
797 }
798 sectionStart += curSectLen; // offset for next section
799 }
800
801 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...)\n",__FILE__,__LINE__,__func__);
802
803 // Prepare filling the data block with the ECG samples by SWRITE
804 if (versionSection < 25)
805 hdr->AS.rawdata = ptr+aECG->Section6.StartPtr+16+6+2*NS;
806 else
807 hdr->AS.rawdata = ptr+aECG->Section12.StartPtr+16+70+4*NS;
808
809 hdr->AS.Header = ptr;
810
811 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %i,%i %i,%i \n",__func__,__LINE__,(int)aECG->Section6.StartPtr,(int)aECG->Section6.Length,(int)aECG->Section12.StartPtr,(int)aECG->Section12.Length);
812
813 return(0);
814 }
815
816 #ifdef __cplusplus
817 }
818 #endif
819