Skip to content

Commit

Permalink
dates and time
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopendlebury committed Mar 18, 2024
1 parent 8eb6ea7 commit a4e1381
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/gribmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@ using namespace std;

//stringstream ss(date);
strptime(date.c_str(), "%Y%m%d", &tm);
//ss >> get_time(&tm, "%Y%m%d");

return std::chrono::system_clock::from_time_t(std::mktime(&tm));
auto forecastTime = getTimeNumeric() / 100;
auto chronoDate = std::chrono::system_clock::from_time_t(std::mktime(&tm));
chronoDate += std::chrono::hours(forecastTime);
return chronoDate;

}

chrono::system_clock::time_point GribMessage::getObsDate() {
auto dt = getChronoDate();

//TODO - this is flakey but follows current logic
//amend to use the stepUnit and not assume all steps are unit "h"
auto hours = getStep();
Expand Down
Binary file added tests/ecmwfaifs0h.grib
Binary file not shown.
Binary file added tests/ecmwfaifs6h.grib
Binary file not shown.
48 changes: 48 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import polars as pl

class TestLocations:

def __getLocations(self):
# Locations are Canary Wharf and Manchester
return pl.DataFrame(
{"lat": [51.5054, 53.4808], "lon": [-0.027176, 2.2426]}
).to_arrow()

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

locations = self.__getLocations()

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

df = pl.concat(
pl.from_arrow(message.getDataWithLocations()) for message in reader
)

date_hours = df.select(pl.col("datetime").dt.hour().unique())['datetime'].to_list()
forecast_hours = df.select(pl.col("forecast_date").dt.hour().unique())['forecast_date'].to_list()

assert all(x == 3 for x in date_hours)
assert all(x == 0 for x in forecast_hours)

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

locations = self.__getLocations()

reader = GribReader(
str(resource) + "/ecmwfaifs0h.grib"
).withLocations(locations)

df = pl.concat(
pl.from_arrow(message.getDataWithLocations()) for message in reader
)

date_hours = df.select(pl.col("datetime").dt.hour().unique())['datetime'].to_list()
forecast_hours = df.select(pl.col("forecast_date").dt.hour().unique())['forecast_date'].to_list()

assert all(x == 0 for x in date_hours)
assert all(x == 18 for x in forecast_hours)

0 comments on commit a4e1381

Please sign in to comment.