14 std::ifstream inputFile;
17 std::vector<double> values;
19 while (std::getline(inputFile, line)) {
20 std::stringstream lineStream(line);
22 while (std::getline(lineStream, cell,
',')) {
23 values.push_back(std::stod(cell));
27 UInt columns = values.size() / rows;
28 return Eigen::Map<const MatrixRow>(values.data(), rows, columns);
33 std::getline(str, line);
35 std::stringstream lineStream(line);
39 while (std::getline(lineStream >> std::ws, cell,
',')) {
40 m_data.push_back(cell);
43 if (!lineStream && cell.empty()) {
53 m_row.readNextRow(*m_str);
68 while (time_step != 0) {
70 m_row.readNextRow(*m_str);
81 : m_str(str.good() ? &str : NULL) {
91 for (
auto file : paths) {
92 if (file.string().find(
".csv") != std::string::npos) {
93 mFileList.push_back(file);
94 std::cout <<
"add " << file << std::endl;
101 mSLog =
Logger::get(name +
"_csvReader", logLevel);
104 for (
const auto &entry : fs::directory_iterator(path)) {
105 mFileList.push_back(entry.path());
110 std::map<String, String> &assignList,
114 mAssignPattern = assignList;
118 std::map<String, String> &assignList,
121 mAssignPattern = assignList;
127 if (sscanf(time.c_str(),
"%d:%d:%d", &hh, &mm, &ss) >= 2) {
128 secs = hh * 3600 + mm * 60 + ss;
138 std::vector<PQData> load_profileDP;
139 std::ifstream csvfile(file);
143 if (mSkipFirstRow && !std::isdigit((*row_).get(0)[0])) {
150 Real presentTime = 0;
152 if ((start_time < 0) | (
Int(presentTime) + 1 >
Int(start_time)))
161 pq.
p = std::stod((*row_).get(1)) * 1000 * scale_factor;
162 pq.
q = std::stod((*row_).get(2)) * 1000 * scale_factor;
163 load_profileDP.push_back(pq);
164 if (end_time > 0 && presentTime > end_time)
166 presentTime =
Int(presentTime) + 1;
168 std::cout <<
"CSV loaded." << std::endl;
169 return load_profileDP;
177 std::ifstream csvfile(file);
179 bool data_with_weighting_factor =
false;
184 if (mSkipFirstRow && !std::isdigit((*loop).get(0)[0])) {
196 CPS::Real nextTime = (need_that_conversion)
198 : std::stod((*nextRow).get(0));
199 if ((*nextRow).size() == 2) {
200 data_with_weighting_factor =
true;
202 if ((start_time < 0) | (nextTime >=
Int(start_time))) {
210 CPS::Real currentTime = (need_that_conversion)
212 : std::stod((*loop).get(0));
213 if (data_with_weighting_factor) {
214 Real wf = std::stod((*loop).get(1));
216 std::pair<Real, Real>(currentTime, wf));
220 pq.
p = std::stod((*loop).get(1)) * 1000;
221 pq.
q = std::stod((*loop).get(2)) * 1000;
222 load_profile.
pqData.insert(std::pair<Real, PQData>(currentTime, pq));
225 if (end_time > 0 && currentTime > end_time)
228 std::vector<CPS::Real> times;
229 for (
CPS::Real x_ = start_time; x_ <= end_time; x_ += time_step) {
233 for (
auto x : times) {
234 if (load_profile.
pqData.find(x) == load_profile.
pqData.end()) {
235 if (data_with_weighting_factor) {
240 load_profile.
pqData.insert(std::pair<Real, PQData>(x, y));
253 std::vector<Real> p_data;
254 std::ifstream csvfile(file);
258 if (mSkipFirstRow && !std::isdigit((*row_).get(0)[0])) {
266 Real presentTime = 0;
268 if ((start_time < 0) | (
Int(presentTime) + 1 >
Int(start_time)))
275 p_data.push_back(std::stod((*row_).get(0)) * 1000);
276 if (end_time > 0 && presentTime > end_time)
278 presentTime =
Int(presentTime) + 1;
280 std::cout <<
"CSV loaded." << std::endl;
292 if (std::shared_ptr<CPS::SP::Ph1::Load> load =
293 std::dynamic_pointer_cast<CPS::SP::Ph1::Load>(obj)) {
294 SPDLOG_LOGGER_INFO(mSLog,
295 "Comparing csv file names with load mRIDs ...");
296 String load_name = load->name();
297 for (
auto file : mFileList) {
298 String file_name = file.filename().string();
300 for (
auto &c : load_name)
302 for (
auto &c : file_name)
305 load_name.erase(remove_if(load_name.begin(), load_name.end(),
306 [](
char c) { return !isalnum(c); }),
308 file_name.erase(remove_if(file_name.begin(), file_name.end(),
309 [](
char c) { return !isalnum(c); }),
311 if (std::string(file_name.begin(), file_name.end() - 3)
312 .compare(load_name) == 0) {
315 load->use_profile =
true;
316 SPDLOG_LOGGER_INFO(mSLog,
"Assigned {} to {}",
317 file.filename().string(), load->name());
325 Int LP_assigned_counter = 0;
326 Int LP_not_assigned_counter = 0;
327 SPDLOG_LOGGER_INFO(mSLog,
328 "Assigning load profiles with user defined pattern ...");
330 if (std::shared_ptr<CPS::SP::Ph1::Load> load =
331 std::dynamic_pointer_cast<CPS::SP::Ph1::Load>(obj)) {
332 std::map<String, String>::iterator file =
333 mAssignPattern.find(load->name());
334 if (file == mAssignPattern.end()) {
335 std::cout << load->name() <<
" has no profile given." << std::endl;
336 SPDLOG_LOGGER_INFO(mSLog,
"{} has no profile given.", load->name());
337 LP_not_assigned_counter++;
342 time_step, end_time);
343 load->use_profile =
true;
344 std::cout <<
" Assigned " << file->second <<
" to " << load->name()
346 SPDLOG_LOGGER_INFO(mSLog,
"Assigned {}.csv to {}", file->second,
348 LP_assigned_counter++;
351 SPDLOG_LOGGER_INFO(mSLog,
352 "Assigned profiles for {} loads, {} not assigned.",
353 LP_assigned_counter, LP_not_assigned_counter);
357 throw std::invalid_argument(
"Load profile assign mode error");
365 std::map<Real, PQData>::const_iterator entry = pqData.upper_bound(x);
368 if (entry == pqData.end()) {
369 return (--entry)->second;
371 if (entry == pqData.begin()) {
372 return entry->second;
374 std::map<Real, PQData>::const_iterator prev = entry;
377 const CPS::Real delta = (x - prev->first) / (entry->first - prev->first);
379 y.
p = delta * entry->second.p + (1 - delta) * prev->second.p;
380 y.
q = delta * entry->second.q + (1 - delta) * prev->second.q;
387 std::map<Real, Real>::const_iterator entry = weightingFactors.upper_bound(x);
390 if (entry == weightingFactors.end()) {
391 return (--entry)->second;
393 if (entry == weightingFactors.begin()) {
394 return entry->second;
396 std::map<Real, Real>::const_iterator prev = entry;
399 const CPS::Real delta = (x - prev->first) / (entry->first - prev->first);
401 y = delta * entry->second + (1 - delta) * prev->second;
std::vector< Real > readPQData(fs::path file, Real start_time=-1, Real time_step=1, Real end_time=-1, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
PowerProfile readLoadProfile(fs::path file, Real start_time=-1, Real time_step=1, Real end_time=-1, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
CSVReader(String name, std::list< fs::path > path, Logger::Level logLevel)
PQData interpol_linear(std::map< Real, PQData > &data_PQ, Real x)
interpolation for PQ data points
MatrixRow csv2Eigen(const String &path)
std::vector< PQData > readLoadProfileDP(fs::path file, Real start_time=-1, Real time_step=1, Real end_time=-1, Real scale_factor=1, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
void assignLoadProfile(SystemTopology &sys, Real start_time=-1, Real time_step=1, Real end_time=-1, CSVReader::Mode mode=CSVReader::Mode::AUTO, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
assign load profile to corresponding load object
Real time_format_convert(const String &time)
CSVReaderIterator & step(Int time_step)
CSVReaderIterator & next()
CSVReaderIterator(std::istream &str)
void readNextRow(std::istream &str)
spdlog::level::level_enum Level
static Log get(const std::string &name, Level filelevel=Level::info, Level clilevel=Level::off)
IdentifiedObject::List mComponents
List of network components.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixRow
std::map< Real, PQData > pqData
std::map< Real, Real > weightingFactors