DPsim
CSVReader.h
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #pragma once
10 
11 #include <fstream>
12 #include <iostream>
13 #include <iterator>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 #include <dpsim-models/DP/DP_Ph1_AvVoltageSourceInverterDQ.h>
19 #include <dpsim-models/DP/DP_Ph1_PQLoadCS.h>
20 #include <dpsim-models/Filesystem.h>
21 #include <dpsim-models/Logger.h>
22 #include <dpsim-models/SP/SP_Ph1_AvVoltageSourceInverterDQ.h>
23 #include <dpsim-models/SP/SP_Ph1_Load.h>
24 #include <dpsim-models/SystemTopology.h>
25 
26 namespace CPS {
28 class CSVReader {
29 private:
31  Logger::Log mSLog;
33  String mPath;
35  std::list<fs::path> mFileList;
37  std::map<String, String> mAssignPattern;
39  Bool mSkipFirstRow = true;
40 
41 public:
44  enum class Mode { AUTO, MANUAL };
45 
46  /*
47  Time Stamp Format.
48  HHMMSS: Hours : Minutes : Seconds, it be casted to the corresponding SECONDS.
49  SECONDS: profiles recorded with total seconds.
50  PVGEN: format comply with https://www.fein-aachen.org/projects/PVgenerator/
51  */
52  enum class DataFormat { HHMMSS, SECONDS, HOURS, MINUTES };
53 
55  CSVReader(String name, std::list<fs::path> path, Logger::Level logLevel);
57  CSVReader(String name, String path, Logger::Level logLevel);
59  CSVReader(String name, std::list<fs::path> path,
60  std::map<String, String> &assignList, Logger::Level logLevel);
62  CSVReader(String name, String path, std::map<String, String> &assignList,
63  Logger::Level logLevel);
64 
67  Real time_format_convert(const String &time);
69  void doSkipFirstRow(Bool value = true) { mSkipFirstRow = value; }
71  MatrixRow csv2Eigen(const String &path);
72 
73  std::vector<PQData> readLoadProfileDP(
74  fs::path file, Real start_time = -1, Real time_step = 1,
75  Real end_time = -1, Real scale_factor = 1,
76  CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
77 
78  // void assignLoadProfilePF(std::vector<std::shared_ptr<CPS::SP::Ph1::AvVoltageSourceInverterDQ>>& loads,
79  // Real start_time = -1, Real time_step = 1, Real end_time = -1, Real scale_factor= 1,
80  // CSVReader::Mode mode = CSVReader::Mode::AUTO,
81  // CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
82 
83  // void assignLoadProfileSP(std::vector<std::shared_ptr<CPS::SP::Ph1::AvVoltageSourceInverterDQ>>& loads,
84  // Real start_time = -1, Real time_step = 1, Real end_time = -1, Real scale_factor= 1,
85  // CSVReader::Mode mode = CSVReader::Mode::AUTO,
86  // CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
87 
88  void assignLoadProfileDP(
89  std::vector<std::shared_ptr<CPS::DP::Ph1::AvVoltageSourceInverterDQ>>
90  &loads,
91  Real start_time = -1, Real time_step = 1, Real end_time = -1,
92  Real scale_factor = 1, CSVReader::Mode mode = CSVReader::Mode::AUTO,
93  CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
94 
98  fs::path file, Real start_time = -1, Real time_step = 1,
99  Real end_time = -1,
100  CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
102  std::vector<Real>
103  readPQData(fs::path file, Real start_time = -1, Real time_step = 1,
104  Real end_time = -1,
105  CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
107  void assignLoadProfile(
108  SystemTopology &sys, Real start_time = -1, Real time_step = 1,
109  Real end_time = -1, CSVReader::Mode mode = CSVReader::Mode::AUTO,
110  CSVReader::DataFormat format = CSVReader::DataFormat::SECONDS);
112  void assignPVGeneration(SystemTopology &sys, Real start_time = -1,
113  Real time_step = 1, Real end_time = -1,
114  CSVReader::Mode mode = CSVReader::Mode::AUTO);
115 
117  PQData interpol_linear(std::map<Real, PQData> &data_PQ, Real x);
118 
120  Real interpol_linear(std::map<Real, Real> &data_wf, Real x);
121 };
122 
123 // #### csv reader section
124 class CSVRow {
125 public:
127  String const &get(std::size_t index) const { return m_data[index]; }
129  Int size() const;
131  void readNextRow(std::istream &str);
132 
133 private:
135  std::vector<String> m_data;
136 };
137 
139 public:
140  CSVReaderIterator(std::istream &str);
142 
143  CSVReaderIterator &next();
144  CSVReaderIterator next(Int);
145  CSVReaderIterator &step(Int time_step);
146  CSVRow const &operator*() const { return m_row; };
147  Bool operator==(CSVReaderIterator const &rhs) {
148  return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));
149  }
150  Bool operator!=(CSVReaderIterator const &rhs) { return !((*this) == rhs); }
151 
152 private:
153  std::istream *m_str;
154  CSVRow m_row;
155 };
156 } // namespace CPS
reads load profiles (csv files only) and assign them to the corresponding load object
Definition: CSVReader.h:28
PowerProfile readLoadProfile(fs::path file, Real start_time=-1, Real time_step=1, Real end_time=-1, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
Definition: CSVReader.cpp:360
PQData interpol_linear(std::map< Real, PQData > &data_PQ, Real x)
interpolation for PQ data points
void doSkipFirstRow(Bool value=true)
Skip first row if it has no digits at beginning.
Definition: CSVReader.h:69
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
Definition: CSVReader.cpp:479
Real interpol_linear(std::map< Real, Real > &data_wf, Real x)
interpolation for weighting factor data points
Real time_format_convert(const String &time)
Definition: CSVReader.cpp:124