From e42fdf83cecd3979c8e3021cffc02deed90f23ba Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Thu, 30 Jan 2025 00:23:04 +0000 Subject: [PATCH] eckit::geo::grid::HEALPix match eccodes tests --- src/eckit/geo/grid/HEALPix.cc | 27 ++++++++++++++++++--------- tests/geo/grid_healpix.cc | 8 +++++++- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/eckit/geo/grid/HEALPix.cc b/src/eckit/geo/grid/HEALPix.cc index 6ceb585b6..1960edc31 100644 --- a/src/eckit/geo/grid/HEALPix.cc +++ b/src/eckit/geo/grid/HEALPix.cc @@ -217,28 +217,37 @@ class Reorder { } // unnamed namespace +static const std::string HEALPIX_ERROR_NSIDE_POSITIVE = "HEALPix: Nside must be greater than zero"; +static const std::string HEALPIX_ERROR_NSIDE_POWER2 = "HEALPix: Nside must be a power of 2"; +static const std::string HEALPIX_ERROR_ORDERING + = "HEALPix: supported ordering: ring, nested (Only orderingConvention are supported)"; + + HEALPix::HEALPix(const Spec& spec) : HEALPix(util::convert_long_to_size_t(spec.get_long("Nside")), [](const std::string& str) { return str == "ring" ? Ordering::healpix_ring : str == "nested" ? Ordering::healpix_nested - : throw AssertionFailed("HEALPix: supported orderings: ring, nested", Here()); + : throw exception::SpecError(HEALPIX_ERROR_ORDERING, Here()); }(spec.get_string("ordering", "ring"))) {} HEALPix::HEALPix(size_t Nside, Ordering ordering) : Reduced(area::BoundingBox{}), Nside_(Nside), ordering_(ordering) { - ASSERT(Nside_ > 0); - ASSERT_MSG(ordering == Ordering::healpix_ring || ordering == Ordering::healpix_nested, - "HEALPix: supported orderings: ring, nested"); + if (Nside_ == 0) { + throw exception::SpecError(HEALPIX_ERROR_NSIDE_POSITIVE, Here()); + } + + if (ordering != Ordering::healpix_ring && ordering != Ordering::healpix_nested) { + throw exception::SpecError(HEALPIX_ERROR_ORDERING, Here()); + } - if (ordering_ == Ordering::healpix_nested) { - ASSERT(is_power_of_2(Nside)); + if (ordering_ == Ordering::healpix_nested && !is_power_of_2(Nside_)) { + throw exception::SpecError(HEALPIX_ERROR_NSIDE_POWER2, Here()); } } Renumber HEALPix::reorder(Ordering ordering) const { - ASSERT_MSG(ordering == Ordering::healpix_ring || ordering == Ordering::healpix_nested, - "HEALPix: supported orderings: ring, nested"); + ASSERT_MSG(ordering == Ordering::healpix_ring || ordering == Ordering::healpix_nested, HEALPIX_ERROR_ORDERING); if (ordering == ordering_) { return Grid::no_reorder(size()); @@ -380,7 +389,7 @@ const std::string& HEALPix::type() const { static const GridRegisterType GRIDTYPE1("HEALPix"); static const GridRegisterType GRIDTYPE2("healpix"); -static const GridRegisterName GRIDNAME("[hH][1-9][0-9]*"); +static const GridRegisterName GRIDNAME("[hH][0-9]+"); } // namespace eckit::geo::grid diff --git a/tests/geo/grid_healpix.cc b/tests/geo/grid_healpix.cc index 5e13d52a7..e729fcb7e 100644 --- a/tests/geo/grid_healpix.cc +++ b/tests/geo/grid_healpix.cc @@ -57,7 +57,6 @@ CASE("sizes") { CASE("points") { - std::unique_ptr ring(new grid::HEALPix(2)); EXPECT(ring->ordering() == Ordering::healpix_ring); @@ -191,6 +190,13 @@ CASE("equals") { } +CASE("wrong spec") { + EXPECT_THROWS_AS(auto* ignore = GridFactory::make_from_string("{grid:h0}"), exception::SpecError); + EXPECT_THROWS_AS(auto* ignore = GridFactory::make_from_string("{grid:h3, ordering:nested}"), exception::SpecError); + EXPECT_THROWS_AS(auto* ignore = GridFactory::make_from_string("{grid:h3, ordering:?}"), exception::SpecError); +} + + } // namespace eckit::geo::test