6#include "tpcclibConfig.h"
24static const char *tac_format[] = {
58static const char *tac_fn_ext[] = {
102 if(c<TAC_FORMAT_UNKNOWN || c>=TAC_FORMAT_LAST)
return NULL;
103 return (
char*)tac_format[c];
119 for(
int i=0; i<TAC_FORMAT_LAST; i++) {
120 if(strcasecmp(tac_format[i], s)==0)
return i;
148 if(c<TAC_FORMAT_UNKNOWN || c>=TAC_FORMAT_LAST)
return NULL;
149 return (
char*)tac_fn_ext[c];
167 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
169 if(fname==NULL || strlen(fname)<1) {
173 if(verbose>1) {printf(
"%s(%s)\n", __func__, fname); fflush(stdout);}
178 fp=fopen(fname,
"rb");
187 if(verbose>3) {printf(
" fsize := %zu\n", fsize); fflush(stdout);}
190 fclose(fp);
return format;
202 fclose(fp);
return format;
205 if(strncasecmp(data,
"#Subject ID,Subject Weight,Subject Sex,Unique Series ID,Series Date,Series Description", 20)==0 ||
206 strncasecmp(data,
"#Subject ID;Subject Weight;Subject Sex;Unique Series ID;Series Date;Series Description", 20)==0) {
212 char *cptr, *line=NULL;
214 while((line=
strTokenDup(cptr,
"\n\r", &j))!=NULL) {
216 free(line); cptr+=j; i++;
221 fclose(fp);
return format;
225 if(verbose>1) {printf(
" checking for magic number\n"); fflush(stdout);}
226 if(strncasecmp(line,
"DFT", 3)==0) {
228 }
else if(strncasecmp(line,
"cpt", 3)==0) {
230 }
else if(strcmp(line,
"directory,file name,num,slice,mean,sd,cov,max,min,pixel,total,group")==0) {
232 }
else if(strncmp(line,
"PatientName,", 12)==0) {
234 }
else if(strstr(line,
" - Time")!=NULL && strstr(line,
"(upper bound)")!=NULL &&
235 strstr(line,
"(lower bound)")!=NULL && strstr(line,
"(standard deviation)")!=NULL) {
240 if(verbose>0) {printf(
" identified as %s file\n",
tacFormattxt(format)); fflush(stdout);}
241 fclose(fp);
return format;
245 if(verbose>1) {printf(
" checking file name extension\n"); fflush(stdout);}
246 cptr=strrchr(fname,
'.');
if(cptr!=NULL) cptr++;
247 if(verbose>1 && cptr!=NULL) printf(
" extension := %s\n", cptr);
248 if(strcasecmp(cptr,
"IDWC")==0 || strcasecmp(cptr,
"IDW")==0) {
250 }
else if(strcasecmp(cptr,
"IF")==0) {
252 }
else if(strcasecmp(cptr,
"XML")==0) {
254 }
else if(strcasecmp(cptr,
"SIF")==0) {
256 }
else if(strcasecmp(cptr,
"LIS")==0) {
258 }
else if(strcasecmp(cptr,
"ALG")==0) {
262 if(verbose>0) {printf(
" identified as %s file\n",
tacFormattxt(format)); fflush(stdout);}
263 fclose(fp);
return format;
281 if(verbose>0) {printf(
" format not identified\n"); fflush(stdout);}
346 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
357 if(format<=TAC_FORMAT_UNKNOWN || format>=TAC_FORMAT_LAST)
359 if(format<=TAC_FORMAT_UNKNOWN || format>=TAC_FORMAT_LAST) {
364 printf(
"%s():\n", __func__);
365 printf(
"writing %d TACs and %d samples in %s format\n",
399 statusSet(status, __func__, __FILE__, __LINE__, ret);
421 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
422 if(fname==NULL || strlen(fname)<1 || d==NULL) {
426 if(verbose>1) printf(
"%s(%s)\n", __func__, fname);
434 fp=fopen(fname,
"r");
444 if(verbose>4) printf(
"file_size := %d\n", n);
445 if(n<2 || binpart!=0) {
447 printf(
"file_size := %d\n", n);
448 printf(
"binpart := %d\n", binpart);
465 printf(
"\n --- CSV contents ---\n");
471 if(verbose>1) printf(
"checking for magic number\n");
472 if(strncasecmp(csv.
c[0].
content,
"DFT", 3)==0) {
474 }
else if(strncasecmp(csv.
c[0].
content,
"cpt", 3)==0) {
476 }
else if(csv.
nr>8 && strcasecmp(csv.
c[0].
content,
"time")==0 &&
477 strcasecmp(csv.
c[1].
content,
"prompt")==0 &&
478 strcasecmp(csv.
c[2].
content,
"delayed")==0 &&
479 strcasecmp(csv.
c[3].
content,
"p_rate")==0) {
481 }
else if(strncasecmp(csv.
c[0].
content,
"Time", 4)==0) {
483 }
else if(strncasecmp(csv.
c[0].
content,
"Start", 5)==0) {
485 }
else if(strncasecmp(csv.
c[0].
content,
"<?xml version", 6)==0) {
487 }
else if(csv.
nr>12 && strcasecmp(csv.
c[0].
content,
"directory")==0 &&
488 strcasecmp(csv.
c[1].
content,
"file name")==0 &&
489 strcasecmp(csv.
c[2].
content,
"num")==0 &&
490 strcasecmp(csv.
c[3].
content,
"slice")==0 &&
491 strcasecmp(csv.
c[4].
content,
"mean")==0 &&
492 strcasecmp(csv.
c[5].
content,
"sd")==0 &&
493 strcasecmp(csv.
c[6].
content,
"cov")==0 &&
494 strcasecmp(csv.
c[7].
content,
"max")==0 &&
495 strcasecmp(csv.
c[8].
content,
"min")==0 &&
496 strcasecmp(csv.
c[9].
content,
"pixel")==0 &&
497 strcasecmp(csv.
c[10].
content,
"total")==0 &&
498 strcasecmp(csv.
c[11].
content,
"group")==0) {
500 }
else if(csv.
nr>10 && strcasecmp(csv.
c[0].
content,
"PatientName")==0 &&
501 strcasecmp(csv.
c[2].
content,
"Patient MRN")==0 ) {
503 }
else if(csv.
nr>5 && strstr(csv.
c[0].
content,
" - Time")!=NULL &&
504 strstr(csv.
c[2].
content,
"(upper bound)")!=NULL &&
505 strstr(csv.
c[3].
content,
"(lower bound)")!=NULL &&
506 strstr(csv.
c[4].
content,
"(standard deviation)")!=NULL) {
508 }
else if(strcasecmp(csv.
c[0].
content,
"TAC")==0) {
513 }
else if(csv.
nr>4 && strcasecmp(csv.
c[0].
content,
"Singles")==0 &&
514 strcasecmp(csv.
c[1].
content,
"Randoms")==0 &&
515 strcasecmp(csv.
c[2].
content,
"Prompts")==0 &&
516 strncasecmp(csv.
c[3].
content,
"Time(ms)", 4)==0) {
518 }
else if(
csvCell(&csv, 1, 0) && strcasecmp(
csvCell(&csv, 1, 0),
"Num Voxels")==0 )
526 if(verbose>1) printf(
"checking file name extension\n");
527 char *cptr, *extension;
529 if(cptr!=NULL && strlen(cptr)>1) extension=
strdup(cptr+1);
530 else extension=
strdup(
"");
531 if(verbose>1) printf(
"extension := '%s'\n", extension);
533 if(strcasecmp(cptr,
"IDWC")==0 || strcasecmp(cptr,
"IDW")==0) {
535 }
else if(strcasecmp(cptr,
"IF")==0) {
537 }
else if(strcasecmp(cptr,
"XML")==0) {
539 }
else if(strcasecmp(cptr,
"HTML")==0 || strcasecmp(cptr,
"HTM")==0) {
541 }
else if(strcasecmp(cptr,
"SIF")==0) {
543 }
else if(strcasecmp(cptr,
"HC")==0) {
545 }
else if(strcasecmp(cptr,
"R")==0) {
554 if(verbose>2) {printf(
"reading all data in IFT structure\n"); fflush(stdout);}
555 fp=fopen(fname,
"r");
556 if(fp!=NULL) {
iftRead(&hdr, fp, 0, 1, status); fclose(fp);}
571 }
else if(
iftFindKey(&hdr,
"Discriminators", 0)>=0) {
581 if(verbose>2) printf(
"\nRegular CSV, therefore trying Mat file format\n");
583 int ok=1;
char *c;
int len;
584 for(
int r=1; r<csv.
row_nr; r++) {
585 c=
csvCell(&csv, r, 0);
if(c==NULL) {ok=0;
break;}
586 len=strlen(c);
if(len<2) {ok=0;
break;}
587 if(c[0]!=
'\'' || c[len-1]!=
'\'') {ok=0;
break;}
595 printf(
"\n FORMAT NOT IDENTIFIED \n");
596 printf(
"\n --- CSV contents ---\n");
605 if(verbose>1) {printf(
" file format: %s\n",
tacFormattxt(format)); fflush(stdout);}
617 fp=fopen(fname,
"r");
618 if(fp!=NULL) {
iftRead(&hdr, fp, 0, 2, status); fclose(fp);}
623 fp=fopen(fname,
"r");
624 if(fp!=NULL) {
iftRead(&hdr, fp, 0, 2, status); fclose(fp);}
629 fp=fopen(fname,
"r");
630 if(fp!=NULL) {
iftRead(&hdr, fp, 0, 2, status); fclose(fp);}
640 ret=tacRead4DM(d, &csv, status);
647 fp=fopen(fname,
"r");
648 if(fp!=NULL) {
iftRead(&hdr, fp, 0, 2, status); fclose(fp);}
659 fp=fopen(fname,
"r");
660 if(fp!=NULL) {
iftRead(&hdr, fp, 0, 2, status); fclose(fp);}
686 if(verbose>0) printf(
"identified as %s file\n",
tacFormattxt(format));
688 statusSet(status, __func__, __FILE__, __LINE__, ret);
694 if(verbose>1) printf(
"checking studynr\n");
696 if(verbose>2) printf(
"studynr not found in %s\n", fname);
701 if(verbose>2) printf(
"studynr based on filename: %s\n", studynr);
704 fprintf(stderr,
"Error: cannot get valid studynr from filename.\n");
708 if(verbose>1) printf(
"checking concentration units\n");
712 printf(
"concentration units based on filename := %s\n",
unitName(d->
cunit)); fflush(stdout);
int csvIsRegular(CSV *csv)
char * csvCell(CSV *csv, int row, int col)
int csvRead(CSV *csv, FILE *fp, TPCSTATUS *status)
int csvWrite(CSV *csv, int regular, FILE *fp, TPCSTATUS *status)
int tacWriteDFT(TAC *tac, FILE *fp, int extra, TPCSTATUS *status)
int tacReadDFT(TAC *tac, CSV *csv, IFT *hdr, TPCSTATUS *status)
char * filenameGetExtension(const char *s)
Get the last extension of a file name.
int tacReadHRPLUSHC(TAC *tac, CSV *csv, TPCSTATUS *status)
int tacReadHRRTHC(TAC *tac, CSV *csv, TPCSTATUS *status)
int iftSearchValue(IFT *ift, const char *s, int start_index)
int iftFindPair(IFT *ift, const char *key, const char *value, int start_index)
int iftFindKey(IFT *ift, const char *key, int start_index)
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
int tacReadMat(TAC *tac, CSV *csv, TPCSTATUS *status)
int tacReadPMOD(TAC *tac, CSV *csv, IFT *hdr, TPCSTATUS *status)
int tacWritePMOD(TAC *tac, FILE *fp, int extra, TPCSTATUS *status)
int tacReadQView(TAC *tac, CSV *csv, const int grouponly, TPCSTATUS *status)
char * asciiFileRead(FILE *fp, char *data, size_t maxlen)
int asciiCommentLine(const char *line, int *cont)
size_t asciiFileSize(FILE *fp, int *nonprintable)
int tacWriteSIF(TAC *tac, FILE *fp, int extra, TPCSTATUS *status)
int tacReadSIF(TAC *tac, CSV *csv, IFT *hdr, TPCSTATUS *status)
int tacReadSimple(TAC *tac, CSV *csv, IFT *hdr, TPCSTATUS *status)
int tacWriteSimple(TAC *tac, FILE *fp, int extra, TPCSTATUS *status)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
char * strdup(const char *s)
char * strTokenDup(const char *s1, const char *s2, int *next)
IFT h
Optional (but often useful) header information.
int verbose
Verbose level, used by statusPrint() etc.
int studynrFromFilename(const char *fname, char *studynr, int force)
int tacReadScanditronics(TAC *tac, IFT *hdr, TPCSTATUS *status)
int tacReadGEMS(TAC *tac, IFT *hdr, TPCSTATUS *status)
int tacReadAllogg(TAC *tac, IFT *hdr, TPCSTATUS *status)
int tacReadOldAllogg(TAC *tac, IFT *hdr, TPCSTATUS *status)
int tacReadInveonCSV(TAC *tac, CSV *csv, TPCSTATUS *status)
int tacReadCSV(TAC *tac, CSV *csv, IFT *hdr, TPCSTATUS *status)
int tacWriteCSV(TAC *tac, FILE *fp, int extra, tacformat format, TPCSTATUS *status)
int tacReadCarimasTxt(TAC *tac, CSV *csv, TPCSTATUS *status)
int tacGetHeaderStudynr(IFT *h, char *s, TPCSTATUS *status)
int tacSetHeaderStudynr(IFT *h, const char *s)
int tacFormatDetermine(const char *fname, TPCSTATUS *status)
int tacFormatWriteSupported(tacformat format)
char * tacDefaultExtension(tacformat c)
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
int tacFormatIdentify(const char *s)
char * tacFormattxt(tacformat c)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacWriteXML(TAC *tac, FILE *fp, TPCSTATUS *status)
Header file for library libtpccsv.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_CANNOT_OPEN
Cannot open file.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_CANNOT_WRITE
Cannot write file.
int unitIdentifyFilename(const char *s)
char * unitName(int unit_code)
#define MAX_STUDYNR_LEN
Define max study number length.
Header file for library libtpcift.
Header file for library libtpcisotope.
Header file for library libtpctac.
@ TAC_FORMAT_IF
Hammersmith input file format.
@ TAC_FORMAT_4DM
4DM TAC format (reading supported)
@ TAC_FORMAT_UNKNOWN
Unknown format.
@ TAC_FORMAT_CSV_INT
International CSV.
@ TAC_FORMAT_ABSS_ALLOGG
ALLOGG ABSS data; reading supported.
@ TAC_FORMAT_MAT
Matlab matrix TAC format (reading supported)
@ TAC_FORMAT_ABSS_GEMS
GEMS ABSS data; reading supported.
@ TAC_FORMAT_HTML
HTML table format (supported for writing)
@ TAC_FORMAT_TSV_UK
UK TSV (point as decimal separator)
@ TAC_FORMAT_TSV_INT
International TSV (comma as decimal separator)
@ TAC_FORMAT_IDWC
Hammersmith tissue file format.
@ TAC_FORMAT_XML
XML format (supported for writing)
@ TAC_FORMAT_PMOD
PMOD TAC format.
@ TAC_FORMAT_QVIEW
QView CSV TAC format (reading supported)
@ TAC_FORMAT_SIMPLE
x and y's with space delimiters
@ TAC_FORMAT_ABSS_ALLOGG_OLD
ALLOGG ABSS data (old format); reading supported.
@ TAC_FORMAT_CARIMAS_TXT
Carimas txt format (reading supported)
@ TAC_FORMAT_HRPLUS_HC
HR+ head curve format (reading supported)
@ TAC_FORMAT_DFT
Data format of Turku PET Centre.
@ TAC_FORMAT_HRRT_HC
HRRT head curve format (reading supported)
@ TAC_FORMAT_ABSS_SCANDITRONICS
Scanditronics ABSS data; reading supported.
@ TAC_FORMAT_CSV_UK
UK CSV.
@ TAC_FORMAT_BINARY
Binary format (currently not supported)
@ TAC_FORMAT_SIF
Scan information file.
@ TAC_FORMAT_INVEON
Inveon format (reading supported)
@ TAC_FORMAT_NCI
Old Turku PET Centre format.