1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "mrilib.h"
8 #include "coxplot.h"
9 #include "display.h"
10 
11 /*---- quickie program to look at some graphs - RWCox - Feb 1999 ----*/
12 
13 #define DEFAULT_NCOLOVR 20
14 
15 static char * INIT_colovr[DEFAULT_NCOLOVR] = {
16    "#ffff00" , "#ffcc00"   , "#ff9900"  , "#ff6900" , "#ff4400" , "#ff0000" ,
17    "#0000ff" , "#0044ff"   , "#0069ff"  , "#0099ff" , "#00ccff" , "#00ffff" ,
18    "green"   , "limegreen" , "violet"   , "hotpink" ,
19    "white"   , "#dddddd"   , "#bbbbbb"  , "black"
20 } ;
21 
22 static char * INIT_labovr[DEFAULT_NCOLOVR] = {
23    "yellow" , "yell-oran" , "oran-yell" , "orange"   , "oran-red" , "red"   ,
24    "dk-blue", "blue"      , "lt-blue1"  , "lt-blue2" , "blue-cyan", "cyan"  ,
25    "green"  , "limegreen" , "violet"    , "hotpink"  ,
26    "white"  , "gry-dd"    , "gry-bb"    , "black"
27 } ;
28 
29 static int nx,nts , sep=1 ;
30 static float ** yar , * xar ;
31 static MCW_DC * dc ;
32 static char * title = NULL , * xlabel = NULL , * ylabel = NULL ;
33 
34 static char * dfile_nar[6] = {
35          "Roll [\\degree]" , "Pitch [\\degree]" , "Yaw [\\degree]"    ,
36          "\\Delta I-S [mm]" , "\\Delta R-L [mm]" , "\\Delta A-P [mm]"  } ;
37 
38 static int    nyar = 0 ;
39 static char * ynar[128] ;
40 static char ** yname = NULL ;
41 
42 void startup_timeout_CB( XtPointer client_data , XtIntervalId * id ) ;
43 void scroll_timeout_CB ( XtPointer client_data , XtIntervalId * id ) ;
44 
45 XtAppContext app ;
46 
main(int argc,char * argv[])47 int main( int argc , char * argv[] )
48 {
49    int iarg , ii , ny , ignore=0 , use=0 , install=0 ;
50    float dx=1.0 ;
51    char * tsfile , * cpt ;
52    char dname[THD_MAX_NAME] , subv[THD_MAX_NAME] ;
53    MRI_IMAGE * inim , * flim ;
54    float * far ;
55    Widget shell ;
56    int use_stdin=0 ; /* 01 Aug 2001 */
57 
58    /*-- help? --*/
59 
60    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
61      printf("Usage: 1dplot [options] tsfile\n"
62             "Graphs the columns of a *.1D type time series file to the screen.\n"
63             "\n"
64             "Options:\n"
65             " -install   = Install a new X11 colormap.\n"
66             " -sep       = Plot each column in a separate sub-graph.\n"
67             " -one       = Plot all columns together in one big graph.\n"
68             "                [default = -sep]\n"
69             " -dx xx     = Spacing between points on the x-axis is 'xx'\n"
70             "                [default = 1]\n"
71             " -ignore nn = Skip first 'nn' rows in the input file\n"
72             "                [default = 0]\n"
73             " -use mm    = Plot 'mm' points [default = all of them]\n"
74             " -xlabel aa = Put string 'aa' below the x-axis\n"
75             "                [default = no axis label]\n"
76             " -ylabel aa = Put string 'aa' to the left of the y-axis\n"
77             "                [default = no axis label]\n"
78             "\n"
79             " -stdin     = Don't read from tsfile; instead, read from\n"
80             "              stdin and plot it.\n"
81             "\n"
82             " -ynames aa bb ... = Use the strings 'aa', 'bb', etc., as\n"
83             "                     labels to the right of the graphs,\n"
84             "                     corresponding to each input column.\n"
85             "                     These strings CANNOT start with the\n"
86             "                     '-' character.\n"
87             "\n"
88             " -volreg           = Makes the 'ynames' be the same as the\n"
89             "                     6 labels used in plug_volreg for\n"
90             "                     Roll, Pitch, Yaw, I-S, R-L, and A-P\n"
91             "                     movements, in that order.\n"
92             "\n"
93             "You may also select a subset of columns to display using\n"
94             "a tsfile specification like 'fred.1D[0,3,5]', indicating\n"
95             "that columns #0, #3, and #5 will be the only ones plotted.\n"
96             "For more details on this selection scheme, see the output\n"
97             "of '3dcalc -help'.\n"
98             "\n"
99             "Example: graphing a 'dfile' output by 3dvolreg, when TR=5:\n"
100             "   1dplot -volreg -dx 5 -xlabel Time 'dfile[1..6]'\n"
101             "\n"
102            ) ;
103       exit(0) ;
104    }
105 
106    mainENTRY("1dplot main"); machdep();
107 
108    /* open X11 */
109 
110    shell = XtVaAppInitialize(
111               &app , "AFNI" , NULL , 0 , &argc , argv , NULL , NULL ) ;
112    if( shell == NULL ){
113       fprintf(stderr,"** Cannot initialize X11!\n") ; exit(1) ;
114    }
115 
116    cpt = my_getenv("TMPDIR") ;  /* just for fun */
117 
118    /*-- scan arguments that X11 didn't eat --*/
119 
120    iarg = 1 ;
121    while( iarg < argc && argv[iarg][0] == '-' ){
122 
123      if( strcmp(argv[iarg],"-install") == 0 ){
124         install++ ; iarg++ ; continue ;
125      }
126 
127      if( strcmp(argv[iarg],"-stdin") == 0 ){  /* 01 Aug 2001 */
128         use_stdin++ ; iarg++ ; continue ;
129      }
130 
131      if( strcmp(argv[iarg],"-") == 0 ){  /* skip */
132         iarg++ ; continue ;
133      }
134 
135      if( strcmp(argv[iarg],"-ynames") == 0 ){
136         iarg++ ;
137         while( iarg < argc && argv[iarg][0] != '-' ){
138            ynar[nyar++] = argv[iarg++] ;
139         }
140         continue ;
141      }
142 
143      if( strcmp(argv[iarg],"-volreg") == 0 ){
144         int ii ;
145         for( ii=0 ; ii < 6 ; ii++ ) ynar[nyar++] = dfile_nar[ii] ;
146         iarg++ ; continue ;
147      }
148 
149      if( strcmp(argv[iarg],"-title") == 0 ){
150         title = argv[++iarg] ;
151         iarg++ ; continue ;
152      }
153 
154      if( strcmp(argv[iarg],"-xlabel") == 0 ){
155         xlabel = argv[++iarg] ;
156         iarg++ ; continue ;
157      }
158 
159      if( strcmp(argv[iarg],"-ylabel") == 0 ){
160         ylabel = argv[++iarg] ;
161         iarg++ ; continue ;
162      }
163 
164      if( strcmp(argv[iarg],"-ignore") == 0 ){
165         ignore = strtod( argv[++iarg] , NULL ) ;
166         if( ignore < 0 ){fprintf(stderr,"** Illegal -ignore value!\n");exit(1);}
167         iarg++ ; continue ;
168      }
169 
170      if( strcmp(argv[iarg],"-use") == 0 ){
171         use = strtod( argv[++iarg] , NULL ) ;
172         if( use < 2 ){fprintf(stderr,"** Illegal -use value!\n");exit(1);}
173         iarg++ ; continue ;
174      }
175 
176      if( strcmp(argv[iarg],"-dx") == 0 ){
177         dx = strtod( argv[++iarg] , NULL ) ;
178         if( dx <= 0.0 ){fprintf(stderr,"** Illegal -dx value!\n");exit(1);}
179         iarg++ ; continue ;
180      }
181 
182      if( strcmp(argv[iarg],"-sep") == 0 ){
183         sep = 1 ; iarg++ ; continue ;
184      }
185 
186      if( strcmp(argv[iarg],"-one") == 0 ){
187         sep = 0 ; iarg++ ; continue ;
188      }
189 
190      fprintf(stderr,"** Unknown option: %s\n",argv[iarg]) ; exit(1) ;
191    }
192 
193    if( iarg >= argc && !use_stdin ){
194       fprintf(stderr,"** No tsfile on command line!\n") ; exit(1) ;
195    }
196 
197    dc = MCW_new_DC( shell , 16 ,
198                     DEFAULT_NCOLOVR , INIT_colovr , INIT_labovr ,
199                     1.0 , install ) ;
200 
201    if( nyar > 0 ) yname = ynar ;
202 
203    /*-- 01 Aug 2001: read from stdin instead of a file --*/
204 
205    if( use_stdin ){
206      char lbuf[2560] , *cpt ;
207      int nval ;
208      float val[9] ;
209 
210      cpt = afni_fgets(lbuf,2560,stdin) ;
211      if( cpt == NULL ){
212         fprintf(stderr,"*** Can't read from stdin!\n"); exit(1);
213      }
214      nval = sscanf(lbuf,"%f%f%f%f%f%f%f%f%f",
215                    val+0,val+1,val+2,val+3,val+4,val+5,val+6,val+7,val+8) ;
216      if( nval < 1 ){
217         fprintf(stderr,"*** Can't read numbers from stdin!\n"); exit(1);
218      }
219 
220      subv[0] = '\0' ; nx = nval ; ny = 1 ;
221      far = (float *) malloc(sizeof(float)*nval) ;
222      memcpy(far,val,sizeof(float)*nx) ;
223      while(1){  /* read from stdin */
224         cpt = afni_fgets(lbuf,2560,stdin) ;
225         if( cpt == NULL ) break ;
226         nval = sscanf(lbuf,"%f%f%f%f%f%f%f%f%f",
227                       val+0,val+1,val+2,val+3,val+4,val+5,val+6,val+7,val+8) ;
228         if( nval < 1 ) break ;
229         far = (float *) realloc( far , sizeof(float)*(ny+1)*nx ) ;
230         memcpy(far+ny*nx,val,sizeof(float)*nx) ;
231         ny++ ;
232      }
233      if( ny < 2 ){
234         fprintf(stderr,"** Can't read enough data from stdin\n"); exit(1);
235      }
236      inim = mri_new_vol_empty( nx,ny,1 , MRI_float ) ;
237      mri_fix_data_pointer( far , inim ) ;
238 
239    } else {  /*-- old code: read from a file --*/
240 
241      /* check input filename for index strings */
242 
243      tsfile = argv[iarg] ;
244      cpt    = strstr(tsfile,"[") ;
245 
246      if( cpt == NULL ){
247         strcpy( dname , tsfile ) ;
248         subv[0] = '\0' ;
249      } else if( cpt == tsfile ){
250         fprintf(stderr,"** Illegal filename on command line!\n");exit(1);
251      } else {
252         ii = cpt - tsfile ;
253         memcpy(dname,tsfile,ii) ; dname[ii] = '\0' ;
254         strcpy(subv,cpt) ;
255      }
256 
257      /* read input file */
258 
259      inim = mri_read_ascii( dname ) ;
260      if( inim == NULL ){
261         fprintf(stderr,"** Can't read input file %s\n",dname) ;
262         exit(1);
263      }
264    } /* end of file input */
265 
266    if( inim->kind != MRI_float ){  /* should not happen */
267       flim = mri_to_float(inim) ; mri_free(inim) ; inim = flim ;
268    }
269    flim = mri_transpose(inim) ; mri_free(inim) ;
270    far  = MRI_FLOAT_PTR(flim) ;
271    nx   = flim->nx ;
272    ny   = flim->ny ;
273 
274    /* make x axis */
275 
276    xar = (float *) malloc( sizeof(float) * nx ) ;
277    for( ii=0 ; ii < nx ; ii++ ) xar[ii] = dx * ii ;
278 
279    /* select data to plot */
280 
281    if( subv[0] == '\0' ){  /* no sub-list */
282 
283       nts = ny ;
284       yar = (float **) malloc(sizeof(float *)*nts) ;
285       for( ii=0 ; ii < ny ; ii++ ) yar[ii] = far + (ii*nx+ignore) ;
286 
287    } else {                /* process sub-list */
288       int  * ivlist , * ivl ;
289 
290       ivlist = MCW_get_intlist( ny , subv ) ;
291       if( ivlist == NULL || ivlist[0] < 1 ){
292          fprintf(stderr,"** Illegal column selectors on command line!\n");
293          exit(1);
294       }
295       nts = ivlist[0] ;
296       ivl = ivlist + 1 ;
297       for( ii=0 ; ii < nts ; ii++ ){
298          if( ivl[ii] < 0 || ivl[ii] >= ny ){
299             fprintf(stderr,"** Illegal selector on command line!\n");
300             exit(1) ;
301          }
302       }
303       yar = (float **) malloc(sizeof(float *)*nts) ;
304       for( ii=0 ; ii < nts ; ii++ ) yar[ii] = far + (ivl[ii]*nx+ignore) ;
305       free(ivlist) ;
306    }
307 
308    nx = nx - ignore ;  /* cut off the ignored points */
309 
310    if( use > 1 && nx > use ) nx = use ;  /* 29 Nov 1999 */
311 
312    /* start X11 */
313 
314    (void) XtAppAddTimeOut( app , 123 , startup_timeout_CB , NULL ) ;
315 
316    XtAppMainLoop(app) ;
317    exit(0) ;
318 }
319 
320 /*-----------------------------------------------------------------*/
killfunc(void * fred)321 void killfunc(void * fred){ exit(0) ; }
322 /*-----------------------------------------------------------------*/
323 
plot_ts_qqq(Display * dpy,int nx,float * x,int ny,float ** y,char * lab_xxx,char * lab_yyy,char * lab_top,char ** nam_yyy,void_func * killfunc)324 MEM_topshell_data * plot_ts_qqq( Display * dpy ,
325                   int nx , float * x , int ny , float ** y ,
326                   char * lab_xxx , char * lab_yyy , char * lab_top ,
327                   char ** nam_yyy , void_func * killfunc )
328 {
329    MEM_plotdata * mp ;
330    MEM_topshell_data * mpmp=NULL ;
331    int ymask = 0 ;
332 
333    if( dpy == NULL ) return ;
334 
335    if( ny < 0 ){ ymask = TSP_SEPARATE_YBOX ; ny = -ny ; }
336 
337    mp = plot_ts_mem( nx,x , ny,ymask,y , lab_xxx , lab_yyy , lab_top , nam_yyy ) ;
338    if( mp != NULL )
339     mpmp = memplot_to_topshell( dpy , mp , killfunc ) ;
340 
341    return mpmp ;
342 }
343 
344 MEM_topshell_data * mp=NULL ;
345 
startup_timeout_CB(XtPointer client_data,XtIntervalId * id)346 void startup_timeout_CB( XtPointer client_data , XtIntervalId * id )
347 {
348    int ng ;
349 
350    /* make graph */
351 
352    ng = (sep) ? (-nts) : (nts) ;
353 
354    mp = plot_ts_qqq( dc->display , nx , xar , ng , yar ,
355                      xlabel , ylabel , title , yname , killfunc ) ;
356 
357    (void) XtAppAddTimeOut( app , 16 , scroll_timeout_CB , NULL ) ;
358    return ;
359 }
360 
scroll_timeout_CB(XtPointer client_data,XtIntervalId * id)361 void scroll_timeout_CB( XtPointer client_data , XtIntervalId * id )
362 {
363    XWindowAttributes xwat ;
364 
365    XGetWindowAttributes( XtDisplay(mp->drawing),
366                          XtWindow(mp->drawing) , &xwat ) ;
367 
368    XCopyArea( XtDisplay(mp->drawing) ,
369               XtWindow(mp->drawing) , XtWindow(mp->drawing) ,
370               DefaultGC(XtDisplay(mp->drawing),0) ,
371               1 , 0 , xwat.width-1 , xwat.height , 0 , 0 ) ;
372 
373    XClearArea( XtDisplay(mp->drawing) ,
374                XtWindow(mp->drawing) ,
375                xwat.width-1 , 0 , 1 , xwat.height , FALSE ) ;
376 
377    (void) XtAppAddTimeOut( app , 16 , scroll_timeout_CB , NULL ) ;
378    return ;
379 }
380