TPCCLIB
Loading...
Searching...
No Matches
qviewio.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcift.h"
8#include "tpcisotope.h"
9#include "tpccsv.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/*****************************************************************************/
29 TAC *tac,
31 CSV *csv,
33 const int grouponly,
35 TPCSTATUS *status
36) {
37 int verbose=0; if(status!=NULL) verbose=status->verbose;
38 if(tac==NULL) {
39 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
40 return TPCERROR_FAIL;
41 }
42 if(verbose>0) {printf("%s()\n", __func__); fflush(stdout);}
43
44 tacFree(tac);
45
46 if(csv==NULL || csv->row_nr<2 || csv->col_nr<12) {
47 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
48 return TPCERROR_NO_DATA;
49 }
50
51 int ret=0;
52
53 /* Check from the first line (title) that data indeed is QView data */
54 if(strcasecmp(csv->c[0].content, "directory")!=0 ||
55 strcasecmp(csv->c[11].content, "group")!=0)
56 {
57 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
59 }
60
61 /* Find the highest frame number from parens in 'num' field */
62 int maxFrameNr=0;
63 {
64 int fn;
65 char *cptr, *cptr2;
66 for(int ri=1; ri<csv->row_nr; ri++) {
67 cptr=csvCell(csv, ri, 2); if(cptr==NULL) continue;
68 if(verbose>50) printf(" '%s'\n", cptr);
69 cptr2=strchr(cptr, '('); if(cptr2==NULL) continue;
70 fn=atoi(cptr2+1); if(fn>maxFrameNr) maxFrameNr=fn;
71 }
72 }
73 if(verbose>1) printf("maxFrameNr := %d\n", maxFrameNr);
74 if(maxFrameNr<1) {
75 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
77 }
78 /* Estimate the number of TACs */
79 int maxTacNr=(csv->row_nr-1)/maxFrameNr; // row_nr includes the title line
80 if(verbose>1) printf("maxTacNr := %d\n", maxTacNr);
81 if(maxFrameNr<1) {
82 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
84 }
85
86 /* Allocate memory for TAC data; and to be on the safe side, for one extra TAC */
87 ret=tacAllocate(tac, maxFrameNr, maxTacNr);
88 statusSet(status, __func__, __FILE__, __LINE__, ret);
89 if(ret!=TPCERROR_OK) return(ret);
90 tac->isframe=0;
91
92 /* Copy data from CSV into TAC structure */
93 ret=0;
94 for(int i=1; i<csv->row_nr; i++) {
95 /* Read the cells of this CSV line */
96 char *cnum, *cslice, *cmean, *cpixel, *cgroup;
97 cnum=csvCell(csv, i, 2); if(cnum==NULL) continue;
98 cslice=csvCell(csv, i, 3); if(cslice==NULL) continue;
99 cmean=csvCell(csv, i, 4); if(cmean==NULL) continue;
100 cpixel=csvCell(csv, i, 9); if(cpixel==NULL) continue;
101 cgroup=csvCell(csv, i, 11); if(cgroup==NULL) continue;
102 /* Get slice number; if missing, then this is group ROI */
103 int slice;
104 ret=atoiCheck(cslice, &slice);
105 if(ret!=0) {slice=-1; ret=0;}
106 /* If only group ROIs were requested, and this is not group */
107 if(slice>0 && grouponly!=0) continue;
108 /* Depending on previous, get frame number and group name, or frame and ROI number */
109 char *cptr=strchr(cnum, '('); if(cptr==NULL) continue;
110 int fn=atoi(cptr+1); if(fn<1 || fn>maxFrameNr) {ret=1; break;}
111 char tacname[MAX_TACNAME_LEN+1];
112 if(slice<1) { // this is group
113 /* Check group name, and use it as TAC name */
114 if(strnlen(cgroup, 2)<1) {ret=2; break;}
115 strlcpy(tacname, cgroup, MAX_TACNAME_LEN);
116 } else { // this is one roi at one image slice
117 /* Get ROI number */
118 int roinr=atoi(cnum); if(roinr<1) {ret=3; break;}
119 /* If group name exists, the use that and ROI number and slice number to construct TAC name */
120 if(strnlen(cgroup, 2)>0 && strnlen(cgroup, 2)<12) {
121 sprintf(tacname, "%s_roi%d__pl%d", cgroup, roinr, slice);
122 } else {
123 /* Otherwise use ROI number and slice number to construct TAC name */
124 sprintf(tacname, "roi%d__pl%d", roinr, slice);
125 }
126 }
127 /* Get the number of pixels */
128 int pxlnr;
129 ret=atoiCheck(cpixel, &pxlnr); if(ret) {pxlnr=0; ret=0;}
130 /* Get the ROI concentration */
131 double conc=atofVerified(cmean);
132 if(verbose>20) printf("tacname='%s' pxlnr=%d fn=%d conc=%g\n", tacname, pxlnr, fn, conc);
133 /* Determine the TAC index for this; first check if this TAC already is added */
134 int ri;
135 for(ri=0; ri<tac->tacNr; ri++) {
136 if(strcmp(tac->c[ri].name, tacname)==0 && doubleMatch(tac->c[ri].size, (double)pxlnr, 0.1))
137 break;
138 /* if TAC is not yet added, then ri will be set to the first free index */
139 }
140 /* check that ri is not too big */
141 if(ri>=maxTacNr) {ret=4; break;}
142 /* Put value in place */
143 tac->c[ri].y[fn-1]=conc;
144 /* If this was first encounter of this TAC, then set other info, and increase tacNr */
145 if(ri>=tac->tacNr) {
146 strcpy(tac->c[ri].name, tacname);
147 tac->c[ri].size=(double)pxlnr;
148 tac->tacNr++;
149 }
150 /* Increase also sampleNr when necessary */
151 if(fn>tac->sampleNr) tac->sampleNr=fn;
152 }
153 if(ret) {
154 if(verbose>1) printf(" error %d\n", ret);
155 tacFree(tac);
156 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
158 }
159
160 if(tac->sampleNr<1 || tac->tacNr<1) {
161 tacFree(tac);
162 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
164 }
165
166 for(int i=0; i<tac->sampleNr; i++) tac->x[i]=tac->x1[i]=tac->x2[i]=0.0;
167
169
170 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
171 return(TPCERROR_OK);
172}
173/*****************************************************************************/
174
175/*****************************************************************************/
char * csvCell(CSV *csv, int row, int col)
Definition csv.c:358
double atofVerified(const char *s)
Definition decpoint.c:75
int doubleMatch(const double v1, const double v2, const double lim)
Definition doubleutil.c:27
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int tacReadQView(TAC *tac, CSV *csv, const int grouponly, TPCSTATUS *status)
Definition qviewio.c:27
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * content
Definition tpccsv.h:30
Definition tpccsv.h:36
int row_nr
Definition tpccsv.h:44
int col_nr
Definition tpccsv.h:46
CSV_item * c
Definition tpccsv.h:38
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
double * y
Definition tpctac.h:75
double size
Definition tpctac.h:71
Definition tpctac.h:87
double * x
Definition tpctac.h:97
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
void tacFree(TAC *tac)
Definition tac.c:106
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition tac.c:130
Header file for library libtpccsv.
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero)
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
Header file for library libtpcift.
Header file for library libtpcisotope.
Header file for library libtpctac.
@ TAC_FORMAT_QVIEW
QView CSV TAC format (reading supported)
Definition tpctac.h:48