Skip to content

Commit

Permalink
eckit::geo::grid::HEALPix match eccodes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Jan 30, 2025
1 parent fcd9498 commit e42fdf8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/eckit/geo/grid/HEALPix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -380,7 +389,7 @@ const std::string& HEALPix::type() const {

static const GridRegisterType<HEALPix> GRIDTYPE1("HEALPix");
static const GridRegisterType<HEALPix> GRIDTYPE2("healpix");
static const GridRegisterName<HEALPix> GRIDNAME("[hH][1-9][0-9]*");
static const GridRegisterName<HEALPix> GRIDNAME("[hH][0-9]+");


} // namespace eckit::geo::grid
8 changes: 7 additions & 1 deletion tests/geo/grid_healpix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ CASE("sizes") {


CASE("points") {

std::unique_ptr<const Grid> ring(new grid::HEALPix(2));

EXPECT(ring->ordering() == Ordering::healpix_ring);
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit e42fdf8

Please sign in to comment.