@@ -1326,7 +1326,7 @@ def rename_like(
1326
1326
Variables will be renamed to match variable names in this xarray object
1327
1327
skip: str, Iterable[str], optional
1328
1328
Limit the renaming excluding
1329
- ("axes", "cell_measures", "coordinates", "standard_names")
1329
+ ("axes", "bounds", cell_measures", "coordinates", "standard_names")
1330
1330
or a subset thereof.
1331
1331
1332
1332
Returns
@@ -1341,24 +1341,48 @@ def rename_like(
1341
1341
good_keys = ourkeys & theirkeys
1342
1342
keydict = {}
1343
1343
for key in good_keys :
1344
- ours = set (_get_all ( self ._obj , key ))
1345
- theirs = set (_get_all ( other , key ))
1344
+ ours = set (apply_mapper ( _get_all , self ._obj , key ))
1345
+ theirs = set (apply_mapper ( _get_all , other , key ))
1346
1346
for attr in skip :
1347
- ours -= set (getattr (self , attr ).get (key , []))
1348
- theirs -= set (getattr (other .cf , attr ).get (key , []))
1347
+ ours . difference_update (getattr (self , attr ).get (key , []))
1348
+ theirs . difference_update (getattr (other .cf , attr ).get (key , []))
1349
1349
if ours and theirs :
1350
1350
keydict [key ] = dict (ours = list (ours ), theirs = list (theirs ))
1351
1351
1352
- conflicts = {}
1353
- for k0 , v0 in keydict .items ():
1354
- if len (v0 ["ours" ]) > 1 or len (v0 ["theirs" ]) > 1 :
1355
- conflicts [k0 ] = v0
1356
- continue
1357
- for v1 in keydict .values ():
1358
- # Conflicts have same ours but different theirs or vice versa
1359
- if (v0 ["ours" ] == v1 ["ours" ]) != (v0 ["theirs" ] == v1 ["theirs" ]):
1352
+ def get_renamer_and_conflicts (keydict ):
1353
+ conflicts = {}
1354
+ for k0 , v0 in keydict .items ():
1355
+ if len (v0 ["ours" ]) > 1 or len (v0 ["theirs" ]) > 1 :
1360
1356
conflicts [k0 ] = v0
1361
- break
1357
+ continue
1358
+ for v1 in keydict .values ():
1359
+ # Conflicts have same ours but different theirs or vice versa
1360
+ if (v0 ["ours" ] == v1 ["ours" ]) != (v0 ["theirs" ] == v1 ["theirs" ]):
1361
+ conflicts [k0 ] = v0
1362
+ break
1363
+
1364
+ renamer = {
1365
+ v ["ours" ][0 ]: v ["theirs" ][0 ]
1366
+ for k , v in keydict .items ()
1367
+ if k not in conflicts
1368
+ }
1369
+
1370
+ return renamer , conflicts
1371
+
1372
+ # Run get_renamer_and_conflicts twice.
1373
+ # The second time add the bounds associated with variables to rename
1374
+ renamer , conflicts = get_renamer_and_conflicts (keydict )
1375
+ if "bounds" not in skip :
1376
+ for k , v in renamer .items ():
1377
+ ours = set (getattr (self , "bounds" , {}).get (k , []))
1378
+ theirs = set (getattr (other .cf , "bounds" , {}).get (v , []))
1379
+ if ours and theirs :
1380
+ ours .update (keydict .get (k , {}).get ("ours" , []))
1381
+ theirs .update (keydict .get (k , {}).get ("theirs" , []))
1382
+ keydict [k ] = dict (ours = list (ours ), theirs = list (theirs ))
1383
+ renamer , conflicts = get_renamer_and_conflicts (keydict )
1384
+
1385
+ # Rename and warn
1362
1386
if conflicts :
1363
1387
warnings .warn (
1364
1388
"Conflicting variables skipped:\n "
@@ -1372,23 +1396,28 @@ def rename_like(
1372
1396
),
1373
1397
UserWarning ,
1374
1398
)
1375
-
1376
- renamer = {
1377
- v ["ours" ][0 ]: v ["theirs" ][0 ]
1378
- for k , v in keydict .items ()
1379
- if k not in conflicts
1380
- }
1381
1399
newobj = self ._obj .rename (renamer )
1382
1400
1383
- # rename variable names in the coordinates attribute
1401
+ # rename variable names in the attributes
1384
1402
# if present
1385
1403
ds = self ._maybe_to_dataset (newobj )
1386
1404
for _ , variable in ds .variables .items ():
1387
- coordinates = variable .attrs .get ("coordinates" , None )
1388
- if coordinates :
1389
- for k , v in renamer .items ():
1390
- coordinates = coordinates .replace (k , v )
1391
- variable .attrs ["coordinates" ] = coordinates
1405
+ for attr in ("bounds" , "coordinates" , "cell_measures" ):
1406
+ if attr == "cell_measures" :
1407
+ varlist = [
1408
+ f"{ k } : { renamer .get (v , v )} "
1409
+ for k , v in parse_cell_methods_attr (
1410
+ variable .attrs .get (attr , "" )
1411
+ ).items ()
1412
+ ]
1413
+ else :
1414
+ varlist = [
1415
+ renamer .get (var , var )
1416
+ for var in variable .attrs .get (attr , "" ).split ()
1417
+ ]
1418
+
1419
+ if varlist :
1420
+ variable .attrs [attr ] = " " .join (varlist )
1392
1421
return self ._maybe_to_dataarray (ds )
1393
1422
1394
1423
def guess_coord_axis (self , verbose : bool = False ) -> Union [DataArray , Dataset ]:
0 commit comments