Skip to content

Commit cedd9db

Browse files
authored
feat: copy datasets from http(s) sources (#307)
1 parent 4e57f7c commit cedd9db

File tree

1 file changed

+34
-1
lines changed
  • src/anemoi/datasets/commands

1 file changed

+34
-1
lines changed

src/anemoi/datasets/commands/copy.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,30 @@ def copy_group(self, source: Any, target: Any, _copy: Any, verbosity: int) -> No
319319
"""
320320
import zarr
321321

322+
if self.verbosity > 0:
323+
LOG.info(f"Copying group {source} to {target}")
324+
322325
for k, v in source.attrs.items():
326+
if self.verbosity > 1:
327+
import textwrap
328+
329+
LOG.info(f"Copying attribute {k} = {textwrap.shorten(str(v), 40)}")
323330
target.attrs[k] = v
324331

325-
for name in sorted(source.keys()):
332+
source_keys = list(source.keys())
333+
334+
if not source_keys:
335+
raise ValueError(f"Source group {source} is empty.")
336+
337+
if self.verbosity > 1:
338+
LOG.info(f"Keys {source_keys}")
339+
340+
for name in sorted(source_keys):
341+
if name.startswith("."):
342+
if self.verbosity > 1:
343+
LOG.info(f"Skipping {name}")
344+
continue
345+
326346
if isinstance(source[name], zarr.hierarchy.Group):
327347
group = target[name] if name in target else target.create_group(name)
328348
self.copy_group(
@@ -362,6 +382,11 @@ def copy(self, source: Any, target: Any, verbosity: int) -> None:
362382
_copy = target["_copy"]
363383
_copy_np = _copy[:]
364384

385+
if self.verbosity > 1:
386+
import numpy as np
387+
388+
LOG.info(f"copy {np.sum(_copy_np)} of {len(_copy_np)}")
389+
365390
self.copy_group(source, target, _copy_np, verbosity)
366391
del target["_copy"]
367392

@@ -417,11 +442,19 @@ def open_target() -> Any:
417442
LOG.error("Target already exists, use either --overwrite or --resume.")
418443
sys.exit(1)
419444

445+
if self.verbosity > 0:
446+
LOG.info(f"Open target: {self.target}")
447+
420448
target = open_target()
421449

422450
assert target is not None, target
423451

452+
if self.verbosity > 0:
453+
LOG.info(f"Open source: {self.source}")
454+
424455
source = zarr.open(self._store(self.source), mode="r")
456+
# zarr.consolidate_metadata(source)
457+
425458
self.copy(source, target, self.verbosity)
426459

427460

0 commit comments

Comments
 (0)