Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
thd_fetchdset.c
Go to the documentation of this file.00001 #include "mrilib.h"
00002
00003
00004
00005
00006
00007 THD_3dim_dataset * THD_fetch_dataset( char * url )
00008 {
00009 char *cp,*hp,*bp , *thp ;
00010 int nhp,nbp , iv ;
00011 THD_3dim_dataset * dset ;
00012 int native_order ;
00013
00014 ENTRY("THD_fetch_dset") ;
00015
00016 if( url == NULL || url[0] == '\0' ) RETURN(NULL) ;
00017
00018 if( STRING_HAS_SUFFIX(url,".hdr") ) RETURN(NULL) ;
00019
00020
00021
00022 hp = AFMALL(char, sizeof(char)*(strlen(url)+32)) ; strcpy(hp,url) ;
00023 cp = strstr(hp,".HEAD") ;
00024 if( cp == NULL &&
00025 !STRING_HAS_SUFFIX(hp,".nii") &&
00026 !STRING_HAS_SUFFIX(hp,".nii.gz") &&
00027 !STRING_HAS_SUFFIX(hp,".mnc") &&
00028 !STRING_HAS_SUFFIX(hp,".mnc.gz") ) strcat(hp,".HEAD") ;
00029
00030
00031
00032 fprintf(stderr,"\n+++ Trying to fetch %s",hp) ;
00033 nhp = NI_read_URL_tmpdir( hp , &thp ) ;
00034 if( nhp <= 0 ){ fprintf(stderr," **FAILED\n"); free(hp); RETURN(NULL); }
00035
00036
00037
00038 fprintf(stderr,": %d bytes read\n ++ Trying to initialize dataset %s\n",nhp,thp) ;
00039 THD_allow_empty_dataset(1) ;
00040 dset = THD_open_one_dataset(thp) ;
00041 if( DSET_IS_MINC(dset) || DSET_IS_NIFTI(dset) ) DSET_load(dset) ;
00042 THD_allow_empty_dataset(0) ;
00043 unlink(thp) ; free(thp) ;
00044 if( dset == NULL ){ fprintf(stderr," ** Can't decode %s\n",hp); free(hp); RETURN(NULL); }
00045
00046 if( DSET_IS_VOLUMES(dset) ){
00047 fprintf(stderr," ** Can't load %s by volumes!\n",hp); free(hp);
00048 DSET_delete(dset); RETURN(NULL);
00049 }
00050
00051 DSET_superlock(dset) ;
00052 if( DSET_IS_MINC(dset) || DSET_IS_NIFTI(dset) ) RETURN(dset) ;
00053 DSET_mallocize(dset) ;
00054
00055
00056
00057 strcpy( hp+(strlen(hp)-5) , ".BRIK.gz" ) ;
00058 fprintf(stderr," ++ Trying to fetch %s",hp) ; iochan_sleep(100) ;
00059 nbp = NI_read_URL( hp , &bp ) ;
00060 if( nbp <= 0 ){
00061 iv = strlen(hp) ; hp[iv-3] = '\0' ;
00062 fprintf(stderr," ** FAILED!\n ++ Trying to fetch %s",hp) ; iochan_sleep(100) ;
00063 nbp = NI_read_URL( hp , &bp ) ;
00064 if( nbp <= 0 ){
00065 fprintf(stderr," ** FAILED\n");
00066 free(hp); DSET_delete(dset); RETURN(NULL);
00067 }
00068 }
00069 if( nbp < dset->dblk->total_bytes ){
00070 fprintf(stderr,"\n ** Got %s but only had %d bytes, but needed %lld\n",
00071 hp,nbp,dset->dblk->total_bytes) ;
00072 free(bp) ; free(hp) ; DSET_delete(dset) ; RETURN(NULL) ;
00073 }
00074 fprintf(stderr,": %d bytes read\n",nbp) ;
00075
00076
00077
00078 for( iv=0 ; iv < DSET_NVALS(dset) ; iv++ ){
00079 mri_fix_data_pointer( bp , DBLK_BRICK(dset->dblk,iv) ) ;
00080 bp += DBLK_BRICK_BYTES(dset->dblk,iv) ;
00081 }
00082
00083
00084
00085 native_order = mri_short_order() ;
00086
00087 if( dset->dblk->diskptr->byte_order <= 0 )
00088 dset->dblk->diskptr->byte_order = native_order ;
00089
00090 if( dset->dblk->diskptr->byte_order != native_order ){
00091 for( iv=0 ; iv < DSET_NVALS(dset) ; iv++ ){
00092 switch( DBLK_BRICK_TYPE(dset->dblk,iv) ){
00093 case MRI_short:
00094 mri_swap2( DBLK_BRICK_NVOX(dset->dblk,iv) , DBLK_ARRAY(dset->dblk,iv) ) ;
00095 break ;
00096
00097 case MRI_complex:
00098 mri_swap4( 2*DBLK_BRICK_NVOX(dset->dblk,iv), DBLK_ARRAY(dset->dblk,iv)) ;
00099 break ;
00100
00101 case MRI_float:
00102 case MRI_int:
00103 mri_swap4( DBLK_BRICK_NVOX(dset->dblk,iv) , DBLK_ARRAY(dset->dblk,iv) ) ;
00104 break ;
00105 }
00106 }
00107 }
00108
00109 free(hp) ; RETURN(dset) ;
00110 }
00111
00112
00113
00114
00115
00116 MRI_IMAGE * THD_fetch_1D( char * url )
00117 {
00118 char *fname ;
00119 int nhp , ii ;
00120 MRI_IMAGE * flim ;
00121 float * far ;
00122
00123 ENTRY("THD_fetch_1D") ;
00124
00125 if( url == NULL || url[0] == '\0' ) RETURN(NULL) ;
00126
00127 fprintf(stderr,"\n+++ Trying to fetch %s",url) ;
00128 nhp = NI_read_URL_tmpdir( url , &fname ) ;
00129 if( nhp <= 0 ){ fprintf(stderr," **FAILED\n"); RETURN(NULL); }
00130 fprintf(stderr,": %d bytes read",nhp) ;
00131 flim = mri_read_1D(fname) ; unlink(fname) ; free(fname) ;
00132 if( flim != NULL ){
00133 mri_add_name( url , flim ) ; fprintf(stderr,": %dx%d file\n",flim->nx,flim->ny) ;
00134 far = MRI_FLOAT_PTR(flim) ;
00135 for( ii=0 ; ii < flim->nvox ; ii++ )
00136 if( fabs(far[ii]) >= 33333.0 ) far[ii] = WAY_BIG ;
00137 } else {
00138 fprintf(stderr," **Can't read as a .1D file!\n") ;
00139 }
00140 RETURN(flim) ;
00141 }
00142
00143
00144
00145
00146
00147
00148 XtPointer_array * THD_fetch_many_datasets( char * url )
00149 {
00150 XtPointer_array * dsar ;
00151 int nlist , i1,i2 , nh , ll , nx ;
00152 char * list ;
00153 char * tnam , * hnam , * dnam ;
00154 THD_3dim_dataset * dset ;
00155
00156 ENTRY("THD_fetch_many_datasets") ;
00157
00158 if( url == NULL || url[0] == '\0' ) RETURN(NULL) ;
00159
00160
00161
00162 hnam = (char *) malloc(sizeof(char)*(strlen(url)+16)) ;
00163 strcpy(hnam,url) ;
00164 tnam = THD_trailname( hnam , 0 ) ;
00165 if( tnam == hnam ){ free(hnam); RETURN(NULL); }
00166 *tnam = '\0' ; nh = strlen(hnam) ;
00167
00168
00169
00170 fprintf(stderr,"\n+++ Trying to fetch %s",url) ;
00171 nlist = NI_read_URL( url , &list ) ;
00172 if( nlist <= 0 ){
00173 fprintf(stderr," **FAILED\n"); free(hnam); RETURN(NULL);
00174 }
00175 fprintf(stderr,": %d bytes read\n",nlist) ;
00176
00177
00178
00179 i1 = 0 ; INIT_XTARR(dsar) ;
00180
00181 while( i1 < nlist ){
00182
00183 for( ; i1 < nlist && isspace(list[i1]) ; i1++ ) ;
00184 if( i1 >= nlist ) break ;
00185 if( list[i1] == '#' ){
00186 for( ; i1 < nlist && list[i1] != '\n' ; i1++ ) ;
00187 continue ;
00188 }
00189
00190 for( i2=i1+1 ; i2 < nlist && !isspace(list[i2]) ; i2++ ) ;
00191
00192
00193
00194 dnam = (char *)malloc(sizeof(char)*(nh+i2-i1+16)) ;
00195 strcpy(dnam,hnam);
00196 memcpy(dnam+nh,list+i1,i2-i1);
00197 dnam[nh+i2-i1] = '\0';
00198
00199 iochan_sleep(100) ;
00200 ll = strlen(dnam)-3 ; if( ll < 1 ) continue ;
00201 if( strcmp(dnam+ll,".1D")==0 ||
00202 strcmp(dnam+ll,"1Dx")==0 ||
00203 strcmp(dnam+ll,"1Dv")==0 ){
00204
00205 MRI_IMAGE * im = THD_fetch_1D( dnam ) ;
00206
00207 if( im != NULL ){
00208 ADDTO_XTARR(dsar,im) ;
00209 nx = XTARR_NUM(dsar)-1 ;
00210 XTARR_IC(dsar,nx) = IC_FLIM ;
00211 }
00212
00213 } else {
00214 dset = THD_fetch_dataset( dnam ) ;
00215 if( ISVALID_DSET(dset) ){
00216 ADDTO_XTARR(dsar,dset) ;
00217 nx = XTARR_NUM(dsar)-1 ;
00218 XTARR_IC(dsar,nx) = IC_DSET ;
00219 }
00220 }
00221 free(dnam) ;
00222
00223 i1 = i2 ;
00224 }
00225
00226
00227
00228 free(list) ; free(hnam) ;
00229 if( dsar->num == 0 ){ FREE_XTARR(dsar) ; dsar = NULL ; }
00230 RETURN(dsar) ;
00231 }