Repository: zarr-developers/VirtualiZarr

Issue:
_ARRAY_DIMENSIONS still present in attrs of virtual dataset
Opening a virtual dataset from netCDF with `open_virtual_dataset` I saw

<img width="382" alt="Screenshot 2024-06-19 at 5 27 06 PM" src="https://github.com/zarr-developers/VirtualiZarr/assets/35968931/ee9ac900-31a9-4fea-8a60-b4311d06b6de">

The `_ARRAY_DIMENSIONS` should not be there, as it is redundant with the actual `.dims` of the `xarray.Variable` object. We should be checking that we've deleted `_ARRAY_DIMENSIONS` from the `.attrs`, and only re-instate it when writing out references to disk.

Discussion:

Patch:
diff --git a/docs/releases.rst b/docs/releases.rst
index ac47912..e7483c0 100644
--- a/docs/releases.rst
+++ b/docs/releases.rst
@@ -25,6 +25,8 @@ Deprecations
 Bug fixes
 ~~~~~~~~~
 
+- Ensure that `_ARRAY_DIMENSIONS` are dropped from variable `.attrs`. (:issue:`150`, :pull:`152`) 
+  By `Tom Nicholas <https://github.com/TomNicholas>`_.
 
 Documentation
 ~~~~~~~~~~~~~
diff --git a/virtualizarr/xarray.py b/virtualizarr/xarray.py
index a67aa01..96dbf23 100644
--- a/virtualizarr/xarray.py
+++ b/virtualizarr/xarray.py
@@ -288,8 +288,12 @@ def variable_from_kerchunk_refs(
 
     arr_refs = kerchunk.extract_array_refs(refs, var_name)
     chunk_dict, zarray, zattrs = kerchunk.parse_array_refs(arr_refs)
+
     manifest = ChunkManifest._from_kerchunk_chunk_dict(chunk_dict)
-    dims = zattrs["_ARRAY_DIMENSIONS"]
+
+    # we want to remove the _ARRAY_DIMENSIONS from the final variables' .attrs
+    dims = zattrs.pop("_ARRAY_DIMENSIONS")
+
     varr = virtual_array_class(zarray=zarray, chunkmanifest=manifest)
 
     return xr.Variable(data=varr, dims=dims, attrs=zattrs)


Test patch:
diff --git a/virtualizarr/tests/test_xarray.py b/virtualizarr/tests/test_xarray.py
index 317b1f6..447d920 100644
--- a/virtualizarr/tests/test_xarray.py
+++ b/virtualizarr/tests/test_xarray.py
@@ -228,6 +228,13 @@ class TestConcat:
         assert result.data.zarray.zarr_format == zarray.zarr_format
 
 
+class TestOpenVirtualDatasetAttrs:
+    def test_drop_array_dimensions(self, netcdf4_file):
+        # regression test for GH issue #150
+        vds = open_virtual_dataset(netcdf4_file, indexes={})
+        assert "_ARRAY_DIMENSIONS" not in vds["air"].attrs
+
+
 class TestOpenVirtualDatasetIndexes:
     def test_no_indexes(self, netcdf4_file):
         vds = open_virtual_dataset(netcdf4_file, indexes={})