Skip to content

TimeSeries decoder to_xarray() leaks datetime dimension across coverages #114

@andreas-grafberger

Description

@andreas-grafberger

What happened?

When a CoverageCollection contains coverages with different forecast dates (e.g. multi-date requests or hindcast requests with hdate), the TimeSeries decoder's to_xarray() produces incorrect xarray datasets. Each dataset gets all forecast dates in its datetime dimension instead of only its own.

For example, requesting 3 dates produces 3 datasets that each have datetime: 3 instead of datetime: 1. Also, all datasets get the first coverage's Forecast date metadata attribute, regardless of which date they actually belong to

The same bug pattern might also affect the Position and VerticalProfile decoders, but I haven't verified that yet.

What are the steps to reproduce the bug?

Here's a small code snippet to reproduce it using earthkit-data and the polytope service:

import earthkit.data

request = {
    "class": "od",
    "stream": "oper",
    "type": "fc",
    "date": "20260414/20260415/20260416",
    "time": "0000",
    "levtype": "sfc",
    "expver": "0001",
    "domain": "g",
    "param": "167",
    "step": "0/to/6",
    "feature": {
        "type": "timeseries",
        "points": [[38, -9.5]],
        "axes": "step",
    },
    "format": "covjson",
}

ds = earthkit.data.from_source(
    "polytope", "ecmwf-mars", request,
    stream=False, address="polytope.ecmwf.int",
)

result = ds.to_xarray()

assert isinstance(result, list), f"Expected list of datasets, got {type(result)}"
assert len(result) == 3, f"Expected 3 datasets, got {len(result)}"

assert list(result[0].datetime.values) == ["2026-04-14T00:00:00Z"], result[0]
assert list(result[1].datetime.values) == ["2026-04-15T00:00:00Z"], result[1]
assert list(result[2].datetime.values) == ["2026-04-16T00:00:00Z"], result[2]

The third xarray dataset looks like this:

 <xarray.Dataset> Size: 496B
 Dimensions:    (latitude: 1, longitude: 1, levelist: 1, number: 1, datetime: 3,
                 t: 7)
 Coordinates:
   * latitude   (latitude) float64 8B 38.0
   * longitude  (longitude) float64 8B 350.4
   * levelist   (levelist) int64 8B 0
   * number     (number) int64 8B 0
   * datetime   (datetime) <U20 240B '2026-04-14T00:00:00Z' ... '2026-04-15T00...
   * t          (t) datetime64[ns] 56B 2026-04-16 ... 2026-04-16T06:00:00
 Data variables:
     2t         (latitude, longitude, levelist, number, datetime, t) float64 168B ...
 Attributes:
     class:          od
     Forecast date:  2026-04-14T00:00:00Z
     domain:         g
     expver:         0001
     levtype:        sfc
     stream:         oper
     type:           fc
     number:         0
     levelist:       0

Version

main / 0.2.16

Platform (OS and architecture)

macos

Relevant log output

Accompanying data

No response

Organisation

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions