TPCCLIB
Loading...
Searching...
No Matches
tsv.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpccurveio.h"
7/*****************************************************************************/
8
9/*****************************************************************************/
17 char *filename,
19 DFT *dft
20) {
21 int ii, si, ri, fi, n, ret;
22 char tmp[1024], tmp2[512], *cptr;
23 IFT ift;
24 float f[13];
25
26
27 /* Check the arguments */
28 if(filename==NULL || dft==NULL || strlen(filename)<1) {
29 strcpy(dfterrmsg, "program error"); return(1);
30 }
31
32 /* Read the file contents */
33 iftInit(&ift); ret=iftRead(&ift, filename, 0, 0);
34 if(ret) {strcpy(dfterrmsg, ift.status); iftEmpty(&ift); return(2);}
35
36 /* Check that this actually is a Amide TAC file */
37 strcpy(tmp, "amide"); ii=iftGet(&ift, tmp, 0);
38 if(ii<0) {strcpy(dfterrmsg, "wrong format"); iftEmpty(&ift); return(3);}
39 if(strncasecmp(ift.item[ii].value, "ROI Analysis File - ", 16)!=0) {
40 strcpy(dfterrmsg, "wrong format"); iftEmpty(&ift); return(3);}
41 sprintf(dft->comments, "# Amide %s\n# original_filename := %s\n",
42 ift.item[ii].value, filename);
43
44 /* Get the number of ROIs */
45 strcpy(tmp, "ROI");
46 ri=0; while((ii=iftGetNth(&ift, tmp, ri+1, 0))>=0) ri++;
47 //printf("ri=%d\n", ri);
48 if(ri<1) {strcpy(dfterrmsg, "wrong format"); iftEmpty(&ift); return(4);}
49
50 /* Get the max frame number */
51 for(ii=0, fi=0; ii<ift.keyNr; ii++) {
52 if(ift.item[ii].type=='#') continue;
53 n=sscanf(ift.item[ii].value, "%f %f %f %f %f %f %f %f %f %f %f %f %f",
54 f, f+1, f+2, f+3, f+4, f+5, f+6, f+7, f+8, f+9, f+10, f+11, f+12);
55 if(n<13) continue;
56 n=temp_roundf(f[0])+1; // Amide frames start from 0
57 if(n>fi) fi=n;
58 }
59 //printf("fi=%d\n", fi);
60 if(fi<1) {strcpy(dfterrmsg, "wrong format"); iftEmpty(&ift); return(5);}
61
62 /* Allocate memory for DFT data */
63 if(dftSetmem(dft, fi, ri)) {
64 strcpy(dfterrmsg, "out of memory"); iftEmpty(&ift); return(7);}
65 dft->frameNr=fi; dft->voiNr=ri;
66
67 /*
68 * Read one ROI at a time
69 */
70 strcpy(tmp, "ROI"); ri=0;
71 while((ii=iftGetNth(&ift, tmp, ri+1, 0))>=0) {
72 /* Get ROI name */
73 strcpy(tmp2, ift.item[ii].value); cptr=strtok(tmp2, " \t\n\r");
74 if(cptr!=NULL) {
75 strncpy(dft->voi[ri].voiname, cptr, MAX_REGIONSUBNAME_LEN);
76 dft->voi[ri].voiname[MAX_REGIONSUBNAME_LEN]='\0';
77 } else {
78 char buf[128]; snprintf(buf, 128, "%03d", ri+1);
79 char *p=buf+strlen(buf)-3;
80 snprintf(dft->voi[ri].voiname, MAX_REGIONSUBNAME_LEN, "VOI%s", p);
81 }
82 /* Get the next data set (output filename will be based on this) */
83 for(si=ii+1; si<ift.keyNr; si++) {
84 if(ift.item[si].type!='#') continue;
85 if(strcasecmp(ift.item[si].key, "Data Set")==0) break;
86 }
87 if(si<0) {strcpy(dfterrmsg, "wrong format"); iftEmpty(&ift); return(8);}
88 strcpy(tmp2, ift.item[si].value); cptr=strtok(tmp2, " \t\n\r");
89 if(cptr!=NULL) {
90 strncpy(dft->voi[ri].name, cptr, MAX_REGIONNAME_LEN);
91 dft->voi[ri].name[MAX_REGIONNAME_LEN]='\0';
92 }
93 /* Read the frame data until the next data set */
94 for(ii=si+1, fi=0; ii<ift.keyNr; ii++)
95 if(ift.item[ii].type!='#') break;
96 for(; ii<ift.keyNr; ii++) {
97 if(ift.item[ii].type=='#') break;
98 n=sscanf(ift.item[ii].value, "%f %f %f %f %f %f %f %f %f %f %f %f %f",
99 f, f+1, f+2, f+3, f+4, f+5, f+6, f+7, f+8, f+9, f+10, f+11, f+12);
100 if(n<13) continue;
101 /* Get mean activity concentration */
102 dft->voi[ri].y[fi]=f[5];
103 /* Get VOI size */
104 if(fi==0)
105 dft->voi[ri].size=f[10];
106 /* Get frame times, if this is the first ROI */
107 if(ri==0) {
108 dft->x[fi]=f[2];
109 dft->x1[fi]=f[2]-0.5*f[1];
110 dft->x2[fi]=f[2]+0.5*f[1];
111 }
112 fi++;
113 } // next frame
114 ri++;
115 }
116 iftEmpty(&ift);
117
118 /* Set the study number based on Data Set */
119 (void)studynr_from_fname(dft->voi[0].name, dft->studynr);
120
121 /* Set the rest of DFT "header" */
122 dft->_type=1;
123 dft->isweight=0;
124 dftTimeunitToDFT(dft, petTunit(TUNIT_SEC)); // time units are in sec
125 dftUnitToDFT(dft, CUNIT_UNKNOWN); // conc units are not known
126
127 return(0);
128}
129/*****************************************************************************/
130
131/*****************************************************************************/
char dfterrmsg[64]
Definition dft.c:6
int dftSetmem(DFT *data, int frameNr, int voiNr)
Definition dft.c:57
void dftUnitToDFT(DFT *dft, int dunit)
Definition dftunit.c:11
int dftTimeunitToDFT(DFT *dft, const char *timeunit)
Definition dftunit.c:103
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
Definition iftfile.c:24
int iftGet(IFT *ift, char *key, int verbose)
Definition iftsrch.c:15
int iftGetNth(IFT *ift, char *key, int n, int verbose)
Definition iftsrch.c:48
Header file for libtpccurveio.
#define MAX_REGIONNAME_LEN
Definition libtpcmisc.h:154
char * petTunit(int tunit)
Definition petunits.c:226
int studynr_from_fname(char *fname, char *studynr)
Definition studynr.c:119
int temp_roundf(float e)
Definition petc99.c:20
#define MAX_REGIONSUBNAME_LEN
Definition libtpcmisc.h:158
int _type
Voi * voi
char studynr[MAX_STUDYNR_LEN+1]
double * x1
char comments[_DFT_COMMENT_LEN+1]
int voiNr
double * x2
int frameNr
int isweight
double * x
int keyNr
Definition libtpcmisc.h:270
const char * status
Definition libtpcmisc.h:277
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279
double size
char voiname[MAX_REGIONSUBNAME_LEN+1]
double * y
char name[MAX_REGIONNAME_LEN+1]
int tsvRead(char *filename, DFT *dft)
Definition tsv.c:15