9#include <dpsim-models/CSVReader.h> 
   13MatrixRow CSVReader::csv2Eigen(
const String &path) {
 
   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);
 
   31void CSVRow::readNextRow(std::istream &str) {
 
   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()) {
 
   49int CSVRow::size()
 const { 
return m_data.size(); }
 
   53    m_row.readNextRow(*m_str);
 
   62  CSVReaderIterator tmp(*
this);
 
   68  while (time_step != 0) {
 
   70      m_row.readNextRow(*m_str);
 
   80CSVReaderIterator::CSVReaderIterator(std::istream &str)
 
   81    : m_str(str.good() ? &str : NULL) {
 
   85CSVReaderIterator::CSVReaderIterator() : m_str(NULL) {}
 
   87CSVReader::CSVReader(CPS::String name, std::list<fs::path> paths,
 
   88                     CPS::Logger::Level logLevel) {
 
   89  mSLog = Logger::get(name + 
"_csvReader", logLevel);
 
   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;
 
   99CSVReader::CSVReader(CPS::String name, CPS::String path,
 
  100                     CPS::Logger::Level logLevel) {
 
  101  mSLog = Logger::get(name + 
"_csvReader", logLevel);
 
  104  for (
const auto &entry : fs::directory_iterator(path)) {
 
  105    mFileList.push_back(entry.path());
 
  109CSVReader::CSVReader(CPS::String name, CPS::String path,
 
  110                     std::map<String, String> &assignList,
 
  111                     CPS::Logger::Level logLevel)
 
  114  mAssignPattern = assignList;
 
  117CSVReader::CSVReader(CPS::String name, std::list<fs::path> paths,
 
  118                     std::map<String, String> &assignList,
 
  119                     CPS::Logger::Level logLevel)
 
  121  mAssignPattern = assignList;
 
  127  if (sscanf(time.c_str(), 
"%d:%d:%d", &hh, &mm, &ss) >= 2) {
 
  128    secs = hh * 3600 + mm * 60 + ss;
 
 
  133std::vector<PQData> CSVReader::readLoadProfileDP(fs::path file, Real start_time,
 
  134                                                 Real time_step, Real end_time,
 
  136                                                 CSVReader::DataFormat format) {
 
  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)))
 
  157  for (; row_ != CSVReaderIterator(); row_.next()) {
 
  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;
 
  173                                        Real time_step, Real end_time,
 
  174                                        CSVReader::DataFormat format) {
 
  177  std::ifstream csvfile(file);
 
  178  bool need_that_conversion = (format == DataFormat::HHMMSS) ? 
true : 
false;
 
  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));
 
  215      load_profile.weightingFactors.insert(
 
  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) {
 
  237        load_profile.weightingFactors.insert(std::pair<Real, Real>(x, y));
 
  240        load_profile.pqData.insert(std::pair<Real, PQData>(x, y));
 
 
  249std::vector<Real> CSVReader::readPQData(fs::path file, Real start_time,
 
  250                                        Real time_step, Real end_time,
 
  251                                        CSVReader::DataFormat format) {
 
  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)))
 
  273  for (; row_ != CSVReaderIterator(); row_.next()) {
 
  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;
 
  285                                  Real time_step, Real end_time,
 
  287                                  CSVReader::DataFormat format) {
 
  290  case CSVReader::Mode::AUTO: {
 
  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());
 
  324  case CSVReader::Mode::MANUAL: {
 
  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;
 
reads load profiles (csv files only) and assign them to the corresponding load object
 
PowerProfile readLoadProfile(fs::path file, Real start_time=-1, Real time_step=1, Real end_time=-1, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
 
PQData interpol_linear(std::map< Real, PQData > &data_PQ, Real x)
interpolation for PQ data points
 
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)
 
IdentifiedObject::List mComponents
List of network components.