TPCCLIB
Loading...
Searching...
No Matches
parift.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10#include <time.h>
11#include <string.h>
12/*****************************************************************************/
13#include "tpcextensions.h"
14#include "tpcift.h"
15/*****************************************************************************/
16#include "tpcpar.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
26 PAR *par,
29 IFT *ift,
31 TPCSTATUS *status
32) {
33 int verbose=0; if(status!=NULL) verbose=status->verbose;
34 if(ift==NULL) {
35 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
36 return TPCERROR_FAIL;
37 }
38 if(verbose>0) printf("%s():\n", __func__);
39 if(par==NULL || par->tacNr<1 || par->parNr<1) {
40 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
41 return TPCERROR_NO_DATA;
42 }
43
44 /* Set decimal and item separators */
45 int tointl=0;
47 tointl=1;
48
49 int ret;
50 char tmp[128];
51
52 /* Set content id */
53 ret=iftPut(ift, "content", "parameters", 1, NULL);
54 if(ret!=TPCERROR_OK) {
55 statusSet(status, __func__, __FILE__, __LINE__, ret);
56 return ret;
57 }
58
59 /* Copy PAR headers */
60 sprintf(tmp, "%d", par->parNr);
61 ret=iftPut(ift, "par_nr", tmp, 1, NULL);
62 for(int i=0; i<par->h.keyNr && ret==TPCERROR_OK; i++) {
63 /* not if value is missing */
64 if(par->h.item[i].value==NULL || strlen(par->h.item[i].value)<1) continue;
65 /* not if key is missing */
66 if(par->h.item[i].key==NULL || strlen(par->h.item[i].key)<1) continue;
67 /* not if this will be written later */
68 if(strcasecmp(par->h.item[i].key, "model")==0) continue;
69 if(strcasecmp(par->h.item[i].key, "dataNr")==0) continue;
70 if(strcasecmp(par->h.item[i].key, "fitNr")==0) continue;
71 if(strcasecmp(par->h.item[i].key, "fit_start")==0) continue;
72 if(strcasecmp(par->h.item[i].key, "fit_end")==0) continue;
73 /* ok to copy */
74 ret=iftPut(ift, par->h.item[i].key, par->h.item[i].value, 1, NULL);
75 }
76 if(ret!=TPCERROR_OK) {
77 statusSet(status, __func__, __FILE__, __LINE__, ret);
78 return ret;
79 }
80
81 if(parIsModel(par)==1) {
82 sprintf(tmp, "%d", par->r[0].model);
83 iftPut(ift, "model", tmp, 0, NULL);
84 }
85 if(parIsDataNr(par)==1) {
86 sprintf(tmp, "%d", par->r[0].dataNr);
87 iftPut(ift, "data_nr", tmp, 0, NULL);
88 }
89 if(parIsFitNr(par)==1) {
90 sprintf(tmp, "%d", par->r[0].fitNr);
91 iftPut(ift, "fit_nr", tmp, 0, NULL);
92 }
93 if(parIsFitRange(par)==1) {
94 sprintf(tmp, "%g min", par->r[0].start);
95 if(tointl) strReplaceChar(tmp, '.', ',');
96 iftPut(ift, "fit_start", tmp, 0, NULL);
97 sprintf(tmp, "%g min", par->r[0].end);
98 if(tointl) strReplaceChar(tmp, '.', ',');
99 iftPut(ift, "fit_end", tmp, 0, NULL);
100 }
101
102 /* Copy parameter table contents into IFT */
103 int ri, pi;
104 for(ri=0; ri<par->tacNr; ri++) {
105 sprintf(tmp, "%d", 1+ri);
106 iftPut(ift, "parameter-set", tmp, 0, NULL);
107 iftPut(ift, "tac_name", par->r[ri].name, 0, NULL);
108 if(parIsModel(par)==2) {
109 sprintf(tmp, "%d", par->r[ri].model);
110 iftPut(ift, "model", tmp, 0, NULL);
111 }
112 if(parIsDataNr(par)==2) {
113 sprintf(tmp, "%d", par->r[ri].dataNr);
114 iftPut(ift, "dataNr", tmp, 0, NULL);
115 }
116 if(parIsFitNr(par)==2) {
117 sprintf(tmp, "%d", par->r[ri].fitNr);
118 iftPut(ift, "fitNr", tmp, 0, NULL);
119 }
120 if(parIsFitRange(par)==2) {
121 sprintf(tmp, "%g min", par->r[ri].start);
122 if(tointl) strReplaceChar(tmp, '.', ',');
123 iftPut(ift, "fit_start", tmp, 0, NULL);
124 sprintf(tmp, "%g min", par->r[ri].end);
125 if(tointl) strReplaceChar(tmp, '.', ',');
126 iftPut(ift, "fit_end", tmp, 0, NULL);
127 }
128 if(!isnan(par->r[ri].wss)) {
129 sprintf(tmp, "%g", par->r[ri].wss);
130 if(tointl) strReplaceChar(tmp, '.', ',');
131 iftPut(ift, "wss", tmp, 0, NULL);
132 }
133 for(pi=0; pi<par->parNr; pi++) if(!isnan(par->r[ri].p[pi])) {
134 if(par->n[pi].unit==UNIT_UNKNOWN) sprintf(tmp, "%g", par->r[ri].p[pi]);
135 else sprintf(tmp, "%g %s", par->r[ri].p[pi], unitName(par->n[pi].unit));
136 if(tointl) strReplaceChar(tmp, '.', ',');
137 iftPut(ift, par->n[pi].name, tmp, 0, NULL);
138 if(!isnan(par->r[ri].sd[pi])) {
139 sprintf(tmp, "%g", par->r[ri].sd[pi]);
140 if(tointl) strReplaceChar(tmp, '.', ',');
141 iftPut(ift, "SD", tmp, 0, NULL);
142 }
143 if(!isnan(par->r[ri].cl1[pi])) {
144 sprintf(tmp, "%g", par->r[ri].cl1[pi]);
145 if(tointl) strReplaceChar(tmp, '.', ',');
146 iftPut(ift, "95%CL1", tmp, 0, NULL);
147 }
148 if(!isnan(par->r[ri].cl2[pi])) {
149 sprintf(tmp, "%g", par->r[ri].cl2[pi]);
150 if(tointl) strReplaceChar(tmp, '.', ',');
151 iftPut(ift, "95%CL2", tmp, 0, NULL);
152 }
153 } // next parameter
154 } // next tac
155
156 /* Quit */
157 statusSet(status, __func__, __FILE__, __LINE__, ret);
158 return(ret);
159}
160/*****************************************************************************/
161
162/*****************************************************************************/
169 PAR *par,
171 FILE *fp,
173 TPCSTATUS *status
174) {
175 int verbose=0; if(status!=NULL) verbose=status->verbose;
176 if(fp==NULL) {
177 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_WRITE);
179 }
180 if(verbose>0) printf("%s()\n", __func__);
181 if(par==NULL || par->tacNr<1 || par->parNr<1) {
182 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
183 return TPCERROR_NO_DATA;
184 }
185
186 int ret;
187 IFT ift;
188 iftInit(&ift);
189 ret=parToIFT(par, &ift, status);
190 if(ret!=TPCERROR_OK) {iftFree(&ift); return ret;}
191 ret=iftWrite(&ift, fp, status);
192 iftFree(&ift);
193 if(ret!=TPCERROR_OK) return ret;
194
195 /* Quit */
196 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
197 return(TPCERROR_OK);
198}
199/*****************************************************************************/
200
201/*****************************************************************************/
210 PAR *par,
212 IFT *ift,
214 TPCSTATUS *status
215) {
216 int verbose=0; if(status!=NULL) verbose=status->verbose;
217 if(par==NULL) {
218 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
219 return TPCERROR_FAIL;
220 }
221 if(verbose>0) printf("%s()\n", __func__);
222 if(ift==NULL || ift->keyNr<1) {
223 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
224 return TPCERROR_NO_DATA;
225 }
226
227 /* Verify that IFT contains results */
228 if(strcasecmp(ift->item[0].key, "content") ||
229 strcasecmp(ift->item[0].value, "parameters")
230 ) {
231 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNSUPPORTED);
233 }
234
235 int ret, parNr, tacNr;
236 /* Read the nr of parameters */
237 if(iftGetInt(ift, iftFindKey(ift, "par_nr", 0), &parNr)!=0 || parNr<1) {
238 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNSUPPORTED);
240 }
241 if(verbose>5) printf("parNr := %d\n", parNr);
242 /* Read the nr of parameter sets (tacs) */
243 tacNr=iftFindNrOfKeys(ift, "parameter-set");
244 if(tacNr<1) {
245 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNSUPPORTED);
247 }
248 if(verbose>5) printf("tacNr := %d\n", tacNr);
249
250 /* Allocate memory for PAR */
251 parFree(par);
252 ret=parAllocate(par, parNr, tacNr);
253 if(ret!=TPCERROR_OK) {
254 statusSet(status, __func__, __FILE__, __LINE__, ret);
255 return ret;
256 }
257 par->parNr=parNr; par->tacNr=tacNr;
258
259 /* Read header contents */
260 for(int i=1, ret=0; i<ift->keyNr && ret==TPCERROR_OK; i++) {
261 /* not if value is missing */
262 if(ift->item[i].value==NULL || strlen(ift->item[i].value)<1) continue;
263 /* not if key is missing */
264 if(ift->item[i].key==NULL || strlen(ift->item[i].key)<1) continue;
265 /* end of header? */
266 if(strcasecmp(ift->item[i].key, "parameter-set")==0) break;
267 /* not needed */
268 if(strcasecmp(ift->item[i].key, "par_nr")==0) continue;
269 if(strcasecmp(ift->item[i].key, "format")==0) continue;
270 if(strcasecmp(ift->item[i].key, "tac_nr")==0) continue;
271 /* ok to copy */
272 ret=iftPut(&par->h, ift->item[i].key, ift->item[i].value, 1, NULL);
273 }
274 if(ret!=TPCERROR_OK) {
275 statusSet(status, __func__, __FILE__, __LINE__, ret);
276 return ret;
277 }
278
279 /* Read parameter values */
280 int i, pi=0, ri=0; ret=0;
281 char tmp[128];
282 i=iftFindKey(ift, "parameter-set", 0);
283 for(i++; i<ift->keyNr && ret==TPCERROR_OK; i++) {
284 if(verbose>60)
285 printf("i=%d key='%s' value='%s'\n",
286 i, ift->item[i].key, ift->item[i].value);
287 /* new parameter-set? */
288 if(strcasecmp(ift->item[i].key, "parameter-set")==0) {
289 ri++; pi=0; if(ri<par->tacNr) continue; else break;}
290 /* parameter set info? */
291 if(strcasecmp(ift->item[i].key, "tac_name")==0) {
292 strncpy(par->r[ri].name, ift->item[i].value, MAX_TACNAME_LEN);
293 par->r[ri].name[MAX_TACNAME_LEN]='\0';
294 continue;
295 }
296 if(strcasecmp(ift->item[i].key, "wss")==0) {
297 if(iftGetDouble(ift, i, &par->r[ri].wss)) {ret++; break;}
298 continue;
299 }
300 if(strcasecmp(ift->item[i].key, "model")==0) {
301 if(iftGetUInt(ift, i, &par->r[ri].model)) {ret++; break;}
302 continue;
303 }
304 if(strcasecmp(ift->item[i].key, "data_nr")==0) {
305 if(iftGetInt(ift, i, &par->r[ri].dataNr)) {ret++; break;}
306 continue;
307 }
308 if(strcasecmp(ift->item[i].key, "fit_nr")==0) {
309 if(iftGetInt(ift, i, &par->r[ri].fitNr)) {ret++; break;}
310 continue;
311 }
312 if(strcasecmp(ift->item[i].key, "fit_start")==0) {
313 if(iftGetDouble(ift, i, &par->r[ri].start)) {ret++; break;}
314 continue;
315 }
316 if(strcasecmp(ift->item[i].key, "fit_end")==0) {
317 if(iftGetDouble(ift, i, &par->r[ri].end)) {ret++; break;}
318 continue;
319 }
320 /* SD or CL ? */
321 if(strcasecmp(ift->item[i].key, "SD")==0) {
322 if(iftGetDouble(ift, i, &par->r[ri].sd[pi])) {ret++; break;}
323 continue;
324 }
325 if(strcasecmp(ift->item[i].key, "95%CL1")==0) {
326 if(iftGetDouble(ift, i, &par->r[ri].cl1[pi])) {ret++; break;}
327 continue;
328 }
329 if(strcasecmp(ift->item[i].key, "95%CL2")==0) {
330 if(iftGetDouble(ift, i, &par->r[ri].cl2[pi])) {ret++; break;}
331 continue;
332 }
333 /* Ok this should be parameter */
334 if(pi>=par->parNr) {ret++; break;}
335 strncpy(par->n[pi].name, ift->item[i].key, MAX_PARNAME_LEN);
336 par->n[pi].name[MAX_PARNAME_LEN]='\0';
337 if(iftGetDouble(ift, i, &par->r[ri].p[pi])) {ret++; break;}
338 /* next token, if one exists, should be the unit */
339 if(strTokenNr(ift->item[i].value, " \t")==2) {
340 if(strTokenNCpy(ift->item[i].value, " \t", 2, tmp, 64)>0)
341 par->n[pi].unit=unitIdentify(tmp);
342 }
343 pi++;
344 }
345 if(ret!=TPCERROR_OK) {
346 statusSet(status, __func__, __FILE__, __LINE__, ret);
347 return ret;
348 }
349
350 /* Set format in struct */
352
353 /* Quit */
354 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
355 return(TPCERROR_OK);
356}
357/*****************************************************************************/
358
359/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
void iftInit(IFT *ift)
Definition ift.c:21
int iftFindNrOfKeys(IFT *ift, const char *key)
Definition iftfind.c:142
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
int iftGetUInt(IFT *ift, int index, unsigned int *v)
Definition iftget.c:101
int iftGetDouble(IFT *ift, int index, double *v)
Definition iftget.c:52
int iftGetInt(IFT *ift, int index, int *v)
Definition iftget.c:76
int iftWrite(IFT *ift, FILE *fp, TPCSTATUS *status)
Definition iftio.c:98
int parIsFitRange(PAR *par)
Definition par.c:267
void parFree(PAR *par)
Definition par.c:75
int parAllocate(PAR *par, int parNr, int tacNr)
Definition par.c:108
int parIsFitNr(PAR *par)
Definition par.c:343
int parIsDataNr(PAR *par)
Definition par.c:362
int parIsModel(PAR *par)
Definition par.c:288
int parWriteIFT(PAR *par, FILE *fp, TPCSTATUS *status)
Definition parift.c:167
int parToIFT(PAR *par, IFT *ift, TPCSTATUS *status)
Definition parift.c:24
int parFromIFT(PAR *par, IFT *ift, TPCSTATUS *status)
Definition parift.c:207
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
void strReplaceChar(char *s, char c1, char c2)
Definition stringext.c:134
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
char * value
Definition tpcift.h:37
char * key
Definition tpcift.h:32
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
Definition tpcpar.h:101
int format
Definition tpcpar.h:103
IFT h
Optional (but often useful) header information.
Definition tpcpar.h:148
int parNr
Definition tpcpar.h:109
int tacNr
Definition tpcpar.h:105
PARR * r
Definition tpcpar.h:115
PARN * n
Definition tpcpar.h:113
int unit
Definition tpcpar.h:87
char name[MAX_PARNAME_LEN+1]
Definition tpcpar.h:83
double wss
Definition tpcpar.h:73
int fitNr
Definition tpcpar.h:59
char name[MAX_TACNAME_LEN+1]
Definition tpcpar.h:50
int dataNr
Definition tpcpar.h:63
double * cl2
Definition tpcpar.h:71
unsigned int model
Definition tpcpar.h:48
double * p
Definition tpcpar.h:65
double start
Definition tpcpar.h:52
double * sd
Definition tpcpar.h:67
double * cl1
Definition tpcpar.h:69
double end
Definition tpcpar.h:54
int verbose
Verbose level, used by statusPrint() etc.
Header file for library libtpcextensions.
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero)
@ UNIT_UNKNOWN
Unknown unit.
#define MAX_PARNAME_LEN
Max string length for PAR name.
@ TPCERROR_FAIL
General error.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_CANNOT_WRITE
Cannot write file.
int unitIdentify(const char *s)
Definition units.c:162
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpcift.
Header file for libtpcpar.
@ PAR_FORMAT_CSV_INT
International CSV.
Definition tpcpar.h:32
@ PAR_FORMAT_IFT
Interfile-type data (supported for writing)
Definition tpcpar.h:31
@ PAR_FORMAT_TSV_INT
International TSV (comma as decimal separator)
Definition tpcpar.h:34