TPCCLIB
Loading...
Searching...
No Matches
pario.c File Reference

I/O functions for TPC parameter files. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpccsv.h"
#include "tpcift.h"
#include "tpcpar.h"

Go to the source code of this file.

Functions

char * parFormattxt (parformat c)
 
int parFormatIdentify (const char *s)
 
int parFormatFromExtension (const char *s)
 
char * parDefaultExtension (parformat c)
 
int parWrite (PAR *par, FILE *fp, parformat format, int extra, TPCSTATUS *status)
 
int parRead (PAR *par, const char *fname, TPCSTATUS *status)
 
void parListLimits (PAR *par, FILE *fp)
 
int parWriteLimits (PAR *par, const char *fname, const int verbose)
 
int parReadLimits (PAR *par, const char *fname, const int verbose)
 

Detailed Description

I/O functions for TPC parameter files.

Test
Test reading old .res and .fit files.

Definition in file pario.c.

Function Documentation

◆ parDefaultExtension()

char * parDefaultExtension ( parformat c)

Return pointer to default PAR file name extension, including the dot, based on the PAR format code.

Returns
pointer to the file name extension string.
See also
parformat, parFormatFromExtension, parFormattxt, parWrite
Author
Vesa Oikonen
Parameters
cPAR format code

Definition at line 133 of file pario.c.

136 {
137 if(c<PAR_FORMAT_UNKNOWN || c>=PAR_FORMAT_LAST) return NULL;
138 return (char*)par_fn_ext[c];
139}
@ PAR_FORMAT_LAST
End of list.
Definition tpcpar.h:38

◆ parFormatFromExtension()

int parFormatFromExtension ( const char * s)

Identify the PAR file format from the extension of file name.

Returns
enum parformat, or 0 (enum PAR_FORMAT_UNKNOWN) if not identified.
Author
Vesa Oikonen
See also
parFormattxt, parformat, parFormatIdentify, parWrite
Parameters
sPointer to file name with or without path; mere extension is not accepted here. For example /path/filename.res or filename.fit.bak are accepted.

Definition at line 102 of file pario.c.

106 {
107 if(s==NULL || strlen(s)<1) return PAR_FORMAT_UNKNOWN;
108 /* Get pointer to the extensions */
109 char *cptr=filenameGetExtensions(s);
110 if(cptr==NULL) return PAR_FORMAT_UNKNOWN;
111 /* Try to find some known extensions */
112 if(strcasestr(cptr, ".res")) return PAR_FORMAT_RES;
113 if(strcasestr(cptr, ".fit")) return PAR_FORMAT_FIT;
114 if(strcasestr(cptr, ".ift")) return PAR_FORMAT_IFT;
115 if(strcasestr(cptr, ".csv")) return PAR_FORMAT_CSV_UK;
116 if(strcasestr(cptr, ".tsv")) return PAR_FORMAT_TSV_UK;
117 if(strcasestr(cptr, ".txt")) return PAR_FORMAT_TSV_UK;
118 if(strcasestr(cptr, ".par")) return PAR_FORMAT_TSV_UK;
119 if(strcasestr(cptr, ".lim")) return PAR_FORMAT_TSV_UK;
120 if(strcasestr(cptr, ".dat")) return PAR_FORMAT_TSV_UK;
121 if(strcasestr(cptr, ".xml")) return PAR_FORMAT_XML;
122 if(strcasestr(cptr, ".htm")) return PAR_FORMAT_HTML;
123 return PAR_FORMAT_UNKNOWN;
124}
char * filenameGetExtensions(const char *s)
Get all extensions of a file name.
Definition filename.c:203
char * strcasestr(const char *haystack, const char *needle)
Definition stringext.c:155
@ PAR_FORMAT_XML
XML format (currently not supported)
Definition tpcpar.h:36
@ PAR_FORMAT_HTML
HTML table format (currently not supported)
Definition tpcpar.h:37
@ PAR_FORMAT_CSV_UK
UK CSV.
Definition tpcpar.h:33
@ PAR_FORMAT_IFT
Interfile-type data (supported for writing)
Definition tpcpar.h:31
@ PAR_FORMAT_RES
Model result format of Turku PET Centre.
Definition tpcpar.h:29
@ PAR_FORMAT_FIT
Function fit format of Turku PET Centre.
Definition tpcpar.h:30
@ PAR_FORMAT_UNKNOWN
Unknown format.
Definition tpcpar.h:28
@ PAR_FORMAT_TSV_UK
UK TSV (point as decimal separator)
Definition tpcpar.h:35

