Skip to content

Commit

Permalink
Specify dimension chunking in C-order
Browse files Browse the repository at this point in the history
  • Loading branch information
sjperkins committed Mar 19, 2024
1 parent c65cf77 commit f704560
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
4 changes: 1 addition & 3 deletions tests/test_xarrayfits.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ def test_distributed(beam_cube):
cluster = stack.enter_context(LocalCluster(n_workers=8, processes=True))
stack.enter_context(Client(cluster))

xds = xds_from_fits(
beam_cube, chunks={"NAXIS1": 15, "NAXIS2": 100, "NAXIS3": 100}
)
xds = xds_from_fits(beam_cube, chunks={0: 100, 1: 100, 2: 15})
expected = np.arange(np.prod(xds.hdu0.shape)).reshape(xds.hdu0.shape)
np.testing.assert_array_equal(expected, xds.hdu0.data)
assert xds.hdu0.data.chunks == ((100, 100, 57), (100, 100, 57), (15, 15, 2))
26 changes: 13 additions & 13 deletions xarrayfits/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def array_from_fits_hdu(
simple = hdu.header["SIMPLE"]

if simple is False:
raise ValueError(f"Non-fits conforming hdu {hdu} " f"header['SIMPLE']={simple}")
raise ValueError(
f"HDU {hdu} doesn't conform " f"to the FITS standard: " f"SIMPLE={simple}"
)

try:
dtype = INV_BITPIX_MAP[bitpix]
Expand All @@ -149,25 +151,23 @@ def array_from_fits_hdu(
flat_chunks = []

# At this point we are dealing with FORTRAN ordered axes
for i in range(1, naxis + 1):
ax_key = f"NAXIS{i}"
s = hdu.header[ax_key]
shape.append(s)
for i in range(naxis):
ax_key = f"NAXIS{naxis - i}"
ax_shape = hdu.header[ax_key]
shape.append(ax_shape)

try:
# Try add existing chunking strategies to the list
flat_chunks.append(chunks[ax_key])
flat_chunks.append(chunks[i])
except KeyError:
# Otherwise do single slices of some row major axes
flat_chunks.append(1 if i > 2 else s)
flat_chunks.append(ax_shape)

array = generate_slice_gets(
fits_proxy,
hdu_index,
# Reverse to get C major ordering
tuple(reversed(shape)),
tuple(shape),
dtype,
tuple(reversed(flat_chunks)),
tuple(flat_chunks),
)

dims = tuple(f"{name_prefix}{hdu_index}-{i}" for i in range(0, naxis))
Expand All @@ -189,8 +189,8 @@ def xds_from_fits(fits_filename, hdus=None, name_prefix="hdu", chunks=None):
chunks : dictionary or list of dictionaries, optional
Chunking strategy for each dimension of each hdu.
Dimensions should be specified via the
standard FITS dimensions
:code:`{'NAXIS1' : 513, 'NAXIS2' : 513, 'NAXIS3' : 33}`
C order dimensions
:code:`{0: 513, 1: 513, 2: 33}`
Returns
-------
Expand Down

0 comments on commit f704560

Please sign in to comment.