CSVReader.cpp 19.2 KB
Newer Older
1
2
/* Copyright 2017-2020 Institute for Automation of Complex Power Systems,
 *                     EONERC, RWTH Aachen University
Markus Mirz's avatar
Markus Mirz committed
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
Markus Mirz's avatar
Markus Mirz committed
7
8
 *********************************************************************************/

9

Markus Mirz's avatar
Markus Mirz committed
10
11
12
13
14
15
#include <cps/CSVReader.h>

namespace fs = std::experimental::filesystem;

using namespace CPS;

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
MatrixRow CSVReader::csv2Eigen(const std::string& path) {
    std::ifstream inputFile;
    inputFile.open(path);
    std::string line;
    std::vector<double> values;
    uint rows = 0;
    while (std::getline(inputFile, line)) {
        std::stringstream lineStream(line);
        std::string cell;
        while (std::getline(lineStream, cell, ',')) {
            values.push_back(std::stod(cell));
        }
        ++rows;
    }
	uint columns = values.size()/rows;
    return Eigen::Map<const MatrixRow>(values.data(), rows, columns);
}

Markus Mirz's avatar
Markus Mirz committed
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
void CSVRow::readNextRow(std::istream& str) {
	std::string line;
	std::getline(str, line);

	std::stringstream   lineStream(line);
	std::string         cell;

	m_data.clear();
	while (std::getline(lineStream >> std::ws, cell, ',')) {
		m_data.push_back(cell);
	}
	// This checks for a trailing comma with no data after it.
	if (!lineStream && cell.empty()){
		// If there was a trailing comma then add an empty element.
		m_data.push_back("");
	}
}

int CSVRow::size() const {
	return m_data.size();
}

CSVReaderIterator& CSVReaderIterator::next() {
	if (m_str) {
		m_row.readNextRow(*m_str);
		if (!(*m_str)) {
			m_str = NULL;
		}
	}
	return *this;
}

CSVReaderIterator CSVReaderIterator::next(int) {
	CSVReaderIterator tmp(*this);
	this->next();
	return tmp;
}

CSVReaderIterator& CSVReaderIterator::step(int time_step) {
	while (time_step != 0) {
		if (m_str) {
			m_row.readNextRow(*m_str);
			if (!(*m_str)) {
				m_str = NULL;
			}
		}
		time_step -= 1;
	}
	return *this;
}


CSVReaderIterator::CSVReaderIterator(std::istream& str)
	: m_str(str.good() ? &str : NULL) {
	this->next();
}

CSVReaderIterator::CSVReaderIterator():m_str(NULL) { }

CSVReader::CSVReader(CPS::String name, std::list<fs::path> paths, CPS::Logger::Level logLevel) {
	mSLog = Logger::get(name + "_csvReader", logLevel);
	//mFileList = paths;
	for(auto file : paths){
		if(file.string().find(".csv")!=std::string::npos){
				mFileList.push_back(file);
				std::cout<<"add "<< file<<std::endl;
		}
	}

}

CSVReader::CSVReader(CPS::String name, CPS::String path,  CPS::Logger::Level logLevel) {
	mSLog = Logger::get(name + "_csvReader", logLevel);

	mPath = path;
	for (const auto & entry : std::experimental::filesystem::directory_iterator(path)) {
			mFileList.push_back(entry.path());

	}
}

CSVReader::CSVReader(CPS::String name, CPS::String path, std::map<String, String>& assignList, CPS::Logger::Level logLevel)
	: CSVReader(name, path, logLevel) {

	mAssignPattern = assignList;
}

CSVReader::CSVReader(CPS::String name, std::list<fs::path> paths, std::map<String, String>& assignList,
CPS::Logger::Level logLevel): CSVReader(name, paths, logLevel){
	mAssignPattern = assignList;
}

CPS::Real CSVReader::time_format_convert(const CPS::String& time) {
	int hh, mm, ss = 0;
	CPS::Real secs = 0;
	if (sscanf(time.c_str(), "%d:%d:%d", &hh, &mm, &ss) >= 2) {
		secs = hh * 3600 + mm * 60 + ss;
	}
	return secs;
}