◆ parFormatIdentify()

int parFormatIdentify ( const char * s)

Identify the string representations of the PAR file format.

Returns
enum parformat, or 0 (enum PAR_FORMAT_UNKNOWN) if not identified.
Author
Vesa Oikonen
See also
parFormattxt, parformat, parFormatFromExtension, parRead
Parameters
sPAR format as a string.

Definition at line 74 of file pario.c.

77 {
78 if(s==NULL || strlen(s)<1) return PAR_FORMAT_UNKNOWN;
79 /* Try if string can be found directly in the table */
80 for(int i=0; i<PAR_FORMAT_LAST; i++) {
81 if(strcasecmp(par_format[i], s)==0) return i;
82 }
83 /* Format string is not following TPC standard, lets try something else */
84 if( strcasecmp(s, "dat")==0) return PAR_FORMAT_TSV_UK;
85 else if(strcasecmp(s, "txt")==0) return PAR_FORMAT_TSV_UK;
86 else if(strcasecmp(s, "par")==0) return PAR_FORMAT_CSV_UK;
87 else if(strcasecmp(s, "lim")==0) return PAR_FORMAT_TSV_UK;
88 else if(strcasecmp(s, "htm")==0) return PAR_FORMAT_HTML;
89 else if(strcasecmp(s, "csv")==0) return PAR_FORMAT_CSV_UK;
90 else if(strcasecmp(s, "tsv")==0) return PAR_FORMAT_TSV_UK;
91
92 return PAR_FORMAT_UNKNOWN;
93}

◆ parFormattxt()

char * parFormattxt ( parformat c)

Return pointer to PAR file format description with the format code.

Returns
pointer to the PAR file format string.
See also
parformat, parFormatIdentify, parFormatFromExtension
Author
Vesa Oikonen
Parameters
cPAR format code.

Definition at line 59 of file pario.c.

62 {
63 if(c<PAR_FORMAT_UNKNOWN || c>=PAR_FORMAT_LAST) return NULL;
64 return (char*)par_format[c];
65}

Referenced by parRead(), parWrite(), and parWriteCSV().

◆ parListLimits()

void parListLimits ( PAR * par,
FILE * fp )

List parameter limits in PAR structure.

See also
parWriteLimits, parWrite, parFree
Parameters
parPointer to PAR structure, from which the limits are printed.
fpFile pointer for the output; usually stdout.

Definition at line 406 of file pario.c.

411 {
412 //printf("%s()\n", __func__);
413 if(par==NULL || par->parNr<1) {fprintf(fp, "No parameter limits.\n"); return;}
414
415 fprintf(fp, "Parameter limits:\n");
416 for(int i=0; i<par->parNr; i++) {
417 if(strlen(par->n[i].name)>0) fprintf(fp, "%s", par->n[i].name);
418 else fprintf(fp, "p%d", 1+i);
419 fprintf(fp, "\t%g\t%g\n", par->n[i].lim1, par->n[i].lim2);
420 }
421 fprintf(fp, "\n");
422 return;
423}
int parNr
Definition tpcpar.h:109
PARN * n
Definition tpcpar.h:113
double lim2
Definition tpcpar.h:91
char name[MAX_PARNAME_LEN+1]
Definition tpcpar.h:83
double lim1
Definition tpcpar.h:89

◆ parRead()

int parRead ( PAR * par,
const char * fname,
TPCSTATUS * status )

Read parameter file contents into PAR data structure. This function reads RES and FIT format, and some CSV formats.

Author
Vesa Oikonen
Returns
code tpcerror, TPCERROR_OK (0) when successful.
See also
parWrite, parInit, parFree
Parameters
parPointer to initiated PAR struct where parameter data will be written; any old content is deleted. Call parInit() before first use.
fnamePointer to the file name; this string is not modified.
statusPointer to status data; enter NULL if not needed.

Definition at line 232 of file pario.c.

