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  

thd_opentcat.c

Go to the documentation of this file.
00001 #include "mrilib.h"
00002 
00003 /*---------------------------------------------------------------------------*/
00004 /*! Open a dataset that is an impromptu catenation of multiple dataset.      */
00005 /*---------------------------------------------------------------------------*/
00006 
00007 THD_3dim_dataset * THD_open_tcat( char *dlist )
00008 {
00009    THD_3dim_dataset *dset_out , **dset_in ;
00010    int ndset_in , dd , nerr , new_nvals ;
00011    NI_str_array *sar ;
00012 
00013 ENTRY("THD_open_tcat") ;
00014 
00015    if( dlist == NULL || *dlist == '\0' ) RETURN(NULL) ;
00016 
00017    if( strchr(dlist,' ') == NULL ){
00018      dset_out = THD_open_dataset(dlist) ; RETURN(dset_out) ;
00019    }
00020 
00021    sar = NI_decode_string_list( dlist , "~" ) ;
00022    if( sar == NULL ) RETURN(NULL) ;
00023 
00024    ndset_in = sar->num ;
00025    dset_in  = (THD_3dim_dataset **)malloc(sizeof(THD_3dim_dataset *)*sar->num) ;
00026    for( nerr=dd=0 ; dd < ndset_in ; dd++ ){
00027      dset_in[dd] = THD_open_dataset( sar->str[dd] ) ;
00028      if( dset_in[dd] == NULL ){
00029        fprintf(stderr,"** THD_open_tcat: can't open dataset %s\n",sar->str[dd]) ;
00030        nerr++ ;
00031      }
00032    }
00033    if( nerr > 0 ){
00034      for( dd=0 ; dd < ndset_in ; dd++ )
00035        if( dset_in[dd] != NULL ) DSET_delete(dset_in[dd]) ;
00036      free((void *)dset_in) ;
00037      NI_delete_str_array(sar) ;
00038      RETURN(NULL) ;
00039    }
00040    if( ndset_in == 1 ){
00041      dset_out = dset_in[0] ;
00042      free((void *)dset_in) ;
00043      NI_delete_str_array(sar) ;
00044      RETURN(dset_out) ;
00045    }
00046 
00047    for( nerr=0,dd=1 ; dd < ndset_in ; dd++ ){
00048      if( DSET_NX(dset_in[0]) != DSET_NX(dset_in[dd]) ||
00049          DSET_NY(dset_in[0]) != DSET_NY(dset_in[dd]) ||
00050          DSET_NZ(dset_in[0]) != DSET_NZ(dset_in[dd])   ){
00051        fprintf(stderr,
00052                "** THD_open_tcat: %s [%dx%dx%d] doesn't match %s [%dx%dx%d]\n",
00053                sar->str[0] ,DSET_NX(dset_in[0]) ,
00054                             DSET_NY(dset_in[0]) ,DSET_NZ(dset_in[0]) ,
00055                sar->str[dd],DSET_NX(dset_in[dd]),
00056                             DSET_NY(dset_in[dd]),DSET_NZ(dset_in[dd]) ) ;
00057        nerr++ ;
00058      } else if( !EQUIV_DATAXES(dset_in[dd]->daxes,dset_in[0]->daxes) ){
00059        fprintf(stderr,
00060                "++ THD_open_tcat: %s grid mismatch with %s\n",
00061                sar->str[0] , sar->str[dd] ) ;
00062      }
00063    }
00064    if( nerr > 0 ){
00065      for( dd=0 ; dd < ndset_in ; dd++ )
00066        if( dset_in[dd] != NULL ) DSET_delete(dset_in[dd]) ;
00067      free((void *)dset_in) ;
00068      NI_delete_str_array(sar) ;
00069      RETURN(NULL) ;
00070    }
00071 
00072    /*-- OK, start making new dataset --*/
00073 
00074    new_nvals = 0 ;
00075    for( dd=0 ; dd < ndset_in ; dd++ )
00076      new_nvals += DSET_NVALS(dset_in[dd]) ;
00077 
00078    for( dd=0 ; dd < ndset_in ; dd++ )
00079       if( DSET_TIMESTEP(dset_in[dd]) > 0.0 ) break ;  /* 1st 3D+time */
00080    if( dd == ndset_in ) dd = 0 ;
00081 
00082    dset_out = EDIT_empty_copy( dset_in[dd] ) ;
00083 
00084    EDIT_dset_items( dset_out ,
00085                       ADN_prefix    , "tcat" ,
00086                       ADN_func_type , ISANAT(dset_in[dd]) ? ANAT_EPI_TYPE
00087                                                           : FUNC_FIM_TYPE ,
00088                       ADN_ntt       , new_nvals ,
00089                       ADN_nvals     , new_nvals ,
00090                     ADN_none ) ;
00091    DSET_mallocize( dset_out ) ;
00092 
00093    /* check if we have a valid time axis; if not, make one up */
00094 
00095    if( DSET_TIMESTEP(dset_out) <= 0.0f ){
00096       float TR=1.0f , torg=0.0f , tdur=0.0f ;
00097       int tunits=UNITS_SEC_TYPE ;
00098       EDIT_dset_items( dset_out ,
00099                           ADN_tunits , tunits ,
00100                           ADN_ttdel  , TR ,
00101                           ADN_ttorg  , torg ,
00102                           ADN_ttdur  , tdur ,
00103                        ADN_none ) ;
00104    }
00105 
00106    dset_out->tcat_list = strdup( dlist ) ;
00107    dset_out->tcat_num  = ndset_in ;
00108    dset_out->tcat_len  = (int *)malloc(sizeof(int)*ndset_in) ;
00109    for( dd=0 ; dd < ndset_in ; dd++ ){
00110      dset_out->tcat_len[dd] = DSET_NVALS(dset_in[dd]) ;
00111      DSET_delete(dset_in[dd]) ;
00112    }
00113    free((void *)dset_in) ;
00114    NI_delete_str_array(sar) ;
00115 
00116 #if 0
00117 fprintf(stderr,"THD_open_tcat('%s'):",dset_out->tcat_list);
00118 for(dd=0;dd<ndset_in;dd++)fprintf(stderr," %d",dset_out->tcat_len[dd]);
00119 fprintf(stderr,"\n");
00120 #endif
00121 
00122    RETURN(dset_out) ;
00123 }
00124 
00125 /*---------------------------------------------------------------------------*/
00126 /*! Load from disk the impromptu catenation.                                 */
00127 /*---------------------------------------------------------------------------*/
00128 
00129 void THD_load_tcat( THD_datablock *dblk )
00130 {
00131    int ivout , dd , iv ;
00132    THD_3dim_dataset *dset_in , *dset_out ;
00133    NI_str_array *sar ;
00134 
00135 ENTRY("THD_load_tcat") ;
00136 
00137    if( !ISVALID_DBLK(dblk) ) EXRETURN ;
00138    dset_out = (THD_3dim_dataset *)dblk->parent ;
00139    if( !ISVALID_DSET(dset_out) ) EXRETURN ;
00140    sar = NI_decode_string_list( dset_out->tcat_list , "~" ) ;
00141    if( sar == NULL ) EXRETURN ;
00142    if( sar->num != dset_out->tcat_num ){ NI_delete_str_array(sar); EXRETURN; }
00143 
00144    ivout = 0 ;
00145    for( dd=0 ; dd < sar->num ; dd++ ){
00146      dset_in = THD_open_dataset( sar->str[dd] ) ;
00147      if( dset_in == NULL ){
00148        NI_delete_str_array(sar) ; DSET_unload(dset_out) ;
00149        EXRETURN ;
00150      }
00151      DSET_mallocize(dset_in) ; DSET_load(dset_in) ;
00152      if( !DSET_LOADED(dset_in) ){
00153        NI_delete_str_array(sar) ; DSET_unload(dset_out) ; DSET_delete(dset_in) ;
00154        EXRETURN ;
00155      }
00156 
00157      for( iv=0 ; iv < DSET_NVALS(dset_in) ; iv++ ){
00158        EDIT_substitute_brick( dset_out , ivout ,
00159                               DSET_BRICK_TYPE(dset_in,iv), DSET_ARRAY(dset_in,iv) );
00160        mri_fix_data_pointer( NULL , DSET_BRICK(dset_in,iv) ) ;
00161        EDIT_BRICK_FACTOR( dset_out , ivout , DSET_BRICK_FACTOR(dset_in,iv) ) ;
00162        ivout++ ;
00163      }
00164      DSET_delete(dset_in) ;
00165    }
00166 
00167    NI_delete_str_array(sar) ; EXRETURN ;
00168 }
 

Powered by Plone

This site conforms to the following standards: