TPCCLIB
Loading...
Searching...
No Matches
hist2svg.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 "tpctacmod.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Plot histogram from data given in TAC format.",
24 " ",
25 "Usage: @P [options] datafile svgfile",
26 " ",
27 "Options:",
28 " -c=<Number>",
29 " The dataset in file to use, in case there are several; by default 1.",
30 " -x1=<start of x axis>",
31 " -x2=<end of x axis>",
32 " -y1=<start of y axis>",
33 " -y2=<end of y axis>",
34 " -mt=\"<main title>\"",
35 " User-specified string to set as main title.",
36 " -stdoptions", // List standard options like --help, -v, etc
37 " ",
38 "See also: tac2svg, imghist",
39 " ",
40 "Keywords: TAC, tool",
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 char tacfile[FILENAME_MAX], svgfile[FILENAME_MAX];
63 char main_title[64];
64 double preset_xmin, preset_xmax, preset_ymin, preset_ymax;
65 int ci=0;
66
67
68 /*
69 * Get arguments
70 */
71 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
72 tacfile[0]=svgfile[0]=(char)0;
73 main_title[0]=(char)0;
74 preset_xmin=preset_xmax=preset_ymin=preset_ymax=nan("");
75 /* Options */
76 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
77 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
78 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
79 if(strncasecmp(cptr, "MT=", 3)==0) {
80 if(strncpyClean(main_title, cptr+3, 64)) continue;
81 } else if(strncasecmp(cptr, "X1=", 3)==0) {
82 if(atofCheck(cptr+3, &preset_xmin)==0) continue;
83 } else if(strncasecmp(cptr, "X2=", 3)==0) {
84 if(atofCheck(cptr+3, &preset_xmax)==0) continue;
85 } else if(strncasecmp(cptr, "Y1=", 3)==0) {
86 if(atofCheck(cptr+3, &preset_ymin)==0) continue;
87 } else if(strncasecmp(cptr, "Y2=", 3)==0) {
88 if(atofCheck(cptr+3, &preset_ymax)==0) continue;
89 } else if(strncasecmp(cptr, "C=", 2)==0) {
90 if(atoiCheck(cptr+2, &ci)==0 && ci>0) {ci--; continue;}
91 }
92 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
93 return(1);
94 } else break; // tac name argument may start with '-'
95
96 TPCSTATUS status; statusInit(&status);
97 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
98 status.verbose=verbose-3;
99
100 /* Print help or version? */
101 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
102 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
103 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
104
105 /* The first argument (non-option) is the filename */
106 if(ai<argc) {strlcpy(tacfile, argv[ai], FILENAME_MAX); ai++;}
107 if(ai<argc) {strlcpy(svgfile, argv[ai], FILENAME_MAX); ai++;}
108 else {fprintf(stderr, "Error: missing filename.\n"); return(1);}
109 if(ai<argc) {fprintf(stderr, "Error: extra command-line argument.\n"); return(1);}
110
111 if(preset_xmin>=preset_xmax || preset_ymin>=preset_ymax) {
112 fprintf(stderr, "Error: invalid axis range settings.\n"); return(1);}
113
114 /* In verbose mode print arguments and options */
115 if(verbose>1) {
116 printf("tacfile := %s\n", tacfile);
117 printf("svgfile := %s\n", svgfile);
118 if(main_title[0]) printf("main_title := %s\n", main_title);
119 if(isfinite(preset_xmin)) printf("preset_xmin := %g\n", preset_xmin);
120 if(isfinite(preset_xmax)) printf("preset_xmax := %g\n", preset_xmax);
121 if(isfinite(preset_ymin)) printf("preset_ymin := %g\n", preset_ymin);
122 if(isfinite(preset_ymax)) printf("preset_ymax := %g\n", preset_ymax);
123 }
124
125 /* Read the file */
126 if(verbose>1) printf("reading %s\n", tacfile);
127 TAC tac; tacInit(&tac);
128 tacRead(&tac, tacfile, &status);
129 if(status.error!=TPCERROR_OK) {
130 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
131 tacFree(&tac); return(2);
132 }
133 if(verbose>2) {
134 printf("fileformat := %s\n", tacFormattxt(tac.format));
135 printf("tacNr := %d\n", tac.tacNr);
136 printf("sampleNr := %d\n", tac.sampleNr);
137 printf("frames := %d\n", tac.isframe);
138 if(tac.tacNr>1) printf("tac := %s\n", tac.c[ci].name);
139 }
140 if(tac.isframe) tacSetX(&tac, NULL);
141 if(ci>=tac.tacNr) {
142 fprintf(stderr, "Error: data file does not contain data set %d.\n", 1+ci);
143 tacFree(&tac); return(2);
144 }
145 /* Delete other datasets than the selected one */
146 if(tac.tacNr>1) {
147 for(int i=0; i<tac.tacNr; i++) if(i==ci) tac.c[i].sw=0; else tac.c[i].sw=1;
148 int i=tac.tacNr-1;
149 while(i>=0) {
150 if(tac.c[i].sw && tacDeleteTACC(&tac, i)!=TPCERROR_OK) {
151 fprintf(stderr, "Error: invalid data file.\n");
152 tacFree(&tac); return(2);
153 }
154 i--;
155 }
156 }
157
158 /* Determine the plot min and max values */
159 double xmin=nan(""), xmax=nan(""), ymin=nan(""), ymax=nan("");
160 if(tacXRange(&tac, &xmin, &xmax)) {
161 fprintf(stderr, "Error: invalid data x range.\n");
162 tacFree(&tac); return(2);
163 }
164 if(verbose>3) printf("data_xrange := %g %g\n", xmin, xmax);
165 if((isfinite(preset_xmin) && preset_xmin>=xmax) || (isfinite(preset_xmax) && preset_xmax<=xmin)) {
166 fprintf(stderr, "Error: invalid x axis range settings.\n");
167 tacFree(&tac); return(1);
168 }
169 if(isfinite(preset_xmin)) xmin=preset_xmin;
170 if(isfinite(preset_xmax)) xmax=preset_xmax;
171 /* Determine the plot min and max y values inside the x range */
172 if(tacYRangeInXRange(&tac, 0, xmin, xmax, &ymin, &ymax, NULL, NULL, NULL, NULL)) {
173 fprintf(stderr, "Error: invalid data y range.\n");
174 tacFree(&tac); return(2);
175 }
176 if(verbose>3) printf("data_yrange := %g %g\n", ymin, ymax);
177 if((isfinite(preset_ymin) && preset_ymin>=ymax) || (isfinite(preset_ymax) && preset_ymax<=ymin)) {
178 fprintf(stderr, "Error: invalid y axis range settings.\n");
179 tacFree(&tac); return(1);
180 }
181 if(ymin>0.0) ymin=0.0;
182 else if(ymax<0.0) ymax=0.0;
183 if(isfinite(preset_ymin)) ymin=preset_ymin;
184 if(isfinite(preset_ymax)) ymax=preset_ymax;
185 if(verbose>3) {
186 printf("plotted_xrange := %g %g\n", xmin, xmax);
187 printf("plotted_yrange := %g %g\n", ymin, ymax);
188 }
189
190
191 /* Plot */
192 tacPlotHistogramSVG(&tac, main_title, xmin, xmax, ymin, ymax, svgfile, &status);
193 if(status.error!=TPCERROR_OK) {
194 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
195 tacFree(&tac); return(10);
196 }
197
198
199 tacFree(&tac);
200 return(0);
201}
202/*****************************************************************************/
203
204/*****************************************************************************/
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
int tacPlotHistogramSVG(TAC *d, const char *main_title, const double x1, const double x2, const double y1, const double y2, const char *fname, TPCSTATUS *status)
Definition histplot.c:30
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
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
int strncpyClean(char *s1, const char *s2, int maxlen)
Definition stringext.c:321
char sw
Definition tpctac.h:77
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
Definition tpctac.h:87
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
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 tacDeleteTACC(TAC *d, int i)
Definition tacorder.c:310
int tacSetX(TAC *d, TPCSTATUS *status)
Set TAC x values based on x1 and x2 values, or guess x1 and x2 values based on x values.
Definition tacx.c:653
int tacXRange(TAC *d, double *xmin, double *xmax)
Get the range of x values (times) in TAC structure.
Definition tacx.c:124
int tacYRangeInXRange(TAC *d, int i, const double xmin, const double xmax, double *ymin, double *ymax, int *smin, int *smax, int *imin, int *imax)
Get the range of y values (concentrations) in TAC struct, including only samples with x (times) insid...
Definition tacy.c:99
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpctac.
Header file for libtpctacmod.