240 {
241 int verbose=0; if(status!=NULL) verbose=status->verbose;
242 if(fname==NULL || strlen(fname)<1 || par==NULL) {
243 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
244 return TPCERROR_FAIL;
245 }
246 if(verbose>1) {printf("%s(%s)\n", __func__, fname); fflush(stdout);}
247 int format=PAR_FORMAT_UNKNOWN;
248
249 /* Delete any previous data */
250 parFree(par);
251
252 /* Try to read the file as CSV file */
253 if(verbose>1) {printf("opening file\n"); fflush(stdout);}
254 FILE *fp;
255 fp=fopen(fname, "r");
256 if(fp==NULL) {
257 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_OPEN);
259 }
260 int ret;
261 CSV csv; csvInit(&csv);
262 ret=csvRead(&csv, fp, status);
263 fclose(fp);
264 if(ret!=TPCERROR_OK) {
265 statusSet(status, __func__, __FILE__, __LINE__, ret);
266 csvFree(&csv); return ret;
267 }
268 if(verbose>20) {
269 printf("\n --- CSV contents ---\n");
270 csvWrite(&csv, 0, stdout, status);
271 }
272
273 /* Try to identify file format from a magic number in the start */
274 if(verbose>1) {printf("checking for magic number\n"); fflush(stdout);}
275 if(strncasecmp(csv.c[0].content, "FIT1", 4)==0) {
276 format=PAR_FORMAT_FIT;
277 } else if(strncasecmp(csv.c[0].content, "<?xml version", 5)==0) {
278 format=PAR_FORMAT_XML;
279 } else if(strcasecmp(csv.c[0].content, "Parameters")==0) {
280 /* now determine the field and decimal separators */
281 if(csv.separator==';') format=PAR_FORMAT_CSV_INT;
282 else if(csv.separator==',') format=PAR_FORMAT_CSV_UK;
283 else if(csv.separator=='\t' || csv.separator==' ') {
284 // either UK or INT, we'll decide it later
285 format=PAR_FORMAT_TSV_UK;
286 }
287 }
288 if(verbose>3) {printf("format := %s\n", parFormattxt(format)); fflush(stdout);}
289
290 /* If identified already, then read data and return */
291 if(format!=PAR_FORMAT_UNKNOWN) {
292 if(verbose>1) {printf("format := %s\n", parFormattxt(format)); fflush(stdout);}
293 IFT hdr; iftInit(&hdr);
294 /* These formats may contain additional header information in comment
295 lines; read headers from those into TAC struct for further processing */
296 if(format==PAR_FORMAT_CSV_UK || format==PAR_FORMAT_CSV_INT ||
297 format==PAR_FORMAT_TSV_UK || format==PAR_FORMAT_TSV_INT
298 ) {
299 fp=fopen(fname, "r");
300 if(fp!=NULL) {
301 if(iftRead(&hdr, fp, 1, 2, status)!=TPCERROR_OK) iftFree(&hdr);
302 fclose(fp);
303 }
304 /* Header may not exist, therefore do not worry about errors here */
305 }
306 /* FIT (and RES) formats are easier to read partially from IFT struct */
307 if(format==PAR_FORMAT_FIT) {
308 fp=fopen(fname, "r");
309 if(fp==NULL) ret=TPCERROR_CANNOT_OPEN; // these formats must have header
310 else {
311 ret=iftRead(&hdr, fp, 0, 1, status);
312 fclose(fp);
313 }
314 if(ret!=TPCERROR_OK) {
315 csvFree(&csv); iftFree(&hdr);
316 statusSet(status, __func__, __FILE__, __LINE__, ret);
317 return ret;
318 }
319 }
320 switch(format) {
321 case PAR_FORMAT_FIT:
322 ret=parReadFIT(par, &csv, &hdr, status);
323 break;
325 /* __attribute__((fallthrough)); */
327 /* __attribute__((fallthrough)); */
329 /* __attribute__((fallthrough)); */
331 /* __attribute__((fallthrough)); */
332 default:
333 ret=parReadCSV(par, &csv, &hdr, status);
334 break;
335 }
336 csvFree(&csv); iftFree(&hdr);
337 if(ret!=TPCERROR_OK) {
338 statusSet(status, __func__, __FILE__, __LINE__, ret);
339 parFree(par); return ret;
340 }
341 /* Set study number, if not yet set, based on file name */
342 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
343 return TPCERROR_OK;
344 }
345
346 /* The rest of the formats are a bit more difficult to identify */
347
348 /* Try to read as IFT */
349 if(verbose>3) {printf("trying IFT format\n"); fflush(stdout);}
350 fp=fopen(fname, "r");
351 if(fp!=NULL) {
352 IFT hdr; iftInit(&hdr);
353 iftRead(&hdr, fp, 1, 1, status); fclose(fp);
354 if(hdr.keyNr>0) {
355 if(!strcasecmp(hdr.item[0].key, "content") && !strcasecmp(hdr.item[0].value, "parameters"))
356 format=PAR_FORMAT_IFT;
357 }
358 if(format==PAR_FORMAT_IFT) {
359 if(verbose>1) printf("format := %s\n", parFormattxt(format));
360 ret=parFromIFT(par, &hdr, status);
361 csvFree(&csv); iftFree(&hdr);
362 if(ret!=TPCERROR_OK) {parFree(par); return ret;}
363 /* Set study number, if not yet set, based on file name */
364 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
365 return TPCERROR_OK;
366 }
367 if(verbose>3) printf("not IFT\n");
368 iftFree(&hdr);
369 }
370
371 /* Try to read as RES file */
372 if(verbose>3) {printf("trying RES format\n"); fflush(stdout);}
373 /* Header is obligatory, read that to IFT */
374 fp=fopen(fname, "r");
375 if(fp!=NULL) {
376 IFT hdr; iftInit(&hdr);
377 ret=iftRead(&hdr, fp, 0, 1, status); fclose(fp);
378 if(ret!=TPCERROR_OK) {
379 csvFree(&csv); iftFree(&hdr);
380 statusSet(status, __func__, __FILE__, __LINE__, ret);
381 return ret;
382 }
383 ret=parReadRES(par, &csv, &hdr, status);
384 iftFree(&hdr);
385 if(ret==TPCERROR_OK) {
386 csvFree(&csv);
387 /* Set study number, if not yet set, based on file name */
388 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
389 return TPCERROR_OK;
390 }
391 }
392
393 /* Currently any other format cannot be read */
394 if(verbose>3) {printf("currently not supported format\n"); fflush(stdout);}
395 csvFree(&csv);
396 parFree(par);
397 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNSUPPORTED);
399}
void csvInit(CSV *csv)
Definition csv.c:22
void csvFree(CSV *csv)
Definition csv.c:38
int csvRead(CSV *csv, FILE *fp, TPCSTATUS *status)
Definition csvio.c:124
int csvWrite(CSV *csv, int regular, FILE *fp, TPCSTATUS *status)
Definition csvio.c:52
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
Definition iftio.c:130
void parFree(PAR *par)
Definition par.c:75
int parReadCSV(PAR *par, CSV *csv, IFT *hdr, TPCSTATUS *status)
Definition parcsv.c:221
int parReadFIT(PAR *par, CSV *csv, IFT *ift, TPCSTATUS *status)
Definition parfit.c:131
int parFromIFT(PAR *par, IFT *ift, TPCSTATUS *status)
Definition parift.c:207
char * parFormattxt(parformat c)
Definition pario.c:59
int parReadRES(PAR *par, CSV *csv, IFT *ift, TPCSTATUS *status)
Definition parres.c:268
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
char * content
Definition tpccsv.h:30
Definition tpccsv.h:36
char separator
Definition tpccsv.h:49
CSV_item * c
Definition tpccsv.h:38
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
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_FAIL
General error.
@ TPCERROR_CANNOT_OPEN
Cannot open file.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_OK
No error.
@ PAR_FORMAT_CSV_INT
International CSV.
Definition tpcpar.h:32
@ PAR_FORMAT_TSV_INT
International TSV (comma as decimal separator)
Definition tpcpar.h:34

◆ parReadLimits()

int parReadLimits ( PAR * par,
const char * fname,
const int verbose )

Read the specified file and try to find constraints and tolerance for parameters listed in PAR structure.

Returns
enum tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
See also
parWriteLimits, parWrite, parFree
Parameters
parPointer to PAR structure, into which the limits are saved. This must also list the parameter names that are searched for from the file.
fnameInput file name.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 489 of file pario.c.

497 {
498 if(verbose>0) {printf("%s(%s)\n", __func__, fname); fflush(stdout);}
499 if(par==NULL || par->parNr<1) return TPCERROR_NO_DATA;
500 if(strlen(fname)<1) return(TPCERROR_INVALID_FILENAME);
501
502 /* Open file */
503 if(verbose>1) printf(" opening %s\n", fname);
504 FILE *fp=fopen(fname, "r");
505 if(fp==NULL) return TPCERROR_CANNOT_READ;
506
507 /* Read file into IFT struct */
508 if(verbose>1) printf(" reading into IFT\n");
509 IFT ift; iftInit(&ift);
510 if(iftRead(&ift, fp, 1, 0, NULL)!=TPCERROR_OK) {fclose(fp); return TPCERROR_CANNOT_READ;}
511 fclose(fp);
512 if(verbose>1) printf(" keyNr=%d\n", ift.keyNr);
513
514 /* Search for parameter limits in the IFT */
515 char pname[MAX_PARNAME_LEN+10];
516 int n=0;
517 double f;
518 for(int i=0; i<par->parNr; i++) {
519 if(strlen(par->n[i].name)>0) strcpy(pname, par->n[i].name);
520 else sprintf(pname, "p%d", 1+i);
521 strcat(pname, "_lower");
522 if(iftGetDoubleValue(&ift, pname, 0, &f)>=0 && !isnan(f)) {par->n[i].lim1=f; n++;}
523 if(verbose>2) printf(" %s -> %g\n", pname, f);
524 pname[strlen(pname)-6]=(char)0; strcat(pname, "_upper");
525 if(iftGetDoubleValue(&ift, pname, 0, &f)>=0 && !isnan(f)) {par->n[i].lim2=f; n++;}
526 if(verbose>2) printf(" %s -> %g\n", pname, f);
527 pname[strlen(pname)-6]=(char)0; strcat(pname, "_tol");
528 if(iftGetDoubleValue(&ift, pname, 0, &f)>=0 && !isnan(f)) {par->n[i].tol=f; n++;}
529 if(verbose>2) printf(" %s -> %g\n", pname, f);
530 }
531 iftFree(&ift);
532 if(verbose>1) printf(" n=%d\n", n);
533 if(n==0) return TPCERROR_NO_DATA;
534
535 return TPCERROR_OK;
536}
int iftGetDoubleValue(IFT *ift, const char *key, int index, double *v)
Definition iftfind.c:191
double tol
Definition tpcpar.h:93
#define MAX_PARNAME_LEN
Max string length for PAR name.
@ TPCERROR_INVALID_FILENAME
Invalid file name.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_CANNOT_READ
Cannot read file.

