Skip to content

Commit

Permalink
Add build index cli
Browse files Browse the repository at this point in the history
  • Loading branch information
benchimols committed Dec 1, 2023
1 parent a1490da commit 9eac9bd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cfgrib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
import os.path
import typing as T
from pathlib import Path

import click

Expand Down Expand Up @@ -176,5 +177,35 @@ def dump(inpaths, variable, cdm, engine):
print(ds_or_da)


@cfgrib_cli.command("build_index")
@click.argument("inpaths", nargs=-1, required=True)
@click.option("--index-basedir", default=None)
@click.option("--force", default=None)
def build_index(inpaths, index_basedir, force):
# type: (T.List[str], str, bool) -> None
from .messages import FileStream, FileIndex
from .dataset import compute_index_keys

index_keys = compute_index_keys(("time", "step", "shortName"), {})
indexpath = "{path}.idx"
if index_basedir:
indexpath = os.path.join(index_basedir, '{path}.idx')

for fp in inpaths:
fp_idx = Path(indexpath.format(path=fp))
if force:
fp_idx.unlink(missing_ok=True)

print(f"{fp}: Creating index to {fp_idx}")
stream = FileStream(str(fp))
index = FileIndex.from_indexpath_or_filestream(
filestream=stream,
index_keys=index_keys,
indexpath=indexpath
)




if __name__ == "__main__": # pragma: no cover
cfgrib_cli()

0 comments on commit 9eac9bd

Please sign in to comment.