Skip to content

Time-dimension in output *.nc-file not read properly by xarray #279

@ghendrickx

Description

@ghendrickx

The issue

When opening the output *.nc-file with xarray, a ValueError is raised as the time-variable cannot be parsed to pandas. Below an MWE:

import xarray as xr

out = xr.open_dataset('/model/directory/aeolis.nc')

The above results in the following ValueError being raised:

ValueError: unable to decode time units "seconds since ['2025-01-01' '00:00:00']" with "calendar 'julian'". Try opening your dataset with decode_times=False or installing cftime if it is not installed.
Raised while decoding variable 'time' with value <xarray.Variable (time: 366)> Size: 3kB
[366 values with dtype=float64]
Attributes:
    long_name:      time
    standard_name:  time
    units:          seconds since ['2025-01-01' '00:00:00']
    calendar:       julian
    axis:           T
    bounds:         time_bounds

The possible solution?

I am not 100% sure how to correct this, but I suspect that it has something to do with the reference date being read as a list of two str-values: "seconds since ['2025-01-01' '00:00:00']". Possibly, a join of the str-values might do the trick:

old_ref_date = ['2025-01-01' '00:00:00']
new_ref_date = ' '.join(old_ref_date)
print(new_ref_date)
>>> '2025-01-01 00:00:00'

The new_ref_date should be parsed properly to the date-handler allowing the decoding of the time-variable.

The work-around

I have found a (temporary) work-around to be able to read the output *.nc-file without the ValueError being raised. As the problem arises when decoding the time-variable, disabling this auto-decoding prevents the ValueError from being raised:

import xarray as xr

out = xr.open_dataset('/model/directory/aeolis.nc', decode_times=False)

The time-variable will be formatted as seconds since instead of dates (and times). As a result, this has to be done afterwards (if this formatting is desired), e.g.:

import datetime

out['time'] = [datetime.datetime(<year>, <month>, <day>, ...) + datetime.timedelta(seconds=t) for t in out['time'].values]

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