Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial code for local index definition of grib files. #512

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions kerchunk/grib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from collections import defaultdict
from typing import Iterable, List, Dict, Set
import ujson
import datetime as dt
import pytz

import fsspec
import zarr
Expand Down Expand Up @@ -112,6 +114,7 @@ def scan_grib(
inline_threshold=100,
skip=0,
filter={},
idx=False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass a filename or maybe open file?
Dumping a load of files into the local directory doesn't seem like a great idea.

) -> List[Dict]:
"""
Generate references for a GRIB2 file
Expand Down Expand Up @@ -149,6 +152,10 @@ def scan_grib(
# These are present by default
# TIME_DIMS = ["step", "time", "valid_time"]

count = 1
if idx:
idx_f = open(f"{url.split('/')[-1]}.idx", "a")

out = []
with fsspec.open(url, "rb", **storage_options) as f:
logger.debug(f"File {url}")
Expand Down Expand Up @@ -267,6 +274,18 @@ def scan_grib(
)
continue

if idx and coord in ["time", "valid_time"]:
line = ""
if coord == "time":
time = dt.datetime.fromtimestamp(x, tz=pytz.UTC)
line = f"{count}:{offset}:d={time.strftime('%Y%m%d%H')}:{varName.upper()}"
count += 1
elif coord == "valid_time":
valid_time = dt.datetime.fromtimestamp(x, tz=pytz.UTC)
diff = int((valid_time - time).total_seconds() / 3600)
line += f":{diff} hour fcst\n"
idx_f.write(line)

if x is None:
continue
coordinates.append(coord)
Expand Down Expand Up @@ -318,6 +337,8 @@ def scan_grib(
"templates": {"u": url},
}
)
if idx:
idx_f.close()
logger.debug("Done")
return out

Expand Down
Loading