Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4c6cb63
adding reader_options kwargs to open_virtual_dataset
norlandrhagen Mar 29, 2024
adf311a
Merge branch 'main' into reader_options
TomNicholas Apr 30, 2024
ba5ac6d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 30, 2024
ea30914
fix typing
TomNicholas Apr 30, 2024
448800b
modifies _automatically_determine_filetype to open file with fsspec t…
norlandrhagen May 1, 2024
8c5dff7
using UPath to get file protocol and open with fsspec
norlandrhagen May 1, 2024
6cd77ce
tests passing locally. Reading over s3/local w+w/o indexes & guessing…
norlandrhagen May 2, 2024
f0daafe
merge w/ main
norlandrhagen May 2, 2024
ed3d0f4
add s3fs to test
norlandrhagen May 2, 2024
beec724
typing school 101
norlandrhagen May 2, 2024
e669841
anon
norlandrhagen May 2, 2024
09f89a6
tying
norlandrhagen May 2, 2024
e4db860
test_anon update
norlandrhagen May 2, 2024
ba8b1e3
anon failing
norlandrhagen May 2, 2024
b12d32c
double down on storage_options
norlandrhagen May 2, 2024
f9478b9
fsspec nit
norlandrhagen May 3, 2024
6958b59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 3, 2024
aefa22d
seting s3 defaults as empty to try to appease the cruel boto3 gods
norlandrhagen May 3, 2024
464ffd3
merge
norlandrhagen May 3, 2024
d108978
added fpath to SingleHDF5ToZarr
norlandrhagen May 3, 2024
5cc5ecd
hardcode in empty storage opts for s3
norlandrhagen May 3, 2024
3509a1f
hardcode default + unpack test
norlandrhagen May 3, 2024
80cf22b
changed reader_options defaults
norlandrhagen May 3, 2024
a3fc72e
Merge branch 'main' into reader_options
norlandrhagen May 3, 2024
0235f51
updated docs install
norlandrhagen May 3, 2024
1e9e2fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 3, 2024
55031f9
changed docstring type in utils to numpy style
norlandrhagen May 6, 2024
6a3d7be
added TYPE_CHECKING for fsspec and s3fs mypy type hints
norlandrhagen May 8, 2024
5aec9db
merged w/ main and lint
norlandrhagen May 8, 2024
83b3c4b
fixed TYPE_CHECKING import
norlandrhagen May 8, 2024
a143cf4
pinned xarray to latest commit on github
norlandrhagen May 9, 2024
9d124ef
merged w/ main to pin xarray and kerchunk
norlandrhagen May 13, 2024
3a29b41
re-add upath
norlandrhagen May 13, 2024
b9c056a
Merge branch 'main' into reader_options
norlandrhagen May 13, 2024
13fc295
merged w/ main
norlandrhagen May 14, 2024
4f766d9
ådds section to usage
norlandrhagen May 14, 2024
e6f047f
Minor formatting nit of code example in docs
TomNicholas May 14, 2024
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
16 changes: 10 additions & 6 deletions virtualizarr/kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class FileType(AutoName):
zarr = auto()

def read_kerchunk_references_from_file(
filepath: str, filetype: Optional[FileType]
filepath: str, filetype: Optional[FileType],
reader_options: Optional[dict] = {'storage_options': {'anon': True}}
) -> KerchunkStoreRefs:
"""
Read a single legacy file and return kerchunk references to its contents.
Expand All @@ -47,6 +48,9 @@ def read_kerchunk_references_from_file(
filetype : FileType, default: None
Type of file to be opened. Used to determine which kerchunk file format backend to use.
If not provided will attempt to automatically infer the correct filetype from the the filepath's extension.
reader_options: dict, default {'storage_options': {'anon': True}}
Dict passed into Kerchunk file readers. Note: Each Kerchunk file reader has distinct arguments,
so ensure reader_options match selected Kerchunk reader arguments.
"""

