Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
3dAFNItoNIML.c
Go to the documentation of this file.00001
00002
00003
00004 #include "mrilib.h"
00005
00006 int main( int argc , char *argv[] )
00007 {
00008 THD_3dim_dataset *dset ;
00009 NI_group *ngr ;
00010 NI_stream ns_out ;
00011 int iarg=1 , dodata=0 , nout=0 ;
00012 char strname[256] = "stdout:" ;
00013
00014
00015
00016 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00017 printf(
00018 "Usage: 3dAFNItoNIML [options] dset\n"
00019 " Dumps AFNI dataset header information to stdout in NIML format.\n"
00020 " Mostly for debugging and testing purposes!\n"
00021 "\n"
00022 " OPTIONS:\n"
00023 " -data == Also put the data into the output (will be huge).\n"
00024 " -tcp:host:port == Instead of stdout, send the dataset to a socket.\n"
00025 " (implies '-data' as well)\n"
00026 "\n"
00027 "-- RWCox - Mar 2005\n"
00028 ) ;
00029 exit(0) ;
00030 }
00031
00032 mainENTRY("3dAFNItoNIML main"); machdep(); PRINT_VERSION("3dAFNItoNIML");
00033 if( PRINT_TRACING ){ STATUS("Enable mcw_malloc()"); enable_mcw_malloc(); }
00034
00035
00036
00037 while( iarg < argc && argv[iarg][0] == '-' ){
00038
00039 if( strcmp(argv[iarg],"-data") == 0 ){
00040 dodata++ ; iarg++ ; continue ;
00041 }
00042
00043 if( strncmp(argv[iarg],"-tcp:",5) == 0 ){
00044 strcpy(strname,argv[iarg]+1) ; dodata++ ; iarg++ ; continue ;
00045 }
00046
00047 fprintf(stderr,"** Illegal option: %s\n",argv[iarg]); exit(1);
00048 }
00049
00050 if( iarg >= argc ){
00051 fprintf(stderr,"** Not enough arguments on command line!\n"); exit(1);
00052 }
00053
00054
00055
00056 dset = THD_open_dataset( argv[iarg++] ) ;
00057 if( dset == NULL ){
00058 fprintf(stderr,"** Can't open dataset %s\n",argv[iarg-1]); exit(1);
00059 }
00060
00061
00062
00063 if( dodata ){
00064 DSET_load(dset) ;
00065 if( !DSET_LOADED(dset) ){
00066 fprintf(stderr,"** Can't load dataset %s\n",argv[iarg-1]); exit(1);
00067 }
00068 }
00069
00070 switch( dodata ){
00071 default:
00072 case 1:
00073 ngr = THD_dataset_to_niml( dset ) ;
00074 DSET_unload(dset) ;
00075 if( ngr == NULL ){
00076 fprintf(stderr,"** Can't do THD_dataset_to_niml()\n"); exit(1);
00077 }
00078 break ;
00079
00080 case 0:
00081 case 2:
00082 ngr = THD_nimlize_dsetatr( dset ) ;
00083 if( ngr == NULL ){
00084 fprintf(stderr,"** Can't do THD_nimlize_dsetatr()\n"); exit(1);
00085 }
00086 break ;
00087
00088 case 3:
00089 ngr = NULL ;
00090 break ;
00091 }
00092
00093
00094
00095 ns_out = NI_stream_open( strname , "w" ) ;
00096 if( ns_out == NULL ){
00097 fprintf(stderr,"** Can't create NIML stream!?\n"); exit(1);
00098 }
00099
00100
00101
00102 if( strcmp(strname,"stdout:") != 0 ){
00103 int nn , nchk ;
00104 for( nchk=0 ; nchk < 99 ; nchk++ ){
00105 nn = NI_stream_writecheck( ns_out , 777 ) ;
00106 if( nn == 1 ){ if(nchk>0)fprintf(stderr,"\n"); break; }
00107 if( nn < 0 ){ fprintf(stderr,"** NIML stream failure!?\n"); exit(1); }
00108 if( nchk==0 ){ fprintf(stderr,"Waiting"); }
00109 fprintf(stderr,".") ;
00110 }
00111 nn = NI_stream_writecheck( ns_out , 1 ) ;
00112 if( nn <= 0 ){ fprintf(stderr,"** Can't connect!?\n"); exit(1); }
00113
00114 if( dodata > 1 ) nout += NI_write_procins( ns_out , "keep_reading" ) ;
00115 }
00116
00117
00118
00119 if( ngr != NULL ){
00120 NI_rename_group( ngr , "AFNI_dataset" ) ;
00121 NI_set_attribute( ngr , "self_prefix" , DSET_PREFIX(dset) ) ;
00122 nout += NI_write_element( ns_out , ngr , NI_TEXT_MODE ) ;
00123 NI_free_element( ngr ) ;
00124 }
00125
00126
00127
00128 if( dodata > 1 ){
00129 int iv ; NI_element *nel ;
00130 for( iv=0 ; iv < DSET_NVALS(dset) ; iv++ ){
00131 nel = THD_subbrick_to_niml( dset , iv , SBFLAG_INDEX ) ;
00132 if( nel != NULL ) nout += NI_write_element( ns_out , nel , NI_BINARY_MODE ) ;
00133 NI_free_element(nel) ;
00134 }
00135 }
00136
00137
00138
00139 NI_stream_closenow( ns_out ) ;
00140 fprintf(stderr,"++ Wrote %d bytes\n",nout) ;
00141 exit(0) ;
00142 }