Debian Codegraph @

biosig4c++/t230/sopen_hl7aecg.cpp

raw symbols SCIP
1 /*
2
3 Copyright (C) 2006,2007,2009,2011,2012,2018 Alois Schloegl <[email protected]>
4 Copyright (C) 2007 Elias Apostolopoulos
5 Copyright (C) 2011 Stoyan Mihaylov
6 Copyright (C) 2018 Stefan Soueiha <[email protected]>
7
8 This file is part of the "BioSig for C/C++" repository
9 (biosig4c++) at http://biosig.sf.net/
10
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 3
15 of the License, or (at your option) any later version.
16
17
18 */
19
20
21 #include <stdio.h> // system includes
22 #include <stdlib.h> // for strtod(3)
23 #include <strings.h>
24
25 #include "../biosig.h"
26 #if defined(WITH_LIBTINYXML)
27 # define TIXML_USE_STL
28 # include <tinyxml.h>
29 #endif
30
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
32 #define max(a,b) (((a) > (b)) ? (a) : (b))
33
34 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
35 convert time in string format into gdf-time format
36 Currently, the following formats are supported
37 YYYYMMDDhhmmss.uuuuuu
38 YYYYMMDDhhmmss
39 YYYYMMDD
40 in case of error, zero is returned
41 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
42 EXTERN_C gdf_time str_time2gdf_time(const char *t1) {
43
44 struct tm t0;
45 gdf_time T;
46 double fracsec = 0.0;
47 double f;
48 int len;
49 #define MAXLEN 22
50 char t[MAXLEN+1];
51
52 if (t1==NULL) return(0);
53 len = strlen(t1);
54 if (len>MAXLEN) return(0);
55 if (len<8) return(0);
56 strncpy(t,t1,MAXLEN);
57 t[len] = 0;
58
59 if (VERBOSE_LEVEL>8)
60 fprintf(stdout,"str_time2gdf_time: [%i]<%s>\n",len,t1);
61
62 char *p = strrchr(t,'.');
63 if (p==NULL) {
64 // no comma
65 p = t+len;
66 }
67 else {
68 for (p++, f=0.1; p[0]; p++, f=f/10) {
69 if (p[0]<'0' || p[0]>'9') return(0);
70 fracsec += (p[0]-'0')*f;
71 }
72 p = strrchr(t,'.');
73 }
74
75 if (VERBOSE_LEVEL>8)
76 fprintf(stdout,"str_time2gdf_time: [%i]<%s>\n",len,t1);
77
78 if (len>=14) {
79 // decode hhmmss
80 p[0] = '\0'; p-=2;
81 t0.tm_sec = atoi(p);
82 p[0] = '\0'; p-=2;
83 t0.tm_min = atoi(p);
84 p[0] = '\0'; p-=2;
85 t0.tm_hour = atoi(p);
86 p[0] = '\0';
87 }
88 else {
89 t0.tm_sec = 0;
90 t0.tm_min = 0;
91 t0.tm_hour = 0;
92 }
93 p -= 2;
94 t0.tm_mday = atoi(p);
95
96 p[0] = '\0'; p-=2;
97 t0.tm_mon = atoi(p)-1;
98
99 p[0] = '\0'; p-=4;
100 t0.tm_year = atoi(t)-1900;
101 t0.tm_isdst = -1;
102 T = tm_time2gdf_time(&t0);
103
104 if (fracsec>0)
105 T += ldexp(fracsec/86400,32);
106
107 if (VERBOSE_LEVEL>8)
108 fprintf(stdout,"str_time2gdf_time: [%i]<%s>\n",len,t1);
109
110 return(T);
111 }
112
113
114 EXTERN_C int sopen_HL7aECG_read(HDRTYPE* hdr) {
115 /*
116 this function is a stub or placeholder and need to be defined in order to be useful.
117 It will be called by the function SOPEN in "biosig.c"
118
119 Input:
120 char* Header // contains the file content
121
122 Output:
123 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
124 */
125
126 char tmp[80];
127
128 if (VERBOSE_LEVEL > 7) fprintf(stdout,"hl7r: [410]\n");
129
130 #if defined(TINYXML_INCLUDED)
131 TiXmlDocument doc(hdr->FileName);
132
133 if (VERBOSE_LEVEL > 7) fprintf(stdout,"hl7r: [411]\n");
134
135 if ( doc.LoadFile() ) {
136
137 if (VERBOSE_LEVEL > 7) fprintf(stdout,"hl7r: [412]\n");
138
139 TiXmlHandle hDoc(&doc);
140 TiXmlHandle geECG = hDoc.FirstChild("CardiologyXML");
141 TiXmlHandle IHE = hDoc.FirstChild("IHEDocumentList");
142 TiXmlHandle aECG = hDoc.FirstChild("AnnotatedECG");
143 TiXmlHandle SierraECG = hDoc.FirstChild("restingECG");
144 TiXmlHandle SierraECG2 = hDoc.FirstChild("restingecgdata");
145
146 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [412]\n");
147
148 if (SierraECG.Element()) {
149 fprintf(stdout,"Great! Philips Sierra ECG is recognized\n");
150 }
151
152 else if (SierraECG2.Element()) {
153 const char *t;
154 const char *e;
155 float notch = 0, lowpass=0, highpass=0;
156 //uint16_t gdftyp = 16;
157 struct tm t0;
158
159 fprintf(stdout,"Great! Philips Sierra ECG 2 is recognized\n");
160 TiXmlHandle H = SierraECG2.FirstChild("dataacquisition");
161
162 if (H.Element()) {
163 e = SierraECG2.FirstChild("dataacquisition").Element()->Attribute("date");
164 t0.tm_year = (int)strtod(e,(char**)&e) - 1900;
165 t0.tm_mon = (int)strtod(e+1,(char**)&e) - 1 ;
166 t0.tm_mday = (int)strtod(e+1,(char**)&e);
167 e = SierraECG2.FirstChild("dataacquisition").Element()->Attribute("time");
168 t0.tm_hour = (int)strtod(e+1,(char**)&e);
169 t0.tm_min = (int)strtod(e+1,(char**)&e);
170 t0.tm_sec = (int)strtod(e+1,(char**)&e);
171 hdr->T0 = tm_time2gdf_time(&t0);
172 }
173
174 H = SierraECG2.FirstChild("dataacquisition").FirstChild("signalcharacteristics").FirstChild("acsetting");
175 if (H.Element()) notch = atof(H.Element()->GetText());
176 H = SierraECG2.FirstChild("reportinfo").FirstChild("reportbandwidth").FirstChild("lowpassfiltersetting");
177 if (H.Element()) lowpass = atof(H.Element()->GetText());
178 H = SierraECG2.FirstChild("reportinfo").FirstChild("reportbandwidth").FirstChild("highpassfiltersetting");
179 if (H.Element()) highpass = atof(H.Element()->GetText());
180
181 H = SierraECG2.FirstChild("dataacquisition").FirstChild("acquirer").FirstChild("institutionname");
182 if (H.Element()) hdr->ID.Hospital = strdup(H.Element()->GetText());
183
184 H = SierraECG2.FirstChild("dataacquisition").FirstChild("signalcharacteristics").FirstChild("samplingrate");
185 if (H.Element()) hdr->SampleRate = atof(H.Element()->GetText());
186
187 H = SierraECG2.FirstChild("dataacquisition").FirstChild("signalcharacteristics").FirstChild("signalresolution");
188 double Cal = 1.0; if (H.Element()) Cal = atof(H.Element()->GetText());
189
190 H = SierraECG2.FirstChild("dataacquisition").FirstChild("signalcharacteristics").FirstChild("numberchannelsvalid");
191 if (H.Element()) hdr->NS = atoi(H.Element()->GetText());
192
193 H = SierraECG2.FirstChild("dataacquisition").FirstChild("signalcharacteristics").FirstChild("numberchannelsallocated");
194 if (H.Element()) {
195 if (hdr->NS != atoi(H.Element()->GetText()) )
196 fprintf(stdout,"SierraECG: number of channels is ambiguous\n");
197 }
198
199 H = SierraECG2.FirstChild("patient").FirstChild("generalpatientdata").FirstChild("patientid");
200 if (H.Element()) memcpy(hdr->Patient.Id, H.Element()->GetText(), MAX_LENGTH_PID);
201 hdr->Patient.Name[0] = 0;
202 size_t NameLength = 0;
203 H = SierraECG2.FirstChild("patient").FirstChild("generalpatientdata").FirstChild("name").FirstChild("lastname");
204 if (H.Element()) {
205 strncpy(hdr->Patient.Name, H.Element()->GetText(), MAX_LENGTH_NAME);
206 hdr->Patient.Name[MAX_LENGTH_NAME]=0;
207 NameLength = strlen(hdr->Patient.Name);
208 }
209 H = SierraECG2.FirstChild("patient").FirstChild("generalpatientdata").FirstChild("name").FirstChild("firstname");
210 if (H.Element()) {
211 const char *str = H.Element()->GetText();
212 size_t l2 = strlen(str);
213 if (NameLength+l2+1 < MAX_LENGTH_NAME) {
214 hdr->Patient.Name[NameLength]= 0x1f; // unit separator ascii(31)
215 strncpy(hdr->Patient.Name+NameLength+1, str, l2+1);
216 NameLength += l2+1;
217 }
218 }
219 H = SierraECG2.FirstChild("patient").FirstChild("generalpatientdata").FirstChild("name").FirstChild("middlename");
220 if (H.Element()) {
221 const char *str = H.Element()->GetText();
222 size_t l2 = strlen(str);
223 if (NameLength+l2+1 < MAX_LENGTH_NAME) {
224 hdr->Patient.Name[NameLength]= ' ';
225 strncpy(hdr->Patient.Name+NameLength+1, str, l2+1);
226 NameLength += l2+1;
227 }
228 }
229 H = SierraECG2.FirstChild("patient").FirstChild("generalpatientdata").FirstChild("age").FirstChild("years");
230 // if (H.Element()) hdr->Patient.Age != atoi(H.Element()->GetText())
231
232 H = SierraECG2.FirstChild("patient").FirstChild("generalpatientdata").FirstChild("sex");
233 if (H.Element()) {
234 t = H.Element()->GetText();
235 hdr->Patient.Sex = (t[0]=='M') + (t[0]=='m') + 2 * ( (t[0]=='F') + (t[0]=='f') );
236 }
237
238 H = SierraECG2.FirstChild("waveforms").FirstChild("parsedwaveforms");
239 if (H.Element()) {
240 hdr->SPR = atof(H.Element()->Attribute("durationperchannel"))*hdr->SampleRate/1000;
241 }
242 hdr->NRec = 1;
243 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
244 for (uint16_t k=0; k<hdr->NS; k++) {
245 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
246 hc->GDFTYP = 16;
247 sprintf(hc->Label,"#%i",k);
248 hc->Transducer[0] = 0;
249 hc->Cal = Cal;
250 hc->Off = 0.0;
251 hc->OnOff = 1;
252 hc->DigMin = -ldexp(1,15);
253 hc->DigMax = +ldexp(1,15)-1;
254 hc->PhysMin = hc->DigMin * Cal;
255 hc->PhysMax = hc->DigMax * Cal;
256 hc->PhysDimCode = PhysDimCode("nV");
257 //strcpy(hc->PhysDim,"nV");
258 hc->bi = k*hdr->SPR*4;
259 hc->bi8 = 0;
260 hc->LeadIdCode = 0;
261 hc->SPR = hdr->SPR;
262 hc->LowPass = lowpass;
263 hc->HighPass = highpass;
264 hc->Notch = notch;
265 hc->TOffset = 0;
266 hc->XYZ[0] = 0;
267 hc->XYZ[1] = 0;
268 hc->XYZ[2] = 0;
269 }
270 hdr->AS.bpb = sizeof(float)*hdr->NS;
271 hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata, hdr->AS.bpb*hdr->NRec*hdr->SPR);
272 hdr->AS.first = 0;
273 hdr->AS.length= hdr->NRec;
274
275 if (H.Element()) {
276 const char *e = H.Element()->GetText();
277 size_t k = 0;
278 while (e != NULL && k < hdr->SPR * (size_t)hdr->NRec * hdr->NS) {
279 ((float*)hdr->AS.rawdata)[k++] = (float)strtod(e,(char**)&e);
280 }
281 }
282
283 }
284 else if (geECG.Element()) {
285 hdr->ID.Manufacturer.Name = "GE";
286
287 TiXmlHandle H = geECG.FirstChild("ClinicalInfo").FirstChild("ObservationDateTime");
288 if (H.Element()) {
289 struct tm t0;
290 t0.tm_hour = atoi(H.FirstChild("Hour").Element()->GetText());
291 t0.tm_min = atoi(H.FirstChild("Minute").Element()->GetText());
292 t0.tm_sec = atoi(H.FirstChild("Second").Element()->GetText());
293 t0.tm_mday = atoi(H.FirstChild("Day").Element()->GetText());
294 t0.tm_mon = atoi(H.FirstChild("Month").Element()->GetText())-1;
295 t0.tm_year = atoi(H.FirstChild("Year").Element()->GetText())-1900;
296 hdr->T0 = tm_time2gdf_time(&t0);
297 }
298
299 H = geECG.FirstChild("Device-Type");
300 if (H.Element()) {
301 strncpy(hdr->ID.Manufacturer._field, H.Element()->GetText(),MAX_LENGTH_PID);
302 hdr->ID.Manufacturer.Model = hdr->ID.Manufacturer._field;
303 }
304
305 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [413]\n");
306
307 H = geECG.FirstChild("PatientInfo");
308 if (H.Element()) {
309 strncpy(hdr->Patient.Id, H.FirstChild("PID").Element()->GetText(),MAX_LENGTH_PID);
310 const char *tmp = H.FirstChild("PID").Element()->GetText();
311 hdr->Patient.Sex = (toupper(tmp[0])=='M') + 2*(toupper(tmp[0])=='F');
312 if (!hdr->FLAG.ANONYMOUS) {
313 const char *str1 = H.FirstChild("Name").FirstChild("FamilyName").Element()->GetText();
314 const char *str2 = H.FirstChild("Name").FirstChild("GivenName").Element()->GetText();
315 size_t l1 = str1 ? strlen(str1) : 0;
316 size_t l2 = str2 ? strlen(str2) : 0;
317 if (0 < l1 && l1 <= MAX_LENGTH_PID) strncpy(hdr->Patient.Name, str1, l1+1);
318 if (l1+l2+1 < MAX_LENGTH_PID) {
319 hdr->Patient.Name[l1] = 0x1f;
320 strncpy(hdr->Patient.Name+l1+1, str2, l2+1);
321 }
322 }
323 }
324
325 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [413]\n");
326
327 double Cal=0.0, LP=NAN, HP=NAN, Notch=0.0;
328 hdr->NRec= 0;
329 hdr->SPR = 1;
330 hdr->NS = 1;
331
332 H = geECG.FirstChild("FilterSetting");
333 if (H.Element()) {
334 LP = atof(H.FirstChild("LowPass").Element()->GetText());
335 HP = atof(H.FirstChild("HighPass").Element()->GetText());
336 if (!strcasecmp("yes",H.FirstChild("Filter50Hz").Element()->GetText()))
337 Notch = 50;
338 else if (!strcasecmp("yes",H.FirstChild("Filter60Hz").Element()->GetText()))
339 Notch = 60;
340 }
341
342 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [413]\n");
343
344 H = geECG.FirstChild("StripData");
345 TiXmlElement *C = NULL;
346 if (H.Element()) {
347 C = H.FirstChild("NumberOfLeads").Element();
348 if (C != NULL) hdr->NS = atoi(C->GetText());
349
350 C = H.FirstChild("ChannelSampleCountTotal").Element();
351 if (C != NULL) hdr->NRec = atoi(C->GetText());
352
353 hdr->SampleRate = atof(H.FirstChild("SampleRate").Element()->GetText());
354 Cal = atof(H.FirstChild("Resolution").Element()->GetText());
355 }
356
357 uint16_t gdftyp = 3;
358 hdr->AS.bpb = 0;
359
360 int k=0, NCHAN = hdr->NS;
361 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS*sizeof(CHANNEL_TYPE));
362 C = H.FirstChild("WaveformData").Element();
363 while (C != NULL) {
364
365 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [413] %i\n",k);
366
367 if (k>=NCHAN) {
368 NCHAN = max(12,(NCHAN+1)*2);
369 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, NCHAN*sizeof(CHANNEL_TYPE));
370 }
371
372 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
373 // default values
374 hc->GDFTYP = gdftyp;
375 hc->PhysDimCode = 4275; //PhysDimCode("uV");
376 hc->DigMin = (double)(int16_t)0x8000;
377 hc->DigMax = (double)(int16_t)0x7fff;
378 strncpy(hc->Label, C->Attribute("lead"), MAX_LENGTH_LABEL);
379 hc->Transducer[0] = 0;
380
381 hc->LeadIdCode = 0;
382 size_t j;
383 for (j=0; strcasecmp(hc->Label, LEAD_ID_TABLE[j]) && LEAD_ID_TABLE[j][0]; j++) {};
384 if (LEAD_ID_TABLE[j][0])
385 hc->LeadIdCode = j;
386
387 hc->LowPass = LP;
388 hc->HighPass = HP;
389 hc->Notch = Notch;
390 hc->Impedance = NAN;
391 hc->XYZ[0] = 0.0;
392 hc->XYZ[1] = 0.0;
393 hc->XYZ[2] = 0.0;
394
395 // defined
396 hc->Cal = Cal;
397 hc->Off = 0.0;
398 hc->SPR = 1;
399 hc->OnOff = 1;
400
401 C = C->NextSiblingElement();
402 k++;
403 }
404
405 hdr->NS = k;
406
407 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [417] %i\n",hdr->NS);
408
409
410 C = H.FirstChild("WaveformData").Element();
411 size_t szRawData = 0;
412 size_t SPR = 0;
413 for (k=0; k<hdr->NS; k++) {
414
415 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [415] %i\n",k);
416
417 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
418
419 /* read data samples */
420 hc->DigMax = -1.0/0.0;
421 hc->DigMin = 1.0/0.0;
422 // int16_t* data = (int16_t*)(hdr->AS.rawdata)+SPR;
423
424 hc->bi = hdr->AS.bpb;
425 char *s = (char*)C->GetText();
426 const char *delim = ",";
427 size_t spr = 0;
428 while (s && *s) {
429 s += strspn(s, delim); //skip kommas
430 if (SPR+spr+1 >= szRawData) {
431 szRawData = max(5000,2*szRawData);
432 hdr->AS.rawdata = (uint8_t*) realloc(hdr->AS.rawdata, szRawData * sizeof(int16_t));
433 }
434 double d = strtod(s,&s);
435 ((int16_t*)hdr->AS.rawdata)[SPR+spr++] = d;
436 /* get Min/Max */
437 if (d > hc->DigMax) hc->DigMax = d;
438 if (d < hc->DigMin) hc->DigMin = d;
439 }
440 SPR += spr;
441 hc->SPR = spr;
442 hdr->SPR = lcm(hdr->SPR,spr);
443
444 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
445 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
446
447 C = C->NextSiblingElement();
448 }
449 hdr->AS.bpb += hdr->SPR * 2;
450 hdr->NRec = 1;
451 hdr->AS.first = 0;
452 hdr->AS.length = hdr->NRec;
453
454 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [497] %i\n",hdr->NS);
455
456
457 }
458 else if (IHE.Element()) {
459
460 fprintf(stderr,"XML IHE: support for IHE XML is experimental - some important features are not implemented yet \n");
461
462 TiXmlHandle activityTime = IHE.FirstChild("activityTime");
463 TiXmlHandle recordTarget = IHE.FirstChild("recordTarget");
464 TiXmlHandle author = IHE.FirstChild("author");
465 /*
466 an IHE file can contain several segments (i.e. components)
467 need to implement TARGET_SEGMENT feature
468 */
469 TiXmlHandle component = IHE.FirstChild("component");
470
471 if (VERBOSE_LEVEL>8)
472 fprintf(stdout,"IHE: [413] \n");
473
474 if (author.FirstChild("assignedAuthor").Element()) {
475 // TiXmlHandle noteText = author.FirstChild("noteText").Element();
476 TiXmlHandle assignedAuthor = author.FirstChild("assignedAuthor").Element();
477 if (assignedAuthor.FirstChild("assignedDevice").Element()) {
478 TiXmlHandle assignedDevice = assignedAuthor.FirstChild("assignedDevice").Element();
479 hdr->ID.Manufacturer.Name = hdr->ID.Manufacturer._field;
480
481 if (assignedDevice.Element()) {
482 strncpy(hdr->ID.Manufacturer._field, assignedDevice.FirstChild("manufacturerModelName").Element()->GetText(), MAX_LENGTH_MANUF);
483 int len = strlen(hdr->ID.Manufacturer._field)+1;
484 hdr->ID.Manufacturer.Model = hdr->ID.Manufacturer._field+len;
485 strncpy(hdr->ID.Manufacturer._field+len, assignedDevice.FirstChild("code").Element()->Attribute("code"),MAX_LENGTH_MANUF-len);
486 len += strlen(hdr->ID.Manufacturer.Model)+1;
487 }
488 }
489 }
490
491 if (recordTarget.FirstChild("patient").Element()) {
492 TiXmlHandle patient = recordTarget.FirstChild("patient").Element();
493
494 TiXmlHandle id = patient.FirstChild("id").Element();
495 TiXmlHandle patientPatient = patient.FirstChild("patientPatient").Element();
496 TiXmlHandle providerOrganization = patient.FirstChild("providerOrganization").Element();
497
498 if (VERBOSE_LEVEL>8)
499 fprintf(stdout,"IHE: [414] %p %p %p\n",id.Element(),patientPatient.Element(),providerOrganization.Element());
500
501 if (id.Element()) {
502 char *strtmp = strdup(id.Element()->Attribute("root"));
503 size_t len = strlen(strtmp);
504 if (len <= MAX_LENGTH_RID) {
505 strcpy(hdr->ID.Recording, strtmp); // Flawfinder: ignore
506
507 if (strtmp) free(strtmp);
508 strtmp = strdup(id.Element()->Attribute("extension"));
509 size_t l1 = strlen(strtmp);
510 if (len+1+l1 < MAX_LENGTH_RID) {
511 len += 1 + l1;
512 hdr->ID.Recording[len] = ' ';
513 memcpy(hdr->ID.Recording+len+1,strtmp,l1+1);
514 }
515 else
516 fprintf(stdout,"Warning HL7aECG(read): length of Recording ID exceeds maximum length %i>%i\n",(int)(len+1+l1),MAX_LENGTH_PID);
517 }
518 else
519 fprintf(stdout,"Warning HL7aECG(read): length of Recording ID exceeds maximum length %i>%i\n",(int)len,MAX_LENGTH_PID);
520
521 if (strtmp) free(strtmp);
522 if (VERBOSE_LEVEL>7)
523 fprintf(stdout,"IHE (read): length of Recording ID %i,%i\n",(int)len,MAX_LENGTH_PID);
524 }
525 if (VERBOSE_LEVEL>7)
526 fprintf(stdout,"IHE: [414] RID= %s\n",hdr->ID.Recording);
527
528 if (providerOrganization.Element()) {
529 hdr->ID.Hospital = strdup(providerOrganization.FirstChild("name").Element()->GetText());
530 }
531
532 if (VERBOSE_LEVEL>7)
533 fprintf(stdout,"IHE: [414] hospital %s\n",hdr->ID.Hospital);
534
535 if (patientPatient.Element()) {
536 if (!hdr->FLAG.ANONYMOUS) {
537 TiXmlHandle Name = patientPatient.FirstChild("name").Element();
538 if (Name.Element()) {
539 char *str1 = strdup(Name.FirstChild("family").Element()->GetText());
540 char *str2 = strdup(Name.FirstChild("given").Element()->GetText());
541 size_t l1 = str1 ? strlen(str1) : 0;
542 size_t l2 = str2 ? strlen(str2) : 0;
543 if (l1 <= MAX_LENGTH_NAME)
544 strcpy(hdr->Patient.Name, str1); // Flawfinder: ignore
545 if (l1+l2+2 <= MAX_LENGTH_NAME) {
546 strcpy(hdr->Patient.Name, str1); // Flawfinder: ignore
547 hdr->Patient.Name[l1] = 0x1f;
548 strcpy(hdr->Patient.Name+l1+1, str2); // Flawfinder: ignore
549 }
550 }
551 }
552 TiXmlHandle Gender = patientPatient.FirstChild("administrativeGenderCode").Element();
553 TiXmlHandle Birth = patientPatient.FirstChild("birthTime").Element();
554
555 if (Gender.Element()) {
556 const char *gender = Gender.Element()->Attribute("code");
557 hdr->Patient.Sex = (tolower(gender[0])=='m') + (tolower(gender[0])=='f');
558 }
559 if (Birth.Element()) {
560 hdr->Patient.Birthday = str_time2gdf_time(Birth.Element()->Attribute("value"));
561 }
562 }
563 }
564
565 if (VERBOSE_LEVEL>8)
566 fprintf(stdout,"IHE: [415] \n");
567
568 }
569 else if(aECG.Element()){
570
571 if (VERBOSE_LEVEL>8)
572 fprintf(stdout,"hl7r: [412]\n");
573
574 size_t len = strlen(aECG.FirstChild("id").Element()->Attribute("root"));
575
576 if (VERBOSE_LEVEL>8)
577 fprintf(stdout,"hl7r: [413]\n");
578
579 strncpy(hdr->ID.Recording,aECG.FirstChild("id").Element()->Attribute("root"),MAX_LENGTH_RID);
580 if (len>MAX_LENGTH_RID)
581 fprintf(stdout,"Warning HL7aECG(read): length of Recording ID exceeds maximum length %i>%i\n",(int)len,MAX_LENGTH_PID);
582
583 if (VERBOSE_LEVEL>8)
584 fprintf(stdout,"hl7r: [414]\n");
585
586
587 TiXmlHandle effectiveTime = aECG.FirstChild("effectiveTime");
588
589 char *T0 = NULL;
590 if(effectiveTime.FirstChild("low").Element())
591 T0 = (char *)effectiveTime.FirstChild("low").Element()->Attribute("value");
592 else if(effectiveTime.FirstChild("center").Element())
593 T0 = (char *)effectiveTime.FirstChild("center").Element()->Attribute("value");
594
595 if (VERBOSE_LEVEL>8)
596 fprintf(stdout,"hl7r: [413 2] <%s>\n", T0);
597
598 if (T0 != NULL)
599 hdr->T0 = str_time2gdf_time(T0);
600
601 if (VERBOSE_LEVEL>8)
602 fprintf(stdout,"hl7r: [413 4]\n");
603
604 TiXmlHandle demographic = aECG.FirstChild("componentOf").FirstChild("timepointEvent").FirstChild("componentOf").FirstChild("subjectAssignment").FirstChild("subject").FirstChild("trialSubject");
605
606 TiXmlElement *id = demographic.FirstChild("id").Element();
607 if(id) {
608 const char* tmpstr = id->Attribute("extension");
609 size_t len = strlen(tmpstr);
610 if (len>MAX_LENGTH_PID)
611 fprintf(stdout,"Warning HL7aECG(read): length of Patient Id exceeds maximum length %i>%i\n",(int)len,MAX_LENGTH_PID);
612 strncpy(hdr->Patient.Id,tmpstr,MAX_LENGTH_PID);
613 }
614
615 if (VERBOSE_LEVEL>7)
616 fprintf(stdout,"hl7r: [413]\n");
617
618 if (!hdr->FLAG.ANONYMOUS)
619 {
620 demographic = demographic.FirstChild("subjectDemographicPerson");
621 TiXmlElement *Name1 = demographic.FirstChild("name").Element();
622
623 if (Name1 != NULL) {
624 const char *name = Name1->GetText();
625 if (name != NULL) {
626 size_t len = strlen(name);
627
628 if (len>MAX_LENGTH_NAME)
629 fprintf(stdout,"Warning HL7aECG(read): length of Patient Name exceeds maximum length %i>%i\n",(int)len,MAX_LENGTH_PID);
630 strncpy(hdr->Patient.Name, name, MAX_LENGTH_NAME);
631 }
632 else {
633 fprintf(stderr,"Warning: composite subject name is not supported.\n");
634 if (VERBOSE_LEVEL>7) {
635 fprintf(stdout,"hl7r: [413++]<%s>\n",name);
636 for (int k=1;k<40;k++)
637 fprintf(stderr,"%c.",((char*)Name1)[k]);
638 }
639 //hdr->Patient.Name[0] = 0;
640 /*
641 ### FIXME: support of composite patient name.
642
643 const char *Name11 = Name1->Attribute("family");
644 fprintf(stdout,"Patient Family Name %p\n", Name11);
645 char *Name2 = Name.FirstChild("given")->GetText();
646
647 if ((Name1!=NULL) || (Name2!=NULL)) {
648 strncpy(hdr->Patient.Name, Name1, MAX_LENGTH_NAME);
649 }
650 */
651 }
652 }
653 else {
654 hdr->Patient.Name[0] = 0;
655 fprintf(stderr,"Warning: Patient Name not available could not be read.\n");
656 }
657 }
658
659 if (VERBOSE_LEVEL>7)
660 fprintf(stdout,"hl7r: [414]\n");
661
662 /* non-standard fields height and weight */
663 TiXmlElement *weight = demographic.FirstChild("weight").Element();
664 if (weight) {
665 uint16_t code = PhysDimCode(weight->Attribute("unit"));
666 if ((code & 0xFFE0) != 1728)
667 fprintf(stderr,"Warning: incorrect weight unit (%s)\n",weight->Attribute("unit"));
668 else // convert to kilogram
669 hdr->Patient.Weight = (uint8_t)(atof(weight->Attribute("value"))*PhysDimScale(code)*1e-3);
670 }
671 TiXmlElement *height = demographic.FirstChild("height").Element();
672
673 if (VERBOSE_LEVEL>7)
674 fprintf(stdout,"hl7r: [415]\n");
675
676 if (height) {
677 uint16_t code = PhysDimCode(height->Attribute("unit"));
678 if ((code & 0xFFE0) != 1280)
679 fprintf(stderr,"Warning: incorrect height unit (%s) %i \n",height->Attribute("unit"),code);
680 else // convert to centimeter
681 hdr->Patient.Height = (uint8_t)(atof(height->Attribute("value"))*PhysDimScale(code)*1e+2);
682 }
683
684 if (VERBOSE_LEVEL>7)
685 fprintf(stdout,"hl7r: [416]\n");
686
687 TiXmlElement *birthday = demographic.FirstChild("birthTime").Element();
688 if(birthday){
689 T0 = (char *)birthday->Attribute("value");
690 if (T0==NULL) T0=(char *)birthday->GetText(); // workaround for reading two different formats
691 hdr->Patient.Birthday = str_time2gdf_time(T0);
692 }
693
694 if (VERBOSE_LEVEL>8)
695 fprintf(stdout,"hl7r: [417]\n");
696
697 if (VERBOSE_LEVEL>8)
698 fprintf(stdout,"hl7r: [418]\n");
699
700 TiXmlElement *sex = demographic.FirstChild("administrativeGenderCode").Element();
701 if(sex){
702
703 if (sex->Attribute("code")==NULL)
704 hdr->Patient.Sex = 0;
705 else if(!strcmp(sex->Attribute("code"),"F"))
706 hdr->Patient.Sex = 2;
707 else if(!strcmp(sex->Attribute("code"),"M"))
708 hdr->Patient.Sex = 1;
709 else
710 hdr->Patient.Sex = 0;
711 }
712 else {
713 hdr->Patient.Sex = 0;
714 }
715
716 if (VERBOSE_LEVEL>8)
717 fprintf(stdout,"hl7r: [419]\n");
718
719 int LowPass=0, HighPass=0, Notch=0;
720 TiXmlHandle channels = aECG.FirstChild("component").FirstChild("series").FirstChild("component").FirstChild("sequenceSet");
721 TiXmlHandle variables = aECG.FirstChild("component").FirstChild("series");
722
723 for(TiXmlElement *tmpvar = variables.FirstChild("controlVariable").Element(); tmpvar; tmpvar = tmpvar->NextSiblingElement("controlVariable")){
724 if(!strcmp(tmpvar->FirstChildElement("controlVariable")->FirstChildElement("code")->Attribute("code"), "MDC_ATTR_FILTER_NOTCH"))
725 Notch = atoi(tmpvar->FirstChildElement("controlVariable")->FirstChildElement("component")->FirstChildElement("controlVariable")->FirstChildElement("value")->Attribute("value"));
726 else if(!strcmp(tmpvar->FirstChildElement("controlVariable")->FirstChildElement("code")->Attribute("code"), "MDC_ATTR_FILTER_LOW_PASS"))
727 LowPass = atoi(tmpvar->FirstChildElement("controlVariable")->FirstChildElement("component")->FirstChildElement("controlVariable")->FirstChildElement("value")->Attribute("value"));
728 else if(!strcmp(tmpvar->FirstChildElement("controlVariable")->FirstChildElement("code")->Attribute("code"), "MDC_ATTR_FILTER_HIGH_PASS"))
729 HighPass = atoi(tmpvar->FirstChildElement("controlVariable")->FirstChildElement("component")->FirstChildElement("controlVariable")->FirstChildElement("value")->Attribute("value"));
730 }
731
732 if (VERBOSE_LEVEL>8)
733 fprintf(stdout,"hl7r: [421]\n");
734
735 hdr->NRec = 1;
736 // hdr->SPR = 1;
737 // hdr->AS.rawdata = (uint8_t *)malloc(hdr->SPR);
738 // int32_t *data;
739
740 hdr->SampleRate = 1.0/atof(channels.FirstChild("component").FirstChild("sequence").FirstChild("value").FirstChild("increment").Element()->Attribute("value"));
741
742 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [517] %f\n",hdr->SampleRate);
743
744 /*************** Annotations **********************/
745 TiXmlHandle AnnotationSet = aECG.FirstChild("component").FirstChild("series").FirstChild("subjectOf").FirstChild("annotationSet");
746 TiXmlHandle Annotation = AnnotationSet.Child("component", 1).FirstChild("annotation").FirstChild("component").FirstChild("annotation");
747 size_t N_Event = 0, N=0;
748 for(int i = 1; AnnotationSet.Child("component", i).FirstChild("annotation").Element(); ++i) {
749 for(int j = 0; j<3; ++j) {
750
751 Annotation = AnnotationSet.Child("component", i).FirstChild("annotation").Child("component",j).FirstChild("annotation");
752
753 if (Annotation.FirstChild("value").Element() == NULL) break;
754 const char *code = Annotation.FirstChild("value").Element()->Attribute("code");
755 if (code==NULL) break;
756
757 uint16_t EventTyp1 = 0, EventTyp2 = 0;
758
759 if (!strcmp(code,"MDC_ECG_WAVC_PWAVE")) {
760 EventTyp1 = 0x0502; // start P-Wave
761 EventTyp2 = 0x8502; // end P-Wave
762 }
763 else if (!strcmp(code,"MDC_ECG_WAVC_QRSWAVE")) {
764 EventTyp1 = 0x0503; // start QRS
765 EventTyp2 = 0x8505; // end QRS
766 }
767 else if (!strcmp(code,"MDC_ECG_WAVC_TWAVE")) {
768 EventTyp1 = 0x0506; // start T-Wave
769 EventTyp2 = 0x8506; // end T-Wave
770 }
771
772 if ((N+3) > N_Event) {
773 N_Event = max(16,2*(N+2));
774 hdr->EVENT.TYP = (typeof(hdr->EVENT.TYP)) realloc(hdr->EVENT.TYP,N_Event*sizeof(*hdr->EVENT.TYP));
775 hdr->EVENT.POS = (typeof(hdr->EVENT.POS)) realloc(hdr->EVENT.POS,N_Event*sizeof(*hdr->EVENT.POS));
776 }
777
778 TiXmlHandle Boundary = Annotation.FirstChild("support").FirstChild("supportingROI").FirstChild("component").FirstChild("boundary").FirstChild("value");
779
780 int64_t pos1=0, pos2=0;
781 if (Boundary.FirstChild("low").Element()) {
782 const char *tmpstr = (Boundary.FirstChild("low").Element()->Attribute("value"));
783 pos1 = (ldexp((str_time2gdf_time(tmpstr)-hdr->T0)*86400*hdr->SampleRate,-32));
784 hdr->EVENT.TYP[N] = EventTyp1;
785 hdr->EVENT.POS[N] = pos1;
786 N++;
787 }
788
789 if (Boundary.FirstChild("high").Element()) {
790 const char *tmpstr = (Boundary.FirstChild("high").Element()->Attribute("value"));
791 pos2 = (ldexp((str_time2gdf_time(tmpstr)-hdr->T0)*86400*hdr->SampleRate,-32));
792 hdr->EVENT.TYP[N] = EventTyp2;
793 hdr->EVENT.POS[N] = pos2;
794 N++;
795 }
796 }
797 }
798 hdr->EVENT.N = N;
799
800 TiXmlHandle channel = channels.Child("component", 1).FirstChild("sequence");
801 for (hdr->NS = 0; channel.Element(); ++(hdr->NS), channel = channels.Child("component", hdr->NS+1).FirstChild("sequence")) {};
802 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
803
804 channel = channels.Child("component", 1).FirstChild("sequence");
805 hdr->AS.bpb = 0;
806
807 size_t szRawData = 0;
808 size_t SPR = 0;
809 hdr->SPR = 1;
810
811 for(int i = 0; channel.Element(); ++i, channel = channels.Child("component", i+1).FirstChild("sequence")){
812
813 const char *code = channel.FirstChild("code").Element()->Attribute("code");
814
815 CHANNEL_TYPE *hc = hdr->CHANNEL+i;
816 hc->LeadIdCode = 0;
817
818 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] %i\n",i);
819
820 strncpy(hc->Label, code, min(40, MAX_LENGTH_LABEL));
821 hc->Label[MAX_LENGTH_LABEL] = '\0';
822 hc->Transducer[0] = '\0';
823 hc->GDFTYP = 16; // float32
824 hc->DigMax = -1.0/0.0;
825 hc->DigMin = 1.0/0.0;
826
827 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] %i\n",i);
828
829 char *s = (char*) channel.FirstChild("value").FirstChild("digits").Element()->GetText();
830 size_t spr = 0;
831 //char *ps = s ? s+strlen(s) : NULL; //end of s
832
833 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] %i %p <%s>\n",i,s,s);
834
835 while (s && *s) {
836 if (SPR+spr+1 >= szRawData) {
837 szRawData = max(5000, 2*szRawData);
838 hdr->AS.rawdata = (uint8_t*) realloc(hdr->AS.rawdata, szRawData * sizeof(float));
839 }
840
841 double d = strtod(s,&s);
842 ((float*)(hdr->AS.rawdata))[SPR + spr++] = d;
843 /* get Min/Max */
844 if(d > hc->DigMax) hc->DigMax = d;
845 if(d < hc->DigMin) hc->DigMin = d;
846 }
847 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] %i %i\n",i,(int)spr);
848
849 hc->bi = hdr->AS.bpb;
850 SPR += spr;
851 hc->SPR = spr;
852 if (spr>0) hdr->SPR = lcm(hdr->SPR,spr);
853
854 hc->OnOff = 1;
855 hc->LeadIdCode = 0;
856 hdr->AS.bpb += hc->SPR * sizeof(float);
857
858 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420+] %i\n",i);
859
860 /* scaling factors */
861 const char *tmpchar;
862 tmpchar = channel.FirstChild("value").FirstChild("scale").Element()->Attribute("value");
863 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] <%s>\n",tmpchar);
864 hc->Cal = atof(tmpchar);
865 tmpchar = channel.FirstChild("value").FirstChild("origin").Element()->Attribute("value");
866 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] <%s>\n",tmpchar);
867 hc->Off = atof(tmpchar);
868 hc->DigMax += 1;
869 hc->DigMin -= 1;
870 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] Cal: %f Off: %f\n",hc->Cal,hc->Off);
871 hc->PhysMax = hc->DigMax*hc->Cal + hc->Off;
872 hc->PhysMin = hc->DigMin*hc->Cal + hc->Off;
873
874 /* Physical units */
875 strncpy(tmp, channel.FirstChild("value").FirstChild("origin").Element()->Attribute("unit"),20);
876 hc->PhysDimCode = PhysDimCode(tmp);
877
878 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7r: [420] %i\n",i);
879
880 hc->LowPass = LowPass;
881 hc->HighPass = HighPass;
882 hc->Notch = Notch;
883 // hc->XYZ[0] = l_endian_f32( *(float*) (Header2+ 4*k + 224*hdr->NS) );
884 // hc->XYZ[1] = l_endian_f32( *(float*) (Header2+ 4*k + 228*hdr->NS) );
885 // hc->XYZ[2] = l_endian_f32( *(float*) (Header2+ 4*k + 232*hdr->NS) );
886 // //memcpy(&hdr->CHANNEL[k].XYZ,Header2 + 4*k + 224*hdr->NS,12);
887 // hc->Impedance= ldexp(1.0, (uint8_t)Header2[k + 236*hdr->NS]/8);
888
889 // hc->Impedance = INF;
890 // for(int k1=0; k1<3; hdr->CHANNEL[index].XYZ[k1++] = 0.0);
891
892
893 }
894 hdr->FLAG.OVERFLOWDETECTION = 0;
895
896 if (VERBOSE_LEVEL>7) {
897 fprintf(stdout,"hl7r: [430] %i\n",hdr->AS.B4C_ERRNUM);
898 hdr2ascii(hdr,stdout,3);
899 fprintf(stdout,"hl7r: [431] %i\n",hdr->AS.B4C_ERRNUM);
900 }
901
902 } else {
903 fprintf(stderr, "%s : failed to parse (2)\n", hdr->FileName);
904 }
905 }
906 else
907 fprintf(stderr, "%s : failed to parse (1)\n", hdr->FileName);
908
909 return(0);
910
911 #else
912 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "HL7aECG not supported because of missing libtinyxml - recompile with libtinyxml");
913 return -1;
914 #endif
915
916
917 };
918
919 EXTERN_C void sopen_HL7aECG_write(HDRTYPE* hdr) {
920
921 if (VERBOSE_LEVEL > 7) fprintf(stdout,"hl7w: [610] <%s>\n",hdr->FileName);
922
923 size_t k;
924 for (k=0; k<hdr->NS; k++) {
925 hdr->CHANNEL[k].GDFTYP = 16; //float32
926 hdr->CHANNEL[k].SPR *= hdr->NRec;
927 }
928 hdr->SPR *= hdr->NRec;
929 hdr->NRec = 1;
930 hdr->FILE.OPEN=2;
931
932 return;
933 };
934
935 EXTERN_C int sclose_HL7aECG_write(HDRTYPE* hdr){
936 /*
937 this function is a stub or placeholder and need to be defined in order to be useful.
938 It will be called by the function SOPEN in "biosig.c"
939
940 Input: HDR structure
941
942 Output:
943 char* HDR.AS.Header // contains the content which will be written to the file in SOPEN
944 */
945
946 if (VERBOSE_LEVEL > 7) fprintf(stdout,"hl7c: [910] <%s>\n",hdr->FileName);
947
948 struct tm *t0;
949 char tmp[80];
950
951 #if defined(TINYXML_INCLUDED)
952 TiXmlDocument doc;
953
954 TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", "");
955 doc.LinkEndChild(decl);
956
957 if (VERBOSE_LEVEL>7) fprintf(stdout,"910 %i\n",1);
958
959 TiXmlElement *root = new TiXmlElement("AnnotatedECG");
960 root->SetAttribute("xmlns", "urn:hl7-org:v3");
961 root->SetAttribute("xmlns:voc", "urn:hl7-org:v3/voc");
962 root->SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
963 root->SetAttribute("xsi:schemaLocation", "urn:hl7-org:v3/HL7/aECG/2003-12/schema/PORT_MT020001.xsd");
964 root->SetAttribute("classCode", "OBS");
965 root->SetAttribute("moodCode", "EVN");
966 root->SetAttribute("type", "Observation");
967 doc.LinkEndChild(root);
968
969 TiXmlElement *rootid = new TiXmlElement("id");
970 rootid->SetAttribute("root", hdr->ID.Recording);
971 root->LinkEndChild(rootid);
972
973 TiXmlElement *rootCode = new TiXmlElement("code");
974 rootCode->SetAttribute("code", "93000");
975 rootCode->SetAttribute("codeSystem", "2.16.840.1.113883.6.12");
976 rootCode->SetAttribute("codeSystemName", "CPT-4");
977 root->LinkEndChild(rootCode);
978
979 if (VERBOSE_LEVEL>7) fprintf(stdout,"910 %i\n",2);
980
981 char timelow[24], timehigh[24];
982 gdf_time t1,t2;
983 t1 = hdr->T0;
984 if (VERBOSE_LEVEL>7) fprintf(stdout,"gdftime: hdr->T0: %ld\n", t1);
985
986 t0 = gdf_time2tm_time(t1);
987 sprintf(timelow, "%4d%2d%2d%2d%2d%2d", t0->tm_year+1900, t0->tm_mon+1, t0->tm_mday, t0->tm_hour, t0->tm_min, t0->tm_sec);
988
989 t1 = hdr->T0 + ldexp((hdr->SPR/hdr->SampleRate)/(3600.0*24),32);
990 t0 = gdf_time2tm_time(t1);
991 sprintf(timehigh, "%4d%2d%2d%2d%2d%2d", t0->tm_year+1900, t0->tm_mon+1, t0->tm_mday, t0->tm_hour, t0->tm_min, t0->tm_sec);
992
993 for(int i=0; i<18; ++i) {
994 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 920 %i\n",i);
995 if(timelow[i] == ' ')
996 timelow[i] = '0';
997 if(timehigh[i] == ' ')
998 timehigh[i] = '0';
999 }
1000
1001 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 930\n");
1002
1003 TiXmlElement *effectiveTime = new TiXmlElement("effectiveTime");
1004 TiXmlElement *effectiveTimeLow = new TiXmlElement("low");
1005 effectiveTimeLow->SetAttribute("value", timelow);
1006 effectiveTime->LinkEndChild(effectiveTimeLow);
1007 TiXmlElement *effectiveTimeHigh = new TiXmlElement("high");
1008 effectiveTimeHigh->SetAttribute("value", timehigh);
1009 effectiveTime->LinkEndChild(effectiveTimeHigh);
1010 root->LinkEndChild(effectiveTime);
1011
1012 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 931\n");
1013
1014 TiXmlElement *rootComponentOf = new TiXmlElement("componentOf");
1015 rootComponentOf->SetAttribute("typeCode", "COMP");
1016 rootComponentOf->SetAttribute("contextConductionInd", "true");
1017 root->LinkEndChild(rootComponentOf);
1018
1019 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 932\n");
1020
1021 TiXmlElement *timePointEvent = new TiXmlElement("timepointEvent");
1022 timePointEvent->SetAttribute("classCode", "CTTEVENT");
1023 timePointEvent->SetAttribute("moodCode", "EVN");
1024 rootComponentOf->LinkEndChild(timePointEvent);
1025
1026 TiXmlElement *timePointComponentOf = new TiXmlElement("componentOf");
1027 timePointComponentOf->SetAttribute("typeCode", "COMP");
1028 timePointComponentOf->SetAttribute("contextConductionInd", "true");
1029 timePointEvent->LinkEndChild(timePointComponentOf);
1030
1031 TiXmlElement *subjectAssignment = new TiXmlElement("subjectAssignment");
1032 subjectAssignment->SetAttribute("classCode", "CLNTRL");
1033 subjectAssignment->SetAttribute("moodCode", "EVN");
1034 timePointComponentOf->LinkEndChild(subjectAssignment);
1035
1036 TiXmlElement *subject = new TiXmlElement("subject");
1037 subject->SetAttribute("typeCode", "SBJ");
1038 subject->SetAttribute("contextControlCode", "OP");
1039 subjectAssignment->LinkEndChild(subject);
1040
1041 TiXmlElement *trialSubject = new TiXmlElement("trialSubject");
1042 trialSubject->SetAttribute("classCode", "RESBJ");
1043 subject->LinkEndChild(trialSubject);
1044
1045 if (strlen(hdr->Patient.Id)>0) {
1046 TiXmlElement *trialSubjectId = new TiXmlElement("id");
1047 trialSubjectId->SetAttribute("extension", hdr->Patient.Id);
1048 trialSubject->LinkEndChild(trialSubjectId);
1049 }
1050
1051 TiXmlElement *trialSubjectDemographicPerson = new TiXmlElement("subjectDemographicPerson");
1052 trialSubjectDemographicPerson->SetAttribute("classCode", "PSN");
1053 trialSubjectDemographicPerson->SetAttribute("determinerCode", "INSTANCE");
1054 trialSubject->LinkEndChild(trialSubjectDemographicPerson);
1055
1056 if (VERBOSE_LEVEL>7) fprintf(stdout,"933\n");
1057
1058 if (strlen(hdr->Patient.Name)>0)
1059 if (!hdr->FLAG.ANONYMOUS)
1060 {
1061 TiXmlElement *subjectDemographicPersonName = new TiXmlElement("name");
1062 char tmpstr[MAX_LENGTH_NAME+1];
1063 strcpy(tmpstr, hdr->Patient.Name);
1064 char *LastName=strtok(tmpstr, "\x1f");
1065 char *FirstName=strtok(NULL, "\x1f");
1066 /* LastName */
1067 TiXmlElement *subjectDemographicPersonLastName = new TiXmlElement("family");
1068 TiXmlText *lastnameText = new TiXmlText(LastName);
1069 subjectDemographicPersonLastName->LinkEndChild(lastnameText);
1070 subjectDemographicPersonName->LinkEndChild(subjectDemographicPersonLastName);
1071 /* FirstName */
1072 TiXmlElement *subjectDemographicPersonFirstName = new TiXmlElement("given");
1073 TiXmlText *firstnameText = new TiXmlText(FirstName);
1074 subjectDemographicPersonFirstName->LinkEndChild(firstnameText);
1075 subjectDemographicPersonName->LinkEndChild(subjectDemographicPersonFirstName);
1076
1077 trialSubjectDemographicPerson->LinkEndChild(subjectDemographicPersonName);
1078 }
1079
1080 TiXmlElement *subjectDemographicPersonGender = new TiXmlElement("administrativeGenderCode");
1081 if(hdr->Patient.Sex == 1){
1082 subjectDemographicPersonGender->SetAttribute("code", "M");
1083 subjectDemographicPersonGender->SetAttribute("displayName", "Male");
1084 }
1085 else if(hdr->Patient.Sex == 2){
1086 subjectDemographicPersonGender->SetAttribute("code", "F");
1087 subjectDemographicPersonGender->SetAttribute("displayName", "Female");
1088 }
1089 else{
1090 subjectDemographicPersonGender->SetAttribute("code", "UN");
1091 subjectDemographicPersonGender->SetAttribute("displayName", "Undefined");
1092 }
1093 subjectDemographicPersonGender->SetAttribute("codeSystem", "2.16.840.1.113883.5.1");
1094 subjectDemographicPersonGender->SetAttribute("codeSystemName", "AdministrativeGender");
1095 trialSubjectDemographicPerson->LinkEndChild(subjectDemographicPersonGender);
1096
1097 if (hdr->Patient.Birthday>0) {
1098 t0 = gdf_time2tm_time(hdr->Patient.Birthday);
1099
1100 // TODO: fixme if "t0->tm_sec"
1101 sprintf(tmp, "%04d%02d%02d%02d%02d%02d", t0->tm_year+1900, t0->tm_mon+1, t0->tm_mday, t0->tm_hour, t0->tm_min, t0->tm_sec);
1102
1103 TiXmlElement *subjectDemographicPersonBirthtime = new TiXmlElement("birthTime");
1104 subjectDemographicPersonBirthtime->SetAttribute("value", tmp);
1105 trialSubjectDemographicPerson->LinkEndChild(subjectDemographicPersonBirthtime);
1106 }
1107
1108 /* write non-standard fields height and weight */
1109 if (hdr->Patient.Weight) {
1110 sprintf(tmp,"%i",hdr->Patient.Weight);
1111 TiXmlElement *subjectDemographicPersonWeight = new TiXmlElement("weight");
1112 subjectDemographicPersonWeight->SetAttribute("value", tmp);
1113 subjectDemographicPersonWeight->SetAttribute("unit", "kg");
1114 trialSubjectDemographicPerson->LinkEndChild(subjectDemographicPersonWeight);
1115 }
1116 if (hdr->Patient.Height) {
1117 sprintf(tmp,"%i",hdr->Patient.Height);
1118 TiXmlElement *subjectDemographicPersonHeight = new TiXmlElement("height");
1119 subjectDemographicPersonHeight->SetAttribute("value", tmp);
1120 subjectDemographicPersonHeight->SetAttribute("unit", "cm");
1121 trialSubjectDemographicPerson->LinkEndChild(subjectDemographicPersonHeight);
1122 }
1123
1124
1125 if (VERBOSE_LEVEL>7) fprintf(stdout,"937\n");
1126
1127 TiXmlElement *subjectAssignmentComponentOf = new TiXmlElement("componentOf");
1128 subjectAssignmentComponentOf->SetAttribute("typeCode", "COMP");
1129 subjectAssignmentComponentOf->SetAttribute("contextConductionInd", "true");
1130 subjectAssignment->LinkEndChild(subjectAssignmentComponentOf);
1131
1132 TiXmlElement *clinicalTrial = new TiXmlElement("clinicalTrial");
1133 clinicalTrial->SetAttribute("classCode", "CLNTRL");
1134 clinicalTrial->SetAttribute("moodCode", "EVN");
1135 subjectAssignmentComponentOf->LinkEndChild(clinicalTrial);
1136
1137 TiXmlElement *clinicalTrialId = new TiXmlElement("id");
1138 clinicalTrialId->SetAttribute("root", "GRATZ");
1139 clinicalTrialId->SetAttribute("extension", "CLINICAL_TRIAL");
1140 clinicalTrial->LinkEndChild(clinicalTrialId);
1141
1142 /* location/trialSite/location/name */
1143 /* hdr->ID.Hospital is malloc'ed only if exist */
1144 TiXmlText *t_location = new TiXmlText(hdr->ID.Hospital ? hdr->ID.Hospital : "");
1145 TiXmlElement *s_location_1 = new TiXmlElement("location");
1146 TiXmlElement *s_location_2 = new TiXmlElement("trialSite");
1147 TiXmlElement *s_location_3 = new TiXmlElement("location");
1148 TiXmlElement *s_location_name = new TiXmlElement("name");
1149 s_location_name->LinkEndChild(t_location);
1150 s_location_3->LinkEndChild(s_location_name);
1151 s_location_2->LinkEndChild(s_location_3);
1152 s_location_1->LinkEndChild(s_location_2);
1153 clinicalTrial->LinkEndChild(s_location_1);
1154
1155 TiXmlElement *rootComponent = new TiXmlElement("component");
1156 rootComponent->SetAttribute("typeCode", "COMP");
1157 rootComponent->SetAttribute("contextConductionInd", "true");
1158 root->LinkEndChild(rootComponent);
1159
1160 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 939\n");
1161
1162 TiXmlElement *series = new TiXmlElement("series");
1163 series->SetAttribute("classCode", "OBSSER");
1164 series->SetAttribute("moodCode", "EVN");
1165 rootComponent->LinkEndChild(series);
1166
1167 TiXmlElement *seriesCode = new TiXmlElement("code");
1168 seriesCode->SetAttribute("code", "RHYTHM");
1169 seriesCode->SetAttribute("seriesCode", "2.16.840.1.113883.5.4");
1170 series->LinkEndChild(seriesCode);
1171
1172 TiXmlElement *seriesEffectiveTime = new TiXmlElement("effectiveTime");
1173 TiXmlElement *seriesEffectiveTimeLow = new TiXmlElement("low");
1174 seriesEffectiveTimeLow->SetAttribute("value", timelow);
1175 seriesEffectiveTime->LinkEndChild(seriesEffectiveTimeLow);
1176 TiXmlElement *seriesEffectiveTimeHigh = new TiXmlElement("high");
1177 seriesEffectiveTimeHigh->SetAttribute("value", timehigh);
1178 seriesEffectiveTime->LinkEndChild(seriesEffectiveTimeHigh);
1179 series->LinkEndChild(seriesEffectiveTime);
1180
1181 /* Manufacturer device
1182 <author><seriesAuthor>
1183 <manufacturedSeriesDevice>
1184 <manufacturerModelName> modelname </manufacturerModelName>
1185 </manufacturedSeriesDevice>
1186 <manufacturerOrganization>
1187 <name> manu name </name>
1188 </manufacturerOrganization> */
1189 TiXmlText *t_modelname = new TiXmlText(hdr->ID.Manufacturer.Model ? hdr->ID.Manufacturer.Model : "");
1190 TiXmlText *t_manuname = new TiXmlText(hdr->ID.Manufacturer.Name ? hdr->ID.Manufacturer.Name : "");
1191 TiXmlElement *s_author = new TiXmlElement("author");
1192 TiXmlElement *s_seriesauthor = new TiXmlElement("seriesAuthor");
1193 TiXmlElement *s_manudev = new TiXmlElement("manufacturedSeriesDevice");
1194 TiXmlElement *s_manumodel = new TiXmlElement("manufacturerModelName");
1195 s_manumodel->LinkEndChild(t_modelname);
1196 s_manudev->LinkEndChild(s_manumodel);
1197 s_seriesauthor->LinkEndChild(s_manudev);
1198 TiXmlElement *s_manuorg = new TiXmlElement("manufacturerOrganization");
1199 TiXmlElement *s_manuorgname = new TiXmlElement("name");
1200 s_manuorgname->LinkEndChild(t_manuname);
1201 s_manuorg->LinkEndChild(s_manuorgname);
1202 s_seriesauthor->LinkEndChild(s_manuorg);
1203
1204 s_author->LinkEndChild(s_seriesauthor);
1205 series->LinkEndChild(s_author);
1206
1207 for(int i=3; i; --i){
1208
1209 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 950 %i\n",i);
1210
1211 TiXmlElement *seriesControlVariable = new TiXmlElement("controlVariable");
1212 seriesControlVariable->SetAttribute("typeCode", "CTRLV");
1213 series->LinkEndChild(seriesControlVariable);
1214
1215 TiXmlElement *CTRLControlVariable = new TiXmlElement("controlVariable");
1216 CTRLControlVariable->SetAttribute("classCode", "OBS");
1217 seriesControlVariable->LinkEndChild(CTRLControlVariable);
1218
1219 TiXmlElement *controlVariableCode = new TiXmlElement("code");
1220 CTRLControlVariable->LinkEndChild(controlVariableCode);
1221
1222 TiXmlElement *controlVariableComponent = new TiXmlElement("component");
1223 controlVariableComponent->SetAttribute("typeCode", "COMP");
1224 CTRLControlVariable->LinkEndChild(controlVariableComponent);
1225
1226 TiXmlElement *componentControlVariable = new TiXmlElement("controlVariable");
1227 componentControlVariable->SetAttribute("classCode", "OBS");
1228 controlVariableComponent->LinkEndChild(componentControlVariable);
1229
1230 TiXmlElement *componentControlVariableCode = new TiXmlElement("code");
1231 componentControlVariable->LinkEndChild(componentControlVariableCode);
1232
1233 TiXmlElement *componentControlVariableValue = new TiXmlElement("value");
1234 componentControlVariableValue->SetAttribute("xsi:type", "PQ");
1235 componentControlVariable->LinkEndChild(componentControlVariableValue);
1236
1237 switch(i){
1238 case 3:
1239 controlVariableCode->SetAttribute("code", "MDC_ATTR_FILTER_NOTCH");
1240 componentControlVariableCode->SetAttribute("code", "MDC_ATTR_NOTCH_FREQ");
1241 componentControlVariableValue->SetDoubleAttribute("value", hdr->CHANNEL[0].Notch);
1242 break;
1243 case 2:
1244 controlVariableCode->SetAttribute("code", "MDC_ATTR_FILTER_LOW_PASS");
1245 componentControlVariableCode->SetAttribute("code", "MDC_ATTR_FILTER_CUTOFF_FREQ");
1246 componentControlVariableValue->SetDoubleAttribute("value", hdr->CHANNEL[0].LowPass);
1247 break;
1248 case 1:
1249 controlVariableCode->SetAttribute("code", "MDC_ATTR_FILTER_HIGH_PASS");
1250 componentControlVariableCode->SetAttribute("code", "MDC_ATTR_FILTER_CUTOFF_FREQ");
1251 componentControlVariableValue->SetDoubleAttribute("value", hdr->CHANNEL[0].HighPass);
1252 break;
1253 }
1254
1255 controlVariableCode->SetAttribute("codeSystem", "2.16.840.1.113883.6.24");
1256 controlVariableCode->SetAttribute("codeSystemName", "MDC");
1257 componentControlVariableCode->SetAttribute("codeSystem", "2.16.840.1.113883.6.24");
1258 componentControlVariableCode->SetAttribute("codeSystemName", "MDC");
1259 componentControlVariableValue->SetAttribute("unit", "Hz");
1260
1261 switch(i){
1262 case 3:
1263 controlVariableCode->SetAttribute("displayName", "Notch Filter");
1264 componentControlVariableCode->SetAttribute("displayName", "Notch Frequency");
1265 break;
1266 case 2:
1267 controlVariableCode->SetAttribute("displayName", "Low Pass Filter");
1268 componentControlVariableCode->SetAttribute("displayName", "Cutoff Frequency");
1269 break;
1270 case 1:
1271 controlVariableCode->SetAttribute("displayName", "High Pass Filter");
1272 componentControlVariableCode->SetAttribute("displayName", "Cutoff Frequency");
1273 break;
1274 }
1275 }
1276
1277 TiXmlElement *seriesComponent = new TiXmlElement("component");
1278 seriesComponent->SetAttribute("typeCode", "COMP");
1279 seriesComponent->SetAttribute("contextConductionInd", "true");
1280 series->LinkEndChild(seriesComponent);
1281
1282 TiXmlElement *sequenceSet = new TiXmlElement("sequenceSet");
1283 sequenceSet->SetAttribute("classCode", "OBSCOR");
1284 sequenceSet->SetAttribute("moodCode", "EVN");
1285 seriesComponent->LinkEndChild(sequenceSet);
1286
1287 TiXmlElement *sequenceSetComponent = new TiXmlElement("component");
1288 sequenceSetComponent->SetAttribute("typeCode", "COMP");
1289 sequenceSetComponent->SetAttribute("contextConductionInd", "true");
1290 sequenceSet->LinkEndChild(sequenceSetComponent);
1291
1292 TiXmlElement *sequence = new TiXmlElement("sequence");
1293 sequence->SetAttribute("classCode", "OBS");
1294 sequence->SetAttribute("moodCode", "EVN");
1295 sequenceSetComponent->LinkEndChild(sequence);
1296
1297 TiXmlElement *sequenceCode = new TiXmlElement("code");
1298 sequenceCode->SetAttribute("code", "TIME_ABSOLUTE");
1299 sequenceCode->SetAttribute("codeSystem", "2.16.840.1.113883.6.24");
1300 sequence->LinkEndChild(sequenceCode);
1301
1302 TiXmlElement *sequenceValue = new TiXmlElement("value");
1303 sequenceValue->SetAttribute("xsi:type", "GLIST_TS");
1304 sequence->LinkEndChild(sequenceValue);
1305
1306 TiXmlElement *valueHead = new TiXmlElement("head");
1307 valueHead->SetAttribute("value", timelow);
1308 valueHead->SetAttribute("unit", "s"); // TODO: value is date/time of the day - unit=[s] does not make sense
1309 sequenceValue->LinkEndChild(valueHead);
1310
1311 TiXmlElement *valueIncrement = new TiXmlElement("increment");
1312 valueIncrement->SetDoubleAttribute("value", 1/hdr->SampleRate);
1313 valueIncrement->SetAttribute("unit", "s");
1314 sequenceValue->LinkEndChild(valueIncrement);
1315
1316 TiXmlText *digitsText;
1317 float*Dat;
1318 char *S;
1319 char *pS;
1320
1321 #ifdef NO_BI
1322 size_t bi = 0;
1323 #endif
1324 for(int i=0; i<hdr->NS; ++i)
1325 if (hdr->CHANNEL[i].OnOff)
1326 {
1327
1328 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 960 %i\n",i);
1329
1330
1331 sequenceSetComponent = new TiXmlElement("component");
1332 sequenceSetComponent->SetAttribute("typeCode", "COMP");
1333 sequenceSetComponent->SetAttribute("contextConductionInd", "true");
1334 sequenceSet->LinkEndChild(sequenceSetComponent);
1335
1336 sequence = new TiXmlElement("sequence");
1337 sequence->SetAttribute("classCode", "OBS");
1338 sequence->SetAttribute("moodCode", "EVN");
1339 sequenceSetComponent->LinkEndChild(sequence);
1340
1341 sequenceCode = new TiXmlElement("code");
1342
1343 if (hdr->CHANNEL[i].LeadIdCode) {
1344 strcpy(tmp,"MDC_ECG_LEAD_"); // Flawfinder: ignore
1345 strcat(tmp,LEAD_ID_TABLE[hdr->CHANNEL[i].LeadIdCode]); // Flawfinder: ignore
1346 }
1347 else
1348 strcpy(tmp,hdr->CHANNEL[i].Label); // Flawfinder: ignore
1349
1350 sequenceCode->SetAttribute("code", tmp);
1351
1352 sequenceCode->SetAttribute("codeSystem", "2.16.840.1.113883.6.24");
1353 sequenceCode->SetAttribute("codeSystemName", "MDC");
1354 sequence->LinkEndChild(sequenceCode);
1355
1356 sequenceValue = new TiXmlElement("value");
1357 sequenceValue->SetAttribute("xsi:type", "SLIST_PQ");
1358 sequence->LinkEndChild(sequenceValue);
1359
1360 valueHead = new TiXmlElement("origin");
1361 valueHead->SetDoubleAttribute("value", hdr->CHANNEL[i].Off);
1362 // valueHead->SetDoubleAttribute("value", 0);
1363 valueHead->SetAttribute("unit", PhysDim3(hdr->CHANNEL[i].PhysDimCode));
1364 sequenceValue->LinkEndChild(valueHead);
1365
1366 valueIncrement = new TiXmlElement("scale");
1367 valueIncrement->SetDoubleAttribute("value", hdr->CHANNEL[i].Cal);
1368 //valueIncrement->SetDoubleAttribute("value", 1);
1369 valueIncrement->SetAttribute("unit", PhysDim3(hdr->CHANNEL[i].PhysDimCode));
1370 sequenceValue->LinkEndChild(valueIncrement);
1371
1372 TiXmlElement *valueDigits = new TiXmlElement("digits");
1373 sequenceValue->LinkEndChild(valueDigits);
1374
1375 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c [967] %i %f\n",i,*(float*)(hdr->AS.rawdata + hdr->CHANNEL[i].bi));
1376
1377 #ifndef NO_BI
1378 Dat=(float*)(hdr->AS.rawdata + hdr->CHANNEL[i].bi);
1379 #else
1380 Dat=(float*)(hdr->AS.rawdata + bi);
1381 #endif
1382
1383 //size_t sz = GDFTYP_BITS[hdr->CHANNEL[i].GDFTYP]>>3;
1384
1385 S=new char[32*hdr->CHANNEL[i].SPR];
1386 S[0]=0;
1387 pS=S;
1388
1389 for (unsigned int j=0; j<hdr->CHANNEL[i].SPR; ++j) {
1390 if (VERBOSE_LEVEL>8) fprintf(stdout,"hl7c 969: %i %i %f \n",i, j, Dat [j]);
1391 pS+=sprintf(pS,"%g ",Dat[j]);
1392 }
1393 #ifdef NO_BI
1394 bi += hdr->CHANNEL[i].SPR*sz;
1395 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 970 %i %i \n%s \n",i, bi, S);
1396 #else
1397 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 970 %i %i \n%s \n",i, hdr->CHANNEL[i].bi, S);
1398 #endif
1399
1400 digitsText = new TiXmlText(S);
1401 valueDigits->LinkEndChild(digitsText);
1402 delete []S;
1403 }
1404
1405 if (hdr->FILE.COMPRESSION)
1406 fprintf(stderr,"WARNING: saving of compressed HL7aECG file not supported - compression flag is ignored\n");
1407 int status = doc.SaveFile(hdr->FileName);
1408
1409 if (VERBOSE_LEVEL>7) fprintf(stdout,"hl7c 989 (%i)\n",status);
1410
1411 #else
1412 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "HL7aECG not supported because of missing libtinyxml - recompile with libtinyxml");
1413 return -1;
1414 #endif
1415
1416
1417
1418 return(0);
1419 };