(time_unit: PDDatetimeUnitOptions)
| 722 | |
| 723 | |
| 724 | def test_decode_cf_time_bounds(time_unit: PDDatetimeUnitOptions) -> None: |
| 725 | da = DataArray( |
| 726 | np.arange(6, dtype="int64").reshape((3, 2)), |
| 727 | coords={"time": [1, 2, 3]}, |
| 728 | dims=("time", "nbnd"), |
| 729 | name="time_bnds", |
| 730 | ) |
| 731 | |
| 732 | attrs = { |
| 733 | "units": "days since 2001-01", |
| 734 | "calendar": "standard", |
| 735 | "bounds": "time_bnds", |
| 736 | } |
| 737 | |
| 738 | ds = da.to_dataset() |
| 739 | ds["time"].attrs.update(attrs) |
| 740 | _update_bounds_attributes(ds.variables) |
| 741 | assert ds.variables["time_bnds"].attrs == { |
| 742 | "units": "days since 2001-01", |
| 743 | "calendar": "standard", |
| 744 | } |
| 745 | dsc = decode_cf(ds, decode_times=CFDatetimeCoder(time_unit=time_unit)) |
| 746 | assert dsc.time_bnds.dtype == np.dtype(f"=M8[{time_unit}]") |
| 747 | dsc = decode_cf(ds, decode_times=False) |
| 748 | assert dsc.time_bnds.dtype == np.dtype("int64") |
| 749 | |
| 750 | # Do not overwrite existing attrs |
| 751 | ds = da.to_dataset() |
| 752 | ds["time"].attrs.update(attrs) |
| 753 | bnd_attr = {"units": "hours since 2001-01", "calendar": "noleap"} |
| 754 | ds["time_bnds"].attrs.update(bnd_attr) |
| 755 | _update_bounds_attributes(ds.variables) |
| 756 | assert ds.variables["time_bnds"].attrs == bnd_attr |
| 757 | |
| 758 | # If bounds variable not available do not complain |
| 759 | ds = da.to_dataset() |
| 760 | ds["time"].attrs.update(attrs) |
| 761 | ds["time"].attrs["bounds"] = "fake_var" |
| 762 | _update_bounds_attributes(ds.variables) |
| 763 | |
| 764 | |
| 765 | @requires_cftime |
nothing calls this directly
no test coverage detected
searching dependent graphs…