TPCCLIB
Loading...
Searching...
No Matches
hcio.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/*****************************************************************************/
27 TAC *tac,
31 CSV *csv,
33 TPCSTATUS *status
34) {
35 int verbose=0; if(status!=NULL) verbose=status->verbose;
36 if(tac==NULL) {
37 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
38 return TPCERROR_FAIL;
39 }
40 if(verbose>0) printf("%s()\n", __func__);
41
42 tacFree(tac);
43
44 if(csv==NULL || csv->row_nr<2 || csv->col_nr<4) {
45 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
46 return TPCERROR_NO_DATA;
47 }
48
49 /* Check from the title fields that data indeed is HRRT HC data */
50 int ret=0;
51 if(strcasecmp(csv->c[0].content, "Singles")!=0) ret++;
52 if(strcasecmp(csv->c[1].content, "Randoms")!=0) ret++;
53 if(strcasecmp(csv->c[2].content, "Prompts")!=0) ret++;
54 if(strncasecmp(csv->c[3].content, "Time(ms)", 4)!=0) ret++;
55 if(ret>0) {
56 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
58 }
59
60 /* Allocate memory for TAC data */
61 ret=tacAllocate(tac, csv->row_nr-1, 3);
62 statusSet(status, __func__, __FILE__, __LINE__, ret);
63 if(ret!=TPCERROR_OK) return ret;
64 tac->tacNr=3; tac->sampleNr=csv->row_nr-1;
65 if(verbose>2) printf(" sampleNr=%d\n", tac->sampleNr);
66
67 /* Set time type */
68 tac->isframe=0;
69 /* Get time unit from the last title field, inside parens */
70 char *cptr=strchr(csv->c[3].content, '(');
71 if(cptr==NULL) {
73 } else {
74 char *tmp=strdup(cptr+1);
75 cptr=strrchr(tmp, ')'); if(cptr!=NULL) *cptr='\0';
76 tac->tunit=unitIdentify(tmp);
77 free(tmp);
78 }
79 /* Set concentration unit */
80 tac->cunit=UNIT_KCPS;
81
82 /* Set TAC names */
83 strcpy(tac->c[0].name, "Singles");
84 strcpy(tac->c[1].name, "Randoms");
85 strcpy(tac->c[2].name, "Prompts");
86
87 /* Copy x and y data from CSV into TAC struct */
88 double v; int oknr=0;
89 for(int i=4; i<csv->nr; i++) {
90 if(csv->c[i].row<1 || csv->c[i].row>tac->sampleNr || csv->c[i].col>3)
91 continue;
92 v=atofVerified(csv->c[i].content); if(!isnan(v)) oknr++;
93 if(csv->c[i].col==3) tac->x[csv->c[i].row-1]=v;
94 else tac->c[csv->c[i].col].y[csv->c[i].row-1]=v;
95 }
96 if(oknr<4) {
97 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
98 return TPCERROR_NO_DATA;
99 }
100
101 /* Set file format */
103
104 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
105 return(TPCERROR_OK);
106}
107/*****************************************************************************/
108
109/*****************************************************************************/
116 TAC *tac,
120 CSV *csv,
122 TPCSTATUS *status
123) {
124 int verbose=0; if(status!=NULL) verbose=status->verbose;
125 if(tac==NULL) {
126 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
127 return TPCERROR_FAIL;
128 }
129 if(verbose>0) printf("%s()\n", __func__);
130
131 tacFree(tac);
132
133 if(csv==NULL || csv->row_nr<2 || csv->col_nr<8) {
134 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
135 return TPCERROR_NO_DATA;
136 }
137
138 /* Check from the title fields that data indeed is HRRT HC data */
139 int ret=0;
140 if(strcasecmp(csv->c[0].content, "time")!=0) ret++;
141 if(strcasecmp(csv->c[1].content, "prompt")!=0) ret++;
142 if(strcasecmp(csv->c[2].content, "delayed")!=0) ret++;
143 if(strcasecmp(csv->c[3].content, "p_rate")!=0) ret++;
144 if(strcasecmp(csv->c[4].content, "d_rate")!=0) ret++;
145 if(strcasecmp(csv->c[5].content, "dtime")!=0) ret++;
146 if(strcasecmp(csv->c[6].content, "frame")!=0) ret++;
147 if(strcasecmp(csv->c[7].content, "singles")!=0) ret++;
148 if(ret>0) {
149 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
151 }
152
153 /* Allocate memory for TAC data */
154 ret=tacAllocate(tac, csv->row_nr-1, 7);
155 statusSet(status, __func__, __FILE__, __LINE__, ret);
156 if(ret!=TPCERROR_OK) return ret;
157 tac->tacNr=7; tac->sampleNr=csv->row_nr-1;
158 if(verbose>2) printf(" sampleNr=%d\n", tac->sampleNr);
159
160 /* Set time type */
161 tac->isframe=0;
162 /* Set time unit */
163 tac->tunit=UNIT_MSEC;
164 /* Set concentration unit */
165 tac->cunit=UNIT_KCPS;
166
167 /* Set TAC names */
168 strcpy(tac->c[0].name, "prompt");
169 strcpy(tac->c[1].name, "delayed");
170 strcpy(tac->c[2].name, "p_rate");
171 strcpy(tac->c[3].name, "d_rate");
172 strcpy(tac->c[4].name, "dtime");
173 strcpy(tac->c[5].name, "frame");
174 strcpy(tac->c[6].name, "singles");
175
176 /* Copy x and y data from CSV into TAC struct */
177 double v; int oknr=0;
178 for(int i=8; i<csv->nr; i++) {
179 if(csv->c[i].row<1 || csv->c[i].row>tac->sampleNr || csv->c[i].col>7)
180 continue;
181 v=atofVerified(csv->c[i].content); if(!isnan(v)) oknr++;
182 if(csv->c[i].col==0) tac->x[csv->c[i].row-1]=v;
183 else tac->c[csv->c[i].col-1].y[csv->c[i].row-1]=v;
184 }
185 if(oknr<8) {
186 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
187 return TPCERROR_NO_DATA;
188 }
189
190 /* Set file format */
192
193 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
194 return(TPCERROR_OK);
195}
196/*****************************************************************************/
197
198/*****************************************************************************/
double atofVerified(const char *s)
Definition decpoint.c:75
int tacReadHRPLUSHC(TAC *tac, CSV *csv, TPCSTATUS *status)
Definition hcio.c:114
int tacReadHRRTHC(TAC *tac, CSV *csv, TPCSTATUS *status)
Definition hcio.c:25
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
char * strdup(const char *s)
Definition stringext.c:185
int col
Definition tpccsv.h:28
int row
Definition tpccsv.h:26
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
int nr
Definition tpccsv.h:42
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int cunit
Definition tpctac.h:105
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
int tunit
Definition tpctac.h:109
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.
@ UNIT_MSEC
milliseconds
@ UNIT_UNKNOWN
Unknown unit.
@ UNIT_KCPS
kilocounts/s
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
int unitIdentify(const char *s)
Definition units.c:162
Header file for library libtpcift.
Header file for library libtpcisotope.
Header file for library libtpctac.
@ TAC_FORMAT_HRPLUS_HC
HR+ head curve format (reading supported)
Definition tpctac.h:52
@ TAC_FORMAT_HRRT_HC
HRRT head curve format (reading supported)
Definition tpctac.h:51