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 "Copy sample times from file2 into file1, or, copy a specified TAC column",
22 "from file2 as sample times into file1.",
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 " -x2y",
31 " Copy sample time column(s) from file2 into file1 as TAC column(s).",
32 " -stdoptions", // List standard options like --help, -v, etc
33 " ",
34 "See also: tacmultx, tacsety, taclist, tacadd0, tacframe, addtimes, tacformat",
35 " ",
36 "Keywords: TAC, tool, time, simulation",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
55int main(int argc, char **argv)
56{
57 int ai, help=0, version=0, verbose=1;
58 int ret;
59 char tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX], rname[MAX_TACNAME_LEN];
60 int mode=0; // 0 = x-to-x, 1 = y-to-x, 2 = x-to-y
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 tacfile1[0]=tacfile2[0]=rname[0]=(char)0;
67 /* Options */
68 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
69 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
70 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
71 if(strcasecmp(cptr, "X2Y")==0) {
72 mode=2; continue;
73 }
74 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
75 return(1);
76 } else break; // tac name argument may start with '-'
77
78 TPCSTATUS status; statusInit(&status);
79 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
80 status.verbose=verbose-1;
81
82 /* Print help or version? */
83 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
84 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
85 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
86
87 /* Arguments */
88 if(ai<argc) {strlcpy(tacfile1, argv[ai++], FILENAME_MAX);}
89 if(ai<argc) {strlcpy(tacfile2, argv[ai++], FILENAME_MAX);}
90 for(int n=0; ai<argc; ai++, n++) { // collect TAC id optionally from multiple parts
91 if(!roinameAddField(rname, argv[ai], n, MAX_TACNAME_LEN)) {
92 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
93 return(1);
94 }
95 }
96 if(rname[0]) mode=1;
97
98 /* Is something missing? */
99 if(!tacfile2[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
100
101 /* In verbose mode print arguments and options */
102 if(verbose>1) {
103 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
104 printf("\n");
105 printf("tacfile1 := %s\n", tacfile1);
106 printf("tacfile2 := %s\n", tacfile2);
107 printf("mode := %d\n", mode);
108 if(rname[0]) printf("rname := %s\n", rname);
109 fflush(stdout);
110 }
111
112
113 /*
114 * Read the files
115 */
116 TAC tac1, tac2;
117 tacInit(&tac1); tacInit(&tac2);
118
119 if(verbose>1) printf("reading %s\n", tacfile1);
120 ret=tacRead(&tac1, tacfile1, &status);
121 if(ret!=TPCERROR_OK) {
122 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
123 tacFree(&tac1); return(2);
124 }
125 if(verbose>2) {
126 printf("fileformat1 := %s\n", tacFormattxt(tac1.format));
127 printf("tacNr1 := %d\n", tac1.tacNr);
128 printf("sampleNr1 := %d\n", tac1.sampleNr);
129 printf("xunit1 := %s\n", unitName(tac1.tunit));
130 printf("yunit1 := %s\n", unitName(tac1.cunit));
131 printf("isframe1 := %d\n", tac1.isframe);
132 fflush(stdout);
133 }
134
135 if(verbose>1) printf("reading %s\n", tacfile2);
136 ret=tacRead(&tac2, tacfile2, &status);
137 if(ret!=TPCERROR_OK) {
138 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
139 tacFree(&tac1); tacFree(&tac2); return(3);
140 }
141 if(verbose>2) {
142 printf("fileformat2 := %s\n", tacFormattxt(tac2.format));
143 printf("tacNr2 := %d\n", tac2.tacNr);
144 printf("sampleNr2 := %d\n", tac2.sampleNr);
145 printf("xunit2 := %s\n", unitName(tac2.tunit));
146 printf("yunit2 := %s\n", unitName(tac2.cunit));
147 printf("isframe2 := %d\n", tac2.isframe);
148 fflush(stdout);
149 }
150
151 /* Check sample number */
152 if(tac1.sampleNr!=tac2.sampleNr) {
153 fprintf(stderr, "Error: datafiles have different sample number.\n");
154 tacFree(&tac1); tacFree(&tac2); return(4);
155 }
156
157 /* Check if data consists of only single column, which is originally read in x[], and
158 one y column filled with NaNs */
159 if(tac1.tacNr<=1 && tac1.isframe==0 && tacYNaNs(&tac1, 0)==tac1.sampleNr) {
160 if(mode==0 || mode==1) {
161 /* In modes x-to-x and y-to-x, we assume that it is actually y column and the user wishes to
162 add x column in it. Thus, we will move values from x column to the first y column. */
163 if(verbose>0) {printf("Note: assuming initially no x column.\n"); fflush(stdout);}
164 for(int i=0; i<tac1.sampleNr; i++) {
165 tac1.c[0].y[i]=tac1.x[i];
166 tac1.x[i]=nan("");
167 }
168 } else {
169 /* In mode x-to-y, we will set tacNr to 0. */
170 tac1.tacNr=0;
171 }
172 }
173
174 /* Do the thing */
175 if(mode==0) {
176 /* Copy x from the other file */
177 if(verbose>1) printf("adding x from '%s'\n", tacfile2);
178 for(int i=0; i<tac1.sampleNr; i++) {
179 tac1.x[i]=tac2.x[i]; tac1.x1[i]=tac2.x1[i]; tac1.x2[i]=tac2.x2[i];
180 }
181 tac1.isframe=tac2.isframe;
182 if(unitIsTime(tac2.tunit)) tac1.tunit=tac2.tunit;
183 } else if(mode==1) {
184 /* Select the region to be copied as x */
185 if(verbose>1) printf("selecting TAC to be copied as x.\n");
186 int voi=-1;
187 int n=tacSelectTACs(&tac2, rname, 0, &status);
188 if(n<=0) {
189 fprintf(stderr, "Error: no match found for '%s'.\n", rname);
190 tacFree(&tac1); tacFree(&tac2); return(5);
191 }
192 if(n>1) {
193 fprintf(stderr, "Error: more than one TAC matches '%s'.\n", rname);
194 tacFree(&tac1); tacFree(&tac2); return(5);
195 }
196 for(int i=0; i<tac2.tacNr; i++) if(tac2.c[i].sw) {voi=i; break;}
197 /* Add specified TAC as x */
198 if(verbose>1) printf("adding region '%s' as x\n", tac1.c[voi].name);
199 for(int i=0; i<tac1.sampleNr; i++)
200 tac1.x[i]=tac1.x1[i]=tac1.x2[i]=tac2.c[voi].y[i];
201 /* Make sure that times (x) are specified as single column only */
202 tac1.isframe=0;
203 } else {
204 /* Copy x column(s) from file2 into file1 as y column(s) */
205 if(tac2.isframe) {
206 if(tacAllocateMore(&tac1, 2)!=TPCERROR_OK) {
207 fprintf(stderr, "Error: cannot allocate memory.\n");
208 tacFree(&tac1); tacFree(&tac2); return(6);
209 }
210 strcpy(tac1.c[tac1.tacNr].name, "X1");
211 for(int i=0; i<tac1.sampleNr; i++) tac1.c[tac1.tacNr].y[i]=tac2.x1[i];
212 tac1.tacNr++;
213 strcpy(tac1.c[tac1.tacNr].name, "X2");
214 for(int i=0; i<tac1.sampleNr; i++) tac1.c[tac1.tacNr].y[i]=tac2.x2[i];
215 tac1.tacNr++;
216 } else {
217 if(tacAllocateMore(&tac1, 1)!=TPCERROR_OK) {
218 fprintf(stderr, "Error: cannot allocate memory.\n");
219 tacFree(&tac1); tacFree(&tac2); return(6);
220 }
221 strcpy(tac1.c[tac1.tacNr].name, "X");
222 for(int i=0; i<tac1.sampleNr; i++) tac1.c[tac1.tacNr].y[i]=tac2.x[i];
223 tac1.tacNr++;
224 }
225 }
226 tacFree(&tac2);
227
228
229 /*
230 * Save data
231 */
232 if(verbose>2) printf("writing %s\n", tacfile1);
233 FILE *fp; fp=fopen(tacfile1, "w");
234 if(fp==NULL) {
235 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile1);
236 tacFree(&tac1); return(11);
237 }
238 ret=tacWrite(&tac1, fp, TAC_FORMAT_UNKNOWN, 1, &status);
239 fclose(fp); tacFree(&tac1);
240 if(ret!=TPCERROR_OK) {
241 fprintf(stderr, "Error: %s.\n", errorMsg(status.error));
242 return(12);
243 }
244
245 return(0);
246}
247/*****************************************************************************/
248
249/*****************************************************************************/
251
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:630
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 tacAllocateMore(TAC *tac, int tacNr)
Definition tac.c:178
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