Skip to content

Commit

Permalink
add tryGetKey show usage of defaults / boolean if with walrus
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopendlebury committed Feb 25, 2024
1 parent 94fb881 commit d8e44b0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pythonApi/grib_to_arrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ PYBIND11_MODULE(gribtoarrow, m)
,pybind11::call_guard<pybind11::gil_scoped_release>(), R"EOL(
Get the key passed to it or if the key is missing returns the user supplied default value
)EOL")
.def("tryGetKey", &GribMessage::tryGetKey
,pybind11::call_guard<pybind11::gil_scoped_release>(), R"EOL(
Get the key passed to it or if the key is missing returns None
)EOL")
.doc() = R"EOL(
This class provides the ability to access attributes such as the parameterId
Expand Down
49 changes: 37 additions & 12 deletions src/gribmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ using namespace std;
double GribMessage::getDoubleParameterOrDefault(string parameterName, double defaultValue) {
double parameterId;
auto ret = codes_get_double(h, parameterName.c_str(), &parameterId);
std::cout << "ret is " << ret << std::endl;
return ret == 0 ? parameterId : defaultValue;
}

Expand All @@ -324,31 +325,55 @@ using namespace std;

}

template <typename T>
T GribMessage::tryGetKey(string parameterName) {
std::variant<long, std::string, double, nullptr_t> GribMessage::tryGetKey(string parameterName) {

auto dblDefault = std::nan("");
long maxLong {std::numeric_limits<long>::max()};
auto stringDefault = "InTheBlueRidgeMountainsOfViriginaOnTheTrailOfTheLonesomePine";

auto doubleResult = getDoubleParameterOrDefault(parameterName, dblDefault);
if (doubleResult != dblDefault) {
//we got the result
return doubleResult;
}
//it wasn't a double or the key doesn't exist
std::cout << "Getting key " << parameterName << std::endl;


auto longResult = getNumericParameterOrDefault(parameterName, maxLong);
if (longResult != maxLong) {
//we got the result
return longResult;
//we got a result but it's possibly a string
if (longResult == 0) {
auto stringResult = getStringParameterOrDefault(parameterName, stringDefault);
std::cout << "Value of string is " << stringResult << std::endl;
if(stringResult == stringDefault) {
std::cout << "returning long" << longResult << std::endl;
return {longResult};
}
return {stringResult};
}
return {longResult};
}

//it wasn't a double or the key doesn't exist
auto doubleResult = getDoubleParameterOrDefault(parameterName, dblDefault);
if (!std::isnan(doubleResult)) {
std::cout << "Value of double is " << doubleResult << std::endl;
//its not a double but it might be a string key
auto stringResult = getStringParameterOrDefault(parameterName, stringDefault);
std::cout << "Value of string is " << stringResult << std::endl;
if(stringResult == stringDefault) {
std::cout << "returning double" << doubleResult << std::endl;
return {doubleResult};
}
std::cout << "returning string" << stringResult << std::endl;
return {stringResult};
}


auto stringResult = getStringParameterOrDefault(parameterName, stringDefault);
if (stringResult != stringDefault) {
return stringResult;
std::cout << "its a string " << std::endl;
return {stringResult};
}

return nullptr;

std::cout << "No result found for key " << parameterName << std::endl;
return {nullptr};

}

Expand Down
5 changes: 3 additions & 2 deletions src/gribmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ class GribMessage
template <typename T>
T tryGetParameter(string parameterName, T defaultValue = nullptr);

template <typename T>
T tryGetKey(string parameterName);
//template <typename T>
//T tryGetKey(string parameterName);
std::variant<long, std::string, double, nullptr_t> tryGetKey(string parameterName);

std::shared_ptr<arrow::Table> getData();
std::shared_ptr<arrow::Table> getDataWithLocations();
Expand Down
40 changes: 40 additions & 0 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import math
import polars as pl

class TestFilterMessageId:
def test_attributes(self, resource):
Expand Down Expand Up @@ -58,3 +59,42 @@ def test_default_missingKeys(self, resource):
#String key does exist should return user specified key
assert "shorty" == message.getStringParameterOrDefault("elChapo", "shorty")

def test_tryGetKey(self, resource):
from gribtoarrow import GribReader

# Locations are Canary Wharf,Manchester & kristiansand
locations = pl.DataFrame(
{"lat": [51.5054, 53.4808, 58.1599], "lon": [-0.027176, 2.2426, 8.0182]}
).to_arrow()

reader = GribReader(str(resource) + "/gep01.t00z.pgrb2a.0p50.f003").withLocations(locations)

message = next(iter(reader))
#key exists so should get value
assert 156 == message.tryGetKey("paramId")
#key doesn't exist (deliberate typo and no default specified)
assert None == message.tryGetKey("paramid")
#key exists should return value
assert 90.0 == message.tryGetKey("latitudeOfFirstGridPointInDegrees")
#key doesn't exist should return value (NaN)
assert None == message.tryGetKey("latitudeOfFirstGridPointInDegreesPleaseBoss")
assert "gh" == message.tryGetKey("shortName")
#String key does exist should return None
assert None == message.tryGetKey("elChapo")

#The next example it to show how we can use tryGetKey (in c# you can return a bool and use ref)
#from python 3.8 we can do this with walrus
messages = []
for message in reader:
msg = pl.from_arrow(message.getDataWithLocations()).with_columns([
#We know this columns doesn't exist but want a different default
pl.lit(key).alias("Shorty") if(key := message.tryGetKey("elChapo")) else pl.lit("sinaloa").alias("Shorty")
])
messages.append(msg)

df : pl.DataFrame = pl.concat(messages)
assert all(x == "sinaloa" for x in df["Shorty"].to_list())




0 comments on commit d8e44b0

Please sign in to comment.