00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "io.h"
00022 #include "linalg.h"
00023
00024
00025
00032 char* read_line( FILE *f, char *line ){
00033 long curpos;
00034 int length=0;
00035 char c;
00036 int i;
00037
00038 if( line==NULL ){
00039 curpos = ftell( f );
00040 while( (c=fgetc( f ))!='\n' )
00041 length++;
00042 dprintf( "length=%i\n", length );
00043 fseek( f, curpos, SEEK_SET );
00044 line = (char*) malloc( (length+1)*sizeof(char) );
00045 }
00046 i = 0;
00047 while( (c=fgetc( f ))!='\n' ){
00048 line[i++]=c;
00049 }
00050 line[i] = '\0';
00051 return line;
00052 }
00053
00068 Array* read_matrix_from_text( const char *fname ){
00069 char buf[MAX_LINE_LENGTH];
00070 int nrows=0;
00071 int ncols=0;
00072 FILE *f;
00073
00074 if( (f=fopen( fname, "r" ))==NULL ){
00075 errprintf("couldn't open file '%s'\n", fname );
00076 return NULL;
00077 }
00078
00079 nrows=stream_count_char( f, '\n' );
00080 rewind( f );
00081 read_line( f, buf );
00082 char c,prev='1';
00083 while( (c=fgetc(f))!='\n' ){
00084 if( isblank(c) && !isblank(prev) ){
00085 ncols++;
00086 }
00087 prev=c;
00088 }
00089 rewind( f );
00090 dprintf( "matrix is (%i,%i)\n", nrows, ncols );
00091
00092 Array *a=array_new2( DOUBLE, 2, nrows, ncols );
00093 int i,j,k;
00094 for( i=0; i<nrows; i++ ){
00095 for( k=0; k<ncols; k++ ){
00096 j=0;
00097 while( !isblank((c=fgetc(f))) ){
00098 buf[j++]=c;
00099 }
00100 buf[j]='\0';
00101 dprintf("%s\n", buf );
00102 mat_IDX( a, i, k )=atof( buf );
00103 while( isblank((c=fgetc(f))) ) ;
00104 fseek( f, -1, SEEK_CUR);
00105 }
00106 }
00107 dprintf("Done\n");
00108
00109 fclose( f );
00110 return a;
00111 }
00112
00122 ChannelInfo* read_chaninfo_ced( const char *fname, ChannelInfo *chans ){
00123 int num_chans=0;
00124 FILE *f;
00125 char buf[MAX_LINE_LENGTH];
00126 int Label_idx=-1,
00127 X_idx=-1, Y_idx=-1, Z_idx=-1;
00128 char *strstart, *strend;
00129 int idx, i;
00130
00131 if( (f=fopen( fname, "r" ))==NULL ){
00132 errprintf("couldn't open file '%s'\n", fname );
00133 return NULL;
00134 }
00135
00136 read_line( f, buf );
00137 dprintf("labels='%s'\n", buf);
00138
00139
00140 strstart=buf;
00141 idx = 0;
00142 while( (strend=strchr( strstart, '\t' ))!=NULL){
00143 *strend='\0';
00144 dprintf( "curlab='%s'\n", strstart );
00145 if( !strcasecmp( strstart, "labels" ) ){
00146 Label_idx=idx;
00147 } else if( !strcasecmp( strstart, "X" ) ){
00148 X_idx=idx;
00149 } else if( !strcasecmp( strstart, "Y" ) ){
00150 Y_idx=idx;
00151 } else if( !strcasecmp( strstart, "Z" ) ){
00152 Z_idx=idx;
00153 }
00154 strstart = strend+1;
00155 idx++;
00156 }
00157 dprintf(" l=%i, X=%i, Y=%i, Z=%i\n", Label_idx, X_idx, Y_idx, Z_idx );
00158 if( X_idx<0 || Y_idx<0 || Z_idx<0 || Label_idx<0 ){
00159 errprintf( " ERROR: did not find one of the labels: X=%i, Y=%i, Z=%i, Labels=%i\n",
00160 X_idx, Y_idx, Z_idx, Label_idx );
00161 return NULL;
00162 }
00163
00164 num_chans = stream_count_char( f, '\n' );
00165 dprintf(" num_chans = %i\n", num_chans );
00166
00167 if( chans==ALLOC_IN_FCT ){
00168 chans = (ChannelInfo*) malloc( num_chans*sizeof(ChannelInfo) );
00169 } else {
00170 if( num_chans!=chans[0].num_chans ){
00171 errprintf( " ERROR: not enough memory in chans-struct\n");
00172 return NULL;
00173 }
00174 }
00175
00176 for( i=0; i<num_chans; i++ ){
00177 read_line( f, buf );
00178 strstart=buf;
00179 idx = 0;
00180 chans[i].num_chans=num_chans;
00181 chans[i].num = i+1;
00182 while( (strend=strchr( strstart, '\t' ))!=NULL){
00183 *strend='\0';
00184 if( idx==Label_idx ){
00185 strcpy( chans[i].label, strstart );
00186 } else if( idx==X_idx ){
00187 chans[i].x = atof( strstart );
00188 } else if( idx==Y_idx ){
00189 chans[i].y = atof( strstart );
00190 } else if( idx==Z_idx ){
00191 chans[i].z = atof( strstart );
00192 }
00193 strstart = strend+1;
00194 idx++;
00195 }
00196 print_channelinfo( stderr, chans+i );
00197 }
00198
00199 return chans;
00200 }
00201
00205 double** read_dblpp_ascii(const char *fname, int xdim, int ydim, double **d){
00206 FILE *f;
00207 int x,y;
00208 int flag;
00209
00210 if((f=fopen(fname, "r"))==NULL){
00211 errprintf("failed reading %s\n", fname );
00212 return NULL;
00213 }
00214
00215 if(d==NULL){
00216 d = (double**) malloc( ydim*sizeof(double*) );
00217 for( y=0; y<ydim; y++ )
00218 d[y] = (double*) malloc( xdim*sizeof(double) );
00219 }
00220
00221 for( y=0; y<ydim; y++ ){
00222 for( x=0; x<xdim; x++ ){
00223 flag=fscanf(f, " %lf ", &(d[y][x]));
00224 }
00225 }
00226 fclose(f);
00227 return d;
00228 }
00229
00232 double* read_dblp_ascii( const char *fname, int N, double *v ){
00233 FILE *f;
00234 int i,flag;
00235
00236 if((f=fopen(fname, "r"))==NULL)
00237 errormsg(ERR_IO, 1);
00238
00239 if(v==NULL){
00240 v = dblp_init( NULL, N, 0.0 );
00241 }
00242 for( i=0; i<N; i++ ){
00243 flag=fscanf(f, " %lf ", &(v[i]));
00244 }
00245 fclose( f );
00246 return v;
00247 }
00248
00249
00250
00251
00252
00258 void write_dblpp_ascii_file(const char *fname, const double **d, int xdim, int ydim, OptArgList *opts){
00259 FILE *f;
00260
00261 dprintf("opening '%s'\n", fname );
00262 if((f=fopen(fname, "w"))==NULL)
00263 errormsg(ERR_IO, 1);
00264
00265 write_dblpp_ascii(f, d, xdim, ydim, opts);
00266
00267 fclose(f);
00268 }
00269
00274 void write_dblpp_ascii(FILE *out, const double **d, int xdim, int ydim, OptArgList *opts){
00275 int x, y;
00276 int precision;
00277 char tformat[20];
00278 double a;
00279
00280 precision=6;
00281 if( optarglist_has_key( opts, "precision" ) ){
00282 a = optarglist_scalar_by_key( opts, "precision" );
00283 if( !isnan( a ) ) precision=(int)a;
00284 }
00285
00286 sprintf( tformat, "%%.%if ", precision );
00287 dprintf("Printing in format: '%s'\n", tformat);
00288
00289 for( y=0; y<ydim; y++ ){
00290 for( x=0; x<xdim; x++ ){
00291 fprintf(out, tformat, d[x][y]);
00292 }
00293 fprintf(out, "\n");
00294 }
00295 }
00296
00297
00298 void write_dblp_ascii(FILE *out, const double *v, int n){
00299 int x;
00300 for( x=0; x<n; x++ ){
00301 fprintf(out, "%f ", v[x]);
00302 }
00303 fprintf(out, "\n");
00304 }
00305
00306
00307
00308 void write_dblp_ascii_file(const char *fname, const double *v, int n){
00309 FILE *f;
00310
00311 if((f=fopen(fname, "w"))==NULL)
00312 errormsg(ERR_IO, 1);
00313
00314 write_dblp_ascii(f, v, n);
00315 fclose(f);
00316 }
00317
00318
00319
00320 void write_intp_ascii(FILE *out, const int *v, int n){
00321 int x;
00322 for( x=0; x<n; x++ ){
00323 fprintf(out, "%i ", v[x]);
00324 }
00325 fprintf(out, "\n");
00326 }
00327
00328
00329
00330 void write_intp_ascii_file(const char *fname, const int *v, int n){
00331 FILE *f;
00332
00333 if((f=fopen(fname, "w"))==NULL)
00334 errormsg(ERR_IO, 1);
00335
00336 write_intp_ascii(f, v, n);
00337 fclose(f);
00338 }