Skip to content

Commit

Permalink
eckit::geo HEALPix iterator fix, codes_get("ordering", string) return…
Browse files Browse the repository at this point in the history
…s "0"/"1" (?)
  • Loading branch information
pmaciel committed Jan 10, 2025
1 parent f52b7c8 commit eef24e1
Showing 1 changed file with 56 additions and 17 deletions.
73 changes: 56 additions & 17 deletions src/geo/GribSpec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,24 @@ const char* get_key(const std::string& name, codes_handle* h)
}


std::string get_string(codes_handle* h, const char* key)
{
if (codes_is_defined(h, key) != 0) {
char buffer[64];
size_t size = sizeof(buffer);

CHECK_CALL(codes_get_string(h, key, buffer, &size));
ASSERT(size < sizeof(buffer) - 1);

if (std::strcmp(buffer, "MISSING") != 0) {
return buffer;
}
}

return "";
};


template <typename T>
struct ProcessingT
{
Expand Down Expand Up @@ -581,22 +599,8 @@ ProcessingT<std::vector<double>>* vector_double(std::initializer_list<std::strin
ProcessingT<std::string>* packing()
{
return new ProcessingT<std::string>([](codes_handle* h, std::string& value) {
auto get = [](codes_handle* h, const char* key) -> std::string {
if (codes_is_defined(h, key) != 0) {
char buffer[64];
size_t size = sizeof(buffer);

CHECK_CALL(codes_get_string(h, key, buffer, &size));
ASSERT(size < sizeof(buffer) - 1);
const auto packingType = get_string(h, "packingType");

if (std::strcmp(buffer, "MISSING") != 0) {
return buffer;
}
}
return "";
};

auto packingType = get(h, "packingType");
for (const auto& prefix : std::vector<std::string>{ "grid_", "spectral_" }) {
if (packingType.find(prefix) == 0) {
value = packingType.substr(prefix.size());
Expand All @@ -610,6 +614,26 @@ ProcessingT<std::string>* packing()
}


ProcessingT<std::string>* healpix_ordering()
{
return new ProcessingT<std::string>([](codes_handle* h, std::string& value) {
const auto ordering = get_string(h, "ordering");

if (ordering == "0") {
value = "ring";
return true;
}

if (ordering == "1") {
value = "nested";
return true;
}

return false;
});
}


template <class T>
struct ConditionedProcessingT
{
Expand Down Expand Up @@ -693,6 +717,19 @@ bool GribSpec::get(const std::string& name, std::string& value) const
return false;
}

static const ProcessingList<std::string> keys_override{
{ "ordering", healpix_ordering(), is("gridType", "healpix") },
};

for (auto& p : keys_override) {
if (name == p.name) {
if (!p.condition || p.condition->eval(handle_)) {
ASSERT(p.processing);
return p.processing->eval(handle_, value);
}
}
}

char buffer[10240];
size_t size = sizeof(buffer);
int err = codes_get_string(handle_, key, buffer, &size);
Expand Down Expand Up @@ -974,7 +1011,7 @@ bool GribSpec::get(const std::string& name, std::vector<double>& value) const
}

size_t count = 0;
int err = codes_get_size(handle_, key, &count);
int err = codes_get_size(handle_, key, &count);
CHECK_ERROR(err, key);

ASSERT(count > 0);
Expand Down Expand Up @@ -1076,7 +1113,9 @@ void GribSpec::json(eckit::JSON& j) const
}

if (type == CODES_TYPE_STRING) {
char value[1024] = {0,};
char value[1024] = {
0,
};
size_t length = sizeof(value);
CHECK_CALL(codes_get_string(handle_, name, value, &length));
j << name << value;
Expand Down

0 comments on commit eef24e1

Please sign in to comment.