TPCCLIB
Loading...
Searching...
No Matches
tacift.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "tpcclibConfig.h"
7/*****************************************************************************/
8#include "tpcift.h"
9#include "tpcisotope.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <math.h>
14#include <time.h>
15#include <string.h>
16/*****************************************************************************/
17#include "tpctac.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
28 IFT *h,
31 char *s,
33 TPCSTATUS *status
34) {
35 int verbose=0; if(status!=NULL) verbose=status->verbose;
36 if(s!=NULL) *s='\0';
37 if(h==NULL) {
38 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
39 return TPCERROR_NO_DATA;
40 }
41 if(verbose>0) printf("%s()\n", __func__);
42
43 /* Find the item index */
44 int i;
45 i=iftFindKey(h, "studynr", 0);
46 if(i<0) i=iftFindKey(h, "study_number", 0);
47 if(i<0) i=iftFindKey(h, "study number", 0);
48 if(i<0) {
49 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_KEY);
50 return TPCERROR_NO_KEY;
51 }
52 /* Check that item has content */
53 size_t len;
54 if(h->item[i].value==NULL) len=0;
55 else len=strlen(h->item[i].value);
56 if(len<1) {
57 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_VALUE);
58 return TPCERROR_NO_VALUE;
59 }
60 if(len>MAX_STUDYNR_LEN) {
61 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_VALUE);
63 }
64 /* All fine, then copy studynr */
65 if(s!=NULL) strlcpy(s, h->item[i].value, MAX_STUDYNR_LEN);
66
67 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
68 return(TPCERROR_OK);
69}
70/*****************************************************************************/
71
72/*****************************************************************************/
81 IFT *h,
84 const char *s
85) {
86 if(h==NULL) return TPCERROR_FAIL;
87
88 /* Find and delete previous studynr(s) */
89 int i=0, start=0;
90 while(1) {
91 i=iftFindKey(h, "studynr", start);
92 if(i<0) i=iftFindKey(h, "study_number", start);
93 if(i<0) i=iftFindKey(h, "study number", start);
94 if(i<0) break;
95 iftDelete(h, i); if(i>start) start=i;
96 }
97
98 /* If new studynr is empty, then just return */
99 if(s==NULL || *s=='\0') return(TPCERROR_OK);
100
101 /* Otherwise create new key and value */
102 return (iftPut(h, "studynr", s, (char)1, NULL));
103}
104/*****************************************************************************/
105
106/*****************************************************************************/
114 IFT *h,
116 int u
117) {
118 if(h==NULL) return TPCERROR_FAIL;
119
120 /* Find and delete previous unit(s) */
121 int i=0, start=0;
122 while(1) {
123 i=iftFindKey(h, "unit", start);
124 if(i<0) i=iftFindKey(h, "calibration_unit", start);
125 if(i<0) i=iftFindKey(h, "activity_units", start);
126 if(i<0) i=iftFindKey(h, "activity units", start);
127 if(i<0) break;
128 iftDelete(h, i); if(i>start) start=i;
129 }
130
131 /* If new unit is unknown, then just return */
132 if(u<=UNIT_UNKNOWN || u>=UNIT_LAST) return(TPCERROR_OK);
133
134 /* Otherwise create new key and value */
135 return (iftPut(h, "unit", unitName(u), (char)1, NULL));
136}
137/*****************************************************************************/
138
139/*****************************************************************************/
147 TAC *tac,
149 TPCSTATUS *status
150) {
151 int verbose=0; if(status!=NULL) verbose=status->verbose;
152 if(tac==NULL) {
153 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
154 return TPCERROR_FAIL;
155 }
156 if(verbose>0) printf("%s()\n", __func__);
157 tac->cunit=UNIT_UNKNOWN;
158
159 /* Find the item index */
160 int i;
161 i=iftFindKey(&tac->h, "unit", 0);
162 if(i<0) i=iftFindKey(&tac->h, "calibration_unit", 0);
163 if(i<0) i=iftFindKey(&tac->h, "activity_units", 0);
164 if(i<0) i=iftFindKey(&tac->h, "activity units", 0);
165 if(i<0) {
166 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_KEY);
167 return TPCERROR_NO_KEY;
168 }
169 /* Check that item has content */
170 size_t len;
171 if(tac->h.item[i].value==NULL) len=0;
172 else len=strlen(tac->h.item[i].value);
173 if(len<1) {
174 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_VALUE);
175 return TPCERROR_NO_VALUE;
176 }
177
178 /* Identify unit string */
179 tac->cunit=unitIdentify(tac->h.item[i].value);
180
181 /* Delete unit strings from header */
183
184 /* If unit was unknown, then return error */
185 if(tac->cunit==UNIT_UNKNOWN) {
186 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_VALUE);
188 }
189 /* Otherwise ok */
190 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
191 return(TPCERROR_OK);
192}
193/*****************************************************************************/
194
195/*****************************************************************************/
203 IFT *h,
205 int u
206) {
207 if(h==NULL) return TPCERROR_FAIL;
208
209 /* Find and delete previous time unit(s) */
210 int i=0, start=0;
211 while(1) {
212 i=iftFindKey(h, "timeunit", start);
213 if(i<0) i=iftFindKey(h, "time_unit", start);
214 if(i<0) i=iftFindKey(h, "time unit", start);
215 if(i<0) i=iftFindKey(h, "time units", start);
216 if(i<0) break;
217 iftDelete(h, i); if(i>start) start=i;
218 }
219
220 /* If new unit is unknown, then just return */
221 if(u<=UNIT_UNKNOWN || u>=UNIT_LAST) return(TPCERROR_OK);
222
223 /* Otherwise create new key and value */
224 return (iftPut(h, "timeunit", unitName(u), (char)1, NULL));
225}
226/*****************************************************************************/
227
228/*****************************************************************************/
236 TAC *tac,
238 TPCSTATUS *status
239) {
240 int verbose=0; if(status!=NULL) verbose=status->verbose;
241 if(tac==NULL) {
242 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
243 return TPCERROR_FAIL;
244 }
245 if(verbose>0) printf("%s()\n", __func__);
246 tac->tunit=UNIT_UNKNOWN;
247
248 /* Find the item index */
249 int i;
250 i=iftFindKey(&tac->h, "timeunit", 0);
251 if(i<0) i=iftFindKey(&tac->h, "time_unit", 0);
252 if(i<0) i=iftFindKey(&tac->h, "time unit", 0);
253 if(i<0) i=iftFindKey(&tac->h, "time units", 0);
254 if(i<0) {
255 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_KEY);
256 return TPCERROR_NO_KEY;
257 }
258 /* Check that item has content */
259 size_t len;
260 if(tac->h.item[i].value==NULL) len=0;
261 else len=strlen(tac->h.item[i].value);
262 if(len<1) {
263 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_VALUE);
264 return TPCERROR_NO_VALUE;
265 }
266
267 /* Identify unit string */
268 tac->tunit=unitIdentify(tac->h.item[i].value);
269
270 /* Delete unit strings from header */
272
273 /* If unit was unknown, then return error */
274 if(tac->tunit==UNIT_UNKNOWN) {
275 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_VALUE);
277 }
278 /* Otherwise ok */
279 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
280 return(TPCERROR_OK);
281}
282/*****************************************************************************/
283
284/*****************************************************************************/
292 IFT *h,
295 char *s,
297 TPCSTATUS *status
298) {
299 int verbose=0; if(status!=NULL) verbose=status->verbose;
300 if(s!=NULL) *s='\0';
301 if(h==NULL) {
302 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
303 return TPCERROR_NO_DATA;
304 }
305 if(verbose>0) printf("%s()\n", __func__);
306
307 /* Find the item index */
308 int i;
309 i=iftFindKey(h, "isotope", 0);
310 if(i<0) {
311 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_KEY);
312 return TPCERROR_NO_KEY;
313 }
314 /* Check that item has content */
315 size_t len;
316 if(h->item[i].value==NULL) len=0;
317 else len=strlen(h->item[i].value);
318 if(len<1) {
319 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_VALUE);
320 return TPCERROR_NO_VALUE;
321 }
322 if(len>MAX_ISOTOPE_LEN) {
323 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_VALUE);
325 }
326 /* All fine, then copy isotope */
327 if(s!=NULL) strlcpy(s, h->item[i].value, MAX_ISOTOPE_LEN);
328
329 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
330 return(TPCERROR_OK);
331}
332/*****************************************************************************/
333
334/*****************************************************************************/
343 IFT *h,
346 const char *s
347) {
348 if(h==NULL) return TPCERROR_FAIL;
349
350 /* Find and delete previous isotope(s) */
351 int i=0, start=0;
352 while(1) {
353 i=iftFindKey(h, "isotope", start);
354 if(i<0) break;
355 iftDelete(h, i); if(i>start) start=i;
356 }
357
358 /* If new isotope is empty, then just return */
359 if(s==NULL || *s=='\0') return(TPCERROR_OK);
360
361 /* Otherwise create new key and value */
362 return (iftPut(h, "isotope", s, (char)1, NULL));
363}
364/*****************************************************************************/
365
366/*****************************************************************************/
374 IFT *h,
377 char *s,
379 TPCSTATUS *status
380) {
381 int verbose=0; if(status!=NULL) verbose=status->verbose;
382 if(s!=NULL) *s='\0';
383 if(h==NULL) {
384 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
385 return TPCERROR_NO_DATA;
386 }
387 if(verbose>0) printf("%s()\n", __func__);
388
389 /* Find the item index */
390 int i;
391 i=iftFindKey(h, "scan_start_time", 0);
392 if(i<0) i=iftFindKey(h, "scan start time", 0);
393 if(i<0) i=iftFindKey(h, "scan_start", 0);
394 if(i<0) i=iftFindKey(h, "scan start", 0);
395 if(i<0) i=iftFindKey(h, "scantime", 0);
396 if(i<0) {
397 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_KEY);
398 return TPCERROR_NO_KEY;
399 }
400 /* Check that item has content */
401 size_t len;
402 if(h->item[i].value==NULL) len=0;
403 else len=strlen(h->item[i].value);
404 if(len<1) {
405 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_VALUE);
406 return TPCERROR_NO_VALUE;
407 }
408 if(len>19) {
409 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_VALUE);
411 }
412 /* All fine, then copy it */
413 if(s!=NULL) strcpy(s, h->item[i].value);
414
415 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
416 return(TPCERROR_OK);
417}
418/*****************************************************************************/
419
420/*****************************************************************************/
429 IFT *h,
432 const char *s
433) {
434 if(h==NULL) return TPCERROR_FAIL;
435
436 /* Find and delete previous scan start time(s) */
437 int i=0, start=0;
438 while(1) {
439 i=iftFindKey(h, "scan_start_time", start);
440 if(i<0) i=iftFindKey(h, "scan start time", start);
441 if(i<0) i=iftFindKey(h, "scan_start", start);
442 if(i<0) i=iftFindKey(h, "scan start", start);
443 if(i<0) i=iftFindKey(h, "scantime", start);
444 if(i<0) break;
445 iftDelete(h, i); if(i>start) start=i;
446 }
447
448 /* If new scan start time is empty, then just return */
449 if(s==NULL || *s=='\0') return(TPCERROR_OK);
450
451 /* Otherwise create new key and value */
452 return (iftPut(h, "scan_start_time", s, (char)1, NULL));
453}
454/*****************************************************************************/
455
456/*****************************************************************************/
464 IFT *h,
467 char *s,
469 TPCSTATUS *status
470) {
471 int verbose=0; if(status!=NULL) verbose=status->verbose;
472 if(s!=NULL) *s='\0';
473 if(h==NULL) {
474 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
475 return TPCERROR_NO_DATA;
476 }
477 if(verbose>0) printf("%s()\n", __func__);
478
479 /* Find the item index */
480 int i;
481 i=iftFindKey(h, "injection_time", 0);
482 if(i<0) i=iftFindKey(h, "injection time", 0);
483 if(i<0) i=iftFindKey(h, "injectiontime", 0);
484 if(i<0) {
485 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_KEY);
486 return TPCERROR_NO_KEY;
487 }
488 /* Check that item has content */
489 size_t len;
490 if(h->item[i].value==NULL) len=0;
491 else len=strlen(h->item[i].value);
492 if(len<1) {
493 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_VALUE);
494 return TPCERROR_NO_VALUE;
495 }
496 if(len>19) {
497 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_VALUE);
499 }
500 /* All fine, then copy it */
501 if(s!=NULL) strcpy(s, h->item[i].value);
502
503 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
504 return(TPCERROR_OK);
505}
506/*****************************************************************************/
507
508/*****************************************************************************/
517 IFT *h,
520 const char *s
521) {
522 if(h==NULL) return TPCERROR_FAIL;
523
524 /* Find and delete previous scan start time(s) */
525 int i=0, start=0;
526 while(1) {
527 i=iftFindKey(h, "injection_time", start);
528 if(i<0) i=iftFindKey(h, "injection time", start);
529 if(i<0) i=iftFindKey(h, "injectiontime", start);
530 if(i<0) break;
531 iftDelete(h, i); if(i>start) start=i;
532 }
533
534 /* If new scan start time is empty, then just return */
535 if(s==NULL || *s=='\0') return(TPCERROR_OK);
536
537 /* Otherwise create new key and value */
538 return (iftPut(h, "injection_time", s, (char)1, NULL));
539}
540/*****************************************************************************/
541
542/*****************************************************************************/
550 IFT *h
551) {
552 if(h==NULL) return(DECAY_UNKNOWN);
553
554 /* Find the item index */
555 int i;
556 i=iftFindKey(h, "decay_correction", 0);
557 if(i<0) i=iftFindKey(h, "decay correction", 0);
558 if(i<0) return(DECAY_UNKNOWN);
559
560 /* Check the contents */
561 if(h->item[i].value==NULL || strlen(h->item[i].value)<1)
562 return(DECAY_UNKNOWN);
563 if(strncasecmp(h->item[i].value, "YES", 1)==0) return(DECAY_CORRECTED);
564 if(strncasecmp(h->item[i].value, "NO", 1)==0) return(DECAY_NOTCORRECTED);
565 if(strcasecmp(h->item[i].value, "ON")==0) return(DECAY_CORRECTED);
566 if(strcasecmp(h->item[i].value, "OFF")==0) return(DECAY_NOTCORRECTED);
567 return(DECAY_UNKNOWN);
568}
569/*****************************************************************************/
570
571/*****************************************************************************/
580 IFT *h,
583) {
584 if(h==NULL) return TPCERROR_FAIL;
585
586 /* Find and delete previous status */
587 int i=0, start=0;
588 while(1) {
589 i=iftFindKey(h, "decay_correction", start);
590 if(i<0) i=iftFindKey(h, "decay correction", start);
591 if(i<0) break;
592 iftDelete(h, i); if(i>start) start=i;
593 }
594
595 /* Create new key and value */
596 if(dc==DECAY_CORRECTED)
597 return (iftPut(h, "decay_correction", "yes", (char)1, NULL));
598 else if(dc==DECAY_NOTCORRECTED)
599 return (iftPut(h, "decay_correction", "no", (char)1, NULL));
600
601 /* If unknown, then just return */
602 return(TPCERROR_OK);
603}
604/*****************************************************************************/
605
606/*****************************************************************************/
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
int iftDelete(IFT *ift, int index)
Definition ift.c:206
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * value
Definition tpcift.h:37
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
Definition tpctac.h:87
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
int cunit
Definition tpctac.h:105
int tunit
Definition tpctac.h:109
int verbose
Verbose level, used by statusPrint() etc.
int tacGetHeaderUnit(TAC *tac, TPCSTATUS *status)
Definition tacift.c:145
decaycorrection tacGetHeaderDecayCorrection(IFT *h)
Definition tacift.c:548
int tacSetHeaderDecayCorrection(IFT *h, decaycorrection dc)
Definition tacift.c:578
int tacSetHeaderScanstarttime(IFT *h, const char *s)
Definition tacift.c:427
int tacGetHeaderTimeunit(TAC *tac, TPCSTATUS *status)
Definition tacift.c:234
int tacGetHeaderScanstarttime(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:372
int tacGetHeaderStudynr(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:26
int tacGetHeaderInjectiontime(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:462
int tacSetHeaderTimeunit(IFT *h, int u)
Definition tacift.c:201
int tacSetHeaderIsotope(IFT *h, const char *s)
Definition tacift.c:341
int tacGetHeaderIsotope(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:290
int tacSetHeaderStudynr(IFT *h, const char *s)
Definition tacift.c:79
int tacSetHeaderInjectiontime(IFT *h, const char *s)
Definition tacift.c:515
int tacSetHeaderUnit(IFT *h, int u)
Definition tacift.c:112
@ UNIT_UNKNOWN
Unknown unit.
@ UNIT_LAST
end of list
@ TPCERROR_INVALID_VALUE
Invalid value.
@ TPCERROR_NO_VALUE
Value not found.
@ TPCERROR_FAIL
General error.
@ TPCERROR_NO_KEY
Key not found.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
int unitIdentify(const char *s)
Definition units.c:162
char * unitName(int unit_code)
Definition units.c:143
#define MAX_STUDYNR_LEN
Define max study number length.
Header file for library libtpcift.
Header file for library libtpcisotope.
#define MAX_ISOTOPE_LEN
Max string length for PET isotope.
Definition tpcisotope.h:41
decaycorrection
Definition tpcisotope.h:78
@ DECAY_UNKNOWN
Not known; usually assumed that data is corrected.
Definition tpcisotope.h:79
@ DECAY_NOTCORRECTED
Data is not corrected for physical decay.
Definition tpcisotope.h:80
@ DECAY_CORRECTED
Data is corrected for physical decay.
Definition tpcisotope.h:81
Header file for library libtpctac.