forked from neurodebian/afni_removeme_eventually
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1dcat.c
274 lines (254 loc) · 9.61 KB
/
1dcat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*****************************************************************************
Major portions of this software are copyrighted by the Medical College
of Wisconsin, 1994-2000, and are released under the Gnu General Public
License, Version 2. See the file README.Copyright for details.
******************************************************************************/
#include "mrilib.h"
#define MAXIM 1024
int main( int argc , char * argv[] )
{
int nim , ii , jj , kk , nx, narg, oform ;
MRI_IMAGE **inim ;
float *far;
char *formatstr=NULL, *sel=NULL, *fname=NULL;
int nonconst=0 , ncol,ncold , cc , nonfixed=0 , stack=0;
intvec *ncv=NULL ;
char *hline=NULL ;
mainENTRY("1dcat:main");
/*-- help? --*/
if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
printf(
"Usage: 1dcat [options] a.1D b.1D ...\n"
" where each file a.1D, b.1D, etc. is a 1D file.\n"
" In the simplest form, a 1D file is an ASCII file of numbers\n"
" arranged in rows and columns.\n"
"\n"
"1dcat takes as input one or more 1D files, and writes out a 1D file\n"
"containing the side-by-side concatenation of all or a subset of the\n"
"columns from the input files.\n"
"\n"
"* Output goes to stdout (the screen); redirect (e.g., '>') to save elsewhere.\n"
"* All files MUST have the same number of rows!\n"
"* Any header lines (i.e., lines that start with '#') will be lost.\n"
"* For generic 1D file usage help and information, see '1dplot -help'\n"
"\n"
"OPTIONS:\n"
"--------\n"
" -nonconst: Columns that are identically constant should be omitted\n"
" from the output.\n"
"\n"
" -nonfixed: Keep only columns that are marked as 'free' in the \n"
" 3dAllineate header from '-1Dparam_save'.\n"
" If there is no such header, all columns are kept.\n"
"\n"
" -form FORM: Format of the numbers to be output.\n"
" You can also substitute -form FORM with shortcuts such \n"
" as -i, -f, or -c.\n"
" For help on -form's usage, and its shortcut versions\n"
" see ccalc's help for the option of the same name. \n"
"\n"
" -stack: Stack the columns of the resultant matrix in the output.\n"
"\n"
" -sel SEL: Apply the same column/row selection string to all filenames\n"
" on the command line.\n"
" For example:\n"
" 1dcat -sel '[0,2]' f1.1D f2.1D\n"
" is the same as: 1dcat f1.1D'[1,2]' f2.1D'[1,2]'\n"
" The advantage of the option is that it allows wildcard use\n"
" in file specification so that you can run something like:\n"
" 1dcat -sel '[0,2]' f?.1D\n"
"\n"
"EXAMPLE:\n"
"--------\n"
" Input file 1:\n 1\n 2\n 3\n 4\n"
" Input file 2:\n 5\n 6\n 7\n 8\n"
"\n"
" 1dcat data1.1D data2.1D > catout.1D\n"
" Output file: \n 1 5\n 2 6\n 3 7\n 4 8\n"
"\n"
) ;
PRINT_COMPILE_DATE ; exit(0) ;
}
machdep() ;
/* do we have any options? */
oform = CCALC_NOT_SET;
sel = NULL;
stack = 0;
narg = 1;
while (narg < argc && argv[narg][0] == '-') {
if( strncmp(argv[narg],"-nonconst",7) == 0 ){ /* 04 Dec 2010 */
nonconst++ ; narg++ ; continue ;
}
if( strncmp(argv[narg],"-nonfixed",7) == 0 ){ /* 06 Dec 2010 */
nonfixed++ ; narg++ ; continue ;
}
if( strncmp(argv[narg],"-stack",6) == 0 ){ /* 05 Sep 2013 */
stack = 1 ; narg++ ; continue ;
}
if (strcmp(argv[narg],"-form") == 0) {
++narg;
if (narg >= argc) {
fprintf (stderr, "need argument after -form ");
exit (1);
}
if (strcmp(argv[narg],"double") == 0 ) oform = CCALC_DOUBLE;
else if (strcmp(argv[narg],"nice") == 0 ) oform = CCALC_NICE;
else if (strcmp(argv[narg],"int") == 0 ) oform = CCALC_INT;
else if (strcmp(argv[narg],"rint") == 0 ) oform = CCALC_INT;
else if (strcmp(argv[narg],"fint") == 0 ) oform = CCALC_FINT;
else if (strcmp(argv[narg],"cint") == 0 ) oform = CCALC_CINT;
else if (strlen(argv[narg])<=256) {
oform = CCALC_CUSTOM;
formatstr = argv[narg];
}
else {
fprintf (stderr, "Format type '%s' not supported.\n"
"See -help for details.\n", argv[narg]);
exit (1);
}
++narg;
} else if (strcmp(argv[narg],"-sel") == 0) {
++narg;
if (narg >= argc) {
fprintf (stderr, "need argument after -sel ");
exit (1);
}
sel = argv[narg]; ++narg;
} else if (strncmp(argv[narg],"-d",2) == 0) {
oform = CCALC_DOUBLE; ++narg;
} else if (strncmp(argv[narg],"-n",2) == 0) {
oform = CCALC_NICE; ++narg;
} else if (strncmp(argv[narg],"-i",2) == 0) {
oform = CCALC_INT; ++narg;
} else if (strncmp(argv[narg],"-r",2) == 0) {
oform = CCALC_INT; ++narg;
} else if (strncmp(argv[narg],"-f",2) == 0) {
oform = CCALC_FINT; ++narg;
} else if (strncmp(argv[narg],"-c",2) == 0) {
oform = CCALC_CINT; ++narg;
} else { /* break if option is not recognized */
++narg;
break;
}
}
/* read input files */
nim = argc-narg ;
inim = (MRI_IMAGE **) malloc( sizeof(MRI_IMAGE *) * nim ) ;
ncol = 0 ;
if( nonconst || nonfixed ) MAKE_intvec(ncv,1) ;
for( jj=0 ; jj < nim ; jj++ ){
#if 0 /** for testing only **/
if( AFNI_yesenv("ragged") ){
MRI_IMAGE *qim ;
qim = mri_read_ascii_ragged( argv[jj+narg] , 3.e+33 ) ;
fprintf(stderr,"qim: nx=%d ny=%d\n",qim->nx,qim->ny) ;
inim[jj] = mri_transpose(qim) ; mri_free(qim) ;
} else
#endif
if (sel) {
fname = (char *)
calloc((strlen(argv[jj+narg])+strlen(sel)+1), sizeof(char));
strcat(fname, argv[jj+narg]); strcat(fname, sel);
} else {
fname = argv[jj+narg];
}
inim[jj] = mri_read_1D( fname ) ;
if( inim[jj] == NULL )
ERROR_exit("Can't read input file '%s'",fname) ;
if( jj > 0 && inim[jj]->nx != inim[0]->nx )
ERROR_exit("Input file %s doesn't match first file %s in length!",
fname,argv[1]) ;
ncold = ncol ; ncol += inim[jj]->ny ;
if( ncv != NULL ){ /* check for constant columns [04 Dec 2010] */
RESIZE_intvec(ncv,ncol) ;
for( kk=0 ; kk < inim[jj]->ny ; kk++ ) ncv->ar[ncold+kk] = 1 ;
far = MRI_FLOAT_PTR(inim[jj]) ; nx = inim[jj]->nx ;
if( nonconst ){
for( kk=0 ; kk < inim[jj]->ny ; kk++ ){ /* loop over columns */
for( ii=1 ; ii < nx ; ii++ ){ /* loop down column */
if( far[ii+kk*nx] != far[kk*nx] ) break ;
}
if( ii == nx ) ncv->ar[ncold+kk] = 0 ; /* constant */
}
}
if( nonfixed ){
char *hl = mri_read_1D_headerlines( fname ) ;
if( hl != NULL && *hl == '#' ){
char *spt = strchr(hl,'\n') ;
if( spt != NULL ) spt = strchr(spt,'#') ; /* start of line 2 */
if( spt != NULL ){
NI_str_array *sar = NI_decode_string_list( spt+1 , "~" ) ;
if( sar != NULL && sar->num >= inim[jj]->ny ){
for( kk=0 ; kk < inim[jj]->ny ; kk++ ){
spt = strchr(sar->str[kk],'$') ;
if( spt != NULL && spt[1] == '\0' ) ncv->ar[ncold+kk] = 0 ;
else {
if( hline == NULL ) hline = strdup("#") ;
hline = THD_zzprintf( hline , " %s" , sar->str[kk] ) ;
}
}
}
NI_delete_str_array(sar) ;
}
}
}
} /* end of computing ncv = array marking non-constant vectors */
if (sel) {
free(fname); fname = NULL;
}
} /* end of input loop */
/* now do the output */
if( hline != NULL ) printf("%s\n",hline) ;
nx = inim[0]->nx ;
if (stack) {
if (oform == CCALC_NOT_SET) {
for( cc=jj=0 ; jj < nim ; jj++ ){
far = MRI_FLOAT_PTR(inim[jj]) ;
for( kk=0 ; kk < inim[jj]->ny ; kk++,cc++ ){
for( ii=0 ; ii < nx ; ii++ ){
if( ncv == NULL || ncv->ar[cc] )
printf(" %g\n", far[ii+kk*nx] ) ;
}
}
}
} else {
for( cc=jj=0 ; jj < nim ; jj++ ){
far = MRI_FLOAT_PTR(inim[jj]) ;
for( kk=0 ; kk < inim[jj]->ny ; kk++,cc++ ){
for( ii=0 ; ii < nx ; ii++ ){
if( ncv == NULL || ncv->ar[cc] )
printf(" %s\n",
format_value_4print(far[ii+kk*nx], oform, formatstr ));
}
}
}
}
} else {
if (oform == CCALC_NOT_SET) {
for( ii=0 ; ii < nx ; ii++ ){
for( cc=jj=0 ; jj < nim ; jj++ ){
far = MRI_FLOAT_PTR(inim[jj]) ;
for( kk=0 ; kk < inim[jj]->ny ; kk++,cc++ ){
if( ncv == NULL || ncv->ar[cc] )
printf(" %g", far[ii+kk*nx] ) ;
/* printf(" %+.2f", far[ii+kk*nx] ) ;*/
}
}
printf("\n") ;
}
} else {
for( ii=0 ; ii < nx ; ii++ ){
for( cc=jj=0 ; jj < nim ; jj++ ){
far = MRI_FLOAT_PTR(inim[jj]) ;
for( kk=0 ; kk < inim[jj]->ny ; kk++,cc++ ){
if( ncv == NULL || ncv->ar[cc] )
printf(" %s",
format_value_4print(far[ii+kk*nx], oform, formatstr ));
}
}
printf("\n") ;
}
}
}
exit(0) ;
}