From 72d2dfb7eda334712d3bd64ab10f72dd14047786 Mon Sep 17 00:00:00 2001 From: GPayne Date: Thu, 12 Dec 2019 16:03:03 -0700 Subject: [PATCH] Fixiing problems with reading csv file --- modules/space/CMakeLists.txt | 2 + .../space/rendering/renderablesatellites.cpp | 8 - .../space/rendering/renderablesatellites.h | 10 + .../space/rendering/renderablesmallbody.cpp | 199 ++++++------------ 4 files changed, 71 insertions(+), 148 deletions(-) diff --git a/modules/space/CMakeLists.txt b/modules/space/CMakeLists.txt index a2cf29a899..d6632f40d6 100644 --- a/modules/space/CMakeLists.txt +++ b/modules/space/CMakeLists.txt @@ -29,6 +29,7 @@ set(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderableconstellationbounds.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablerings.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablesatellites.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablesmallbody.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablestars.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/simplespheregeometry.h ${CMAKE_CURRENT_SOURCE_DIR}/translation/keplertranslation.h @@ -44,6 +45,7 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderableconstellationbounds.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablerings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablesatellites.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablesmallbody.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderablestars.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/simplespheregeometry.cpp ${CMAKE_CURRENT_SOURCE_DIR}/translation/keplertranslation.cpp diff --git a/modules/space/rendering/renderablesatellites.cpp b/modules/space/rendering/renderablesatellites.cpp index 7453ff44f5..585157e4c3 100644 --- a/modules/space/rendering/renderablesatellites.cpp +++ b/modules/space/rendering/renderablesatellites.cpp @@ -84,14 +84,6 @@ namespace { } namespace openspace { - - // The list of leap years only goes until 2056 as we need to touch this file then - // again anyway ;) - const std::vector LeapYears = { - 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, - 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, - 2044, 2048, 2052, 2056 - }; // Count the number of full days since the beginning of 2000 to the beginning of // the parameter 'year' diff --git a/modules/space/rendering/renderablesatellites.h b/modules/space/rendering/renderablesatellites.h index e052683cfc..0c230a6fe0 100644 --- a/modules/space/rendering/renderablesatellites.h +++ b/modules/space/rendering/renderablesatellites.h @@ -37,6 +37,16 @@ namespace openspace { +const std::vector LeapYears = { + 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, + 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, + 2044, 2048, 2052, 2056 +}; +int countDays(int year); +int countLeapSeconds(int year, int dayOfYear); +double calculateSemiMajorAxis(double meanMotion); +double epochFromSubstring(const std::string& epochString); + class RenderableSatellites : public Renderable { public: RenderableSatellites(const ghoul::Dictionary& dictionary); diff --git a/modules/space/rendering/renderablesmallbody.cpp b/modules/space/rendering/renderablesmallbody.cpp index 6602658b11..82185536d5 100644 --- a/modules/space/rendering/renderablesmallbody.cpp +++ b/modules/space/rendering/renderablesmallbody.cpp @@ -24,6 +24,7 @@ #include +#include #include #include #include @@ -84,14 +85,6 @@ namespace { } namespace openspace { - -// The list of leap years only goes until 2056 as we need to touch this file then -// again anyway, due to TLE format only supporting 2-digit years starting in 1957. -const std::vector LeapYears = { - 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, - 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, - 2044, 2048, 2052, 2056 -}; const std::vector DaysOfMonths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 @@ -112,100 +105,6 @@ enum Months { December }; -// Count the number of full days since the beginning of 2000 to the beginning of -// the parameter 'year' -int countDays(int year) { - // Find the position of the current year in the vector, the difference - // between its position and the position of 2000 (for J2000) gives the - // number of leap years - constexpr const int Epoch = 2000; - constexpr const int DaysRegularYear = 365; - constexpr const int DaysLeapYear = 366; - - if (year == Epoch) { - return 0; - } - - // Get the position of the most recent leap year - const auto lb = std::lower_bound(LeapYears.begin(), LeapYears.end(), year); - - // Get the position of the epoch - const auto y2000 = std::find(LeapYears.begin(), LeapYears.end(), Epoch); - - // The distance between the two iterators gives us the number of leap years - const int nLeapYears = static_cast(std::abs(std::distance(y2000, lb))); - - const int nYears = std::abs(year - Epoch); - const int nRegularYears = nYears - nLeapYears; - - // Get the total number of days as the sum of leap years + non leap years - const int result = nRegularYears * DaysRegularYear + nLeapYears * DaysLeapYear; - return result; -} - -// Returns the number of leap seconds that lie between the {year, dayOfYear} -// time point and { 2000, 1 } -int countLeapSeconds(int year, int dayOfYear) { - // Find the position of the current year in the vector; its position in - // the vector gives the number of leap seconds - struct LeapSecond { - int year; - int dayOfYear; - bool operator<(const LeapSecond& rhs) const { - return std::tie(year, dayOfYear) < std::tie(rhs.year, rhs.dayOfYear); - } - }; - - const LeapSecond Epoch = { 2000, 1 }; - - // List taken from: https://www.ietf.org/timezones/data/leap-seconds.list - static const std::vector LeapSeconds = { - { 1972, 1 }, - { 1972, 183 }, - { 1973, 1 }, - { 1974, 1 }, - { 1975, 1 }, - { 1976, 1 }, - { 1977, 1 }, - { 1978, 1 }, - { 1979, 1 }, - { 1980, 1 }, - { 1981, 182 }, - { 1982, 182 }, - { 1983, 182 }, - { 1985, 182 }, - { 1988, 1 }, - { 1990, 1 }, - { 1991, 1 }, - { 1992, 183 }, - { 1993, 182 }, - { 1994, 182 }, - { 1996, 1 }, - { 1997, 182 }, - { 1999, 1 }, - { 2006, 1 }, - { 2009, 1 }, - { 2012, 183 }, - { 2015, 182 }, - { 2017, 1 } - }; - - // Get the position of the last leap second before the desired date - LeapSecond date { year, dayOfYear }; - const auto it = std::lower_bound(LeapSeconds.begin(), LeapSeconds.end(), date); - - // Get the position of the Epoch - const auto y2000 = std::lower_bound( - LeapSeconds.begin(), - LeapSeconds.end(), - Epoch - ); - - // The distance between the two iterators gives us the number of leap years - const int nLeapSeconds = static_cast(std::abs(std::distance(y2000, it))); - return nLeapSeconds; -} - int daysIntoGivenYear(int year, int month, int dayOfMonth) { //month and dayCount are zero-based month -= 1; @@ -217,7 +116,7 @@ int daysIntoGivenYear(int year, int month, int dayOfMonth) { return dayCount; } -double epochFromSubstring(const std::string& epochString) { +double epochFromYMDdSubstring(const std::string& epochString) { // The epochString is in the form: // YYYYMMDD.ddddddd // With YYYY as the year, MM the month (1 - 12), DD the day of month (1-31), @@ -367,54 +266,74 @@ void RenderableSmallBody::readJplSbDb(const std::string& filename) { std::string line; std::string name; std::string ignore; - std::getline(file, line); // get rid of first line (header) - for (std::streamoff i = 0; i < numberOfLines; i++) { - KeplerParameters keplerElements; - // Object designator string - std::getline(file, name, ','); + std::streamoff csvLine; - // (Ignore object status for NEO, PHA, diameter) - std::getline(file, ignore, ','); - std::getline(file, ignore, ','); - std::getline(file, ignore, ','); + try { + std::getline(file, line); // get rid of first line (header) + for (csvLine = 0; csvLine < numberOfLines; csvLine++) { + KeplerParameters keplerElements; + // Object designator string + std::getline(file, name, ','); - // Eccentricity (unit-less) - std::getline(file, ignore, ','); - keplerElements.eccentricity = std::stod(ignore); + // (Ignore object status for NEO, PHA, diameter) + std::getline(file, ignore, ','); + std::getline(file, ignore, ','); + std::getline(file, ignore, ','); - // Semi-major axis (astronomical units - au) - std::getline(file, ignore, ','); - keplerElements.semiMajorAxis = std::stod(ignore); - keplerElements.semiMajorAxis *= convertAuToKm; + // Ignore date + std::getline(file, ignore, ','); - // Inclination (degrees) - std::getline(file, ignore, ','); - keplerElements.inclination = std::stod(ignore); + // Eccentricity (unit-less) + std::getline(file, ignore, ','); + keplerElements.eccentricity = std::stod(ignore); - // Longitude of ascending node (degrees) - std::getline(file, ignore, ','); - keplerElements.ascendingNode = std::stod(ignore); + // Semi-major axis (astronomical units - au) + std::getline(file, ignore, ','); + keplerElements.semiMajorAxis = std::stod(ignore); + keplerElements.semiMajorAxis *= convertAuToKm; - // Argument of Periapsis (degrees) - std::getline(file, ignore, ','); - keplerElements.argumentOfPeriapsis = std::stod(ignore); + // Inclination (degrees) + std::getline(file, ignore, ','); + keplerElements.inclination = std::stod(ignore); - // Mean Anomaly (degrees) - std::getline(file, ignore, ','); - keplerElements.meanAnomaly = std::stod(ignore); + // Longitude of ascending node (degrees) + std::getline(file, ignore, ','); + keplerElements.ascendingNode = std::stod(ignore); - // Epoch (MJD) - std::getline(file, ignore, ','); - keplerElements.epoch = epochFromSubstring(ignore); + // Argument of Periapsis (degrees) + std::getline(file, ignore, ','); + keplerElements.argumentOfPeriapsis = std::stod(ignore); - // Period (days) - std::getline(file, ignore, ','); - keplerElements.period = std::stod(ignore); - keplerElements.period *= convertDaysToSecs; + // Mean Anomaly (degrees) + std::getline(file, ignore, ','); + keplerElements.meanAnomaly = std::stod(ignore); - _sbData.push_back(keplerElements); - _sbNames.push_back(name); + // Epoch (MJD) + std::getline(file, ignore, ','); + keplerElements.epoch = epochFromYMDdSubstring(ignore); + + // Period (days) + std::getline(file, ignore); + keplerElements.period = std::stod(ignore); + keplerElements.period *= convertDaysToSecs; + + _sbData.push_back(keplerElements); + _sbNames.push_back(name); + } } + catch (std::invalid_argument&) { + LERROR(fmt::format( + "invalid_argument exception on line {} of {}", + csvLine - 1, filename + )); + } + catch (std::out_of_range&) { + LERROR(fmt::format( + "out_of_range exception on line {} of {}", + csvLine - 1, filename + )); + } + file.close(); }