code: def ensure_time_as_index(ds):
      
      time_indexed_coords = {TIME_WEIGHTS_STR, TIME_BOUNDS_STR}
      time_indexed_vars = set(ds.data_vars).union(time_indexed_coords)
      time_indexed_vars = time_indexed_vars.intersection(ds.variables)
      variables_to_replace = {}
      for name in time_indexed_vars:
          if TIME_STR not in ds[name].indexes:
              da = ds[name]
              if TIME_STR not in da.dims:
                  da = ds[name].expand_dims(TIME_STR)
              da = da.assign_coords(**{TIME_STR: ds[TIME_STR]})
              variables_to_replace[name] = da
      return ds.assign(**variables_to_replace)
docs: Ensures that time is an indexed coordinate on relevant quantites.
  
      Sometimes when the data we load from disk has only one timestep, the
      indexing of time-defined quantities in the resulting xarray.Dataset gets
      messed up, in that the time bounds array and data variables don't get
      indexed by time, even though they should.  Therefore, we need this helper
      function to (possibly) correct this.
  
      Note that this must be applied before CF-conventions are decoded; otherwise
      it casts ``np.datetime64[ns]`` as ``int`` values.
  
      Parameters
      ----------
      ds : Dataset
          Dataset with a time coordinate
  
      Returns
      -------
      Dataset