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

Add astropy for fits test #403

Closed
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ hdf_reader = [
icechunk = [
"icechunk==0.1.0a8",
]
kerchunk_readers = [
"astropy",
]
test = [
"codecov",
"fastparquet",
Expand All @@ -53,6 +56,7 @@ test = [
"pandas-stubs",
"pooch",
"pre-commit",
"pytest-asyncio",
"pytest-cov",
"pytest-mypy",
"pytest",
Expand Down Expand Up @@ -87,6 +91,10 @@ datatree = ["py.typed"]
files = "virtualizarr/**/*.py"
show_error_codes = true

[[tool.mypy.overrides]]
module = "astropy.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "fsspec.*"
ignore_missing_imports = true
Expand Down
12 changes: 7 additions & 5 deletions virtualizarr/readers/fits.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Iterable, Mapping, Optional
from typing import Any, Iterable, Mapping

from xarray import Dataset, Index, Variable

Expand All @@ -23,18 +23,20 @@ def open_virtual_dataset(
loadable_variables: Iterable[str] | None = None,
decode_times: bool | None = None,
indexes: Mapping[str, Index] | None = None,
virtual_backend_kwargs: Optional[dict] = None,
reader_options: Optional[dict] = None,
virtual_backend_kwargs: Mapping[str, Any] | None = None,
reader_options: Mapping[str, Any] | None = None,
) -> Dataset:
from kerchunk.fits import process_file
from virtualizarr.vendor.kerchunk.fits import process_file

if virtual_backend_kwargs:
raise NotImplementedError(
"FITS reader does not understand any virtual_backend_kwargs"
)

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

# both group=None and group='' mean to read root group
if group:
Expand Down
13 changes: 9 additions & 4 deletions virtualizarr/vendor/kerchunk/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from virtualizarr.vendor.kerchunk.codecs import AsciiTableCodec, VarArrCodec

try:
from astropy.io import fits
from astropy.wcs import WCS
from astropy.io import fits # noqa: F401
from astropy.wcs import WCS # noqa: F401
except ModuleNotFoundError: # pragma: no cover
raise ImportError(
"astropy is required for kerchunking FITS files. Please install with "
"`pip/conda install astropy`."
)
) from None

logger = logging.getLogger("fits-to-zarr")

Expand Down Expand Up @@ -162,7 +162,12 @@ def process_file(
# TODO: we could sub-chunk on biggest dimension
name = hdu.name or str(ext)
arr = g.empty(
name, dtype=dtype, shape=shape, chunks=shape, compression=None, **kwargs
name=name,
dtype=dtype,
shape=shape,
chunks=shape,
compression=None,
**kwargs,
)
arr.attrs.update(
{
Expand Down