1010 _set_attrs_on_all_vars ,
1111 _set_as_coord ,
1212 _1d_coord_from_spacing ,
13+ _maybe_rename_dimension ,
1314)
1415
1516REGISTERED_GEOMETRIES = {}
@@ -144,7 +145,7 @@ def apply_geometry(ds, geometry_name, *, coordinates=None, grid=None):
144145 # 'dx' may not be consistent between different regions (e.g. core and PFR).
145146 # For some geometries xcoord may have already been created by
146147 # add_geometry_coords, in which case we do not need this.
147- nx = updated_ds .dims [xcoord ]
148+ nx = updated_ds .sizes [xcoord ]
148149
149150 # can't use commented out version, uncommented one works around xarray bug
150151 # removing attrs
@@ -181,7 +182,7 @@ def apply_geometry(ds, geometry_name, *, coordinates=None, grid=None):
181182 if zcoord in updated_ds .dims and zcoord not in updated_ds .coords :
182183 # Generates a coordinate whose value is 0 on the first grid point, not dz/2, to
183184 # match how BOUT++ generates fields from input file expressions.
184- nz = updated_ds .dims [zcoord ]
185+ nz = updated_ds .sizes [zcoord ]
185186
186187 # In BOUT++ v5, dz is either a Field2D or Field3D.
187188 # We can use it as a 1D coordinate if it's a Field3D, _or_ if nz == 1
@@ -213,7 +214,7 @@ def apply_geometry(ds, geometry_name, *, coordinates=None, grid=None):
213214 dz = updated_ds ["dz" ]
214215
215216 z0 = 2 * np .pi * updated_ds .metadata ["ZMIN" ]
216- z1 = z0 + nz * dz
217+ z1 = z0 + nz * dz . data [()]
217218 if not np .all (
218219 np .isclose (
219220 z1 ,
@@ -392,12 +393,12 @@ def add_toroidal_geometry_coords(ds, *, coordinates=None, grid=None):
392393 ],
393394 )
394395
395- if "t" in ds . dims :
396+ if coordinates [ "t" ] != "t" :
396397 # Rename 't' if user requested it
397- ds = ds . rename ( t = coordinates ["t" ])
398+ ds = _maybe_rename_dimension ( ds , "t" , coordinates ["t" ])
398399
399400 # Change names of dimensions to Orthogonal Toroidal ones
400- ds = ds . rename ( y = coordinates ["y" ])
401+ ds = _maybe_rename_dimension ( ds , "y" , coordinates ["y" ])
401402
402403 # TODO automatically make this coordinate 1D in simplified cases?
403404 ds = ds .rename (psixy = coordinates ["x" ])
@@ -413,7 +414,7 @@ def add_toroidal_geometry_coords(ds, *, coordinates=None, grid=None):
413414
414415 # If full data (not just grid file) then toroidal dim will be present
415416 if "z" in ds .dims :
416- ds = ds . rename ( z = coordinates ["z" ])
417+ ds = _maybe_rename_dimension ( ds , "z" , coordinates ["z" ])
417418
418419 # Record which dimension 'z' was renamed to.
419420 ds .metadata ["bout_zdim" ] = coordinates ["z" ]
@@ -505,7 +506,7 @@ def add_s_alpha_geometry_coords(ds, *, coordinates=None, grid=None):
505506 ds ["r" ] = ds ["hthe" ].isel ({ycoord : 0 }).squeeze (drop = True )
506507 ds ["r" ].attrs ["units" ] = "m"
507508 ds = ds .set_coords ("r" )
508- ds = ds .rename (x = "r" )
509+ ds = ds .swap_dims (x = "r" )
509510 ds .metadata ["bout_xdim" ] = "r"
510511
511512 if hthe_from_grid :
0 commit comments