std::vector<PQData> CSVReader::readLoadProfileDP(std::experimental::filesystem::path file,
	Real start_time, Real time_step, Real end_time, Real scale_factor, CSVReader::DataFormat format) {

	std::vector<PQData> load_profileDP;
	std::ifstream csvfile(file);
	CSVReaderIterator row_(csvfile);

142
	
Markus Mirz's avatar
Markus Mirz committed
143
	// ignore the first row if it is a title
144
	if (mSkipFirstRow && !std::isdigit((*row_).get(0)[0])) {
Markus Mirz's avatar
Markus Mirz committed
145
146
		row_.next();
	}
147

Markus Mirz's avatar
Markus Mirz committed
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
	/*
	 loop over rows of the csv file to find the entry point to read in.
	 if start_time and end_time are negative (as default), it reads in all rows.
	*/
	Real presentTime = 0;
	for (; row_ != CSVReaderIterator(); row_.next()) {
		if ((start_time < 0) | (Int(presentTime) + 1 > Int(start_time)))
			break;
		presentTime++;
	}
	/*
	 reading data after entry point until end_time is reached
	*/
	for (; row_ != CSVReaderIterator(); row_.next()) {
		// IMPORTANT: take care of units. assume kW
		PQData pq;
		// multiplied by 1000 due to unit conversion (kw to w)
		pq.p = std::stod((*row_).get(1)) * 1000 * scale_factor;
		pq.q = std::stod((*row_).get(2)) * 1000 * scale_factor;
		load_profileDP.push_back(pq);
		if (end_time > 0 && presentTime > end_time)
			break;
		/*while (presentTime + time_step < Int(presentTime) + 1)
		{
			presentTime += time_step;
			p_data.push_back(p_data.back());
		}*/
		presentTime = Int(presentTime) + 1;
	}
	std::cout<<"CSV loaded."<<std::endl;
	return load_profileDP;
}

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
// void CSVReader::assignLoadProfileSP(std::vector<std::shared_ptr<CPS::SP::Ph1::AvVoltageSourceInverterDQ>>& loads,
//  Real start_time, Real time_step, Real end_time, Real scale_factor,
// 	CSVReader::Mode mode, CSVReader::DataFormat format) {

// 	switch (mode) {
// 		case CSVReader::Mode::AUTO: {
// 			for (auto load : loads) {
// 				if(!load->isLoad())
// 					continue;
// 				String load_name = load->name();
// 				for (auto file : mFileList) {
// 					String file_name = file.filename().string();
// 					/// changing file name and load name to upper case for later matching
// 					for (auto & c : load_name) c = toupper(c);
// 					for (auto & c : file_name) c = toupper(c);
// 					/// strip off all non-alphanumeric characters
// 					load_name.erase(remove_if(load_name.begin(), load_name.end(), [](char c) { return !isalnum(c); }), load_name.end());
// 					file_name.erase(remove_if(file_name.begin(), file_name.end(), [](char c) { return !isalnum(c); }), file_name.end());
// 					if (std::stoi(file_name) == std::stoi(load_name)) {
// 						load->mLoadProfile = readLoadProfileDP(file, start_time, time_step, end_time, scale_factor, format);
// 						mSLog->info("Assigned {} to {}", file.filename().string(), load->name());
// 					}
// 				}
// 			}

// 			break;
// 		}
// 		case CSVReader::Mode::MANUAL: {
// 			Int LP_assigned_counter = 0;
// 			Int LP_not_assigned_counter = 0;
// 			mSLog->info("Assigning load profiles with user defined pattern ...");
// 			for (auto load : loads) {
// 					std::map<String, String>::iterator file = mAssignPattern.find(load->name());
// 					if (file == mAssignPattern.end()) {

// 						mSLog->info("{} has no profile given.", load->name());
// 						LP_not_assigned_counter++;
// 						continue;
// 					}
// 					for(auto path: mFileList){
// 						if(path.string().find(file->second)!= std::string::npos){
// 							load->mLoadProfile = readLoadProfileDP(path, start_time, time_step, end_time, scale_factor);
// 							mSLog->info("Assigned {}.csv to {}", file->second, load->name());
// 							LP_assigned_counter++;
// 						}

// 					}
// 			}
// 			mSLog->info("Assigned profiles for {} loads, {} not assigned.", LP_assigned_counter, LP_not_assigned_counter);
// 			break;
// 		}
// 		default: {
// 			throw std::invalid_argument(
// 				"Load profile assign mode error");
// 			break;
// 		}
// 	}
// }
Markus Mirz's avatar
Markus Mirz committed
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


// void CSVReader::assignLoadProfilePF(std::vector<std::shared_ptr<CPS::SP::Ph1::AvVoltageSourceInverterDQ>>& loads,
//  Real start_time, Real time_step, Real end_time, Real scale_factor,
// 	CSVReader::Mode mode, CSVReader::DataFormat format) {

// 	switch (mode) {
// 		case CSVReader::Mode::AUTO: {
// 			for (auto load : loads) {
// 				if(!load->isLoad())
// 					continue;
// 				String load_name = load->name();
// 				for (auto file : mFileList) {
// 					String file_name = file.filename().string();
// 					/// changing file name and load name to upper case for later matching
// 					for (auto & c : load_name) c = toupper(c);
// 					for (auto & c : file_name) c = toupper(c);
// 					/// strip off all non-alphanumeric characters
// 					load_name.erase(remove_if(load_name.begin(), load_name.end(), [](char c) { return !isalnum(c); }), load_name.end());
// 					file_name.erase(remove_if(file_name.begin(), file_name.end(), [](char c) { return !isalnum(c); }), file_name.end());
// 					if (std::stoi(file_name) == std::stoi(load_name)) {
// 						load->mPFAvVoltageSourceInverter->mLoadProfile = readLoadProfile(std::experimental::filesystem::path(mPath + file->second + ".csv"), start_time, time_step, end_time);
// 						load->mPFAvVoltageSourceInverter->use_profile = true;
// 						mSLog->info("Assigned {} to {}", file.filename().string(), load->name());
// 					}
// 				}
// 			}

// 			break;
// 		}
// 		case CSVReader::Mode::MANUAL: {
// 			Int LP_assigned_counter = 0;
// 			Int LP_not_assigned_counter = 0;
// 			mSLog->info("Assigning load profiles with user defined pattern ...");
// 			for (auto load : loads) {
// 					std::map<String, String>::iterator file = mAssignPattern.find(load->name());
// 					if (file == mAssignPattern.end()) {

// 						mSLog->info("{} has no profile given.", load->name());
// 						LP_not_assigned_counter++;
// 						continue;
// 					}
// 					for(auto path: mFileList){
// 						if(path.string().find(file->second)!= std::string::npos){
// 							load->mPFAvVoltageSourceInverter->mLoadProfile = readLoadProfile(std::experimental::filesystem::path(mPath + file->second + ".csv"), start_time, time_step, end_time);
// 							load->mPFAvVoltageSourceInverter->use_profile = true;
// 							mSLog->info("Assigned {}.csv to {}", file->second, load->name());
// 							LP_assigned_counter++;
// 						}

// 					}
// 			}
// 			mSLog->info("Assigned profiles for {} loads, {} not assigned.", LP_assigned_counter, LP_not_assigned_counter);
// 			break;
// 		}
// 		default: {
// 			throw std::invalid_argument(
// 				"Load profile assign mode error");
// 			break;
// 		}
// 	}
// }


303
304
305
306
// TODO: profile handling currently not supported by average inverter in DP1ph
// void CSVReader::assignLoadProfileDP(std::vector<std::shared_ptr<CPS::DP::Ph1::AvVoltageSourceInverterDQ>>& loads,
//  Real start_time, Real time_step, Real end_time, Real scale_factor,
// 	CSVReader::Mode mode, CSVReader::DataFormat format) {
Markus Mirz's avatar
Markus Mirz committed
307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// 	switch (mode) {
// 		case CSVReader::Mode::AUTO: {
// 			for (auto load : loads) {
// 				if(!load->isLoad())
// 					continue;
// 				String load_name = load->name();
// 				for (auto file : mFileList) {
// 					String file_name = file.filename().string();
// 					/// changing file name and load name to upper case for later matching
// 					for (auto & c : load_name) c = toupper(c);
// 					for (auto & c : file_name) c = toupper(c);
// 					/// strip off all non-alphanumeric characters
// 					load_name.erase(remove_if(load_name.begin(), load_name.end(), [](char c) { return !isalnum(c); }), load_name.end());
// 					file_name.erase(remove_if(file_name.begin(), file_name.end(), [](char c) { return !isalnum(c); }), file_name.end());
// 					if (std::stoi(file_name) == std::stoi(load_name)) {
// 						load->mLoadProfile = readLoadProfileDP(file, start_time, time_step, end_time, scale_factor, format);
// 						mSLog->info("Assigned {} to {}", file.filename().string(), load->name());
// 					}
// 				}
// 			}
Markus Mirz's avatar
Markus Mirz committed
328

329
330
331
332
333
334
335
336
337
// 			break;
// 		}
// 		case CSVReader::Mode::MANUAL: {
// 			Int LP_assigned_counter = 0;
// 			Int LP_not_assigned_counter = 0;
// 			mSLog->info("Assigning load profiles with user defined pattern ...");
// 			for (auto load : loads) {
// 					std::map<String, String>::iterator file = mAssignPattern.find(load->name());
// 					if (file == mAssignPattern.end()) {
Markus Mirz's avatar
Markus Mirz committed
338

339
340
341
342
343
344
345
346
347
348
// 						mSLog->info("{} has no profile given.", load->name());
// 						LP_not_assigned_counter++;
// 						continue;
// 					}
// 					for(auto path: mFileList){
// 						if(path.string().find(file->second)!= std::string::npos){
// 							load->mLoadProfile = readLoadProfileDP(path, start_time, time_step, end_time, scale_factor);
// 							mSLog->info("Assigned {}.csv to {}", file->second, load->name());
// 							LP_assigned_counter++;
// 						}
Markus Mirz's avatar
Markus Mirz committed
349

350
351
352
353
354
355
356
357
358
359
360
361
// 					}
// 			}
// 			mSLog->info("Assigned profiles for {} loads, {} not assigned.", LP_assigned_counter, LP_not_assigned_counter);
// 			break;
// 		}
// 		default: {
// 			throw std::invalid_argument(
// 				"Load profile assign mode error");
// 			break;
// 		}
// 	}
// }
Markus Mirz's avatar
Markus Mirz committed
362
363
364
365




