Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

1dplot.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "mrilib.h"
00008 #include "coxplot.h"
00009 #include "display.h"
00010 
00011 /*-------------------------------------------------------------------*/
00012 /*---- quickie program to look at some graphs - RWCox - Feb 1999 ----*/
00013 
00014 #define DEFAULT_NCOLOVR 20
00015 
00016 static char *INIT_colovr[DEFAULT_NCOLOVR] = {
00017    "#ffff00" , "#ffcc00"   , "#ff9900"  , "#ff6900" , "#ff4400" , "#ff0000" ,
00018    "#0000ff" , "#0044ff"   , "#0069ff"  , "#0099ff" , "#00ccff" , "#00ffff" ,
00019    "green"   , "limegreen" , "violet"   , "hotpink" ,
00020    "white"   , "#dddddd"   , "#bbbbbb"  , "black"
00021 } ;
00022 
00023 static char *INIT_labovr[DEFAULT_NCOLOVR] = {
00024    "yellow" , "yell-oran" , "oran-yell" , "orange"   , "oran-red" , "red"   ,
00025    "dk-blue", "blue"      , "lt-blue1"  , "lt-blue2" , "blue-cyan", "cyan"  ,
00026    "green"  , "limegreen" , "violet"    , "hotpink"  ,
00027    "white"  , "gry-dd"    , "gry-bb"    , "black"
00028 } ;
00029 
00030 static int nx,nts , sep=1 ;
00031 static float **yar , *xar ;
00032 static MCW_DC *dc ;
00033 static char *title = NULL , *xlabel = NULL , *ylabel = NULL ;
00034 
00035 static char *dfile_nar[6] = {
00036          "Roll [\\degree]" , "Pitch [\\degree]" , "Yaw [\\degree]"    ,
00037          "\\Delta I-S [mm]" , "\\Delta R-L [mm]" , "\\Delta A-P [mm]"  } ;
00038 
00039 static int    nyar = 0 ;
00040 static char  *ynar[128] ;
00041 static char **yname = NULL ;
00042 
00043 void startup_timeout_CB( XtPointer client_data , XtIntervalId *id ) ;
00044 
00045 /*-----------------------------------------------------------------*/
00046 
00047 int main( int argc , char *argv[] )
00048 {
00049    int iarg , ii , ny , ignore=0 , use=0 , install=0 ;
00050    float dx=1.0 , xzero=0.0 ;
00051    char *cpt ;
00052    MRI_IMAGE *inim , *flim ;
00053    float *far ;
00054    XtAppContext app ;
00055    Widget shell ;
00056    int use_stdin=0 ; /* 01 Aug 2001 */
00057    int out_ps   =0 ; /* 29 Nov 2002 */
00058    int nopush   =0 ;
00059    int nnax=0,mmax=0 , nnay=0,mmay=0 ;
00060    float xbot,xtop   , ybot,ytop ;
00061 
00062    /*-- help? --*/
00063 
00064    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00065      printf("Usage: 1dplot [options] tsfile ...\n"
00066             "Graphs the columns of a *.1D type time series file to the screen.\n"
00067             "\n"
00068             "Options:\n"
00069             " -install   = Install a new X11 colormap.\n"
00070             " -sep       = Plot each column in a separate sub-graph.\n"
00071             " -one       = Plot all columns together in one big graph.\n"
00072             "                [default = -sep]\n"
00073             " -dx xx     = Spacing between points on the x-axis is 'xx'\n"
00074             "                [default = 1]\n"
00075             " -xzero zz  = Initial x coordinate is 'zz' [default = 0]\n"
00076             " -nopush    = Don't 'push' axes ranges outwards.\n"
00077             " -ignore nn = Skip first 'nn' rows in the input file\n"
00078             "                [default = 0]\n"
00079             " -use mm    = Plot 'mm' points [default = all of them]\n"
00080             " -xlabel aa = Put string 'aa' below the x-axis\n"
00081             "                [default = no axis label]\n"
00082             " -ylabel aa = Put string 'aa' to the left of the y-axis\n"
00083             "                [default = no axis label]\n"
00084             "\n"
00085             " -stdin     = Don't read from tsfile; instead, read from\n"
00086             "              stdin and plot it. You cannot combine input\n"
00087             "              from stdin and tsfile(s).  If you want to do\n"
00088             "              so, see program 1dcat.\n"
00089             "\n"
00090             " -ps        = Don't draw plot in a window; instead, write it\n"
00091             "              to stdout in PostScript format.\n"
00092             "              N.B.: If you view this result in 'gv', you should\n"
00093             "                    turn 'anti-alias' off, and switch to\n"
00094             "                    landscape mode.\n"
00095             "\n"
00096             " -xaxis b:t:n:m    = Set the x-axis to run from value 'b' to\n"
00097             "                     value 't', with 'n' major divisions and\n"
00098             "                     'm' minor tic marks per major division.\n"
00099             "                     For example:\n"
00100             "                       -xaxis 0:100:5:20\n"
00101             "                     Setting 'n' to 0 means no tic marks or labels.\n"
00102             "\n"
00103             " -yaxis b:t:n:m    = Similar to above, for the y-axis.  These\n"
00104             "                     options override the normal autoscaling\n"
00105             "                     of their respective axes.\n"
00106             "\n"
00107             " -ynames aa bb ... = Use the strings 'aa', 'bb', etc., as\n"
00108             "                     labels to the right of the graphs,\n"
00109             "                     corresponding to each input column.\n"
00110             "                     These strings CANNOT start with the\n"
00111             "                     '-' character.\n"
00112             "\n"
00113             " -volreg           = Makes the 'ynames' be the same as the\n"
00114             "                     6 labels used in plug_volreg for\n"
00115             "                     Roll, Pitch, Yaw, I-S, R-L, and A-P\n"
00116             "                     movements, in that order.\n"
00117             "\n"
00118             "You may also select a subset of columns to display using\n"
00119             "a tsfile specification like 'fred.1D[0,3,5]', indicating\n"
00120             "that columns #0, #3, and #5 will be the only ones plotted.\n"
00121             "For more details on this selection scheme, see the output\n"
00122             "of '3dcalc -help'.\n"
00123             "\n"
00124             "Example: graphing a 'dfile' output by 3dvolreg, when TR=5:\n"
00125             "   1dplot -volreg -dx 5 -xlabel Time 'dfile[1..6]'\n"
00126             "\n"
00127             "You can also input more than one tsfile, in which case the files\n"
00128             "will all be plotted.  However, if the files have different column\n"
00129             "lengths, the shortest one will rule.\n"
00130             "\n"
00131             "The colors for the line graphs cycle between black, red, green, and\n"
00132             "blue.  You can alter these colors by setting Unix environment\n"
00133             "variables of the form AFNI_1DPLOT_COLOR_xx -- cf. README.environment.\n"
00134             "You can alter the thickness of the lines by setting the variable\n"
00135             "AFNI_1DPLOT_THIK to a value between 0.00 and 0.05 -- the units are\n"
00136             "fractions of the page size.\n"
00137 
00138             "\n"
00139             TS_HELP_STRING
00140            ) ;
00141       exit(0) ;
00142    }
00143 
00144    mainENTRY("1dplot main"); machdep(); PRINT_VERSION("1dplot");
00145 
00146    /* 29 Nov 2002: scan for -ps */
00147 
00148    for( ii=1 ; ii < argc ; ii++ )
00149      if( strcmp(argv[ii],"-ps") == 0 ){ out_ps = 1; break; }
00150 
00151    /* open X11 */
00152 
00153    if( !out_ps ){
00154      shell = XtVaAppInitialize(
00155                 &app , "AFNI" , NULL , 0 , &argc , argv , NULL , NULL ) ;
00156      if( shell == NULL ) ERROR_exit("Cannot initialize X11!") ;
00157    }
00158 
00159    cpt = my_getenv("TMPDIR") ;  /* just for fun */
00160 
00161    /*-- scan arguments that X11 didn't eat --*/
00162 
00163    iarg = 1 ;
00164    while( iarg < argc && argv[iarg][0] == '-' ){
00165 
00166      if( strcmp(argv[iarg],"-xaxis") == 0 ){   /* 22 Jul 2003 */
00167        sscanf(argv[++iarg],"%f:%f:%d:%d",&xbot,&xtop,&nnax,&mmax) ;
00168        if( xbot >= xtop || nnax < 0 || mmax < 1 )
00169          ERROR_exit("String after -xaxis is illegal!\n") ;
00170 
00171        plot_ts_xfix( nnax,mmax , xbot,xtop ) ;
00172        iarg++ ; continue ;
00173      }
00174 
00175      if( strcmp(argv[iarg],"-yaxis") == 0 ){   /* 22 Jul 2003 */
00176        sscanf(argv[++iarg],"%f:%f:%d:%d",&ybot,&ytop,&nnay,&mmay) ;
00177        if( ybot >= ytop || nnay < 0 || mmay < 1 )
00178          ERROR_exit("String after -yaxis is illegal!\n") ;
00179 
00180        plot_ts_yfix( nnay,mmay , ybot,ytop ) ;
00181        iarg++ ; continue ;
00182      }
00183 
00184      if( strcmp(argv[iarg],"-nopush") == 0 ){  /* 12 Mar 2003 */
00185        plot_ts_xypush( 0 , 0 ) ;
00186        iarg++ ; continue ;
00187      }
00188 
00189      if( strcmp(argv[iarg],"-ps") == 0 ){   /* 29 Nov 2002: already handled above */
00190         iarg++ ; continue ;
00191      }
00192 
00193      if( strcmp(argv[iarg],"-install") == 0 ){
00194         install++ ; iarg++ ; continue ;
00195      }
00196 
00197      if( strcmp(argv[iarg],"-stdin") == 0 ){  /* 01 Aug 2001 */
00198         use_stdin++ ; iarg++ ; continue ;
00199      }
00200 
00201      if( strcmp(argv[iarg],"-") == 0 ){  /* skip */
00202         iarg++ ; continue ;
00203      }
00204 
00205      if( strcmp(argv[iarg],"-ynames") == 0 ){
00206         iarg++ ;
00207         while( iarg < argc && argv[iarg][0] != '-' ){
00208            ynar[nyar++] = argv[iarg++] ;
00209         }
00210         continue ;
00211      }
00212 
00213      if( strcmp(argv[iarg],"-volreg") == 0 ){
00214         int ii ;
00215         for( ii=0 ; ii < 6 ; ii++ ) ynar[nyar++] = dfile_nar[ii] ;
00216         iarg++ ; continue ;
00217      }
00218 
00219      if( strcmp(argv[iarg],"-title") == 0 ){
00220         title = argv[++iarg] ;
00221         iarg++ ; continue ;
00222      }
00223 
00224      if( strcmp(argv[iarg],"-xlabel") == 0 ){
00225         xlabel = argv[++iarg] ;
00226         iarg++ ; continue ;
00227      }
00228 
00229      if( strcmp(argv[iarg],"-ylabel") == 0 ){
00230         ylabel = argv[++iarg] ;
00231         iarg++ ; continue ;
00232      }
00233 
00234      if( strcmp(argv[iarg],"-ignore") == 0 ){
00235         ignore = strtod( argv[++iarg] , NULL ) ;
00236         if( ignore < 0 ) ERROR_exit("Illegal -ignore value!\n") ;
00237         iarg++ ; continue ;
00238      }
00239 
00240      if( strcmp(argv[iarg],"-use") == 0 ){
00241         use = strtod( argv[++iarg] , NULL ) ;
00242         if( use < 2 ) ERROR_exit("Illegal -use value!\n") ;
00243         iarg++ ; continue ;
00244      }
00245 
00246      if( strcmp(argv[iarg],"-dx") == 0 ){
00247         dx = strtod( argv[++iarg] , NULL ) ;
00248         if( dx <= 0.0 ) ERROR_exit("Illegal -dx value!\n");
00249         iarg++ ; continue ;
00250      }
00251 
00252      if( strcmp(argv[iarg],"-xzero") == 0 ){
00253         xzero = strtod( argv[++iarg] , NULL ) ;
00254         iarg++ ; continue ;
00255      }
00256 
00257      if( strcmp(argv[iarg],"-sep") == 0 ){
00258         sep = 1 ; iarg++ ; continue ;
00259      }
00260 
00261      if( strcmp(argv[iarg],"-one") == 0 ){
00262         sep = 0 ; iarg++ ; continue ;
00263      }
00264 
00265      ERROR_exit("Unknown option: %s\n",argv[iarg]) ;
00266    }
00267 
00268    if( iarg >= argc && !use_stdin )
00269       ERROR_exit("No time series file on command line!\n") ;
00270 
00271    if( !out_ps )
00272      dc = MCW_new_DC( shell , 16 ,
00273                       DEFAULT_NCOLOVR , INIT_colovr , INIT_labovr ,
00274                       1.0 , install ) ;
00275 
00276    if( nyar > 0 ) yname = ynar ;
00277 
00278    /*-- 01 Aug 2001: read from stdin instead of a file --*/
00279 
00280 #define NLBUF 131072
00281 #define NVMAX 10000
00282    if( use_stdin ){
00283      char *lbuf , *cpt , *dpt ;
00284      int   nval ;
00285      float *val , fff ;
00286 
00287      lbuf = (char * )malloc(sizeof(char )*NLBUF) ;
00288      val  = (float *)malloc(sizeof(float)*NVMAX) ;
00289 
00290      /** 13 May 2005: modified to read up to NVMAX numbers from stdin,
00291                       rather than the fixed size array of length 9 of old **/
00292 
00293      do{               /* read lines until 1st char is non-blank and non-# */
00294        cpt = fgets(lbuf,NLBUF,stdin) ;
00295        if( cpt==NULL ) ERROR_exit("Can't read from stdin!\n");
00296        for( ii=0 ; cpt[ii] != '\0' && isspace(cpt[ii]) ; ii++ ) ; /* nada */
00297      } while( cpt[ii] == '\0' || cpt[ii] == '#' ) ;
00298 
00299      nval = 0 ; cpt = lbuf ;   /* read numbers from lbuf into val */
00300      while(1){
00301        fff = strtod(cpt,&dpt) ; if( dpt  == cpt   ) break ;
00302        val[nval++] = fff ;      if( nval == NVMAX ) break ;
00303        cpt = dpt; if( *cpt == ','  ) cpt++; if( *cpt == '\0' ) break;
00304      }
00305      if( nval < 1 )
00306        ERROR_exit("Can't read numbers from stdin!\n"
00307                   "  First line: '%-.30s'\n"       , lbuf) ;
00308 
00309      nx = nval ; ny = 1 ;
00310      far = (float *) malloc(sizeof(float)*nx) ;
00311      memcpy(far,val,sizeof(float)*nx) ;
00312      while(1){  /* read from stdin */
00313         cpt = fgets(lbuf,NLBUF,stdin) ;
00314         if( cpt == NULL ) break ;            /* done */
00315         for( ii=0 ; cpt[ii] != '\0' && isspace(cpt[ii]) ; ii++ ) ; /* nada */
00316         if( cpt[ii] == '\0' || cpt[ii] == '#' ) continue ;         /* skip */
00317         memset(val,0,sizeof(float)*nx) ;
00318 
00319         nval = 0 ; cpt = lbuf ;   /* read numbers from lbuf into val */
00320         while(1){
00321           fff = strtod(cpt,&dpt) ; if( dpt  == cpt ) break ;
00322           val[nval++] = fff ;      if( nval == nx  ) break ;
00323           cpt = dpt; if( *cpt == ','  ) cpt++; if( *cpt == '\0' ) break;
00324         }
00325         far = (float *) realloc( far , sizeof(float)*(ny+1)*nx ) ;
00326         memcpy(far+ny*nx,val,sizeof(float)*nx) ; ny++ ;
00327      }
00328      if( ny < 2 && nx < 2 )
00329        ERROR_exit("Can't read at least 2 lines from stdin\n");
00330 
00331      flim = mri_new_vol_empty( nx,ny,1 , MRI_float ) ;
00332      mri_fix_data_pointer( far , flim ) ;
00333      if( ny > 1 ){      /* more than one row ==> transpose (the usual case) */
00334        inim = mri_transpose(flim) ; mri_free(flim) ;
00335      } else {           /* only 1 row ==> am OK this way [13 May 2005] */
00336        inim = flim ;
00337      }
00338      free((void *)val); free((void *)lbuf);
00339 
00340    } else {  /*-- old code: read from a file --*/
00341              /*-- 05 Mar 2003: or more than 1 file --*/
00342 
00343      if( iarg >= argc )
00344        ERROR_exit("No input files on command line?!\n");
00345 
00346 
00347      if( iarg == argc-1 ){                 /* only 1 input file */
00348        inim = mri_read_1D( argv[iarg] ) ;
00349        if( inim == NULL )
00350          ERROR_exit("Can't read input file %s\n",argv[iarg]) ;
00351 
00352      } else {                              /* multiple inputs [05 Mar 2003] */
00353        MRI_IMARR *imar ;                   /* read them & glue into 1 image */
00354        int iarg_first=iarg, nysum=0, ii,jj,nx ;
00355        float *far,*iar ;
00356 
00357        INIT_IMARR(imar) ;
00358        for( ; iarg < argc ; iarg++ ){
00359          inim = mri_read_1D( argv[iarg] ) ;
00360          if( inim == NULL )
00361            ERROR_exit("Can't read input file %s\n",argv[iarg]) ;
00362 
00363          if( iarg == iarg_first || inim->nx < nx ) nx = inim->nx ;
00364          ADDTO_IMARR(imar,inim) ; nysum += inim->ny ;
00365        }
00366        flim = mri_new( nx,nysum, MRI_float ); far = MRI_FLOAT_PTR(flim);
00367        for( nysum=ii=0 ; ii < imar->num ; ii++ ){
00368          inim = IMARR_SUBIM(imar,ii) ; iar = MRI_FLOAT_PTR(inim) ;
00369          for( jj=0 ; jj < inim->ny ; jj++,nysum++ ){
00370            memcpy( far + nx*nysum , iar + jj*inim->nx , sizeof(float)*nx ) ;
00371          }
00372        }
00373        DESTROY_IMARR(imar) ; inim = flim ;
00374      }
00375 
00376      if( inim->nx == 1 && inim->ny > 1 ){  /* 13 May 2005 */
00377        flim = mri_transpose(inim); mri_free(inim); inim = flim;
00378      }
00379 
00380    } /* end of file input */
00381 
00382    flim = inim ;
00383    far  = MRI_FLOAT_PTR(flim) ;
00384    nx   = flim->nx ;
00385    ny   = flim->ny ;
00386 
00387    if( nx < 2 )
00388      ERROR_exit("1dplot can't plot curves only 1 point long!\n") ;
00389 
00390    /* make x axis */
00391 
00392    xar = (float *) malloc( sizeof(float) * nx ) ;
00393    for( ii=0 ; ii < nx ; ii++ ) xar[ii] = xzero + dx*ii ;
00394 
00395    /* select data to plot */
00396 
00397    nts = ny ;
00398    yar = (float **) malloc(sizeof(float *)*nts) ;
00399    for( ii=0 ; ii < ny ; ii++ ) yar[ii] = far + (ii*nx+ignore) ;
00400 
00401    nx = nx - ignore ;  /* cut off the ignored points */
00402 
00403    if( use > 1 && nx > use ) nx = use ;  /* 29 Nov 1999 */
00404 
00405    /* start X11 */
00406 
00407    if( !out_ps ){
00408      (void) XtAppAddTimeOut( app , 123 , startup_timeout_CB , NULL ) ;
00409      XtAppMainLoop(app) ;   /* never returns */
00410    }
00411 
00412    /* 29 Nov 2002: if here, output PostScript to stdout */
00413 
00414    { MEM_plotdata *mp ;
00415      int ymask = (sep) ? TSP_SEPARATE_YBOX : 0 ;
00416 
00417      mp = plot_ts_mem( nx,xar , nts,ymask,yar ,
00418                        xlabel , ylabel , title , yname ) ;
00419 
00420      memplot_to_postscript( "-" , mp ) ;
00421    }
00422 
00423    exit(0) ;
00424 }
00425 
00426 /*-----------------------------------------------------------------*/
00427 void killfunc(void *fred){ exit(0) ; }
00428 /*-----------------------------------------------------------------*/
00429 
00430 void startup_timeout_CB( XtPointer client_data , XtIntervalId *id )
00431 {
00432    int ng ;
00433 
00434    /* make graph */
00435 
00436    ng = (sep) ? (-nts) : (nts) ;
00437 
00438    plot_ts_lab( dc->display , nx , xar , ng , yar ,
00439                 xlabel , ylabel , title , yname , killfunc ) ;
00440 
00441    return ;
00442 }
 

Powered by Plone

This site conforms to the following standards: