Skip to content

Commit

Permalink
make date timestamp axis work
Browse files Browse the repository at this point in the history
  • Loading branch information
mathleur committed Nov 23, 2023
1 parent 4231257 commit 9742d31
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
polytope.egg-info
.pytest_cache
*.prof
*.idx
*.idx
*.grib
4 changes: 1 addition & 3 deletions polytope/datacube/backends/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def __init__(self, dataarray: xr.DataArray, axis_options={}):
self.complete_axes = []
self.blocked_axes = []
self.transformation = None
self.fake_axes = []
for name, values in dataarray.coords.variables.items():
if name in dataarray.dims:
options = axis_options.get(name, {})
Expand Down Expand Up @@ -48,14 +47,13 @@ def __init__(self, dataarray: xr.DataArray, axis_options={}):
def get(self, requests: IndexTree):
for r in requests.leaves:
path = r.flatten()
path = self.remap_path(path)
if len(path.items()) == self.axis_counter:
# first, find the grid mapper transform
unmapped_path = {}
path_copy = deepcopy(path)
for key in path_copy:
axis = self._axes[key]
(path, unmapped_path) = axis.unmap_total_path_to_datacube(path, unmapped_path)
(path, unmapped_path) = axis.unmap_to_datacube(path, unmapped_path)
path = self.fit_path(path)
subxarray = self.dataarray.sel(path, method="nearest")
subxarray = subxarray.sel(unmapped_path)
Expand Down
4 changes: 0 additions & 4 deletions polytope/datacube/datacube_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,6 @@ def find_indexes(path, datacube):
ordered_indices = unordered_indices
return ordered_indices

def unmap_to_datacube(path, unmapped_path):
return (path, unmapped_path)

def remap_to_requested(path, unmapped_path):
return (path, unmapped_path)

Expand Down Expand Up @@ -445,7 +442,6 @@ def remap(range):

cls.remap = remap
cls.find_indexes = find_indexes
cls.unmap_to_datacube = unmap_to_datacube
cls.remap_to_requested = remap_to_requested
cls.find_indices_between = find_indices_between

Expand Down
11 changes: 10 additions & 1 deletion polytope/datacube/transformations/datacube_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def __init__(self, name, merge_options):
self._second_axis = merge_options["with"]
self._linkers = merge_options["linkers"]

def _mapped_axes(self):
return self._first_axis

def unwanted_axes(self):
return []

def blocked_axes(self):
return [self._second_axis]

Expand Down Expand Up @@ -44,7 +50,10 @@ def unmerge(self, merged_val):
first_linker_size = len(self._linkers[0])
second_linked_size = len(self._linkers[1])
second_val = merged_val[first_idx + first_linker_size : -second_linked_size]
first_val = str(first_val).replace("-", "")
second_val = second_val.replace(":", "")
return (first_val, second_val)

def change_val_type(self, axis_name, values):
return values
new_values = pd.to_datetime(values)
return new_values
9 changes: 4 additions & 5 deletions tests/test_merge_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ def setup_method(self, method):
np.random.randn(1, 1),
dims=("date", "time"),
coords={
"date": ["2000-01-01"],
"time": ["06:00"],
"date": ["20000101"],
"time": ["0600"],
},
)
self.options = {"date": {"transformation": {"merge": {"with": "time", "linkers": [" ", ":00"]}}}}
self.options = {"date": {"transformation": {"merge": {"with": "time", "linkers": ["T", "00"]}}}}
self.xarraydatacube = XArrayDatacube(self.array)
self.slicer = HullSlicer()
self.API = Polytope(datacube=self.array, engine=self.slicer, axis_options=self.options)

def test_merge_axis(self):
request = Request(Select("date", [pd.Timestamp("2000-01-01T06:00:00")]))
request = Request(Select("date", [pd.Timestamp("20000101T060000")]))
result = self.API.retrieve(request)
# assert result.leaves[0].flatten()["date"] == np.datetime64("2000-01-01T06:00:00")
assert result.leaves[0].flatten()["date"] == pd.Timestamp("2000-01-01T06:00:00")
44 changes: 44 additions & 0 deletions tests/test_slice_date_range_fdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pandas as pd
import pytest

from polytope.datacube.backends.FDB_datacube import FDBDatacube
from polytope.engine.hullslicer import HullSlicer
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select, Span


class TestSlicingFDBDatacube:
def setup_method(self, method):
# Create a dataarray with 3 labelled axes using different index types
self.options = {
"values": {
"transformation": {
"mapper": {"type": "octahedral", "resolution": 1280, "axes": ["latitude", "longitude"]}
}
},
"date": {"transformation": {"merge": {"with": "time", "linkers": ["T", "00"]}}},
"step": {"transformation": {"type_change": "int"}},
}
self.config = {"class": "od", "expver": "0001", "levtype": "sfc", "step": 0}
self.fdbdatacube = FDBDatacube(self.config, axis_options=self.options)
self.slicer = HullSlicer()
self.API = Polytope(datacube=self.fdbdatacube, engine=self.slicer, axis_options=self.options)

# Testing different shapes
@pytest.mark.skip(reason="can't install fdb branch on CI")
def test_fdb_datacube(self):
request = Request(
Select("step", [0]),
Select("levtype", ["sfc"]),
Span("date", pd.Timestamp("20230625T120000"), pd.Timestamp("20230626T120000")),
Select("domain", ["g"]),
Select("expver", ["0001"]),
Select("param", ["167"]),
Select("class", ["od"]),
Select("stream", ["oper"]),
Select("type", ["an"]),
Box(["latitude", "longitude"], [0, 0], [0.2, 0.2]),
)
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 9

0 comments on commit 9742d31

Please sign in to comment.