◆ parWrite()

int parWrite ( PAR * par,
FILE * fp,
parformat format,
int extra,
TPCSTATUS * status )

Write PAR data into specified file in specified format.

Returns
enum tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
See also
parRead, parFree, parAllocate, parFormatFromExtension
Parameters
parPointer to PAR struct, contents of which are to be written.
fpOutput file pointer.
formatFile format code; enter PAR_FORMAT_UNKNOWN (0) to write data in the format specified inside the PAR struct.
extraWrite (1) or do not write (0) also extra header fields found in PAR; only effective with CSV formats.
statusPointer to status data; enter NULL if not needed.

Definition at line 148 of file pario.c.

161 {
162 int verbose=0; if(status!=NULL) verbose=status->verbose;
163 if(fp==NULL) {
164 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_WRITE);
166 }
167 if(par==NULL || par->tacNr<1 || par->parNr<1) {
168 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
169 return TPCERROR_NO_DATA;
170 }
171 if(par->tacNr>par->_tacNr || par->parNr>par->_parNr) { // programmers fail
172 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
173 return TPCERROR_FAIL;
174 }
175
176 /* Determine and verify the write format */
177 if(format<=PAR_FORMAT_UNKNOWN || format>=PAR_FORMAT_LAST)
178 format=par->format;
179 if(format<=PAR_FORMAT_UNKNOWN || format>=PAR_FORMAT_LAST) {
180 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
182 }
183 if(verbose>0) {
184 printf("%s():\n", __func__);
185 printf("writing %d parameters from %d TACs in %s format\n",
186 par->parNr, par->tacNr, parFormattxt(format));
187 fflush(stdout);
188 }
189
190 /* Write file */
191 int ret;
192 switch(format) {
194 /* __attribute__((fallthrough)); */
196 /* __attribute__((fallthrough)); */
198 /* __attribute__((fallthrough)); */
200 ret=parWriteCSV(par, fp, format, extra, status);
201 break;
202 case PAR_FORMAT_RES:
203 ret=parWriteRES(par, fp, status);
204 break;
205 case PAR_FORMAT_FIT:
206 ret=parWriteFIT(par, fp, status);
207 break;
208 case PAR_FORMAT_IFT:
209 ret=parWriteIFT(par, fp, status);
210 break;
211 case PAR_FORMAT_XML:
212 ret=parWriteXML(par, fp, status);
213 break;
214 default:
216 }
217 fflush(fp); fflush(stderr);
218
219 /* Quit */
220 statusSet(status, __func__, __FILE__, __LINE__, ret);
221 return(ret);
222}
int parWriteCSV(PAR *par, FILE *fp, parformat format, int extra, TPCSTATUS *status)
Definition parcsv.c:26
int parWriteFIT(PAR *par, FILE *fp, TPCSTATUS *status)
Definition parfit.c:27
int parWriteIFT(PAR *par, FILE *fp, TPCSTATUS *status)
Definition parift.c:167
int parWriteRES(PAR *par, FILE *fp, TPCSTATUS *status)
Definition parres.c:26
int parWriteXML(PAR *par, FILE *fp, TPCSTATUS *status)
Definition parxmlio.c:26
int format
Definition tpcpar.h:103
int _parNr
Definition tpcpar.h:111
int tacNr
Definition tpcpar.h:105
int _tacNr
Definition tpcpar.h:107
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_CANNOT_WRITE
Cannot write file.

