| 1 |
import os.path |
| 2 |
import fsspec |
| 3 |
import pytest |
| 4 |
|
| 5 |
from kerchunk.utils import refs_as_store |
| 6 |
|
| 7 |
|
| 8 |
fits = pytest.importorskip("astropy.io.fits") |
| 9 |
import kerchunk.fits |
| 10 |
import zarr |
| 11 |
|
| 12 |
testdir = os.path.dirname(fits.util.get_testdata_filepath("btable.fits")) |
| 13 |
btable = os.path.join(testdir, "btable.fits") |
| 14 |
range_im = os.path.join(testdir, "arange.fits") |
| 15 |
var = os.path.join(testdir, "variable_length_table.fits") |
| 16 |
|
| 17 |
|
| 18 |
def test_image(): |
| 19 |
# this one directly hits a remote server - should cache? |
| 20 |
url = "https://fits.gsfc.nasa.gov/samples/WFPC2ASSNu5780205bx.fits" |
| 21 |
out = kerchunk.fits.process_file(url) |
| 22 |
g = zarr.open( |
| 23 |
"reference://", |
| 24 |
storage_options=dict( |
| 25 |
fo=out, |
| 26 |
remote_protocol="https", |
| 27 |
asynchronous=True, |
| 28 |
remote_options={"asynchronous": True}, |
| 29 |
), |
| 30 |
) |
| 31 |
|
| 32 |
# store = refs_as_store(out, remote_protocol="https") |
| 33 |
# g = zarr.open(store) |
| 34 |
arr = g["PRIMARY"][:] |
| 35 |
with fsspec.open( |
| 36 |
"https://fits.gsfc.nasa.gov/samples/WFPC2ASSNu5780205bx.fits" |
| 37 |
) as f: |
| 38 |
hdu = fits.getdata(f) |
| 39 |
assert (hdu == arr).all() |
| 40 |
|
| 41 |
|
| 42 |
def test_ascii_table(): |
| 43 |
# this one directly hits a remote server - should cache? |
| 44 |
url = "https://fits.gsfc.nasa.gov/samples/WFPC2u5780205r_c0fx.fits" |
| 45 |
out = kerchunk.fits.process_file(url, extension=1) |
| 46 |
store = refs_as_store(out, remote_protocol="https") |
| 47 |
g = zarr.open(store, zarr_format=2) |
| 48 |
arr = g["u5780205r_cvt.c0h.tab"][:] |
| 49 |
with fsspec.open( |
| 50 |
"https://fits.gsfc.nasa.gov/samples/WFPC2u5780205r_c0fx.fits" |
| 51 |
) as f: |
| 52 |
hdul = fits.open(f) |
| 53 |
hdu = hdul[1] |
| 54 |
assert list(hdu.data.astype(arr.dtype) == arr) == [True, True, True, True] |
| 55 |
|
| 56 |
|
| 57 |
def test_binary_table(): |
| 58 |
out = kerchunk.fits.process_file(btable, extension=1) |
| 59 |
store = refs_as_store(out) |
| 60 |
z = zarr.open(store, zarr_format=2) |
| 61 |
arr = z["1"] |
| 62 |
with open(btable, "rb") as f: |
| 63 |
hdul = fits.open(f) |
| 64 |
attr2 = dict(arr.attrs) |
| 65 |
assert attr2.pop("_ARRAY_DIMENSIONS") == ["x"] |
| 66 |
assert attr2 == dict(hdul[1].header) |
| 67 |
assert (arr[:]["order"] == hdul[1].data["order"]).all() |
| 68 |
assert (arr[:]["mag"] == hdul[1].data["mag"]).all() |
| 69 |
assert ( |
| 70 |
arr[:]["name"].astype("U") == hdul[1].data["name"] |
| 71 |
).all() # string come out as bytes |
| 72 |
|
| 73 |
|
| 74 |
def test_cube(): |
| 75 |
out = kerchunk.fits.process_file(range_im) |
| 76 |
store = refs_as_store(out) |
| 77 |
z = zarr.open(store, zarr_format=2) |
| 78 |
arr = z["PRIMARY"] |
| 79 |
with open(range_im, "rb") as f: |
| 80 |
hdul = fits.open(f) |
| 81 |
expected = hdul[0].data |
| 82 |
assert (arr[:] == expected).all() |
| 83 |
|
| 84 |
|
| 85 |
def test_with_class(): |
| 86 |
ftz = kerchunk.fits.FitsToZarr(range_im) |
| 87 |
out = ftz.translate() |
| 88 |
assert "fits" in repr(ftz) |
| 89 |
store = refs_as_store(out) |
| 90 |
z = zarr.open(store, zarr_format=2) |
| 91 |
arr = z["PRIMARY"] |
| 92 |
with open(range_im, "rb") as f: |
| 93 |
hdul = fits.open(f) |
| 94 |
expected = hdul[0].data |
| 95 |
assert (arr[:] == expected).all() |
| 96 |
|
| 97 |
|
| 98 |
def test_var(): |
| 99 |
data = fits.open(var)[1].data |
| 100 |
expected = [_.tolist() for _ in data["var"]] |
| 101 |
|
| 102 |
ftz = kerchunk.fits.FitsToZarr(var) |
| 103 |
out = ftz.translate() |
| 104 |
store = refs_as_store(out) |
| 105 |
z = zarr.open(store, zarr_format=2) |
| 106 |
arr = z["1"] |
| 107 |
vars = [_.tolist() for _ in arr[:]["var"]] |
| 108 |
|
| 109 |
assert vars == expected |
| 110 |
assert (z["1"][:]["xyz"] == data["xyz"]).all() |