-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from sfc-aqua/paolo-CSVParser
This PR introduces a new OrbitalDataParser object among the utils. This will be useful to read the orbital data used in the following PRs for satellite links.
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |