TPCCLIB
Loading...
Searching...
No Matches
tacsetx.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 "tpctac.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Adds specified TAC from file2 as x (time) column to file1, or if not given,",
22 "then copy sample times from file2.",
23 "Files must have the same number of samples (time frames).",
24 "If file1 contains only one data column, that is assumed to represent y",
25 "values, and x values are added as new column to the left.",
26 " ",
27 "Usage: @P [Options] tacfile1 tacfile2 [tacid]",
28 " ",
29 "Options:",
30 " -stdoptions", // List standard options like --help, -v, etc
31 " ",
32 "See also: tacmultx, tacsety, taclist, tacadd0, tacframe, addtimes, tacformat",
33 " ",
34 "Keywords: TAC, tool, time, simulation",
35 0};
36/*****************************************************************************/
37
38/*****************************************************************************/
39/* Turn on the globbing of the command line, since it is disabled by default in
40 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
41 In Unix&Linux wildcard command line processing is enabled by default. */
42/*
43#undef _CRT_glob
44#define _CRT_glob -1
45*/
46int _dowildcard = -1;
47/*****************************************************************************/
48
49/*****************************************************************************/
53int main(int argc, char **argv)
54{
55 int ai, help=0, version=0, verbose=1;
56 int ret, voi=-1;
57 char tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX], rname[MAX_TACNAME_LEN];
58 TAC tac1, tac2;
59
60 /*
61 * Get arguments
62 */
63 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
64 tacInit(&tac1); tacInit(&tac2);
65 tacfile1[0]=tacfile2[0]=rname[0]=(char)0;
66 /* Options */
67 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
68 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
69 // char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
70 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
71 return(1);
72 } else break; // tac name argument may start with '-'
73
74 TPCSTATUS status; statusInit(&status);
75 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
76 status.verbose=verbose-1;
77
78 /* Print help or version? */
79 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
80 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
81 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
82
83 /* Arguments */
84 if(ai<argc) {strlcpy(tacfile1, argv[ai++], FILENAME_MAX);}
85 if(ai<argc) {strlcpy(tacfile2, argv[ai++], FILENAME_MAX);}
86 for(int n=0; ai<argc; ai++, n++) { // collect TAC id optionally from multiple parts
87 if(!roinameAddField(rname, argv[ai], n, MAX_TACNAME_LEN)) {
88 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
89 return(1);
90 }
91 }
92
93 /* Is something missing? */
94 if(!tacfile2[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
95
96 /* In verbose mode print arguments and options */
97 if(verbose>1) {
98 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
99 printf("\n");
100 printf("tacfile1 := %s\n", tacfile1);
101 printf("tacfile2 := %s\n", tacfile2);
102 if(rname[0]) printf("rname := %s\n", rname);
103 fflush(stdout);
104 }
105
106
107 /*
108 * Read the files
109 */
110 if(verbose>1) printf("reading %s\n", tacfile1);
111 ret=tacRead(&tac1, tacfile1, &status);
112 if(ret!=TPCERROR_OK) {
113 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
114 tacFree(&tac1); return(2);
115 }
116 if(verbose>2) {
117 printf("fileformat1 := %s\n", tacFormattxt(tac1.format));
118 printf("tacNr1 := %d\n", tac1.tacNr);
119 printf("sampleNr1 := %d\n", tac1.sampleNr);
120 printf("xunit1 := %s\n", unitName(tac1.tunit));
121 printf("yunit1 := %s\n", unitName(tac1.cunit));
122 printf("isframe1 := %d\n", tac1.isframe);
123 fflush(stdout);
124 }
125 if(verbose>1) printf("reading %s\n", tacfile2);
126 ret=tacRead(&tac2, tacfile2, &status);
127 if(ret!=TPCERROR_OK) {
128 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
129 tacFree(&tac1); tacFree(&tac2); return(3);
130 }
131 if(verbose>2) {
132 printf("fileformat2 := %s\n", tacFormattxt(tac2.format));
133 printf("tacNr2 := %d\n", tac2.tacNr);
134 printf("sampleNr2 := %d\n", tac2.sampleNr);
135 printf("xunit2 := %s\n", unitName(tac2.tunit));
136 printf("yunit2 := %s\n", unitName(tac2.cunit));
137 printf("isframe2 := %d\n", tac2.isframe);
138 fflush(stdout);
139 }
140 /* Check if file actually contained only y values and no x values at all */
141 if(tac1.tacNr<=1 && tac1.isframe==0 && tacYNaNs(&tac1, 0)==tac1.sampleNr) {
142 if(verbose>0) {printf("Note: initially no x column.\n"); fflush(stdout);}
143 /* Move values from x column to the first y column */
144 for(int i=0; i<tac1.sampleNr; i++) {
145 tac1.c[0].y[i]=tac1.x[i];
146 tac1.x[i]=nan("");
147 }
148 }
149
150 /* Check sample number */
151 if(tac1.sampleNr!=tac2.sampleNr) {
152 fprintf(stderr, "Error: datafiles have different sample nr.\n");
153 tacFree(&tac1); tacFree(&tac2); return(4);
154 }
155
156 if(rname[0]) {
157 /* Select the region to be copied as x */
158 if(verbose>1) printf("selecting TAC to be copied as x.\n");
159 int n=tacSelectTACs(&tac2, rname, 0, &status);
160 if(n<=0) {
161 fprintf(stderr, "Error: no match found for '%s'.\n", rname);
162 tacFree(&tac1); tacFree(&tac2); return(5);
163 }
164 if(n>1) {
165 fprintf(stderr, "Error: more than one TAC matches '%s'.\n", rname);
166 tacFree(&tac1); tacFree(&tac2); return(5);
167 }
168 for(int i=0; i<tac2.tacNr; i++) if(tac2.c[i].sw) {voi=i; break;}
169 }
170
171
172 if(rname[0]) {
173 /* Add specified TAC as x */
174 if(verbose>1) printf("adding region '%s' as x\n", tac1.c[voi].name);
175 for(int i=0; i<tac1.sampleNr; i++)
176 tac1.x[i]=tac1.x1[i]=tac1.x2[i]=tac2.c[voi].y[i];
177 /* Make sure that times (x) are specified as single column only */
178 tac1.isframe=0;
179 } else {
180 /* Copy x from the other file */
181 if(verbose>1) printf("adding x from '%s'\n", tacfile2);
182 for(int i=0; i<tac1.sampleNr; i++) {
183 tac1.x[i]=tac2.x[i]; tac1.x1[i]=tac2.x1[i]; tac1.x2[i]=tac2.x2[i];
184 }
185 tac1.isframe=tac2.isframe;
186 if(unitIsTime(tac2.tunit)) tac1.tunit=tac2.tunit;
187 }
188 tacFree(&tac2);
189
190
191 /*
192 * Save data
193 */
194 if(verbose>2) printf("writing %s\n", tacfile1);
195 FILE *fp; fp=fopen(tacfile1, "w");
196 if(fp==NULL) {
197 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile1);
198 tacFree(&tac1); return(11);
199 }
200 ret=tacWrite(&tac1, fp, TAC_FORMAT_UNKNOWN, 1, &status);
201 fclose(fp); tacFree(&tac1);
202 if(ret!=TPCERROR_OK) {
203 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
204 return(12);
205 }
206
207 return(0);
208}
209/*****************************************************************************/
210
211/*****************************************************************************/
213
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
char * roinameAddField(char *roiname, const char *field, const unsigned int in, const unsigned int count)
Definition roiname.c:106
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
char sw
Definition tpctac.h:77
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
unit tunit
Definition tpctac.h:109
double * x1
Definition tpctac.h:99
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 tacYNaNs(TAC *tac, const int i)
Definition tacnan.c:47
int tacSelectTACs(TAC *d, const char *region_name, int reset, TPCSTATUS *status)
Definition tacselect.c:24
Header file for library libtpcextensions.
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero).
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
int unitIsTime(int u)
Definition units.c:359
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28