@@ -432,13 +432,10 @@ def enhanced_add_variables() -> None:
432432 return mdio_template
433433
434434
435- def _chunk_variable (ds : Dataset , variable_name : str ) -> None :
436- """Determines the chunking for a Varible in the Dataset."""
437- idx = - 1
438- for i in range (len (ds .variables )):
439- if ds .variables [i ].name == variable_name :
440- idx = i
441- break
435+ def _chunk_variable (ds : Dataset , target_variable_name : str ) -> None :
436+ """Determines and sets the chunking for a specific Variable in the Dataset."""
437+ # Find variable index by name
438+ index = next ((i for i , obj in enumerate (ds .variables ) if obj .name == target_variable_name ), None )
442439
443440 def determine_target_size (var_type : str ) -> int :
444441 """Determines the target size (in bytes) for a Variable based on its type."""
@@ -447,19 +444,18 @@ def determine_target_size(var_type: str) -> int:
447444 return MAX_COORDINATES_BYTES
448445
449446 # Create the chunk grid metadata
450- var_type = ds .variables [idx ].data_type
451- full_shape = tuple (dim .size for dim in ds .variables [idx ].dimensions )
447+ var_type = ds .variables [index ].data_type
448+ full_shape = tuple (dim .size for dim in ds .variables [index ].dimensions )
452449 target_size = determine_target_size (var_type )
453450
454451 chunk_shape = get_constrained_chunksize (full_shape , var_type , target_size )
455- chunks = RegularChunkGrid (configuration = RegularChunkShape (chunk_shape = chunk_shape ))
452+ chunk_grid = RegularChunkGrid (configuration = RegularChunkShape (chunk_shape = chunk_shape ))
456453
457- # Update the variable's metadata with the new chunk grid
458- if ds .variables [idx ].metadata is None :
459- # ds.variables[idx].metadata = VariableMetadata(chunk_shape=chunks.chunk_shape)
460- ds .variables [idx ].metadata = VariableMetadata (chunk_grid = chunks )
461- else :
462- ds .variables [idx ].metadata .chunk_grid = chunks
454+ # Create variable metadata if it doesn't exist
455+ if ds .variables [index ].metadata is None :
456+ ds .variables [index ].metadata = VariableMetadata ()
457+
458+ ds .variables [index ].metadata .chunk_grid = chunk_grid
463459
464460
465461def segy_to_mdio ( # noqa PLR0913
@@ -520,9 +516,10 @@ def segy_to_mdio( # noqa PLR0913
520516
521517 _add_grid_override_to_metadata (dataset = mdio_ds , grid_overrides = grid_overrides )
522518
523- _chunk_variable (ds = mdio_ds , variable_name = "trace_mask" ) # trace_mask is a Variable and not a Coordinate
519+ # Dynamically chunk the variables based on their type
520+ _chunk_variable (ds = mdio_ds , target_variable_name = "trace_mask" ) # trace_mask is a Variable and not a Coordinate
524521 for coord in mdio_template .coordinate_names :
525- _chunk_variable (ds = mdio_ds , variable_name = coord )
522+ _chunk_variable (ds = mdio_ds , target_variable_name = coord )
526523
527524 xr_dataset : xr_Dataset = to_xarray_dataset (mdio_ds = mdio_ds )
528525
0 commit comments