Skip to content

Commit 42f57c7

Browse files
committed
some refactoring
1 parent d282a86 commit 42f57c7

File tree

2 files changed

+25
-79
lines changed

2 files changed

+25
-79
lines changed

src/anemoi/datasets/commands/inspect/zarr.py renamed to src/anemoi/datasets/commands/inspect.py

+25-48
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from anemoi.datasets.data.stores import open_zarr
2727
from anemoi.datasets.data.stores import zarr_lookup
2828

29+
from . import Command
30+
2931
LOG = logging.getLogger(__name__)
3032

3133

@@ -79,46 +81,12 @@ def __init__(self, path, zarr, metadata, version):
7981
self.metadata = metadata
8082
self.version = version
8183
self.dataset = None
82-
# try:
8384
self.dataset = open_dataset(self.path)
84-
# except Exception as e:
85-
# LOG.error("Error opening dataset '%s': %s", self.path, e)
8685

8786
def describe(self):
8887
print(f"📦 Path : {self.path}")
8988
print(f"🔢 Format version: {self.version}")
9089

91-
def probe(self):
92-
if "cos_local_time" not in self.name_to_index:
93-
print("⚠️ probe: no cos_local_time")
94-
return
95-
96-
try:
97-
lon = self.longitudes
98-
except AttributeError:
99-
print("⚠️ probe: no longitudes")
100-
return
101-
# print(json.dumps(self.metadata, indent=4))
102-
cos_local_time = self.name_to_index["cos_local_time"]
103-
data = self.data
104-
start, end, frequency = self.first_date, self.last_date, self.frequency
105-
date = start
106-
same = 0
107-
for i in range(10):
108-
field = data[i, cos_local_time]
109-
buggy = cos_local_time_bug(lon, date).reshape(field.hape)
110-
diff = np.abs(field - buggy)
111-
if np.max(diff) < 1e-5:
112-
same += 1
113-
date += datetime.timedelta(hours=frequency)
114-
if date > end:
115-
break
116-
if same > 1:
117-
print("❌ probe: cos_local_time is buggy")
118-
return
119-
120-
print("✅ probe: cos_local_time is fixed")
121-
12290
@property
12391
def name_to_index(self):
12492
return find(self.metadata, "name_to_index")
@@ -587,29 +555,35 @@ def build_lengths(self):
587555
}
588556

589557

590-
class InspectZarr:
591-
"""Inspect a checkpoint or zarr file."""
558+
class InspectZarr(Command):
559+
"""Inspect a zarr dataset."""
592560

593-
def inspect_zarr(self, path, **kwargs):
594-
version = self._info(path)
561+
def add_arguments(self, command_parser):
562+
command_parser.add_argument("path", metavar="DATASET")
563+
command_parser.add_argument("--detailed", action="store_true")
564+
565+
command_parser.add_argument("--progress", action="store_true")
566+
command_parser.add_argument("--statistics", action="store_true")
567+
command_parser.add_argument("--size", action="store_true", help="Print size")
595568

596-
# try:
597-
# with open("/tmp/probe.json", "w") as f:
598-
# json.dump(version.metadata, f, indent=4, sort_keys=True)
599-
# except Exception:
600-
# pass
569+
def run(self, args):
570+
self.inspect_zarr(**vars(args))
571+
572+
def inspect_zarr(self, path, progress=False, statistics=False, detailed=False, size=False, **kwargs):
573+
version = self._info(path)
601574

602575
dotted_line()
603576
version.describe()
604577

605578
try:
606-
if kwargs.get("probe"):
607-
return version.probe()
608-
if kwargs.get("progress"):
579+
if progress:
609580
return version.progress()
610-
if kwargs.get("statistics"):
581+
582+
if statistics:
611583
return version.brute_force_statistics()
612-
version.info(kwargs.get("detailed"), kwargs.get("size"))
584+
585+
version.info(detailed, size)
586+
613587
except Exception as e:
614588
LOG.error("Error inspecting zarr file '%s': %s", path, e)
615589

@@ -634,3 +608,6 @@ def _info(self, path):
634608
candidate = klass
635609

636610
return candidate(path, z, metadata, version)
611+
612+
613+
command = InspectZarr

src/anemoi/datasets/commands/inspect/__init__.py

-31
This file was deleted.

0 commit comments

Comments
 (0)