TPCCLIB
Loading...
Searching...
No Matches
bpr2cpr.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17#include "tpctac.h"
18#include "tpcli.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Converts blood-to-plasma (or optionally plasma-to-blood) ratio curve to",
24 "RBC-to-plasma (blood cell-to-plasma) curve using haematocrit (HCT),",
25 "based on equation",
26 " ",
27 " Blood = HCT*RBC + (1-HCT)*Plasma ",
28 " ",
29 "HCT is normally between 0.40-0.51 in men and 0.36-0.47 in women.",
30 " ",
31 "Usage: @P [Options] bprfile HCT cprfile",
32 " ",
33 "Options:",
34 " -pbr",
35 " Conversion is applied to plasma-to-blood ratio data.",
36 " -stdoptions", // List standard options like --help, -v, etc
37 " ",
38 "See also: b2rbc, p2blood, b2plasma, taccalc, fit_bpr",
39 " ",
40 "Keywords: input, modelling, simulation, RBC, plasma, blood",
41 0};
42/*****************************************************************************/
43
44/*****************************************************************************/
45/* Turn on the globbing of the command line, since it is disabled by default in
46 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
47 In Unix&Linux wildcard command line processing is enabled by default. */
48/*
49#undef _CRT_glob
50#define _CRT_glob -1
51*/
52int _dowildcard = -1;
53/*****************************************************************************/
54
55/*****************************************************************************/
59int main(int argc, char **argv)
60{
61 int ai, help=0, version=0, verbose=1;
62 int ret;
63 char *cptr, bprfile[FILENAME_MAX], cprfile[FILENAME_MAX];
64 TAC tac;
65 double HCT=-1.0;
66 int inmode=0; // Input is 0=BPR, or 1=PBR
67
68
69 /*
70 * Get arguments
71 */
72 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
73 bprfile[0]=cprfile[0]=(char)0;
74 tacInit(&tac);
75 /* Options */
76 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
77 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
78 cptr=argv[ai]+1;
79 if(strcasecmp(cptr, "BPR")==0) {
80 inmode=0; continue;
81 } else if(strcasecmp(cptr, "PBR")==0) {
82 inmode=1; continue;
83 }
84 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
85 return(1);
86 } else break; // tac name argument may start with '-'
87
88 TPCSTATUS status; statusInit(&status);
89 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
90 status.verbose=verbose-1;
91
92 /* Print help or version? */
93 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
94 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
95 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
96
97 /* Arguments */
98 for(; ai<argc; ai++) {
99 if(!bprfile[0]) {
100 strlcpy(bprfile, argv[ai], FILENAME_MAX); continue;
101 } else if(HCT<0.0) {
102 if(!atofCheck(argv[ai], &HCT)) {
103 if(HCT>1.0) HCT/=100.0;
104 if(HCT>0.0 && HCT<=1.0) continue;
105 }
106 fprintf(stderr, "Error: invalid HCT.\n");
107 return(1);
108 } else if(!cprfile[0]) {
109 strlcpy(cprfile, argv[ai], FILENAME_MAX); continue;
110 }
111 fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]);
112 return(1);
113 }
114
115 /* Is something missing? */
116 if(!cprfile[0]) {
117 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
118 return(1);
119 }
120
121 /* In verbose mode print arguments and options */
122 if(verbose>1) {
123 for(ai=0; ai<argc; ai++)
124 printf("%s ", argv[ai]);
125 printf("\n");
126 printf("bprfile := %s\n", bprfile);
127 printf("cprfile := %s\n", cprfile);
128 printf("HCT := %g\n", HCT);
129 printf("inmode := %d\n", inmode);
130 }
131
132
133 /*
134 * Read blood-to-plasma ratio
135 */
136 if(verbose) printf("reading %s\n", bprfile);
137 ret=tacRead(&tac, bprfile, &status);
138 if(ret!=TPCERROR_OK) {
139 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
140 tacFree(&tac); return(2);
141 }
142 if(verbose>2) {
143 printf("fileformat := %s\n", tacFormattxt(tac.format));
144 printf("tacNr := %d\n", tac.tacNr);
145 printf("sampleNr := %d\n", tac.sampleNr);
146 printf("xunit := %s\n", unitName(tac.tunit));
147 printf("yunit := %s\n", unitName(tac.cunit));
148 }
149 if(tac.tacNr>1) {
150 fprintf(stderr, "Warning: only first ratio curve is used.\n");
151 tac.tacNr=1;
152 }
153
154 /* Sort by sample times */
155 tacSortByTime(&tac, NULL);
156
157
158 /* Calculate RBC-to-plasma curve */
159 for(int i=0; i<tac.sampleNr; i++) {
160 if(isnan(tac.c[0].y[i])) continue;
161 if(inmode==1) { // convert plasma/blood to blood/plasma
162 if(tac.c[0].y[i]<1.0E-100) tac.c[0].y[i]=0.0;
163 else tac.c[0].y[i]=1.0/tac.c[0].y[i];
164 }
165 tac.c[0].y[i]-=(1.0-HCT);
166 tac.c[0].y[i]/=HCT;
167 }
168
169 /* Save RBC-to-plasma ratio */
170 if(verbose>1) printf("writing %s\n", cprfile);
171 FILE *fp; fp=fopen(cprfile, "w");
172 if(fp==NULL) {
173 fprintf(stderr, "Error: cannot open file for writing (%s)\n", cprfile);
174 tacFree(&tac); return(11);
175 }
176 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
177 fclose(fp); tacFree(&tac);
178 if(ret!=TPCERROR_OK) {
179 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
180 return(12);
181 }
182 if(verbose>=0) printf("RBC-to-plasma curve written in %s\n", cprfile);
183
184 return(0);
185}
186/*****************************************************************************/
187
188/*****************************************************************************/
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
double * y
Definition tpctac.h:75
Definition tpctac.h:87
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
TACC * c
Definition tpctac.h:117
unit tunit
Definition tpctac.h:109
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
int tacSortByTime(TAC *d, TPCSTATUS *status)
Definition tacorder.c:74
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpcift.
Header file for libtpcli.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28