Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
dsetdup.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 #include "mrilib.h"
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 static char DUP_session[THD_MAX_NAME]  = "./" ;
00020 static char DUP_prefix[THD_MAX_PREFIX] = "dup" ;
00021 static char DUP_label[THD_MAX_LABEL]   = "\0" ;
00022 static char DUP_dname[THD_MAX_NAME]    = "\0" ;
00023 
00024 static int anatomy_type  = ILLEGAL_TYPE ;
00025 static int function_type = ILLEGAL_TYPE ;
00026 static int dataset_type  = ILLEGAL_TYPE ;
00027 
00028 THD_3dim_dataset * duplicate_dataset( THD_3dim_dataset * parent ) ;
00029 
00030 int main( int argc , char * argv[] )
00031 {
00032    int nopt , ii ;
00033    THD_3dim_dataset * dset_in , * dset_out ;
00034 
00035 
00036 
00037    if( argc < 2 || strncmp(argv[1],"-help",3) == 0 ){
00038       printf(
00039        "Usage: dsetdup [options] dataset\n"
00040        " 'Duplicates' a dataset by making a warp-on-demand copy.\n"
00041        " Applications:\n"
00042        "   - allows AFNI to resample a dataset to a new grid without\n"
00043        "       destroying an existing data .BRIK\n"
00044        "   - change a functional dataset to anatomical, or vice-versa\n"
00045        "\n"
00046        "OPTIONS:\n"
00047        "  -type             = convert to the given 'type', which must be\n"
00048        "                       chosen from the same list as in to3d\n"
00049        "  -session dirname  = write output into given directory (default=./)\n"
00050        "  -prefix  pname    = use 'pname' for the output directory prefix\n"
00051        "                       (default=dup)\n"
00052        "  -label   string   = use 'string' for the label in the output\n"
00053        "                       dataset (default = pname)\n"
00054        "  -dname   name     = will make 3D dataset's name = 'name'\n"
00055        "\n"
00056        "N.B.: Even if the new dataset is anatomical, it will not contain\n"
00057        "      any markers, duplicated from the original, or otherwise.\n"
00058      ) ;
00059      exit(0) ;
00060    }
00061 
00062 
00063 
00064 #define DUPERR(str) \
00065    do{ fprintf(stderr,"ERROR: %s\n",(str)) ; exit(1) ; } while(1)
00066 
00067    nopt = 1 ;
00068    while( nopt < argc && argv[nopt][0] == '-' ){
00069 
00070       
00071 
00072       
00073 
00074       for( ii=FIRST_ANAT_TYPE ; ii <= LAST_ANAT_TYPE ; ii++ )
00075          if( strncmp( &(argv[nopt][1]) ,
00076                       ANAT_prefixstr[ii] , THD_MAX_PREFIX ) == 0 ) break ;
00077 
00078       if( ii <= LAST_ANAT_TYPE ){
00079          anatomy_type = ii ;
00080          dataset_type = HEAD_ANAT_TYPE ;
00081          nopt++ ; continue ;
00082       }
00083 
00084       
00085 
00086       for( ii=FIRST_FUNC_TYPE ; ii <= LAST_FUNC_TYPE ; ii++ )
00087          if( strncmp( &(argv[nopt][1]) ,
00088                       FUNC_prefixstr[ii] , THD_MAX_PREFIX ) == 0 ) break ;
00089 
00090       if( ii <= LAST_FUNC_TYPE ){
00091          function_type = ii ;
00092          dataset_type  = HEAD_FUNC_TYPE ;
00093          nopt++ ; continue ;
00094       }
00095 
00096       
00097 
00098       if( strncmp(argv[nopt],"-session",6) == 0 ){
00099          nopt++ ;
00100          if( nopt >= argc ) DUPERR("need argument after -session!") ;
00101          MCW_strncpy( DUP_session , argv[nopt++] , THD_MAX_NAME ) ;
00102          continue ;
00103       }
00104 
00105       
00106 
00107       if( strncmp(argv[nopt],"-prefix",6) == 0 ){
00108          nopt++ ;
00109          if( nopt >= argc ) DUPERR("need argument after -prefix!") ;
00110          MCW_strncpy( DUP_prefix , argv[nopt++] , THD_MAX_PREFIX ) ;
00111          continue ;
00112       }
00113 
00114       
00115 
00116       if( strncmp(argv[nopt],"-label",6) == 0 ){
00117          nopt++ ;
00118          if( nopt >= argc ) DUPERR("need argument after -label!") ;
00119          MCW_strncpy( DUP_label , argv[nopt++] , THD_MAX_LABEL ) ;
00120          continue ;
00121       }
00122 
00123       
00124 
00125       if( strncmp(argv[nopt],"-dname",6) == 0 ){
00126          nopt++ ;
00127          if( nopt >= argc ) DUPERR("need argument after -dname!") ;
00128          MCW_strncpy( DUP_dname , argv[nopt++] , THD_MAX_NAME ) ;
00129          continue ;
00130       }
00131 
00132       
00133 
00134       fprintf(stderr,"*** unrecognized option %s\n",argv[nopt]) ;
00135       exit(1) ;
00136    }  
00137 
00138    if( strlen(DUP_label) == 0 ){
00139       MCW_strncpy(DUP_label,DUP_prefix,THD_MAX_LABEL) ;
00140    }
00141 
00142    if( nopt >= argc ) DUPERR("no dataset name given!") ;
00143 
00144    
00145 
00146    dset_in = THD_open_one_dataset( argv[nopt] ) ;
00147    if( ! ISVALID_3DIM_DATASET(dset_in) ) DUPERR("cannot read dataset!\n") ;
00148 
00149    
00150 
00151    dset_out = duplicate_dataset( dset_in ) ;
00152    if( ! ISVALID_3DIM_DATASET(dset_out) ) DUPERR("cannot make duplicate!\n") ;
00153 
00154    
00155 
00156    strcpy( dset_out->label1 , DUP_label ) ;
00157    if( strlen(DUP_dname) > 0 ) strcpy( dset_out->self_name , DUP_dname ) ;
00158 
00159    
00160 
00161    if( dataset_type >= FIRST_3DIM_TYPE && dataset_type <= LAST_3DIM_TYPE ){
00162 
00163       int isfunc , new_nvals ;
00164 
00165       isfunc    = ISFUNCTYPE(dataset_type) ;
00166       new_nvals = (isfunc) ? FUNC_nvals[function_type]
00167                            : ANAT_nvals[anatomy_type]  ;
00168 
00169       if( new_nvals > dset_in->dblk->nvals ){
00170          fprintf(stderr,
00171                  "ERROR: new dataset type has %d values per voxel, but old has %d!\n"
00172                  "       ==> cannot make duplicate!\n" ,
00173                  new_nvals , dset_in->dblk->nvals ) ;
00174          exit(1) ;
00175 
00176       } else if( new_nvals < dset_in->dblk->nvals ){
00177          fprintf(stderr,
00178                  "WARNING: new dataset type has %d values per voxel, but old has %d!\n"
00179                  "         ==> new dataset will not access all data in old!\n",
00180                  new_nvals , dset_in->dblk->nvals ) ;
00181       }
00182 
00183       dset_out->type      = dataset_type ;
00184       dset_out->func_type = ISANAT(dset_out) ? (anatomy_type)
00185                                              : (function_type) ;
00186    }
00187 
00188    
00189 
00190    THD_write_3dim_dataset( DUP_session , DUP_prefix , dset_out , False ) ;
00191 
00192    exit(0) ;
00193 }
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 THD_3dim_dataset * duplicate_dataset( THD_3dim_dataset * parent )
00202 {
00203    THD_3dim_dataset * new_dset ;
00204    int ii ;
00205 
00206    
00207 
00208    if( ! ISVALID_3DIM_DATASET(parent) ) return NULL ;
00209 
00210    
00211 
00212    new_dset = myXtNew( THD_3dim_dataset ) ; INIT_KILL( new_dset->kl ) ;
00213 
00214    new_dset->type      = parent->type ;
00215    new_dset->func_type = parent->func_type ;
00216    new_dset->view_type = parent->view_type ;
00217 
00218    new_dset->anat_parent         = NULL ;   
00219    new_dset->anat_parent_name[0] = '\0' ;
00220 
00221    new_dset->warp_parent =  parent ;        
00222    MCW_strncpy( new_dset->warp_parent_name , parent->self_name , THD_MAX_NAME ) ;
00223 
00224    
00225 
00226    new_dset->vox_warp       = myXtNew( THD_warp ) ;
00227    new_dset->vox_warp->type = ILLEGAL_TYPE ;        
00228    new_dset->warp           = myXtNew( THD_warp ) ;
00229    *(new_dset->warp)        = IDENTITY_WARP ;
00230 
00231    
00232 
00233    MCW_strncpy( new_dset->self_name  , parent->self_name , THD_MAX_NAME-5 ) ;
00234    ii = strlen( new_dset->self_name ) ;
00235    MCW_strncpy( &(new_dset->self_name[ii]) , "%duplicate" , THD_MAX_NAME-ii ) ;
00236 
00237    MCW_strncpy( new_dset->label1 , parent->label1 , THD_MAX_LABEL ) ;
00238    MCW_strncpy( new_dset->label2 , parent->label2 , THD_MAX_LABEL ) ;
00239 
00240    
00241 
00242 
00243    new_dset->daxes         = myXtNew( THD_dataxes ) ;  
00244    *(new_dset->daxes)      = *(parent->daxes) ;      
00245    new_dset->daxes->parent = (XtPointer) new_dset ;  
00246 
00247    new_dset->wod_daxes     = myXtNew( THD_dataxes ) ;  
00248    *(new_dset->wod_daxes)  = *(new_dset->daxes) ;
00249    new_dset->wod_flag      = True ;
00250 
00251    
00252 
00253 
00254    new_dset->dblk = myXtNew( THD_datablock ) ; INIT_KILL( new_dset->dblk->kl ) ;
00255 
00256    new_dset->dblk->type        = DATABLOCK_TYPE ;
00257    new_dset->dblk->nvals       = parent->dblk->nvals ;
00258    new_dset->dblk->brick       = NULL ;
00259    new_dset->dblk->malloc_type = DATABLOCK_MEM_UNDEFINED ;
00260    new_dset->dblk->total_bytes = 0 ;
00261    new_dset->dblk->brick_bytes = 0 ;
00262    new_dset->dblk->natr        = new_dset->dblk->natr_alloc  = 0 ;
00263    new_dset->dblk->atr         = NULL ;
00264    new_dset->dblk->parent      = (XtPointer) new_dset ;
00265 
00266    DBLK_unlock(new_dset->dblk) ;
00267 
00268    new_dset->dblk->diskptr               = myXtNew( THD_diskptr ) ;
00269    new_dset->dblk->diskptr->type         = DISKPTR_TYPE ;
00270    new_dset->dblk->diskptr->nvals        = parent->dblk->nvals ;
00271    new_dset->dblk->diskptr->rank         = 3 ;
00272    new_dset->dblk->diskptr->storage_mode = STORAGE_UNDEFINED ;
00273    new_dset->dblk->diskptr->byte_order   = THD_get_write_order() ;  
00274    new_dset->dblk->diskptr->dimsizes[0]  = new_dset->daxes->nxx ;
00275    new_dset->dblk->diskptr->dimsizes[1]  = new_dset->daxes->nyy ;
00276    new_dset->dblk->diskptr->dimsizes[2]  = new_dset->daxes->nzz ;
00277 
00278    
00279 
00280 
00281    THD_init_diskptr_names( new_dset->dblk->diskptr ,
00282                            parent->dblk->diskptr->directory_name , NULL ,
00283                            parent->dblk->diskptr->prefix ,
00284                            new_dset->view_type , True ) ;
00285 
00286    ADDTO_KILL( new_dset->dblk->kl , new_dset->dblk->diskptr ) ;
00287 
00288    
00289 
00290 
00291    ADDTO_KILL( new_dset->kl , new_dset->warp ) ;
00292    ADDTO_KILL( new_dset->kl , new_dset->vox_warp ) ;
00293    ADDTO_KILL( new_dset->kl , new_dset->daxes ) ;
00294    ADDTO_KILL( new_dset->kl , new_dset->wod_daxes ) ;
00295    ADDTO_KILL( new_dset->kl , new_dset->dblk ) ;
00296 
00297    new_dset->self_warp = NULL ;  
00298 
00299    if( parent->stats != NULL ){
00300       new_dset->stats         = myXtNew( THD_statistics ) ;  
00301       *(new_dset->stats)      = *(parent->stats) ;         
00302       new_dset->stats->parent = (XtPointer) new_dset ;
00303       ADDTO_KILL( new_dset->kl , new_dset->stats ) ;
00304    } else {
00305       new_dset->stats = NULL ;
00306    }
00307 
00308    new_dset->markers     = NULL ;  
00309    new_dset->death_mark  = 0 ;     
00310    new_dset->tcat_list   = 0 ;
00311    new_dset->tcat_num    = 0 ;
00312    new_dset->tcat_len    = NULL ;
00313 
00314    return(new_dset) ;
00315 }