Skip to content

ChunkCachedArray._raw_vindex raises IndexError when given empty index arrays #2906

Description

@j-atkins

Parcels version

v4.0

Description

Indexing a ChunkCachedArray-backed Field using an empty selection raises an IndexError.

This may occur in multi-phase kernels where e.g. particles only sample a field during specific stages (in my case it happened during Argo Float simulations in VirtualShip sampling only on ascent). At timesteps where no particles meet the criteria yet, the selection mask evaluates to zero particles. E.g. the VirtualShip sampling kernel below at t=0:

def _argo_sample_temperature(particles, fieldset):
    # Phase 3: ascending — sample temperature
    phase_mask = particles.cycle_phase == 3
    depth_mask = particles.z < particles.min_depth 
    sampling_particles = particles[np.logical_and(phase_mask, depth_mask)] # <-- ❗️ 0 particles in this phase at t=0
    sampling_particles.temperature = fieldset.T[sampling_particles] # <-- ❗️ raises IndexError

Code sample

import dask.array as da
import numpy as np
from parcels._chunk_cached_array.core import ChunkCachedArray

arr = da.zeros((10, 10), chunks=(5, 5))
cca = ChunkCachedArray(arr, max_cache_bytes=10_000_000)

empty = np.array([])
result = cca._raw_vindex(empty, empty)
IndexError                                Traceback (most recent call last)
Cell In[9], line 1
----> 1 result = cca._raw_vindex(empty, empty)

File ~/Documents/virtualship/.pixi/envs/default/lib/python3.14/site-packages/parcels/_chunk_cached_array/core.py:126, in ChunkCachedArray._raw_vindex(self, *indices)
    123 grp_indices = sort_order[grp_slice]
    125 # Recover the chunk key tuple from any point in this group.
--> 126 key = tuple(int(chunk_ids[d, grp_indices[0]]) for d in range(ndim))
    128 chunk_data = self.cache.get(key)
    129 if chunk_data is None:

File ~/Documents/virtualship/.pixi/envs/default/lib/python3.14/site-packages/parcels/_chunk_cached_array/core.py:126, in <genexpr>(.0)
    123 grp_indices = sort_order[grp_slice]
    125 # Recover the chunk key tuple from any point in this group.
--> 126 key = tuple(int(chunk_ids[d, grp_indices[0]]) for d in range(ndim))
    128 chunk_data = self.cache.get(key)
    129 if chunk_data is None:

IndexError: index 0 is out of bounds for axis 0 with size 0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triageIssue that has not been reviewed by a Parcels team member

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions