THD_session * THD_init_session( char *sessname )
{
    THD_session            *sess ;
    XtPointer_array        *dblk_arrarr ;
    THD_datablock_array    *dblk_arr ;
    THD_3dim_dataset       *dset=NULL , *temp_dset;
    THD_3dim_dataset_array *dset_arr ;

    int ibar , idset , iview  , nds , qview=-666 ;

    ENTRY("THD_init_session") ;

    /*-- sanity check --*/

    if( sessname == NULL || strlen(sessname) == 0 || !THD_is_directory(sessname) )
        RETURN( NULL ) ;

    /*-- initialize session --*/

    sess         = myXtNew( THD_session ) ;
    sess->type   = SESSION_TYPE ;
    sess->parent = NULL ;
#ifdef DEBUG_SESSIONS
    fprintf(stderr, "blanking session\n");
#endif
    BLANK_SESSION(sess) ;  /* null out all entries */

    /* save directory name, with a trailing slash */

    MCW_strncpy( sess->sessname , sessname , THD_MAX_NAME ) ;
    iview = strlen(sess->sessname) ;
    if( sess->sessname[iview-1] != '/' ) { /* tack trailing / onto sessname */
        sess->sessname[iview]   = '/' ;
        sess->sessname[iview+1] = '\0' ;
    } else {
        iview-- ;  /* iview now points to last non-NUL character in string */
    }

    /* save last name from sessname */
#if 1
    {   char *env = my_getenv( "AFNI_SESSTRAIL" ) ;
        int tt = 0 ;
        if( env != NULL ) tt = strtol(env,NULL,10) ;
        env = THD_trailname(sess->sessname,tt) ;
        tt = 1+strlen(env) - THD_MAX_NAME ;
        if( tt < 0 ) tt = 0 ;
        strcpy( sess->lastname , env+tt ) ;
    }
#else
    for( iview-- ; iview >= 0 ; iview-- )
        if( sess->sessname[iview] == '/' ) break ;
    MCW_strncpy( sess->lastname, &(sess->sessname[iview+1]), THD_MAX_NAME ) ;
#endif

    /*-- override dataset 'view'??  [30 May 2013] --*/

    {   char *env = my_getenv("AFNI_OVERRIDE_VIEW") ;
        if( env != NULL ) {
            if( toupper(*env) == 'T' ) qview = VIEW_TALAIRACH_TYPE ;
            else if( toupper(*env) == 'O' ) qview = VIEW_ORIGINAL_TYPE  ;
            else
                WARNING_message("AFNI_OVERRIDE_VIEW=%s is not understood",env) ;
        }
    }

    /*-- read all datablocks --*/

    dblk_arrarr = THD_init_alldir_datablocks( sess->sessname ) ;

    /*-- for each datablock array ... --*/

    for( ibar=0 ; ibar < dblk_arrarr->num ; ibar++ ) {

        /*-- get the current array of datablocks --*/

        dblk_arr = (THD_datablock_array *) dblk_arrarr->ar[ibar] ;
        if( dblk_arr == NULL || dblk_arr->num <= 0 ) continue ;    /* huh? */

        /*-- convert it into an array of datasets --*/

        dset_arr = THD_array_3dim_from_block( dblk_arr ) ;
        if( dset_arr == NULL || dset_arr->num <= 0 ) continue ;

        /*-- place it into the next row of the THD_session [28 Jul 2003] --*/

        nds = sess->num_dsset ;

        if( nds >= THD_MAX_SESSION_SIZE ) {  /* bad! */
            fprintf(stderr,
                    "\n*** Session %s table overflow with dataset %s ***\n",
                    sessname , dset_arr->ar[0]->self_name) ;
            for( idset=0 ; idset < dset_arr->num ; idset++ )
                THD_delete_3dim_dataset( dset_arr->ar[idset] , False ) ;
            FREE_3DARR(dset_arr) ;
            continue ;  /* skip to next dblk_arr (ibar loop) */
        }

        /*-- put each dataset into this row in its view place --*/

        for( idset=0 ; idset < dset_arr->num ; idset++ ) {
            dset  = dset_arr->ar[idset] ;
            iview = dset->view_type ;
            if( qview >= 0 ) iview = qview ;  /* 30 May 2013 */

            if( GET_SESSION_DSET(sess,nds,iview) != NULL ) { /* should never happen */
                fprintf(stderr,
                        "\n*** Session %s has duplicate dataset views of %s ***\n",
                        sessname , dset->self_name) ;
                THD_delete_3dim_dataset( dset , False ) ;
            } else {
#ifdef DEBUG_SESSIONS
                fprintf(stderr,"\nputting datasets into initial view \n");
#endif
                SET_SESSION_DSET(dset, sess, nds, iview);  /* should always happen */
                /*        sess->dsset_xform_table[nds][iview] = dset ; */  /* should always happen */
            }
        }

        sess->num_dsset ++ ;  /* one more row */

        FREE_3DARR(dset_arr) ;

    } /* end of loop over each datablock array (ibar) */

    /*-- throw away the datablock arrays at this point --*/

    STATUS("trashing dblk_arrarr") ;

    for( ibar=0 ; ibar < dblk_arrarr->num ; ibar++ ) {
        dblk_arr = (THD_datablock_array *) dblk_arrarr->ar[ibar] ;
        FREE_DBARR( dblk_arr ) ;
    }
    FREE_XTARR( dblk_arrarr ) ;

    /*-- 29 Oct 2001: try to read .mnc "datasets" --*/

    if( !AFNI_noenv("AFNI_MINC_DATASETS") ) {
        char ename[THD_MAX_NAME] , **fn_minc , *eee ;
        int num_minc , ii ;

        STATUS("looking for MINC files") ;

        strcpy(ename,sess->sessname) ;
        strcat(ename,"*.mnc") ;
        eee = ename ;
        MCW_file_expand( 1,&eee , &num_minc,&fn_minc ) ;  /* find files */

        if( num_minc > 0 ) {                              /* got some! */
            STATUS("opening MINC files") ;
            for( ii=0 ; ii < num_minc ; ii++ ) {            /* loop over files */
                dset = THD_open_minc( fn_minc[ii] ) ;         /* try it on */
                if( !ISVALID_DSET(dset) ) continue ;          /* doesn't fit? */
                nds = sess->num_dsset ;
                if( nds >= THD_MAX_SESSION_SIZE ) {
                    fprintf(stderr,
                            "\n*** Session %s table overflow with MINC dataset %s ***\n",
                            sessname , fn_minc[ii] ) ;
                    THD_delete_3dim_dataset( dset , False ) ;
                    break ; /* out of for loop */
                }
                iview = dset->view_type ;
                SET_SESSION_DSET(dset, sess, nds, iview);
                /*         sess->dsset_xform_table[nds][iview] = dset ;*/
                sess->num_dsset ++ ;
            } /* end of loop over files */
            MCW_free_expand( num_minc , fn_minc ) ;
        } /* end of if we found MINC files */
    }

    /*-- 06 Apr 2005: try to read NIfTI-1 files [KRH and RWC] --*/

    if( !AFNI_noenv("AFNI_NIFTI_DATASETS") ) {
        char *ename[2] , **fn_nifti ;
        int num_nifti , ii ;

        STATUS("looking for NIFTI files") ;

        ename[0] = AFMALL(char, THD_MAX_NAME) ;
        ename[1] = AFMALL(char, THD_MAX_NAME) ;
        strcpy(ename[0],sess->sessname) ;
        strcat(ename[0],"*.nii") ;
        strcpy(ename[1],sess->sessname) ;
        strcat(ename[1],"*.nii.gz") ;
        MCW_file_expand( 2,ename , &num_nifti,&fn_nifti ) ;  /* find files */
        free(ename[0]) ;
        free(ename[1]) ;

        if( num_nifti > 0 ) {                              /* got some! */
            STATUS("opening NIFTI files") ;
            for( ii=0 ; ii < num_nifti ; ii++ ) {            /* loop over files */
                dset = THD_open_nifti( fn_nifti[ii] ) ;        /* try it on */
                if( !ISVALID_DSET(dset) ) continue ;           /* doesn't fit? */
                nds = sess->num_dsset ;
                if( nds >= THD_MAX_SESSION_SIZE ) {
                    fprintf(stderr,
                            "\n*** Session %s table overflow with NIfTI dataset %s ***\n",
                            sessname , fn_nifti[ii] ) ;
                    THD_delete_3dim_dataset( dset , False ) ;
                    break ; /* out of for loop */
                }
                iview = dset->view_type ;
                SET_SESSION_DSET(dset, sess, nds, iview);
                /*         sess->dsset_xform_table[nds][iview] = dset ;*/
                sess->num_dsset ++ ;
            } /* end of loop over files */
            MCW_free_expand( num_nifti , fn_nifti ) ;
        } /* end of if we found NIFTI files */
    }
Пример #2
0
int main( int argc , char *argv[] )
{
   THD_3dim_dataset *dset ;
   char *prefix ;
   int narg=1 ;

#ifndef DONT_ALLOW_MINC

WARNING_message("This program (3dMINCtoAFNI) is old, obsolete, and not maintained!") ;

   if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
      printf("Usage: 3dMINCtoAFNI [-prefix ppp] dataset.mnc\n"
             "Reads in a MINC formatted file and writes it out as an\n"
             "AFNI dataset file pair with the given prefix.  If the\n"
             "prefix option isn't used, the input filename will be\n"
             "used, after the '.mnc' is chopped off.\n"
             "\n"
             "NOTES:\n"
             "* Setting environment variable AFNI_MINC_FLOATIZE to Yes\n"
             "   will cause MINC datasets to be converted to floats on\n"
             "   input.  Otherwise, they will be kept in their 'native'\n"
             "   data type if possible, which may cause problems with\n"
             "   scaling on occasion.\n"
             "* The TR recorded in MINC files is often incorrect.  You may\n"
             "   need to fix this (or other parameters) using 3drefit.\n"
            ) ;
      PRINT_COMPILE_DATE ; exit(0) ;
   }

   if( strcmp(argv[narg],"-prefix") == 0 ){
      prefix = argv[++narg] ;
      if( !THD_filename_ok(prefix) ){
         fprintf(stderr,"** Prefix string is illegal: %s\n",prefix) ;
         exit(1) ;
      }
      narg++ ;
   } else {
      int ii ;
      prefix = strdup(argv[narg]) ;
      ii = strlen(prefix) ;
      if( ii > 5 ) prefix[ii-4] = '\0' ;
      if( !THD_filename_ok(prefix) ){
         fprintf(stderr,"** Prefix string is illegal: %s\n",prefix) ;
         exit(1) ;
      }
   }

   dset = THD_open_minc( argv[narg] ) ;
   if( dset == NULL ){
      fprintf(stderr,"** Can't open dataset %s\n",argv[narg]) ;
      exit(1) ;
   }
   if( !DSET_IS_MINC(dset) ){
      fprintf(stderr,"** Not a MINC dataset: %s\n",argv[narg]) ;
      exit(1) ;
   }
   DSET_load(dset) ; CHECK_LOAD_ERROR(dset) ;

   EDIT_dset_items( dset , ADN_prefix,prefix , ADN_none ) ;
   dset->idcode = MCW_new_idcode() ;

   dset->dblk->diskptr->storage_mode = STORAGE_BY_BRICK ;

   tross_Make_History( "3dMINCtoAFNI" , argc,argv , dset ) ;

   DSET_write(dset) ;
   fprintf(stderr,"++ Wrote dataset %s\n",DSET_BRIKNAME(dset)) ;
   exit(0) ;

#else
   ERROR_exit("3dMINCtoAFNI is no longer compiled") ;
#endif
}