Skip to content

Commit e3d6a61

Browse files
committed
Make ParticleFile store and create_new_zarrfile attrs read only
1 parent 16032a4 commit e3d6a61

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

parcels/particlefile.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ def __init__(self, store, outputdt, chunks=None, create_new_zarrfile=True):
7878
self._maxids = 0
7979
self._pids_written = {}
8080
self.metadata = {}
81-
self.create_new_zarrfile = create_new_zarrfile
81+
self._create_new_zarrfile = create_new_zarrfile
8282

83-
if isinstance(store, zarr.storage.Store):
84-
self.store = store
85-
else:
86-
self.store = _get_store_from_pathlike(store)
83+
if not isinstance(store, zarr.storage.Store):
84+
store = _get_store_from_pathlike(store)
85+
86+
self._store = store
8787

8888
# TODO v4: Enable once updating to zarr v3
8989
# if store.read_only:
@@ -118,6 +118,14 @@ def outputdt(self):
118118
def chunks(self):
119119
return self._chunks
120120

121+
@property
122+
def store(self):
123+
return self._store
124+
125+
@property
126+
def create_new_zarrfile(self):
127+
return self._create_new_zarrfile
128+
121129
def _convert_varout_name(self, var):
122130
if var == "depth":
123131
return "z"
@@ -223,7 +231,7 @@ def _write_particle_data(self, *, particle_data, pclass, time_interval, time, in
223231
ds[varout] = xr.DataArray(data=data, dims=dims, attrs=attrs[var.name])
224232
ds[varout].encoding["chunks"] = self.chunks[0] if var.to_write == "once" else self.chunks # type: ignore[index]
225233
ds.to_zarr(store, mode="w")
226-
self.create_new_zarrfile = False
234+
self._create_new_zarrfile = False
227235
else:
228236
Z = zarr.group(store=store, overwrite=False)
229237
obs = particle_data["obs_written"][indices_to_write]

tests/v4/test_particlefile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ def test_particlefile_init(tmp_store):
405405
ParticleFile(tmp_store, outputdt=np.timedelta64(1, "s"), chunks=(1, 3))
406406

407407

408+
@pytest.mark.parametrize("name", ["store", "outputdt", "chunks", "create_new_zarrfile"])
409+
def test_particlefile_readonly_attrs(tmp_store, name):
410+
pfile = ParticleFile(tmp_store, outputdt=np.timedelta64(1, "s"), chunks=(1, 3))
411+
with pytest.raises(AttributeError, match="property .* of 'ParticleFile' object has no setter"):
412+
setattr(pfile, name, "something")
413+
414+
408415
def test_particlefile_init_invalid(tmp_store): # TODO: Add test for read only store
409416
with pytest.raises(ValueError, match="chunks must be a tuple"):
410417
ParticleFile(tmp_store, outputdt=np.timedelta64(1, "s"), chunks=1)

0 commit comments

Comments
 (0)