Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
3ddup.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 
00022 static int anatomy_type  = ILLEGAL_TYPE ;
00023 static int function_type = ILLEGAL_TYPE ;
00024 static int dataset_type  = ILLEGAL_TYPE ;
00025 
00026 THD_3dim_dataset * duplicate_dataset( THD_3dim_dataset * parent ) ;
00027 
00028 int main( int argc , char * argv[] )
00029 {
00030    int nopt , ii ;
00031    THD_3dim_dataset * dset_in , * dset_out ;
00032    THD_warp * warp , * twarp ;
00033 
00034 
00035 
00036    if( argc < 2 || strncmp(argv[1],"-help",3) == 0 ){
00037       printf(
00038        "Usage: 3ddup [options] dataset\n"
00039        " 'Duplicates' a 3D dataset by making a warp-on-demand copy.\n"
00040        " Applications:\n"
00041        "   - allows AFNI to resample a dataset to a new grid without\n"
00042        "       destroying an existing data .BRIK\n"
00043        "   - change a functional dataset to anatomical, or vice-versa\n"
00044        "\n"
00045        "OPTIONS:\n"
00046        "  -'type'           = Convert to the given 'type', which must be\n"
00047        "                       chosen from the same list as in to3d\n"
00048        "  -session dirname  = Write output into given directory (default=./)\n"
00049        "  -prefix  pname    = Use 'pname' for the output directory prefix\n"
00050        "                       (default=dup)\n"
00051        "N.B.: Even if the new dataset is anatomical, it will not contain\n"
00052        "      any markers, duplicated from the original, or otherwise.\n"
00053      ) ;
00054      exit(0) ;
00055    }
00056 
00057 
00058 
00059 #define DUPERR(str) \
00060    do{ fprintf(stderr,"ERROR: %s\n",(str)) ; exit(1) ; } while(0)
00061 
00062    nopt = 1 ;
00063    while( nopt < argc && argv[nopt][0] == '-' ){
00064 
00065       
00066 
00067       
00068 
00069       for( ii=FIRST_ANAT_TYPE ; ii <= LAST_ANAT_TYPE ; ii++ )
00070          if( strncmp( &(argv[nopt][1]) ,
00071                       ANAT_prefixstr[ii] , THD_MAX_PREFIX ) == 0 ) break ;
00072 
00073       if( ii <= LAST_ANAT_TYPE ){
00074          anatomy_type = ii ;
00075          dataset_type = HEAD_ANAT_TYPE ;
00076          nopt++ ; continue ;
00077       }
00078 
00079       
00080 
00081       for( ii=FIRST_FUNC_TYPE ; ii <= LAST_FUNC_TYPE ; ii++ )
00082          if( strncmp( &(argv[nopt][1]) ,
00083                       FUNC_prefixstr[ii] , THD_MAX_PREFIX ) == 0 ) break ;
00084 
00085       if( ii <= LAST_FUNC_TYPE ){
00086          function_type = ii ;
00087          dataset_type  = HEAD_FUNC_TYPE ;
00088          nopt++ ; continue ;
00089       }
00090 
00091       
00092 
00093       if( strncmp(argv[nopt],"-session",6) == 0 ){
00094          nopt++ ;
00095          if( nopt >= argc ) DUPERR("need argument after -session!") ;
00096          MCW_strncpy( DUP_session , argv[nopt++] , THD_MAX_NAME ) ;
00097          continue ;
00098       }
00099 
00100       
00101 
00102       if( strncmp(argv[nopt],"-prefix",6) == 0 ){
00103          nopt++ ;
00104          if( nopt >= argc ) DUPERR("need argument after -prefix!") ;
00105          MCW_strncpy( DUP_prefix , argv[nopt++] , THD_MAX_PREFIX ) ;
00106          continue ;
00107       }
00108 
00109       
00110 
00111       fprintf(stderr,"*** unrecognized option %s\n",argv[nopt]) ;
00112       exit(1) ;
00113    }  
00114 
00115    if( nopt >= argc ) DUPERR("no input dataset name given!") ;
00116 
00117    
00118 
00119    dset_in = THD_open_one_dataset( argv[nopt] ) ;
00120    if( ! ISVALID_3DIM_DATASET(dset_in) ) DUPERR("cannot read dataset!\n") ;
00121 
00122    
00123 
00124    dset_out = EDIT_empty_copy( dset_in ) ;
00125    if( !ISVALID_3DIM_DATASET(dset_out) ) DUPERR("duplication fails!\n");
00126 
00127    EDIT_dset_items( dset_out ,
00128                       ADN_prefix         , DUP_prefix ,
00129                       ADN_label1         , DUP_prefix ,   
00130                       ADN_directory_name , DUP_session ,
00131                       ADN_self_name      , DUP_prefix ,
00132                     ADN_none ) ;
00133 
00134    tross_Copy_History( dset_in , dset_out ) ;
00135    tross_Make_History( "3ddup" , argc , argv , dset_out ) ;
00136 
00137    
00138 
00139    if( dataset_type>=FIRST_3DIM_TYPE && dataset_type<=LAST_3DIM_TYPE ){
00140 
00141       int isfunc , new_nvals , old_ntimes ;
00142 
00143       old_ntimes = DSET_NUM_TIMES(dset_in) ;
00144 
00145       isfunc    = ISFUNCTYPE(dataset_type) ;
00146       new_nvals = (isfunc) ? FUNC_nvals[function_type]
00147                            : ANAT_nvals[anatomy_type]  ;
00148 
00149       if( ( isfunc && function_type == FUNC_BUCK_TYPE) ||
00150           (!isfunc && anatomy_type  == ANAT_BUCK_TYPE)   )  
00151          new_nvals = dset_in->dblk->nvals ;
00152       
00153 
00154       if( new_nvals > dset_in->dblk->nvals ){
00155          fprintf(stderr,
00156                  "ERROR: new dataset type has %d values per voxel,"
00157                  " but old has %d!\n"
00158                  "  ==> cannot make duplicate!\n" ,
00159                  new_nvals , dset_in->dblk->nvals ) ;
00160          exit(1) ;
00161 
00162       } else if( new_nvals < dset_in->dblk->nvals ){
00163          if( old_ntimes == 1 )
00164             fprintf(stderr,
00165                  "WARNING: new dataset type has %d values per voxel,"
00166                  " but old has %d!\n"
00167                  "  ==> new dataset will not access all data in old!\n",
00168                  new_nvals , dset_in->dblk->nvals ) ;
00169          else if( new_nvals > 1){
00170             fprintf(stderr,
00171                  "ERROR: new dataset type has %d values per voxel per time,"
00172                  " but old has %d!\n"
00173                  "  *** this conversion is not legal for time-dependent data!\n",
00174                  new_nvals , DSET_NVALS_PER_TIME(dset_in) ) ;
00175             exit(1) ;
00176          }
00177       }
00178 
00179       EDIT_dset_items( dset_out ,
00180                           ADN_type , dataset_type ,
00181                           ADN_func_type ,
00182                           ISANATTYPE(dataset_type) ? (anatomy_type)
00183                                                    : (function_type) ,
00184                        ADN_none ) ;
00185    }
00186 
00187    warp = myXtNew( THD_warp ) ; *warp = IDENTITY_WARP ;
00188 
00189    EDIT_dset_items( dset_out ,
00190                        ADN_warp        , warp    ,
00191                        ADN_warp_parent , dset_in ,
00192                     ADN_none ) ;
00193 
00194    
00195 
00196    THD_write_3dim_dataset( NULL , NULL , dset_out , False ) ;
00197 
00198    exit(0) ;
00199 }