◆ parWriteLimits()

int parWriteLimits ( PAR * par,
const char * fname,
const int verbose )

Write parameter constraints from PAR structure into specified file.

Returns
enum tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
See also
parReadLimits, parWrite, parFree, parListLimits
Parameters
parPointer to PAR structure, from which the limits are to be written.
fnameOutput file name. Enter 'stdout' to print to stdout.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 432 of file pario.c.

439 {
440 if(verbose>0) {printf("%s(%s)\n", __func__, fname); fflush(stdout);}
441 if(par==NULL || par->parNr<1) return TPCERROR_NO_DATA;
442 if(strlen(fname)<1) return(TPCERROR_INVALID_FILENAME);
443
444 /* Copy limits into IFT struct */
445 IFT ift; iftInit(&ift);
446 char pname[MAX_PARNAME_LEN+10];
447 for(int i=0; i<par->parNr; i++) {
448 if(!isnan(par->n[i].lim1)) {
449 if(strlen(par->n[i].name)>0) strcpy(pname, par->n[i].name); else sprintf(pname, "p%d", 1+i);
450 strcat(pname, "_lower"); iftPutDouble(&ift, pname, par->n[i].lim1, 0, NULL);
451 }
452 if(!isnan(par->n[i].lim2)) {
453 if(strlen(par->n[i].name)>0) strcpy(pname, par->n[i].name); else sprintf(pname, "p%d", 1+i);
454 strcat(pname, "_upper"); iftPutDouble(&ift, pname, par->n[i].lim2, 0, NULL);
455 }
456 if(!isnan(par->n[i].tol)) {
457 if(strlen(par->n[i].name)>0) strcpy(pname, par->n[i].name); else sprintf(pname, "p%d", 1+i);
458 strcat(pname, "_tol"); iftPutDouble(&ift, pname, par->n[i].tol, 0, NULL);
459 }
460 }
461 if(verbose>1) printf(" keyNr=%d\n", ift.keyNr);
462 if(ift.keyNr<1) {iftFree(&ift); return TPCERROR_NO_DATA;}
463
464 /* Write IFT */
465 if(strcasecmp(fname, "stdout")==0) { // into stdout
466 iftWrite(&ift, stdout, NULL);
467 } else { // into specified file
468 if(verbose>1) printf(" opening %s\n", fname);
469 FILE *fp=fopen(fname, "w");
470 if(fp==NULL) {iftFree(&ift); return TPCERROR_CANNOT_WRITE;}
471 if(verbose>1) printf(" writing in %s\n", fname);
472 if(iftWrite(&ift, fp, NULL)!=TPCERROR_OK) {
473 iftFree(&ift); fclose(fp); return TPCERROR_CANNOT_WRITE;
474 }
475 fclose(fp);
476 }
477 iftFree(&ift);
478
479 return TPCERROR_OK;
480}
int iftPutDouble(IFT *ift, const char *key, const double value, char comment, TPCSTATUS *status)
Definition ift.c:128
int iftWrite(IFT *ift, FILE *fp, TPCSTATUS *status)
Definition iftio.c:98