366
PowerProfile CSVReader::readLoadProfile(std::experimental::filesystem::path file,
Markus Mirz's avatar
Markus Mirz committed
367
368
	Real start_time, Real time_step, Real end_time, CSVReader::DataFormat format) {

369
	PowerProfile load_profile;
Markus Mirz's avatar
Markus Mirz committed
370
371
372
373
374
375
376
	std::ifstream csvfile(file);
	bool need_that_conversion = (format == DataFormat::HHMMSS) ? true : false;
	bool data_with_weighting_factor = false;

	CSVReaderIterator loop(csvfile);

	// ignore the first row if it is a title
377
	if (mSkipFirstRow && !std::isdigit((*loop).get(0)[0])) {
Markus Mirz's avatar
Markus Mirz committed
378
379
		loop.next();
	}
380

Markus Mirz's avatar
Markus Mirz committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
	/*
	 loop over rows of the csv file to find the entry point to read in.
	 and determine data type prior to read in. (assuming only time,p,q or time,weighting factor)
	 if start_time and end_time are negative (as default), it reads in all rows.
	*/
	for (; loop != CSVReaderIterator(); loop.next()) {
		CSVReaderIterator nextRow = loop;
		nextRow.next();
		CPS::Real nextTime = (need_that_conversion) ? time_format_convert((*nextRow).get(0)) : std::stod((*nextRow).get(0));
		if ((*nextRow).size() == 2) {
			data_with_weighting_factor = true;
		}
		if ((start_time < 0) | (nextTime >= Int(start_time)))
		{
			break;
		}
	}
	/*
	 reading data after entry point until end_time is reached
	*/
	for (; loop != CSVReaderIterator(); loop.next()) {
		CPS::Real currentTime = (need_that_conversion) ? time_format_convert((*loop).get(0)) : std::stod((*loop).get(0));
		if (data_with_weighting_factor) {
			Real wf = std::stod((*loop).get(1));
405
			load_profile.weightingFactors.insert(std::pair<Real, Real>(currentTime, wf));
Markus Mirz's avatar
Markus Mirz committed
406
407
408
409
410
411
		}
		else {
		PQData pq;
		// multiplied by 1000 due to unit conversion (kw to w)
		pq.p = std::stod((*loop).get(1)) * 1000;
		pq.q = std::stod((*loop).get(2)) * 1000;
412
		load_profile.pqData.insert(std::pair<Real,PQData>(currentTime,pq));
Markus Mirz's avatar
Markus Mirz committed
413
414
415
416
417
418
419
420
421
422
423
		}

		if (end_time > 0 && currentTime > end_time)
			break;
	}
		std::vector<CPS::Real> times;
	for (CPS::Real x_ = start_time; x_ <= end_time; x_ += time_step) {
		times.push_back(x_);
	}

	for (auto x : times) {
424
		if (load_profile.pqData.find(x) == load_profile.pqData.end()) {
Markus Mirz's avatar
Markus Mirz committed
425
			if (data_with_weighting_factor) {
426
427
				Real y = interpol_linear(load_profile.weightingFactors, x);
				load_profile.weightingFactors.insert(std::pair<Real, Real>(x, y));
Markus Mirz's avatar
Markus Mirz committed
428
429
			}
			else {
430
431
				PQData y = interpol_linear(load_profile.pqData, x);
				load_profile.pqData.insert(std::pair<Real,PQData>(x,y));
Markus Mirz's avatar
Markus Mirz committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
			}
		}
	}

	return load_profile;
}

// can only read one file for now
std::vector<Real> CSVReader::readPQData(fs::path file,
	Real start_time, Real time_step, Real end_time,
	CSVReader::DataFormat format) {

	std::vector<Real> p_data;
	std::ifstream csvfile(file);
	CSVReaderIterator row_(csvfile);

	// ignore the first row if it is a title
449
	if (mSkipFirstRow && !std::isdigit((*row_).get(0)[0])) {
Markus Mirz's avatar
Markus Mirz committed
450
451
		row_.next();
	}
452

Markus Mirz's avatar
Markus Mirz committed
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	/*
	 loop over rows of the csv file to find the entry point to read in.
	 if start_time and end_time are negative (as default), it reads in all rows.
	*/
	Real presentTime = 0;
	for (; row_ != CSVReaderIterator(); row_.next()) {
		if ((start_time < 0) | (Int(presentTime) + 1 > Int(start_time)))
			break;
		presentTime++;
	}
	/*
	 reading data after entry point until end_time is reached
	*/
	for (; row_ != CSVReaderIterator(); row_.next()) {
		// IMPORTANT: take care of units. assume kW
		p_data.push_back(std::stod((*row_).get(0)) * 1000);
		if (end_time > 0 && presentTime > end_time)
			break;
		/*while (presentTime + time_step < Int(presentTime) + 1)
		{
			presentTime += time_step;
			p_data.push_back(p_data.back());
		}*/
		presentTime = Int(presentTime) + 1;
	}
	std::cout<<"CSV loaded."<<std::endl;
	return p_data;
}