if filetype is None:
Expand All @@ -57,24 +61,24 @@ def read_kerchunk_references_from_file(

if filetype.name.lower() == "netcdf3":
from kerchunk.netCDF3 import NetCDF3ToZarr
refs = NetCDF3ToZarr(filepath, inline_threshold=0).translate()

refs = NetCDF3ToZarr(filepath, inline_threshold=0, **reader_options).translate()
elif filetype.name.lower() == "netcdf4":
from kerchunk.hdf import SingleHdf5ToZarr

refs = SingleHdf5ToZarr(filepath, inline_threshold=0).translate()
refs = SingleHdf5ToZarr(filepath, inline_threshold=0, **reader_options).translate()
elif filetype.name.lower() == "grib":
# TODO Grib files should be handled as a DataTree object
# see https://github.com/TomNicholas/VirtualiZarr/issues/11
raise NotImplementedError(f"Unsupported file type: {filetype}")
elif filetype.name.lower() == "tiff":
from kerchunk.tiff import tiff_to_zarr

refs = tiff_to_zarr(filepath, inline_threshold=0)
elif filetype.name.lower() == "fits":
refs = tiff_to_zarr(filepath, inline_threshold=0, **reader_options)
elif filetype.lower() == "fits":
from kerchunk.fits import process_file

refs = process_file(filepath, inline_threshold=0)
refs = process_file(filepath, inline_threshold=0, **reader_options)
else:
raise NotImplementedError(f"Unsupported file type: {filetype.name}")

Expand Down
10 changes: 10 additions & 0 deletions virtualizarr/tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import xarray as xr
import xarray.testing as xrt
from xarray.core.indexes import Index
import pytest

from virtualizarr import open_virtual_dataset
from virtualizarr.manifests import ChunkManifest, ManifestArray
Expand Down Expand Up @@ -271,6 +272,15 @@ def test_combine_by_coords(self, netcdf4_files):
assert combined_vds.xindexes["time"].to_pandas_index().is_monotonic_increasing


pytest.importorskip("s3fs")
@pytest.mark.xfail(reason="currently should xfail for None filetype and None indexes.",run=False)
@pytest.mark.parametrize("filetype", ['netcdf4', None], ids=["netcdf4 filetype", "None filetype"])
@pytest.mark.parametrize("indexes", [None, {}], ids=["None index", "empty dict index"])
def test_anon_read_s3(filetype, indexes):
fpath = 's3://nex-gddp-cmip6/NEX-GDDP-CMIP6/CESM2/historical/r4i1p1f1/pr/pr_day_CESM2_historical_r4i1p1f1_gn_2010.nc'
assert open_virtual_dataset(fpath, filetype=filetype, indexes=indexes, reader_options={'storage_options': {'anon': True}})


class TestLoadVirtualDataset:
def test_loadable_variables(self, netcdf4_file):
vars_to_load = ['air', 'time']
Expand Down
5 changes: 5 additions & 0 deletions virtualizarr/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def open_virtual_dataset(
loadable_variables: Optional[Iterable[str]] = None,
indexes: Optional[Mapping[str, Index]] = None,
virtual_array_class=ManifestArray,
reader_options: Optional[dict] = {'storage_options': {'anon': True}}
) -> xr.Dataset:
"""
Open a file or store as an xarray Dataset wrapping virtualized zarr arrays.
Expand Down Expand Up @@ -53,6 +54,9 @@ def open_virtual_dataset(
virtual_array_class
Virtual array class to use to represent the references to the chunks in each on-disk array.
Currently can only be ManifestArray, but once VirtualZarrArray is implemented the default should be changed to that.
reader_options: dict, default {'storage_options': {'anon': True}}
Dict passed into Kerchunk file readers. Note: Each Kerchunk file reader has distinct arguments,
so ensure reader_options match selected Kerchunk reader arguments.

Returns
-------
Expand Down Expand Up @@ -81,6 +85,7 @@ def open_virtual_dataset(
vds_refs = kerchunk.read_kerchunk_references_from_file(
filepath=filepath,
filetype=filetype,
reader_options=reader_options,
)
virtual_vars = virtual_vars_from_kerchunk_refs(
vds_refs,
Expand Down