TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcimgio
sifio.c
Go to the documentation of this file.
1
5
/*****************************************************************************/
6
#include "
libtpcimgio.h
"
7
/*****************************************************************************/
8
9
/*****************************************************************************/
21
int
sifRead
(
23
char
*filename,
25
SIF
*data
26
) {
27
int
i, n, frameNr, yy, mm, dd, h, m, s;
28
int
ret;
29
struct
tm *st;
30
time_t timet;
31
32
33
if
(
SIF_TEST
) printf(
"sifRead(%s, *sif)\n"
, filename);
34
if
(filename==NULL || data==NULL)
return
(1);
35
/* Empty data */
36
sifEmpty
(data);
37
38
/* Read the lines of the SIF */
39
STR_TOKEN_LIST
tlst;
str_token_list_init
(&tlst);
40
//printf("reading lines\n");
41
ret=
textfileReadLines
(filename, &tlst);
42
if
(ret>2) {strcpy(
siferrmsg
,
"cannot read file"
);
return
(5);}
43
else
if
(ret>0) {strcpy(
siferrmsg
,
"cannot open file"
);
return
(2);}
44
if
(0) {
45
printf(
"lineNr:=%d\n"
, tlst.
token_nr
);
46
for
(
int
i=0; i<tlst.
token_nr
; i++) printf(
"token[%d] := '%s'\n"
, i, tlst.
tok
[i]);
47
}
48
49
/* Remove any comment or empty lines */
50
//printf("removing any comment lines\n");
51
i=tlst.
token_nr
-1;
52
while
(i>=0 && tlst.
token_nr
>0) {
53
//printf("i=%d line='%s'\n", i, tlst.tok[i]);
54
if
(
strnlen
(tlst.
tok
[i], 2)<1 ||
asciiCommentLine
(tlst.
tok
[i], NULL)) {
55
//printf("-> remove\n");
56
str_token_list_del
(&tlst, 1+i);
57
}
58
i--;
59
}
60
if
(tlst.
token_nr
<1) {
61
str_token_list_empty
(&tlst);
62
strcpy(
siferrmsg
,
"wrong format"
);
return
(4);
63
}
64
if
(0) {
65
printf(
"lineNr:=%d\n"
, tlst.
token_nr
);
66
for
(
int
i=0; i<tlst.
token_nr
; i++)
67
printf(
"token[%d] := '%s'\n"
, i, tlst.
tok
[i]);
68
}
69
70
/* Read the title line */
71
if
(
SIF_TEST
) printf(
"SIF title := '%s'\n"
, tlst.
tok
[0]);
72
n=sscanf(tlst.
tok
[0],
"%d/%d/%d %d:%d:%d %d %d %d %255s %7s"
,
73
&dd, &mm, &yy, &h, &m, &s, &frameNr, &data->
colNr
, &data->
version
,
74
data->
studynr
, data->
isotope_name
);
75
//printf("tok_n := %d\n", n);
76
if
(n<9 || frameNr<1 || data->colNr<2 || data->version!=1) {
77
if
(
SIF_TEST
) printf(
"invalid SIF title line\n"
);
78
strcpy(
siferrmsg
,
"wrong filetype"
);
79
str_token_list_empty
(&tlst);
80
return
(4);
81
}
82
timet=time(NULL); st=gmtime(&timet);
83
if
(st!=NULL) {
84
st->tm_mday=dd; st->tm_mon=mm-1; st->tm_year=yy-1900;
85
st->tm_hour=h; st->tm_min=m; st->tm_sec=s; st->tm_isdst=-1;
86
data->
scantime
=
timegm
(st);
if
(data->
scantime
==-1) data->
scantime
=0;
87
}
else
{
88
data->
scantime
=0;
89
}
90
//printf("frameNr := %d\n", frameNr);
91
92
/* Allocate memory for SIF data */
93
if
(
sifSetmem
(data, frameNr)) {
94
str_token_list_empty
(&tlst);
95
strcpy(
siferrmsg
,
"cannot allocate SIF"
);
return
(6);
96
}
97
98
/* Read data lines into SIF */
99
i=0;
100
while
(i<data->frameNr && i<tlst.
token_nr
-1) {
101
//printf("i := %d\n", i);
102
data->
prompts
[i]=data->
randoms
[i]=0.0;
103
n=sscanf(tlst.
tok
[1+i],
"%lf %lf %lf %lf"
, &data->
x1
[i], &data->
x2
[i],
104
&data->
prompts
[i], &data->
randoms
[i]);
105
//printf("n := %d\n", n);
106
//if(n<data->colNr || data->x2[i]<data->x1[i]) {
107
if
(n<2) {
108
strcpy(
siferrmsg
,
"wrong data format"
);
109
str_token_list_empty
(&tlst);
110
sifEmpty
(data);
111
return
(9);
112
}
113
if
(data->
x2
[i]<data->
x1
[i]) {
114
strcpy(
siferrmsg
,
"invalid time frames"
);
115
str_token_list_empty
(&tlst);
116
sifEmpty
(data);
117
return
(9);
118
}
119
i++;
120
}
121
str_token_list_empty
(&tlst);
122
if
(i!=data->
frameNr
) {
123
strcpy(
siferrmsg
,
"wrong data format"
);
124
sifEmpty
(data);
125
return
(9);
126
}
127
128
129
/* Calculate trues */
130
if
(data->
colNr
>=4)
for
(i=0; i<data->
frameNr
; i++)
131
data->
trues
[i]=data->
prompts
[i]-data->
randoms
[i];
132
/* Set weights to 1.0 */
133
for
(i=0; i<data->
frameNr
; i++) data->
weights
[i]=1.0;
134
135
return
(0);
136
}
137
/*****************************************************************************/
138
139
/*****************************************************************************/
145
int
sifWrite
(
147
SIF
*data,
150
char
*filename
151
) {
152
int
i, n, req_decimals=0;
153
char
buf[1024];
154
struct
tm tm;
//*st;
155
156
157
if
(
SIF_TEST
) printf(
"sifWrite(*sif, %s)\n"
, filename);
158
/* Check data */
159
if
(data->
frameNr
<1) {strcpy(
siferrmsg
,
"no data to save"
);
return
1;}
160
161
/* Set file pointer */
162
FILE *fp;
163
int
intofile;
164
if
(strcasecmp(filename,
"STDOUT"
)==0) {
165
fp=stdout; intofile=0;
166
}
else
{
167
/* Open file */
168
fp=fopen(filename,
"w"
);
169
if
(fp==NULL) {strcpy(
siferrmsg
,
"cannot open file"
);
return
2;}
170
intofile=1;
171
}
172
173
/* Write title line */
174
if
(!
gmtime_r
((time_t*)&data->
scantime
, &tm) || !strftime(buf, 1024,
"%d/%m/%Y %H:%M:%S"
, &tm))
175
strcpy(buf,
"1/1/1900 00:00:00"
);
176
n=fprintf(fp,
"%s %d %d %d"
, buf, data->
frameNr
, data->
colNr
, data->
version
);
177
if
(
SIF_TEST
) printf(
"%s %d %d %d\n"
, buf, data->
frameNr
, data->
colNr
, data->
version
);
178
if
(n<7) {
179
strcpy(
siferrmsg
,
"cannot write file"
);
if
(intofile) fclose(fp);
180
return
2;
181
}
182
if
(strlen(data->
studynr
)!=0 || strlen(data->
isotope_name
)!=0) {
183
/* Write also study number and isotope */
184
if
(strlen(data->
studynr
)==0) fprintf(fp,
" ."
);
else
fprintf(fp,
" %s"
, data->
studynr
);
185
if
(strlen(data->
isotope_name
)>0) fprintf(fp,
" %s"
, data->
isotope_name
);
186
}
187
fprintf(fp,
"\n"
);
188
189
/* Check if frame times need to printed with decimals */
190
for
(i=1, req_decimals=0; i<data->
frameNr
; i++) {
191
if
(round(data->
x1
[i])==round(data->
x1
[i-1])) {req_decimals=1;
break
;}
192
if
(round(data->
x2
[i])==round(data->
x2
[i-1])) {req_decimals=1;
break
;}
193
}
194
195
/* Write data lines */
196
for
(i=0; i<data->
frameNr
; i++) {
197
if
(req_decimals==0) n=fprintf(fp,
"%.0f %.0f"
, data->
x1
[i], data->
x2
[i]);
198
else
n=fprintf(fp,
"%.1f %.1f"
, data->
x1
[i], data->
x2
[i]);
199
if
(n<3) {
200
strcpy(
siferrmsg
,
"cannot write file"
);
if
(intofile) fclose(fp);
201
return
3;
202
}
203
if
(data->
colNr
<=2) {n=fprintf(fp,
"\n"
);
continue
;}
204
n=fprintf(fp,
" %.0f %.0f"
, data->
prompts
[i], data->
randoms
[i]);
205
if
(n<1) {
206
strcpy(
siferrmsg
,
"cannot write file"
);
if
(intofile) fclose(fp);
207
return
3;
208
}
209
if
(data->
colNr
<5) {n=fprintf(fp,
"\n"
);
continue
;}
210
n=fprintf(fp,
" %.0f"
, data->
trues
[i]);
211
if
(n<1) {
212
strcpy(
siferrmsg
,
"cannot write file"
);
if
(intofile) fclose(fp);
213
return
3;
214
}
215
if
(data->
colNr
<6) {n=fprintf(fp,
"\n"
);
continue
;}
216
n=fprintf(fp,
" %.5f\n"
, data->
weights
[i]);
217
if
(n<1) {
218
strcpy(
siferrmsg
,
"cannot write file"
);
if
(intofile) fclose(fp);
219
return
3;
220
}
221
}
222
223
/* Close file */
224
if
(intofile) fclose(fp);
225
226
return
0;
227
}
228
/*****************************************************************************/
229
230
/*****************************************************************************/
234
void
sifPrint
(
236
SIF
*data
237
) {
238
char
buf[32];
239
240
if
(!
ctime_r_int
((time_t*)&data->
scantime
, buf))
241
strcpy(buf,
"1900-01-01 00:00:00"
);
242
printf(
"Scan time: %s\n"
, buf);
243
printf(
"Isotope: %s\n"
, data->
isotope_name
);
244
printf(
"Frame start end Prompts Randoms Trues Weight\n"
);
245
for
(
int
i=0; i<data->
frameNr
; i++) {
246
printf(
" %03d %6.1f %6.1f %10.0f %10.0f %10.0f %8.6f\n"
, i+1,
247
data->
x1
[i], data->
x2
[i], data->
prompts
[i], data->
randoms
[i],
248
data->
trues
[i], data->
weights
[i]);
249
}
250
return
;
251
}
252
/*****************************************************************************/
253
254
/*****************************************************************************/
ctime_r_int
char * ctime_r_int(const time_t *t, char *buf)
Convert calendard time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
Definition
datetime.c:110
timegm
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
Definition
datetime.c:69
gmtime_r
struct tm * gmtime_r(const time_t *t, struct tm *tm)
Convert time_t to GMT struct tm.
Definition
datetime.c:22
libtpcimgio.h
Header file for libtpcimgio.
SIF_TEST
int SIF_TEST
Definition
sif.c:6
sifSetmem
int sifSetmem(SIF *data, int frameNr)
Definition
sif.c:56
sifEmpty
void sifEmpty(SIF *data)
Definition
sif.c:33
siferrmsg
char siferrmsg[128]
Definition
sif.c:7
str_token_list_empty
void str_token_list_empty(STR_TOKEN_LIST *lst)
Definition
readfile.c:26
str_token_list_del
int str_token_list_del(STR_TOKEN_LIST *lst, int item)
Definition
readfile.c:70
textfileReadLines
int textfileReadLines(const char *filename, STR_TOKEN_LIST *lst)
Definition
readfile.c:136
str_token_list_init
void str_token_list_init(STR_TOKEN_LIST *lst)
Definition
readfile.c:13
strnlen
size_t strnlen(const char *s, size_t n)
Definition
strext.c:181
asciiCommentLine
int asciiCommentLine(const char *line, int *cont)
Definition
readfile.c:246
sifWrite
int sifWrite(SIF *data, char *filename)
Definition
sifio.c:145
sifPrint
void sifPrint(SIF *data)
Definition
sifio.c:234
sifRead
int sifRead(char *filename, SIF *data)
Definition
sifio.c:21
SIF
Definition
libtpcimgio.h:1443
SIF::x1
double * x1
Definition
libtpcimgio.h:1457
SIF::prompts
double * prompts
Definition
libtpcimgio.h:1461
SIF::frameNr
int frameNr
Definition
libtpcimgio.h:1447
SIF::x2
double * x2
Definition
libtpcimgio.h:1459
SIF::version
int version
Definition
libtpcimgio.h:1451
SIF::studynr
char studynr[MAX_STUDYNR_LEN+1]
Definition
libtpcimgio.h:1453
SIF::scantime
time_t scantime
Definition
libtpcimgio.h:1445
SIF::isotope_name
char isotope_name[8]
Definition
libtpcimgio.h:1455
SIF::weights
double * weights
Definition
libtpcimgio.h:1467
SIF::colNr
int colNr
Definition
libtpcimgio.h:1449
SIF::randoms
double * randoms
Definition
libtpcimgio.h:1463
SIF::trues
double * trues
Definition
libtpcimgio.h:1465
STR_TOKEN_LIST
Definition
libtpcmisc.h:387
STR_TOKEN_LIST::tok
char ** tok
Definition
libtpcmisc.h:393
STR_TOKEN_LIST::token_nr
int token_nr
Definition
libtpcmisc.h:389
Generated on
for TPCCLIB by
1.17.0