1 #include "mrilib.h"
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <time.h>
5 
6 /*---------------------------------------------------------------------------*/
7 
pick_one(int nn,float * ar)8 static float pick_one( int nn , float *ar )
9 {
10    int ii ; float val ;
11 
12    while(1){
13      ii = (int)( lrand48() % nn ) ;
14      if( ar[ii] > 0.0f ){
15        val = ar[ii] ; ar[ii] = -666.0f ; return val ;
16      }
17    }
18 }
19 
20 /*---------------------------------------------------------------------------*/
21 
main(int argc,char * argv[])22 int main( int argc , char *argv[] )
23 {
24    int iarg , ii,nzs ;
25    MRI_IMAGE *gim=NULL,*tim ; float *gar ; int ngx,ngy , nphas=0 ; char *gnam ;
26    float first=0.0f , s1,s2, *g1,*g2 ;
27 
28    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
29      printf(
30       "Usage: 1dGentimes [options]\n"
31       "Generates timing files for -stim_times\n"
32       "\n"
33       "Options:\n"
34       "=======\n"
35       " -first ff  = Set first stimulus time to 'ff' seconds.\n"
36       "                [Default=0]\n"
37       " -1phase gg = Each stimulus has only 1 phase.  'gg' is\n"
38       "                the file specifying the allowable gaps\n"
39       "                between stimuli, which will be chosen\n"
40       "                randomly without replacement.  This\n"
41       "                gap file should have 1 row.\n"
42       " -2phase gg = Each stimulus has 2 phases.  'gg' is the\n"
43       "                gap file.  The first row is the gap\n"
44       "                between phase #1 and phase #2.  The\n"
45       "                second row is the gap between phase #2\n"
46       "                and the next phase #1.\n"
47       "\n"
48      ) ;
49      PRINT_COMPILE_DATE ; exit(0) ;
50    }
51 
52    iarg = 1 ;
53    while( iarg < argc ){
54 
55      if( strcmp(argv[iarg],"-first") == 0 ){
56        if( ++iarg == argc ) ERROR_exit("need argument after %s",argv[iarg-1]) ;
57        first = (float)strtod(argv[iarg],NULL) ;
58        iarg++ ; continue ;
59      }
60 
61      if( strcmp(argv[iarg],"-1phase") == 0 ){
62        if( ++iarg == argc ) ERROR_exit("need argument after %s",argv[iarg-1]) ;
63        if( gim    != NULL ) ERROR_exit("Can't specify 2 gap files!") ;
64        tim = mri_read_1D( argv[iarg] ) ; gnam = argv[iarg] ;
65        if( tim    == NULL ) ERROR_exit("Can't read gap file %s",argv[iarg]) ;
66        gim = mri_transpose(tim) ; mri_free(tim) ;
67        ngx = gim->nx ; ngy = gim->ny ; nphas = 1 ; gar = MRI_FLOAT_PTR(gim) ;
68        if( ngy > 1 )        ERROR_exit("Gap file %s doesn't have exactly 1 row",argv[iarg]) ;
69        if( ngx < 2 )        ERROR_exit("Gap file %s has too few columns",argv[iarg]) ;
70        iarg++ ; continue ;
71      }
72 
73      if( strcmp(argv[iarg],"-2phase") == 0 ){
74        if( ++iarg == argc ) ERROR_exit("need argument after %s",argv[iarg-1]) ;
75        if( gim    != NULL ) ERROR_exit("Can't specify 2 gap files!") ;
76        tim = mri_read_1D( argv[iarg] ) ; gnam = argv[iarg] ;
77        if( tim    == NULL ) ERROR_exit("Can't read gap file %s",argv[iarg]) ;
78        gim = mri_transpose(tim) ; mri_free(tim) ;
79        ngx = gim->nx ; ngy = gim->ny ; nphas = 2 ; gar = MRI_FLOAT_PTR(gim) ;
80        if( ngy != 2 )       ERROR_exit("Gap file %s doesn't have exactly 2 rows",argv[iarg]) ;
81        if( ngx < 2 )        ERROR_exit("Gap file %s has too few columns",argv[iarg]) ;
82        iarg++ ; continue ;
83      }
84 
85      ERROR_exit("Unknown option: '%s'",argv[iarg]) ;
86    }
87 
88    /*-- check for errors --*/
89 
90    if( gim == NULL ) ERROR_exit("No -1phase or -2phase option given!") ;
91 
92    for( nzs=ii=0 ; ii < gim->nvox ; ii++ ) if( gar[ii] <= 0.0f ) nzs++ ;
93    if( nzs > 0 ) ERROR_exit("Found %d non-positive values in gap file %s",nzs,gnam) ;
94 
95    /*-- do the work --*/
96 
97    srand48((long)time(NULL)+(long)getpid());
98 
99    s1 = first ;
100    switch( nphas ){
101      default: ERROR_exit("WTF?") ;
102 
103      case 1:
104        g1 = gar ;
105        printf("%g\n",s1) ;
106        for( ii=0 ; ii < ngx-1 ; ii++ ){
107          s1 += pick_one(ngx,g1) ;
108          printf("%g\n",s1) ;
109        }
110      break ;
111 
112      case 2:
113        g1 = gar ; g2 = gar + ngx ;
114        s1 = first ; s2 = s1 + pick_one(ngx,g1) ;
115        printf("%g  %g\n",s1,s2) ;
116        for( ii=0 ; ii < ngx-1 ; ii++ ){
117          s1 = s2 + pick_one(ngx,g2) ;
118          s2 = s1 + pick_one(ngx,g1) ;
119          printf("%g  %g\n",s1,s2) ;
120        }
121      break ;
122    }
123 
124    exit(0) ;
125 }
126