void THD_set_dataset_attributes( THD_3dim_dataset *dset ) { THD_datablock *blk ; THD_dataxes *daxes ; THD_diskptr *dkptr ; int itemp[IFILL_DIM] , ii ; float ftemp[FFILL_DIM] ; int id , nx , ny , nz , nv , nxy , nxyz , ibr , nb ; int atrank[ATRSIZE_DATASET_RANK] , atdims[ATRSIZE_DATASET_DIMENSIONS] ; MRI_IMAGE *im ; int save_order ; THD_dmat33 tmat ; THD_dfvec3 tvec ; mat44 Tc, Tr; float angle; char name[666] ; floatvec *fv ; ENTRY("THD_set_dataset_attributes") ; /*-- sanity checks --*/ if( ! ISVALID_3DIM_DATASET(dset) || ! ISVALID_DATABLOCK(dset->dblk) || ! ISVALID_DISKPTR(dset->dblk->diskptr) ) EXRETURN ; blk = dset->dblk ; daxes = dset->daxes ; /* always used fixed daxes */ dkptr = blk->diskptr ; /******/ /****** These attributes used to be set in THD_write_3dim_dataset() *****/ /******/ /*----- write TYPESTRING attribute -----*/ THD_set_string_atr( blk , ATRNAME_TYPESTRING , DATASET_typestr[dset->type] ) ; /*----- write IDCODE attributes -----*/ THD_set_string_atr( blk , ATRNAME_IDSTRING , dset->idcode.str ) ; THD_set_string_atr( blk , ATRNAME_IDDATE , dset->idcode.date ) ; if( ! ISZERO_IDCODE(dset->anat_parent_idcode) ) THD_set_string_atr( blk, ATRNAME_IDANATPAR, dset->anat_parent_idcode.str ); else THD_erase_one_atr ( blk, ATRNAME_IDANATPAR ) ; if( ! ISZERO_IDCODE(dset->warp_parent_idcode) ) THD_set_string_atr( blk, ATRNAME_IDWARPPAR, dset->warp_parent_idcode.str ); else THD_erase_one_atr ( blk, ATRNAME_IDWARPPAR ) ; /*----- write SCENE_TYPE attribute -----*/ itemp[0] = dset->view_type ; itemp[1] = dset->func_type ; itemp[2] = dset->type ; ITFILL(3,ATRSIZE_SCENE_TYPE) ; THD_set_int_atr( blk , ATRNAME_SCENE_TYPE , ATRSIZE_SCENE_TYPE , itemp ) ; /*----- write data labels -----*/ if( strlen(dset->self_name) == 0 ) DSET_FIX_NAMES(dset) ; THD_set_string_atr( blk , ATRNAME_LABEL1 , dset->label1 ) ; THD_set_string_atr( blk , ATRNAME_LABEL2 , dset->label2 ) ; THD_set_string_atr( blk , ATRNAME_DATANAME , dset->self_name ) ; if( dset->keywords != NULL ) THD_set_string_atr( blk , ATRNAME_KEYWORDS , dset->keywords ) ; else THD_erase_one_atr ( blk , ATRNAME_KEYWORDS ) ; /*----- write parent names, if they exist -----*/ if( strlen(dset->warp_parent_name) > 0 ) THD_set_string_atr( blk , ATRNAME_WARP_PARENT , dset->warp_parent_name ) ; else THD_erase_one_atr ( blk , ATRNAME_WARP_PARENT ) ; if( strlen(dset->anat_parent_name) > 0 ) THD_set_string_atr( blk , ATRNAME_ANATOMY_PARENT , dset->anat_parent_name ) ; else THD_erase_one_atr ( blk , ATRNAME_ANATOMY_PARENT ) ; /*----- write axes orientation -----*/ itemp[0] = daxes->xxorient ; itemp[1] = daxes->yyorient ; itemp[2] = daxes->zzorient ; ITFILL(3,ATRSIZE_ORIENT_SPECIFIC) ; THD_set_int_atr( blk , ATRNAME_ORIENT_SPECIFIC , ATRSIZE_ORIENT_SPECIFIC , itemp ) ; /*----- write axes origin -----*/ ftemp[0] = daxes->xxorg ; ftemp[1] = daxes->yyorg ; ftemp[2] = daxes->zzorg ; FTFILL(3,ATRSIZE_ORIGIN) ; THD_set_float_atr( blk , ATRNAME_ORIGIN , ATRSIZE_ORIGIN , ftemp ) ; /*----- write axes spacings -----*/ ftemp[0] = daxes->xxdel ; ftemp[1] = daxes->yydel ; ftemp[2] = daxes->zzdel ; FTFILL(3,ATRSIZE_DELTA) ; THD_set_float_atr( blk , ATRNAME_DELTA , ATRSIZE_DELTA , ftemp ) ; /*-- write matrix for (i,j,k) to DICOM (x,y,z) conversion [15 Dec 2005] --*/ if( !ISVALID_MAT44(daxes->ijk_to_dicom) ) THD_daxes_to_mat44( daxes ) ; if( ISVALID_MAT44(daxes->ijk_to_dicom) ){ UNLOAD_MAT44(daxes->ijk_to_dicom, ftemp[0],ftemp[1],ftemp[2],ftemp[3], ftemp[4],ftemp[5],ftemp[6],ftemp[7], ftemp[8],ftemp[9],ftemp[10],ftemp[11] ); THD_set_float_atr( blk , "IJK_TO_DICOM" , 12 , ftemp ) ; } /*-- write matrix for (i,j,k) to DICOM real (x,y,z) conversion [18 May 2007] --*/ /* to store obliquity information */ if(!THD_update_obliquity_status()){ /* maybe update the obliquity unless refitting */ THD_check_oblique_field(dset); if (ISVALID_MAT44(dset->daxes->ijk_to_dicom_real)){ /* if not oblique already,compute Tc (Cardinal transformation matrix) */ angle = THD_compute_oblique_angle(daxes->ijk_to_dicom_real, 0); if(angle==0.0){ THD_dicom_card_xform(dset, &tmat, &tvec); LOAD_MAT44(Tc, tmat.mat[0][0], tmat.mat[0][1], tmat.mat[0][2], tvec.xyz[0], tmat.mat[1][0], tmat.mat[1][1], tmat.mat[1][2], tvec.xyz[1], tmat.mat[2][0], tmat.mat[2][1], tmat.mat[2][2], tvec.xyz[2]); daxes->ijk_to_dicom_real = Tc; } } } if( ISVALID_MAT44(daxes->ijk_to_dicom_real) ){ UNLOAD_MAT44(daxes->ijk_to_dicom_real, ftemp[0],ftemp[1],ftemp[2],ftemp[3], ftemp[4],ftemp[5],ftemp[6],ftemp[7], ftemp[8],ftemp[9],ftemp[10],ftemp[11] ); THD_set_float_atr( blk , "IJK_TO_DICOM_REAL" , 12 , ftemp ) ; } /*----- write markers, if present -----*/ if( dset->markers != NULL ){ for( ii=0 ; ii < MARKS_MAXNUM ; ii++ ){ /* put bad data in */ if( !dset->markers->valid[ii] ) /* invalid markers */ dset->markers->xyz[ii][0] = dset->markers->xyz[ii][1] = dset->markers->xyz[ii][2] = FFILL ; } THD_set_float_atr( blk , ATRNAME_MARKSXYZ , ATRSIZE_MARKSXYZ , &(dset->markers->xyz[0][0]) ) ; THD_set_char_atr( blk , ATRNAME_MARKSLAB , ATRSIZE_MARKSLAB , &(dset->markers->label[0][0]) ) ; THD_set_char_atr( blk , ATRNAME_MARKSHELP , ATRSIZE_MARKSHELP , &(dset->markers->help[0][0]) ) ; THD_set_int_atr( blk , ATRNAME_MARKSFLAG , ATRSIZE_MARKSFLAG , &(dset->markers->aflags[0]) ) ; } else { THD_erase_one_atr( blk , ATRNAME_MARKSXYZ ) ; THD_erase_one_atr( blk , ATRNAME_MARKSLAB ) ; THD_erase_one_atr( blk , ATRNAME_MARKSHELP ) ; THD_erase_one_atr( blk , ATRNAME_MARKSFLAG ) ; } /*----- write warp, if present -----*/ if( dset->warp != NULL ){ int wdata_size = 0 ; switch( dset->warp->type ){ case WARP_AFFINE_TYPE:{ THD_affine_warp *ww = (THD_affine_warp *) dset->warp ; itemp[0] = WARP_AFFINE_TYPE ; itemp[1] = ww->resam_type ; wdata_size = MAPPING_LINEAR_FSIZE ; COPY_FROM_STRUCT( ww->warp , MAPPING_LINEAR_FSTART , float , ftemp , MAPPING_LINEAR_FSIZE ) ; } break ; case WARP_TALAIRACH_12_TYPE:{ THD_talairach_12_warp *ww = (THD_talairach_12_warp *) dset->warp ; int iw , ioff ; itemp[0] = WARP_TALAIRACH_12_TYPE ; itemp[1] = ww->resam_type ; wdata_size = WARP_TALAIRACH_12_SIZE ; for( iw=0 ; iw < 12 ; iw++ ){ ioff = iw * MAPPING_LINEAR_FSIZE ; COPY_FROM_STRUCT( ww->warp[iw] , MAPPING_LINEAR_FSTART , float , &(ftemp[ioff]) , MAPPING_LINEAR_FSIZE ) ; } } break ; } /* end of switch on warp type */ ITFILL(2,ATRSIZE_WARP_TYPE) ; THD_set_int_atr( blk , ATRNAME_WARP_TYPE , ATRSIZE_WARP_TYPE , itemp ) ; THD_set_float_atr( blk , ATRNAME_WARP_DATA , wdata_size , ftemp ) ; } else { /* no warp exists */
THD_3dim_dataset * Volumewise_Operations(THD_3dim_dataset *dset, char *prefix, int datum) { int iv6[] = { 17, 0, 13, 4, 6, 8 }; float fv = 3.14159; THD_3dim_dataset *oset = NULL; int nvox, isb, ivx; float *sb=NULL, *vout=NULL, *yout=NULL; char *stmp; ENTRY("Volumewise_Operations"); DSET_load(dset); nvox = DSET_NVOX(dset); vout = (float *)calloc(nvox, sizeof(float)); /* Allocate for one whole volume, one sub-brick that is */ yout = (float *)calloc(nvox, sizeof(float)); /* and another */ for (isb=0; isb<DSET_NVALS(dset); ++isb) { if (!(sb = THD_extract_to_float(isb, dset))) { ERROR_message("Failed to extract sub-brick %d from %s\n", isb, DSET_PREFIX(dset)); RETURN(NULL); } for (ivx=0; ivx<nvox; ++ivx) { vout[ivx] += sb[ivx]; yout[ivx] += 1./(sqrt(fabs(sb[ivx])+0.1)); } free(sb); sb = NULL; } /* Put the results in a new dataset */ oset = EDIT_empty_copy( dset ) ; if (datum == MRI_float) { /* store results as float */ stmp = modify_afni_prefix(prefix,NULL,".volops.float"); EDIT_dset_items( oset , ADN_prefix , stmp, ADN_datum_all, MRI_float , ADN_nvals , 2 , ADN_ntt, 0, ADN_none ) ; EDIT_substitute_brick( oset , 0 , MRI_float , vout ) ; vout = NULL; /* do not free vout, oset uses the same pointer because column types is oset are float */ EDIT_substitute_brick( oset , 1 , MRI_float , yout ) ; yout = NULL; } else { /* store results as short */ stmp = modify_afni_prefix(prefix,NULL,".volops.short"); EDIT_dset_items( oset , ADN_prefix , stmp, ADN_datum_all, MRI_short , ADN_nvals , 2 , ADN_none ) ; /* Store the results using short precision and auto scale */ EDIT_substscale_brick(oset, 0, MRI_float, vout, MRI_short, -1.0); free(vout); vout = NULL; /* vout got copied by value, so free it */ /* Store the results using short precision and set your own scale (here out of laziness I used the same scale as for sub-brick 0) */ EDIT_substscale_brick(oset, 1, MRI_float, yout, MRI_short, DSET_BRICK_FACTOR(oset,0)); free(yout); yout = NULL; /* vout got copied by value, so free it */ } /* Label the sub-bricks */ EDIT_BRICK_LABEL(oset , 0, "sum"); EDIT_BRICK_LABEL(oset , 1, "sisqrt"); /* Add your own special attribute -- Note that private attributes are not preserved when making copies of datasets */ THD_set_string_atr( oset->dblk , "Pinot_Noir" , "sideways" ) ; THD_set_int_atr ( oset->dblk , "Lottery_Numbers", 6, iv6 ); THD_set_float_atr( oset->dblk , "PI", 1, &fv ); RETURN(oset); }