mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 07:50:04 +00:00
Provide year/month hint to WMO header parser based on filename
This commit is contained in:
parent
46972e8769
commit
a6ba312f6b
4 changed files with 31 additions and 5 deletions
|
|
@ -29,7 +29,7 @@ public:
|
|||
std::shared_ptr<TextProductMessage> message(size_t i) const;
|
||||
|
||||
bool LoadFile(const std::string& filename);
|
||||
bool LoadData(std::istream& is);
|
||||
bool LoadData(std::string_view filename, std::istream& is);
|
||||
|
||||
private:
|
||||
std::unique_ptr<TextProductFileImpl> p;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include <fstream>
|
||||
|
||||
#include <re2/re2.h>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace awips
|
||||
|
|
@ -59,16 +61,34 @@ bool TextProductFile::LoadFile(const std::string& filename)
|
|||
|
||||
if (fileValid)
|
||||
{
|
||||
fileValid = LoadData(f);
|
||||
fileValid = LoadData(filename, f);
|
||||
}
|
||||
|
||||
return fileValid;
|
||||
}
|
||||
|
||||
bool TextProductFile::LoadData(std::istream& is)
|
||||
bool TextProductFile::LoadData(std::string_view filename, std::istream& is)
|
||||
{
|
||||
static constexpr LazyRE2 kDateTimePattern_ = {
|
||||
R"(((?:19|20)\d{2}))" // Year (YYYY)
|
||||
R"((0[1-9]|1[0-2]))" // Month (MM)
|
||||
R"((0[1-9]|[12]\d|3[01]))" // Day (DD)
|
||||
R"(_?)" // Optional separator (not captured)
|
||||
R"(([01]\d|2[0-3]))" // Hour (HH)
|
||||
};
|
||||
|
||||
logger_->trace("Loading Data");
|
||||
|
||||
// Attempt to parse the date from the filename
|
||||
std::optional<std::chrono::year_month> yearMonth;
|
||||
int year {};
|
||||
unsigned int month {};
|
||||
|
||||
if (RE2::PartialMatch(filename, *kDateTimePattern_, &year, &month))
|
||||
{
|
||||
yearMonth = std::chrono::year {year} / std::chrono::month {month};
|
||||
}
|
||||
|
||||
while (!is.eof())
|
||||
{
|
||||
std::shared_ptr<TextProductMessage> message =
|
||||
|
|
@ -88,6 +108,11 @@ bool TextProductFile::LoadData(std::istream& is)
|
|||
|
||||
if (!duplicate)
|
||||
{
|
||||
if (yearMonth.has_value())
|
||||
{
|
||||
message->wmo_header()->SetDateHint(yearMonth.value());
|
||||
}
|
||||
|
||||
p->messages_.push_back(message);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,10 +163,11 @@ IemApiProvider::LoadTextProducts(const std::vector<std::string>& textProducts)
|
|||
if (response.status_code == cpr::status::HTTP_OK)
|
||||
{
|
||||
// Load file
|
||||
auto productId = asyncResponse.first;
|
||||
std::shared_ptr<awips::TextProductFile> textProductFile {
|
||||
std::make_shared<awips::TextProductFile>()};
|
||||
std::istringstream responseBody {response.text};
|
||||
if (textProductFile->LoadData(responseBody))
|
||||
if (textProductFile->LoadData(productId, responseBody))
|
||||
{
|
||||
textProductFiles.push_back(textProductFile);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ WarningsProvider::LoadUpdatedFiles(
|
|||
std::shared_ptr<awips::TextProductFile> textProductFile {
|
||||
std::make_shared<awips::TextProductFile>()};
|
||||
std::istringstream responseBody {response.text};
|
||||
if (textProductFile->LoadData(responseBody))
|
||||
if (textProductFile->LoadData(filename, responseBody))
|
||||
{
|
||||
updatedFiles.push_back(textProductFile);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue