1 #include "SUMA_suma.h"
2 
usage_SUMA_FScurv_to_1D_Main()3 void usage_SUMA_FScurv_to_1D_Main ()
4 
5   {/*Usage*/
6       static char FuncName[]={"usage_FScurv_to_1D"};
7       char * s = NULL;
8 
9       SUMA_ENTRY;
10 
11           printf ("\n"
12                   "Usage:  FScurv_to_1D [-skip_coords] [-output outfile] -input curv_name.asc  \n"
13                   "   Reads in a FreeSurfer curvature file and writes it out in 1D format. \n"
14                   "   But the format is 1D to begin with, so 'what is that program for?' you ask. \n"
15                   "   Not much, I say. It is used to test a SUMA function and also allows you\n"
16                   "   to select the node index and data values from the 5 columns of the curv files.\n"
17                   "\n"
18                   "   -input curv_name.asc: name of ASCII curvature file. To change a curvature file \n"
19                   "                     to ASCII, use mris_convert -c curv_name surf curvfile.asc \n"
20                   "                     surf is the surface over which the curvfile is defined, like\n"
21                   "                     lh.inflated.\n"
22                   "   -skip_coords: If specified, the node coordinates are not included in the output.\n"
23                   "   -output outfile: If specified, the output goes to a file instead of stdout, \n"
24                   "                    which is the screen\n"
25                   "\n");
26        s = SUMA_New_Additions(0, 1); printf("%s\n", s);SUMA_free(s); s = NULL;
27        printf("       Ziad S. Saad SSCC/NIMH/NIH saadz@mail.nih.gov     \n");
28        exit (0);
29   }/*Usage*/
30 
main(int argc,char * argv[])31 int main (int argc,char *argv[])
32 {/* Main */
33    static char FuncName[]={"FScurv_to_1D"};
34    int i, j, id, nrows=0, ncols=0, kar;
35    float *v = NULL;
36    char *outname = NULL;
37    char *fname = NULL;
38    FILE *outfile=NULL;
39    SUMA_Boolean SkipCoords = NOPE, brk, rowmajor;
40    SUMA_Boolean LocalHead = NOPE;
41 
42 	/* allocate space for CommonFields structure */
43 	SUMAg_CF = SUMA_Create_CommonFields ();
44 	if (SUMAg_CF == NULL) {
45 		fprintf(SUMA_STDERR,"Error %s: Failed in SUMA_Create_CommonFields\n", FuncName);
46 		exit(1);
47 	}
48 
49    /* parse command line */
50    kar = 1;
51    outname = NULL;
52    fname = NULL;
53    SkipCoords = NOPE;
54    rowmajor = YUP;  /* just to test the function's execution */
55 	brk = NOPE;
56 	while (kar < argc) { /* loop accross command ine options */
57 		/*fprintf(stdout, "%s verbose: Parsing command line...\n", FuncName);*/
58 		if (strcmp(argv[kar], "-h") == 0 || strcmp(argv[kar], "-help") == 0) {
59 			 usage_SUMA_FScurv_to_1D_Main();
60           exit (0);
61 		}
62       if (!brk && ( (strcmp(argv[kar], "-skip_coords") == 0) ) ) {
63 			SkipCoords = YUP;
64          brk = YUP;
65 		}
66       if (!brk && (strcmp(argv[kar], "-output") == 0)) {
67          kar ++;
68 			if (kar >= argc)  {
69 		  		fprintf (SUMA_STDERR, "need argument after -output\n");
70 				exit (1);
71 			}
72          outname = argv[kar];
73 			brk = YUP;
74 		}
75       if (!brk && (strcmp(argv[kar], "-input") == 0)) {
76          kar ++;
77 			if (kar >= argc)  {
78 		  		fprintf (SUMA_STDERR, "need argument after -input\n");
79 				exit (1);
80 			}
81          fname = argv[kar];
82 			brk = YUP;
83 		}
84       if (!brk) {
85 			fprintf (SUMA_STDERR,"Error %s:\nOption %s not understood. Try -help for usage\n", FuncName, argv[kar]);
86 			exit (1);
87 		} else {
88 			brk = NOPE;
89 			kar ++;
90 		}
91    }
92 
93    if (!fname) {
94       SUMA_SL_Err("No input file specified.");
95       exit(1);
96    }
97    /* work the output name */
98    if (!outname) {
99       outfile = SUMA_STDOUT;
100    } else {
101       outname = SUMA_Extension(outname, ".1D", NOPE); /* outname should be freed at the end */
102       if (SUMA_filexists(outname)) {
103          fprintf(SUMA_STDERR,"Error %s: Output file %s exists, will not overwrite.\n", FuncName, outname);
104          exit(1);
105       }
106       outfile = fopen(outname, "w");
107       if (!outfile) {
108          SUMA_SL_Crit("Failed to open file for writing.\n"
109                       "Check file permissions.");
110          exit(1);
111       }
112    }
113 
114    /* do the deed */
115    v = SUMA_readFScurv (fname, &nrows, &ncols, rowmajor, SkipCoords);
116    if (!v) {
117       SUMA_SL_Err("Failed in SUMA_readFScurv");
118       exit(1);
119    }
120 
121    if (rowmajor) {
122       for (i=0; i<nrows; ++i) {
123          id = ncols * i;
124          fprintf(outfile,"%d\t", (int) v[id]);
125          for (j=1; j<ncols; ++j) fprintf(outfile,"%f\t", v[id+j]);
126          fprintf(outfile,"\n");
127       }
128 
129    } else {
130       for (i=0; i<nrows; ++i) {
131          fprintf(outfile,"%d\t", (int) v[i]);
132          for (j=1; j<ncols; ++j) fprintf(outfile,"%f\t", v[i+j*nrows]);
133          fprintf(outfile,"\n");
134       }
135    }
136 
137    if (outname) {
138       fclose (outfile); outfile = NULL;
139       SUMA_free(outname); outname = NULL;
140    }
141    SUMA_free(v); v = NULL;
142 
143    exit(0);
144 }
145