MCPcopy
hub / github.com/pydata/xarray / _update_bounds_encoding

Function _update_bounds_encoding

xarray/conventions.py:291–336  ·  view source on GitHub ↗

Adds time encoding to time bounds variables. Variables handling time bounds ("Cell boundaries" in the CF conventions) do not necessarily carry the necessary attributes to be decoded. This copies the encoding from the time variable to the associated bounds variable so that we write C

(variables: T_Variables)

Source from the content-addressed store, hash-verified

289
290
291def _update_bounds_encoding(variables: T_Variables) -> None:
292 """Adds time encoding to time bounds variables.
293
294 Variables handling time bounds ("Cell boundaries" in the CF
295 conventions) do not necessarily carry the necessary attributes to be
296 decoded. This copies the encoding from the time variable to the
297 associated bounds variable so that we write CF-compliant files.
298
299 See Also:
300
301 http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/
302 cf-conventions.html#cell-boundaries
303
304 https://github.com/pydata/xarray/issues/2565
305 """
306
307 # For all time variables with bounds
308 for name, v in variables.items():
309 attrs = v.attrs
310 encoding = v.encoding
311 has_date_units = "units" in encoding and "since" in encoding["units"]
312 is_datetime_type = np.issubdtype(
313 v.dtype, np.datetime64
314 ) or contains_cftime_datetimes(v)
315
316 if (
317 is_datetime_type
318 and not has_date_units
319 and "bounds" in attrs
320 and attrs["bounds"] in variables
321 ):
322 emit_user_level_warning(
323 f"Variable {name} has datetime type and a "
324 f"bounds variable but {name}.encoding does not have "
325 f"units specified. The units encodings for {name} "
326 f"and {attrs['bounds']} will be determined independently "
327 "and may not be equal, counter to CF-conventions. "
328 "If this is a concern, specify a units encoding for "
329 f"{name} before writing to a file.",
330 )
331
332 if has_date_units and "bounds" in attrs and attrs["bounds"] in variables:
333 bounds_encoding = variables[attrs["bounds"]].encoding
334 bounds_encoding.setdefault("units", encoding["units"])
335 if "calendar" in encoding:
336 bounds_encoding.setdefault("calendar", encoding["calendar"])
337
338
339T = TypeVar("T")

Callers 1

cf_encoderFunction · 0.85

Calls 3

emit_user_level_warningFunction · 0.90
itemsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…