@@ -2408,6 +2408,109 @@ def set_construct(
2408
2408
# Return the construct key
2409
2409
return out
2410
2410
2411
+ def del_construct (self , * identity , default = ValueError (), ** filter_kwargs ):
2412
+ """Remove a metadata construct.
2413
+
2414
+ If a domain axis construct is selected for removal then it
2415
+ can't be spanned by any data arrays of the field nor metadata
2416
+ constructs, nor be referenced by any cell method
2417
+ constructs. However, a domain ancillary construct may be
2418
+ removed even if it is referenced by coordinate reference
2419
+ construct.
2420
+
2421
+ .. versionadded:: NEXTVERSION
2422
+
2423
+ .. seealso:: `get_construct`, `constructs`, `has_construct`,
2424
+ `set_construct`
2425
+
2426
+ :Parameters:
2427
+
2428
+ identity:
2429
+ Select the unique construct that has the identity,
2430
+ defined by its `!identities` method, that matches the
2431
+ given values.
2432
+
2433
+ Additionally, the values are matched against construct
2434
+ identifiers, with or without the ``'key%'`` prefix.
2435
+
2436
+ {{value match}}
2437
+
2438
+ {{displayed identity}}
2439
+
2440
+ default: optional
2441
+ Return the value of the *default* parameter if the
2442
+ data axes have not been set.
2443
+
2444
+ {{default Exception}}
2445
+
2446
+ {{filter_kwargs: optional}}
2447
+
2448
+ .. versionadded:: NEXTVERSION
2449
+
2450
+ :Returns:
2451
+
2452
+ The removed metadata construct.
2453
+
2454
+ **Examples**
2455
+
2456
+ >>> f = {{package}}.example_field(0)
2457
+ >>> print(f)
2458
+ Field: specific_humidity (ncvar%q)
2459
+ ----------------------------------
2460
+ Data : specific_humidity(latitude(5), longitude(8)) 1
2461
+ Cell methods : area: mean
2462
+ Dimension coords: latitude(5) = [-75.0, ..., 75.0] degrees_north
2463
+ : longitude(8) = [22.5, ..., 337.5] degrees_east
2464
+ : time(1) = [2019-01-01 00:00:00]
2465
+ >>> f.del_construct('time')
2466
+ <{{repr}}DimensionCoordinate: time(1) days since 2018-12-01 >
2467
+ >>> f.del_construct('time')
2468
+ Traceback (most recent call last):
2469
+ ...
2470
+ ValueError: Can't find unique construct to remove
2471
+ >>> f.del_construct('time', default='No time')
2472
+ 'No time'
2473
+ >>> f.del_construct('dimensioncoordinate1')
2474
+ <{{repr}}DimensionCoordinate: longitude(8) degrees_east>
2475
+ >>> print(f)
2476
+ Field: specific_humidity (ncvar%q)
2477
+ ----------------------------------
2478
+ Data : specific_humidity(latitude(5), ncdim%lon(8)) 1
2479
+ Cell methods : area: mean
2480
+ Dimension coords: latitude(5) = [-75.0, ..., 75.0] degrees_north
2481
+
2482
+ """
2483
+ # Need to re-define to overload this method since cfdm doesn't
2484
+ # have the concept of cyclic axes, so have to update the
2485
+ # register of cyclic axes when we delete a construct in cf.
2486
+
2487
+ # Get the relevant key first because it will be lost upon deletion
2488
+ key = self .construct_key (* identity , default = None , ** filter_kwargs )
2489
+ cyclic_axes = self ._cyclic
2490
+
2491
+ deld_construct = super ().del_construct (
2492
+ * identity , default = None , ** filter_kwargs
2493
+ )
2494
+ if deld_construct is None :
2495
+ if default is None :
2496
+ return
2497
+
2498
+ return self ._default (
2499
+ default , "Can't find unique construct to remove"
2500
+ )
2501
+
2502
+ # If the construct deleted was a cyclic axes, remove it from the set
2503
+ # of stored cyclic axes, to sync that. This is safe now, since given
2504
+ # the block above we can be sure the deletion was successful.
2505
+ if key in cyclic_axes :
2506
+ # Never change value of _cyclic attribute in-place. Only copy now
2507
+ # when the copy is known to be required.
2508
+ cyclic_axes = cyclic_axes .copy ()
2509
+ cyclic_axes .remove (key )
2510
+ self ._cyclic = cyclic_axes
2511
+
2512
+ return deld_construct
2513
+
2411
2514
def set_coordinate_reference (
2412
2515
self , coordinate_reference , key = None , parent = None , strict = True
2413
2516
):
0 commit comments