forked from neurodebian/afni_removeme_eventually
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1dsum.c
93 lines (76 loc) · 2.69 KB
/
1dsum.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
#include "mrilib.h"
#define MAXIM 1024
/*** Adapted from 1dcat.c -- RWCox -- 18 Apr 2001 ***/
int main( int argc , char * argv[] )
{
int nim , ii , jj , kk , nx ;
MRI_IMAGE ** inim ;
float * far ;
int ncol , ic ;
float * csum ;
int nn_ignore=0 , mm_use=0 , iarg=1 ;
/*-- help? --*/
if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
printf("Usage: 1dsum [options] a.1D b.1D ...\n"
"where each file a.1D, b.1D, etc. is an ASCII file of numbers arranged\n"
"in rows and columns. The sum of each column is written to stdout.\n"
"\n"
"Options:\n"
" -ignore nn = skip the first nn rows of each file\n"
" -use mm = use only mm rows from each file\n"
) ;
PRINT_COMPILE_DATE ; exit(0) ;
}
machdep() ;
/* parse options */
while( iarg < argc && argv[iarg][0] == '-' ){
if( strncmp(argv[iarg],"-ignore",4) == 0 ){
nn_ignore = (int) strtod(argv[++iarg],NULL) ;
if( nn_ignore < 0 ){fprintf(stderr,"** Illegal -ignore value!\n");exit(1);}
iarg++ ; continue ;
}
if( strncmp(argv[iarg],"-use",4) == 0 ){
mm_use = (int) strtod(argv[++iarg],NULL) ;
if( mm_use < 0 ){fprintf(stderr,"** Illegal -use value!\n");exit(1);}
iarg++ ; continue ;
}
fprintf(stderr,"** Unknown option: %s\n",argv[iarg]) ; exit(1) ;
}
/* read input files */
nim = argc-iarg ;
inim = (MRI_IMAGE **) malloc( sizeof(MRI_IMAGE *) * nim ) ;
ncol = 0 ;
for( jj=0 ; jj < nim ; jj++ ){
inim[jj] = mri_read_1D( argv[jj+iarg] ) ;
if( inim[jj] == NULL ){
fprintf(stderr,"** Can't read input file %s\n",argv[jj+iarg]) ;
exit(1) ;
}
if( jj > 0 && inim[jj]->nx != inim[0]->nx ){
fprintf(stderr,
"** Input file %s doesn't match first file %s in length!\n",
argv[jj+iarg],argv[iarg]) ;
exit(1) ;
}
ncol += inim[jj]->ny ;
}
if( mm_use == 0 ){
mm_use = inim[0]->nx - nn_ignore ;
if( mm_use < 0 ){fprintf(stderr,"** -ignore is too big for these files!\n");exit(1);}
}
if( nn_ignore + mm_use > inim[0]->nx ){
fprintf(stderr,"** -ignore + -use is too big for these files!\n");exit(1);
}
csum = (float *) malloc(sizeof(float)*ncol) ;
for( ic=0 ; ic < ncol ; ic++ ) csum[ic] = 0.0 ;
nx = inim[0]->nx ;
for( ii=nn_ignore ; ii < nn_ignore+mm_use ; ii++ ){
for( ic=jj=0 ; jj < nim ; jj++ ){
far = MRI_FLOAT_PTR(inim[jj]) ;
for( kk=0 ; kk < inim[jj]->ny ; kk++ ) csum[ic++] += far[ii+kk*nx] ;
}
}
for( ic=0 ; ic < ncol ; ic++ ) printf("%g ",csum[ic]) ;
printf("\n");
exit(0) ;
}