void CSVReader::assignLoadProfile(CPS::SystemTopology& sys, Real start_time, Real time_step, Real end_time,
	CSVReader::Mode mode, CSVReader::DataFormat format) {

	switch (mode) {
		case CSVReader::Mode::AUTO: {
			for (auto obj : sys.mComponents) {
				if (std::shared_ptr<CPS::SP::Ph1::Load> load = std::dynamic_pointer_cast<CPS::SP::Ph1::Load>(obj)) {
					mSLog->info("Comparing csv file names with load mRIDs ...");
					String load_name = load->name();
					for (auto file : mFileList) {
						String file_name = file.filename().string();
						/// changing file name and load name to upper case for later matching
						for (auto & c : load_name) c = toupper(c);
						for (auto & c : file_name) c = toupper(c);
						/// strip off all non-alphanumeric characters
						load_name.erase(remove_if(load_name.begin(), load_name.end(), [](char c) { return !isalnum(c); }), load_name.end());
						file_name.erase(remove_if(file_name.begin(), file_name.end(), [](char c) { return !isalnum(c); }), file_name.end());
						if (std::string(file_name.begin(), file_name.end() - 3).compare(load_name) == 0) {
							load->mLoadProfile = readLoadProfile(file, start_time, time_step, end_time, format);
							load->use_profile = true;
							mSLog->info("Assigned {} to {}", file.filename().string(), load->name());
						}
					}
				}
			}
			break;
		}
		case CSVReader::Mode::MANUAL: {
			Int LP_assigned_counter = 0;
			Int LP_not_assigned_counter = 0;
			mSLog->info("Assigning load profiles with user defined pattern ...");
			for (auto obj : sys.mComponents) {
				if (std::shared_ptr<CPS::SP::Ph1::Load> load = std::dynamic_pointer_cast<CPS::SP::Ph1::Load>(obj)) {
					std::map<String, String>::iterator file = mAssignPattern.find(load->name());
					if (file == mAssignPattern.end()) {
						std::cout<< load->name()<<" has no profile given."<<std::endl;
						mSLog->info("{} has no profile given.", load->name());
						LP_not_assigned_counter++;
						continue;
					}
					load->mLoadProfile = readLoadProfile(std::experimental::filesystem::path(mPath + file->second + ".csv"), start_time, time_step, end_time);
					load->use_profile = true;
					std::cout<<" Assigned "<< file->second<< " to " <<load->name()<<std::endl;
					mSLog->info("Assigned {}.csv to {}", file->second, load->name());
					LP_assigned_counter++;
				}
			}
			mSLog->info("Assigned profiles for {} loads, {} not assigned.", LP_assigned_counter, LP_not_assigned_counter);
			break;
		}
		default: {
			throw std::invalid_argument(
				"Load profile assign mode error");
			break;
		}
	}
}

540
541
CPS::PQData CSVReader::interpol_linear(std::map<CPS::Real, CPS::PQData>& pqData, CPS::Real x) {
	std::map <Real, PQData>::const_iterator entry = pqData.upper_bound(x);
Markus Mirz's avatar
Markus Mirz committed
542
543
	PQData y;

544
	if (entry == pqData.end()) {
Markus Mirz's avatar
Markus Mirz committed
545
546
		return (--entry)->second;
	}
547
	if (entry == pqData.begin()) {
Markus Mirz's avatar
Markus Mirz committed
548
549
550
551
552
553
554
555
556
557
558
559
		return entry->second;
	}
	std::map <Real, PQData>::const_iterator prev = entry;
	--prev;

	const CPS::Real delta = (x - prev->first) / (entry->first - prev->first);

	y.p = delta * entry->second.p + (1 - delta)*prev->second.p;
	y.q = delta * entry->second.q + (1 - delta)*prev->second.q;
	return y;
}

560
561
CPS::Real CSVReader::interpol_linear(std::map<CPS::Real, CPS::Real>& weightingFactors, CPS::Real x) {
	std::map <Real, Real>::const_iterator entry = weightingFactors.upper_bound(x);
Markus Mirz's avatar
Markus Mirz committed
562
563
	CPS::Real y;

564
	if (entry == weightingFactors.end()) {
Markus Mirz's avatar
Markus Mirz committed
565
566
		return (--entry)->second;
	}
567
	if (entry == weightingFactors.begin()) {
Markus Mirz's avatar
Markus Mirz committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
		return entry->second;
	}
	std::map <Real, Real>::const_iterator prev = entry;
	--prev;

	const CPS::Real delta = (x - prev->first) / (entry->first - prev->first);

	y = delta * entry->second + (1 - delta)*prev->second;
	return y;
}