ECC-1620: Introduction of the "eccodes" namespace

This commit is contained in:
Eugen Betke 2023-12-06 15:58:51 +00:00
parent f414dc41de
commit 5bd9d62642
10 changed files with 88 additions and 73 deletions

View File

@ -214,8 +214,8 @@ static int convert_time_range_long_(
Assert(lengthOfTimeRange != NULL);
if (indicatorOfUnitForTimeRange != stepUnits) {
Step time_range{*lengthOfTimeRange, indicatorOfUnitForTimeRange};
time_range.set_unit(Unit{stepUnits});
eccodes::Step time_range{*lengthOfTimeRange, indicatorOfUnitForTimeRange};
time_range.set_unit(eccodes::Unit{stepUnits});
if (time_range.value<long>() != time_range.value<double>()) {
return GRIB_DECODING_ERROR;
}
@ -297,8 +297,8 @@ static int unpack_one_time_range_double_(grib_accessor* a, double *val , size_t*
if ((err = grib_get_long_internal(h, self->typeOfTimeIncrement, &typeOfTimeIncrement)))
return err;
Step start_step{start_step_value, start_step_unit};
Step time_range{time_range_value, time_range_unit};
eccodes::Step start_step{start_step_value, start_step_unit};
eccodes::Step time_range{time_range_value, time_range_unit};
if (typeOfTimeIncrement == 1) {
/* See GRIB-488 */
@ -309,10 +309,10 @@ static int unpack_one_time_range_double_(grib_accessor* a, double *val , size_t*
}
}
if (add_time_range) {
*val = (start_step + time_range).value<double>(Unit(step_units));
*val = (start_step + time_range).value<double>(eccodes::Unit(step_units));
}
else {
*val = start_step.value<double>(Unit(start_step_unit));
*val = start_step.value<double>(eccodes::Unit(start_step_unit));
}
return GRIB_SUCCESS;
@ -392,7 +392,7 @@ static int unpack_multiple_time_ranges_double_(grib_accessor* a, double* val, si
if ((err = grib_get_long_internal(h, "startStepUnit", &start_step_unit)))
return err;
Step start_step{start_step_value, start_step_unit};
eccodes::Step start_step{start_step_value, start_step_unit};
if ((err = grib_get_long_internal(h, self->step_units, &step_units)))
return err;
@ -420,8 +420,8 @@ static int unpack_multiple_time_ranges_double_(grib_accessor* a, double* val, si
long the_coded_unit = arr_coded_unit[i];
long the_coded_time_range = arr_coded_time_range[i];
Step time_range{the_coded_unit, the_coded_time_range};
*val = (start_step + time_range).value<double>(Unit(step_units));
eccodes::Step time_range{the_coded_unit, the_coded_time_range};
*val = (start_step + time_range).value<double>(eccodes::Unit(step_units));
return GRIB_SUCCESS;
}
@ -542,7 +542,7 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
double dend, dstep;
int show_hours = a->context->show_hour_stepunit;
Step end_step{end_step_value, end_step_unit};
eccodes::Step end_step{end_step_value, end_step_unit};
/*point in time */
if (self->year == NULL) {
@ -575,7 +575,7 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
if ((err= grib_get_long_internal(h, "forceStepUnits", &force_step_units)) != GRIB_SUCCESS)
return err;
if (Unit{start_step_unit} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{start_step_unit} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
grib_context_log(h->context, GRIB_LOG_ERROR,
"missing start step unit");
return GRIB_WRONG_STEP_UNIT;
@ -584,8 +584,8 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
if ((err = grib_get_long_internal(h, self->typeOfTimeIncrement, &typeOfTimeIncrement)))
return err;
Step start_step{start_step_value, start_step_unit};
Step time_range = end_step - start_step;
eccodes::Step start_step{start_step_value, start_step_unit};
eccodes::Step time_range = end_step - start_step;
if (time_range.value<double>() < 0) {
grib_context_log(h->context, GRIB_LOG_ERROR,
@ -597,7 +597,7 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
if (err != GRIB_SUCCESS)
return err;
dstep = end_step.value<double>(Unit{Unit::Value::DAY});
dstep = end_step.value<double>(eccodes::Unit{eccodes::Unit::Value::DAY});
dend += dstep;
err = grib_julian_to_datetime(dend, &year_of_end_of_interval, &month_of_end_of_interval,
@ -621,14 +621,14 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
const char* forecast_time_value_key = "forecastTime";
const char* forecast_time_unit_key = "indicatorOfUnitOfTimeRange";
Step forecast_time_opt;
Step time_range_opt;
if (Unit{force_step_units} == Unit{Unit::Value::MISSING}) {
eccodes::Step forecast_time_opt;
eccodes::Step time_range_opt;
if (eccodes::Unit{force_step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
std::tie(forecast_time_opt, time_range_opt) = find_common_units(start_step.optimize_unit(), time_range.optimize_unit());
}
else {
forecast_time_opt = Step{start_step.value<long>(Unit{force_step_units}), Unit{force_step_units}};
time_range_opt = Step{time_range.value<long>(Unit{force_step_units}), Unit{force_step_units}};
forecast_time_opt = eccodes::Step{start_step.value<long>(eccodes::Unit{force_step_units}), eccodes::Unit{force_step_units}};
time_range_opt = eccodes::Step{time_range.value<long>(eccodes::Unit{force_step_units}), eccodes::Unit{force_step_units}};
}
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), self->time_range_value, time_range_opt.value<long>())) != GRIB_SUCCESS)
@ -663,7 +663,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
return ret;
try {
Step step(step_value, step_units);
eccodes::Step step(step_value, step_units);
step.set_unit(step_units);
std::stringstream ss;
@ -698,12 +698,12 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
try {
long end_step_unit;
if (Unit{force_step_units} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{force_step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
if ((ret = grib_get_long_internal(h, "endStepUnit", &end_step_unit)) != GRIB_SUCCESS)
return ret;
if (Unit{end_step_unit} == Unit{Unit::Value::MISSING})
end_step_unit = Unit{Unit::Value::HOUR}.value<long>();
if (eccodes::Unit{end_step_unit} == eccodes::Unit{eccodes::Unit::Value::MISSING})
end_step_unit = eccodes::Unit{eccodes::Unit::Value::HOUR}.value<long>();
}
else {
end_step_unit = force_step_units;
@ -726,7 +726,7 @@ static int pack_string(grib_accessor* a, const char* val, size_t* len)
return ret;
try {
Step end_step = step_from_string(val, Unit{force_step_units});
eccodes::Step end_step = step_from_string(val, eccodes::Unit{force_step_units});
end_step.optimize_unit();
if ((ret = grib_set_long_internal(h, "endStepUnit", end_step.unit().value<long>())) != GRIB_SUCCESS)

View File

@ -149,7 +149,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
if ((ret= grib_get_long_internal(h, "stepUnits", &step_units)) != GRIB_SUCCESS)
return ret;
try {
if (Unit{step_units} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
if ((ret = grib_get_long_internal(h, "stepUnits", &step_units)) != GRIB_SUCCESS)
return ret;
}
@ -160,7 +160,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
return ret;
std::stringstream ss;
Step start_step{start_step_value, step_units};
eccodes::Step start_step{start_step_value, step_units};
if (self->end_step == NULL) {
ss << start_step.value<std::string>(fp_format, show_hours);
}
@ -168,7 +168,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
if ((ret = grib_get_double_internal(h, self->end_step, &end_step_value)) != GRIB_SUCCESS)
return ret;
Step end_step{end_step_value, step_units};
eccodes::Step end_step{end_step_value, step_units};
if (start_step_value == end_step_value) {
ss << end_step.value<std::string>(fp_format, show_hours);
@ -206,24 +206,24 @@ static int pack_string(grib_accessor* a, const char* val, size_t* len)
return ret;
try {
std::vector<Step> steps = parse_range(val, Unit{force_step_units});
std::vector<eccodes::Step> steps = parse_range(val, eccodes::Unit{force_step_units});
if (steps.size() == 0) {
grib_context_log(a->context, GRIB_LOG_ERROR, "Could not parse step range: %s", val);
return GRIB_INVALID_ARGUMENT;
}
Step step_0;
Step step_1;
if (Unit{force_step_units} == Unit{Unit::Value::MISSING}) {
eccodes::Step step_0;
eccodes::Step step_1;
if (eccodes::Unit{force_step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
if (steps.size() > 1)
std::tie(step_0, step_1) = find_common_units(steps[0].optimize_unit(), steps[1].optimize_unit());
else
step_0 = steps[0].optimize_unit();
}
else {
step_0 = Step{steps[0].value<long>(Unit{force_step_units}), Unit{force_step_units}};
step_0 = eccodes::Step{steps[0].value<long>(eccodes::Unit{force_step_units}), eccodes::Unit{force_step_units}};
if (steps.size() > 1) {
step_1 = Step{steps[1].value<long>(Unit{force_step_units}), Unit{force_step_units}};
step_1 = eccodes::Step{steps[1].value<long>(eccodes::Unit{force_step_units}), eccodes::Unit{force_step_units}};
}
}
@ -281,19 +281,19 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
try {
if ((ret = grib_get_long_internal(h, "stepUnits", &step_units)) != GRIB_SUCCESS)
throw std::runtime_error("Failed to get stepUnits");
if (Unit{step_units} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
if ((ret = grib_get_long_internal(h, "stepUnits", &step_units)) != GRIB_SUCCESS)
return ret;
}
Step start_step{end_start_value, step_units};
eccodes::Step start_step{end_start_value, step_units};
if (self->end_step == NULL) {
*val = start_step.value<long>();
}
else {
if ((ret = grib_get_long_internal(h, self->end_step, &end_step_value)) != GRIB_SUCCESS)
return ret;
Step end_step{end_step_value, step_units};
eccodes::Step end_step{end_step_value, step_units};
*val = end_step.value<long>();
}
}
@ -321,19 +321,19 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
throw std::runtime_error("Failed to get stepUnits");
try {
if (Unit{step_units} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
if ((ret = grib_get_long_internal(h, "stepUnits", &step_units)) != GRIB_SUCCESS)
return ret;
}
Step start_step{end_start_value, step_units};
eccodes::Step start_step{end_start_value, step_units};
if (self->end_step == NULL) {
*val = start_step.value<long>();
}
else {
if ((ret = grib_get_double_internal(h, self->end_step, &end_step_value)) != GRIB_SUCCESS)
return ret;
Step end_step{end_step_value, step_units};
eccodes::Step end_step{end_step_value, step_units};
*val = end_step.value<double>();
}
}

View File

@ -137,24 +137,24 @@ static size_t string_length(grib_accessor* a)
return 255;
}
static long staticStepUnits = Unit{Unit::Value::MISSING}.value<long>();
static long staticForceStepUnits = Unit{Unit::Value::MISSING}.value<long>();
static long staticStepUnits = eccodes::Unit{eccodes::Unit::Value::MISSING}.value<long>();
static long staticForceStepUnits = eccodes::Unit{eccodes::Unit::Value::MISSING}.value<long>();
static int pack_long(grib_accessor* a, const long* val, size_t* len)
{
grib_handle* h = grib_handle_of_accessor(a);
auto supported_units = Unit::list_supported_units();
auto supported_units = eccodes::Unit::list_supported_units();
try {
Unit unit{*val}; // throws if not supported
eccodes::Unit unit{*val}; // throws if not supported
auto iter = std::find(supported_units.begin(), supported_units.end(), unit);
if (iter == supported_units.end()) {
throw std::runtime_error{"Unit not supported"};
throw std::runtime_error{"eccodes::Unit not supported"};
}
}
catch (std::exception& e) {
std::string supported_units_str;
for (auto& u : supported_units)
supported_units_str += Unit{u}.value<std::string>() + ",";
supported_units_str += eccodes::Unit{u}.value<std::string>() + ",";
supported_units_str.pop_back();
std::string msg = std::string{"Invalid unit: "} + std::to_string(*val) + " (" + e.what() + ")" + ". Available units are: " + supported_units_str;
@ -174,7 +174,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
static int unpack_long(grib_accessor* a, long* val, size_t* len)
{
try {
if (Unit{staticStepUnits} != Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{staticStepUnits} != eccodes::Unit{eccodes::Unit::Value::MISSING}) {
*val = staticStepUnits;
return GRIB_SUCCESS;
}
@ -196,7 +196,7 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
*val = time_range_opt.value().optimize_unit().unit().value<long>();
}
else if (!forecast_time_opt && !time_range_opt) {
*val = Unit{Unit::Value::HOUR}.value<long>();
*val = eccodes::Unit{eccodes::Unit::Value::HOUR}.value<long>();
}
}
catch (std::exception& e) {
@ -210,14 +210,14 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
static int pack_string(grib_accessor* a, const char* val, size_t* len)
{
try {
long unit = Unit{val}.value<long>();
long unit = eccodes::Unit{val}.value<long>();
pack_long(a, &unit, len);
}
catch (std::exception& e) {
auto supported_units = Unit::list_supported_units();
auto supported_units = eccodes::Unit::list_supported_units();
std::string supported_units_str;
for (auto& u : supported_units)
supported_units_str += Unit{u}.value<std::string>() + ",";
supported_units_str += eccodes::Unit{u}.value<std::string>() + ",";
supported_units_str.pop_back();
std::string msg = "Invalid unit: " + std::string(val) + " (" + e.what() + ")" + ". Available units are: " + supported_units_str;
@ -235,7 +235,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
size_t unit_len = 0;
if ((ret = unpack_long(a, &unit, &unit_len)) != GRIB_SUCCESS)
return ret;
*len = snprintf(val, *len, "%s", Unit{unit}.value<std::string>().c_str());
*len = snprintf(val, *len, "%s", eccodes::Unit{unit}.value<std::string>().c_str());
return GRIB_SUCCESS;
}

View File

@ -152,13 +152,13 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
return err;
try {
Step step{forecast_time_value, forecast_time_unit};
eccodes::Step step{forecast_time_value, forecast_time_unit};
step.optimize_unit();
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.value<long>())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, "startStepUnit", eccodes::Unit{step_units}.value<long>())) != GRIB_SUCCESS)
return err;
*val = step.value<long>(Unit{step_units});
*val = step.value<long>(eccodes::Unit{step_units});
}
catch (std::exception& e) {
grib_context_log(h->context, GRIB_LOG_ERROR, "step_in_units: %s", e.what());
@ -183,12 +183,12 @@ static int unpack_double(grib_accessor* a, double * val, size_t* len)
return err;
try {
Step step{forecast_time_value, forecast_time_unit};
eccodes::Step step{forecast_time_value, forecast_time_unit};
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.value<long>())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, "startStepUnit", eccodes::Unit{step_units}.value<long>())) != GRIB_SUCCESS)
return err;
*val = step.value<double>(Unit{step_units});
*val = step.value<double>(eccodes::Unit{step_units});
}
catch (std::exception& e) {
grib_context_log(h->context, GRIB_LOG_ERROR, "step_in_units: %s", e.what());
@ -215,9 +215,9 @@ static int pack_long_new_(grib_accessor* a, const long start_step_value, const l
if ((err = grib_get_long_internal(h, "startStepUnit", &start_step_unit_old)) != GRIB_SUCCESS)
return err;
Step start_step_old(start_step_value_old, start_step_unit_old);
Step forecast_time(start_step_value, start_step_unit);
Step time_range_new{};
eccodes::Step start_step_old(start_step_value_old, start_step_unit_old);
eccodes::Step forecast_time(start_step_value, start_step_unit);
eccodes::Step time_range_new{};
auto time_range_opt = get_step(h, self->time_range_value, self->time_range_unit);
@ -225,7 +225,7 @@ static int pack_long_new_(grib_accessor* a, const long start_step_value, const l
auto time_range = time_range_opt.value();
time_range = time_range - (forecast_time - start_step_old);
if (time_range.value<long>() < 0)
time_range = Step{0l, time_range.unit()};
time_range = eccodes::Step{0l, time_range.unit()};
auto [sa, sb] = find_common_units(forecast_time.optimize_unit(), time_range.optimize_unit());
if ((err = set_step(h, self->forecast_time_value, self->forecast_time_unit, sa)) != GRIB_SUCCESS)
return err;
@ -236,7 +236,7 @@ static int pack_long_new_(grib_accessor* a, const long start_step_value, const l
return GRIB_SUCCESS;
}
if (Unit{force_step_units} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{force_step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
forecast_time.optimize_unit();
}
@ -259,12 +259,12 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
long start_step_unit;
try {
if (Unit{force_step_units} == Unit{Unit::Value::MISSING}) {
if (eccodes::Unit{force_step_units} == eccodes::Unit{eccodes::Unit::Value::MISSING}) {
if ((ret = grib_get_long_internal(h, "startStepUnit", &start_step_unit)) != GRIB_SUCCESS)
return ret;
if (Unit{start_step_unit} == Unit{Unit::Value::MISSING})
start_step_unit = Unit{Unit::Value::HOUR}.value<long>();
if (eccodes::Unit{start_step_unit} == eccodes::Unit{eccodes::Unit::Value::MISSING})
start_step_unit = eccodes::Unit{eccodes::Unit::Value::HOUR}.value<long>();
}
else {
start_step_unit = force_step_units;
@ -283,14 +283,14 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
static int pack_string(grib_accessor* a, const char* val, size_t* len)
{
grib_handle* h = grib_handle_of_accessor(a);
//long force_step_units = Unit(Unit::Value::MISSING).value<long>();
//long force_step_units = eccodes::Unit(eccodes::Unit::Value::MISSING).value<long>();
int ret = GRIB_SUCCESS;
long force_step_units;
if ((ret = grib_get_long_internal(h, "forceStepUnits", &force_step_units)) != GRIB_SUCCESS)
return ret;
try {
Step step = step_from_string(val, Unit{force_step_units});
eccodes::Step step = step_from_string(val, eccodes::Unit{force_step_units});
if ((ret = pack_long_new_(a, step.value<long>(), step.unit().value<long>(), force_step_units)) != GRIB_SUCCESS)
return ret;
}
@ -324,7 +324,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
return ret;
try {
Step step{start_step_value, start_step_unit};
eccodes::Step step{start_step_value, start_step_unit};
std::stringstream ss;
ss << step.value<std::string>(fp_format, show_hours);

View File

@ -20,6 +20,8 @@
#include "step_unit.h"
#include "step.h"
namespace eccodes {
Step step_from_string(const std::string& step, const Unit& force_unit)
{
std::regex re("([0-9.]+)([smhDMYC]?)");
@ -214,3 +216,5 @@ std::string Step::value<std::string>(const std::string& format, bool show_hours)
}
return output;
}
} // namespace eccodes

View File

@ -24,6 +24,7 @@
#include "step_unit.h"
namespace eccodes {
class Step {
public:
// Constructors
@ -122,3 +123,5 @@ template <typename T> T Step::value(const Unit& unit) const {
T value = from_seconds<T>(seconds, unit);
return value;
}
} // namespace eccodes

View File

@ -10,6 +10,8 @@
#include "step_unit.h"
namespace eccodes {
Unit::Map Unit::map_{};
std::vector<Unit::Value> Unit::grib_selected_units = {
@ -47,3 +49,5 @@ template <> Unit::Value Unit::value<Unit::Value>() const {
template <> std::string Unit::value<std::string>() const {
return map_.unit_to_name(internal_value_);
}
} // namespace eccodes

View File

@ -18,6 +18,8 @@
#include <unordered_map>
#include <algorithm>
namespace eccodes {
template <typename T> using Minutes = std::chrono::duration<T, std::ratio<60>>;
template <typename T> using Hours = std::chrono::duration<T, std::ratio<3600>>;
template <typename T> using Days = std::chrono::duration<T, std::ratio<86400>>;
@ -232,3 +234,5 @@ T from_seconds(Seconds<T> seconds, const Unit& unit) {
}
return value;
}
} // namespace eccodes

View File

@ -11,7 +11,7 @@
#include "step_utilities.h"
#include <type_traits>
std::optional<Step> get_step(grib_handle* h, const char* value_key, const char* unit_key)
std::optional<eccodes::Step> get_step(grib_handle* h, const char* value_key, const char* unit_key)
{
if (value_key && unit_key && grib_is_defined(h, unit_key) && grib_is_defined(h, value_key)) {
long unit = 0;
@ -22,14 +22,14 @@ std::optional<Step> get_step(grib_handle* h, const char* value_key, const char*
if (grib_get_long_internal(h, value_key, &value) != GRIB_SUCCESS)
return {};
return Step(value, unit);
return eccodes::Step(value, unit);
}
else {
return {};
}
}
int set_step(grib_handle* h, const std::string& value_key, const std::string& unit_key, const Step& step)
int set_step(grib_handle* h, const std::string& value_key, const std::string& unit_key, const eccodes::Step& step)
{
int err;
if ((err = grib_set_long_internal(h, value_key.c_str(), step.value<long>())) != GRIB_SUCCESS)

View File

@ -14,5 +14,5 @@
#include "step.h"
#include <optional>
std::optional<Step> get_step(grib_handle* h, const char* value_key, const char* unit_key);
int set_step(grib_handle* h, const std::string& value_key, const std::string& unit_key, const Step& step);
std::optional<eccodes::Step> get_step(grib_handle* h, const char* value_key, const char* unit_key);
int set_step(grib_handle* h, const std::string& value_key, const std::string& unit_key, const eccodes::Step& step);