Debian Codegraph @

tests/test_fits.py

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