Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Sep 24, 2023
1 parent 868c1e6 commit 932b951
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 17 additions & 6 deletions ecml_tools/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import calendar
import datetime
import json
import logging
import os
import re
Expand All @@ -23,6 +24,8 @@


class Dataset:
metadata = {}

@cached_property
def _len(self):
return len(self)
Expand All @@ -37,8 +40,8 @@ def _subset(self, **kwargs):
return Subset(self, self._frequency_to_indices(frequency))._subset(**kwargs)

if "start" in kwargs or "end" in kwargs:
start = kwargs.pop("start")
end = kwargs.pop("end")
start = kwargs.pop("start", None)
end = kwargs.pop("end", None)

return Subset(self, self._dates_to_indices(start, end))._subset(**kwargs)

Expand All @@ -53,9 +56,11 @@ def _subset(self, **kwargs):
if "reorder" in kwargs:
reorder = kwargs.pop("reorder")
return Select(self, self._reorder_to_columns(reorder))._subset(**kwargs)

if "rename" in kwargs:
rename = kwargs.pop("rename")
return Rename(self, rename)._subset(**kwargs)

raise NotImplementedError("Unsupported arguments: " + ", ".join(kwargs))

def _frequency_to_indices(self, frequency):
Expand Down Expand Up @@ -110,6 +115,9 @@ def _reorder_to_columns(self, vars):
def save(self, path, chunks=None, buffer_size=10):
z = zarr.convenience.open(path, "w")

print(json.dumps(self.metadata, indent=4))
print(json.dumps(self.zarrs(), indent=4))

if chunks is None:
chunks = (1,) + self.shape[1:]

Expand Down Expand Up @@ -137,13 +145,13 @@ def save(self, path, chunks=None, buffer_size=10):
z.data[i:j] = self[i:j]

def zarrs(self):
return []
return {}


class Zarr(Dataset):
def __init__(self, path):
if isinstance(path, zarr.hierarchy.Group):
self.path = "-"
self.path = str(id(path))
self.z = path
else:
self.path = path
Expand Down Expand Up @@ -222,7 +230,7 @@ def variables(self):
]

def zarrs(self):
return [self.z]
return {self.path: dict(**self.z.attrs)}

def __repr__(self):
return self.path
Expand Down Expand Up @@ -310,7 +318,10 @@ def check_compatibility(self, d1, d2):
raise ValueError(f"Incompatible grid ({d1} {d2})")

def zarrs(self):
return sum([d.zarrs() for d in self.datasets], [])
result = {}
for d in self.datasets:
result.update(d.zarrs())
return result

def __repr__(self):
lst = ", ".join(repr(d) for d in self.datasets)
Expand Down
4 changes: 3 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
z = open_dataset(
"aifs-ea-an-oper-0001-mars-o96-2021-6h-v2-only-z",
"aifs-ea-an-oper-0001-mars-o96-2021-6h-v2-without-z",
end="2021-01-01",
)
print(len(z))

print(z)
# print(z)

z.save("new-zarr.zarr")
exit(0)
Expand Down

0 comments on commit 932b951

Please sign in to comment.