Skip to content

Include filename or path in open_mfdataset #2550

@jsignell

Description

@jsignell

When reading from multiple files, sometimes there is information encoded in the filename. For example in these grib files the time: ./ST4.2018092500.01h, ./ST4.2018092501.01h. It seems like a generally useful thing would be to allow the passing of a kwargs (such as path_as_coord or something) that would define a set of coords with one for the data from each file.

I think the code change would be small:

if path_as_coord:
    ds = ds.assign_coords(path=file_name)

In use it would be like:

>>>xr.open_mfdataset(['./ST4.2018092500.01h', './ST4.2018092501.01h'], engine='pynio', concat_dim='path')
<xarray.Dataset>
Dimensions:               (x: 881, y: 1121, time: 2)
Coordinates:
    lat             (x, y) float32 23.116999 ... 45.618984
    lon             (x, y) float32 -119.023 ... -59.954613
  * path            (path) <U20 './ST4.2018092500.01h' './ST4.2018092501.01h'
Dimensions without coordinates: x, y
Data variables:
    var_0  (time, x, y) float32 dask.array<shape=(2, 881, 1121), chunksize=(1, 881, 1121)>
    var_1   (time, x, y) float32 dask.array<shape=(2, 881, 1121), chunksize=(1, 881, 1121)>

For context I have implemented something similar in dask: dask/dask#3908

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions