Conversation
|
Hi @pgierz, what do you think? For the change in pipeline.py, it works! |
|
I can suggest an alternative workflow in this case. we can use To standardize the time dimension label, user can create a custom function called import xarray as xr
from pymor.core.time_utils import get_time_label
def relabel_time_dimension_if_required(
obj: xr.Dataset | xr.DataArray,
target: str = "time",
raise_on_conflict: bool = True,
) -> xr.Dataset | xr.DataArray:
"""Renames the time dimension to a target label, if required.
This function detects the time dimension using `get_time_label`. If the
detected label differs from the `target`, it returns a new object with the
dimension renamed. Otherwise, it returns the original object.
Parameters
----------
obj : xr.Dataset or xr.DataArray
The object to process.
target : str, optional
The desired name for the time dimension, by default "time".
raise_on_conflict : bool, optional
If True, raise a ValueError if the target name already exists but is
not the detected time dimension. If False, the function will be a no-op
in case of a conflict, returning the original object.
Returns
-------
xr.Dataset or xr.DataArray
A new object with the time dimension renamed, or the original object
if no changes were necessary.
Raises
------
ValueError
If `raise_on_conflict` is True and a naming conflict occurs.
"""
current = get_time_label(obj)
# No-op condition 1: No time dim or it already has the target name.
if current is None or current == target:
return obj
# No-op condition 2: Collision detected and raise_on_conflict is False.
if (target in obj.dims or target in obj.coords) and target != current:
if raise_on_conflict:
raise ValueError(
f"Target '{target}' exists and differs from detected time dim '{current}'."
)
return obj
# A rename is required; create a new object.
out = obj.rename({current: target})
# Preserve attrs/encoding and set CF-ish hints.
if target in out.coords:
src_coord = obj.coords.get(current)
tcoord = out.coords[target]
attrs = dict(getattr(src_coord, "attrs", {}))
attrs.setdefault("standard_name", "time")
attrs.setdefault("axis", "T")
tcoord.attrs = attrs
if src_coord is not None and hasattr(src_coord, "encoding"):
tcoord.encoding = getattr(src_coord, "encoding", {}).copy()
return out |
|
Hey @mzapponi, can you try out @siligam's suggestion using a We are also curious in the developer team: did you learn how to use Pymor mostly from the workshop (videos, reading, etc), or from the handbook (which still needs improvement...) |
|
So, you would need something like this: ... # From pavan
def use_time_renamer(data, rule):
new_data = relabel_time_dimension_if_required(data, rule)
return new_data |
|
hi @siligam, i get this error (I could not locate the get_time_label function at the beginning and for me it is in pymor.std_lib.dataset_helpers rather than in pymor.core.time_utils, i guess is the same you are referring to): 16:37:02.631 | INFO | Task run 'load_mfdataset-f55' - Finished in state Completed() PS yes, i looked at some of the videos/slides of the workshop and then i just started making some tests to figure out how the tool would work, i think it is pretty intuitive to be honest, but i didn't know about the handbook! |
Co-authored-by: Paul Gierz <pgierz@awi.de>
Co-authored-by: Paul Gierz <pgierz@awi.de>
OpenIFS has the time dimension with a different name, so i added two lines when input files are read (gather_inputs.py) to rename the data time dimension in case it does not match the expected one.