Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
thd_writeatr.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mrilib.h"
00008 #include "thd.h"
00009
00010
00011
00012 Boolean THD_write_atr( THD_datablock *blk )
00013 {
00014 THD_diskptr *dkptr ;
00015 int ia , code , ii ;
00016 ATR_any *atr_any ;
00017 Boolean good = True ;
00018 FILE *header_file ;
00019
00020 ENTRY("THD_write_atr") ;
00021
00022
00023
00024 if( ! ISVALID_DATABLOCK(blk) ) RETURN( False );
00025
00026 dkptr = blk->diskptr ;
00027 if( ! ISVALID_DISKPTR(dkptr) || strlen(dkptr->header_name) == 0 ) RETURN( False );
00028
00029 if( DBLK_IS_MINC(blk) ) RETURN( False );
00030 if( DBLK_IS_ANALYZE(blk) ) RETURN( False );
00031 if( DBLK_IS_NIFTI(blk) ) RETURN( False );
00032 if( DBLK_IS_CTFMRI(blk) ) RETURN( False );
00033 if( DBLK_IS_CTFSAM(blk) ) RETURN( False );
00034 if( DBLK_IS_MPEG(blk) ) RETURN( False );
00035
00036
00037
00038 if( AFNI_yesenv("AFNI_WRITE_NIML") ){
00039 return THD_write_nimlatr( blk ) ;
00040 }
00041
00042
00043
00044 header_file = fopen( dkptr->header_name , "w" ) ;
00045 if( header_file == NULL ){
00046 fprintf(stderr,
00047 "*** ERROR: failed to open file %s for attribute writing;\n"
00048 " - Do you have permission to write to this disk?\n"
00049 " - Is the disk full?\n" ,
00050 dkptr->header_name) ;
00051 RETURN( False );
00052 }
00053
00054 for( ia=0 ; ia < blk->natr ; ia++ ){
00055
00056 atr_any = &(blk->atr[ia]) ;
00057 if( atr_any == NULL ) continue ;
00058
00059 code = SUCCESS ;
00060 switch( atr_any->type ){
00061
00062 case ATR_FLOAT_TYPE:{
00063 ATR_float * atr_flo = (ATR_float *) atr_any ;
00064
00065 code = fprintf( header_file ,
00066 "\ntype = %s\nname = %s\ncount = %d\n" ,
00067 ATR_typestr[ATR_FLOAT_TYPE] ,
00068 atr_flo->name , atr_flo->nfl ) ;
00069
00070 if( code < 1 ){ code = FAIL ; break ; }
00071
00072 for( ii=0 ; ii < atr_flo->nfl ; ii++ ){
00073 code = fprintf( header_file , " %14.7g" , atr_flo->fl[ii] ) ;
00074 if( ii % 5 == 4 && ii < atr_flo->nfl-1 )
00075 fprintf( header_file , "\n" ) ;
00076 }
00077
00078 code = (code < 1 ) ? FAIL : SUCCESS ;
00079 }
00080 break ;
00081
00082 case ATR_INT_TYPE:{
00083 ATR_int * atr_int = (ATR_int *) atr_any ;
00084
00085 code = fprintf( header_file ,
00086 "\ntype = %s\nname = %s\ncount = %d\n" ,
00087 ATR_typestr[ATR_INT_TYPE] ,
00088 atr_int->name , atr_int->nin ) ;
00089
00090 if( code < 1 ){ code = FAIL ; break ; }
00091
00092 for( ii=0 ; ii < atr_int->nin ; ii++ ){
00093 code = fprintf( header_file , " %d" , atr_int->in[ii] ) ;
00094 if( ii % 5 == 4 && ii < atr_int->nin-1 )
00095 fprintf( header_file , "\n" ) ;
00096 }
00097
00098 code = (code < 1 ) ? FAIL : SUCCESS ;
00099 }
00100 break ;
00101
00102 case ATR_STRING_TYPE:{
00103 ATR_string * atr_str = (ATR_string *) atr_any ;
00104
00105 code = fprintf( header_file ,
00106 "\ntype = %s\nname = %s\ncount = %d\n" ,
00107 ATR_typestr[ATR_STRING_TYPE] ,
00108 atr_str->name , atr_str->nch ) ;
00109
00110
00111 if( code < 1 ){ code = FAIL ; break ; }
00112
00113 THD_zblock( atr_str->nch , atr_str->ch ) ;
00114
00115 fprintf( header_file , "'" ) ;
00116 for( ii=0 ; ii < atr_str->nch ; ii++ ){
00117 code = fprintf( header_file , "%c" , atr_str->ch[ii] ) ;
00118 }
00119
00120 code = (code < 1) ? FAIL : SUCCESS ;
00121
00122 THD_unzblock( atr_str->nch , atr_str->ch ) ;
00123 }
00124 break ;
00125
00126 }
00127
00128 good = good && (code != FAIL) ;
00129 fprintf(header_file,"\n") ;
00130
00131 }
00132
00133 if( good == False ){
00134 fprintf(stderr,
00135 "*** WARNING: error in output to attribute file %s;\n"
00136 " - Is the disk full?\n" ,
00137 dkptr->header_name) ;
00138 }
00139
00140
00141
00142 fclose( header_file ) ;
00143 RETURN( True );
00144 }
00145
00146
00147
00148 Boolean THD_write_nimlatr( THD_datablock *blk )
00149 {
00150 NI_stream ns ;
00151 NI_group *ngr ;
00152 char sname[2048] ;
00153 THD_3dim_dataset *dset ;
00154
00155 ENTRY("THD_write_nimlatr") ;
00156
00157
00158
00159 dset = (THD_3dim_dataset *)blk->parent ;
00160 if( !ISVALID_DSET(dset) ){
00161 STATUS("parent is not valid dataset!"); RETURN(False);
00162 }
00163
00164
00165
00166 ngr = THD_nimlize_dsetatr( dset ) ;
00167 if( ngr == NULL ){
00168 STATUS("can't create NIML header element!"); RETURN(False);
00169 }
00170 NI_set_attribute( ngr , "self_prefix" , blk->diskptr->prefix ) ;
00171
00172
00173
00174 sprintf(sname,"file:%s",blk->diskptr->header_name) ;
00175 ns = NI_stream_open( sname , "w" ) ;
00176 if( ns == (NI_stream)NULL ){
00177 STATUS("can't open output NIML stream!"); RETURN(False);
00178 }
00179
00180
00181
00182 STATUS("writing NIML header") ;
00183 NI_stream_writestring( ns , "<?xml version='1.0' ?>\n" ) ;
00184 NI_write_element( ns , ngr , NI_TEXT_MODE ) ;
00185 NI_stream_close( ns ) ;
00186 RETURN(True) ;
00187 }