From c7b5a659abf47261289498357943e25e41ac79e8 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 3 Apr 2022 23:01:16 -0500 Subject: [PATCH] Generic product record retrieval for level 2 or level 3 data --- .../scwx/qt/manager/radar_product_manager.cpp | 81 ++++++++++++------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp index 50677406..f564ac96 100644 --- a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp @@ -26,6 +26,9 @@ static const std::string logPrefix_ = typedef std::function()> CreateNexradFileFunction; +typedef std::map> + RadarProductRecordMap; static constexpr uint32_t NUM_RADIAL_GATES_0_5_DEGREE = common::MAX_0_5_DEGREE_RADIALS * common::MAX_DATA_MOMENT_GATES; @@ -63,9 +66,15 @@ public: } ~RadarProductManagerImpl() = default; + static std::shared_ptr + GetRadarProductRecord(RadarProductRecordMap& map, + std::chrono::system_clock::time_point time); std::shared_ptr GetLevel2ProductRecord(std::chrono::system_clock::time_point time); std::shared_ptr + GetLevel3ProductRecord(const std::string& product, + std::chrono::system_clock::time_point time); + std::shared_ptr StoreRadarProductRecord(std::shared_ptr record); static void @@ -80,13 +89,8 @@ public: std::vector coordinates0_5Degree_; std::vector coordinates1Degree_; - std::map> - level2ProductRecords_; - std::unordered_map>> - level3ProductRecords_; + RadarProductRecordMap level2ProductRecords_; + std::unordered_map level3ProductRecords_; }; RadarProductManager::RadarProductManager(const std::string& radarId) : @@ -302,46 +306,67 @@ void RadarProductManagerImpl::LoadNexradFile( } std::shared_ptr -RadarProductManagerImpl::GetLevel2ProductRecord( - std::chrono::system_clock::time_point time) +RadarProductManagerImpl::GetRadarProductRecord( + RadarProductRecordMap& map, std::chrono::system_clock::time_point time) { std::shared_ptr record = nullptr; // TODO: Round to minutes // Find the first product record greater than the time requested - auto it = level2ProductRecords_.upper_bound(time); + auto it = map.upper_bound(time); // A product record with a time greater was found - if (it != level2ProductRecords_.cend()) + if (it != map.cend()) { // Are there product records prior to this record? - if (it != level2ProductRecords_.cbegin()) + if (it != map.cbegin()) { // Get the product record immediately preceding, this the record we are // looking for - --it; - - // Does the record contain the time we are looking for? - if (it->second->level2_file()->start_time() <= time && - time <= it->second->level2_file()->end_time()) - { - record = it->second; - } + record = (--it)->second; } } - else if (level2ProductRecords_.size() > 0) + else if (map.size() > 0) { // A product record with a time greater was not found. If it exists, it // must be the last record. - auto rit = level2ProductRecords_.rbegin(); + record = map.rbegin()->second; + } - // Does the record contain the time we are looking for? - if (rit->second->level2_file()->start_time() <= time && - time <= rit->second->level2_file()->end_time()) - { - record = rit->second; - } + return record; +} + +std::shared_ptr +RadarProductManagerImpl::GetLevel2ProductRecord( + std::chrono::system_clock::time_point time) +{ + std::shared_ptr record = + GetRadarProductRecord(level2ProductRecords_, time); + + // TODO: Round to minutes + + // Does the record contain the time we are looking for? + if (record != nullptr && (time < record->level2_file()->start_time() || + record->level2_file()->end_time() < time)) + { + record = nullptr; + } + + return record; +} + +std::shared_ptr +RadarProductManagerImpl::GetLevel3ProductRecord( + const std::string& product, std::chrono::system_clock::time_point time) +{ + std::shared_ptr record = nullptr; + + auto it = level3ProductRecords_.find(product); + + if (it != level3ProductRecords_.cend()) + { + record = GetRadarProductRecord(it->second, time); } return record;