9#include "tpcclibConfig.h"
23static char *info[] = {
24 "Insert one or more TACs from TAC file 2 into another, optionally new,",
25 "TAC file 1. Files must have the same number of samples (time frames).",
26 "If the TAC number or name is not specified, then all TACs in file2 are added.",
27 "If file1 does not exist, it will be created.",
28 "Alternatively, all TACs from several files can be added to file1.",
30 "Usage: @P [options] file1 file2 [tacname or tacnumber]",
32 "Usage: @P [options] file1 file2 [file3 [file4 ...]]",
36 " Copied region names are changed to contain the study number.",
38 " TAC name must match the initial part of names in file2.",
40 " Existing file1 is overwritten.",
42 " Program does not mind if the time or calibration units do not match.",
44 " Selected TAC(s) are removed from file2.",
47 "Example 1: Add TACs with name 'striatum' from file a123_12.dat into",
49 " @P a123_06.dat a123_12.dat striatum",
51 "Example 2a: Combine specified TAC files into a new file",
52 " @P combined.tac s6789*.tac",
54 "Example 2b: Combine specified TAC files into a new file",
55 "using for loop in MS Windows command prompt window",
56 " for %g in (s6789*.tac) do @P combined.tac %g",
58 "Example 2c: Combine all putamen TACs from TAC files into a new file,",
59 "storing study number as region name for each TAC,",
60 "using for loop in MS Windows command prompt window",
61 " for %g in (a*.tac) do @P -sn putamen.tac %g put",
63 "See also: taclist, tacdel, tacsplit, tacsort, tacblend, tacunit, tac2svg",
65 "Keywords: TAC, tool",
84int main(
int argc,
char **argv)
86 int ai, help=0, version=0, verbose=1;
93 int rnameStartGiven=0;
94 char *cptr, tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX];
96 int firstfile=0, fileNr=0;
102 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
103 tacfile1[0]=tacfile2[0]=rname[0]=(char)0;
105 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
107 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
108 if(strcasecmp(cptr,
"SN")==0 || strncasecmp(cptr,
"STUDYNUMBER", 5)==0) {
110 }
else if(strncasecmp(cptr,
"OVR", 2)==0) {
111 overwrite=1;
continue;
112 }
else if(strcasecmp(cptr,
"MOVE")==0) {
114 }
else if(strncasecmp(cptr,
"FORCE", 1)==0) {
115 checkunits=0;
continue;
116 }
else if(strncasecmp(cptr,
"-FORCE", 2)==0) {
117 checkunits=0;
continue;
118 }
else if(strcasecmp(cptr,
"NT")==0) {
119 checkunits=0;
continue;
120 }
else if(strcasecmp(cptr,
"NAMESTART")==0) {
121 rnameStartGiven=1;
continue;
123 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
132 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
137 if(ai<argc) {
strlcpy(tacfile1, argv[ai++], FILENAME_MAX);}
139 firstfile=ai; fileNr=1;
140 strlcpy(tacfile2, argv[ai++], FILENAME_MAX);
142 fprintf(stderr,
"Error: missing filename.\n");
return(1);
146 for(; ai<argc; ai++) {
147 if(access(argv[ai], 0) == -1) ret++;
else fileNr++;
150 fileNr=1; ai=firstfile+1;
151 strlcpy(rname, argv[ai++], 1024);
152 for(
int n=1; ai<argc && n<3; ai++, n++) {
153 if((strlen(rname)+strlen(argv[ai]))<1000) {
155 strlcat(rname, argv[ai], 1024);
160 fprintf(stderr,
"Error: extra command-line argument.\n");
164 if(!tacfile2[0]) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
168 for(ai=firstfile; ai<argc; ai++) {
170 if(strcasecmp(argv[ai], tacfile1)==0) {
171 fprintf(stderr,
"Error: the same name for input and output file.\n");
177 if(fileNr>1 && tacmove!=0) {
178 fprintf(stderr,
"Warning: ignoring option -move.\n");
184 printf(
"tacfile1 := %s\n", tacfile1);
185 printf(
"tacfile2 := %s\n", tacfile2);
186 printf(
"overwrite := %d\n", overwrite);
187 printf(
"checkunits := %d\n", checkunits);
188 printf(
"sn2vn := %d\n", sn2vn);
189 if(rname[0]) printf(
"rname := %s\n", rname);
190 if(fileNr>1) printf(
"fileNr := %d\n", fileNr);
191 if(fileNr==1) printf(
"move := %d\n", tacmove);
197 if(verbose>1) printf(
"trying to read %s\n", tacfile1);
199 ret=
tacRead(&tac1, tacfile1, &status);
203 if(verbose>=0) printf(
"File %s is overwritten.\n", tacfile1);
205 }
else if(verbose>1) {
207 printf(
"tacNr1 := %d\n", tac1.
tacNr);
208 printf(
"sampleNr1 := %d\n", tac1.
sampleNr);
212 if(verbose>=0) printf(
"File %s did not exist and will be created.\n", tacfile1);
214 }
else if(overwrite==0) {
216 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
219 if(verbose>0) printf(
"File %s will be overwritten.\n", tacfile1);
227 if(verbose>1) printf(
"reading %s\n", tacfile2);
229 ret=
tacRead(&tac2, tacfile2, &status);
236 printf(
"tacNr2 := %d\n", tac2.
tacNr);
237 printf(
"sampleNr2 := %d\n", tac2.
sampleNr);
242 fprintf(stderr,
"Error: TAC files have different sample number.\n");
257 if(fileNr==1 && rname[0]) {
258 if(verbose>1) {printf(
"selecting regions to be copied.\n"); fflush(stdout);}
259 if(rnameStartGiven) {
260 char rname2[strlen(rname)+3];
261 strcpy(rname2, rname); strcat(rname2,
"*");
267 fprintf(stderr,
"Error: specified TACs not found in %s.\n", tacfile2);
270 if(addNr==tac2.
tacNr) {
271 fprintf(stderr,
"Warning: adding all TACs from %s.\n", tacfile2);
274 fprintf(stderr,
"Warning: ignoring option -move.\n");
278 if(verbose>1) printf(
"%d tac(s) match name '%s'\n", addNr, rname);
281 for(
int i=0; i<tac2.
tacNr; i++) tac2.
c[i].
sw=1;
285 fprintf(stderr,
"Warning: ignoring option -move.\n");
289 if(verbose>1) printf(
" %d TAC(s) will be added.\n", addNr);
297 if(verbose>1) printf(
"changing region name to include study number\n");
301 fprintf(stderr,
"Error: TAC file does not contain valid studynumber.\n");
304 if(verbose>2) printf(
"studynr2 := %s\n", studynr);
307 for(
int i=0; i<tac2.
tacNr; i++)
if(tac2.
c[i].
sw) {
309 len=1+strlen(tac2.
c[i].
name);
310 len+=strlen(studynr);
314 strcpy(tmp, studynr); strcat(tmp,
"_"); strcat(tmp, tac2.
c[i].
name);
315 strcpy(tac2.
c[i].
name, tmp);
318 strcpy(tac2.
c[i].
name, studynr);
328 if(verbose>2) printf(
"creating space for %d TACs\n", addNr);
332 fprintf(stderr,
"Error: cannot allocate memory.\n");
338 fprintf(stderr,
"Error: cannot copy TAC header.\n");
344 fprintf(stderr,
"Error: cannot copy sample times.\n");
348 if(verbose>2) printf(
"adding space for %d TACs\n", addNr);
352 fprintf(stderr,
"Error: cannot allocate memory.\n");
361 fprintf(stderr,
"Error: units do match.\n");
366 if(verbose>0) fprintf(stderr,
"Warning: units do match.\n");
376 for(
int i=0; i<tac2.
tacNr; i++)
if(tac2.
c[i].
sw) {
377 if(verbose>3) printf(
" copying TAC %d\n", 1+i);
380 fprintf(stderr,
"Error: cannot copy TAC.\n");
389 for(
int j=0; j<tac2.
sampleNr; j++) tac1.
w[j]=tac2.
w[j];
399 if(verbose>2) printf(
"%s deleted\n", tac2.
c[i].
name);
405 fprintf(stderr,
"Warning: cannot delete TAC.\n");
406 }
else if(n>0 && tac2.
tacNr>0) {
407 if(verbose>1) printf(
"writing %s\n", tacfile2);
408 FILE *fp; fp=fopen(tacfile2,
"w");
410 fprintf(stderr,
"Warning: cannot open file for writing (%s)\n", tacfile2);
414 if(ret!=
TPCERROR_OK) fprintf(stderr,
"Warning: cannot write file %s\n", tacfile2);
426 for(
int tfi=1; tfi<fileNr; tfi++, ai++) {
428 strlcpy(tacfile2, argv[ai], FILENAME_MAX);
429 if(verbose>1) printf(
"reading %s\n", tacfile2);
430 ret=
tacRead(&tac2, tacfile2, &status);
432 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
437 printf(
"tacNr%d := %d\n", 1+tfi, tac2.
tacNr);
438 printf(
"sampleNr%d := %d\n", 1+tfi, tac2.
sampleNr);
442 fprintf(stderr,
"Error: TAC files have different sample number.\n");
446 if(verbose>1) printf(
" %d TAC(s) will be added.\n", addNr);
451 if(verbose>2) printf(
"changing region name to include study number\n");
455 fprintf(stderr,
"Error: TAC file does not contain valid studynumber.\n");
458 if(verbose>2) printf(
"studynr%d := %s\n", 1+tfi, studynr);
461 for(
int i=0; i<tac2.
tacNr; i++) {
463 len=1+strlen(tac2.
c[i].
name);
464 len+=strlen(studynr);
468 strcpy(tmp, studynr); strcat(tmp,
"_"); strcat(tmp, tac2.
c[i].
name);
469 strcpy(tac2.
c[i].
name, tmp);
472 strcpy(tac2.
c[i].
name, studynr);
478 if(verbose>2) printf(
"adding space for %d TACs\n", addNr);
482 fprintf(stderr,
"Error: cannot allocate memory.\n");
491 fprintf(stderr,
"Error: units do match.\n");
496 if(verbose>0) fprintf(stderr,
"Warning: units do match.\n");
505 for(
int i=0; i<tac2.
tacNr; i++) {
506 if(verbose>3) printf(
" copying TAC %d\n", 1+i);
509 fprintf(stderr,
"Error: cannot copy TAC.\n");
518 for(
int j=0; j<tac2.
sampleNr; j++) tac1.
w[j]=tac2.
w[j];
526 if(verbose>1) printf(
"writing %s\n", tacfile1);
527 FILE *fp; fp=fopen(tacfile1,
"w");
529 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", tacfile1);
536 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
543 if(verbose>=0) printf(
" %d TAC(s) added to %s\n", sumAddNr, tacfile1);
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
size_t strlcat(char *dst, const char *src, size_t dstsize)
char name[MAX_TACNAME_LEN+1]
IFT h
Optional (but often useful) header information.
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
int tacAllocateMore(TAC *tac, int tacNr)
int tacCopyTacc(TACC *d1, TACC *d2, int sampleNr)
int tacCopyHdr(TAC *tac1, TAC *tac2)
Copy TAC header data from tac1 to tac2.
int tacGetHeaderStudynr(IFT *h, char *s, TPCSTATUS *status)
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
char * tacFormattxt(tacformat c)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacDeleteTACC(TAC *d, int i)
int tacSelectTACs(TAC *d, const char *region_name, int reset, TPCSTATUS *status)
int tacYUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
int tacIsWeighted(TAC *tac)
int tacXCopy(TAC *tac1, TAC *tac2, int i1, int i2)
Header file for library libtpcextensions.
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero).
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_UNKNOWN_UNIT
Unknown data unit.
@ TPCERROR_CANNOT_OPEN
Cannot open file.
#define MAX_STUDYNR_LEN
Define max study number length.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.