Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SATELLITE LINKS] PR 1: CSV Parsing module in utils #555

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions quisp/utils/OrbitalDataParser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* OrbitalDataParser.cc
*
* Created on: Nov 28, 2023
* Author: paolo
* Quick parser for CSVs, written for csv-based Free-space channels.
*/

#include "OrbitalDataParser.h"

OrbitalDataParser::OrbitalDataParser(const string filename) {
file.open(filename);
if (!file.is_open()) throw cRuntimeError("Couldn't find CSV file!");
string line;
double key = 0;
double val = 0;
char sep; // throwaway
while (file >> line) {
std::istringstream ss(line);
ss >> key >> sep >> val;
property.insert(std::pair<double, double>(key, val));
}
return;
}

double OrbitalDataParser::getPropertyAtTime(const double time) {
if (time < getLowestDatapoint()) return getLowestDatavalue();
if (time > getHighestDatapoint()) return getHighestDatavalue();

// These two first lines are there just to provide mathematically sound results.
// They should never be used in actual simulation, as they are NOT in any way
// guaranteed to be accurate.

if (time != last_polled_time) {
last_polled_time = time;
auto u_b = property.upper_bound(time);
if (u_b == property.end()) {
last_polled_value = (--u_b)->second;
} else if (u_b == property.begin()) {
last_polled_value = u_b->second;
} else {
auto l_b = u_b;
--l_b;
const double delta = (time - l_b->first) / (u_b->first - l_b->first);
last_polled_value = delta * u_b->second + (1 - delta) * l_b->second;
}
}
return last_polled_value;
}

double OrbitalDataParser::getLowestDatapoint() { return property.begin()->first; }

double OrbitalDataParser::getHighestDatapoint() { return property.rbegin()->first; }

double OrbitalDataParser::getLowestDatavalue() { return property.begin()->second; }

double OrbitalDataParser::getHighestDatavalue() { return property.rbegin()->second; }

OrbitalDataParser::~OrbitalDataParser() {}
35 changes: 35 additions & 0 deletions quisp/utils/OrbitalDataParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* OrbitalDataParser.h
*
* Created on: Nov 28, 2023
* Author: paolo
*/

#pragma once

#include <omnetpp.h>
#include <fstream>
#include <map>

using namespace omnetpp;
using std::string;

class OrbitalDataParser {
public:
OrbitalDataParser();
OrbitalDataParser(const string filename);
virtual ~OrbitalDataParser();
double getPropertyAtTime(const double time);
double getLowestDatapoint();
double getHighestDatapoint();
double getLowestDatavalue();
double getHighestDatavalue();
char* getName;

private:
std::ifstream file;
char* name;
double last_polled_time = -1;
double last_polled_value = -1;
std::map<double, double> property;
};
56 changes: 56 additions & 0 deletions quisp/utils/OrbitalDataParser_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <gtest/gtest.h>

#include <test_utils/TestUtils.h>

#include <fstream>

#include "OrbitalDataParser.h"

#include <cstdio>

using namespace quisp_test;
using namespace quisp_test::utils;
using namespace quisp::messages;
using namespace quisp::modules::SharedResource;

namespace {

class OrbitalDataParserTest : public ::testing::Test {
protected:
void SetUp() {
csv_to_generate.open("channels/test_csv.csv");
csv_to_generate << "200,100000\n";
csv_to_generate << "300,300000\n";
csv_to_generate << "400,200000\n";
csv_to_generate.close();
csv_parser = new OrbitalDataParser("channels/test_csv.csv");
}
void TearDown() { std::remove("channels/test_csv.csv"); }

OrbitalDataParser* csv_parser;
std::ofstream csv_to_generate;
};

TEST_F(OrbitalDataParserTest, lowerBound) {
ASSERT_DOUBLE_EQ(csv_parser->getLowestDatapoint(), 200);
ASSERT_DOUBLE_EQ(csv_parser->getLowestDatavalue(), 100000);
}

TEST_F(OrbitalDataParserTest, upperBound) {
ASSERT_DOUBLE_EQ(csv_parser->getHighestDatapoint(), 400);
ASSERT_DOUBLE_EQ(csv_parser->getHighestDatavalue(), 200000);
}

TEST_F(OrbitalDataParserTest, lowerThanLB) { ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(100), 100000); }

TEST_F(OrbitalDataParserTest, higherThanUB) { ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(500), 200000); }

TEST_F(OrbitalDataParserTest, normalOperation) {
ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(250), 200000);
ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(300), 300000);
ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(350), 250000);
ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(234.5), 169000);
ASSERT_DOUBLE_EQ(csv_parser->getPropertyAtTime(287.4), 274800);
}

} // namespace
Loading