-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_io.cpp
508 lines (410 loc) · 14.3 KB
/
file_io.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <string>
#include <complex>
#include <vector>
#include <fstream>
#include <Eigen/Dense>
#include <map>
#include <filesystem> // C++17
#include "file_io.h"
#include "H5Cpp.h"
using namespace H5;
using namespace std;
namespace fs = std::filesystem;
using namespace std::complex_literals;
using namespace Eigen;
typedef complex<double> dcomp;
typedef Matrix<double,Dynamic,Dynamic,RowMajor> MatrixXd_row;
#define MAXBUFSIZE ((int) 1e6)
vector<string> create_directories(const string& base_path, const vector<string>& directory_names, bool create_new_dirs_if_exists)
{
// Created subdirectory paths as output
vector<string> directory_paths;
// If base directory does not exist, do nothing and return empty list
if(!fs::exists(base_path))
return directory_paths;
// Create the needed directories
for(auto it = directory_names.begin(); it != directory_names.end(); it++){
string directory_path = base_path + "/" + *it;
//Count the existing directories with the same base name
unsigned int path_count = 0;
string count_str = "";
while(fs::exists(directory_path+count_str)){
path_count++;
count_str = to_string(path_count);
}
// If the wanted directory does not exist, create new.
// Else if a new directory is wanted in any case,
// create new with a number
if(!fs::exists(directory_path)){
fs::create_directory(directory_path);
directory_paths.push_back(directory_path);
}
else if(create_new_dirs_if_exists){
fs::create_directory(directory_path+"_"+
to_string(path_count));
directory_paths.push_back(directory_path+"_"+
to_string(path_count));
} else
directory_paths.push_back(directory_path);
}
return directory_paths;
}
MatrixXd readRealMatrix(const char* filename, const char sep)
{
int cols = 0, rows = 0;
double buff[MAXBUFSIZE];
// Read numbers from file into buffer.
ifstream infile;
infile.open(filename);
if(infile.fail())
cout << "File could not be opened" << endl;
while (infile.good())
{
string line;
getline(infile, line);
if(line.empty())
break;
int temp_cols = 0;
stringstream stream(line);
string elem;
while(stream.good()){
getline(stream,elem,sep);
// Store in column major fashion
buff[cols*rows+temp_cols++] = stod(elem);
}
if (temp_cols == 0)
continue;
if (cols == 0)
cols = temp_cols;
rows++;
}
infile.close();
//rows--;
//Map<Matrix<double,Dynamic,Dynamic, RowMajor>>
//result(buff,rows,cols);
//return result;
// Populate matrix with numbers.
MatrixXd result(rows,cols);
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
result(i,j) = buff[ cols*i+j ];
}
}
return result;
};
MatrixXcd readComplexMatrix(const char* filename, const char sep)
{
int cols = 0, rows = 0;
dcomp buff[MAXBUFSIZE/2];
// Read numbers from file into buffer.
ifstream infile;
infile.open(filename);
if(infile.fail())
cout << "File could not be opened" << endl;
while (infile.good())
{
string line;
getline(infile, line);
if(line.empty())
break;
int temp_cols = 0;
stringstream stream(line);
string elem;
while(stream.good()){
getline(stream,elem,sep);
istringstream comp_stream(elem);
comp_stream >> buff[cols*rows+temp_cols++];
//char character;
//double real;
//double imag;
//comp_stream >> character >> real >> character >> imag >> character;
//buff[cols*rows+temp_cols++] = real + 1.0i*imag;
}
if (temp_cols == 0)
continue;
if (cols == 0)
cols = temp_cols;
rows++;
}
infile.close();
//rows--;
//
//Map<Matrix<dcomp,Dynamic,Dynamic, RowMajor>>
//result(buff,rows,cols);
//return result;
// Populate matrix with numbers.
MatrixXcd result(rows,cols);
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
result(i,j) = buff[ cols*i+j ];
}
}
return result;
};
map<string,string> load_file_to_string_map(const string& file_path, const string& sep)
{
ifstream file(file_path);
map<string,string> output;
// Enter the file and
if(file.is_open()){
string line;
while(getline(file,line)){
line.erase(remove_if(line.begin(),line.end(), ::isspace),line.end());
if(line[0] == '#' || line.empty())
continue;
size_t commentPos = line.find("#");
if(commentPos != string::npos)
line.erase(line.begin()+commentPos,line.end());
size_t delimiterPos = line.find(sep);
string name = line.substr(0,delimiterPos);
string value = line.substr(delimiterPos+sep.length());
output[name] = value;
}
} else {
cerr << "Couldn't open the parameter file for reading.\n";
}
return output;
}
bool save_string_map_to_file(const map<string,string>& params, const string& sep,const string& path, const string& file_name)
{
ofstream file(path+"/"+file_name);
if(!file.is_open()){
cout << "File : " << path+"/"+file_name << " couldn't be opened." << endl;
return false;
}
for(auto it = params.begin();it != params.end();it++)
file << it->first << sep << it->second << endl;
file.close();
return true;
}
// Function to separate a string to parts based on a separator string
vector<string> split_str(string input, string sep)
{
vector<string> output;
while(!input.empty()){
size_t sep_pos = input.find(sep);
if(sep_pos == string::npos){
output.push_back(input);
input.erase(input.begin(),input.end());
}
else{
output.push_back(input.substr(0,sep_pos));
input.erase(input.begin(),input.begin()+sep_pos+1);
}
}
return output;
}
void write_MatrixXcd_to_file(H5File* file, const MatrixXcd& mat, const H5std_string dset_path)
{
const H5std_string dset_real_path = dset_path+"_r";
const H5std_string dset_imag_path = dset_path+"_i";
hsize_t rows = mat.rows(), cols = mat.cols();
hsize_t dims[2] = {rows,cols};
// DataSpaces and DataSets for the real and imaginary parts
DataSpace space(2,dims);
DataSet* dset_r = new DataSet(file->createDataSet(dset_real_path,PredType::IEEE_F64LE, space));
DataSet* dset_i = new DataSet(file->createDataSet(dset_imag_path,PredType::IEEE_F64LE, space));
// Fill buffer with data
double* buff_r = new double[rows*cols];
double* buff_i = new double[rows*cols];
Map<MatrixXd_row>(buff_r, rows,cols) = mat.real();
Map<MatrixXd_row>(buff_i, rows,cols) = mat.imag();
// Write to the datasets
dset_r->write(buff_r,PredType::NATIVE_DOUBLE);
dset_i->write(buff_i,PredType::NATIVE_DOUBLE);
// Release resources
delete dset_r;
delete dset_i;
delete[] buff_r;
delete[] buff_i;
}
MatrixXcd get_MatrixXcd_from_file(H5File* file, const H5std_string dset_path)
{
MatrixXcd output;
const H5std_string dset_real_path = dset_path+"_r";
const H5std_string dset_imag_path = dset_path+"_i";
DataSet* dset_r = new DataSet(file->openDataSet(dset_real_path));
DataSet* dset_i = new DataSet(file->openDataSet(dset_imag_path));
DataSpace r_space = dset_r->getSpace();
//DataSpace i_space = dset_i->getSpace();
//
int n_dims = r_space.getSimpleExtentNdims();
hsize_t dims[n_dims];
r_space.getSimpleExtentDims(&dims[0]);
double* buff_r = new double[dims[0]*dims[1]];
double* buff_i = new double[dims[0]*dims[1]];
dset_r->read(buff_r, PredType::NATIVE_DOUBLE);
dset_i->read(buff_i, PredType::NATIVE_DOUBLE);
output = Map<MatrixXd_row>(buff_r, dims[0], dims[1]) +
Map<MatrixXd_row>(buff_i, dims[0], dims[1])*1.0i;
delete dset_r;
delete dset_i;
delete[] buff_r;
delete[] buff_i;
return output;
}
void write_MatrixXd_to_file(H5File* file, const MatrixXd& mat, const H5std_string dset_path)
{
hsize_t rows = mat.rows(), cols = mat.cols();
hsize_t dims[2] = {rows,cols};
// DataSpaces and DataSets for the real and imaginary parts
DataSpace space(2,dims);
DataSet* dset = new DataSet(file->createDataSet(dset_path,PredType::IEEE_F64LE, space));
double* buff = new double[rows*cols];
Map<MatrixXd_row>(buff, rows,cols) = mat;
// Write to the datasets
dset->write(buff,PredType::NATIVE_DOUBLE);
// Release resources
delete dset;
delete[] buff;
}
MatrixXd get_MatrixXd_from_file(H5File* file, const H5std_string dset_path)
{
DataSet* dset = new DataSet(file->openDataSet(dset_path));
DataSpace space = dset->getSpace();
int n_dims = space.getSimpleExtentNdims();
hsize_t dims[n_dims];
space.getSimpleExtentDims(&dims[0]);
MatrixXd output = MatrixXd::Zero(dims[0],dims[1]);
dset->read(output.data(), PredType::NATIVE_DOUBLE);
delete dset;
return output;
}
void write_MatrixXcd_to_group(Group* group, const MatrixXcd& mat, const H5std_string dset_path)
{
const H5std_string dset_real_path = dset_path+"_r";
const H5std_string dset_imag_path = dset_path+"_i";
hsize_t rows = mat.rows(), cols = mat.cols();
hsize_t dims[2] = {rows,cols};
// DataSpaces and DataSets for the real and imaginary parts
DataSpace space(2,dims);
DataSet* dset_r;
DataSet* dset_i;
if(!group->nameExists(dset_real_path)){
dset_r = new DataSet(group->createDataSet(dset_real_path,PredType::IEEE_F64LE, space));
dset_i = new DataSet(group->createDataSet(dset_imag_path,PredType::IEEE_F64LE, space));
} else {
cout << "Dataset " << dset_path << " already exists. Overwriting" << endl;
dset_r = new DataSet(group->openDataSet(dset_real_path));
dset_i = new DataSet(group->openDataSet(dset_imag_path));
}
// Fill buffer with data
double* buff_r = new double[rows*cols];
double* buff_i = new double[rows*cols];
Map<MatrixXd_row>(buff_r, rows,cols) = mat.real();
Map<MatrixXd_row>(buff_i, rows,cols) = mat.imag();
// Write to the datasets
dset_r->write(buff_r,PredType::NATIVE_DOUBLE);
dset_i->write(buff_i,PredType::NATIVE_DOUBLE);
// Release resources
delete dset_r;
delete dset_i;
delete[] buff_r;
delete[] buff_i;
}
MatrixXcd get_MatrixXcd_from_group(Group* group, const H5std_string dset_path)
{
MatrixXcd output;
const H5std_string dset_real_path = dset_path+"_r";
const H5std_string dset_imag_path = dset_path+"_i";
DataSet* dset_r = new DataSet(group->openDataSet(dset_real_path));
DataSet* dset_i = new DataSet(group->openDataSet(dset_imag_path));
DataSpace r_space = dset_r->getSpace();
//DataSpace i_space = dset_i->getSpace();
//
int n_dims = r_space.getSimpleExtentNdims();
hsize_t dims[n_dims];
r_space.getSimpleExtentDims(&dims[0]);
double* buff_r = new double[dims[0]*dims[1]];
double* buff_i = new double[dims[0]*dims[1]];
dset_r->read(buff_r, PredType::NATIVE_DOUBLE);
dset_i->read(buff_i, PredType::NATIVE_DOUBLE);
output = Map<MatrixXd_row>(buff_r, dims[0], dims[1]) +
Map<MatrixXd_row>(buff_i, dims[0], dims[1])*1.0i;
delete dset_r;
delete dset_i;
delete[] buff_r;
delete[] buff_i;
return output;
}
void write_MatrixXd_to_group(Group* group, const MatrixXd& mat, const H5std_string dset_path)
{
hsize_t rows = mat.rows(), cols = mat.cols();
hsize_t dims[2] = {rows,cols};
// DataSpaces and DataSets for the real and imaginary parts
DataSpace space(2,dims);
DataSet* dset = new DataSet(group->createDataSet(dset_path,PredType::IEEE_F64LE, space));
double* buff = new double[rows*cols];
Map<MatrixXd_row>(buff, rows,cols) = mat;
// Write to the datasets
dset->write(buff,PredType::NATIVE_DOUBLE);
// Release resources
delete dset;
}
MatrixXd get_MatrixXd_from_group(Group* group, const H5std_string dset_path)
{
DataSet* dset = new DataSet(group->openDataSet(dset_path));
DataSpace space = dset->getSpace();
int n_dims = space.getSimpleExtentNdims();
hsize_t dims[n_dims];
space.getSimpleExtentDims(dims);
MatrixXd output = MatrixXd::Zero(dims[0],dims[1]);
dset->read(output.data(), PredType::NATIVE_DOUBLE);
delete dset;
return output;
}
bool get_data_from_h5attr(Group* group, string name, string type, void* mem)
{
if(!group->attrExists(name))
return false;
//try{
// if(!group->attrExists(name))
// throw(false);
//} catch (bool succ) {
// cout << "Cannot open attribute " << name << ": it does not exists." << endl;
// return;
//}
Attribute attr = group->openAttribute(name);
if(type == "double")
attr.read(PredType::NATIVE_DOUBLE,mem);
if(type == "string"){
StrType dt(PredType::C_S1,H5T_VARIABLE);
attr.read(dt,*(string*)mem);
}
if(type == "int")
attr.read(PredType::NATIVE_INT,mem);
attr.close();
return true;
}
void save_data_to_h5attr(Group* group, string name, string type, const void* mem)
{
Attribute attr;
DataSpace ds(H5S_SCALAR);
if(type == "string"){
StrType st(PredType::C_S1,H5T_VARIABLE);
if( group->attrExists(name))
attr = group->openAttribute(name);
else
attr = group->createAttribute(name,st,ds);
attr.write(st,*(string*)mem);
st.close();
} else if (type == "int"){
IntType it(PredType::STD_I64LE);
if( group->attrExists(name))
attr = group->openAttribute(name);
else
attr = group->createAttribute(name,it,ds);
attr.write(it,mem);
it.close();
} else if (type == "double"){
FloatType ft(PredType::IEEE_F64LE);
if( group->attrExists(name))
attr = group->openAttribute(name);
else
attr = group->createAttribute(name,ft,ds);
attr.write(ft,mem);
ft.close();
}
ds.close();
attr.close();
}