From e5c40b9eb561c7b23385b27c23d1466eea92fb86 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sat, 22 Jan 2022 14:09:42 -0600 Subject: [PATCH] Text product files --- test/data | 2 +- .../scwx/awips/text_product_file.test.cpp | 39 +++++++++ test/source/scwx/wsr88d/level3_file.test.cpp | 6 +- test/test.cmake | 3 + .../include/scwx/awips/text_product_file.hpp | 38 ++++++++ .../source/scwx/awips/text_product_file.cpp | 86 +++++++++++++++++++ wxdata/wxdata.cmake | 2 + 7 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 test/source/scwx/awips/text_product_file.test.cpp create mode 100644 wxdata/include/scwx/awips/text_product_file.hpp create mode 100644 wxdata/source/scwx/awips/text_product_file.cpp diff --git a/test/data b/test/data index c1d6d76c..9d377550 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit c1d6d76c56f75d8f68b6d7dd8f223b0a199c6e36 +Subproject commit 9d377550477ba00b9da3b594b838ba08d3ebcaaf diff --git a/test/source/scwx/awips/text_product_file.test.cpp b/test/source/scwx/awips/text_product_file.test.cpp new file mode 100644 index 00000000..30991b8f --- /dev/null +++ b/test/source/scwx/awips/text_product_file.test.cpp @@ -0,0 +1,39 @@ +#include + +#include + +#include + +namespace scwx +{ +namespace awips +{ + +static const std::string logPrefix_ = "[scwx::awips::text_product_file.test] "; + +class TextProductValidFileTest : public testing::TestWithParam +{ +}; + +TEST_P(TextProductValidFileTest, ValidFile) +{ + TextProductFile file; + + auto param = GetParam(); + + const std::string filename {std::string(SCWX_TEST_DATA_DIR) + param}; + file.LoadFile(filename); + + EXPECT_GT(file.message_count(), 0); +} + +INSTANTIATE_TEST_SUITE_P( + TextProductFile, + TextProductValidFileTest, + testing::Values("/warnings/warnings_20210604_21.txt", + "/warnings/warnings_20210606_15.txt", + "/warnings/warnings_20210606_22-59.txt", + "/nexrad/level3/KLSX_NOUS63_FTMLSX_202201041404")); + +} // namespace awips +} // namespace scwx diff --git a/test/source/scwx/wsr88d/level3_file.test.cpp b/test/source/scwx/wsr88d/level3_file.test.cpp index 145e8ce6..f833ffd9 100644 --- a/test/source/scwx/wsr88d/level3_file.test.cpp +++ b/test/source/scwx/wsr88d/level3_file.test.cpp @@ -7,12 +7,12 @@ namespace scwx namespace wsr88d { -class ValidFileTest : +class Level3ValidFileTest : public testing::TestWithParam> { }; -TEST_P(ValidFileTest, ValidFile) +TEST_P(Level3ValidFileTest, ValidFile) { Level3File file; @@ -31,7 +31,7 @@ TEST_P(ValidFileTest, ValidFile) INSTANTIATE_TEST_SUITE_P( Level3File, - ValidFileTest, + Level3ValidFileTest, testing::Values( std::pair {2, "KLSX_NXUS63_GSMLSX_202112110238"}, std::pair {19, "KLSX_SDUS53_N0RLSX_202105041639"}, diff --git a/test/test.cmake b/test/test.cmake index fa5d8783..03a62516 100644 --- a/test/test.cmake +++ b/test/test.cmake @@ -8,6 +8,7 @@ find_package(BZip2) find_package(GTest) set(SRC_MAIN source/scwx/wxtest.cpp) +set(SRC_AWIPS_TESTS source/scwx/awips/text_product_file.test.cpp) set(SRC_COMMON_TESTS source/scwx/common/color_table.test.cpp) set(SRC_QT_MANAGER_TESTS source/scwx/qt/manager/settings_manager.test.cpp) set(SRC_UTIL_TESTS source/scwx/util/rangebuf.test.cpp @@ -17,12 +18,14 @@ set(SRC_WSR88D_TESTS source/scwx/wsr88d/ar2v_file.test.cpp source/scwx/wsr88d/level3_file.test.cpp) add_executable(wxtest ${SRC_MAIN} + ${SRC_AWIPS_TESTS} ${SRC_COMMON_TESTS} ${SRC_QT_MANAGER_TESTS} ${SRC_UTIL_TESTS} ${SRC_WSR88D_TESTS}) source_group("Source Files\\main" FILES ${SRC_MAIN}) +source_group("Source Files\\awips" FILES ${SRC_AWIPS_TESTS}) source_group("Source Files\\common" FILES ${SRC_COMMON_TESTS}) source_group("Source Files\\qt\\manager" FILES ${SRC_QT_MANAGER_TESTS}) source_group("Source Files\\util" FILES ${SRC_UTIL_TESTS}) diff --git a/wxdata/include/scwx/awips/text_product_file.hpp b/wxdata/include/scwx/awips/text_product_file.hpp new file mode 100644 index 00000000..eec4baa2 --- /dev/null +++ b/wxdata/include/scwx/awips/text_product_file.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include + +#include +#include + +namespace scwx +{ +namespace awips +{ + +class TextProductFileImpl; + +class TextProductFile +{ +public: + explicit TextProductFile(); + ~TextProductFile(); + + TextProductFile(const TextProductFile&) = delete; + TextProductFile& operator=(const TextProductFile&) = delete; + + TextProductFile(TextProductFile&&) noexcept; + TextProductFile& operator=(TextProductFile&&) noexcept; + + size_t message_count() const; + std::shared_ptr message(size_t i) const; + + bool LoadFile(const std::string& filename); + bool LoadData(std::istream& is); + +private: + std::unique_ptr p; +}; + +} // namespace awips +} // namespace scwx diff --git a/wxdata/source/scwx/awips/text_product_file.cpp b/wxdata/source/scwx/awips/text_product_file.cpp new file mode 100644 index 00000000..847e643c --- /dev/null +++ b/wxdata/source/scwx/awips/text_product_file.cpp @@ -0,0 +1,86 @@ +#include + +#include + +#include + +namespace scwx +{ +namespace awips +{ + +static const std::string logPrefix_ = "[scwx::awips::text_product_file] "; + +class TextProductFileImpl +{ +public: + explicit TextProductFileImpl() : messages_ {} {}; + ~TextProductFileImpl() = default; + + std::vector> messages_; +}; + +TextProductFile::TextProductFile() : p(std::make_unique()) +{ +} +TextProductFile::~TextProductFile() = default; + +TextProductFile::TextProductFile(TextProductFile&&) noexcept = default; +TextProductFile& +TextProductFile::operator=(TextProductFile&&) noexcept = default; + +size_t TextProductFile::message_count() const +{ + return p->messages_.size(); +} + +std::shared_ptr TextProductFile::message(size_t i) const +{ + return p->messages_[i]; +} + +bool TextProductFile::LoadFile(const std::string& filename) +{ + BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "LoadFile(" << filename << ")"; + bool fileValid = true; + + std::ifstream f(filename, std::ios_base::in | std::ios_base::binary); + if (!f.good()) + { + BOOST_LOG_TRIVIAL(warning) + << logPrefix_ << "Could not open file for reading: " << filename; + fileValid = false; + } + + if (fileValid) + { + fileValid = LoadData(f); + } + + return fileValid; +} + +bool TextProductFile::LoadData(std::istream& is) +{ + BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Loading Data"; + + while (!is.eof()) + { + std::shared_ptr message = + TextProductMessage::Create(is); + + if (message != nullptr) + { + p->messages_.push_back(message); + } + else + { + break; + } + } + + return !p->messages_.empty(); +} + +} // namespace awips +} // namespace scwx diff --git a/wxdata/wxdata.cmake b/wxdata/wxdata.cmake index 65ec6117..e67ca419 100644 --- a/wxdata/wxdata.cmake +++ b/wxdata/wxdata.cmake @@ -3,9 +3,11 @@ project(scwx-data) find_package(Boost) set(HDR_AWIPS include/scwx/awips/message.hpp + include/scwx/awips/text_product_file.hpp include/scwx/awips/text_product_message.hpp include/scwx/awips/wmo_header.hpp) set(SRC_AWIPS source/scwx/awips/message.cpp + source/scwx/awips/text_product_file.cpp source/scwx/awips/text_product_message.cpp source/scwx/awips/wmo_header.cpp) set(HDR_COMMON include/scwx/common/characters.hpp