Menu

[09bd5b]: / src / io.c  Maximize  Restore  History

Download this file

345 lines (291 with data), 8.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/***************************************************************************
* Copyright (C) 2010 by Matthias Ihrke *
* ihrke@nld.ds.mpg.de
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <string.h>
#include <ctype.h>
#include "definitions.h"
#include "helper.h"
#include "io.h"
#include "linalg.h"
#include "mathadd.h"
/* ------------- READER --------------------- */
/** reads from FILE* until '\n' and puts it into line.
'\0' is added to line.
\param f
\param line empty string (long enough), or NULL ->own memory
\return NULL if error, else ptr to line
*/
char* read_line( FILE *f, char *line ){
long curpos;
int length=0;
char c;
int i;
if( line==NULL ){ /* find out how much memory is needed */
curpos = ftell( f );
while( (c=fgetc( f ))!='\n' )
length++;
dprintf( "length=%i\n", length );
fseek( f, curpos, SEEK_SET );
line = (char*) malloc( (length+1)*sizeof(char) );
}
i = 0;
while( (c=fgetc( f ))!='\n' ){
line[i++]=c;
}
line[i] = '\0';
return line;
}
/**\brief Read a matrix from blank-separated file.
The format is something like this:
\verbatim
-0.005344 0.696318 0.005792 589.041211
-0.005348 0.691603 0.007035 589.074254
-0.005352 0.686796 0.008324 589.096248
-0.005516 0.680181 0.010164 589.118214
...
\endverbatim
\param fname the name of the file
\return the matrix
*/
Array* read_matrix_from_text( const char *fname ){
char buf[MAX_LINE_LENGTH];
int nrows=0;
int ncols=0;
FILE *f;
if( (f=fopen( fname, "r" ))==NULL ){
errprintf("couldn't open file '%s'\n", fname );
return NULL;
}
nrows=stream_count_char( f, '\n' );
rewind( f );
read_line( f, buf );
char c,prev='1';
while( (c=fgetc(f))!='\n' ){
if( isblank(c) && !isblank(prev) ){
ncols++;
}
prev=c;
}
rewind( f );
dprintf( "matrix is (%i,%i)\n", nrows, ncols );
Array *a=array_new2( DOUBLE, 2, nrows, ncols );
int i,j,k;
for( i=0; i<nrows; i++ ){
for( k=0; k<ncols; k++ ){
j=0;
while( !isblank((c=fgetc(f))) ){
buf[j++]=c;
}
buf[j]='\0';
dprintf("%s\n", buf );
mat_IDX( a, i, k )=atof( buf );
while( isblank((c=fgetc(f))) ) ;
fseek( f, -1, SEEK_CUR);
}
}
dprintf("Done\n");
fclose( f );
return a;
}
/** reads ChannelInfo struct from .ced file (EEGlab).
Format is: One line with labels (tab-sep), followed by lines of values (tab-sep).
This function reads:
- labels: "labels" "X", "Y", "Z"
- values: char* double double double
\param fname filename
\param chans ChannelInfo struct (large enough) or NULL (memory allocated in function)
\return chaninfo read from file or NULL if error occured
*/
ChannelInfo* read_chaninfo_ced( const char *fname, ChannelInfo *chans ){
int num_chans=0;
FILE *f;
char buf[MAX_LINE_LENGTH];
int Label_idx=-1,
X_idx=-1, Y_idx=-1, Z_idx=-1;
char *strstart, *strend;
int idx, i;
if( (f=fopen( fname, "r" ))==NULL ){
errprintf("couldn't open file '%s'\n", fname );
return NULL;
}
read_line( f, buf );
dprintf("labels='%s'\n", buf);
/* labels present ?*/
strstart=buf;
idx = 0;
while( (strend=strchr( strstart, '\t' ))!=NULL){
*strend='\0';
dprintf( "curlab='%s'\n", strstart );
if( !strcasecmp( strstart, "labels" ) ){
Label_idx=idx;
} else if( !strcasecmp( strstart, "X" ) ){
X_idx=idx;
} else if( !strcasecmp( strstart, "Y" ) ){
Y_idx=idx;
} else if( !strcasecmp( strstart, "Z" ) ){
Z_idx=idx;
}
strstart = strend+1;
idx++;
}
dprintf(" l=%i, X=%i, Y=%i, Z=%i\n", Label_idx, X_idx, Y_idx, Z_idx );
if( X_idx<0 || Y_idx<0 || Z_idx<0 || Label_idx<0 ){
errprintf( " ERROR: did not find one of the labels: X=%i, Y=%i, Z=%i, Labels=%i\n",
X_idx, Y_idx, Z_idx, Label_idx );
return NULL;
}
num_chans = stream_count_char( f, '\n' );
dprintf(" num_chans = %i\n", num_chans );
if( chans==ALLOC_IN_FCT ){
chans = (ChannelInfo*) malloc( num_chans*sizeof(ChannelInfo) );
} else { /* num_chans from chans-struct */
if( num_chans!=chans[0].num_chans ){
errprintf( " ERROR: not enough memory in chans-struct\n");
return NULL;
}
}
for( i=0; i<num_chans; i++ ){ /* for all channels */
read_line( f, buf );
strstart=buf;
idx = 0;
chans[i].num_chans=num_chans;
chans[i].num = i+1;
while( (strend=strchr( strstart, '\t' ))!=NULL){
*strend='\0';
if( idx==Label_idx ){
strcpy( chans[i].label, strstart );
} else if( idx==X_idx ){
chans[i].x = atof( strstart );
} else if( idx==Y_idx ){
chans[i].y = atof( strstart );
} else if( idx==Z_idx ){
chans[i].z = atof( strstart );
}
strstart = strend+1;
idx++;
}
print_channelinfo( stderr, chans+i );
}
return chans;
}
/** read xdim x ydim matrix from ascii file with delimiter 'delim';
if NULL is passed as d, the functin allocates memory.
*/
double** read_dblpp_ascii(const char *fname, int xdim, int ydim, double **d){
FILE *f;
int x,y;
int flag;
if((f=fopen(fname, "r"))==NULL){
errprintf("failed reading %s\n", fname );
return NULL;
}
if(d==NULL){
d = (double**) malloc( ydim*sizeof(double*) );
for( y=0; y<ydim; y++ )
d[y] = (double*) malloc( xdim*sizeof(double) );
}
for( y=0; y<ydim; y++ ){
for( x=0; x<xdim; x++ ){
flag=fscanf(f, " %lf ", &(d[y][x]));
}
}
fclose(f);
return d;
}
/** read 1D vector frome file
*/
double* read_dblp_ascii( const char *fname, int N, double *v ){
FILE *f;
int i,flag;
if((f=fopen(fname, "r"))==NULL)
errormsg(ERR_IO, 1);
if(v==NULL){
v = dblp_init( NULL, N, 0.0 );
}
for( i=0; i<N; i++ ){
flag=fscanf(f, " %lf ", &(v[i]));
}
fclose( f );
return v;
}
/* ------------- WRITER --------------------- */
/**
write double matrix to file in ASCII format.
\param opts may contain
- "precision=int" number of significant digits (after comma); default=6;
*/
void write_dblpp_ascii_file(const char *fname, const double **d, int xdim, int ydim, OptArgList *opts){
FILE *f;
dprintf("opening '%s'\n", fname );
if((f=fopen(fname, "w"))==NULL)
errormsg(ERR_IO, 1);
write_dblpp_ascii(f, d, xdim, ydim, opts);
fclose(f);
}
/** write double matrix to file in ASCII format.
\param opts may contain
- "precision=int" number of significant digits (after comma); default=6;
*/
void write_dblpp_ascii(FILE *out, const double **d, int xdim, int ydim, OptArgList *opts){
int x, y;
int precision;
char tformat[20];
double a;
precision=6;
if( optarglist_has_key( opts, "precision" ) ){
a = optarglist_scalar_by_key( opts, "precision" );
if( !isnan( a ) ) precision=(int)a;
}
sprintf( tformat, "%%.%if ", precision );
dprintf("Printing in format: '%s'\n", tformat);
for( y=0; y<ydim; y++ ){
for( x=0; x<xdim; x++ ){
fprintf(out, tformat, d[x][y]);
}
fprintf(out, "\n");
}
}
void write_dblp_ascii(FILE *out, const double *v, int n){
int x;
for( x=0; x<n; x++ ){
fprintf(out, "%f ", v[x]);
}
fprintf(out, "\n");
}
void write_dblp_ascii_file(const char *fname, const double *v, int n){
FILE *f;
if((f=fopen(fname, "w"))==NULL)
errormsg(ERR_IO, 1);
write_dblp_ascii(f, v, n);
fclose(f);
}
void write_intp_ascii(FILE *out, const int *v, int n){
int x;
for( x=0; x<n; x++ ){
fprintf(out, "%i ", v[x]);
}
fprintf(out, "\n");
}
void write_intp_ascii_file(const char *fname, const int *v, int n){
FILE *f;
if((f=fopen(fname, "w"))==NULL)
errormsg(ERR_IO, 1);
write_intp_ascii(f, v, n);
fclose(f);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.