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_initsess.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "mrilib.h"
00008 #include "thd.h"
00009 
00010 /*---------------------------------------------------------------------
00011    Given a directory name, read in all the datasets and make
00012    a session data structure from them.
00013 
00014    [28 Jul 2003] Modified to use new THD_session struct, wherein all
00015                  datasets are in one array (no anat/func distinction).
00016 -----------------------------------------------------------------------*/
00017 
00018 THD_session * THD_init_session( char * sessname )
00019 {
00020    THD_session            * sess ;
00021    XtPointer_array        * dblk_arrarr ;
00022    THD_datablock_array    * dblk_arr ;
00023    THD_3dim_dataset       * dset ;
00024    THD_3dim_dataset_array * dset_arr ;
00025 
00026    int ibar , idset , iview  , nds ;
00027 
00028 ENTRY("THD_init_session") ;
00029 
00030    /*-- sanity check --*/
00031 
00032    if( sessname == NULL || strlen(sessname) == 0 || !THD_is_directory(sessname) )
00033      RETURN( NULL ) ;
00034 
00035    /*-- initialize session --*/
00036 
00037    sess         = myXtNew( THD_session ) ;
00038    sess->type   = SESSION_TYPE ;
00039    sess->parent = NULL ;
00040    BLANK_SESSION(sess) ;  /* null out all entries */
00041 
00042    /* save directory name, with a trailing slash */
00043 
00044    MCW_strncpy( sess->sessname , sessname , THD_MAX_NAME ) ;
00045    iview = strlen(sess->sessname) ;
00046    if( sess->sessname[iview-1] != '/' ){  /* tack trailing / onto sessname */
00047      sess->sessname[iview]   = '/' ;
00048      sess->sessname[iview+1] = '\0' ;
00049    } else {
00050      iview-- ;  /* iview now points to last non-NUL character in string */
00051    }
00052 
00053    /* save last name from sessname */
00054 #if 1
00055    { char * env = my_getenv( "AFNI_SESSTRAIL" ) ; int tt = 0 ;
00056      if( env != NULL ) tt = strtol(env,NULL,10) ;
00057      env = THD_trailname(sess->sessname,tt) ;
00058      tt = 1+strlen(env) - THD_MAX_NAME ; if( tt < 0 ) tt = 0 ;
00059      strcpy( sess->lastname , env+tt ) ;
00060    }
00061 #else
00062      for( iview-- ; iview >= 0 ; iview-- )
00063        if( sess->sessname[iview] == '/' ) break ;
00064      MCW_strncpy( sess->lastname, &(sess->sessname[iview+1]), THD_MAX_NAME ) ;
00065 #endif
00066 
00067    /*-- read all datablocks --*/
00068 
00069    dblk_arrarr = THD_init_alldir_datablocks( sess->sessname ) ;
00070 
00071    /*-- for each datablock array ... --*/
00072 
00073    for( ibar=0 ; ibar < dblk_arrarr->num ; ibar++ ){
00074 
00075       /*-- get the current array of datablocks --*/
00076 
00077       dblk_arr = (THD_datablock_array *) dblk_arrarr->ar[ibar] ;
00078       if( dblk_arr == NULL || dblk_arr->num <= 0 ) continue ;    /* huh? */
00079 
00080       /*-- convert it into an array of datasets --*/
00081 
00082       dset_arr = THD_array_3dim_from_block( dblk_arr ) ;
00083       if( dset_arr == NULL || dset_arr->num <= 0 ) continue ;
00084 
00085       /*-- place it into the next row of the THD_session [28 Jul 2003] --*/
00086 
00087       nds = sess->num_dsset ;
00088 
00089       if( nds >= THD_MAX_SESSION_SIZE ){   /* bad! */
00090         fprintf(stderr,
00091          "\n*** Session %s table overflow with dataset %s ***\n",
00092              sessname , dset_arr->ar[0]->self_name) ;
00093         for( idset=0 ; idset < dset_arr->num ; idset++ )
00094           THD_delete_3dim_dataset( dset_arr->ar[idset] , False ) ;
00095         FREE_3DARR(dset_arr) ;
00096         continue ;  /* skip to next dblk_arr (ibar loop) */
00097       }
00098 
00099       /*-- put each dataset into this row in its view place --*/
00100 
00101       for( idset=0 ; idset < dset_arr->num ; idset++ ){
00102         dset  = dset_arr->ar[idset] ;
00103         iview = dset->view_type ;
00104 
00105         if( sess->dsset[nds][iview] != NULL ){  /* should never happen */
00106           fprintf(stderr,
00107            "\n*** Session %s has duplicate dataset views of %s ***\n",
00108            sessname , dset->self_name) ;
00109           THD_delete_3dim_dataset( dset , False ) ;
00110         } else {
00111           sess->dsset[nds][iview] = dset ;       /* should always happen */
00112         }
00113       }
00114 
00115       sess->num_dsset ++ ;  /* one more row */
00116 
00117       FREE_3DARR(dset_arr) ;
00118 
00119    } /* end of loop over each datablock array (ibar) */
00120 
00121    /*-- throw away the datablock arrays at this point --*/
00122 
00123    STATUS("trashing dblk_arrarr") ;
00124 
00125    for( ibar=0 ; ibar < dblk_arrarr->num ; ibar++ ){
00126      dblk_arr = (THD_datablock_array *) dblk_arrarr->ar[ibar] ;
00127      FREE_DBARR( dblk_arr ) ;
00128    }
00129    FREE_XTARR( dblk_arrarr ) ;
00130 
00131    /*-- 29 Oct 2001: try to read .mnc "datasets" --*/
00132 
00133    if( !AFNI_noenv("AFNI_MINC_DATASETS") ){
00134      char ename[THD_MAX_NAME] , **fn_minc , *eee ;
00135      int num_minc , ii ;
00136 
00137      STATUS("looking for MINC files") ;
00138 
00139      strcpy(ename,sess->sessname) ; strcat(ename,"*.mnc") ;
00140      eee = ename ;
00141      MCW_file_expand( 1,&eee , &num_minc,&fn_minc ) ;  /* find files */
00142 
00143      if( num_minc > 0 ){                               /* got some! */
00144        STATUS("opening MINC files") ;
00145        for( ii=0 ; ii < num_minc ; ii++ ){             /* loop over files */
00146          dset = THD_open_minc( fn_minc[ii] ) ;         /* try it on */
00147          if( !ISVALID_DSET(dset) ) continue ;          /* doesn't fit? */
00148          nds = sess->num_dsset ;
00149          if( nds >= THD_MAX_SESSION_SIZE ){
00150            fprintf(stderr,
00151              "\n*** Session %s table overflow with MINC dataset %s ***\n",
00152              sessname , fn_minc[ii] ) ;
00153            THD_delete_3dim_dataset( dset , False ) ;
00154            break ; /* out of for loop */
00155          }
00156          iview = dset->view_type ;
00157          sess->dsset[nds][iview] = dset ;
00158          sess->num_dsset ++ ;
00159        } /* end of loop over files */
00160        MCW_free_expand( num_minc , fn_minc ) ;
00161      } /* end of if we found MINC files */
00162    }
00163 
00164    /*-- 06 Apr 2005: try to read NIfTI-1 files [KRH and RWC] --*/
00165 
00166    if( !AFNI_noenv("AFNI_NIFTI_DATASETS") ){
00167      char *ename[2] , **fn_nifti ;
00168      int num_nifti , ii ;
00169 
00170      STATUS("looking for NIFTI files") ;
00171 
00172      ename[0] = AFMALL(char, THD_MAX_NAME) ;
00173      ename[1] = AFMALL(char, THD_MAX_NAME) ;
00174      strcpy(ename[0],sess->sessname) ; strcat(ename[0],"*.nii") ;
00175      strcpy(ename[1],sess->sessname) ; strcat(ename[1],"*.nii.gz") ;
00176      MCW_file_expand( 2,ename , &num_nifti,&fn_nifti ) ;  /* find files */
00177      free(ename[0]) ; free(ename[1]) ;
00178 
00179      if( num_nifti > 0 ){                               /* got some! */
00180        STATUS("opening NIFTI files") ;
00181        for( ii=0 ; ii < num_nifti ; ii++ ){             /* loop over files */
00182          dset = THD_open_nifti( fn_nifti[ii] ) ;        /* try it on */
00183          if( !ISVALID_DSET(dset) ) continue ;           /* doesn't fit? */
00184          nds = sess->num_dsset ;
00185          if( nds >= THD_MAX_SESSION_SIZE ){
00186            fprintf(stderr,
00187              "\n*** Session %s table overflow with NIfTI dataset %s ***\n",
00188              sessname , fn_nifti[ii] ) ;
00189            THD_delete_3dim_dataset( dset , False ) ;
00190            break ; /* out of for loop */
00191          }
00192          iview = dset->view_type ;
00193          sess->dsset[nds][iview] = dset ;
00194          sess->num_dsset ++ ;
00195        } /* end of loop over files */
00196        MCW_free_expand( num_nifti , fn_nifti ) ;
00197      } /* end of if we found NIFTI files */
00198    }
00199 
00200    /*-- 27 Aug 2002: try to read any ANALYZE "datasets" here --*/
00201 
00202    if( !AFNI_noenv("AFNI_ANALYZE_DATASETS") ){
00203      char *ename[2] , **fn_anlz ;
00204      int num_anlz , ii , nee ;
00205 #ifdef ALLOW_FSL_FEAT
00206      int feat_exf=-1 , feat_hrs=-1 , feat_std=-1 ;
00207      int feat_nds_start=sess->num_dsset ;
00208 #endif
00209 
00210      STATUS("looking for ANALYZE files") ;
00211 
00212      ename[0] = AFMALL(char, THD_MAX_NAME) ;
00213      strcpy(ename[0],sess->sessname) ; strcat(ename[0],"*.hdr") ;
00214      nee = 1 ;
00215 #ifdef ALLOW_FSL_FEAT
00216      ename[1] = AFMALL(char, THD_MAX_NAME) ;
00217      strcpy(ename[1],sess->sessname) ; strcat(ename[1],"stats/*stat*.hdr") ;
00218      nee++ ;
00219 #endif
00220      MCW_file_expand( nee,ename , &num_anlz,&fn_anlz ) ;  /* find files */
00221      for( ii=0 ; ii < nee ; ii++ ) free(ename[ii]) ;
00222 
00223      if( num_anlz > 0 ){                               /* got some! */
00224        STATUS("opening ANALYZE files") ;
00225        for( ii=0 ; ii < num_anlz ; ii++ ){             /* loop over files */
00226          dset = THD_open_analyze( fn_anlz[ii] ) ;      /* try it on */
00227          if( !ISVALID_DSET(dset) ) continue ;          /* doesn't fit? */
00228          nds = sess->num_dsset ;
00229          if( nds >= THD_MAX_SESSION_SIZE ){
00230            fprintf(stderr,
00231              "\n*** Session %s table overflow with ANALYZE dataset %s ***\n",
00232              sessname , fn_anlz[ii] ) ;
00233            THD_delete_3dim_dataset( dset , False ) ;
00234            break ; /* out of for loop */
00235         }
00236         iview = dset->view_type ;
00237         sess->dsset[nds][iview] = dset ;
00238         sess->num_dsset ++ ;
00239 #ifdef ALLOW_FSL_FEAT
00240              if( strcmp(DSET_PREFIX(dset),"example_func.hdr") == 0 ) feat_exf = nds;
00241         else if( strcmp(DSET_PREFIX(dset),"highres.hdr")      == 0 ) feat_hrs = nds;
00242         else if( strcmp(DSET_PREFIX(dset),"standard.hdr")     == 0 ) feat_std = nds;
00243 #endif
00244        } /* end of loop over files */
00245 
00246        MCW_free_expand( num_anlz , fn_anlz ) ;  /* free file list */
00247 
00248        /* now read in linear mappings (.mat files) for FSL FEAT */
00249 
00250 #ifdef ALLOW_FSL_FEAT
00251        if( feat_exf >= 0                        &&
00252            sess->num_dsset - feat_nds_start > 0 &&
00253            (feat_hrs >= 0 || feat_std >= 0)       ){  /* found FEAT files */
00254 
00255          THD_3dim_dataset *dset_exf=NULL, *dset_hrs=NULL, *dset_std=NULL ;
00256          char fnam[THD_MAX_NAME] ;
00257          FILE *fp ;
00258          float a11,a12,a13,s1 , a21,a22,a23,s2 , a31,a32,a33,s3 ;
00259          THD_warp *warp_exf_hrs=NULL , *warp_exf_std=NULL , *warp_std_hrs=NULL ;
00260 
00261                              dset_exf = sess->dsset[feat_exf][0] ;  /* Must have this. */
00262          if( feat_hrs >= 0 ) dset_hrs = sess->dsset[feat_hrs][0] ;  /* And at least   */
00263          if( feat_std >= 0 ) dset_std = sess->dsset[feat_std][0] ;  /* one of these. */
00264 
00265          /* try to read the warp from example_func (EPI) to standard */
00266 
00267          if( dset_std != NULL ){
00268            strcpy(fnam,sess->sessname) ; strcat(fnam,"example_func2standard.mat") ;
00269            fp = fopen(fnam,"r") ;
00270            if( fp != NULL ){
00271              ii = fscanf(fp,"%f%f%f%f %f%f%f%f %f%f%f%f" ,
00272                          &a11,&a12,&a13,&s1 , &a21,&a22,&a23,&s2 ,
00273                                               &a31,&a32,&a33,&s3   ) ;
00274              if( ii == 12 )
00275                warp_exf_std = AFNI_make_affwarp_12( a11,a12,a13,s1 ,
00276                                                     a21,a22,a23,s2 ,
00277                                                     a31,a32,a33,s3   ) ;
00278              fclose(fp) ;
00279            }
00280 
00281            /* 28 Aug 2002:
00282                (i)  correct orientation of example_func
00283                (ii) correct warp for non-DICOM order of coords in datasets      */
00284 
00285            if( warp_exf_std != NULL ){
00286              THD_mat33 mmm,nnn ;
00287              int   ix , iy , iz ;
00288              float ax , ay , az ;
00289 
00290 #if 0
00291 printf("warp_exf_std BEFORE:") ; DUMP_LMAP(warp_exf_std->rig_bod.warp) ;
00292 #endif
00293 
00294 #undef FIX_EXF
00295 #ifdef FIX_EXF
00296              /* make matrix that transforms standard to example_func */
00297 
00298              LOAD_MAT(mmm,a11,a12,a13,a21,a22,a23,a31,a32,a33) ;
00299              nnn = MAT_INV(mmm) ;
00300              UNLOAD_MAT(nnn,a11,a12,a13,a21,a22,a23,a31,a32,a33) ;
00301 
00302              /* for each for, find index and value of largest element */
00303 
00304              ix = 1 ; ax = a11 ;
00305              if( fabs(a12) > fabs(ax) ){ ix = 2 ; ax = a12 ; }
00306              if( fabs(a13) > fabs(ax) ){ ix = 3 ; ax = a13 ; }
00307 
00308              iy = 1 ; ay = a21 ;
00309              if( fabs(a22) > fabs(ay) ){ iy = 2 ; ay = a22 ; }
00310              if( fabs(a23) > fabs(ay) ){ iy = 3 ; ay = a23 ; }
00311 
00312              iz = 1 ; az = a31 ;
00313              if( fabs(a32) > fabs(az) ){ iz = 2 ; az = a32 ; }
00314              if( fabs(a33) > fabs(az) ){ iz = 3 ; az = a33 ; }
00315 #else
00316              ix = 1 ; iy = 2 ; iz = 3 ;
00317 #endif
00318 
00319              if( ix+iy+iz == 6 ){  /* (ix,iy,iz) must be a permutation of (1,2,3) */
00320                THD_ivec3 orixyz ;
00321                THD_fvec3 dxyz ;
00322                THD_dataxes *daxes = dset_exf->daxes ;
00323                THD_warp *from_exf , *to_std ;
00324 
00325 #ifdef FIX_EXF
00326                /** fix orientation of dset_exf dataset **/
00327 
00328                switch(ix){
00329                  case 1:    /* example_func x-axis is mainly same
00330                                as standard x-axis (ax>0) or its opposite (ax<0) */
00331                    if( ax > 0 ) ix = dset_std->daxes->xxorient ;
00332                    else         ix = ORIENT_OPPOSITE(dset_std->daxes->xxorient) ;
00333                  break ;
00334 
00335                  case 2:    /* example_func x-axis is mainly same
00336                                as standard y-axis (ax>0) or its opposite (ax<0) */
00337                    if( ax > 0 ) ix = dset_std->daxes->yyorient ;
00338                    else         ix = ORIENT_OPPOSITE(dset_std->daxes->yyorient) ;
00339                  break ;
00340 
00341                  case 3:    /* example_func x-axis is mainly same
00342                                as standard z-axis (ax>0) or its opposite (ax<0) */
00343                    if( ax > 0 ) ix = dset_std->daxes->zzorient ;
00344                    else         ix = ORIENT_OPPOSITE(dset_std->daxes->zzorient) ;
00345                  break ;
00346                }
00347                switch(iy){
00348                  case 1: if( ay > 0 ) iy = dset_std->daxes->xxorient ;
00349                          else         iy = ORIENT_OPPOSITE(dset_std->daxes->xxorient) ;
00350                  break ;
00351                  case 2: if( ay > 0 ) iy = dset_std->daxes->yyorient ;
00352                          else         iy = ORIENT_OPPOSITE(dset_std->daxes->yyorient) ;
00353                  break ;
00354                  case 3: if( ay > 0 ) iy = dset_std->daxes->zzorient ;
00355                          else         iy = ORIENT_OPPOSITE(dset_std->daxes->zzorient) ;
00356                  break ;
00357                }
00358                switch(iz){
00359                  case 1: if( az > 0 ) iz = dset_std->daxes->xxorient ;
00360                          else         iz = ORIENT_OPPOSITE(dset_std->daxes->xxorient) ;
00361                  break ;
00362                  case 2: if( az > 0 ) iz = dset_std->daxes->yyorient ;
00363                          else         iz = ORIENT_OPPOSITE(dset_std->daxes->yyorient) ;
00364                  break ;
00365                  case 3: if( az > 0 ) iz = dset_std->daxes->zzorient ;
00366                          else         iz = ORIENT_OPPOSITE(dset_std->daxes->zzorient) ;
00367                  break ;
00368                }
00369                orixyz.ijk[0] = ix ; orixyz.ijk[1] = iy ; orixyz.ijk[2] = iz ;
00370                dxyz.xyz[0] = fabs(daxes->xxdel) ;
00371                dxyz.xyz[1] = fabs(daxes->yydel) ;
00372                dxyz.xyz[2] = fabs(daxes->zzdel) ;
00373                if( ORIENT_sign[ix] == '-' ) dxyz.xyz[0] = -dxyz.xyz[0] ;
00374                if( ORIENT_sign[iy] == '-' ) dxyz.xyz[1] = -dxyz.xyz[1] ;
00375                if( ORIENT_sign[iz] == '-' ) dxyz.xyz[2] = -dxyz.xyz[2] ;
00376                EDIT_dset_items( dset_exf , ADN_xyzorient,orixyz, ADN_xyzdel,dxyz, ADN_none ) ;
00377 #endif
00378 
00379                /** now fix warp from example_func to standard to do it in DICOM coords **/
00380 
00381                nnn = SNGL_mat_to_dicomm( dset_exf ) ; mmm = TRANSPOSE_MAT(nnn) ;
00382                from_exf = AFNI_make_affwarp_mat( mmm ) ;
00383                nnn = SNGL_mat_to_dicomm( dset_std ) ;
00384                to_std = AFNI_make_affwarp_mat( nnn ) ;
00385                AFNI_concatenate_warp( warp_exf_std , from_exf ) ;
00386                AFNI_concatenate_warp( to_std , warp_exf_std ) ;
00387                myXtFree(warp_exf_std) ; myXtFree(from_exf) ;
00388                warp_exf_std = to_std ;
00389 
00390 #if 0
00391 printf("warp_exf_std AFTER:") ; DUMP_LMAP(warp_exf_std->rig_bod.warp) ;
00392 #endif
00393 
00394              } /* end of if warp had reasonable axis combinations */
00395            } /* end of if we got a good warp */
00396          }
00397 
00398          /* try to read the warp from example_func (EPI) to highres */
00399 
00400          if( dset_hrs != NULL ){
00401            strcpy(fnam,sess->sessname) ; strcat(fnam,"example_func2highres.mat") ;
00402            fp = fopen(fnam,"r") ;
00403            if( fp != NULL ){
00404              ii = fscanf(fp,"%f%f%f%f %f%f%f%f %f%f%f%f" ,
00405                          &a11,&a12,&a13,&s1 , &a21,&a22,&a23,&s2 ,
00406                                               &a31,&a32,&a33,&s3   ) ;
00407              if( ii == 12 )
00408                warp_exf_hrs = AFNI_make_affwarp_12( a11,a12,a13,s1 ,
00409                                                     a21,a22,a23,s2 ,
00410                                                     a31,a32,a33,s3   ) ;
00411              fclose(fp) ;
00412            }
00413 
00414            /* 28 Aug 2002: correct warp for non-DICOM order of coords in datasets */
00415 
00416            if( warp_exf_hrs != NULL ){
00417              THD_mat33 mmm,nnn ;
00418              THD_warp *from_exf , *to_hrs ;
00419 
00420 #if 0
00421 printf("warp_exf_hrs BEFORE:") ; DUMP_LMAP(warp_exf_hrs->rig_bod.warp) ;
00422 #endif
00423 
00424              nnn = SNGL_mat_to_dicomm( dset_exf ) ; mmm = TRANSPOSE_MAT(nnn) ;
00425              from_exf = AFNI_make_affwarp_mat( mmm ) ;
00426              nnn = SNGL_mat_to_dicomm( dset_hrs ) ;
00427              to_hrs = AFNI_make_affwarp_mat( nnn ) ;
00428              AFNI_concatenate_warp( warp_exf_hrs , from_exf ) ;
00429              AFNI_concatenate_warp( to_hrs , warp_exf_hrs ) ;
00430              myXtFree(warp_exf_hrs) ; myXtFree(from_exf) ;
00431              warp_exf_hrs = to_hrs ;
00432 
00433 #if 0
00434 printf("warp_exf_hrs AFTER:") ; DUMP_LMAP(warp_exf_hrs->rig_bod.warp) ;
00435 #endif
00436            }
00437          }
00438 
00439          /* try to read the warp from standard to highres */
00440 
00441          if( dset_hrs != NULL && dset_std != NULL ){
00442            strcpy(fnam,sess->sessname) ; strcat(fnam,"standard2highres.mat") ;
00443            fp = fopen(fnam,"r") ;
00444            if( fp != NULL ){
00445              ii = fscanf(fp,"%f%f%f%f %f%f%f%f %f%f%f%f" ,
00446                          &a11,&a12,&a13,&s1 , &a21,&a22,&a23,&s2 ,
00447                                               &a31,&a32,&a33,&s3   ) ;
00448              if( ii == 12 )
00449                warp_std_hrs = AFNI_make_affwarp_12( a11,a12,a13,s1 ,
00450                                                     a21,a22,a23,s2 ,
00451                                                     a31,a32,a33,s3   ) ;
00452              fclose(fp) ;
00453            }
00454 
00455            /* 28 Aug 2002: correct warp for non-DICOM order of coords in datasets */
00456 
00457            if( warp_std_hrs != NULL ){
00458              THD_mat33 mmm,nnn ;
00459              THD_warp *from_std , *to_hrs ;
00460 
00461 #if 0
00462 printf("warp_std_hrs BEFORE:") ; DUMP_LMAP(warp_std_hrs->rig_bod.warp) ;
00463 #endif
00464 
00465              nnn = SNGL_mat_to_dicomm( dset_std ) ; mmm = TRANSPOSE_MAT(nnn) ;
00466              from_std = AFNI_make_affwarp_mat( mmm ) ;
00467              nnn = SNGL_mat_to_dicomm( dset_hrs ) ;
00468              to_hrs = AFNI_make_affwarp_mat( nnn ) ;
00469              AFNI_concatenate_warp( warp_std_hrs , from_std ) ;
00470              AFNI_concatenate_warp( to_hrs , warp_std_hrs ) ;
00471              myXtFree(warp_std_hrs) ; myXtFree(from_std) ;
00472              warp_std_hrs = to_hrs ;
00473 
00474 #if 0
00475 printf("warp_std_hrs AFTER:") ; DUMP_LMAP(warp_std_hrs->rig_bod.warp) ;
00476 #endif
00477            }
00478          }
00479 
00480          /* if we have these warps,
00481             then build a hashtable describing their use in
00482             transforming funcs (in exf coords) to hrs and std coords */
00483 
00484          if( warp_exf_hrs != NULL || warp_exf_std != NULL ){
00485 
00486            if( sess->warptable == NULL )
00487               sess->warptable = new_Htable(0) ;  /* use minimum table size */
00488 
00489            if( warp_exf_hrs != NULL ){
00490              for( ii=feat_nds_start ; ii < sess->num_dsset ; ii++ ){
00491                if( ISFUNC(sess->dsset[ii][0]) ){
00492                  sprintf(fnam,"%s,%s",dset_hrs->idcode.str,sess->dsset[ii][0]->idcode.str) ;
00493                  addto_Htable( fnam , warp_exf_hrs , sess->warptable ) ;
00494                }
00495              }
00496              for( ii=feat_nds_start ; ii < sess->num_dsset ; ii++ ){
00497                if( ii != feat_hrs && ii != feat_std && ISANAT(sess->dsset[ii][0]) ){
00498                  sprintf(fnam,"%s,%s",dset_hrs->idcode.str,sess->dsset[ii][0]->idcode.str) ;
00499                  addto_Htable( fnam , warp_exf_hrs , sess->warptable ) ;
00500                }
00501              }
00502            }
00503 
00504            if( warp_exf_std != NULL ){
00505              for( ii=feat_nds_start ; ii < sess->num_dsset ; ii++ ){
00506                if( ISFUNC(sess->dsset[ii][0]) ){
00507                  sprintf(fnam,"%s,%s",dset_std->idcode.str,sess->dsset[ii][0]->idcode.str) ;
00508                  addto_Htable( fnam , warp_exf_std , sess->warptable ) ;
00509                }
00510              }
00511              for( ii=feat_nds_start ; ii < sess->num_dsset ; ii++ ){
00512                if( ii != feat_hrs && ii != feat_std && ISANAT(sess->dsset[ii][0]) ){
00513                  sprintf(fnam,"%s,%s",dset_std->idcode.str,sess->dsset[ii][0]->idcode.str) ;
00514                  addto_Htable( fnam , warp_exf_std , sess->warptable ) ;
00515                }
00516              }
00517            }
00518 
00519            if( warp_std_hrs != NULL ){
00520              sprintf(fnam,"%s,%s",dset_hrs->idcode.str,dset_std->idcode.str) ;
00521              addto_Htable( fnam , warp_std_hrs , sess->warptable ) ;
00522            }
00523 
00524          } /* end of making warptable */
00525        } /* end of FEATing */
00526 #endif
00527 
00528      } /* end of if we found ANALYZE files */
00529    }
00530 
00531    /*-- 04 Dec 2002: try to read CTF .mri and .svl "datasets" --*/
00532 
00533    if( !AFNI_noenv("AFNI_CTF_DATASETS") ){
00534      char *ename[2] , **fn_ctf ;
00535      int num_ctf , ii ;
00536 
00537      STATUS("looking for CTF files") ;
00538 
00539      ename[0] = AFMALL(char, THD_MAX_NAME) ;
00540      ename[1] = AFMALL(char, THD_MAX_NAME) ;
00541      strcpy(ename[0],sess->sessname) ; strcat(ename[0],"*.mri") ;
00542      strcpy(ename[1],sess->sessname) ; strcat(ename[1],"*.svl") ;
00543      MCW_file_expand( 2,ename , &num_ctf,&fn_ctf ) ;  /* find files */
00544      free(ename[0]) ; free(ename[1]) ;
00545 
00546      if( num_ctf > 0 ){                               /* got some files! */
00547        STATUS("opening CTF files") ;
00548        for( ii=0 ; ii < num_ctf ; ii++ ){             /* loop over files */
00549 
00550          if( strstr(fn_ctf[ii],".mri") != NULL )      /* try to read: */
00551            dset = THD_open_ctfmri( fn_ctf[ii] ) ;     /*   as MRI */
00552          else if( strstr(fn_ctf[ii],".svl") != NULL )
00553            dset = THD_open_ctfsam( fn_ctf[ii] ) ;     /*   as SAM */
00554 
00555          if( !ISVALID_DSET(dset) ) continue ;         /* doesn't read? */
00556          nds = sess->num_dsset ;
00557          if( nds >= THD_MAX_SESSION_SIZE ){
00558            fprintf(stderr,
00559             "\n*** Session %s table overflow with dataset %s ***\n",
00560             sessname , fn_ctf[ii] ) ;
00561            THD_delete_3dim_dataset( dset , False ) ;
00562            break ; /* out of for loop */
00563          }
00564          iview = dset->view_type ;
00565          sess->dsset[nds][iview] = dset ;
00566          sess->num_dsset ++ ;
00567        } /* end of loop over files */
00568        MCW_free_expand( num_ctf , fn_ctf ) ;
00569      } /* end of if we found CTF files */
00570    }
00571 
00572    /*-- 03 Dec 2001: try to read MPEG "datasets" --*/
00573 
00574    if( !AFNI_noenv("AFNI_MPEG_DATASETS") ){
00575      char ename[4*THD_MAX_NAME+64] , **fn_mpeg ;
00576      int num_mpeg , ii ;
00577 
00578      STATUS("looking for MPEG files") ;
00579 
00580      sprintf(ename,"%s*.mpg %s*.mpeg %s*.MPEG %s*.MPG" , 
00581              sess->sessname, sess->sessname, sess->sessname, sess->sessname ) ;
00582      MCW_wildcards( ename , &num_mpeg , &fn_mpeg ) ;   /* find files */
00583 
00584      if( num_mpeg > 0 ){                               /* got some! */
00585        STATUS("opening MPEG files") ;
00586        for( ii=0 ; ii < num_mpeg ; ii++ ){             /* loop over files */
00587          dset = THD_open_mpeg( fn_mpeg[ii] ) ;         /* try it on */
00588          if( !ISVALID_DSET(dset) ) continue ;          /* doesn't fit? */
00589          nds = sess->num_dsset ;
00590          if( nds >= THD_MAX_SESSION_SIZE ){
00591            fprintf(stderr,
00592              "\n*** Session %s table overflow with MPEG dataset %s ***\n",
00593              sessname , fn_mpeg[ii] ) ;
00594            THD_delete_3dim_dataset( dset , False ) ;
00595            break ; /* out of for loop */
00596          }
00597          iview = dset->view_type ;
00598          sess->dsset[nds][iview] = dset ;
00599          sess->num_dsset ++ ;
00600        } /* end of loop over files */
00601        MCW_free_expand( num_mpeg , fn_mpeg ) ;
00602      } /* end of if we found MPEG files */
00603    }
00604 
00605    /*-- done! --*/
00606 
00607    if( sess->num_dsset == 0 ){
00608       myXtFree( sess ) ; RETURN( NULL ) ;
00609    }
00610 
00611    /*-- 29 Jul 2003: order dataset rows --*/
00612 
00613    THD_order_session( sess ) ;
00614 
00615    RETURN( sess ) ;
00616 }
00617 
00618 /*-------------------------------------------------------------------------*/
00619 /*! Order the datasets within a session (anats first, funcs last).
00620 ---------------------------------------------------------------------------*/
00621 
00622 void THD_order_session( THD_session *sess )
00623 {
00624    THD_3dim_dataset *qset[THD_MAX_SESSION_SIZE][LAST_VIEW_TYPE+1] ;
00625    THD_3dim_dataset *dset ;
00626    int iview , ids , nds ;
00627 
00628 ENTRY("THD_order_session") ;
00629    if( sess == NULL || sess->num_dsset <= 1 ) EXRETURN ;
00630 
00631    /* put anats into qset */
00632 
00633    nds = 0 ;
00634    for( ids=0 ; ids < sess->num_dsset ; ids++ ){
00635      for( iview=0 ; iview <= LAST_VIEW_TYPE ; iview++ ){
00636        dset = sess->dsset[ids][iview] ;
00637        if( dset != NULL && ISANAT(dset) ) break ;
00638      }
00639      if( iview <= LAST_VIEW_TYPE ){
00640        for( iview=0 ; iview <= LAST_VIEW_TYPE ; iview++ )
00641          qset[nds][iview] = sess->dsset[ids][iview] ;
00642        nds++ ;
00643      }
00644    }
00645 
00646    /* put funcs into qset */
00647 
00648    for( ids=0 ; ids < sess->num_dsset ; ids++ ){
00649      for( iview=0 ; iview <= LAST_VIEW_TYPE ; iview++ ){
00650        dset = sess->dsset[ids][iview] ;
00651        if( dset != NULL && ISFUNC(dset) ) break ;
00652      }
00653      if( iview <= LAST_VIEW_TYPE ){
00654        for( iview=0 ; iview <= LAST_VIEW_TYPE ; iview++ )
00655          qset[nds][iview] = sess->dsset[ids][iview] ;
00656        nds++ ;
00657      }
00658    }
00659 
00660    /* copy qset back into sess, overwriting dsset */
00661 
00662    for( ids=0 ; ids < nds ; ids++ )
00663      for( iview=0 ; iview <= LAST_VIEW_TYPE ; iview++ )
00664        sess->dsset[ids][iview] = qset[ids][iview] ;
00665 
00666    sess->num_dsset = nds ;  /* shouldn't change */
00667    EXRETURN ;
00668 }
 

Powered by Plone

This site conforms to the following standards: