Use a single radar product manager across multiple maps

This commit is contained in:
Dan Paulat 2021-11-22 14:58:48 -06:00
parent adde167899
commit 4485b915c1
3 changed files with 54 additions and 10 deletions

View file

@ -4,6 +4,7 @@
#include <deque> #include <deque>
#include <execution> #include <execution>
#include <mutex>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/range/irange.hpp> #include <boost/range/irange.hpp>
@ -30,25 +31,34 @@ static constexpr uint32_t NUM_COORIDNATES_0_5_DEGREE =
static constexpr uint32_t NUM_COORIDNATES_1_DEGREE = static constexpr uint32_t NUM_COORIDNATES_1_DEGREE =
NUM_RADIAL_GATES_1_DEGREE * 2; NUM_RADIAL_GATES_1_DEGREE * 2;
// TODO: Configure this in settings for radar loop // TODO: Find a way to garbage collect this
static constexpr size_t MAX_LEVEL2_FILES = 6; static std::unordered_map<std::string, std::shared_ptr<RadarProductManager>>
instanceMap_;
class RadarProductManagerImpl class RadarProductManagerImpl
{ {
public: public:
explicit RadarProductManagerImpl() {} explicit RadarProductManagerImpl(const std::string& radarSite) :
radarSite_ {radarSite}, initialized_ {false}
{
}
~RadarProductManagerImpl() = default; ~RadarProductManagerImpl() = default;
std::string radarSite_;
bool initialized_;
std::vector<float> coordinates0_5Degree_; std::vector<float> coordinates0_5Degree_;
std::vector<float> coordinates1Degree_; std::vector<float> coordinates1Degree_;
std::map<std::chrono::system_clock::time_point, std::map<std::chrono::system_clock::time_point,
std::shared_ptr<wsr88d::Ar2vFile>> std::shared_ptr<wsr88d::Ar2vFile>>
level2VolumeScans_; level2VolumeScans_;
std::mutex fileLoadMutex_;
}; };
RadarProductManager::RadarProductManager() : RadarProductManager::RadarProductManager(const std::string& radarSite) :
p(std::make_unique<RadarProductManagerImpl>()) p(std::make_unique<RadarProductManagerImpl>(radarSite))
{ {
} }
RadarProductManager::~RadarProductManager() = default; RadarProductManager::~RadarProductManager() = default;
@ -79,6 +89,11 @@ std::shared_ptr<const wsr88d::Ar2vFile> RadarProductManager::level2_data() const
void RadarProductManager::Initialize() void RadarProductManager::Initialize()
{ {
if (p->initialized_)
{
return;
}
BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Initialize()"; BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Initialize()";
boost::timer::cpu_timer timer; boost::timer::cpu_timer timer;
@ -162,6 +177,8 @@ void RadarProductManager::Initialize()
BOOST_LOG_TRIVIAL(debug) BOOST_LOG_TRIVIAL(debug)
<< logPrefix_ << "Coordinates (1 degree) calculated in " << logPrefix_ << "Coordinates (1 degree) calculated in "
<< timer.format(6, "%ws"); << timer.format(6, "%ws");
p->initialized_ = true;
} }
void RadarProductManager::LoadLevel2Data(const std::string& filename) void RadarProductManager::LoadLevel2Data(const std::string& filename)
@ -169,6 +186,11 @@ void RadarProductManager::LoadLevel2Data(const std::string& filename)
std::shared_ptr<wsr88d::Ar2vFile> ar2vFile = std::shared_ptr<wsr88d::Ar2vFile> ar2vFile =
std::make_shared<wsr88d::Ar2vFile>(); std::make_shared<wsr88d::Ar2vFile>();
if (!p->initialized_)
{
Initialize();
}
bool success = ar2vFile->LoadFile(filename); bool success = ar2vFile->LoadFile(filename);
if (!success) if (!success)
{ {
@ -200,17 +222,38 @@ RadarProductManager::GetLevel2Data(wsr88d::rda::DataBlockType dataBlockType,
else else
{ {
scwx::util::async([&]() { scwx::util::async([&]() {
std::lock_guard<std::mutex> guard(p->fileLoadMutex_);
BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Start load";
QString filename = qgetenv("AR2V_FILE"); QString filename = qgetenv("AR2V_FILE");
if (!filename.isEmpty()) if (!filename.isEmpty() && p->level2VolumeScans_.size() == 0)
{ {
LoadLevel2Data(filename.toUtf8().constData()); LoadLevel2Data(filename.toUtf8().constData());
} }
BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "End load";
}); });
} }
return std::tie(radarData, elevationCut, elevationCuts); return std::tie(radarData, elevationCut, elevationCuts);
} }
std::shared_ptr<RadarProductManager>
RadarProductManager::Instance(const std::string& radarSite)
{
static std::mutex instanceMutex;
std::lock_guard<std::mutex> guard(instanceMutex);
if (!instanceMap_.contains(radarSite))
{
instanceMap_[radarSite] =
std::make_shared<RadarProductManager>(radarSite);
}
return instanceMap_[radarSite];
}
} // namespace manager } // namespace manager
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -22,7 +22,7 @@ class RadarProductManager : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit RadarProductManager(); explicit RadarProductManager(const std::string& radarSite);
~RadarProductManager(); ~RadarProductManager();
const std::vector<float>& coordinates(common::RadialSize radialSize) const; const std::vector<float>& coordinates(common::RadialSize radialSize) const;
@ -40,6 +40,9 @@ public:
float elevation, float elevation,
std::chrono::system_clock::time_point time = {}); std::chrono::system_clock::time_point time = {});
static std::shared_ptr<RadarProductManager>
Instance(const std::string& radarSite);
signals: signals:
void Level2DataLoaded(); void Level2DataLoaded();

View file

@ -53,7 +53,7 @@ public:
widget_ {widget}, widget_ {widget},
settings_(settings), settings_(settings),
map_(), map_(),
radarProductManager_ {std::make_shared<manager::RadarProductManager>()}, radarProductManager_ {manager::RadarProductManager::Instance("KLSX")},
radarProductLayer_ {nullptr}, radarProductLayer_ {nullptr},
radarProductView_ {nullptr}, radarProductView_ {nullptr},
overlayLayer_ {nullptr}, overlayLayer_ {nullptr},
@ -98,8 +98,6 @@ MapWidget::MapWidget(const QMapboxGLSettings& settings) :
p(std::make_unique<MapWidgetImpl>(this, settings)) p(std::make_unique<MapWidgetImpl>(this, settings))
{ {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
p->radarProductManager_->Initialize();
} }
MapWidget::~MapWidget() MapWidget::~MapWidget()