Skip to content

Commit

Permalink
refactor fits
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNicholas committed Oct 17, 2024
1 parent bb39907 commit 97fc588
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
13 changes: 9 additions & 4 deletions virtualizarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,16 @@ def open_virtual_dataset(
)

case "fits":
from kerchunk.fits import process_file

# handle inconsistency in kerchunk, see GH issue https://github.com/zarr-developers/VirtualiZarr/issues/160
refs = {"refs": process_file(filepath, **reader_options)}
from virtualizarr.readers.fits import open_virtual_dataset

return open_virtual_dataset(
filepath,
group=group,
drop_variables=drop_variables,
loadable_variables=loadable_variables,
indexes=indexes,
reader_options=reader_options,
)
case _:
raise NotImplementedError(f"Unsupported file type: {filetype.name}")

Expand Down
53 changes: 53 additions & 0 deletions virtualizarr/readers/fits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from typing import Iterable, Mapping, Optional

from kerchunk.fits import process_file
from xarray import Dataset
from xarray.core.indexes import Index

from virtualizarr.backend import (
construct_virtual_dataset,
open_loadable_vars_and_indexes,
)
from virtualizarr.translators.kerchunk import (
extract_group,
virtual_vars_and_metadata_from_kerchunk_refs,
)


def open_virtual_dataset(
filepath: str,
group: str | None = None,
drop_variables: Iterable[str] | None = None,
loadable_variables: Iterable[str] | None = None,
decode_times: bool | None = None,
indexes: Mapping[str, Index] | None = None,
reader_options: Optional[dict] = None,
) -> Dataset:
# handle inconsistency in kerchunk, see GH issue https://github.com/zarr-developers/VirtualiZarr/issues/160
refs = {"refs": process_file(filepath, **reader_options)}

refs = extract_group(refs, group)

virtual_vars, attrs, coord_names = virtual_vars_and_metadata_from_kerchunk_refs(
refs,
loadable_variables,
drop_variables,
)

loadable_vars, indexes = open_loadable_vars_and_indexes(
filepath,
loadable_variables=loadable_variables,
reader_options=reader_options,
drop_variables=drop_variables,
indexes=indexes,
group=group,
decode_times=decode_times,
)

return construct_virtual_dataset(
virtual_vars=virtual_vars,
loadable_vars=loadable_vars,
indexes=indexes,
coord_names=coord_names,
attrs=attrs,
)

0 comments on commit 97fc588

Please sign in to comment.